blob: 3978ba7a0c6b864c589796d1674cd7ec6243fb2a [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
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskinefc411f12018-10-25 22:34:48 +020014#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
15
Gilles Peskine96ee5c72018-07-12 17:24:54 +020016#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
17
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#else
Gilles Peskine2d277862018-06-18 15:41:12 +020021#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030022#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023
Jaeden Amerof24c7f82018-06-27 17:20:43 +010024/** An invalid export length that will never be set by psa_export_key(). */
25static const size_t INVALID_EXPORT_LENGTH = ~0U;
26
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027/** Test if a buffer contains a constant byte value.
28 *
29 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020030 *
31 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020032 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 * \param size Size of the buffer in bytes.
34 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020035 * \return 1 if the buffer is all-bits-zero.
36 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039{
40 size_t i;
41 for( i = 0; i < size; i++ )
42 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020043 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020046 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020047}
Gilles Peskine818ca122018-06-20 18:16:48 +020048
Gilles Peskine0b352bc2018-06-28 00:16:11 +020049/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
50static int asn1_write_10x( unsigned char **p,
51 unsigned char *start,
52 size_t bits,
53 unsigned char x )
54{
55 int ret;
56 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020057 if( bits == 0 )
58 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
59 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030061 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020062 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
63 *p -= len;
64 ( *p )[len-1] = x;
65 if( bits % 8 == 0 )
66 ( *p )[1] |= 1;
67 else
68 ( *p )[0] |= 1 << ( bits % 8 );
69 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
70 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
71 MBEDTLS_ASN1_INTEGER ) );
72 return( len );
73}
74
75static int construct_fake_rsa_key( unsigned char *buffer,
76 size_t buffer_size,
77 unsigned char **p,
78 size_t bits,
79 int keypair )
80{
81 size_t half_bits = ( bits + 1 ) / 2;
82 int ret;
83 int len = 0;
84 /* Construct something that looks like a DER encoding of
85 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
86 * RSAPrivateKey ::= SEQUENCE {
87 * version Version,
88 * modulus INTEGER, -- n
89 * publicExponent INTEGER, -- e
90 * privateExponent INTEGER, -- d
91 * prime1 INTEGER, -- p
92 * prime2 INTEGER, -- q
93 * exponent1 INTEGER, -- d mod (p-1)
94 * exponent2 INTEGER, -- d mod (q-1)
95 * coefficient INTEGER, -- (inverse of q) mod p
96 * otherPrimeInfos OtherPrimeInfos OPTIONAL
97 * }
98 * Or, for a public key, the same structure with only
99 * version, modulus and publicExponent.
100 */
101 *p = buffer + buffer_size;
102 if( keypair )
103 {
104 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* q */
111 asn1_write_10x( p, buffer, half_bits, 1 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
113 asn1_write_10x( p, buffer, half_bits, 3 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* d */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 }
117 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
118 asn1_write_10x( p, buffer, 17, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* n */
120 asn1_write_10x( p, buffer, bits, 1 ) );
121 if( keypair )
122 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
123 mbedtls_asn1_write_int( p, buffer, 0 ) );
124 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
125 {
126 const unsigned char tag =
127 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
128 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
129 }
130 return( len );
131}
132
Gilles Peskine818ca122018-06-20 18:16:48 +0200133static int exercise_mac_key( psa_key_slot_t key,
134 psa_key_usage_t usage,
135 psa_algorithm_t alg )
136{
137 psa_mac_operation_t operation;
138 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200139 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200140 size_t mac_length = sizeof( mac );
141
142 if( usage & PSA_KEY_USAGE_SIGN )
143 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200144 TEST_ASSERT( psa_mac_sign_setup( &operation,
145 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200146 TEST_ASSERT( psa_mac_update( &operation,
147 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200148 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200149 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200150 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200151 }
152
153 if( usage & PSA_KEY_USAGE_VERIFY )
154 {
155 psa_status_t verify_status =
156 ( usage & PSA_KEY_USAGE_SIGN ?
157 PSA_SUCCESS :
158 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200159 TEST_ASSERT( psa_mac_verify_setup( &operation,
160 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200161 TEST_ASSERT( psa_mac_update( &operation,
162 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200163 TEST_ASSERT( psa_mac_verify_finish( &operation,
164 mac,
165 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200166 }
167
168 return( 1 );
169
170exit:
171 psa_mac_abort( &operation );
172 return( 0 );
173}
174
175static int exercise_cipher_key( psa_key_slot_t key,
176 psa_key_usage_t usage,
177 psa_algorithm_t alg )
178{
179 psa_cipher_operation_t operation;
180 unsigned char iv[16] = {0};
181 size_t iv_length = sizeof( iv );
182 const unsigned char plaintext[16] = "Hello, world...";
183 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
184 size_t ciphertext_length = sizeof( ciphertext );
185 unsigned char decrypted[sizeof( ciphertext )];
186 size_t part_length;
187
188 if( usage & PSA_KEY_USAGE_ENCRYPT )
189 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200190 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
191 key, alg ) == PSA_SUCCESS );
192 TEST_ASSERT( psa_cipher_generate_iv( &operation,
193 iv, sizeof( iv ),
194 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200195 TEST_ASSERT( psa_cipher_update( &operation,
196 plaintext, sizeof( plaintext ),
197 ciphertext, sizeof( ciphertext ),
198 &ciphertext_length ) == PSA_SUCCESS );
199 TEST_ASSERT( psa_cipher_finish( &operation,
200 ciphertext + ciphertext_length,
201 sizeof( ciphertext ) - ciphertext_length,
202 &part_length ) == PSA_SUCCESS );
203 ciphertext_length += part_length;
204 }
205
206 if( usage & PSA_KEY_USAGE_DECRYPT )
207 {
208 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700209 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200210 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
211 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200212 size_t bits;
213 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
214 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
215 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200216 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
217 key, alg ) == PSA_SUCCESS );
218 TEST_ASSERT( psa_cipher_set_iv( &operation,
219 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200220 TEST_ASSERT( psa_cipher_update( &operation,
221 ciphertext, ciphertext_length,
222 decrypted, sizeof( decrypted ),
223 &part_length ) == PSA_SUCCESS );
224 status = psa_cipher_finish( &operation,
225 decrypted + part_length,
226 sizeof( decrypted ) - part_length,
227 &part_length );
228 /* For a stream cipher, all inputs are valid. For a block cipher,
229 * if the input is some aribtrary data rather than an actual
230 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700231 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700232 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200233 TEST_ASSERT( status == PSA_SUCCESS );
234 else
235 TEST_ASSERT( status == PSA_SUCCESS ||
236 status == PSA_ERROR_INVALID_PADDING );
237 }
238
239 return( 1 );
240
241exit:
242 psa_cipher_abort( &operation );
243 return( 0 );
244}
245
246static int exercise_aead_key( psa_key_slot_t key,
247 psa_key_usage_t usage,
248 psa_algorithm_t alg )
249{
250 unsigned char nonce[16] = {0};
251 size_t nonce_length = sizeof( nonce );
252 unsigned char plaintext[16] = "Hello, world...";
253 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
254 size_t ciphertext_length = sizeof( ciphertext );
255 size_t plaintext_length = sizeof( ciphertext );
256
257 if( usage & PSA_KEY_USAGE_ENCRYPT )
258 {
259 TEST_ASSERT( psa_aead_encrypt( key, alg,
260 nonce, nonce_length,
261 NULL, 0,
262 plaintext, sizeof( plaintext ),
263 ciphertext, sizeof( ciphertext ),
264 &ciphertext_length ) == PSA_SUCCESS );
265 }
266
267 if( usage & PSA_KEY_USAGE_DECRYPT )
268 {
269 psa_status_t verify_status =
270 ( usage & PSA_KEY_USAGE_ENCRYPT ?
271 PSA_SUCCESS :
272 PSA_ERROR_INVALID_SIGNATURE );
273 TEST_ASSERT( psa_aead_decrypt( key, alg,
274 nonce, nonce_length,
275 NULL, 0,
276 ciphertext, ciphertext_length,
277 plaintext, sizeof( plaintext ),
278 &plaintext_length ) == verify_status );
279 }
280
281 return( 1 );
282
283exit:
284 return( 0 );
285}
286
287static int exercise_signature_key( psa_key_slot_t key,
288 psa_key_usage_t usage,
289 psa_algorithm_t alg )
290{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200291 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
292 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200293 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200294 size_t signature_length = sizeof( signature );
295
296 if( usage & PSA_KEY_USAGE_SIGN )
297 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200298 /* Some algorithms require the payload to have the size of
299 * the hash encoded in the algorithm. Use this input size
300 * even for algorithms that allow other input sizes. */
301 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
302 if( hash_alg != 0 )
303 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200304 TEST_ASSERT( psa_asymmetric_sign( key, alg,
305 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 signature, sizeof( signature ),
307 &signature_length ) == PSA_SUCCESS );
308 }
309
310 if( usage & PSA_KEY_USAGE_VERIFY )
311 {
312 psa_status_t verify_status =
313 ( usage & PSA_KEY_USAGE_SIGN ?
314 PSA_SUCCESS :
315 PSA_ERROR_INVALID_SIGNATURE );
316 TEST_ASSERT( psa_asymmetric_verify( key, alg,
317 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200318 signature, signature_length ) ==
319 verify_status );
320 }
321
322 return( 1 );
323
324exit:
325 return( 0 );
326}
327
328static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
329 psa_key_usage_t usage,
330 psa_algorithm_t alg )
331{
332 unsigned char plaintext[256] = "Hello, world...";
333 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
334 size_t ciphertext_length = sizeof( ciphertext );
335 size_t plaintext_length = 16;
336
337 if( usage & PSA_KEY_USAGE_ENCRYPT )
338 {
339 TEST_ASSERT(
340 psa_asymmetric_encrypt( key, alg,
341 plaintext, plaintext_length,
342 NULL, 0,
343 ciphertext, sizeof( ciphertext ),
344 &ciphertext_length ) == PSA_SUCCESS );
345 }
346
347 if( usage & PSA_KEY_USAGE_DECRYPT )
348 {
349 psa_status_t status =
350 psa_asymmetric_decrypt( key, alg,
351 ciphertext, ciphertext_length,
352 NULL, 0,
353 plaintext, sizeof( plaintext ),
354 &plaintext_length );
355 TEST_ASSERT( status == PSA_SUCCESS ||
356 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
357 ( status == PSA_ERROR_INVALID_ARGUMENT ||
358 status == PSA_ERROR_INVALID_PADDING ) ) );
359 }
360
361 return( 1 );
362
363exit:
364 return( 0 );
365}
Gilles Peskine02b75072018-07-01 22:31:34 +0200366
Gilles Peskineea0fb492018-07-12 17:17:20 +0200367static int exercise_key_derivation_key( psa_key_slot_t key,
368 psa_key_usage_t usage,
369 psa_algorithm_t alg )
370{
371 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
372 unsigned char label[16] = "This is a label.";
373 size_t label_length = sizeof( label );
374 unsigned char seed[16] = "abcdefghijklmnop";
375 size_t seed_length = sizeof( seed );
376 unsigned char output[1];
377
378 if( usage & PSA_KEY_USAGE_DERIVE )
379 {
380 TEST_ASSERT( psa_key_derivation( &generator,
381 key, alg,
382 label, label_length,
383 seed, seed_length,
384 sizeof( output ) ) == PSA_SUCCESS );
385 TEST_ASSERT( psa_generator_read( &generator,
386 output,
387 sizeof( output ) ) == PSA_SUCCESS );
388 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
389 }
390
391 return( 1 );
392
393exit:
394 return( 0 );
395}
396
Gilles Peskinec7998b72018-11-07 18:45:02 +0100397/* We need two keys to exercise key agreement. Exercise the
398 * private key against its own public key. */
399static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
400 psa_key_type_t key_slot,
401 psa_algorithm_t alg )
402{
403 psa_key_type_t private_key_type;
404 psa_key_type_t public_key_type;
405 size_t key_bits;
406 uint8_t *public_key = NULL;
407 size_t public_key_length;
408 /* Return UNKNOWN_ERROR if something other than the final call to
409 * psa_key_agreement fails. This isn't fully satisfactory, but it's
410 * good enough: callers will report it as a failed test anyway. */
411 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
412
413 TEST_ASSERT( psa_get_key_information( key_slot,
414 &private_key_type,
415 &key_bits ) == PSA_SUCCESS );
416 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
417 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
418 ASSERT_ALLOC( public_key, public_key_length );
419 TEST_ASSERT( public_key != NULL );
420 TEST_ASSERT( psa_export_public_key( key_slot,
421 public_key, public_key_length,
422 &public_key_length ) == PSA_SUCCESS );
423
424 status = psa_key_agreement( generator, key_slot,
425 public_key, public_key_length,
426 alg );
427exit:
428 mbedtls_free( public_key );
429 return( status );
430}
431
Gilles Peskine01d718c2018-09-18 12:01:02 +0200432static int exercise_key_agreement_key( psa_key_slot_t key,
433 psa_key_usage_t usage,
434 psa_algorithm_t alg )
435{
436 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200437 unsigned char output[1];
438 int ok = 0;
439
440 if( usage & PSA_KEY_USAGE_DERIVE )
441 {
442 /* We need two keys to exercise key agreement. Exercise the
443 * private key against its own public key. */
Gilles Peskinec7998b72018-11-07 18:45:02 +0100444 TEST_ASSERT( key_agreement_with_self( &generator, key, alg ) ==
445 PSA_SUCCESS );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200446 TEST_ASSERT( psa_generator_read( &generator,
447 output,
448 sizeof( output ) ) == PSA_SUCCESS );
449 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
450 }
451 ok = 1;
452
453exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200454 return( ok );
455}
456
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200457static int is_oid_of_key_type( psa_key_type_t type,
458 const uint8_t *oid, size_t oid_length )
459{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200460 const uint8_t *expected_oid = NULL;
461 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200462#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200463 if( PSA_KEY_TYPE_IS_RSA( type ) )
464 {
465 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
466 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
467 }
468 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200469#endif /* MBEDTLS_RSA_C */
470#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200471 if( PSA_KEY_TYPE_IS_ECC( type ) )
472 {
473 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
474 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
475 }
476 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200477#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200478 {
479 char message[40];
480 mbedtls_snprintf( message, sizeof( message ),
481 "OID not known for key type=0x%08lx",
482 (unsigned long) type );
483 test_fail( message, __LINE__, __FILE__ );
484 return( 0 );
485 }
486
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200487 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200488 return( 1 );
489
490exit:
491 return( 0 );
492}
493
494static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
495 size_t min_bits, size_t max_bits,
496 int must_be_odd )
497{
498 size_t len;
499 size_t actual_bits;
500 unsigned char msb;
501 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
502 MBEDTLS_ASN1_INTEGER ) == 0 );
503 /* Tolerate a slight departure from DER encoding:
504 * - 0 may be represented by an empty string or a 1-byte string.
505 * - The sign bit may be used as a value bit. */
506 if( ( len == 1 && ( *p )[0] == 0 ) ||
507 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
508 {
509 ++( *p );
510 --len;
511 }
512 if( min_bits == 0 && len == 0 )
513 return( 1 );
514 msb = ( *p )[0];
515 TEST_ASSERT( msb != 0 );
516 actual_bits = 8 * ( len - 1 );
517 while( msb != 0 )
518 {
519 msb >>= 1;
520 ++actual_bits;
521 }
522 TEST_ASSERT( actual_bits >= min_bits );
523 TEST_ASSERT( actual_bits <= max_bits );
524 if( must_be_odd )
525 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
526 *p += len;
527 return( 1 );
528exit:
529 return( 0 );
530}
531
532static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
533 size_t *len,
534 unsigned char n, unsigned char tag )
535{
536 int ret;
537 ret = mbedtls_asn1_get_tag( p, end, len,
538 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
539 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
540 if( ret != 0 )
541 return( ret );
542 end = *p + *len;
543 ret = mbedtls_asn1_get_tag( p, end, len, tag );
544 if( ret != 0 )
545 return( ret );
546 if( *p + *len != end )
547 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
548 return( 0 );
549}
550
551static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
552 uint8_t *exported, size_t exported_length )
553{
554 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200555 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200556 else
557 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200558
559#if defined(MBEDTLS_DES_C)
560 if( type == PSA_KEY_TYPE_DES )
561 {
562 /* Check the parity bits. */
563 unsigned i;
564 for( i = 0; i < bits / 8; i++ )
565 {
566 unsigned bit_count = 0;
567 unsigned m;
568 for( m = 1; m <= 0x100; m <<= 1 )
569 {
570 if( exported[i] & m )
571 ++bit_count;
572 }
573 TEST_ASSERT( bit_count % 2 != 0 );
574 }
575 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200576 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200577#endif
578
579#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
580 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
581 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200582 uint8_t *p = exported;
583 uint8_t *end = exported + exported_length;
584 size_t len;
585 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200586 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200587 * modulus INTEGER, -- n
588 * publicExponent INTEGER, -- e
589 * privateExponent INTEGER, -- d
590 * prime1 INTEGER, -- p
591 * prime2 INTEGER, -- q
592 * exponent1 INTEGER, -- d mod (p-1)
593 * exponent2 INTEGER, -- d mod (q-1)
594 * coefficient INTEGER, -- (inverse of q) mod p
595 * }
596 */
597 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
598 MBEDTLS_ASN1_SEQUENCE |
599 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
600 TEST_ASSERT( p + len == end );
601 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
604 goto exit;
605 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
606 goto exit;
607 /* Require d to be at least half the size of n. */
608 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
609 goto exit;
610 /* Require p and q to be at most half the size of n, rounded up. */
611 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
612 goto exit;
613 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
614 goto exit;
615 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
616 goto exit;
617 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
618 goto exit;
619 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
620 goto exit;
621 TEST_ASSERT( p == end );
622 }
623 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200624#endif /* MBEDTLS_RSA_C */
625
626#if defined(MBEDTLS_ECP_C)
627 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
628 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100629 /* Just the secret value */
630 TEST_ASSERT( exported_length == PSA_BITS_TO_BYTES( bits ) );
631 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200632 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200633#endif /* MBEDTLS_ECP_C */
634
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200635 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
636 {
637 uint8_t *p = exported;
638 uint8_t *end = exported + exported_length;
639 size_t len;
640 mbedtls_asn1_buf alg;
641 mbedtls_asn1_buf params;
642 mbedtls_asn1_bitstring bitstring;
643 /* SubjectPublicKeyInfo ::= SEQUENCE {
644 * algorithm AlgorithmIdentifier,
645 * subjectPublicKey BIT STRING }
646 * AlgorithmIdentifier ::= SEQUENCE {
647 * algorithm OBJECT IDENTIFIER,
648 * parameters ANY DEFINED BY algorithm OPTIONAL }
649 */
650 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
651 MBEDTLS_ASN1_SEQUENCE |
652 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
653 TEST_ASSERT( p + len == end );
654 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
655 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
656 goto exit;
657 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
658 TEST_ASSERT( p == end );
659 p = bitstring.p;
660#if defined(MBEDTLS_RSA_C)
661 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
662 {
663 /* RSAPublicKey ::= SEQUENCE {
664 * modulus INTEGER, -- n
665 * publicExponent INTEGER } -- e
666 */
667 TEST_ASSERT( bitstring.unused_bits == 0 );
668 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
669 MBEDTLS_ASN1_SEQUENCE |
670 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
671 TEST_ASSERT( p + len == end );
672 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
673 goto exit;
674 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
675 goto exit;
676 TEST_ASSERT( p == end );
677 }
678 else
679#endif /* MBEDTLS_RSA_C */
680#if defined(MBEDTLS_ECP_C)
681 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
682 {
683 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200684 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200685 * -- then x_P as an n-bit string, big endian;
686 * -- then y_P as a n-bit string, big endian,
687 * -- where n is the order of the curve.
688 */
689 TEST_ASSERT( bitstring.unused_bits == 0 );
690 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
691 TEST_ASSERT( p[0] == 4 );
692 }
693 else
694#endif /* MBEDTLS_ECP_C */
695 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100696 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200697 mbedtls_snprintf( message, sizeof( message ),
698 "No sanity check for public key type=0x%08lx",
699 (unsigned long) type );
700 test_fail( message, __LINE__, __FILE__ );
701 return( 0 );
702 }
703 }
704 else
705
706 {
707 /* No sanity checks for other types */
708 }
709
710 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200711
712exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200713 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200714}
715
716static int exercise_export_key( psa_key_slot_t slot,
717 psa_key_usage_t usage )
718{
719 psa_key_type_t type;
720 size_t bits;
721 uint8_t *exported = NULL;
722 size_t exported_size = 0;
723 size_t exported_length = 0;
724 int ok = 0;
725
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200726 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
727
728 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
729 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200730 {
731 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
732 PSA_ERROR_NOT_PERMITTED );
733 return( 1 );
734 }
735
Gilles Peskined14664a2018-08-10 19:07:32 +0200736 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200737 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200738
739 TEST_ASSERT( psa_export_key( slot,
740 exported, exported_size,
741 &exported_length ) == PSA_SUCCESS );
742 ok = exported_key_sanity_check( type, bits, exported, exported_length );
743
744exit:
745 mbedtls_free( exported );
746 return( ok );
747}
748
749static int exercise_export_public_key( psa_key_slot_t slot )
750{
751 psa_key_type_t type;
752 psa_key_type_t public_type;
753 size_t bits;
754 uint8_t *exported = NULL;
755 size_t exported_size = 0;
756 size_t exported_length = 0;
757 int ok = 0;
758
759 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
760 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
761 {
762 TEST_ASSERT( psa_export_public_key( slot,
763 NULL, 0, &exported_length ) ==
764 PSA_ERROR_INVALID_ARGUMENT );
765 return( 1 );
766 }
767
768 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
769 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200770 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200771
772 TEST_ASSERT( psa_export_public_key( slot,
773 exported, exported_size,
774 &exported_length ) == PSA_SUCCESS );
775 ok = exported_key_sanity_check( public_type, bits,
776 exported, exported_length );
777
778exit:
779 mbedtls_free( exported );
780 return( ok );
781}
782
Gilles Peskine02b75072018-07-01 22:31:34 +0200783static int exercise_key( psa_key_slot_t slot,
784 psa_key_usage_t usage,
785 psa_algorithm_t alg )
786{
787 int ok;
788 if( alg == 0 )
789 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
790 else if( PSA_ALG_IS_MAC( alg ) )
791 ok = exercise_mac_key( slot, usage, alg );
792 else if( PSA_ALG_IS_CIPHER( alg ) )
793 ok = exercise_cipher_key( slot, usage, alg );
794 else if( PSA_ALG_IS_AEAD( alg ) )
795 ok = exercise_aead_key( slot, usage, alg );
796 else if( PSA_ALG_IS_SIGN( alg ) )
797 ok = exercise_signature_key( slot, usage, alg );
798 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
799 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200800 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
801 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200802 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
803 ok = exercise_key_agreement_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200804 else
805 {
806 char message[40];
807 mbedtls_snprintf( message, sizeof( message ),
808 "No code to exercise alg=0x%08lx",
809 (unsigned long) alg );
810 test_fail( message, __LINE__, __FILE__ );
811 ok = 0;
812 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200813
814 ok = ok && exercise_export_key( slot, usage );
815 ok = ok && exercise_export_public_key( slot );
816
Gilles Peskine02b75072018-07-01 22:31:34 +0200817 return( ok );
818}
819
Gilles Peskine10df3412018-10-25 22:35:43 +0200820static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
821 psa_algorithm_t alg )
822{
823 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
824 {
825 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
826 PSA_KEY_USAGE_VERIFY :
827 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
828 }
829 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
830 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
831 {
832 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
833 PSA_KEY_USAGE_ENCRYPT :
834 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
835 }
836 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
837 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
838 {
839 return( PSA_KEY_USAGE_DERIVE );
840 }
841 else
842 {
843 return( 0 );
844 }
845
846}
Darryl Green0c6575a2018-11-07 16:05:30 +0000847
848typedef enum {
849 IMPORT_KEY = 0,
850 GENERATE_KEY = 1,
851 DERIVE_KEY = 2
852} generate_method;
853
Gilles Peskinee59236f2018-01-27 23:32:46 +0100854/* END_HEADER */
855
856/* BEGIN_DEPENDENCIES
857 * depends_on:MBEDTLS_PSA_CRYPTO_C
858 * END_DEPENDENCIES
859 */
860
861/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200862void static_checks( )
863{
864 size_t max_truncated_mac_size =
865 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
866
867 /* Check that the length for a truncated MAC always fits in the algorithm
868 * encoding. The shifted mask is the maximum truncated value. The
869 * untruncated algorithm may be one byte larger. */
870 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
871}
872/* END_CASE */
873
874/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200875void fill_slots( int max_arg )
876{
877 /* Fill all the slots until we run out of memory or out of slots,
878 * or until some limit specified in the test data for the sake of
879 * implementations with an essentially unlimited number of slots.
880 * This test assumes that available slots are numbered from 1. */
881
882 psa_key_slot_t slot;
883 psa_key_slot_t max = 0;
884 psa_key_policy_t policy;
885 uint8_t exported[sizeof( max )];
886 size_t exported_size;
887 psa_status_t status;
888
889 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
890
891 psa_key_policy_init( &policy );
892 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
893
894 for( max = 1; max <= (size_t) max_arg; max++ )
895 {
896 status = psa_set_key_policy( max, &policy );
897 /* Stop filling slots if we run out of memory or out of
898 * available slots. */
899 TEST_ASSERT( status == PSA_SUCCESS ||
900 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
901 status == PSA_ERROR_INVALID_ARGUMENT );
902 if( status != PSA_SUCCESS )
903 break;
904 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
905 (uint8_t*) &max, sizeof( max ) );
906 /* Since psa_set_key_policy succeeded, we know that the slot
907 * number is valid. But we may legitimately run out of memory. */
908 TEST_ASSERT( status == PSA_SUCCESS ||
909 status == PSA_ERROR_INSUFFICIENT_MEMORY );
910 if( status != PSA_SUCCESS )
911 break;
912 }
913 /* `max` is now the first slot number that wasn't filled. */
914 max -= 1;
915
916 for( slot = 1; slot <= max; slot++ )
917 {
918 TEST_ASSERT( psa_export_key( slot,
919 exported, sizeof( exported ),
920 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200921 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200922 }
923
924exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200925 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200926 mbedtls_psa_crypto_free( );
927}
928/* END_CASE */
929
930/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200931void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100932{
933 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200934 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100935 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100936
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100937 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300938 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
940
Gilles Peskine4abf7412018-06-18 16:35:34 +0200941 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200942 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943 if( status == PSA_SUCCESS )
944 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
945
946exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947 mbedtls_psa_crypto_free( );
948}
949/* END_CASE */
950
951/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100952void import_twice( int alg_arg, int usage_arg,
953 int type1_arg, data_t *data1,
954 int expected_import1_status_arg,
955 int type2_arg, data_t *data2,
956 int expected_import2_status_arg )
957{
958 int slot = 1;
959 psa_algorithm_t alg = alg_arg;
960 psa_key_usage_t usage = usage_arg;
961 psa_key_type_t type1 = type1_arg;
962 psa_status_t expected_import1_status = expected_import1_status_arg;
963 psa_key_type_t type2 = type2_arg;
964 psa_status_t expected_import2_status = expected_import2_status_arg;
965 psa_key_policy_t policy;
966 psa_status_t status;
967
968 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
969
970 psa_key_policy_init( &policy );
971 psa_key_policy_set_usage( &policy, usage, alg );
972 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
973
974 status = psa_import_key( slot, type1, data1->x, data1->len );
975 TEST_ASSERT( status == expected_import1_status );
976 status = psa_import_key( slot, type2, data2->x, data2->len );
977 TEST_ASSERT( status == expected_import2_status );
978
979 if( expected_import1_status == PSA_SUCCESS ||
980 expected_import2_status == PSA_SUCCESS )
981 {
982 TEST_ASSERT( exercise_key( slot, usage, alg ) );
983 }
984
985exit:
986 mbedtls_psa_crypto_free( );
987}
988/* END_CASE */
989
990/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200991void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
992{
993 int slot = 1;
994 size_t bits = bits_arg;
995 psa_status_t expected_status = expected_status_arg;
996 psa_status_t status;
997 psa_key_type_t type =
998 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
999 size_t buffer_size = /* Slight overapproximations */
1000 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001001 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001002 unsigned char *p;
1003 int ret;
1004 size_t length;
1005
1006 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001007 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001008
1009 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1010 bits, keypair ) ) >= 0 );
1011 length = ret;
1012
1013 /* Try importing the key */
1014 status = psa_import_key( slot, type, p, length );
1015 TEST_ASSERT( status == expected_status );
1016 if( status == PSA_SUCCESS )
1017 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1018
1019exit:
1020 mbedtls_free( buffer );
1021 mbedtls_psa_crypto_free( );
1022}
1023/* END_CASE */
1024
1025/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001026void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001027 int type_arg,
1028 int alg_arg,
1029 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001030 int expected_bits,
1031 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001032 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033 int canonical_input )
1034{
1035 int slot = 1;
1036 int slot2 = slot + 1;
1037 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001038 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001039 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001041 unsigned char *exported = NULL;
1042 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001043 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001044 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001045 size_t reexported_length;
1046 psa_key_type_t got_type;
1047 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001048 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001050 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001051 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001052 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001053 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001054 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001055 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001056 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1057
mohammad1603a97cb8c2018-03-28 03:46:26 -07001058 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001059 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001060 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1061
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062 /* Import the key */
1063 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001064 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001065
1066 /* Test the key information */
1067 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001068 &got_type,
1069 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070 TEST_ASSERT( got_type == type );
1071 TEST_ASSERT( got_bits == (size_t) expected_bits );
1072
1073 /* Export the key */
1074 status = psa_export_key( slot,
1075 exported, export_size,
1076 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001077 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001078
1079 /* The exported length must be set by psa_export_key() to a value between 0
1080 * and export_size. On errors, the exported length must be 0. */
1081 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1082 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1083 TEST_ASSERT( exported_length <= export_size );
1084
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001085 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001086 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001088 {
1089 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001090 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001091 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001092
Gilles Peskine8f609232018-08-11 01:24:55 +02001093 if( ! exercise_export_key( slot, usage_arg ) )
1094 goto exit;
1095
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001096 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001097 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098 else
1099 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001100 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1101
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001102 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001103 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001104 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001105 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001106 reexported,
1107 export_size,
1108 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001109 ASSERT_COMPARE( exported, exported_length,
1110 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001111 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001112 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001113
1114destroy:
1115 /* Destroy the key */
1116 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1117 TEST_ASSERT( psa_get_key_information(
1118 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1119
1120exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001121 mbedtls_free( exported );
1122 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001123 mbedtls_psa_crypto_free( );
1124}
1125/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001126
Moran Pekerf709f4a2018-06-06 17:26:04 +03001127/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001128void import_key_nonempty_slot( )
1129{
1130 int slot = 1;
1131 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1132 psa_status_t status;
1133 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
1134 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1135
1136 /* Import the key */
1137 TEST_ASSERT( psa_import_key( slot, type,
1138 data, sizeof( data ) ) == PSA_SUCCESS );
1139
1140 /* Import the key again */
1141 status = psa_import_key( slot, type, data, sizeof( data ) );
1142 TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT );
1143
1144exit:
1145 mbedtls_psa_crypto_free( );
1146}
1147/* END_CASE */
1148
1149/* BEGIN_CASE */
1150void export_invalid_slot( int slot, int expected_export_status_arg )
1151{
1152 psa_status_t status;
1153 unsigned char *exported = NULL;
1154 size_t export_size = 0;
1155 size_t exported_length = INVALID_EXPORT_LENGTH;
1156 psa_status_t expected_export_status = expected_export_status_arg;
1157
1158 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1159
1160 /* Export the key */
1161 status = psa_export_key( slot,
1162 exported, export_size,
1163 &exported_length );
1164 TEST_ASSERT( status == expected_export_status );
1165
1166exit:
1167 mbedtls_psa_crypto_free( );
1168}
1169/* END_CASE */
1170
1171/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001172void export_with_no_key_activity( )
1173{
1174 int slot = 1;
1175 psa_algorithm_t alg = PSA_ALG_CTR;
1176 psa_status_t status;
1177 psa_key_policy_t policy;
1178 unsigned char *exported = NULL;
1179 size_t export_size = 0;
1180 size_t exported_length = INVALID_EXPORT_LENGTH;
1181
1182 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1183
1184 psa_key_policy_init( &policy );
1185 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1186 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1187
1188 /* Export the key */
1189 status = psa_export_key( slot,
1190 exported, export_size,
1191 &exported_length );
1192 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1193
1194exit:
1195 mbedtls_psa_crypto_free( );
1196}
1197/* END_CASE */
1198
1199/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001200void cipher_with_no_key_activity( )
1201{
1202 int slot = 1;
1203 psa_status_t status;
1204 psa_key_policy_t policy;
1205 psa_cipher_operation_t operation;
1206 int exercise_alg = PSA_ALG_CTR;
1207
1208 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1209
1210 psa_key_policy_init( &policy );
1211 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
1212 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1213
1214 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1215 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1216
1217exit:
1218 psa_cipher_abort( &operation );
1219 mbedtls_psa_crypto_free( );
1220}
1221/* END_CASE */
1222
1223/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001224void export_after_import_failure( data_t *data, int type_arg,
1225 int expected_import_status_arg )
1226{
1227 int slot = 1;
1228 psa_key_type_t type = type_arg;
1229 psa_status_t status;
1230 unsigned char *exported = NULL;
1231 size_t export_size = 0;
1232 psa_status_t expected_import_status = expected_import_status_arg;
1233 size_t exported_length = INVALID_EXPORT_LENGTH;
1234
1235 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1236
1237 /* Import the key - expect failure */
1238 status = psa_import_key( slot, type,
1239 data->x, data->len );
1240 TEST_ASSERT( status == expected_import_status );
1241
1242 /* Export the key */
1243 status = psa_export_key( slot,
1244 exported, export_size,
1245 &exported_length );
1246 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1247
1248exit:
1249 mbedtls_psa_crypto_free( );
1250}
1251/* END_CASE */
1252
1253/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001254void cipher_after_import_failure( data_t *data, int type_arg,
1255 int expected_import_status_arg )
1256{
1257 int slot = 1;
1258 psa_cipher_operation_t operation;
1259 psa_key_type_t type = type_arg;
1260 psa_status_t status;
1261 psa_status_t expected_import_status = expected_import_status_arg;
1262 int exercise_alg = PSA_ALG_CTR;
1263
1264 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1265
1266 /* Import the key - expect failure */
1267 status = psa_import_key( slot, type,
1268 data->x, data->len );
1269 TEST_ASSERT( status == expected_import_status );
1270
1271 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1272 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1273
1274exit:
1275 psa_cipher_abort( &operation );
1276 mbedtls_psa_crypto_free( );
1277}
1278/* END_CASE */
1279
1280/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001281void export_after_destroy_key( data_t *data, int type_arg )
1282{
1283 int slot = 1;
1284 psa_key_type_t type = type_arg;
1285 psa_status_t status;
1286 psa_key_policy_t policy;
1287 psa_algorithm_t alg = PSA_ALG_CTR;
1288 unsigned char *exported = NULL;
1289 size_t export_size = 0;
1290 size_t exported_length = INVALID_EXPORT_LENGTH;
1291
1292 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1293
1294 psa_key_policy_init( &policy );
1295 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1296 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1297 export_size = (ptrdiff_t) data->len;
1298 ASSERT_ALLOC( exported, export_size );
1299
1300 /* Import the key */
1301 TEST_ASSERT( psa_import_key( slot, type,
1302 data->x, data->len ) == PSA_SUCCESS );
1303
1304 TEST_ASSERT( psa_export_key( slot, exported, export_size,
1305 &exported_length ) == PSA_SUCCESS );
1306
1307 /* Destroy the key */
1308 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1309
1310 /* Export the key */
1311 status = psa_export_key( slot, exported, export_size,
1312 &exported_length );
1313 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1314
1315exit:
1316 mbedtls_free( exported );
1317 mbedtls_psa_crypto_free( );
1318}
1319/* END_CASE */
1320
1321/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001322void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001323 int type_arg,
1324 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001325 int export_size_delta,
1326 int expected_export_status_arg,
1327 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001328{
1329 int slot = 1;
1330 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001331 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001332 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001333 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001334 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001335 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001336 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001337 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001338
Moran Pekerf709f4a2018-06-06 17:26:04 +03001339 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1340
1341 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001342 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001343 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1344
1345 /* Import the key */
1346 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001347 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001348
Gilles Peskine49c25912018-10-29 15:15:31 +01001349 /* Export the public key */
1350 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001351 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001352 exported, export_size,
1353 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001354 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001355 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001356 {
1357 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1358 size_t bits;
1359 TEST_ASSERT( psa_get_key_information( slot, NULL, &bits ) ==
1360 PSA_SUCCESS );
1361 TEST_ASSERT( expected_public_key->len <=
1362 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001363 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1364 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001365 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001366
1367exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001368 mbedtls_free( exported );
Gilles Peskine49c25912018-10-29 15:15:31 +01001369 psa_destroy_key( slot );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001370 mbedtls_psa_crypto_free( );
1371}
1372/* END_CASE */
1373
Gilles Peskine20035e32018-02-03 22:44:14 +01001374/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001375void import_and_exercise_key( data_t *data,
1376 int type_arg,
1377 int bits_arg,
1378 int alg_arg )
1379{
1380 int slot = 1;
1381 psa_key_type_t type = type_arg;
1382 size_t bits = bits_arg;
1383 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001384 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001385 psa_key_policy_t policy;
1386 psa_key_type_t got_type;
1387 size_t got_bits;
1388 psa_status_t status;
1389
1390 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1391
1392 psa_key_policy_init( &policy );
1393 psa_key_policy_set_usage( &policy, usage, alg );
1394 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1395
1396 /* Import the key */
1397 status = psa_import_key( slot, type, data->x, data->len );
1398 TEST_ASSERT( status == PSA_SUCCESS );
1399
1400 /* Test the key information */
1401 TEST_ASSERT( psa_get_key_information( slot,
1402 &got_type,
1403 &got_bits ) == PSA_SUCCESS );
1404 TEST_ASSERT( got_type == type );
1405 TEST_ASSERT( got_bits == bits );
1406
1407 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001408 if( ! exercise_key( slot, usage, alg ) )
1409 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001410
1411exit:
1412 psa_destroy_key( slot );
1413 mbedtls_psa_crypto_free( );
1414}
1415/* END_CASE */
1416
1417/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001418void key_policy( int usage_arg, int alg_arg )
1419{
1420 int key_slot = 1;
1421 psa_algorithm_t alg = alg_arg;
1422 psa_key_usage_t usage = usage_arg;
1423 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1424 unsigned char key[32] = {0};
1425 psa_key_policy_t policy_set;
1426 psa_key_policy_t policy_get;
1427
1428 memset( key, 0x2a, sizeof( key ) );
1429
1430 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1431
1432 psa_key_policy_init( &policy_set );
1433 psa_key_policy_init( &policy_get );
1434
1435 psa_key_policy_set_usage( &policy_set, usage, alg );
1436
1437 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1438 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1439 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1440
1441 TEST_ASSERT( psa_import_key( key_slot, key_type,
1442 key, sizeof( key ) ) == PSA_SUCCESS );
1443
1444 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1445
1446 TEST_ASSERT( policy_get.usage == policy_set.usage );
1447 TEST_ASSERT( policy_get.alg == policy_set.alg );
1448
1449exit:
1450 psa_destroy_key( key_slot );
1451 mbedtls_psa_crypto_free( );
1452}
1453/* END_CASE */
1454
1455/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001456void mac_key_policy( int policy_usage,
1457 int policy_alg,
1458 int key_type,
1459 data_t *key_data,
1460 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001461{
1462 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001463 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001464 psa_mac_operation_t operation;
1465 psa_status_t status;
1466 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001467
1468 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1469
1470 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001471 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001472 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1473
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001474 TEST_ASSERT( psa_import_key( key_slot, key_type,
1475 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001476
Gilles Peskine89167cb2018-07-08 20:12:23 +02001477 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001478 if( policy_alg == exercise_alg &&
1479 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1480 TEST_ASSERT( status == PSA_SUCCESS );
1481 else
1482 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1483 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001484
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001485 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001486 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001487 if( policy_alg == exercise_alg &&
1488 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001489 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001490 else
1491 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1492
1493exit:
1494 psa_mac_abort( &operation );
1495 psa_destroy_key( key_slot );
1496 mbedtls_psa_crypto_free( );
1497}
1498/* END_CASE */
1499
1500/* BEGIN_CASE */
1501void cipher_key_policy( int policy_usage,
1502 int policy_alg,
1503 int key_type,
1504 data_t *key_data,
1505 int exercise_alg )
1506{
1507 int key_slot = 1;
1508 psa_key_policy_t policy;
1509 psa_cipher_operation_t operation;
1510 psa_status_t status;
1511
1512 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1513
1514 psa_key_policy_init( &policy );
1515 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1516 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1517
1518 TEST_ASSERT( psa_import_key( key_slot, key_type,
1519 key_data->x, key_data->len ) == PSA_SUCCESS );
1520
Gilles Peskinefe119512018-07-08 21:39:34 +02001521 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001522 if( policy_alg == exercise_alg &&
1523 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1524 TEST_ASSERT( status == PSA_SUCCESS );
1525 else
1526 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1527 psa_cipher_abort( &operation );
1528
Gilles Peskinefe119512018-07-08 21:39:34 +02001529 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001530 if( policy_alg == exercise_alg &&
1531 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1532 TEST_ASSERT( status == PSA_SUCCESS );
1533 else
1534 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1535
1536exit:
1537 psa_cipher_abort( &operation );
1538 psa_destroy_key( key_slot );
1539 mbedtls_psa_crypto_free( );
1540}
1541/* END_CASE */
1542
1543/* BEGIN_CASE */
1544void aead_key_policy( int policy_usage,
1545 int policy_alg,
1546 int key_type,
1547 data_t *key_data,
1548 int nonce_length_arg,
1549 int tag_length_arg,
1550 int exercise_alg )
1551{
1552 int key_slot = 1;
1553 psa_key_policy_t policy;
1554 psa_status_t status;
1555 unsigned char nonce[16] = {0};
1556 size_t nonce_length = nonce_length_arg;
1557 unsigned char tag[16];
1558 size_t tag_length = tag_length_arg;
1559 size_t output_length;
1560
1561 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1562 TEST_ASSERT( tag_length <= sizeof( tag ) );
1563
1564 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1565
1566 psa_key_policy_init( &policy );
1567 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1568 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1569
1570 TEST_ASSERT( psa_import_key( key_slot, key_type,
1571 key_data->x, key_data->len ) == PSA_SUCCESS );
1572
1573 status = psa_aead_encrypt( key_slot, exercise_alg,
1574 nonce, nonce_length,
1575 NULL, 0,
1576 NULL, 0,
1577 tag, tag_length,
1578 &output_length );
1579 if( policy_alg == exercise_alg &&
1580 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1581 TEST_ASSERT( status == PSA_SUCCESS );
1582 else
1583 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1584
1585 memset( tag, 0, sizeof( tag ) );
1586 status = psa_aead_decrypt( key_slot, exercise_alg,
1587 nonce, nonce_length,
1588 NULL, 0,
1589 tag, tag_length,
1590 NULL, 0,
1591 &output_length );
1592 if( policy_alg == exercise_alg &&
1593 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1594 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1595 else
1596 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1597
1598exit:
1599 psa_destroy_key( key_slot );
1600 mbedtls_psa_crypto_free( );
1601}
1602/* END_CASE */
1603
1604/* BEGIN_CASE */
1605void asymmetric_encryption_key_policy( int policy_usage,
1606 int policy_alg,
1607 int key_type,
1608 data_t *key_data,
1609 int exercise_alg )
1610{
1611 int key_slot = 1;
1612 psa_key_policy_t policy;
1613 psa_status_t status;
1614 size_t key_bits;
1615 size_t buffer_length;
1616 unsigned char *buffer = NULL;
1617 size_t output_length;
1618
1619 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1620
1621 psa_key_policy_init( &policy );
1622 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1623 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1624
1625 TEST_ASSERT( psa_import_key( key_slot, key_type,
1626 key_data->x, key_data->len ) == PSA_SUCCESS );
1627
1628 TEST_ASSERT( psa_get_key_information( key_slot,
1629 NULL,
1630 &key_bits ) == PSA_SUCCESS );
1631 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1632 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001633 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001634
1635 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1636 NULL, 0,
1637 NULL, 0,
1638 buffer, buffer_length,
1639 &output_length );
1640 if( policy_alg == exercise_alg &&
1641 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1642 TEST_ASSERT( status == PSA_SUCCESS );
1643 else
1644 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1645
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001646 if( buffer_length != 0 )
1647 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001648 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1649 buffer, buffer_length,
1650 NULL, 0,
1651 buffer, buffer_length,
1652 &output_length );
1653 if( policy_alg == exercise_alg &&
1654 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1655 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1656 else
1657 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1658
1659exit:
1660 psa_destroy_key( key_slot );
1661 mbedtls_psa_crypto_free( );
1662 mbedtls_free( buffer );
1663}
1664/* END_CASE */
1665
1666/* BEGIN_CASE */
1667void asymmetric_signature_key_policy( int policy_usage,
1668 int policy_alg,
1669 int key_type,
1670 data_t *key_data,
1671 int exercise_alg )
1672{
1673 int key_slot = 1;
1674 psa_key_policy_t policy;
1675 psa_status_t status;
1676 unsigned char payload[16] = {1};
1677 size_t payload_length = sizeof( payload );
1678 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1679 size_t signature_length;
1680
1681 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1682
1683 psa_key_policy_init( &policy );
1684 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1685 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1686
1687 TEST_ASSERT( psa_import_key( key_slot, key_type,
1688 key_data->x, key_data->len ) == PSA_SUCCESS );
1689
1690 status = psa_asymmetric_sign( key_slot, exercise_alg,
1691 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001692 signature, sizeof( signature ),
1693 &signature_length );
1694 if( policy_alg == exercise_alg &&
1695 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1696 TEST_ASSERT( status == PSA_SUCCESS );
1697 else
1698 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1699
1700 memset( signature, 0, sizeof( signature ) );
1701 status = psa_asymmetric_verify( key_slot, exercise_alg,
1702 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703 signature, sizeof( signature ) );
1704 if( policy_alg == exercise_alg &&
1705 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1706 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1707 else
1708 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001709
1710exit:
1711 psa_destroy_key( key_slot );
1712 mbedtls_psa_crypto_free( );
1713}
1714/* END_CASE */
1715
1716/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001717void derive_key_policy( int policy_usage,
1718 int policy_alg,
1719 int key_type,
1720 data_t *key_data,
1721 int exercise_alg )
1722{
1723 int key_slot = 1;
1724 psa_key_policy_t policy;
1725 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1726 psa_status_t status;
1727
1728 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1729
1730 psa_key_policy_init( &policy );
1731 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1732 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1733
1734 TEST_ASSERT( psa_import_key( key_slot, key_type,
1735 key_data->x, key_data->len ) == PSA_SUCCESS );
1736
1737 status = psa_key_derivation( &generator, key_slot,
1738 exercise_alg,
1739 NULL, 0,
1740 NULL, 0,
1741 1 );
1742 if( policy_alg == exercise_alg &&
1743 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1744 TEST_ASSERT( status == PSA_SUCCESS );
1745 else
1746 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1747
1748exit:
1749 psa_generator_abort( &generator );
1750 psa_destroy_key( key_slot );
1751 mbedtls_psa_crypto_free( );
1752}
1753/* END_CASE */
1754
1755/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001756void agreement_key_policy( int policy_usage,
1757 int policy_alg,
1758 int key_type_arg,
1759 data_t *key_data,
1760 int exercise_alg )
1761{
1762 int key_slot = 1;
1763 psa_key_policy_t policy;
1764 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001765 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1766 psa_status_t status;
1767
1768 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1769
1770 psa_key_policy_init( &policy );
1771 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1772 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1773
1774 TEST_ASSERT( psa_import_key( key_slot, key_type,
1775 key_data->x, key_data->len ) == PSA_SUCCESS );
1776
Gilles Peskinec7998b72018-11-07 18:45:02 +01001777 status = key_agreement_with_self( &generator, key_slot, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001778
Gilles Peskine01d718c2018-09-18 12:01:02 +02001779 if( policy_alg == exercise_alg &&
1780 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1781 TEST_ASSERT( status == PSA_SUCCESS );
1782 else
1783 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1784
1785exit:
1786 psa_generator_abort( &generator );
1787 psa_destroy_key( key_slot );
1788 mbedtls_psa_crypto_free( );
1789}
1790/* END_CASE */
1791
1792/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001793void key_lifetime( int lifetime_arg )
1794{
1795 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001796 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001797 unsigned char key[32] = {0};
1798 psa_key_lifetime_t lifetime_set = lifetime_arg;
1799 psa_key_lifetime_t lifetime_get;
1800
1801 memset( key, 0x2a, sizeof( key ) );
1802
1803 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1804
1805 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1806 lifetime_set ) == PSA_SUCCESS );
1807
1808 TEST_ASSERT( psa_import_key( key_slot, key_type,
1809 key, sizeof( key ) ) == PSA_SUCCESS );
1810
1811 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1812 &lifetime_get ) == PSA_SUCCESS );
1813
1814 TEST_ASSERT( lifetime_get == lifetime_set );
1815
1816exit:
1817 psa_destroy_key( key_slot );
1818 mbedtls_psa_crypto_free( );
1819}
1820/* END_CASE */
1821
1822/* BEGIN_CASE */
1823void key_lifetime_set_fail( int key_slot_arg,
1824 int lifetime_arg,
1825 int expected_status_arg )
1826{
1827 psa_key_slot_t key_slot = key_slot_arg;
1828 psa_key_lifetime_t lifetime_set = lifetime_arg;
1829 psa_status_t actual_status;
1830 psa_status_t expected_status = expected_status_arg;
1831
1832 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1833
1834 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1835
1836 if( actual_status == PSA_SUCCESS )
1837 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1838
1839 TEST_ASSERT( expected_status == actual_status );
1840
1841exit:
1842 psa_destroy_key( key_slot );
1843 mbedtls_psa_crypto_free( );
1844}
1845/* END_CASE */
1846
1847/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001848void hash_setup( int alg_arg,
1849 int expected_status_arg )
1850{
1851 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001852 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001853 psa_hash_operation_t operation;
1854 psa_status_t status;
1855
1856 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1857
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001858 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001859 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001860 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001861
1862exit:
1863 mbedtls_psa_crypto_free( );
1864}
1865/* END_CASE */
1866
1867/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001868void hash_bad_order( )
1869{
1870 unsigned char input[] = "";
1871 /* SHA-256 hash of an empty string */
1872 unsigned char hash[] = {
1873 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1874 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1875 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1876 size_t hash_len;
1877 psa_hash_operation_t operation;
1878
1879 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1880
1881 /* psa_hash_update without calling psa_hash_setup beforehand */
1882 memset( &operation, 0, sizeof( operation ) );
1883 TEST_ASSERT( psa_hash_update( &operation,
1884 input, sizeof( input ) ) ==
1885 PSA_ERROR_INVALID_ARGUMENT );
1886
1887 /* psa_hash_verify without calling psa_hash_setup beforehand */
1888 memset( &operation, 0, sizeof( operation ) );
1889 TEST_ASSERT( psa_hash_verify( &operation,
1890 hash, sizeof( hash ) ) ==
1891 PSA_ERROR_INVALID_ARGUMENT );
1892
1893 /* psa_hash_finish without calling psa_hash_setup beforehand */
1894 memset( &operation, 0, sizeof( operation ) );
1895 TEST_ASSERT( psa_hash_finish( &operation,
1896 hash, sizeof( hash ), &hash_len ) ==
1897 PSA_ERROR_INVALID_ARGUMENT );
1898
1899exit:
1900 mbedtls_psa_crypto_free( );
1901}
1902/* END_CASE */
1903
itayzafrir27e69452018-11-01 14:26:34 +02001904/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1905void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001906{
1907 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001908 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1909 * appended to it */
1910 unsigned char hash[] = {
1911 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1912 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1913 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001914 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001915 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001916
1917 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1918
itayzafrir27e69452018-11-01 14:26:34 +02001919 /* psa_hash_verify with a smaller hash than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001920 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1921 TEST_ASSERT( psa_hash_verify( &operation,
1922 hash, expected_size - 1 ) ==
1923 PSA_ERROR_INVALID_SIGNATURE );
1924
itayzafrir27e69452018-11-01 14:26:34 +02001925 /* psa_hash_verify with a non-matching hash */
itayzafrirec93d302018-10-18 18:01:10 +03001926 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrirec93d302018-10-18 18:01:10 +03001927 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001928 hash + 1, expected_size ) ==
itayzafrirec93d302018-10-18 18:01:10 +03001929 PSA_ERROR_INVALID_SIGNATURE );
1930
itayzafrir27e69452018-11-01 14:26:34 +02001931 /* psa_hash_verify with a hash longer than expected */
itayzafrir4271df92018-10-24 18:16:19 +03001932 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrir4271df92018-10-24 18:16:19 +03001933 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001934 hash, sizeof( hash ) ) ==
itayzafrir4271df92018-10-24 18:16:19 +03001935 PSA_ERROR_INVALID_SIGNATURE );
1936
itayzafrirec93d302018-10-18 18:01:10 +03001937exit:
1938 mbedtls_psa_crypto_free( );
1939}
1940/* END_CASE */
1941
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001942/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1943void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001944{
1945 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001946 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001947 size_t expected_size = PSA_HASH_SIZE( alg );
1948 psa_hash_operation_t operation;
1949 size_t hash_len;
1950
1951 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1952
itayzafrir58028322018-10-25 10:22:01 +03001953 /* psa_hash_finish with a smaller hash buffer than expected */
1954 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1955 TEST_ASSERT( psa_hash_finish( &operation,
1956 hash, expected_size - 1,
1957 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1958
1959exit:
1960 mbedtls_psa_crypto_free( );
1961}
1962/* END_CASE */
1963
1964/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001965void mac_setup( int key_type_arg,
1966 data_t *key,
1967 int alg_arg,
1968 int expected_status_arg )
1969{
1970 int key_slot = 1;
1971 psa_key_type_t key_type = key_type_arg;
1972 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001973 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001974 psa_mac_operation_t operation;
1975 psa_key_policy_t policy;
1976 psa_status_t status;
1977
1978 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1979
1980 psa_key_policy_init( &policy );
1981 psa_key_policy_set_usage( &policy,
1982 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1983 alg );
1984 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1985
1986 TEST_ASSERT( psa_import_key( key_slot, key_type,
1987 key->x, key->len ) == PSA_SUCCESS );
1988
Gilles Peskine89167cb2018-07-08 20:12:23 +02001989 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001990 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001991 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001992
1993exit:
1994 psa_destroy_key( key_slot );
1995 mbedtls_psa_crypto_free( );
1996}
1997/* END_CASE */
1998
1999/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002000void mac_sign( int key_type_arg,
2001 data_t *key,
2002 int alg_arg,
2003 data_t *input,
2004 data_t *expected_mac )
2005{
2006 int key_slot = 1;
2007 psa_key_type_t key_type = key_type_arg;
2008 psa_algorithm_t alg = alg_arg;
2009 psa_mac_operation_t operation;
2010 psa_key_policy_t policy;
2011 /* Leave a little extra room in the output buffer. At the end of the
2012 * test, we'll check that the implementation didn't overwrite onto
2013 * this extra room. */
2014 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2015 size_t mac_buffer_size =
2016 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2017 size_t mac_length = 0;
2018
2019 memset( actual_mac, '+', sizeof( actual_mac ) );
2020 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2021 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2022
2023 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2024
2025 psa_key_policy_init( &policy );
2026 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
2027 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2028
2029 TEST_ASSERT( psa_import_key( key_slot, key_type,
2030 key->x, key->len ) == PSA_SUCCESS );
2031
2032 /* Calculate the MAC. */
2033 TEST_ASSERT( psa_mac_sign_setup( &operation,
2034 key_slot, alg ) == PSA_SUCCESS );
2035 TEST_ASSERT( psa_mac_update( &operation,
2036 input->x, input->len ) == PSA_SUCCESS );
2037 TEST_ASSERT( psa_mac_sign_finish( &operation,
2038 actual_mac, mac_buffer_size,
2039 &mac_length ) == PSA_SUCCESS );
2040
2041 /* Compare with the expected value. */
2042 TEST_ASSERT( mac_length == expected_mac->len );
2043 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
2044
2045 /* Verify that the end of the buffer is untouched. */
2046 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2047 sizeof( actual_mac ) - mac_length ) );
2048
2049exit:
2050 psa_destroy_key( key_slot );
2051 mbedtls_psa_crypto_free( );
2052}
2053/* END_CASE */
2054
2055/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002056void mac_verify( int key_type_arg,
2057 data_t *key,
2058 int alg_arg,
2059 data_t *input,
2060 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002061{
2062 int key_slot = 1;
2063 psa_key_type_t key_type = key_type_arg;
2064 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002065 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07002066 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002067
Gilles Peskine69c12672018-06-28 00:07:19 +02002068 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2069
Gilles Peskine8c9def32018-02-08 10:02:12 +01002070 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002071 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002072 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002073 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002074 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2075 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002076
2077 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2078
mohammad16036df908f2018-04-02 08:34:15 -07002079 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002080 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07002081 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2082
Gilles Peskine8c9def32018-02-08 10:02:12 +01002083 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002084 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002085
Gilles Peskine89167cb2018-07-08 20:12:23 +02002086 TEST_ASSERT( psa_mac_verify_setup( &operation,
2087 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002088 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
2089 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002090 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02002091 TEST_ASSERT( psa_mac_verify_finish( &operation,
2092 expected_mac->x,
2093 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002094
2095exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002096 psa_destroy_key( key_slot );
2097 mbedtls_psa_crypto_free( );
2098}
2099/* END_CASE */
2100
2101/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002102void cipher_setup( int key_type_arg,
2103 data_t *key,
2104 int alg_arg,
2105 int expected_status_arg )
2106{
2107 int key_slot = 1;
2108 psa_key_type_t key_type = key_type_arg;
2109 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002110 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002111 psa_cipher_operation_t operation;
2112 psa_key_policy_t policy;
2113 psa_status_t status;
2114
2115 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2116
2117 psa_key_policy_init( &policy );
2118 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2119 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2120
2121 TEST_ASSERT( psa_import_key( key_slot, key_type,
2122 key->x, key->len ) == PSA_SUCCESS );
2123
Gilles Peskinefe119512018-07-08 21:39:34 +02002124 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002125 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002126 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002127
2128exit:
2129 psa_destroy_key( key_slot );
2130 mbedtls_psa_crypto_free( );
2131}
2132/* END_CASE */
2133
2134/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002135void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002136 data_t *key,
2137 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002138 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002139{
2140 int key_slot = 1;
2141 psa_status_t status;
2142 psa_key_type_t key_type = key_type_arg;
2143 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002144 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002145 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002146 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002147 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002148 size_t output_buffer_size = 0;
2149 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002150 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002151 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002152 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002153
Gilles Peskine50e586b2018-06-08 14:28:46 +02002154 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002155 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002156 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002157 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2158 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2159 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002160
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002161 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2162 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002163
2164 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2165
Moran Pekered346952018-07-05 15:22:45 +03002166 psa_key_policy_init( &policy );
2167 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2168 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2169
Gilles Peskine50e586b2018-06-08 14:28:46 +02002170 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002171 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002172
Gilles Peskinefe119512018-07-08 21:39:34 +02002173 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2174 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002175
Gilles Peskinefe119512018-07-08 21:39:34 +02002176 TEST_ASSERT( psa_cipher_set_iv( &operation,
2177 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002178 output_buffer_size = (size_t) input->len +
2179 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002180 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002181
Gilles Peskine4abf7412018-06-18 16:35:34 +02002182 TEST_ASSERT( psa_cipher_update( &operation,
2183 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002184 output, output_buffer_size,
2185 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002186 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002187 status = psa_cipher_finish( &operation,
2188 output + function_output_length,
2189 output_buffer_size,
2190 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002191 total_output_length += function_output_length;
2192
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002193 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002194 if( expected_status == PSA_SUCCESS )
2195 {
2196 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002197 ASSERT_COMPARE( expected_output->x, expected_output->len,
2198 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002199 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002200
Gilles Peskine50e586b2018-06-08 14:28:46 +02002201exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002202 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002203 psa_destroy_key( key_slot );
2204 mbedtls_psa_crypto_free( );
2205}
2206/* END_CASE */
2207
2208/* BEGIN_CASE */
2209void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002210 data_t *key,
2211 data_t *input,
2212 int first_part_size,
2213 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002214{
2215 int key_slot = 1;
2216 psa_key_type_t key_type = key_type_arg;
2217 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002218 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002219 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002220 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002221 size_t output_buffer_size = 0;
2222 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002223 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002224 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002225 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002226
Gilles Peskine50e586b2018-06-08 14:28:46 +02002227 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002228 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002229 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002230 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2231 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2232 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002233
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002234 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2235 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002236
2237 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2238
Moran Pekered346952018-07-05 15:22:45 +03002239 psa_key_policy_init( &policy );
2240 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2241 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2242
Gilles Peskine50e586b2018-06-08 14:28:46 +02002243 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002244 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002245
Gilles Peskinefe119512018-07-08 21:39:34 +02002246 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2247 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002248
Gilles Peskinefe119512018-07-08 21:39:34 +02002249 TEST_ASSERT( psa_cipher_set_iv( &operation,
2250 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002251 output_buffer_size = (size_t) input->len +
2252 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002253 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002254
Gilles Peskine4abf7412018-06-18 16:35:34 +02002255 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002256 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002257 output, output_buffer_size,
2258 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002259 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002260 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002261 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002262 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002263 output, output_buffer_size,
2264 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002265 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002266 TEST_ASSERT( psa_cipher_finish( &operation,
2267 output + function_output_length,
2268 output_buffer_size,
2269 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002270 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002271 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2272
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002273 ASSERT_COMPARE( expected_output->x, expected_output->len,
2274 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002275
2276exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002277 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002278 psa_destroy_key( key_slot );
2279 mbedtls_psa_crypto_free( );
2280}
2281/* END_CASE */
2282
2283/* BEGIN_CASE */
2284void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002285 data_t *key,
2286 data_t *input,
2287 int first_part_size,
2288 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002289{
2290 int key_slot = 1;
2291
2292 psa_key_type_t key_type = key_type_arg;
2293 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002294 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002295 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002296 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002297 size_t output_buffer_size = 0;
2298 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002299 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002300 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002301 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002302
Gilles Peskine50e586b2018-06-08 14:28:46 +02002303 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002304 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002305 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002306 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2307 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2308 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002309
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002310 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2311 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002312
2313 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2314
Moran Pekered346952018-07-05 15:22:45 +03002315 psa_key_policy_init( &policy );
2316 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2317 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2318
Gilles Peskine50e586b2018-06-08 14:28:46 +02002319 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002320 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002321
Gilles Peskinefe119512018-07-08 21:39:34 +02002322 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2323 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002324
Gilles Peskinefe119512018-07-08 21:39:34 +02002325 TEST_ASSERT( psa_cipher_set_iv( &operation,
2326 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002327
mohammad16033d91abe2018-07-03 13:15:54 +03002328 output_buffer_size = (size_t) input->len +
2329 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002330 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002331
Gilles Peskine4abf7412018-06-18 16:35:34 +02002332 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2333 TEST_ASSERT( psa_cipher_update( &operation,
2334 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002335 output, output_buffer_size,
2336 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002337 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002338 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002339 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002340 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002341 output, output_buffer_size,
2342 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002343 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002344 TEST_ASSERT( psa_cipher_finish( &operation,
2345 output + function_output_length,
2346 output_buffer_size,
2347 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002348 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002349 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2350
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002351 ASSERT_COMPARE( expected_output->x, expected_output->len,
2352 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002353
2354exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002355 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002356 psa_destroy_key( key_slot );
2357 mbedtls_psa_crypto_free( );
2358}
2359/* END_CASE */
2360
Gilles Peskine50e586b2018-06-08 14:28:46 +02002361/* BEGIN_CASE */
2362void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002363 data_t *key,
2364 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002365 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002366{
2367 int key_slot = 1;
2368 psa_status_t status;
2369 psa_key_type_t key_type = key_type_arg;
2370 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002371 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002372 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002373 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002374 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002375 size_t output_buffer_size = 0;
2376 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002377 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002378 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002379 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002380
Gilles Peskine50e586b2018-06-08 14:28:46 +02002381 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002382 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002383 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002384 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2385 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2386 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002387
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002388 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2389 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002390
2391 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2392
Moran Pekered346952018-07-05 15:22:45 +03002393 psa_key_policy_init( &policy );
2394 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2395 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2396
Gilles Peskine50e586b2018-06-08 14:28:46 +02002397 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002398 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002399
Gilles Peskinefe119512018-07-08 21:39:34 +02002400 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2401 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002402
Gilles Peskinefe119512018-07-08 21:39:34 +02002403 TEST_ASSERT( psa_cipher_set_iv( &operation,
2404 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405
mohammad16033d91abe2018-07-03 13:15:54 +03002406 output_buffer_size = (size_t) input->len +
2407 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002408 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002409
Gilles Peskine4abf7412018-06-18 16:35:34 +02002410 TEST_ASSERT( psa_cipher_update( &operation,
2411 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002412 output, output_buffer_size,
2413 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002414 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002415 status = psa_cipher_finish( &operation,
2416 output + function_output_length,
2417 output_buffer_size,
2418 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002419 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002420 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002421
2422 if( expected_status == PSA_SUCCESS )
2423 {
2424 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002425 ASSERT_COMPARE( expected_output->x, expected_output->len,
2426 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002427 }
2428
Gilles Peskine50e586b2018-06-08 14:28:46 +02002429exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002430 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002431 psa_destroy_key( key_slot );
2432 mbedtls_psa_crypto_free( );
2433}
2434/* END_CASE */
2435
Gilles Peskine50e586b2018-06-08 14:28:46 +02002436/* BEGIN_CASE */
2437void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002438 data_t *key,
2439 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002440{
2441 int key_slot = 1;
2442 psa_key_type_t key_type = key_type_arg;
2443 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002444 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002445 size_t iv_size = 16;
2446 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002447 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002448 size_t output1_size = 0;
2449 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002450 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002451 size_t output2_size = 0;
2452 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002453 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002454 psa_cipher_operation_t operation1;
2455 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002456 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002457
mohammad1603d7d7ba52018-03-12 18:51:53 +02002458 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002459 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002460 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2461 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002462
mohammad1603d7d7ba52018-03-12 18:51:53 +02002463 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2464
Moran Pekered346952018-07-05 15:22:45 +03002465 psa_key_policy_init( &policy );
2466 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2467 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2468
mohammad1603d7d7ba52018-03-12 18:51:53 +02002469 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002470 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002471
Gilles Peskinefe119512018-07-08 21:39:34 +02002472 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2473 key_slot, alg ) == PSA_SUCCESS );
2474 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2475 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002476
Gilles Peskinefe119512018-07-08 21:39:34 +02002477 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2478 iv, iv_size,
2479 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002480 output1_size = (size_t) input->len +
2481 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002482 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002483
Gilles Peskine4abf7412018-06-18 16:35:34 +02002484 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002485 output1, output1_size,
2486 &output1_length ) == PSA_SUCCESS );
2487 TEST_ASSERT( psa_cipher_finish( &operation1,
2488 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002489 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002490
Gilles Peskine048b7f02018-06-08 14:20:49 +02002491 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002492
2493 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2494
2495 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002496 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002497
Gilles Peskinefe119512018-07-08 21:39:34 +02002498 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2499 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002500 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2501 output2, output2_size,
2502 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002503 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002504 TEST_ASSERT( psa_cipher_finish( &operation2,
2505 output2 + output2_length,
2506 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002507 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002508
Gilles Peskine048b7f02018-06-08 14:20:49 +02002509 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002510
Janos Follath25c4fa82018-07-06 16:23:25 +01002511 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002512
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002513 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002514
2515exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002516 mbedtls_free( output1 );
2517 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002518 psa_destroy_key( key_slot );
2519 mbedtls_psa_crypto_free( );
2520}
2521/* END_CASE */
2522
2523/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002524void cipher_verify_output_multipart( int alg_arg,
2525 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002526 data_t *key,
2527 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002528 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002529{
2530 int key_slot = 1;
2531 psa_key_type_t key_type = key_type_arg;
2532 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002533 unsigned char iv[16] = {0};
2534 size_t iv_size = 16;
2535 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002536 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002537 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002538 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002539 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002540 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002541 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002542 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002543 psa_cipher_operation_t operation1;
2544 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002545 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002546
Moran Pekerded84402018-06-06 16:36:50 +03002547 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002548 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002549 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2550 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002551
Moran Pekerded84402018-06-06 16:36:50 +03002552 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2553
Moran Pekered346952018-07-05 15:22:45 +03002554 psa_key_policy_init( &policy );
2555 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2556 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2557
Moran Pekerded84402018-06-06 16:36:50 +03002558 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002559 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002560
Gilles Peskinefe119512018-07-08 21:39:34 +02002561 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2562 key_slot, alg ) == PSA_SUCCESS );
2563 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2564 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002565
Gilles Peskinefe119512018-07-08 21:39:34 +02002566 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2567 iv, iv_size,
2568 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002569 output1_buffer_size = (size_t) input->len +
2570 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002571 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002572
Gilles Peskine4abf7412018-06-18 16:35:34 +02002573 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002574
itayzafrir3e02b3b2018-06-12 17:06:52 +03002575 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002576 output1, output1_buffer_size,
2577 &function_output_length ) == PSA_SUCCESS );
2578 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002579
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002580 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002581 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002582 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002583 output1, output1_buffer_size,
2584 &function_output_length ) == PSA_SUCCESS );
2585 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002586
Gilles Peskine048b7f02018-06-08 14:20:49 +02002587 TEST_ASSERT( psa_cipher_finish( &operation1,
2588 output1 + output1_length,
2589 output1_buffer_size - output1_length,
2590 &function_output_length ) == PSA_SUCCESS );
2591 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002592
2593 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2594
Gilles Peskine048b7f02018-06-08 14:20:49 +02002595 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002596 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002597
Gilles Peskinefe119512018-07-08 21:39:34 +02002598 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2599 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002600
2601 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002602 output2, output2_buffer_size,
2603 &function_output_length ) == PSA_SUCCESS );
2604 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002605
Gilles Peskine048b7f02018-06-08 14:20:49 +02002606 TEST_ASSERT( psa_cipher_update( &operation2,
2607 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002608 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002609 output2, output2_buffer_size,
2610 &function_output_length ) == PSA_SUCCESS );
2611 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002612
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002613 TEST_ASSERT( psa_cipher_finish( &operation2,
2614 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002615 output2_buffer_size - output2_length,
2616 &function_output_length ) == PSA_SUCCESS );
2617 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002618
Janos Follath25c4fa82018-07-06 16:23:25 +01002619 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002620
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002621 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002622
2623exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002624 mbedtls_free( output1 );
2625 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002626 psa_destroy_key( key_slot );
2627 mbedtls_psa_crypto_free( );
2628}
2629/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002630
Gilles Peskine20035e32018-02-03 22:44:14 +01002631/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002632void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002633 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002634 data_t *nonce,
2635 data_t *additional_data,
2636 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002637 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002638{
2639 int slot = 1;
2640 psa_key_type_t key_type = key_type_arg;
2641 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002642 unsigned char *output_data = NULL;
2643 size_t output_size = 0;
2644 size_t output_length = 0;
2645 unsigned char *output_data2 = NULL;
2646 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002647 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002648 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002649 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002650
Gilles Peskinea1cac842018-06-11 19:33:02 +02002651 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002652 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002653 TEST_ASSERT( nonce != NULL );
2654 TEST_ASSERT( additional_data != NULL );
2655 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2656 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2657 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2658 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2659
Gilles Peskine4abf7412018-06-18 16:35:34 +02002660 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002661 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002662
2663 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2664
2665 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002666 psa_key_policy_set_usage( &policy,
2667 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2668 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002669 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2670
2671 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002672 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002673
2674 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002675 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002676 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002677 additional_data->len,
2678 input_data->x, input_data->len,
2679 output_data, output_size,
2680 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002681
2682 if( PSA_SUCCESS == expected_result )
2683 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002684 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002685
2686 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002687 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002688 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002689 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002690 output_data, output_length,
2691 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002692 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002693
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002694 ASSERT_COMPARE( input_data->x, input_data->len,
2695 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002696 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002697
Gilles Peskinea1cac842018-06-11 19:33:02 +02002698exit:
2699 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002700 mbedtls_free( output_data );
2701 mbedtls_free( output_data2 );
2702 mbedtls_psa_crypto_free( );
2703}
2704/* END_CASE */
2705
2706/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002707void aead_encrypt( int key_type_arg, data_t *key_data,
2708 int alg_arg,
2709 data_t *nonce,
2710 data_t *additional_data,
2711 data_t *input_data,
2712 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002713{
2714 int slot = 1;
2715 psa_key_type_t key_type = key_type_arg;
2716 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002717 unsigned char *output_data = NULL;
2718 size_t output_size = 0;
2719 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002720 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002721 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002722
Gilles Peskinea1cac842018-06-11 19:33:02 +02002723 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002724 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002725 TEST_ASSERT( additional_data != NULL );
2726 TEST_ASSERT( nonce != NULL );
2727 TEST_ASSERT( expected_result != NULL );
2728 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2729 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2730 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2731 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2732 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2733
Gilles Peskine4abf7412018-06-18 16:35:34 +02002734 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002735 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002736
Gilles Peskinea1cac842018-06-11 19:33:02 +02002737 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2738
2739 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002740 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002741 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2742
2743 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002744 key_data->x,
2745 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002746
2747 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002748 nonce->x, nonce->len,
2749 additional_data->x, additional_data->len,
2750 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002751 output_data, output_size,
2752 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002753
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002754 ASSERT_COMPARE( expected_result->x, expected_result->len,
2755 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002756
Gilles Peskinea1cac842018-06-11 19:33:02 +02002757exit:
2758 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002759 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002760 mbedtls_psa_crypto_free( );
2761}
2762/* END_CASE */
2763
2764/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002765void aead_decrypt( int key_type_arg, data_t *key_data,
2766 int alg_arg,
2767 data_t *nonce,
2768 data_t *additional_data,
2769 data_t *input_data,
2770 data_t *expected_data,
2771 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002772{
2773 int slot = 1;
2774 psa_key_type_t key_type = key_type_arg;
2775 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002776 unsigned char *output_data = NULL;
2777 size_t output_size = 0;
2778 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002779 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002780 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002781 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002782
Gilles Peskinea1cac842018-06-11 19:33:02 +02002783 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002784 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002785 TEST_ASSERT( additional_data != NULL );
2786 TEST_ASSERT( nonce != NULL );
2787 TEST_ASSERT( expected_data != NULL );
2788 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2789 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2790 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2791 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2792 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2793
Gilles Peskine4abf7412018-06-18 16:35:34 +02002794 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002795 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002796
Gilles Peskinea1cac842018-06-11 19:33:02 +02002797 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2798
2799 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002800 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002801 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2802
2803 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002804 key_data->x,
2805 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002806
2807 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002808 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002809 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002810 additional_data->len,
2811 input_data->x, input_data->len,
2812 output_data, output_size,
2813 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002814
Gilles Peskine2d277862018-06-18 15:41:12 +02002815 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002816 ASSERT_COMPARE( expected_data->x, expected_data->len,
2817 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002818
Gilles Peskinea1cac842018-06-11 19:33:02 +02002819exit:
2820 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002821 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002822 mbedtls_psa_crypto_free( );
2823}
2824/* END_CASE */
2825
2826/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002827void signature_size( int type_arg,
2828 int bits,
2829 int alg_arg,
2830 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002831{
2832 psa_key_type_t type = type_arg;
2833 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002834 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002835 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2836exit:
2837 ;
2838}
2839/* END_CASE */
2840
2841/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002842void sign_deterministic( int key_type_arg, data_t *key_data,
2843 int alg_arg, data_t *input_data,
2844 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002845{
2846 int slot = 1;
2847 psa_key_type_t key_type = key_type_arg;
2848 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002849 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002850 unsigned char *signature = NULL;
2851 size_t signature_size;
2852 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002853 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002854
Gilles Peskine20035e32018-02-03 22:44:14 +01002855 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002856 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002857 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002858 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2859 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2860 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002861
2862 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2863
mohammad1603a97cb8c2018-03-28 03:46:26 -07002864 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002865 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002866 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2867
Gilles Peskine20035e32018-02-03 22:44:14 +01002868 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002869 key_data->x,
2870 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002871 TEST_ASSERT( psa_get_key_information( slot,
2872 NULL,
2873 &key_bits ) == PSA_SUCCESS );
2874
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002875 /* Allocate a buffer which has the size advertized by the
2876 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002877 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2878 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002879 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002880 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002881 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002882
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002883 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002884 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002885 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002886 signature, signature_size,
2887 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002888 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002889 ASSERT_COMPARE( output_data->x, output_data->len,
2890 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002891
2892exit:
2893 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002894 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002895 mbedtls_psa_crypto_free( );
2896}
2897/* END_CASE */
2898
2899/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002900void sign_fail( int key_type_arg, data_t *key_data,
2901 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002902 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002903{
2904 int slot = 1;
2905 psa_key_type_t key_type = key_type_arg;
2906 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002907 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002908 psa_status_t actual_status;
2909 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002910 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002911 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002912 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002913
Gilles Peskine20035e32018-02-03 22:44:14 +01002914 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002915 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002916 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2917 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2918
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002919 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002920
2921 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2922
mohammad1603a97cb8c2018-03-28 03:46:26 -07002923 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002924 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002925 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2926
Gilles Peskine20035e32018-02-03 22:44:14 +01002927 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002928 key_data->x,
2929 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002930
2931 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002932 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002933 signature, signature_size,
2934 &signature_length );
2935 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002936 /* The value of *signature_length is unspecified on error, but
2937 * whatever it is, it should be less than signature_size, so that
2938 * if the caller tries to read *signature_length bytes without
2939 * checking the error code then they don't overflow a buffer. */
2940 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002941
2942exit:
2943 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002944 mbedtls_free( signature );
2945 mbedtls_psa_crypto_free( );
2946}
2947/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002948
2949/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002950void sign_verify( int key_type_arg, data_t *key_data,
2951 int alg_arg, data_t *input_data )
2952{
2953 int slot = 1;
2954 psa_key_type_t key_type = key_type_arg;
2955 psa_algorithm_t alg = alg_arg;
2956 size_t key_bits;
2957 unsigned char *signature = NULL;
2958 size_t signature_size;
2959 size_t signature_length = 0xdeadbeef;
2960 psa_key_policy_t policy;
2961
2962 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2963
2964 psa_key_policy_init( &policy );
2965 psa_key_policy_set_usage( &policy,
2966 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2967 alg );
2968 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2969
2970 TEST_ASSERT( psa_import_key( slot, key_type,
2971 key_data->x,
2972 key_data->len ) == PSA_SUCCESS );
2973 TEST_ASSERT( psa_get_key_information( slot,
2974 NULL,
2975 &key_bits ) == PSA_SUCCESS );
2976
2977 /* Allocate a buffer which has the size advertized by the
2978 * library. */
2979 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2980 key_bits, alg );
2981 TEST_ASSERT( signature_size != 0 );
2982 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002983 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002984
2985 /* Perform the signature. */
2986 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2987 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002988 signature, signature_size,
2989 &signature_length ) == PSA_SUCCESS );
2990 /* Check that the signature length looks sensible. */
2991 TEST_ASSERT( signature_length <= signature_size );
2992 TEST_ASSERT( signature_length > 0 );
2993
2994 /* Use the library to verify that the signature is correct. */
2995 TEST_ASSERT( psa_asymmetric_verify(
2996 slot, alg,
2997 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002998 signature, signature_length ) == PSA_SUCCESS );
2999
3000 if( input_data->len != 0 )
3001 {
3002 /* Flip a bit in the input and verify that the signature is now
3003 * detected as invalid. Flip a bit at the beginning, not at the end,
3004 * because ECDSA may ignore the last few bits of the input. */
3005 input_data->x[0] ^= 1;
3006 TEST_ASSERT( psa_asymmetric_verify(
3007 slot, alg,
3008 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02003009 signature,
3010 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
3011 }
3012
3013exit:
3014 psa_destroy_key( slot );
3015 mbedtls_free( signature );
3016 mbedtls_psa_crypto_free( );
3017}
3018/* END_CASE */
3019
3020/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003021void asymmetric_verify( int key_type_arg, data_t *key_data,
3022 int alg_arg, data_t *hash_data,
3023 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003024{
3025 int slot = 1;
3026 psa_key_type_t key_type = key_type_arg;
3027 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003028 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03003029
Gilles Peskine69c12672018-06-28 00:07:19 +02003030 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3031
itayzafrir5c753392018-05-08 11:18:38 +03003032 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003033 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003034 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003035 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3036 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3037 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003038
3039 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3040
3041 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003042 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03003043 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3044
3045 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003046 key_data->x,
3047 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003048
3049 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003050 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003051 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003052 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003053exit:
3054 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03003055 mbedtls_psa_crypto_free( );
3056}
3057/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003058
3059/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003060void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3061 int alg_arg, data_t *hash_data,
3062 data_t *signature_data,
3063 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003064{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003065 int slot = 1;
3066 psa_key_type_t key_type = key_type_arg;
3067 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003068 psa_status_t actual_status;
3069 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003070 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003071
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003072 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003073 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003074 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003075 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3076 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3077 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078
3079 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3080
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003081 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003082 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003083 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3084
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003086 key_data->x,
3087 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003088
3089 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003090 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003091 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003092 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003094 TEST_ASSERT( actual_status == expected_status );
3095
3096exit:
3097 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003098 mbedtls_psa_crypto_free( );
3099}
3100/* END_CASE */
3101
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003102/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003103void asymmetric_encrypt( int key_type_arg,
3104 data_t *key_data,
3105 int alg_arg,
3106 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003107 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003108 int expected_output_length_arg,
3109 int expected_status_arg )
3110{
3111 int slot = 1;
3112 psa_key_type_t key_type = key_type_arg;
3113 psa_algorithm_t alg = alg_arg;
3114 size_t expected_output_length = expected_output_length_arg;
3115 size_t key_bits;
3116 unsigned char *output = NULL;
3117 size_t output_size;
3118 size_t output_length = ~0;
3119 psa_status_t actual_status;
3120 psa_status_t expected_status = expected_status_arg;
3121 psa_key_policy_t policy;
3122
3123 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3124
3125 /* Import the key */
3126 psa_key_policy_init( &policy );
3127 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
3128 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3129 TEST_ASSERT( psa_import_key( slot, key_type,
3130 key_data->x,
3131 key_data->len ) == PSA_SUCCESS );
3132
3133 /* Determine the maximum output length */
3134 TEST_ASSERT( psa_get_key_information( slot,
3135 NULL,
3136 &key_bits ) == PSA_SUCCESS );
3137 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003138 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003139
3140 /* Encrypt the input */
3141 actual_status = psa_asymmetric_encrypt( slot, alg,
3142 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003143 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003144 output, output_size,
3145 &output_length );
3146 TEST_ASSERT( actual_status == expected_status );
3147 TEST_ASSERT( output_length == expected_output_length );
3148
Gilles Peskine68428122018-06-30 18:42:41 +02003149 /* If the label is empty, the test framework puts a non-null pointer
3150 * in label->x. Test that a null pointer works as well. */
3151 if( label->len == 0 )
3152 {
3153 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003154 if( output_size != 0 )
3155 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003156 actual_status = psa_asymmetric_encrypt( slot, alg,
3157 input_data->x, input_data->len,
3158 NULL, label->len,
3159 output, output_size,
3160 &output_length );
3161 TEST_ASSERT( actual_status == expected_status );
3162 TEST_ASSERT( output_length == expected_output_length );
3163 }
3164
Gilles Peskine656896e2018-06-29 19:12:28 +02003165exit:
3166 psa_destroy_key( slot );
3167 mbedtls_free( output );
3168 mbedtls_psa_crypto_free( );
3169}
3170/* END_CASE */
3171
3172/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003173void asymmetric_encrypt_decrypt( int key_type_arg,
3174 data_t *key_data,
3175 int alg_arg,
3176 data_t *input_data,
3177 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178{
3179 int slot = 1;
3180 psa_key_type_t key_type = key_type_arg;
3181 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003182 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003183 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003184 size_t output_size;
3185 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003186 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003187 size_t output2_size;
3188 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003189 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003193 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3194 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3195
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3197
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003198 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003199 psa_key_policy_set_usage( &policy,
3200 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003201 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003202 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3203
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003204 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003205 key_data->x,
3206 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003207
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003208
3209 /* Determine the maximum ciphertext length */
3210 TEST_ASSERT( psa_get_key_information( slot,
3211 NULL,
3212 &key_bits ) == PSA_SUCCESS );
3213 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003214 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003215 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003216 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003217
Gilles Peskineeebd7382018-06-08 18:11:54 +02003218 /* We test encryption by checking that encrypt-then-decrypt gives back
3219 * the original plaintext because of the non-optional random
3220 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02003221 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003222 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003223 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003224 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003225 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003226 /* We don't know what ciphertext length to expect, but check that
3227 * it looks sensible. */
3228 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003229
Gilles Peskine2d277862018-06-18 15:41:12 +02003230 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003231 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02003232 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003233 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003234 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003235 ASSERT_COMPARE( input_data->x, input_data->len,
3236 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237
3238exit:
3239 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003240 mbedtls_free( output );
3241 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003243}
3244/* END_CASE */
3245
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003246/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003247void asymmetric_decrypt( int key_type_arg,
3248 data_t *key_data,
3249 int alg_arg,
3250 data_t *input_data,
3251 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003252 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253{
3254 int slot = 1;
3255 psa_key_type_t key_type = key_type_arg;
3256 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003258 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003259 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003260 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003261
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003263 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003264 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003265 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3266 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3267 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3268
Gilles Peskine4abf7412018-06-18 16:35:34 +02003269 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003270 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003271
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003272 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3273
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003274 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003275 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003276 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3277
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003278 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003279 key_data->x,
3280 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003282 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003283 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003284 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003285 output,
3286 output_size,
3287 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003288 ASSERT_COMPARE( expected_data->x, expected_data->len,
3289 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003290
Gilles Peskine68428122018-06-30 18:42:41 +02003291 /* If the label is empty, the test framework puts a non-null pointer
3292 * in label->x. Test that a null pointer works as well. */
3293 if( label->len == 0 )
3294 {
3295 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003296 if( output_size != 0 )
3297 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003298 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
3299 input_data->x, input_data->len,
3300 NULL, label->len,
3301 output,
3302 output_size,
3303 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003304 ASSERT_COMPARE( expected_data->x, expected_data->len,
3305 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003306 }
3307
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308exit:
3309 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003310 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003311 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312}
3313/* END_CASE */
3314
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003316void asymmetric_decrypt_fail( int key_type_arg,
3317 data_t *key_data,
3318 int alg_arg,
3319 data_t *input_data,
3320 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003321 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003322{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003323 int slot = 1;
3324 psa_key_type_t key_type = key_type_arg;
3325 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003326 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003327 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003328 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329 psa_status_t actual_status;
3330 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003331 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003332
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003333 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003334 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003335 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3336 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3337
Gilles Peskine4abf7412018-06-18 16:35:34 +02003338 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003339 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003340
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003341 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3342
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003343 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003344 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003345 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3346
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003347 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003348 key_data->x,
3349 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003350
Gilles Peskine2d277862018-06-18 15:41:12 +02003351 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003352 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003353 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003354 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003355 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003356 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003357 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003358
Gilles Peskine68428122018-06-30 18:42:41 +02003359 /* If the label is empty, the test framework puts a non-null pointer
3360 * in label->x. Test that a null pointer works as well. */
3361 if( label->len == 0 )
3362 {
3363 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003364 if( output_size != 0 )
3365 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003366 actual_status = psa_asymmetric_decrypt( slot, alg,
3367 input_data->x, input_data->len,
3368 NULL, label->len,
3369 output, output_size,
3370 &output_length );
3371 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003372 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003373 }
3374
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003375exit:
3376 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003377 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003378 mbedtls_psa_crypto_free( );
3379}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003380/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003381
3382/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003383void derive_setup( int key_type_arg,
3384 data_t *key_data,
3385 int alg_arg,
3386 data_t *salt,
3387 data_t *label,
3388 int requested_capacity_arg,
3389 int expected_status_arg )
3390{
3391 psa_key_slot_t slot = 1;
3392 size_t key_type = key_type_arg;
3393 psa_algorithm_t alg = alg_arg;
3394 size_t requested_capacity = requested_capacity_arg;
3395 psa_status_t expected_status = expected_status_arg;
3396 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3397 psa_key_policy_t policy;
3398
3399 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3400
3401 psa_key_policy_init( &policy );
3402 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3403 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3404
3405 TEST_ASSERT( psa_import_key( slot, key_type,
3406 key_data->x,
3407 key_data->len ) == PSA_SUCCESS );
3408
3409 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3410 salt->x, salt->len,
3411 label->x, label->len,
3412 requested_capacity ) == expected_status );
3413
3414exit:
3415 psa_generator_abort( &generator );
3416 psa_destroy_key( slot );
3417 mbedtls_psa_crypto_free( );
3418}
3419/* END_CASE */
3420
3421/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003422void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003423{
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003424 psa_key_slot_t base_key = 1;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003425 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003426 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003427 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003428 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003429 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003430 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3431 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3432 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003433 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003434
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003435 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3436
3437 psa_key_policy_init( &policy );
3438 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3439 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3440
3441 TEST_ASSERT( psa_import_key( base_key, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003442 key_data,
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003443 sizeof( key_data ) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003444
3445 /* valid key derivation */
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003446 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003447 NULL, 0,
3448 NULL, 0,
3449 capacity ) == PSA_SUCCESS );
3450
3451 /* state of generator shouldn't allow additional generation */
3452 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3453 NULL, 0,
3454 NULL, 0,
3455 capacity ) == PSA_ERROR_BAD_STATE );
3456
3457 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3458 == PSA_SUCCESS );
3459
3460 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3461 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3462
3463
3464exit:
3465 psa_generator_abort( &generator );
3466 psa_destroy_key( base_key );
3467 mbedtls_psa_crypto_free( );
3468}
3469/* END_CASE */
3470
3471
3472/* BEGIN_CASE */
3473void test_derive_invalid_generator_tests( )
3474{
3475 uint8_t output_buffer[16];
3476 size_t buffer_size = 16;
3477 size_t capacity = 0;
3478 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3479
Nir Sonnenschein50789302018-10-31 12:16:38 +02003480 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003481 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003482
3483 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003484 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003485
Nir Sonnenschein50789302018-10-31 12:16:38 +02003486 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003487
Nir Sonnenschein50789302018-10-31 12:16:38 +02003488 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003489 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003490
Nir Sonnenschein50789302018-10-31 12:16:38 +02003491 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003492 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003493
3494exit:
3495 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003496}
3497/* END_CASE */
3498
3499/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003500void derive_output( int alg_arg,
3501 data_t *key_data,
3502 data_t *salt,
3503 data_t *label,
3504 int requested_capacity_arg,
3505 data_t *expected_output1,
3506 data_t *expected_output2 )
3507{
3508 psa_key_slot_t slot = 1;
3509 psa_algorithm_t alg = alg_arg;
3510 size_t requested_capacity = requested_capacity_arg;
3511 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3512 uint8_t *expected_outputs[2] =
3513 {expected_output1->x, expected_output2->x};
3514 size_t output_sizes[2] =
3515 {expected_output1->len, expected_output2->len};
3516 size_t output_buffer_size = 0;
3517 uint8_t *output_buffer = NULL;
3518 size_t expected_capacity;
3519 size_t current_capacity;
3520 psa_key_policy_t policy;
3521 psa_status_t status;
3522 unsigned i;
3523
3524 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3525 {
3526 if( output_sizes[i] > output_buffer_size )
3527 output_buffer_size = output_sizes[i];
3528 if( output_sizes[i] == 0 )
3529 expected_outputs[i] = NULL;
3530 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003531 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003532 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3533
3534 psa_key_policy_init( &policy );
3535 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3536 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3537
3538 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3539 key_data->x,
3540 key_data->len ) == PSA_SUCCESS );
3541
3542 /* Extraction phase. */
3543 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3544 salt->x, salt->len,
3545 label->x, label->len,
3546 requested_capacity ) == PSA_SUCCESS );
3547 TEST_ASSERT( psa_get_generator_capacity( &generator,
3548 &current_capacity ) ==
3549 PSA_SUCCESS );
3550 TEST_ASSERT( current_capacity == requested_capacity );
3551 expected_capacity = requested_capacity;
3552
3553 /* Expansion phase. */
3554 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3555 {
3556 /* Read some bytes. */
3557 status = psa_generator_read( &generator,
3558 output_buffer, output_sizes[i] );
3559 if( expected_capacity == 0 && output_sizes[i] == 0 )
3560 {
3561 /* Reading 0 bytes when 0 bytes are available can go either way. */
3562 TEST_ASSERT( status == PSA_SUCCESS ||
3563 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3564 continue;
3565 }
3566 else if( expected_capacity == 0 ||
3567 output_sizes[i] > expected_capacity )
3568 {
3569 /* Capacity exceeded. */
3570 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3571 expected_capacity = 0;
3572 continue;
3573 }
3574 /* Success. Check the read data. */
3575 TEST_ASSERT( status == PSA_SUCCESS );
3576 if( output_sizes[i] != 0 )
3577 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3578 output_sizes[i] ) == 0 );
3579 /* Check the generator status. */
3580 expected_capacity -= output_sizes[i];
3581 TEST_ASSERT( psa_get_generator_capacity( &generator,
3582 &current_capacity ) ==
3583 PSA_SUCCESS );
3584 TEST_ASSERT( expected_capacity == current_capacity );
3585 }
3586 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3587
3588exit:
3589 mbedtls_free( output_buffer );
3590 psa_generator_abort( &generator );
3591 psa_destroy_key( slot );
3592 mbedtls_psa_crypto_free( );
3593}
3594/* END_CASE */
3595
3596/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003597void derive_full( int alg_arg,
3598 data_t *key_data,
3599 data_t *salt,
3600 data_t *label,
3601 int requested_capacity_arg )
3602{
3603 psa_key_slot_t slot = 1;
3604 psa_algorithm_t alg = alg_arg;
3605 size_t requested_capacity = requested_capacity_arg;
3606 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3607 unsigned char output_buffer[16];
3608 size_t expected_capacity = requested_capacity;
3609 size_t current_capacity;
3610 psa_key_policy_t policy;
3611
3612 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3613
3614 psa_key_policy_init( &policy );
3615 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3616 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3617
3618 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3619 key_data->x,
3620 key_data->len ) == PSA_SUCCESS );
3621
3622 /* Extraction phase. */
3623 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3624 salt->x, salt->len,
3625 label->x, label->len,
3626 requested_capacity ) == PSA_SUCCESS );
3627 TEST_ASSERT( psa_get_generator_capacity( &generator,
3628 &current_capacity ) ==
3629 PSA_SUCCESS );
3630 TEST_ASSERT( current_capacity == expected_capacity );
3631
3632 /* Expansion phase. */
3633 while( current_capacity > 0 )
3634 {
3635 size_t read_size = sizeof( output_buffer );
3636 if( read_size > current_capacity )
3637 read_size = current_capacity;
3638 TEST_ASSERT( psa_generator_read( &generator,
3639 output_buffer,
3640 read_size ) == PSA_SUCCESS );
3641 expected_capacity -= read_size;
3642 TEST_ASSERT( psa_get_generator_capacity( &generator,
3643 &current_capacity ) ==
3644 PSA_SUCCESS );
3645 TEST_ASSERT( current_capacity == expected_capacity );
3646 }
3647
3648 /* Check that the generator refuses to go over capacity. */
3649 TEST_ASSERT( psa_generator_read( &generator,
3650 output_buffer,
3651 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3652
3653 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3654
3655exit:
3656 psa_generator_abort( &generator );
3657 psa_destroy_key( slot );
3658 mbedtls_psa_crypto_free( );
3659}
3660/* END_CASE */
3661
3662/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003663void derive_key_exercise( int alg_arg,
3664 data_t *key_data,
3665 data_t *salt,
3666 data_t *label,
3667 int derived_type_arg,
3668 int derived_bits_arg,
3669 int derived_usage_arg,
3670 int derived_alg_arg )
3671{
3672 psa_key_slot_t base_key = 1;
3673 psa_key_slot_t derived_key = 2;
3674 psa_algorithm_t alg = alg_arg;
3675 psa_key_type_t derived_type = derived_type_arg;
3676 size_t derived_bits = derived_bits_arg;
3677 psa_key_usage_t derived_usage = derived_usage_arg;
3678 psa_algorithm_t derived_alg = derived_alg_arg;
3679 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3680 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3681 psa_key_policy_t policy;
3682 psa_key_type_t got_type;
3683 size_t got_bits;
3684
3685 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3686
3687 psa_key_policy_init( &policy );
3688 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3689 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3690 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3691 key_data->x,
3692 key_data->len ) == PSA_SUCCESS );
3693
3694 /* Derive a key. */
3695 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3696 salt->x, salt->len,
3697 label->x, label->len,
3698 capacity ) == PSA_SUCCESS );
3699 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3700 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3701 TEST_ASSERT( psa_generator_import_key( derived_key,
3702 derived_type,
3703 derived_bits,
3704 &generator ) == PSA_SUCCESS );
3705
3706 /* Test the key information */
3707 TEST_ASSERT( psa_get_key_information( derived_key,
3708 &got_type,
3709 &got_bits ) == PSA_SUCCESS );
3710 TEST_ASSERT( got_type == derived_type );
3711 TEST_ASSERT( got_bits == derived_bits );
3712
3713 /* Exercise the derived key. */
3714 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3715 goto exit;
3716
3717exit:
3718 psa_generator_abort( &generator );
3719 psa_destroy_key( base_key );
3720 psa_destroy_key( derived_key );
3721 mbedtls_psa_crypto_free( );
3722}
3723/* END_CASE */
3724
3725/* BEGIN_CASE */
3726void derive_key_export( int alg_arg,
3727 data_t *key_data,
3728 data_t *salt,
3729 data_t *label,
3730 int bytes1_arg,
3731 int bytes2_arg )
3732{
3733 psa_key_slot_t base_key = 1;
3734 psa_key_slot_t derived_key = 2;
3735 psa_algorithm_t alg = alg_arg;
3736 size_t bytes1 = bytes1_arg;
3737 size_t bytes2 = bytes2_arg;
3738 size_t capacity = bytes1 + bytes2;
3739 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003740 uint8_t *output_buffer = NULL;
3741 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003742 psa_key_policy_t policy;
3743 size_t length;
3744
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003745 ASSERT_ALLOC( output_buffer, capacity );
3746 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003747 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3748
3749 psa_key_policy_init( &policy );
3750 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3751 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3752 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3753 key_data->x,
3754 key_data->len ) == PSA_SUCCESS );
3755
3756 /* Derive some material and output it. */
3757 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3758 salt->x, salt->len,
3759 label->x, label->len,
3760 capacity ) == PSA_SUCCESS );
3761 TEST_ASSERT( psa_generator_read( &generator,
3762 output_buffer,
3763 capacity ) == PSA_SUCCESS );
3764 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3765
3766 /* Derive the same output again, but this time store it in key objects. */
3767 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3768 salt->x, salt->len,
3769 label->x, label->len,
3770 capacity ) == PSA_SUCCESS );
3771 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3772 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3773 TEST_ASSERT( psa_generator_import_key( derived_key,
3774 PSA_KEY_TYPE_RAW_DATA,
3775 PSA_BYTES_TO_BITS( bytes1 ),
3776 &generator ) == PSA_SUCCESS );
3777 TEST_ASSERT( psa_export_key( derived_key,
3778 export_buffer, bytes1,
3779 &length ) == PSA_SUCCESS );
3780 TEST_ASSERT( length == bytes1 );
3781 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3782 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3783 TEST_ASSERT( psa_generator_import_key( derived_key,
3784 PSA_KEY_TYPE_RAW_DATA,
3785 PSA_BYTES_TO_BITS( bytes2 ),
3786 &generator ) == PSA_SUCCESS );
3787 TEST_ASSERT( psa_export_key( derived_key,
3788 export_buffer + bytes1, bytes2,
3789 &length ) == PSA_SUCCESS );
3790 TEST_ASSERT( length == bytes2 );
3791
3792 /* Compare the outputs from the two runs. */
3793 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3794
3795exit:
3796 mbedtls_free( output_buffer );
3797 mbedtls_free( export_buffer );
3798 psa_generator_abort( &generator );
3799 psa_destroy_key( base_key );
3800 psa_destroy_key( derived_key );
3801 mbedtls_psa_crypto_free( );
3802}
3803/* END_CASE */
3804
3805/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003806void key_agreement_setup( int alg_arg,
3807 int our_key_type_arg, data_t *our_key_data,
3808 data_t *peer_key_data,
3809 int expected_status_arg )
3810{
3811 psa_key_slot_t our_key = 1;
3812 psa_algorithm_t alg = alg_arg;
3813 psa_key_type_t our_key_type = our_key_type_arg;
3814 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3815 psa_key_policy_t policy;
3816
3817 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3818
3819 psa_key_policy_init( &policy );
3820 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3821 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3822 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3823 our_key_data->x,
3824 our_key_data->len ) == PSA_SUCCESS );
3825
3826 TEST_ASSERT( psa_key_agreement( &generator,
3827 our_key,
3828 peer_key_data->x, peer_key_data->len,
3829 alg ) == expected_status_arg );
3830
3831exit:
3832 psa_generator_abort( &generator );
3833 psa_destroy_key( our_key );
3834 mbedtls_psa_crypto_free( );
3835}
3836/* END_CASE */
3837
3838/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003839void key_agreement_capacity( int alg_arg,
3840 int our_key_type_arg, data_t *our_key_data,
3841 data_t *peer_key_data,
3842 int expected_capacity_arg )
3843{
3844 psa_key_slot_t our_key = 1;
3845 psa_algorithm_t alg = alg_arg;
3846 psa_key_type_t our_key_type = our_key_type_arg;
3847 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3848 psa_key_policy_t policy;
3849 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003850 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003851
3852 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3853
3854 psa_key_policy_init( &policy );
3855 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3856 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3857 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3858 our_key_data->x,
3859 our_key_data->len ) == PSA_SUCCESS );
3860
3861 TEST_ASSERT( psa_key_agreement( &generator,
3862 our_key,
3863 peer_key_data->x, peer_key_data->len,
3864 alg ) == PSA_SUCCESS );
3865
Gilles Peskinebf491972018-10-25 22:36:12 +02003866 /* Test the advertized capacity. */
Gilles Peskine59685592018-09-18 12:11:34 +02003867 TEST_ASSERT( psa_get_generator_capacity(
3868 &generator, &actual_capacity ) == PSA_SUCCESS );
3869 TEST_ASSERT( actual_capacity == (size_t) expected_capacity_arg );
3870
Gilles Peskinebf491972018-10-25 22:36:12 +02003871 /* Test the actual capacity by reading the output. */
3872 while( actual_capacity > sizeof( output ) )
3873 {
3874 TEST_ASSERT( psa_generator_read( &generator,
3875 output, sizeof( output ) ) ==
3876 PSA_SUCCESS );
3877 actual_capacity -= sizeof( output );
3878 }
3879 TEST_ASSERT( psa_generator_read( &generator,
3880 output, actual_capacity ) ==
3881 PSA_SUCCESS );
3882 TEST_ASSERT( psa_generator_read( &generator, output, 1 ) ==
3883 PSA_ERROR_INSUFFICIENT_CAPACITY );
3884
Gilles Peskine59685592018-09-18 12:11:34 +02003885exit:
3886 psa_generator_abort( &generator );
3887 psa_destroy_key( our_key );
3888 mbedtls_psa_crypto_free( );
3889}
3890/* END_CASE */
3891
3892/* BEGIN_CASE */
3893void key_agreement_output( int alg_arg,
3894 int our_key_type_arg, data_t *our_key_data,
3895 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003896 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003897{
3898 psa_key_slot_t our_key = 1;
3899 psa_algorithm_t alg = alg_arg;
3900 psa_key_type_t our_key_type = our_key_type_arg;
3901 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3902 psa_key_policy_t policy;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003903 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003904
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003905 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3906 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003907
3908 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3909
3910 psa_key_policy_init( &policy );
3911 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3912 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3913 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3914 our_key_data->x,
3915 our_key_data->len ) == PSA_SUCCESS );
3916
3917 TEST_ASSERT( psa_key_agreement( &generator,
3918 our_key,
3919 peer_key_data->x, peer_key_data->len,
3920 alg ) == PSA_SUCCESS );
3921
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003922 TEST_ASSERT(
3923 psa_generator_read( &generator,
3924 actual_output,
3925 expected_output1->len ) == PSA_SUCCESS );
3926 TEST_ASSERT( memcmp( actual_output, expected_output1->x,
3927 expected_output1->len ) == 0 );
3928 if( expected_output2->len != 0 )
3929 {
3930 TEST_ASSERT(
3931 psa_generator_read( &generator,
3932 actual_output,
3933 expected_output2->len ) == PSA_SUCCESS );
3934 TEST_ASSERT( memcmp( actual_output, expected_output2->x,
3935 expected_output2->len ) == 0 );
3936 }
Gilles Peskine59685592018-09-18 12:11:34 +02003937
3938exit:
3939 psa_generator_abort( &generator );
3940 psa_destroy_key( our_key );
3941 mbedtls_psa_crypto_free( );
3942 mbedtls_free( actual_output );
3943}
3944/* END_CASE */
3945
3946/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003947void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003948{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003949 size_t bytes = bytes_arg;
3950 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003951 unsigned char *output = NULL;
3952 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003953 size_t i;
3954 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003955
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003956 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3957 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003958 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003959
3960 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3961
Gilles Peskinea50d7392018-06-21 10:22:13 +02003962 /* Run several times, to ensure that every output byte will be
3963 * nonzero at least once with overwhelming probability
3964 * (2^(-8*number_of_runs)). */
3965 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003966 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003967 if( bytes != 0 )
3968 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003969 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3970
3971 /* Check that no more than bytes have been overwritten */
3972 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3973
3974 for( i = 0; i < bytes; i++ )
3975 {
3976 if( output[i] != 0 )
3977 ++changed[i];
3978 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003979 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003980
3981 /* Check that every byte was changed to nonzero at least once. This
3982 * validates that psa_generate_random is overwriting every byte of
3983 * the output buffer. */
3984 for( i = 0; i < bytes; i++ )
3985 {
3986 TEST_ASSERT( changed[i] != 0 );
3987 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003988
3989exit:
3990 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003991 mbedtls_free( output );
3992 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003993}
3994/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003995
3996/* BEGIN_CASE */
3997void generate_key( int type_arg,
3998 int bits_arg,
3999 int usage_arg,
4000 int alg_arg,
4001 int expected_status_arg )
4002{
4003 int slot = 1;
4004 psa_key_type_t type = type_arg;
4005 psa_key_usage_t usage = usage_arg;
4006 size_t bits = bits_arg;
4007 psa_algorithm_t alg = alg_arg;
4008 psa_status_t expected_status = expected_status_arg;
4009 psa_key_type_t got_type;
4010 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004011 psa_status_t expected_info_status =
4012 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
4013 psa_key_policy_t policy;
4014
4015 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
4016
4017 psa_key_policy_init( &policy );
4018 psa_key_policy_set_usage( &policy, usage, alg );
4019 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
4020
4021 /* Generate a key */
4022 TEST_ASSERT( psa_generate_key( slot, type, bits,
4023 NULL, 0 ) == expected_status );
4024
4025 /* Test the key information */
4026 TEST_ASSERT( psa_get_key_information( slot,
4027 &got_type,
4028 &got_bits ) == expected_info_status );
4029 if( expected_info_status != PSA_SUCCESS )
4030 goto exit;
4031 TEST_ASSERT( got_type == type );
4032 TEST_ASSERT( got_bits == bits );
4033
Gilles Peskine818ca122018-06-20 18:16:48 +02004034 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02004035 if( ! exercise_key( slot, usage, alg ) )
4036 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004037
4038exit:
4039 psa_destroy_key( slot );
4040 mbedtls_psa_crypto_free( );
4041}
4042/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004043
Darryl Greend49a4992018-06-18 17:27:26 +01004044/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4045void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4046 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004047 int alg_arg, int generation_method,
4048 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004049{
4050 psa_key_slot_t slot = 1;
Darryl Green0c6575a2018-11-07 16:05:30 +00004051 psa_key_slot_t base_key = 2;
Darryl Greend49a4992018-06-18 17:27:26 +01004052 psa_key_type_t type = (psa_key_type_t) type_arg;
4053 psa_key_type_t type_get;
4054 size_t bits_get;
4055 psa_key_policy_t policy_set;
4056 psa_key_policy_t policy_get;
4057 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4058 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004059 psa_key_policy_t base_policy_set;
4060 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4061 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004062 unsigned char *first_export = NULL;
4063 unsigned char *second_export = NULL;
4064 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4065 size_t first_exported_length;
4066 size_t second_exported_length;
4067
4068 ASSERT_ALLOC( first_export, export_size );
4069 ASSERT_ALLOC( second_export, export_size );
4070
4071 TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS );
4072
4073 TEST_ASSERT( psa_set_key_lifetime(
4074 slot, PSA_KEY_LIFETIME_PERSISTENT ) == PSA_SUCCESS );
4075
4076 psa_key_policy_init( &policy_set );
4077
4078 psa_key_policy_set_usage( &policy_set, policy_usage,
4079 policy_alg );
4080
4081 TEST_ASSERT( psa_set_key_policy( slot, &policy_set ) == PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00004082 switch( generation_method )
4083 {
4084 case IMPORT_KEY:
4085 /* Import the key */
4086 TEST_ASSERT( psa_import_key( slot, type,
4087 data->x, data->len ) == PSA_SUCCESS );
4088 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004089
Darryl Green0c6575a2018-11-07 16:05:30 +00004090 case GENERATE_KEY:
4091 /* Generate a key */
4092 TEST_ASSERT( psa_generate_key( slot, type, bits,
4093 NULL, 0 ) == PSA_SUCCESS );
4094 break;
4095
4096 case DERIVE_KEY:
4097 /* Create base key */
4098 psa_key_policy_init( &base_policy_set );
4099
4100 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4101 base_policy_alg );
4102 TEST_ASSERT( psa_set_key_policy(
4103 base_key, &base_policy_set ) == PSA_SUCCESS );
4104 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4105 data->x, data->len ) == PSA_SUCCESS );
4106 /* Derive a key. */
4107 TEST_ASSERT( psa_key_derivation( &generator, base_key,
4108 base_policy_alg,
4109 NULL, 0, NULL, 0,
4110 export_size ) == PSA_SUCCESS );
4111 TEST_ASSERT( psa_generator_import_key(
4112 slot, PSA_KEY_TYPE_RAW_DATA,
4113 bits, &generator ) == PSA_SUCCESS );
4114 break;
4115 }
Darryl Greend49a4992018-06-18 17:27:26 +01004116
4117 /* Export the key */
4118 TEST_ASSERT( psa_export_key( slot, first_export, export_size,
Darryl Green0c6575a2018-11-07 16:05:30 +00004119 &first_exported_length ) == export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004120
4121 /* Shutdown and restart */
4122 mbedtls_psa_crypto_free();
4123
4124 TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS );
4125
4126 /* Mark slot as persistent again */
4127 TEST_ASSERT( psa_set_key_lifetime(
4128 slot, PSA_KEY_LIFETIME_PERSISTENT ) == PSA_SUCCESS );
4129
4130 /* Check key slot still contains key data */
4131 TEST_ASSERT( psa_get_key_information(
4132 slot, &type_get, &bits_get ) == PSA_SUCCESS );
4133 TEST_ASSERT( type_get == type );
4134 TEST_ASSERT( bits_get == (size_t) bits );
4135
4136 TEST_ASSERT( psa_get_key_policy( slot, &policy_get ) == PSA_SUCCESS );
4137 TEST_ASSERT( psa_key_policy_get_usage(
4138 &policy_get ) == policy_usage );
4139 TEST_ASSERT( psa_key_policy_get_algorithm(
4140 &policy_get ) == policy_alg );
4141
4142 /* Export the key again */
4143 TEST_ASSERT( psa_export_key( slot, second_export, export_size,
Darryl Green0c6575a2018-11-07 16:05:30 +00004144 &second_exported_length ) == export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004145
Darryl Green0c6575a2018-11-07 16:05:30 +00004146 if( export_status == PSA_SUCCESS )
4147 {
4148 ASSERT_COMPARE( first_export, first_exported_length,
4149 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004150
Darryl Green0c6575a2018-11-07 16:05:30 +00004151 switch( generation_method )
4152 {
4153 case IMPORT_KEY:
4154 ASSERT_COMPARE( data->x, data->len,
4155 first_export, first_exported_length );
4156 break;
4157 default:
4158 break;
4159 }
4160 }
4161
4162 /* Do something with the key according to its type and permitted usage. */
4163 if( ! exercise_key( slot, policy_usage, policy_alg ) )
4164 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004165
4166exit:
4167 mbedtls_free( first_export );
4168 mbedtls_free( second_export );
4169 psa_destroy_key( slot );
4170 mbedtls_psa_crypto_free();
4171}
4172/* END_CASE */