blob: 8b3c79428f0ca1d95bf0aa42c9f9560205ec450d [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 Peskineb866e2b2018-06-21 09:25:10 +0200875void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876{
877 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200878 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100879 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100880
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300882 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
884
Gilles Peskine4abf7412018-06-18 16:35:34 +0200885 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200886 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887 if( status == PSA_SUCCESS )
888 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
889
890exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891 mbedtls_psa_crypto_free( );
892}
893/* END_CASE */
894
895/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100896void import_twice( int alg_arg, int usage_arg,
897 int type1_arg, data_t *data1,
898 int expected_import1_status_arg,
899 int type2_arg, data_t *data2,
900 int expected_import2_status_arg )
901{
902 int slot = 1;
903 psa_algorithm_t alg = alg_arg;
904 psa_key_usage_t usage = usage_arg;
905 psa_key_type_t type1 = type1_arg;
906 psa_status_t expected_import1_status = expected_import1_status_arg;
907 psa_key_type_t type2 = type2_arg;
908 psa_status_t expected_import2_status = expected_import2_status_arg;
909 psa_key_policy_t policy;
910 psa_status_t status;
911
912 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
913
914 psa_key_policy_init( &policy );
915 psa_key_policy_set_usage( &policy, usage, alg );
916 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
917
918 status = psa_import_key( slot, type1, data1->x, data1->len );
919 TEST_ASSERT( status == expected_import1_status );
920 status = psa_import_key( slot, type2, data2->x, data2->len );
921 TEST_ASSERT( status == expected_import2_status );
922
923 if( expected_import1_status == PSA_SUCCESS ||
924 expected_import2_status == PSA_SUCCESS )
925 {
926 TEST_ASSERT( exercise_key( slot, usage, alg ) );
927 }
928
929exit:
930 mbedtls_psa_crypto_free( );
931}
932/* END_CASE */
933
934/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200935void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
936{
937 int slot = 1;
938 size_t bits = bits_arg;
939 psa_status_t expected_status = expected_status_arg;
940 psa_status_t status;
941 psa_key_type_t type =
942 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
943 size_t buffer_size = /* Slight overapproximations */
944 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200945 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200946 unsigned char *p;
947 int ret;
948 size_t length;
949
950 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200951 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200952
953 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
954 bits, keypair ) ) >= 0 );
955 length = ret;
956
957 /* Try importing the key */
958 status = psa_import_key( slot, type, p, length );
959 TEST_ASSERT( status == expected_status );
960 if( status == PSA_SUCCESS )
961 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
962
963exit:
964 mbedtls_free( buffer );
965 mbedtls_psa_crypto_free( );
966}
967/* END_CASE */
968
969/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300970void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300971 int type_arg,
972 int alg_arg,
973 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974 int expected_bits,
975 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200976 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 int canonical_input )
978{
979 int slot = 1;
980 int slot2 = slot + 1;
981 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200982 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200983 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985 unsigned char *exported = NULL;
986 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100988 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 size_t reexported_length;
990 psa_key_type_t got_type;
991 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200992 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300995 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300996 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200997 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200999 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1001
mohammad1603a97cb8c2018-03-28 03:46:26 -07001002 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001003 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001004 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1005
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001006 /* Import the key */
1007 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001008 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009
1010 /* Test the key information */
1011 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001012 &got_type,
1013 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 TEST_ASSERT( got_type == type );
1015 TEST_ASSERT( got_bits == (size_t) expected_bits );
1016
1017 /* Export the key */
1018 status = psa_export_key( slot,
1019 exported, export_size,
1020 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001021 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001022
1023 /* The exported length must be set by psa_export_key() to a value between 0
1024 * and export_size. On errors, the exported length must be 0. */
1025 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1026 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1027 TEST_ASSERT( exported_length <= export_size );
1028
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001029 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001030 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001032 {
1033 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001035 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036
Gilles Peskine8f609232018-08-11 01:24:55 +02001037 if( ! exercise_export_key( slot, usage_arg ) )
1038 goto exit;
1039
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001041 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042 else
1043 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001044 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1045
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001046 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001047 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001048 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001050 reexported,
1051 export_size,
1052 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001053 ASSERT_COMPARE( exported, exported_length,
1054 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001055 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001056 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057
1058destroy:
1059 /* Destroy the key */
1060 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1061 TEST_ASSERT( psa_get_key_information(
1062 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1063
1064exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001065 mbedtls_free( exported );
1066 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001067 mbedtls_psa_crypto_free( );
1068}
1069/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001070
Moran Pekerf709f4a2018-06-06 17:26:04 +03001071/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001072void import_key_nonempty_slot( )
1073{
1074 int slot = 1;
1075 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1076 psa_status_t status;
1077 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
1078 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1079
1080 /* Import the key */
1081 TEST_ASSERT( psa_import_key( slot, type,
1082 data, sizeof( data ) ) == PSA_SUCCESS );
1083
1084 /* Import the key again */
1085 status = psa_import_key( slot, type, data, sizeof( data ) );
1086 TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT );
1087
1088exit:
1089 mbedtls_psa_crypto_free( );
1090}
1091/* END_CASE */
1092
1093/* BEGIN_CASE */
1094void export_invalid_slot( int slot, int expected_export_status_arg )
1095{
1096 psa_status_t status;
1097 unsigned char *exported = NULL;
1098 size_t export_size = 0;
1099 size_t exported_length = INVALID_EXPORT_LENGTH;
1100 psa_status_t expected_export_status = expected_export_status_arg;
1101
1102 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1103
1104 /* Export the key */
1105 status = psa_export_key( slot,
1106 exported, export_size,
1107 &exported_length );
1108 TEST_ASSERT( status == expected_export_status );
1109
1110exit:
1111 mbedtls_psa_crypto_free( );
1112}
1113/* END_CASE */
1114
1115/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001116void export_with_no_key_activity( )
1117{
1118 int slot = 1;
1119 psa_algorithm_t alg = PSA_ALG_CTR;
1120 psa_status_t status;
1121 psa_key_policy_t policy;
1122 unsigned char *exported = NULL;
1123 size_t export_size = 0;
1124 size_t exported_length = INVALID_EXPORT_LENGTH;
1125
1126 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1127
1128 psa_key_policy_init( &policy );
1129 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1130 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1131
1132 /* Export the key */
1133 status = psa_export_key( slot,
1134 exported, export_size,
1135 &exported_length );
1136 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1137
1138exit:
1139 mbedtls_psa_crypto_free( );
1140}
1141/* END_CASE */
1142
1143/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001144void cipher_with_no_key_activity( )
1145{
1146 int slot = 1;
1147 psa_status_t status;
1148 psa_key_policy_t policy;
1149 psa_cipher_operation_t operation;
1150 int exercise_alg = PSA_ALG_CTR;
1151
1152 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1153
1154 psa_key_policy_init( &policy );
1155 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
1156 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1157
1158 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1159 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1160
1161exit:
1162 psa_cipher_abort( &operation );
1163 mbedtls_psa_crypto_free( );
1164}
1165/* END_CASE */
1166
1167/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001168void export_after_import_failure( data_t *data, int type_arg,
1169 int expected_import_status_arg )
1170{
1171 int slot = 1;
1172 psa_key_type_t type = type_arg;
1173 psa_status_t status;
1174 unsigned char *exported = NULL;
1175 size_t export_size = 0;
1176 psa_status_t expected_import_status = expected_import_status_arg;
1177 size_t exported_length = INVALID_EXPORT_LENGTH;
1178
1179 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1180
1181 /* Import the key - expect failure */
1182 status = psa_import_key( slot, type,
1183 data->x, data->len );
1184 TEST_ASSERT( status == expected_import_status );
1185
1186 /* Export the key */
1187 status = psa_export_key( slot,
1188 exported, export_size,
1189 &exported_length );
1190 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1191
1192exit:
1193 mbedtls_psa_crypto_free( );
1194}
1195/* END_CASE */
1196
1197/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001198void cipher_after_import_failure( data_t *data, int type_arg,
1199 int expected_import_status_arg )
1200{
1201 int slot = 1;
1202 psa_cipher_operation_t operation;
1203 psa_key_type_t type = type_arg;
1204 psa_status_t status;
1205 psa_status_t expected_import_status = expected_import_status_arg;
1206 int exercise_alg = PSA_ALG_CTR;
1207
1208 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1209
1210 /* Import the key - expect failure */
1211 status = psa_import_key( slot, type,
1212 data->x, data->len );
1213 TEST_ASSERT( status == expected_import_status );
1214
1215 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1216 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1217
1218exit:
1219 psa_cipher_abort( &operation );
1220 mbedtls_psa_crypto_free( );
1221}
1222/* END_CASE */
1223
1224/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001225void export_after_destroy_key( data_t *data, int type_arg )
1226{
1227 int slot = 1;
1228 psa_key_type_t type = type_arg;
1229 psa_status_t status;
1230 psa_key_policy_t policy;
1231 psa_algorithm_t alg = PSA_ALG_CTR;
1232 unsigned char *exported = NULL;
1233 size_t export_size = 0;
1234 size_t exported_length = INVALID_EXPORT_LENGTH;
1235
1236 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1237
1238 psa_key_policy_init( &policy );
1239 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1240 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1241 export_size = (ptrdiff_t) data->len;
1242 ASSERT_ALLOC( exported, export_size );
1243
1244 /* Import the key */
1245 TEST_ASSERT( psa_import_key( slot, type,
1246 data->x, data->len ) == PSA_SUCCESS );
1247
1248 TEST_ASSERT( psa_export_key( slot, exported, export_size,
1249 &exported_length ) == PSA_SUCCESS );
1250
1251 /* Destroy the key */
1252 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1253
1254 /* Export the key */
1255 status = psa_export_key( slot, exported, export_size,
1256 &exported_length );
1257 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1258
1259exit:
1260 mbedtls_free( exported );
1261 mbedtls_psa_crypto_free( );
1262}
1263/* END_CASE */
1264
1265/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001266void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001267 int type_arg,
1268 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001269 int export_size_delta,
1270 int expected_export_status_arg,
1271 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001272{
1273 int slot = 1;
1274 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001275 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001276 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001277 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001278 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001279 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001280 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001281 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001282
Moran Pekerf709f4a2018-06-06 17:26:04 +03001283 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1284
1285 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001286 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001287 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1288
1289 /* Import the key */
1290 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001291 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001292
Gilles Peskine49c25912018-10-29 15:15:31 +01001293 /* Export the public key */
1294 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001295 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001296 exported, export_size,
1297 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001298 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001299 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001300 {
1301 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1302 size_t bits;
1303 TEST_ASSERT( psa_get_key_information( slot, NULL, &bits ) ==
1304 PSA_SUCCESS );
1305 TEST_ASSERT( expected_public_key->len <=
1306 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001307 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1308 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001309 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001310
1311exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001312 mbedtls_free( exported );
Gilles Peskine49c25912018-10-29 15:15:31 +01001313 psa_destroy_key( slot );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001314 mbedtls_psa_crypto_free( );
1315}
1316/* END_CASE */
1317
Gilles Peskine20035e32018-02-03 22:44:14 +01001318/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001319void import_and_exercise_key( data_t *data,
1320 int type_arg,
1321 int bits_arg,
1322 int alg_arg )
1323{
1324 int slot = 1;
1325 psa_key_type_t type = type_arg;
1326 size_t bits = bits_arg;
1327 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001328 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001329 psa_key_policy_t policy;
1330 psa_key_type_t got_type;
1331 size_t got_bits;
1332 psa_status_t status;
1333
1334 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1335
1336 psa_key_policy_init( &policy );
1337 psa_key_policy_set_usage( &policy, usage, alg );
1338 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1339
1340 /* Import the key */
1341 status = psa_import_key( slot, type, data->x, data->len );
1342 TEST_ASSERT( status == PSA_SUCCESS );
1343
1344 /* Test the key information */
1345 TEST_ASSERT( psa_get_key_information( slot,
1346 &got_type,
1347 &got_bits ) == PSA_SUCCESS );
1348 TEST_ASSERT( got_type == type );
1349 TEST_ASSERT( got_bits == bits );
1350
1351 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001352 if( ! exercise_key( slot, usage, alg ) )
1353 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001354
1355exit:
1356 psa_destroy_key( slot );
1357 mbedtls_psa_crypto_free( );
1358}
1359/* END_CASE */
1360
1361/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001362void key_policy( int usage_arg, int alg_arg )
1363{
1364 int key_slot = 1;
1365 psa_algorithm_t alg = alg_arg;
1366 psa_key_usage_t usage = usage_arg;
1367 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1368 unsigned char key[32] = {0};
1369 psa_key_policy_t policy_set;
1370 psa_key_policy_t policy_get;
1371
1372 memset( key, 0x2a, sizeof( key ) );
1373
1374 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1375
1376 psa_key_policy_init( &policy_set );
1377 psa_key_policy_init( &policy_get );
1378
1379 psa_key_policy_set_usage( &policy_set, usage, alg );
1380
1381 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1382 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1383 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1384
1385 TEST_ASSERT( psa_import_key( key_slot, key_type,
1386 key, sizeof( key ) ) == PSA_SUCCESS );
1387
1388 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1389
1390 TEST_ASSERT( policy_get.usage == policy_set.usage );
1391 TEST_ASSERT( policy_get.alg == policy_set.alg );
1392
1393exit:
1394 psa_destroy_key( key_slot );
1395 mbedtls_psa_crypto_free( );
1396}
1397/* END_CASE */
1398
1399/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001400void mac_key_policy( int policy_usage,
1401 int policy_alg,
1402 int key_type,
1403 data_t *key_data,
1404 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001405{
1406 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001407 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001408 psa_mac_operation_t operation;
1409 psa_status_t status;
1410 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001411
1412 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1413
1414 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001415 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001416 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1417
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001418 TEST_ASSERT( psa_import_key( key_slot, key_type,
1419 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001420
Gilles Peskine89167cb2018-07-08 20:12:23 +02001421 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001422 if( policy_alg == exercise_alg &&
1423 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1424 TEST_ASSERT( status == PSA_SUCCESS );
1425 else
1426 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1427 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001428
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001429 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001430 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001431 if( policy_alg == exercise_alg &&
1432 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001434 else
1435 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1436
1437exit:
1438 psa_mac_abort( &operation );
1439 psa_destroy_key( key_slot );
1440 mbedtls_psa_crypto_free( );
1441}
1442/* END_CASE */
1443
1444/* BEGIN_CASE */
1445void cipher_key_policy( int policy_usage,
1446 int policy_alg,
1447 int key_type,
1448 data_t *key_data,
1449 int exercise_alg )
1450{
1451 int key_slot = 1;
1452 psa_key_policy_t policy;
1453 psa_cipher_operation_t operation;
1454 psa_status_t status;
1455
1456 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1457
1458 psa_key_policy_init( &policy );
1459 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1460 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1461
1462 TEST_ASSERT( psa_import_key( key_slot, key_type,
1463 key_data->x, key_data->len ) == PSA_SUCCESS );
1464
Gilles Peskinefe119512018-07-08 21:39:34 +02001465 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001466 if( policy_alg == exercise_alg &&
1467 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1468 TEST_ASSERT( status == PSA_SUCCESS );
1469 else
1470 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1471 psa_cipher_abort( &operation );
1472
Gilles Peskinefe119512018-07-08 21:39:34 +02001473 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001474 if( policy_alg == exercise_alg &&
1475 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1476 TEST_ASSERT( status == PSA_SUCCESS );
1477 else
1478 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1479
1480exit:
1481 psa_cipher_abort( &operation );
1482 psa_destroy_key( key_slot );
1483 mbedtls_psa_crypto_free( );
1484}
1485/* END_CASE */
1486
1487/* BEGIN_CASE */
1488void aead_key_policy( int policy_usage,
1489 int policy_alg,
1490 int key_type,
1491 data_t *key_data,
1492 int nonce_length_arg,
1493 int tag_length_arg,
1494 int exercise_alg )
1495{
1496 int key_slot = 1;
1497 psa_key_policy_t policy;
1498 psa_status_t status;
1499 unsigned char nonce[16] = {0};
1500 size_t nonce_length = nonce_length_arg;
1501 unsigned char tag[16];
1502 size_t tag_length = tag_length_arg;
1503 size_t output_length;
1504
1505 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1506 TEST_ASSERT( tag_length <= sizeof( tag ) );
1507
1508 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1509
1510 psa_key_policy_init( &policy );
1511 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1512 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1513
1514 TEST_ASSERT( psa_import_key( key_slot, key_type,
1515 key_data->x, key_data->len ) == PSA_SUCCESS );
1516
1517 status = psa_aead_encrypt( key_slot, exercise_alg,
1518 nonce, nonce_length,
1519 NULL, 0,
1520 NULL, 0,
1521 tag, tag_length,
1522 &output_length );
1523 if( policy_alg == exercise_alg &&
1524 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1525 TEST_ASSERT( status == PSA_SUCCESS );
1526 else
1527 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1528
1529 memset( tag, 0, sizeof( tag ) );
1530 status = psa_aead_decrypt( key_slot, exercise_alg,
1531 nonce, nonce_length,
1532 NULL, 0,
1533 tag, tag_length,
1534 NULL, 0,
1535 &output_length );
1536 if( policy_alg == exercise_alg &&
1537 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1538 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1539 else
1540 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1541
1542exit:
1543 psa_destroy_key( key_slot );
1544 mbedtls_psa_crypto_free( );
1545}
1546/* END_CASE */
1547
1548/* BEGIN_CASE */
1549void asymmetric_encryption_key_policy( int policy_usage,
1550 int policy_alg,
1551 int key_type,
1552 data_t *key_data,
1553 int exercise_alg )
1554{
1555 int key_slot = 1;
1556 psa_key_policy_t policy;
1557 psa_status_t status;
1558 size_t key_bits;
1559 size_t buffer_length;
1560 unsigned char *buffer = NULL;
1561 size_t output_length;
1562
1563 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1564
1565 psa_key_policy_init( &policy );
1566 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1567 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1568
1569 TEST_ASSERT( psa_import_key( key_slot, key_type,
1570 key_data->x, key_data->len ) == PSA_SUCCESS );
1571
1572 TEST_ASSERT( psa_get_key_information( key_slot,
1573 NULL,
1574 &key_bits ) == PSA_SUCCESS );
1575 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1576 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001577 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001578
1579 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1580 NULL, 0,
1581 NULL, 0,
1582 buffer, buffer_length,
1583 &output_length );
1584 if( policy_alg == exercise_alg &&
1585 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1586 TEST_ASSERT( status == PSA_SUCCESS );
1587 else
1588 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1589
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001590 if( buffer_length != 0 )
1591 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001592 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1593 buffer, buffer_length,
1594 NULL, 0,
1595 buffer, buffer_length,
1596 &output_length );
1597 if( policy_alg == exercise_alg &&
1598 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1599 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1600 else
1601 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1602
1603exit:
1604 psa_destroy_key( key_slot );
1605 mbedtls_psa_crypto_free( );
1606 mbedtls_free( buffer );
1607}
1608/* END_CASE */
1609
1610/* BEGIN_CASE */
1611void asymmetric_signature_key_policy( int policy_usage,
1612 int policy_alg,
1613 int key_type,
1614 data_t *key_data,
1615 int exercise_alg )
1616{
1617 int key_slot = 1;
1618 psa_key_policy_t policy;
1619 psa_status_t status;
1620 unsigned char payload[16] = {1};
1621 size_t payload_length = sizeof( payload );
1622 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1623 size_t signature_length;
1624
1625 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1626
1627 psa_key_policy_init( &policy );
1628 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1629 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1630
1631 TEST_ASSERT( psa_import_key( key_slot, key_type,
1632 key_data->x, key_data->len ) == PSA_SUCCESS );
1633
1634 status = psa_asymmetric_sign( key_slot, exercise_alg,
1635 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001636 signature, sizeof( signature ),
1637 &signature_length );
1638 if( policy_alg == exercise_alg &&
1639 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1640 TEST_ASSERT( status == PSA_SUCCESS );
1641 else
1642 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1643
1644 memset( signature, 0, sizeof( signature ) );
1645 status = psa_asymmetric_verify( key_slot, exercise_alg,
1646 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001647 signature, sizeof( signature ) );
1648 if( policy_alg == exercise_alg &&
1649 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1650 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1651 else
1652 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001653
1654exit:
1655 psa_destroy_key( key_slot );
1656 mbedtls_psa_crypto_free( );
1657}
1658/* END_CASE */
1659
1660/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001661void derive_key_policy( int policy_usage,
1662 int policy_alg,
1663 int key_type,
1664 data_t *key_data,
1665 int exercise_alg )
1666{
1667 int key_slot = 1;
1668 psa_key_policy_t policy;
1669 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1670 psa_status_t status;
1671
1672 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1673
1674 psa_key_policy_init( &policy );
1675 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1676 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1677
1678 TEST_ASSERT( psa_import_key( key_slot, key_type,
1679 key_data->x, key_data->len ) == PSA_SUCCESS );
1680
1681 status = psa_key_derivation( &generator, key_slot,
1682 exercise_alg,
1683 NULL, 0,
1684 NULL, 0,
1685 1 );
1686 if( policy_alg == exercise_alg &&
1687 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1688 TEST_ASSERT( status == PSA_SUCCESS );
1689 else
1690 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1691
1692exit:
1693 psa_generator_abort( &generator );
1694 psa_destroy_key( key_slot );
1695 mbedtls_psa_crypto_free( );
1696}
1697/* END_CASE */
1698
1699/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001700void agreement_key_policy( int policy_usage,
1701 int policy_alg,
1702 int key_type_arg,
1703 data_t *key_data,
1704 int exercise_alg )
1705{
1706 int key_slot = 1;
1707 psa_key_policy_t policy;
1708 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001709 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1710 psa_status_t status;
1711
1712 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1713
1714 psa_key_policy_init( &policy );
1715 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1716 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1717
1718 TEST_ASSERT( psa_import_key( key_slot, key_type,
1719 key_data->x, key_data->len ) == PSA_SUCCESS );
1720
Gilles Peskinec7998b72018-11-07 18:45:02 +01001721 status = key_agreement_with_self( &generator, key_slot, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001722
Gilles Peskine01d718c2018-09-18 12:01:02 +02001723 if( policy_alg == exercise_alg &&
1724 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1725 TEST_ASSERT( status == PSA_SUCCESS );
1726 else
1727 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1728
1729exit:
1730 psa_generator_abort( &generator );
1731 psa_destroy_key( key_slot );
1732 mbedtls_psa_crypto_free( );
1733}
1734/* END_CASE */
1735
1736/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001737void hash_setup( int alg_arg,
1738 int expected_status_arg )
1739{
1740 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001741 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001742 psa_hash_operation_t operation;
1743 psa_status_t status;
1744
1745 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1746
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001747 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001748 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001749 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001750
1751exit:
1752 mbedtls_psa_crypto_free( );
1753}
1754/* END_CASE */
1755
1756/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001757void hash_bad_order( )
1758{
1759 unsigned char input[] = "";
1760 /* SHA-256 hash of an empty string */
1761 unsigned char hash[] = {
1762 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1763 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1764 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1765 size_t hash_len;
1766 psa_hash_operation_t operation;
1767
1768 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1769
1770 /* psa_hash_update without calling psa_hash_setup beforehand */
1771 memset( &operation, 0, sizeof( operation ) );
1772 TEST_ASSERT( psa_hash_update( &operation,
1773 input, sizeof( input ) ) ==
1774 PSA_ERROR_INVALID_ARGUMENT );
1775
1776 /* psa_hash_verify without calling psa_hash_setup beforehand */
1777 memset( &operation, 0, sizeof( operation ) );
1778 TEST_ASSERT( psa_hash_verify( &operation,
1779 hash, sizeof( hash ) ) ==
1780 PSA_ERROR_INVALID_ARGUMENT );
1781
1782 /* psa_hash_finish without calling psa_hash_setup beforehand */
1783 memset( &operation, 0, sizeof( operation ) );
1784 TEST_ASSERT( psa_hash_finish( &operation,
1785 hash, sizeof( hash ), &hash_len ) ==
1786 PSA_ERROR_INVALID_ARGUMENT );
1787
1788exit:
1789 mbedtls_psa_crypto_free( );
1790}
1791/* END_CASE */
1792
itayzafrir27e69452018-11-01 14:26:34 +02001793/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1794void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001795{
1796 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001797 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1798 * appended to it */
1799 unsigned char hash[] = {
1800 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1801 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1802 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001803 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001804 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001805
1806 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1807
itayzafrir27e69452018-11-01 14:26:34 +02001808 /* psa_hash_verify with a smaller hash than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001809 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1810 TEST_ASSERT( psa_hash_verify( &operation,
1811 hash, expected_size - 1 ) ==
1812 PSA_ERROR_INVALID_SIGNATURE );
1813
itayzafrir27e69452018-11-01 14:26:34 +02001814 /* psa_hash_verify with a non-matching hash */
itayzafrirec93d302018-10-18 18:01:10 +03001815 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrirec93d302018-10-18 18:01:10 +03001816 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001817 hash + 1, expected_size ) ==
itayzafrirec93d302018-10-18 18:01:10 +03001818 PSA_ERROR_INVALID_SIGNATURE );
1819
itayzafrir27e69452018-11-01 14:26:34 +02001820 /* psa_hash_verify with a hash longer than expected */
itayzafrir4271df92018-10-24 18:16:19 +03001821 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrir4271df92018-10-24 18:16:19 +03001822 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001823 hash, sizeof( hash ) ) ==
itayzafrir4271df92018-10-24 18:16:19 +03001824 PSA_ERROR_INVALID_SIGNATURE );
1825
itayzafrirec93d302018-10-18 18:01:10 +03001826exit:
1827 mbedtls_psa_crypto_free( );
1828}
1829/* END_CASE */
1830
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001831/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1832void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001833{
1834 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001835 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001836 size_t expected_size = PSA_HASH_SIZE( alg );
1837 psa_hash_operation_t operation;
1838 size_t hash_len;
1839
1840 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1841
itayzafrir58028322018-10-25 10:22:01 +03001842 /* psa_hash_finish with a smaller hash buffer than expected */
1843 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1844 TEST_ASSERT( psa_hash_finish( &operation,
1845 hash, expected_size - 1,
1846 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1847
1848exit:
1849 mbedtls_psa_crypto_free( );
1850}
1851/* END_CASE */
1852
1853/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001854void mac_setup( int key_type_arg,
1855 data_t *key,
1856 int alg_arg,
1857 int expected_status_arg )
1858{
1859 int key_slot = 1;
1860 psa_key_type_t key_type = key_type_arg;
1861 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001862 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001863 psa_mac_operation_t operation;
1864 psa_key_policy_t policy;
1865 psa_status_t status;
1866
1867 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1868
1869 psa_key_policy_init( &policy );
1870 psa_key_policy_set_usage( &policy,
1871 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1872 alg );
1873 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1874
1875 TEST_ASSERT( psa_import_key( key_slot, key_type,
1876 key->x, key->len ) == PSA_SUCCESS );
1877
Gilles Peskine89167cb2018-07-08 20:12:23 +02001878 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001879 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001880 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001881
1882exit:
1883 psa_destroy_key( key_slot );
1884 mbedtls_psa_crypto_free( );
1885}
1886/* END_CASE */
1887
1888/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001889void mac_sign( int key_type_arg,
1890 data_t *key,
1891 int alg_arg,
1892 data_t *input,
1893 data_t *expected_mac )
1894{
1895 int key_slot = 1;
1896 psa_key_type_t key_type = key_type_arg;
1897 psa_algorithm_t alg = alg_arg;
1898 psa_mac_operation_t operation;
1899 psa_key_policy_t policy;
1900 /* Leave a little extra room in the output buffer. At the end of the
1901 * test, we'll check that the implementation didn't overwrite onto
1902 * this extra room. */
1903 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1904 size_t mac_buffer_size =
1905 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1906 size_t mac_length = 0;
1907
1908 memset( actual_mac, '+', sizeof( actual_mac ) );
1909 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1910 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1911
1912 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1913
1914 psa_key_policy_init( &policy );
1915 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1916 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1917
1918 TEST_ASSERT( psa_import_key( key_slot, key_type,
1919 key->x, key->len ) == PSA_SUCCESS );
1920
1921 /* Calculate the MAC. */
1922 TEST_ASSERT( psa_mac_sign_setup( &operation,
1923 key_slot, alg ) == PSA_SUCCESS );
1924 TEST_ASSERT( psa_mac_update( &operation,
1925 input->x, input->len ) == PSA_SUCCESS );
1926 TEST_ASSERT( psa_mac_sign_finish( &operation,
1927 actual_mac, mac_buffer_size,
1928 &mac_length ) == PSA_SUCCESS );
1929
1930 /* Compare with the expected value. */
1931 TEST_ASSERT( mac_length == expected_mac->len );
1932 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
1933
1934 /* Verify that the end of the buffer is untouched. */
1935 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1936 sizeof( actual_mac ) - mac_length ) );
1937
1938exit:
1939 psa_destroy_key( key_slot );
1940 mbedtls_psa_crypto_free( );
1941}
1942/* END_CASE */
1943
1944/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001945void mac_verify( int key_type_arg,
1946 data_t *key,
1947 int alg_arg,
1948 data_t *input,
1949 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001950{
1951 int key_slot = 1;
1952 psa_key_type_t key_type = key_type_arg;
1953 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001954 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001955 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001956
Gilles Peskine69c12672018-06-28 00:07:19 +02001957 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1958
Gilles Peskine8c9def32018-02-08 10:02:12 +01001959 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001960 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001961 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001962 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001963 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1964 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001965
1966 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1967
mohammad16036df908f2018-04-02 08:34:15 -07001968 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001969 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001970 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1971
Gilles Peskine8c9def32018-02-08 10:02:12 +01001972 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001973 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001974
Gilles Peskine89167cb2018-07-08 20:12:23 +02001975 TEST_ASSERT( psa_mac_verify_setup( &operation,
1976 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001977 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1978 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001979 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001980 TEST_ASSERT( psa_mac_verify_finish( &operation,
1981 expected_mac->x,
1982 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001983
1984exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001985 psa_destroy_key( key_slot );
1986 mbedtls_psa_crypto_free( );
1987}
1988/* END_CASE */
1989
1990/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001991void cipher_setup( int key_type_arg,
1992 data_t *key,
1993 int alg_arg,
1994 int expected_status_arg )
1995{
1996 int key_slot = 1;
1997 psa_key_type_t key_type = key_type_arg;
1998 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001999 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002000 psa_cipher_operation_t operation;
2001 psa_key_policy_t policy;
2002 psa_status_t status;
2003
2004 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2005
2006 psa_key_policy_init( &policy );
2007 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2008 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2009
2010 TEST_ASSERT( psa_import_key( key_slot, key_type,
2011 key->x, key->len ) == PSA_SUCCESS );
2012
Gilles Peskinefe119512018-07-08 21:39:34 +02002013 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002014 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002015 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002016
2017exit:
2018 psa_destroy_key( key_slot );
2019 mbedtls_psa_crypto_free( );
2020}
2021/* END_CASE */
2022
2023/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002024void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002025 data_t *key,
2026 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002027 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002028{
2029 int key_slot = 1;
2030 psa_status_t status;
2031 psa_key_type_t key_type = key_type_arg;
2032 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002033 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002034 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002035 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002036 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002037 size_t output_buffer_size = 0;
2038 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002039 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002040 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002041 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002042
Gilles Peskine50e586b2018-06-08 14:28:46 +02002043 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002044 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002045 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002046 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2047 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2048 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002049
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002050 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2051 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002052
2053 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2054
Moran Pekered346952018-07-05 15:22:45 +03002055 psa_key_policy_init( &policy );
2056 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2057 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2058
Gilles Peskine50e586b2018-06-08 14:28:46 +02002059 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002060 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002061
Gilles Peskinefe119512018-07-08 21:39:34 +02002062 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2063 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002064
Gilles Peskinefe119512018-07-08 21:39:34 +02002065 TEST_ASSERT( psa_cipher_set_iv( &operation,
2066 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002067 output_buffer_size = (size_t) input->len +
2068 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002069 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002070
Gilles Peskine4abf7412018-06-18 16:35:34 +02002071 TEST_ASSERT( psa_cipher_update( &operation,
2072 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002073 output, output_buffer_size,
2074 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002075 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002076 status = psa_cipher_finish( &operation,
2077 output + function_output_length,
2078 output_buffer_size,
2079 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002080 total_output_length += function_output_length;
2081
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002082 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002083 if( expected_status == PSA_SUCCESS )
2084 {
2085 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002086 ASSERT_COMPARE( expected_output->x, expected_output->len,
2087 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002088 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002089
Gilles Peskine50e586b2018-06-08 14:28:46 +02002090exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002091 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002092 psa_destroy_key( key_slot );
2093 mbedtls_psa_crypto_free( );
2094}
2095/* END_CASE */
2096
2097/* BEGIN_CASE */
2098void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002099 data_t *key,
2100 data_t *input,
2101 int first_part_size,
2102 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002103{
2104 int key_slot = 1;
2105 psa_key_type_t key_type = key_type_arg;
2106 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002107 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002108 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002109 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002110 size_t output_buffer_size = 0;
2111 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002112 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002113 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002114 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002115
Gilles Peskine50e586b2018-06-08 14:28:46 +02002116 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002117 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002118 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002119 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2120 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2121 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002122
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002123 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2124 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002125
2126 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2127
Moran Pekered346952018-07-05 15:22:45 +03002128 psa_key_policy_init( &policy );
2129 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2130 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2131
Gilles Peskine50e586b2018-06-08 14:28:46 +02002132 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002133 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002134
Gilles Peskinefe119512018-07-08 21:39:34 +02002135 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2136 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002137
Gilles Peskinefe119512018-07-08 21:39:34 +02002138 TEST_ASSERT( psa_cipher_set_iv( &operation,
2139 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002140 output_buffer_size = (size_t) input->len +
2141 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002142 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002143
Gilles Peskine4abf7412018-06-18 16:35:34 +02002144 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002145 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002146 output, output_buffer_size,
2147 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002148 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002149 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002150 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002151 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002152 output, output_buffer_size,
2153 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002154 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002155 TEST_ASSERT( psa_cipher_finish( &operation,
2156 output + function_output_length,
2157 output_buffer_size,
2158 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002159 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002160 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2161
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002162 ASSERT_COMPARE( expected_output->x, expected_output->len,
2163 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002164
2165exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002166 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002167 psa_destroy_key( key_slot );
2168 mbedtls_psa_crypto_free( );
2169}
2170/* END_CASE */
2171
2172/* BEGIN_CASE */
2173void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002174 data_t *key,
2175 data_t *input,
2176 int first_part_size,
2177 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002178{
2179 int key_slot = 1;
2180
2181 psa_key_type_t key_type = key_type_arg;
2182 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002183 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002184 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002185 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002186 size_t output_buffer_size = 0;
2187 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002188 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002189 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002190 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002191
Gilles Peskine50e586b2018-06-08 14:28:46 +02002192 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002193 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002194 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002195 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2196 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2197 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002198
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002199 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2200 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002201
2202 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2203
Moran Pekered346952018-07-05 15:22:45 +03002204 psa_key_policy_init( &policy );
2205 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2206 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2207
Gilles Peskine50e586b2018-06-08 14:28:46 +02002208 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002209 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002210
Gilles Peskinefe119512018-07-08 21:39:34 +02002211 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2212 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002213
Gilles Peskinefe119512018-07-08 21:39:34 +02002214 TEST_ASSERT( psa_cipher_set_iv( &operation,
2215 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002216
mohammad16033d91abe2018-07-03 13:15:54 +03002217 output_buffer_size = (size_t) input->len +
2218 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002219 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002220
Gilles Peskine4abf7412018-06-18 16:35:34 +02002221 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2222 TEST_ASSERT( psa_cipher_update( &operation,
2223 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002224 output, output_buffer_size,
2225 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002226 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002227 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002228 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002229 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002230 output, output_buffer_size,
2231 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002232 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002233 TEST_ASSERT( psa_cipher_finish( &operation,
2234 output + function_output_length,
2235 output_buffer_size,
2236 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002237 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002238 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2239
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002240 ASSERT_COMPARE( expected_output->x, expected_output->len,
2241 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002242
2243exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002244 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002245 psa_destroy_key( key_slot );
2246 mbedtls_psa_crypto_free( );
2247}
2248/* END_CASE */
2249
Gilles Peskine50e586b2018-06-08 14:28:46 +02002250/* BEGIN_CASE */
2251void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002252 data_t *key,
2253 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002254 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002255{
2256 int key_slot = 1;
2257 psa_status_t status;
2258 psa_key_type_t key_type = key_type_arg;
2259 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002260 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002261 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002262 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002263 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002264 size_t output_buffer_size = 0;
2265 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002266 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002267 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002268 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002269
Gilles Peskine50e586b2018-06-08 14:28:46 +02002270 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002271 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002272 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002273 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2274 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2275 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002276
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002277 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2278 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002279
2280 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2281
Moran Pekered346952018-07-05 15:22:45 +03002282 psa_key_policy_init( &policy );
2283 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2284 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2285
Gilles Peskine50e586b2018-06-08 14:28:46 +02002286 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002287 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002288
Gilles Peskinefe119512018-07-08 21:39:34 +02002289 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2290 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002291
Gilles Peskinefe119512018-07-08 21:39:34 +02002292 TEST_ASSERT( psa_cipher_set_iv( &operation,
2293 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002294
mohammad16033d91abe2018-07-03 13:15:54 +03002295 output_buffer_size = (size_t) input->len +
2296 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002297 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002298
Gilles Peskine4abf7412018-06-18 16:35:34 +02002299 TEST_ASSERT( psa_cipher_update( &operation,
2300 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002301 output, output_buffer_size,
2302 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002303 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002304 status = psa_cipher_finish( &operation,
2305 output + function_output_length,
2306 output_buffer_size,
2307 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002308 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002309 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002310
2311 if( expected_status == PSA_SUCCESS )
2312 {
2313 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002314 ASSERT_COMPARE( expected_output->x, expected_output->len,
2315 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002316 }
2317
Gilles Peskine50e586b2018-06-08 14:28:46 +02002318exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002319 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002320 psa_destroy_key( key_slot );
2321 mbedtls_psa_crypto_free( );
2322}
2323/* END_CASE */
2324
Gilles Peskine50e586b2018-06-08 14:28:46 +02002325/* BEGIN_CASE */
2326void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002327 data_t *key,
2328 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002329{
2330 int key_slot = 1;
2331 psa_key_type_t key_type = key_type_arg;
2332 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002333 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002334 size_t iv_size = 16;
2335 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002336 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002337 size_t output1_size = 0;
2338 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002339 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002340 size_t output2_size = 0;
2341 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002342 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002343 psa_cipher_operation_t operation1;
2344 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002345 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002346
mohammad1603d7d7ba52018-03-12 18:51:53 +02002347 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002348 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002349 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2350 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002351
mohammad1603d7d7ba52018-03-12 18:51:53 +02002352 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2353
Moran Pekered346952018-07-05 15:22:45 +03002354 psa_key_policy_init( &policy );
2355 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2356 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2357
mohammad1603d7d7ba52018-03-12 18:51:53 +02002358 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002359 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002360
Gilles Peskinefe119512018-07-08 21:39:34 +02002361 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2362 key_slot, alg ) == PSA_SUCCESS );
2363 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2364 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002365
Gilles Peskinefe119512018-07-08 21:39:34 +02002366 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2367 iv, iv_size,
2368 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002369 output1_size = (size_t) input->len +
2370 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002371 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002372
Gilles Peskine4abf7412018-06-18 16:35:34 +02002373 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002374 output1, output1_size,
2375 &output1_length ) == PSA_SUCCESS );
2376 TEST_ASSERT( psa_cipher_finish( &operation1,
2377 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002378 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002379
Gilles Peskine048b7f02018-06-08 14:20:49 +02002380 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002381
2382 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2383
2384 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002385 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002386
Gilles Peskinefe119512018-07-08 21:39:34 +02002387 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2388 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002389 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2390 output2, output2_size,
2391 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002392 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002393 TEST_ASSERT( psa_cipher_finish( &operation2,
2394 output2 + output2_length,
2395 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002396 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002397
Gilles Peskine048b7f02018-06-08 14:20:49 +02002398 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002399
Janos Follath25c4fa82018-07-06 16:23:25 +01002400 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002401
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002402 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002403
2404exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002405 mbedtls_free( output1 );
2406 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002407 psa_destroy_key( key_slot );
2408 mbedtls_psa_crypto_free( );
2409}
2410/* END_CASE */
2411
2412/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002413void cipher_verify_output_multipart( int alg_arg,
2414 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002415 data_t *key,
2416 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002417 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002418{
2419 int key_slot = 1;
2420 psa_key_type_t key_type = key_type_arg;
2421 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002422 unsigned char iv[16] = {0};
2423 size_t iv_size = 16;
2424 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002425 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002426 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002427 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002428 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002429 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002430 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002431 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002432 psa_cipher_operation_t operation1;
2433 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002434 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002435
Moran Pekerded84402018-06-06 16:36:50 +03002436 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002437 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002438 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2439 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002440
Moran Pekerded84402018-06-06 16:36:50 +03002441 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2442
Moran Pekered346952018-07-05 15:22:45 +03002443 psa_key_policy_init( &policy );
2444 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2445 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2446
Moran Pekerded84402018-06-06 16:36:50 +03002447 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002448 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002449
Gilles Peskinefe119512018-07-08 21:39:34 +02002450 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2451 key_slot, alg ) == PSA_SUCCESS );
2452 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2453 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002454
Gilles Peskinefe119512018-07-08 21:39:34 +02002455 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2456 iv, iv_size,
2457 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002458 output1_buffer_size = (size_t) input->len +
2459 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002460 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002461
Gilles Peskine4abf7412018-06-18 16:35:34 +02002462 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002463
itayzafrir3e02b3b2018-06-12 17:06:52 +03002464 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002465 output1, output1_buffer_size,
2466 &function_output_length ) == PSA_SUCCESS );
2467 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002468
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002469 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002470 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002471 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002472 output1, output1_buffer_size,
2473 &function_output_length ) == PSA_SUCCESS );
2474 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002475
Gilles Peskine048b7f02018-06-08 14:20:49 +02002476 TEST_ASSERT( psa_cipher_finish( &operation1,
2477 output1 + output1_length,
2478 output1_buffer_size - output1_length,
2479 &function_output_length ) == PSA_SUCCESS );
2480 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002481
2482 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2483
Gilles Peskine048b7f02018-06-08 14:20:49 +02002484 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002485 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002486
Gilles Peskinefe119512018-07-08 21:39:34 +02002487 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2488 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002489
2490 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002491 output2, output2_buffer_size,
2492 &function_output_length ) == PSA_SUCCESS );
2493 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002494
Gilles Peskine048b7f02018-06-08 14:20:49 +02002495 TEST_ASSERT( psa_cipher_update( &operation2,
2496 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002497 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002498 output2, output2_buffer_size,
2499 &function_output_length ) == PSA_SUCCESS );
2500 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002501
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002502 TEST_ASSERT( psa_cipher_finish( &operation2,
2503 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002504 output2_buffer_size - output2_length,
2505 &function_output_length ) == PSA_SUCCESS );
2506 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002507
Janos Follath25c4fa82018-07-06 16:23:25 +01002508 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002509
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002510 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002511
2512exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002513 mbedtls_free( output1 );
2514 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002515 psa_destroy_key( key_slot );
2516 mbedtls_psa_crypto_free( );
2517}
2518/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002519
Gilles Peskine20035e32018-02-03 22:44:14 +01002520/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002521void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002522 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002523 data_t *nonce,
2524 data_t *additional_data,
2525 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002526 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002527{
2528 int slot = 1;
2529 psa_key_type_t key_type = key_type_arg;
2530 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002531 unsigned char *output_data = NULL;
2532 size_t output_size = 0;
2533 size_t output_length = 0;
2534 unsigned char *output_data2 = NULL;
2535 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002536 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002537 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002538 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002539
Gilles Peskinea1cac842018-06-11 19:33:02 +02002540 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002541 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002542 TEST_ASSERT( nonce != NULL );
2543 TEST_ASSERT( additional_data != NULL );
2544 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2545 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2546 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2547 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2548
Gilles Peskine4abf7412018-06-18 16:35:34 +02002549 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002550 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002551
2552 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2553
2554 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002555 psa_key_policy_set_usage( &policy,
2556 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2557 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002558 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2559
2560 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002561 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002562
2563 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002564 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002565 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002566 additional_data->len,
2567 input_data->x, input_data->len,
2568 output_data, output_size,
2569 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002570
2571 if( PSA_SUCCESS == expected_result )
2572 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002573 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002574
2575 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002576 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002577 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002578 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002579 output_data, output_length,
2580 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002581 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002582
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002583 ASSERT_COMPARE( input_data->x, input_data->len,
2584 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002585 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002586
Gilles Peskinea1cac842018-06-11 19:33:02 +02002587exit:
2588 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002589 mbedtls_free( output_data );
2590 mbedtls_free( output_data2 );
2591 mbedtls_psa_crypto_free( );
2592}
2593/* END_CASE */
2594
2595/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002596void aead_encrypt( int key_type_arg, data_t *key_data,
2597 int alg_arg,
2598 data_t *nonce,
2599 data_t *additional_data,
2600 data_t *input_data,
2601 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002602{
2603 int slot = 1;
2604 psa_key_type_t key_type = key_type_arg;
2605 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002606 unsigned char *output_data = NULL;
2607 size_t output_size = 0;
2608 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002609 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002610 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002611
Gilles Peskinea1cac842018-06-11 19:33:02 +02002612 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002613 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002614 TEST_ASSERT( additional_data != NULL );
2615 TEST_ASSERT( nonce != NULL );
2616 TEST_ASSERT( expected_result != NULL );
2617 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2618 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2619 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2620 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2621 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2622
Gilles Peskine4abf7412018-06-18 16:35:34 +02002623 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002624 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002625
Gilles Peskinea1cac842018-06-11 19:33:02 +02002626 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2627
2628 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002629 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002630 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2631
2632 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002633 key_data->x,
2634 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002635
2636 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002637 nonce->x, nonce->len,
2638 additional_data->x, additional_data->len,
2639 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002640 output_data, output_size,
2641 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002642
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002643 ASSERT_COMPARE( expected_result->x, expected_result->len,
2644 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002645
Gilles Peskinea1cac842018-06-11 19:33:02 +02002646exit:
2647 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002648 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002649 mbedtls_psa_crypto_free( );
2650}
2651/* END_CASE */
2652
2653/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002654void aead_decrypt( int key_type_arg, data_t *key_data,
2655 int alg_arg,
2656 data_t *nonce,
2657 data_t *additional_data,
2658 data_t *input_data,
2659 data_t *expected_data,
2660 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002661{
2662 int slot = 1;
2663 psa_key_type_t key_type = key_type_arg;
2664 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002665 unsigned char *output_data = NULL;
2666 size_t output_size = 0;
2667 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002668 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002669 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002670 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002671
Gilles Peskinea1cac842018-06-11 19:33:02 +02002672 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002673 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002674 TEST_ASSERT( additional_data != NULL );
2675 TEST_ASSERT( nonce != NULL );
2676 TEST_ASSERT( expected_data != NULL );
2677 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2678 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2679 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2680 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2681 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2682
Gilles Peskine4abf7412018-06-18 16:35:34 +02002683 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002684 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002685
Gilles Peskinea1cac842018-06-11 19:33:02 +02002686 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2687
2688 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002689 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002690 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2691
2692 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002693 key_data->x,
2694 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002695
2696 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002697 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002698 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002699 additional_data->len,
2700 input_data->x, input_data->len,
2701 output_data, output_size,
2702 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002703
Gilles Peskine2d277862018-06-18 15:41:12 +02002704 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002705 ASSERT_COMPARE( expected_data->x, expected_data->len,
2706 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002707
Gilles Peskinea1cac842018-06-11 19:33:02 +02002708exit:
2709 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002710 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002711 mbedtls_psa_crypto_free( );
2712}
2713/* END_CASE */
2714
2715/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002716void signature_size( int type_arg,
2717 int bits,
2718 int alg_arg,
2719 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002720{
2721 psa_key_type_t type = type_arg;
2722 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002723 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002724 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2725exit:
2726 ;
2727}
2728/* END_CASE */
2729
2730/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002731void sign_deterministic( int key_type_arg, data_t *key_data,
2732 int alg_arg, data_t *input_data,
2733 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002734{
2735 int slot = 1;
2736 psa_key_type_t key_type = key_type_arg;
2737 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002738 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002739 unsigned char *signature = NULL;
2740 size_t signature_size;
2741 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002742 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002743
Gilles Peskine20035e32018-02-03 22:44:14 +01002744 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002745 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002746 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002747 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2748 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2749 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002750
2751 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2752
mohammad1603a97cb8c2018-03-28 03:46:26 -07002753 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002754 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002755 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2756
Gilles Peskine20035e32018-02-03 22:44:14 +01002757 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002758 key_data->x,
2759 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002760 TEST_ASSERT( psa_get_key_information( slot,
2761 NULL,
2762 &key_bits ) == PSA_SUCCESS );
2763
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002764 /* Allocate a buffer which has the size advertized by the
2765 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002766 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2767 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002768 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002769 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002770 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002771
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002772 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002773 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002774 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002775 signature, signature_size,
2776 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002777 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002778 ASSERT_COMPARE( output_data->x, output_data->len,
2779 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002780
2781exit:
2782 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002783 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002784 mbedtls_psa_crypto_free( );
2785}
2786/* END_CASE */
2787
2788/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002789void sign_fail( int key_type_arg, data_t *key_data,
2790 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002791 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002792{
2793 int slot = 1;
2794 psa_key_type_t key_type = key_type_arg;
2795 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002796 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002797 psa_status_t actual_status;
2798 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002799 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002800 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002801 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002802
Gilles Peskine20035e32018-02-03 22:44:14 +01002803 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002804 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002805 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2806 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2807
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002808 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002809
2810 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2811
mohammad1603a97cb8c2018-03-28 03:46:26 -07002812 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002813 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002814 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2815
Gilles Peskine20035e32018-02-03 22:44:14 +01002816 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002817 key_data->x,
2818 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002819
2820 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002821 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002822 signature, signature_size,
2823 &signature_length );
2824 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002825 /* The value of *signature_length is unspecified on error, but
2826 * whatever it is, it should be less than signature_size, so that
2827 * if the caller tries to read *signature_length bytes without
2828 * checking the error code then they don't overflow a buffer. */
2829 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002830
2831exit:
2832 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002833 mbedtls_free( signature );
2834 mbedtls_psa_crypto_free( );
2835}
2836/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002837
2838/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002839void sign_verify( int key_type_arg, data_t *key_data,
2840 int alg_arg, data_t *input_data )
2841{
2842 int slot = 1;
2843 psa_key_type_t key_type = key_type_arg;
2844 psa_algorithm_t alg = alg_arg;
2845 size_t key_bits;
2846 unsigned char *signature = NULL;
2847 size_t signature_size;
2848 size_t signature_length = 0xdeadbeef;
2849 psa_key_policy_t policy;
2850
2851 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2852
2853 psa_key_policy_init( &policy );
2854 psa_key_policy_set_usage( &policy,
2855 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2856 alg );
2857 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2858
2859 TEST_ASSERT( psa_import_key( slot, key_type,
2860 key_data->x,
2861 key_data->len ) == PSA_SUCCESS );
2862 TEST_ASSERT( psa_get_key_information( slot,
2863 NULL,
2864 &key_bits ) == PSA_SUCCESS );
2865
2866 /* Allocate a buffer which has the size advertized by the
2867 * library. */
2868 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2869 key_bits, alg );
2870 TEST_ASSERT( signature_size != 0 );
2871 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002872 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002873
2874 /* Perform the signature. */
2875 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2876 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002877 signature, signature_size,
2878 &signature_length ) == PSA_SUCCESS );
2879 /* Check that the signature length looks sensible. */
2880 TEST_ASSERT( signature_length <= signature_size );
2881 TEST_ASSERT( signature_length > 0 );
2882
2883 /* Use the library to verify that the signature is correct. */
2884 TEST_ASSERT( psa_asymmetric_verify(
2885 slot, alg,
2886 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002887 signature, signature_length ) == PSA_SUCCESS );
2888
2889 if( input_data->len != 0 )
2890 {
2891 /* Flip a bit in the input and verify that the signature is now
2892 * detected as invalid. Flip a bit at the beginning, not at the end,
2893 * because ECDSA may ignore the last few bits of the input. */
2894 input_data->x[0] ^= 1;
2895 TEST_ASSERT( psa_asymmetric_verify(
2896 slot, alg,
2897 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002898 signature,
2899 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2900 }
2901
2902exit:
2903 psa_destroy_key( slot );
2904 mbedtls_free( signature );
2905 mbedtls_psa_crypto_free( );
2906}
2907/* END_CASE */
2908
2909/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002910void asymmetric_verify( int key_type_arg, data_t *key_data,
2911 int alg_arg, data_t *hash_data,
2912 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002913{
2914 int slot = 1;
2915 psa_key_type_t key_type = key_type_arg;
2916 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002917 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002918
Gilles Peskine69c12672018-06-28 00:07:19 +02002919 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2920
itayzafrir5c753392018-05-08 11:18:38 +03002921 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002922 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002923 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002924 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2925 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2926 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002927
2928 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2929
2930 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002931 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002932 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2933
2934 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002935 key_data->x,
2936 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002937
2938 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002939 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002940 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002941 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002942exit:
2943 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002944 mbedtls_psa_crypto_free( );
2945}
2946/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002947
2948/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002949void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2950 int alg_arg, data_t *hash_data,
2951 data_t *signature_data,
2952 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002953{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002954 int slot = 1;
2955 psa_key_type_t key_type = key_type_arg;
2956 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002957 psa_status_t actual_status;
2958 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002959 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002960
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002961 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002962 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002963 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002964 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2965 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2966 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002967
2968 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2969
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002970 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002971 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002972 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2973
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002974 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002975 key_data->x,
2976 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002977
2978 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002979 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002980 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002981 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002982
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002983 TEST_ASSERT( actual_status == expected_status );
2984
2985exit:
2986 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002987 mbedtls_psa_crypto_free( );
2988}
2989/* END_CASE */
2990
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002991/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002992void asymmetric_encrypt( int key_type_arg,
2993 data_t *key_data,
2994 int alg_arg,
2995 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002996 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002997 int expected_output_length_arg,
2998 int expected_status_arg )
2999{
3000 int slot = 1;
3001 psa_key_type_t key_type = key_type_arg;
3002 psa_algorithm_t alg = alg_arg;
3003 size_t expected_output_length = expected_output_length_arg;
3004 size_t key_bits;
3005 unsigned char *output = NULL;
3006 size_t output_size;
3007 size_t output_length = ~0;
3008 psa_status_t actual_status;
3009 psa_status_t expected_status = expected_status_arg;
3010 psa_key_policy_t policy;
3011
3012 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3013
3014 /* Import the key */
3015 psa_key_policy_init( &policy );
3016 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
3017 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3018 TEST_ASSERT( psa_import_key( slot, key_type,
3019 key_data->x,
3020 key_data->len ) == PSA_SUCCESS );
3021
3022 /* Determine the maximum output length */
3023 TEST_ASSERT( psa_get_key_information( slot,
3024 NULL,
3025 &key_bits ) == PSA_SUCCESS );
3026 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003027 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003028
3029 /* Encrypt the input */
3030 actual_status = psa_asymmetric_encrypt( slot, alg,
3031 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003032 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003033 output, output_size,
3034 &output_length );
3035 TEST_ASSERT( actual_status == expected_status );
3036 TEST_ASSERT( output_length == expected_output_length );
3037
Gilles Peskine68428122018-06-30 18:42:41 +02003038 /* If the label is empty, the test framework puts a non-null pointer
3039 * in label->x. Test that a null pointer works as well. */
3040 if( label->len == 0 )
3041 {
3042 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003043 if( output_size != 0 )
3044 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003045 actual_status = psa_asymmetric_encrypt( slot, alg,
3046 input_data->x, input_data->len,
3047 NULL, label->len,
3048 output, output_size,
3049 &output_length );
3050 TEST_ASSERT( actual_status == expected_status );
3051 TEST_ASSERT( output_length == expected_output_length );
3052 }
3053
Gilles Peskine656896e2018-06-29 19:12:28 +02003054exit:
3055 psa_destroy_key( slot );
3056 mbedtls_free( output );
3057 mbedtls_psa_crypto_free( );
3058}
3059/* END_CASE */
3060
3061/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003062void asymmetric_encrypt_decrypt( int key_type_arg,
3063 data_t *key_data,
3064 int alg_arg,
3065 data_t *input_data,
3066 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003067{
3068 int slot = 1;
3069 psa_key_type_t key_type = key_type_arg;
3070 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003071 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003072 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003073 size_t output_size;
3074 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003075 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003076 size_t output2_size;
3077 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003078 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003079
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003080 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003081 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003082 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3083 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3084
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3086
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003087 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003088 psa_key_policy_set_usage( &policy,
3089 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003090 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003091 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3092
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003094 key_data->x,
3095 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003096
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003097
3098 /* Determine the maximum ciphertext length */
3099 TEST_ASSERT( psa_get_key_information( slot,
3100 NULL,
3101 &key_bits ) == PSA_SUCCESS );
3102 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003103 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003104 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003105 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003106
Gilles Peskineeebd7382018-06-08 18:11:54 +02003107 /* We test encryption by checking that encrypt-then-decrypt gives back
3108 * the original plaintext because of the non-optional random
3109 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02003110 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003111 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003112 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003113 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003114 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003115 /* We don't know what ciphertext length to expect, but check that
3116 * it looks sensible. */
3117 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003118
Gilles Peskine2d277862018-06-18 15:41:12 +02003119 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003120 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02003121 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003122 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003123 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003124 ASSERT_COMPARE( input_data->x, input_data->len,
3125 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003126
3127exit:
3128 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003129 mbedtls_free( output );
3130 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003131 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003132}
3133/* END_CASE */
3134
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003135/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003136void asymmetric_decrypt( int key_type_arg,
3137 data_t *key_data,
3138 int alg_arg,
3139 data_t *input_data,
3140 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003141 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003142{
3143 int slot = 1;
3144 psa_key_type_t key_type = key_type_arg;
3145 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003146 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003147 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003148 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003149 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003150
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003153 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003154 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3155 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3156 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3157
Gilles Peskine4abf7412018-06-18 16:35:34 +02003158 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003159 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003160
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003161 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3162
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003163 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003164 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003165 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3166
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003167 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003168 key_data->x,
3169 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003170
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003171 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003172 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003173 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003174 output,
3175 output_size,
3176 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003177 ASSERT_COMPARE( expected_data->x, expected_data->len,
3178 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179
Gilles Peskine68428122018-06-30 18:42:41 +02003180 /* If the label is empty, the test framework puts a non-null pointer
3181 * in label->x. Test that a null pointer works as well. */
3182 if( label->len == 0 )
3183 {
3184 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003185 if( output_size != 0 )
3186 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003187 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
3188 input_data->x, input_data->len,
3189 NULL, label->len,
3190 output,
3191 output_size,
3192 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003193 ASSERT_COMPARE( expected_data->x, expected_data->len,
3194 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003195 }
3196
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197exit:
3198 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003199 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003200 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201}
3202/* END_CASE */
3203
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003204/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003205void asymmetric_decrypt_fail( int key_type_arg,
3206 data_t *key_data,
3207 int alg_arg,
3208 data_t *input_data,
3209 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003210 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212 int slot = 1;
3213 psa_key_type_t key_type = key_type_arg;
3214 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003215 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003216 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003217 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003218 psa_status_t actual_status;
3219 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003220 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003222 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003223 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003224 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3225 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3226
Gilles Peskine4abf7412018-06-18 16:35:34 +02003227 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003228 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003229
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003230 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3231
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003232 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003233 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003234 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3235
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003236 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003237 key_data->x,
3238 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003239
Gilles Peskine2d277862018-06-18 15:41:12 +02003240 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003241 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003242 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003243 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003244 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003245 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003246 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247
Gilles Peskine68428122018-06-30 18:42:41 +02003248 /* If the label is empty, the test framework puts a non-null pointer
3249 * in label->x. Test that a null pointer works as well. */
3250 if( label->len == 0 )
3251 {
3252 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003253 if( output_size != 0 )
3254 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003255 actual_status = psa_asymmetric_decrypt( slot, alg,
3256 input_data->x, input_data->len,
3257 NULL, label->len,
3258 output, output_size,
3259 &output_length );
3260 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003261 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003262 }
3263
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003264exit:
3265 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003266 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267 mbedtls_psa_crypto_free( );
3268}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003269/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003270
3271/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003272void derive_setup( int key_type_arg,
3273 data_t *key_data,
3274 int alg_arg,
3275 data_t *salt,
3276 data_t *label,
3277 int requested_capacity_arg,
3278 int expected_status_arg )
3279{
3280 psa_key_slot_t slot = 1;
3281 size_t key_type = key_type_arg;
3282 psa_algorithm_t alg = alg_arg;
3283 size_t requested_capacity = requested_capacity_arg;
3284 psa_status_t expected_status = expected_status_arg;
3285 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3286 psa_key_policy_t policy;
3287
3288 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3289
3290 psa_key_policy_init( &policy );
3291 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3292 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3293
3294 TEST_ASSERT( psa_import_key( slot, key_type,
3295 key_data->x,
3296 key_data->len ) == PSA_SUCCESS );
3297
3298 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3299 salt->x, salt->len,
3300 label->x, label->len,
3301 requested_capacity ) == expected_status );
3302
3303exit:
3304 psa_generator_abort( &generator );
3305 psa_destroy_key( slot );
3306 mbedtls_psa_crypto_free( );
3307}
3308/* END_CASE */
3309
3310/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003311void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003312{
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003313 psa_key_slot_t base_key = 1;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003314 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003315 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003316 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003317 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003318 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003319 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3320 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3321 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003322 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003323
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003324 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3325
3326 psa_key_policy_init( &policy );
3327 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3328 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3329
3330 TEST_ASSERT( psa_import_key( base_key, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003331 key_data,
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003332 sizeof( key_data ) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003333
3334 /* valid key derivation */
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003335 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003336 NULL, 0,
3337 NULL, 0,
3338 capacity ) == PSA_SUCCESS );
3339
3340 /* state of generator shouldn't allow additional generation */
3341 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3342 NULL, 0,
3343 NULL, 0,
3344 capacity ) == PSA_ERROR_BAD_STATE );
3345
3346 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3347 == PSA_SUCCESS );
3348
3349 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3350 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3351
3352
3353exit:
3354 psa_generator_abort( &generator );
3355 psa_destroy_key( base_key );
3356 mbedtls_psa_crypto_free( );
3357}
3358/* END_CASE */
3359
3360
3361/* BEGIN_CASE */
3362void test_derive_invalid_generator_tests( )
3363{
3364 uint8_t output_buffer[16];
3365 size_t buffer_size = 16;
3366 size_t capacity = 0;
3367 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3368
Nir Sonnenschein50789302018-10-31 12:16:38 +02003369 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003370 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003371
3372 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003373 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003374
Nir Sonnenschein50789302018-10-31 12:16:38 +02003375 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003376
Nir Sonnenschein50789302018-10-31 12:16:38 +02003377 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003378 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003379
Nir Sonnenschein50789302018-10-31 12:16:38 +02003380 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003381 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003382
3383exit:
3384 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003385}
3386/* END_CASE */
3387
3388/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003389void derive_output( int alg_arg,
3390 data_t *key_data,
3391 data_t *salt,
3392 data_t *label,
3393 int requested_capacity_arg,
3394 data_t *expected_output1,
3395 data_t *expected_output2 )
3396{
3397 psa_key_slot_t slot = 1;
3398 psa_algorithm_t alg = alg_arg;
3399 size_t requested_capacity = requested_capacity_arg;
3400 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3401 uint8_t *expected_outputs[2] =
3402 {expected_output1->x, expected_output2->x};
3403 size_t output_sizes[2] =
3404 {expected_output1->len, expected_output2->len};
3405 size_t output_buffer_size = 0;
3406 uint8_t *output_buffer = NULL;
3407 size_t expected_capacity;
3408 size_t current_capacity;
3409 psa_key_policy_t policy;
3410 psa_status_t status;
3411 unsigned i;
3412
3413 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3414 {
3415 if( output_sizes[i] > output_buffer_size )
3416 output_buffer_size = output_sizes[i];
3417 if( output_sizes[i] == 0 )
3418 expected_outputs[i] = NULL;
3419 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003420 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003421 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3422
3423 psa_key_policy_init( &policy );
3424 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3425 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3426
3427 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3428 key_data->x,
3429 key_data->len ) == PSA_SUCCESS );
3430
3431 /* Extraction phase. */
3432 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3433 salt->x, salt->len,
3434 label->x, label->len,
3435 requested_capacity ) == PSA_SUCCESS );
3436 TEST_ASSERT( psa_get_generator_capacity( &generator,
3437 &current_capacity ) ==
3438 PSA_SUCCESS );
3439 TEST_ASSERT( current_capacity == requested_capacity );
3440 expected_capacity = requested_capacity;
3441
3442 /* Expansion phase. */
3443 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3444 {
3445 /* Read some bytes. */
3446 status = psa_generator_read( &generator,
3447 output_buffer, output_sizes[i] );
3448 if( expected_capacity == 0 && output_sizes[i] == 0 )
3449 {
3450 /* Reading 0 bytes when 0 bytes are available can go either way. */
3451 TEST_ASSERT( status == PSA_SUCCESS ||
3452 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3453 continue;
3454 }
3455 else if( expected_capacity == 0 ||
3456 output_sizes[i] > expected_capacity )
3457 {
3458 /* Capacity exceeded. */
3459 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3460 expected_capacity = 0;
3461 continue;
3462 }
3463 /* Success. Check the read data. */
3464 TEST_ASSERT( status == PSA_SUCCESS );
3465 if( output_sizes[i] != 0 )
3466 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3467 output_sizes[i] ) == 0 );
3468 /* Check the generator status. */
3469 expected_capacity -= output_sizes[i];
3470 TEST_ASSERT( psa_get_generator_capacity( &generator,
3471 &current_capacity ) ==
3472 PSA_SUCCESS );
3473 TEST_ASSERT( expected_capacity == current_capacity );
3474 }
3475 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3476
3477exit:
3478 mbedtls_free( output_buffer );
3479 psa_generator_abort( &generator );
3480 psa_destroy_key( slot );
3481 mbedtls_psa_crypto_free( );
3482}
3483/* END_CASE */
3484
3485/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003486void derive_full( int alg_arg,
3487 data_t *key_data,
3488 data_t *salt,
3489 data_t *label,
3490 int requested_capacity_arg )
3491{
3492 psa_key_slot_t slot = 1;
3493 psa_algorithm_t alg = alg_arg;
3494 size_t requested_capacity = requested_capacity_arg;
3495 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3496 unsigned char output_buffer[16];
3497 size_t expected_capacity = requested_capacity;
3498 size_t current_capacity;
3499 psa_key_policy_t policy;
3500
3501 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3502
3503 psa_key_policy_init( &policy );
3504 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3505 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3506
3507 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3508 key_data->x,
3509 key_data->len ) == PSA_SUCCESS );
3510
3511 /* Extraction phase. */
3512 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3513 salt->x, salt->len,
3514 label->x, label->len,
3515 requested_capacity ) == PSA_SUCCESS );
3516 TEST_ASSERT( psa_get_generator_capacity( &generator,
3517 &current_capacity ) ==
3518 PSA_SUCCESS );
3519 TEST_ASSERT( current_capacity == expected_capacity );
3520
3521 /* Expansion phase. */
3522 while( current_capacity > 0 )
3523 {
3524 size_t read_size = sizeof( output_buffer );
3525 if( read_size > current_capacity )
3526 read_size = current_capacity;
3527 TEST_ASSERT( psa_generator_read( &generator,
3528 output_buffer,
3529 read_size ) == PSA_SUCCESS );
3530 expected_capacity -= read_size;
3531 TEST_ASSERT( psa_get_generator_capacity( &generator,
3532 &current_capacity ) ==
3533 PSA_SUCCESS );
3534 TEST_ASSERT( current_capacity == expected_capacity );
3535 }
3536
3537 /* Check that the generator refuses to go over capacity. */
3538 TEST_ASSERT( psa_generator_read( &generator,
3539 output_buffer,
3540 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3541
3542 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3543
3544exit:
3545 psa_generator_abort( &generator );
3546 psa_destroy_key( slot );
3547 mbedtls_psa_crypto_free( );
3548}
3549/* END_CASE */
3550
3551/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003552void derive_key_exercise( int alg_arg,
3553 data_t *key_data,
3554 data_t *salt,
3555 data_t *label,
3556 int derived_type_arg,
3557 int derived_bits_arg,
3558 int derived_usage_arg,
3559 int derived_alg_arg )
3560{
3561 psa_key_slot_t base_key = 1;
3562 psa_key_slot_t derived_key = 2;
3563 psa_algorithm_t alg = alg_arg;
3564 psa_key_type_t derived_type = derived_type_arg;
3565 size_t derived_bits = derived_bits_arg;
3566 psa_key_usage_t derived_usage = derived_usage_arg;
3567 psa_algorithm_t derived_alg = derived_alg_arg;
3568 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3569 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3570 psa_key_policy_t policy;
3571 psa_key_type_t got_type;
3572 size_t got_bits;
3573
3574 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3575
3576 psa_key_policy_init( &policy );
3577 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3578 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3579 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3580 key_data->x,
3581 key_data->len ) == PSA_SUCCESS );
3582
3583 /* Derive a key. */
3584 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3585 salt->x, salt->len,
3586 label->x, label->len,
3587 capacity ) == PSA_SUCCESS );
3588 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3589 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3590 TEST_ASSERT( psa_generator_import_key( derived_key,
3591 derived_type,
3592 derived_bits,
3593 &generator ) == PSA_SUCCESS );
3594
3595 /* Test the key information */
3596 TEST_ASSERT( psa_get_key_information( derived_key,
3597 &got_type,
3598 &got_bits ) == PSA_SUCCESS );
3599 TEST_ASSERT( got_type == derived_type );
3600 TEST_ASSERT( got_bits == derived_bits );
3601
3602 /* Exercise the derived key. */
3603 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3604 goto exit;
3605
3606exit:
3607 psa_generator_abort( &generator );
3608 psa_destroy_key( base_key );
3609 psa_destroy_key( derived_key );
3610 mbedtls_psa_crypto_free( );
3611}
3612/* END_CASE */
3613
3614/* BEGIN_CASE */
3615void derive_key_export( int alg_arg,
3616 data_t *key_data,
3617 data_t *salt,
3618 data_t *label,
3619 int bytes1_arg,
3620 int bytes2_arg )
3621{
3622 psa_key_slot_t base_key = 1;
3623 psa_key_slot_t derived_key = 2;
3624 psa_algorithm_t alg = alg_arg;
3625 size_t bytes1 = bytes1_arg;
3626 size_t bytes2 = bytes2_arg;
3627 size_t capacity = bytes1 + bytes2;
3628 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003629 uint8_t *output_buffer = NULL;
3630 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003631 psa_key_policy_t policy;
3632 size_t length;
3633
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003634 ASSERT_ALLOC( output_buffer, capacity );
3635 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003636 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3637
3638 psa_key_policy_init( &policy );
3639 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3640 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3641 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3642 key_data->x,
3643 key_data->len ) == PSA_SUCCESS );
3644
3645 /* Derive some material and output it. */
3646 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3647 salt->x, salt->len,
3648 label->x, label->len,
3649 capacity ) == PSA_SUCCESS );
3650 TEST_ASSERT( psa_generator_read( &generator,
3651 output_buffer,
3652 capacity ) == PSA_SUCCESS );
3653 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3654
3655 /* Derive the same output again, but this time store it in key objects. */
3656 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3657 salt->x, salt->len,
3658 label->x, label->len,
3659 capacity ) == PSA_SUCCESS );
3660 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3661 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3662 TEST_ASSERT( psa_generator_import_key( derived_key,
3663 PSA_KEY_TYPE_RAW_DATA,
3664 PSA_BYTES_TO_BITS( bytes1 ),
3665 &generator ) == PSA_SUCCESS );
3666 TEST_ASSERT( psa_export_key( derived_key,
3667 export_buffer, bytes1,
3668 &length ) == PSA_SUCCESS );
3669 TEST_ASSERT( length == bytes1 );
3670 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3671 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3672 TEST_ASSERT( psa_generator_import_key( derived_key,
3673 PSA_KEY_TYPE_RAW_DATA,
3674 PSA_BYTES_TO_BITS( bytes2 ),
3675 &generator ) == PSA_SUCCESS );
3676 TEST_ASSERT( psa_export_key( derived_key,
3677 export_buffer + bytes1, bytes2,
3678 &length ) == PSA_SUCCESS );
3679 TEST_ASSERT( length == bytes2 );
3680
3681 /* Compare the outputs from the two runs. */
3682 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3683
3684exit:
3685 mbedtls_free( output_buffer );
3686 mbedtls_free( export_buffer );
3687 psa_generator_abort( &generator );
3688 psa_destroy_key( base_key );
3689 psa_destroy_key( derived_key );
3690 mbedtls_psa_crypto_free( );
3691}
3692/* END_CASE */
3693
3694/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003695void key_agreement_setup( int alg_arg,
3696 int our_key_type_arg, data_t *our_key_data,
3697 data_t *peer_key_data,
3698 int expected_status_arg )
3699{
3700 psa_key_slot_t our_key = 1;
3701 psa_algorithm_t alg = alg_arg;
3702 psa_key_type_t our_key_type = our_key_type_arg;
3703 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3704 psa_key_policy_t policy;
3705
3706 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3707
3708 psa_key_policy_init( &policy );
3709 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3710 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3711 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3712 our_key_data->x,
3713 our_key_data->len ) == PSA_SUCCESS );
3714
3715 TEST_ASSERT( psa_key_agreement( &generator,
3716 our_key,
3717 peer_key_data->x, peer_key_data->len,
3718 alg ) == expected_status_arg );
3719
3720exit:
3721 psa_generator_abort( &generator );
3722 psa_destroy_key( our_key );
3723 mbedtls_psa_crypto_free( );
3724}
3725/* END_CASE */
3726
3727/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003728void key_agreement_capacity( int alg_arg,
3729 int our_key_type_arg, data_t *our_key_data,
3730 data_t *peer_key_data,
3731 int expected_capacity_arg )
3732{
3733 psa_key_slot_t our_key = 1;
3734 psa_algorithm_t alg = alg_arg;
3735 psa_key_type_t our_key_type = our_key_type_arg;
3736 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3737 psa_key_policy_t policy;
3738 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003739 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003740
3741 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3742
3743 psa_key_policy_init( &policy );
3744 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3745 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3746 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3747 our_key_data->x,
3748 our_key_data->len ) == PSA_SUCCESS );
3749
3750 TEST_ASSERT( psa_key_agreement( &generator,
3751 our_key,
3752 peer_key_data->x, peer_key_data->len,
3753 alg ) == PSA_SUCCESS );
3754
Gilles Peskinebf491972018-10-25 22:36:12 +02003755 /* Test the advertized capacity. */
Gilles Peskine59685592018-09-18 12:11:34 +02003756 TEST_ASSERT( psa_get_generator_capacity(
3757 &generator, &actual_capacity ) == PSA_SUCCESS );
3758 TEST_ASSERT( actual_capacity == (size_t) expected_capacity_arg );
3759
Gilles Peskinebf491972018-10-25 22:36:12 +02003760 /* Test the actual capacity by reading the output. */
3761 while( actual_capacity > sizeof( output ) )
3762 {
3763 TEST_ASSERT( psa_generator_read( &generator,
3764 output, sizeof( output ) ) ==
3765 PSA_SUCCESS );
3766 actual_capacity -= sizeof( output );
3767 }
3768 TEST_ASSERT( psa_generator_read( &generator,
3769 output, actual_capacity ) ==
3770 PSA_SUCCESS );
3771 TEST_ASSERT( psa_generator_read( &generator, output, 1 ) ==
3772 PSA_ERROR_INSUFFICIENT_CAPACITY );
3773
Gilles Peskine59685592018-09-18 12:11:34 +02003774exit:
3775 psa_generator_abort( &generator );
3776 psa_destroy_key( our_key );
3777 mbedtls_psa_crypto_free( );
3778}
3779/* END_CASE */
3780
3781/* BEGIN_CASE */
3782void key_agreement_output( int alg_arg,
3783 int our_key_type_arg, data_t *our_key_data,
3784 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003785 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003786{
3787 psa_key_slot_t our_key = 1;
3788 psa_algorithm_t alg = alg_arg;
3789 psa_key_type_t our_key_type = our_key_type_arg;
3790 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3791 psa_key_policy_t policy;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003792 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003793
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003794 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3795 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003796
3797 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3798
3799 psa_key_policy_init( &policy );
3800 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3801 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3802 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3803 our_key_data->x,
3804 our_key_data->len ) == PSA_SUCCESS );
3805
3806 TEST_ASSERT( psa_key_agreement( &generator,
3807 our_key,
3808 peer_key_data->x, peer_key_data->len,
3809 alg ) == PSA_SUCCESS );
3810
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003811 TEST_ASSERT(
3812 psa_generator_read( &generator,
3813 actual_output,
3814 expected_output1->len ) == PSA_SUCCESS );
3815 TEST_ASSERT( memcmp( actual_output, expected_output1->x,
3816 expected_output1->len ) == 0 );
3817 if( expected_output2->len != 0 )
3818 {
3819 TEST_ASSERT(
3820 psa_generator_read( &generator,
3821 actual_output,
3822 expected_output2->len ) == PSA_SUCCESS );
3823 TEST_ASSERT( memcmp( actual_output, expected_output2->x,
3824 expected_output2->len ) == 0 );
3825 }
Gilles Peskine59685592018-09-18 12:11:34 +02003826
3827exit:
3828 psa_generator_abort( &generator );
3829 psa_destroy_key( our_key );
3830 mbedtls_psa_crypto_free( );
3831 mbedtls_free( actual_output );
3832}
3833/* END_CASE */
3834
3835/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003836void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003837{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003838 size_t bytes = bytes_arg;
3839 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003840 unsigned char *output = NULL;
3841 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003842 size_t i;
3843 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003844
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003845 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3846 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003847 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003848
3849 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3850
Gilles Peskinea50d7392018-06-21 10:22:13 +02003851 /* Run several times, to ensure that every output byte will be
3852 * nonzero at least once with overwhelming probability
3853 * (2^(-8*number_of_runs)). */
3854 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003855 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003856 if( bytes != 0 )
3857 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003858 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3859
3860 /* Check that no more than bytes have been overwritten */
3861 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3862
3863 for( i = 0; i < bytes; i++ )
3864 {
3865 if( output[i] != 0 )
3866 ++changed[i];
3867 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003868 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003869
3870 /* Check that every byte was changed to nonzero at least once. This
3871 * validates that psa_generate_random is overwriting every byte of
3872 * the output buffer. */
3873 for( i = 0; i < bytes; i++ )
3874 {
3875 TEST_ASSERT( changed[i] != 0 );
3876 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003877
3878exit:
3879 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003880 mbedtls_free( output );
3881 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003882}
3883/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003884
3885/* BEGIN_CASE */
3886void generate_key( int type_arg,
3887 int bits_arg,
3888 int usage_arg,
3889 int alg_arg,
3890 int expected_status_arg )
3891{
3892 int slot = 1;
3893 psa_key_type_t type = type_arg;
3894 psa_key_usage_t usage = usage_arg;
3895 size_t bits = bits_arg;
3896 psa_algorithm_t alg = alg_arg;
3897 psa_status_t expected_status = expected_status_arg;
3898 psa_key_type_t got_type;
3899 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003900 psa_status_t expected_info_status =
3901 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3902 psa_key_policy_t policy;
3903
3904 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3905
3906 psa_key_policy_init( &policy );
3907 psa_key_policy_set_usage( &policy, usage, alg );
3908 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3909
3910 /* Generate a key */
3911 TEST_ASSERT( psa_generate_key( slot, type, bits,
3912 NULL, 0 ) == expected_status );
3913
3914 /* Test the key information */
3915 TEST_ASSERT( psa_get_key_information( slot,
3916 &got_type,
3917 &got_bits ) == expected_info_status );
3918 if( expected_info_status != PSA_SUCCESS )
3919 goto exit;
3920 TEST_ASSERT( got_type == type );
3921 TEST_ASSERT( got_bits == bits );
3922
Gilles Peskine818ca122018-06-20 18:16:48 +02003923 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003924 if( ! exercise_key( slot, usage, alg ) )
3925 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003926
3927exit:
3928 psa_destroy_key( slot );
3929 mbedtls_psa_crypto_free( );
3930}
3931/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003932
Darryl Greend49a4992018-06-18 17:27:26 +01003933/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
3934void persistent_key_load_key_from_storage( data_t *data, int type_arg,
3935 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00003936 int alg_arg, int generation_method,
3937 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01003938{
3939 psa_key_slot_t slot = 1;
Darryl Green0c6575a2018-11-07 16:05:30 +00003940 psa_key_slot_t base_key = 2;
Darryl Greend49a4992018-06-18 17:27:26 +01003941 psa_key_type_t type = (psa_key_type_t) type_arg;
3942 psa_key_type_t type_get;
3943 size_t bits_get;
3944 psa_key_policy_t policy_set;
3945 psa_key_policy_t policy_get;
3946 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
3947 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00003948 psa_key_policy_t base_policy_set;
3949 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
3950 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003951 unsigned char *first_export = NULL;
3952 unsigned char *second_export = NULL;
3953 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
3954 size_t first_exported_length;
3955 size_t second_exported_length;
3956
3957 ASSERT_ALLOC( first_export, export_size );
3958 ASSERT_ALLOC( second_export, export_size );
3959
3960 TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS );
3961
3962 TEST_ASSERT( psa_set_key_lifetime(
3963 slot, PSA_KEY_LIFETIME_PERSISTENT ) == PSA_SUCCESS );
3964
3965 psa_key_policy_init( &policy_set );
3966
3967 psa_key_policy_set_usage( &policy_set, policy_usage,
3968 policy_alg );
3969
3970 TEST_ASSERT( psa_set_key_policy( slot, &policy_set ) == PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00003971 switch( generation_method )
3972 {
3973 case IMPORT_KEY:
3974 /* Import the key */
3975 TEST_ASSERT( psa_import_key( slot, type,
3976 data->x, data->len ) == PSA_SUCCESS );
3977 break;
Darryl Greend49a4992018-06-18 17:27:26 +01003978
Darryl Green0c6575a2018-11-07 16:05:30 +00003979 case GENERATE_KEY:
3980 /* Generate a key */
3981 TEST_ASSERT( psa_generate_key( slot, type, bits,
3982 NULL, 0 ) == PSA_SUCCESS );
3983 break;
3984
3985 case DERIVE_KEY:
3986 /* Create base key */
3987 psa_key_policy_init( &base_policy_set );
3988
3989 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
3990 base_policy_alg );
3991 TEST_ASSERT( psa_set_key_policy(
3992 base_key, &base_policy_set ) == PSA_SUCCESS );
3993 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3994 data->x, data->len ) == PSA_SUCCESS );
3995 /* Derive a key. */
3996 TEST_ASSERT( psa_key_derivation( &generator, base_key,
3997 base_policy_alg,
3998 NULL, 0, NULL, 0,
3999 export_size ) == PSA_SUCCESS );
4000 TEST_ASSERT( psa_generator_import_key(
4001 slot, PSA_KEY_TYPE_RAW_DATA,
4002 bits, &generator ) == PSA_SUCCESS );
4003 break;
4004 }
Darryl Greend49a4992018-06-18 17:27:26 +01004005
4006 /* Export the key */
4007 TEST_ASSERT( psa_export_key( slot, first_export, export_size,
Darryl Green0c6575a2018-11-07 16:05:30 +00004008 &first_exported_length ) == export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004009
4010 /* Shutdown and restart */
4011 mbedtls_psa_crypto_free();
4012
4013 TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS );
4014
4015 /* Mark slot as persistent again */
4016 TEST_ASSERT( psa_set_key_lifetime(
4017 slot, PSA_KEY_LIFETIME_PERSISTENT ) == PSA_SUCCESS );
4018
4019 /* Check key slot still contains key data */
4020 TEST_ASSERT( psa_get_key_information(
4021 slot, &type_get, &bits_get ) == PSA_SUCCESS );
4022 TEST_ASSERT( type_get == type );
4023 TEST_ASSERT( bits_get == (size_t) bits );
4024
4025 TEST_ASSERT( psa_get_key_policy( slot, &policy_get ) == PSA_SUCCESS );
4026 TEST_ASSERT( psa_key_policy_get_usage(
4027 &policy_get ) == policy_usage );
4028 TEST_ASSERT( psa_key_policy_get_algorithm(
4029 &policy_get ) == policy_alg );
4030
4031 /* Export the key again */
4032 TEST_ASSERT( psa_export_key( slot, second_export, export_size,
Darryl Green0c6575a2018-11-07 16:05:30 +00004033 &second_exported_length ) == export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004034
Darryl Green0c6575a2018-11-07 16:05:30 +00004035 if( export_status == PSA_SUCCESS )
4036 {
4037 ASSERT_COMPARE( first_export, first_exported_length,
4038 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004039
Darryl Green0c6575a2018-11-07 16:05:30 +00004040 switch( generation_method )
4041 {
4042 case IMPORT_KEY:
4043 ASSERT_COMPARE( data->x, data->len,
4044 first_export, first_exported_length );
4045 break;
4046 default:
4047 break;
4048 }
4049 }
4050
4051 /* Do something with the key according to its type and permitted usage. */
4052 if( ! exercise_key( slot, policy_usage, policy_alg ) )
4053 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004054
4055exit:
4056 mbedtls_free( first_export );
4057 mbedtls_free( second_export );
4058 psa_destroy_key( slot );
4059 mbedtls_psa_crypto_free();
4060}
4061/* END_CASE */