blob: ee29ec951cdae79552b9ef0fa1088441707d7a21 [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 Peskine96ee5c72018-07-12 17:24:54 +020014#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
15
itayzafrir3e02b3b2018-06-12 17:06:52 +030016#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020017#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#else
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskined35a1cc2018-06-26 21:26:10 +020025/** Test if a buffer is all-bits zero.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020026 *
27 * \param buffer Pointer to the beginning of the buffer.
28 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020033static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
38 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskine818ca122018-06-20 18:16:48 +0200128static int exercise_mac_key( psa_key_slot_t key,
129 psa_key_usage_t usage,
130 psa_algorithm_t alg )
131{
132 psa_mac_operation_t operation;
133 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200134 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200135 size_t mac_length = sizeof( mac );
136
137 if( usage & PSA_KEY_USAGE_SIGN )
138 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200139 TEST_ASSERT( psa_mac_sign_setup( &operation,
140 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200141 TEST_ASSERT( psa_mac_update( &operation,
142 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200143 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200144 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200145 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200146 }
147
148 if( usage & PSA_KEY_USAGE_VERIFY )
149 {
150 psa_status_t verify_status =
151 ( usage & PSA_KEY_USAGE_SIGN ?
152 PSA_SUCCESS :
153 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200154 TEST_ASSERT( psa_mac_verify_setup( &operation,
155 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200156 TEST_ASSERT( psa_mac_update( &operation,
157 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200158 TEST_ASSERT( psa_mac_verify_finish( &operation,
159 mac,
160 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200161 }
162
163 return( 1 );
164
165exit:
166 psa_mac_abort( &operation );
167 return( 0 );
168}
169
170static int exercise_cipher_key( psa_key_slot_t key,
171 psa_key_usage_t usage,
172 psa_algorithm_t alg )
173{
174 psa_cipher_operation_t operation;
175 unsigned char iv[16] = {0};
176 size_t iv_length = sizeof( iv );
177 const unsigned char plaintext[16] = "Hello, world...";
178 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
179 size_t ciphertext_length = sizeof( ciphertext );
180 unsigned char decrypted[sizeof( ciphertext )];
181 size_t part_length;
182
183 if( usage & PSA_KEY_USAGE_ENCRYPT )
184 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200185 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
186 key, alg ) == PSA_SUCCESS );
187 TEST_ASSERT( psa_cipher_generate_iv( &operation,
188 iv, sizeof( iv ),
189 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200190 TEST_ASSERT( psa_cipher_update( &operation,
191 plaintext, sizeof( plaintext ),
192 ciphertext, sizeof( ciphertext ),
193 &ciphertext_length ) == PSA_SUCCESS );
194 TEST_ASSERT( psa_cipher_finish( &operation,
195 ciphertext + ciphertext_length,
196 sizeof( ciphertext ) - ciphertext_length,
197 &part_length ) == PSA_SUCCESS );
198 ciphertext_length += part_length;
199 }
200
201 if( usage & PSA_KEY_USAGE_DECRYPT )
202 {
203 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700204 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200205 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
206 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200207 size_t bits;
208 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
209 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
210 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200211 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
212 key, alg ) == PSA_SUCCESS );
213 TEST_ASSERT( psa_cipher_set_iv( &operation,
214 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200215 TEST_ASSERT( psa_cipher_update( &operation,
216 ciphertext, ciphertext_length,
217 decrypted, sizeof( decrypted ),
218 &part_length ) == PSA_SUCCESS );
219 status = psa_cipher_finish( &operation,
220 decrypted + part_length,
221 sizeof( decrypted ) - part_length,
222 &part_length );
223 /* For a stream cipher, all inputs are valid. For a block cipher,
224 * if the input is some aribtrary data rather than an actual
225 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700226 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700227 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200228 TEST_ASSERT( status == PSA_SUCCESS );
229 else
230 TEST_ASSERT( status == PSA_SUCCESS ||
231 status == PSA_ERROR_INVALID_PADDING );
232 }
233
234 return( 1 );
235
236exit:
237 psa_cipher_abort( &operation );
238 return( 0 );
239}
240
241static int exercise_aead_key( psa_key_slot_t key,
242 psa_key_usage_t usage,
243 psa_algorithm_t alg )
244{
245 unsigned char nonce[16] = {0};
246 size_t nonce_length = sizeof( nonce );
247 unsigned char plaintext[16] = "Hello, world...";
248 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
249 size_t ciphertext_length = sizeof( ciphertext );
250 size_t plaintext_length = sizeof( ciphertext );
251
252 if( usage & PSA_KEY_USAGE_ENCRYPT )
253 {
254 TEST_ASSERT( psa_aead_encrypt( key, alg,
255 nonce, nonce_length,
256 NULL, 0,
257 plaintext, sizeof( plaintext ),
258 ciphertext, sizeof( ciphertext ),
259 &ciphertext_length ) == PSA_SUCCESS );
260 }
261
262 if( usage & PSA_KEY_USAGE_DECRYPT )
263 {
264 psa_status_t verify_status =
265 ( usage & PSA_KEY_USAGE_ENCRYPT ?
266 PSA_SUCCESS :
267 PSA_ERROR_INVALID_SIGNATURE );
268 TEST_ASSERT( psa_aead_decrypt( key, alg,
269 nonce, nonce_length,
270 NULL, 0,
271 ciphertext, ciphertext_length,
272 plaintext, sizeof( plaintext ),
273 &plaintext_length ) == verify_status );
274 }
275
276 return( 1 );
277
278exit:
279 return( 0 );
280}
281
282static int exercise_signature_key( psa_key_slot_t key,
283 psa_key_usage_t usage,
284 psa_algorithm_t alg )
285{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200286 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
287 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200288 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200289 size_t signature_length = sizeof( signature );
290
291 if( usage & PSA_KEY_USAGE_SIGN )
292 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200293 /* Some algorithms require the payload to have the size of
294 * the hash encoded in the algorithm. Use this input size
295 * even for algorithms that allow other input sizes. */
296 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
297 if( hash_alg != 0 )
298 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200299 TEST_ASSERT( psa_asymmetric_sign( key, alg,
300 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200301 signature, sizeof( signature ),
302 &signature_length ) == PSA_SUCCESS );
303 }
304
305 if( usage & PSA_KEY_USAGE_VERIFY )
306 {
307 psa_status_t verify_status =
308 ( usage & PSA_KEY_USAGE_SIGN ?
309 PSA_SUCCESS :
310 PSA_ERROR_INVALID_SIGNATURE );
311 TEST_ASSERT( psa_asymmetric_verify( key, alg,
312 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200313 signature, signature_length ) ==
314 verify_status );
315 }
316
317 return( 1 );
318
319exit:
320 return( 0 );
321}
322
323static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
324 psa_key_usage_t usage,
325 psa_algorithm_t alg )
326{
327 unsigned char plaintext[256] = "Hello, world...";
328 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
329 size_t ciphertext_length = sizeof( ciphertext );
330 size_t plaintext_length = 16;
331
332 if( usage & PSA_KEY_USAGE_ENCRYPT )
333 {
334 TEST_ASSERT(
335 psa_asymmetric_encrypt( key, alg,
336 plaintext, plaintext_length,
337 NULL, 0,
338 ciphertext, sizeof( ciphertext ),
339 &ciphertext_length ) == PSA_SUCCESS );
340 }
341
342 if( usage & PSA_KEY_USAGE_DECRYPT )
343 {
344 psa_status_t status =
345 psa_asymmetric_decrypt( key, alg,
346 ciphertext, ciphertext_length,
347 NULL, 0,
348 plaintext, sizeof( plaintext ),
349 &plaintext_length );
350 TEST_ASSERT( status == PSA_SUCCESS ||
351 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
352 ( status == PSA_ERROR_INVALID_ARGUMENT ||
353 status == PSA_ERROR_INVALID_PADDING ) ) );
354 }
355
356 return( 1 );
357
358exit:
359 return( 0 );
360}
Gilles Peskine02b75072018-07-01 22:31:34 +0200361
Gilles Peskineea0fb492018-07-12 17:17:20 +0200362static int exercise_key_derivation_key( psa_key_slot_t key,
363 psa_key_usage_t usage,
364 psa_algorithm_t alg )
365{
366 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
367 unsigned char label[16] = "This is a label.";
368 size_t label_length = sizeof( label );
369 unsigned char seed[16] = "abcdefghijklmnop";
370 size_t seed_length = sizeof( seed );
371 unsigned char output[1];
372
373 if( usage & PSA_KEY_USAGE_DERIVE )
374 {
375 TEST_ASSERT( psa_key_derivation( &generator,
376 key, alg,
377 label, label_length,
378 seed, seed_length,
379 sizeof( output ) ) == PSA_SUCCESS );
380 TEST_ASSERT( psa_generator_read( &generator,
381 output,
382 sizeof( output ) ) == PSA_SUCCESS );
383 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
384 }
385
386 return( 1 );
387
388exit:
389 return( 0 );
390}
391
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200392typedef struct
Gilles Peskined14664a2018-08-10 19:07:32 +0200393{
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200394 unsigned char length;
395 unsigned char string[];
396} small_byte_string_t;
397#define DECLARE_SMALL_STRING_OF_LITERAL( name, literal ) \
398 static const small_byte_string_t name = \
399 { sizeof( literal ) - 1, literal }
400
401#if defined(MBEDTLS_RSA_C)
402DECLARE_SMALL_STRING_OF_LITERAL( key_type_oid_rsa,
403 MBEDTLS_OID_PKCS1_RSA );
404#endif
405#if defined(MBEDTLS_ECP_C)
406DECLARE_SMALL_STRING_OF_LITERAL( key_type_oid_ecc,
407 MBEDTLS_OID_EC_ALG_UNRESTRICTED );
408#endif
409
410static int is_oid_of_key_type( psa_key_type_t type,
411 const uint8_t *oid, size_t oid_length )
412{
413 const small_byte_string_t *expected_oid =
414#if defined(MBEDTLS_RSA_C)
415 PSA_KEY_TYPE_IS_RSA( type ) ? &key_type_oid_rsa :
416#endif /* MBEDTLS_RSA_C */
417#if defined(MBEDTLS_ECP_C)
418 PSA_KEY_TYPE_IS_ECC( type ) ? &key_type_oid_ecc :
419#endif /* MBEDTLS_ECP_C */
420 NULL;
421
422 if( expected_oid == NULL )
423 {
424 char message[40];
425 mbedtls_snprintf( message, sizeof( message ),
426 "OID not known for key type=0x%08lx",
427 (unsigned long) type );
428 test_fail( message, __LINE__, __FILE__ );
429 return( 0 );
430 }
431
432 TEST_ASSERT( oid_length == expected_oid->length );
433 TEST_ASSERT( memcmp( oid, expected_oid->string, oid_length ) == 0 );
434 return( 1 );
435
436exit:
437 return( 0 );
438}
439
440static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
441 size_t min_bits, size_t max_bits,
442 int must_be_odd )
443{
444 size_t len;
445 size_t actual_bits;
446 unsigned char msb;
447 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
448 MBEDTLS_ASN1_INTEGER ) == 0 );
449 /* Tolerate a slight departure from DER encoding:
450 * - 0 may be represented by an empty string or a 1-byte string.
451 * - The sign bit may be used as a value bit. */
452 if( ( len == 1 && ( *p )[0] == 0 ) ||
453 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
454 {
455 ++( *p );
456 --len;
457 }
458 if( min_bits == 0 && len == 0 )
459 return( 1 );
460 msb = ( *p )[0];
461 TEST_ASSERT( msb != 0 );
462 actual_bits = 8 * ( len - 1 );
463 while( msb != 0 )
464 {
465 msb >>= 1;
466 ++actual_bits;
467 }
468 TEST_ASSERT( actual_bits >= min_bits );
469 TEST_ASSERT( actual_bits <= max_bits );
470 if( must_be_odd )
471 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
472 *p += len;
473 return( 1 );
474exit:
475 return( 0 );
476}
477
478static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
479 size_t *len,
480 unsigned char n, unsigned char tag )
481{
482 int ret;
483 ret = mbedtls_asn1_get_tag( p, end, len,
484 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
485 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
486 if( ret != 0 )
487 return( ret );
488 end = *p + *len;
489 ret = mbedtls_asn1_get_tag( p, end, len, tag );
490 if( ret != 0 )
491 return( ret );
492 if( *p + *len != end )
493 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
494 return( 0 );
495}
496
497static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
498 uint8_t *exported, size_t exported_length )
499{
500 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200501 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200502 else
503 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200504
505#if defined(MBEDTLS_DES_C)
506 if( type == PSA_KEY_TYPE_DES )
507 {
508 /* Check the parity bits. */
509 unsigned i;
510 for( i = 0; i < bits / 8; i++ )
511 {
512 unsigned bit_count = 0;
513 unsigned m;
514 for( m = 1; m <= 0x100; m <<= 1 )
515 {
516 if( exported[i] & m )
517 ++bit_count;
518 }
519 TEST_ASSERT( bit_count % 2 != 0 );
520 }
521 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200522 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200523#endif
524
525#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
526 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
527 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200528 uint8_t *p = exported;
529 uint8_t *end = exported + exported_length;
530 size_t len;
531 /* RSAPrivateKey ::= SEQUENCE {
532 * version Version, -- 0
533 * modulus INTEGER, -- n
534 * publicExponent INTEGER, -- e
535 * privateExponent INTEGER, -- d
536 * prime1 INTEGER, -- p
537 * prime2 INTEGER, -- q
538 * exponent1 INTEGER, -- d mod (p-1)
539 * exponent2 INTEGER, -- d mod (q-1)
540 * coefficient INTEGER, -- (inverse of q) mod p
541 * }
542 */
543 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
544 MBEDTLS_ASN1_SEQUENCE |
545 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
546 TEST_ASSERT( p + len == end );
547 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
548 goto exit;
549 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
550 goto exit;
551 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
552 goto exit;
553 /* Require d to be at least half the size of n. */
554 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
555 goto exit;
556 /* Require p and q to be at most half the size of n, rounded up. */
557 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
558 goto exit;
559 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
560 goto exit;
561 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
562 goto exit;
563 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
564 goto exit;
565 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
566 goto exit;
567 TEST_ASSERT( p == end );
568 }
569 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200570#endif /* MBEDTLS_RSA_C */
571
572#if defined(MBEDTLS_ECP_C)
573 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
574 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200575 uint8_t *p = exported;
576 uint8_t *end = exported + exported_length;
577 size_t len;
578 int version;
579 /* ECPrivateKey ::= SEQUENCE {
580 * version INTEGER, -- must be 1
581 * privateKey OCTET STRING,
582 * -- `ceiling(log_{256}(n))`-byte string, big endian,
583 * -- where n is the order of the curve.
584 * parameters ECParameters {{ NamedCurve }}, -- mandatory
585 * publicKey BIT STRING -- mandatory
586 * }
587 */
588 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
589 MBEDTLS_ASN1_SEQUENCE |
590 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
591 TEST_ASSERT( p + len == end );
592 TEST_ASSERT( mbedtls_asn1_get_int( &p, end, &version ) == 0 );
593 TEST_ASSERT( version == 1 );
594 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
595 MBEDTLS_ASN1_OCTET_STRING ) == 0 );
596 /* Bug in Mbed TLS: the length of the octet string depends on the value */
597 // TEST_ASSERT( len == PSA_BITS_TO_BYTES( bits ) );
598 p += len;
599 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 0,
600 MBEDTLS_ASN1_OID ) == 0 );
601 p += len;
602 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 1,
603 MBEDTLS_ASN1_BIT_STRING ) == 0 );
604 TEST_ASSERT( p + len == end );
605 TEST_ASSERT( p[0] == 0 ); /* 0 unused bits in the bit string */
606 ++p;
607 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
608 TEST_ASSERT( p[0] == 4 );
609 }
610 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200611#endif /* MBEDTLS_ECP_C */
612
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200613 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
614 {
615 uint8_t *p = exported;
616 uint8_t *end = exported + exported_length;
617 size_t len;
618 mbedtls_asn1_buf alg;
619 mbedtls_asn1_buf params;
620 mbedtls_asn1_bitstring bitstring;
621 /* SubjectPublicKeyInfo ::= SEQUENCE {
622 * algorithm AlgorithmIdentifier,
623 * subjectPublicKey BIT STRING }
624 * AlgorithmIdentifier ::= SEQUENCE {
625 * algorithm OBJECT IDENTIFIER,
626 * parameters ANY DEFINED BY algorithm OPTIONAL }
627 */
628 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
629 MBEDTLS_ASN1_SEQUENCE |
630 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
631 TEST_ASSERT( p + len == end );
632 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
633 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
634 goto exit;
635 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
636 TEST_ASSERT( p == end );
637 p = bitstring.p;
638#if defined(MBEDTLS_RSA_C)
639 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
640 {
641 /* RSAPublicKey ::= SEQUENCE {
642 * modulus INTEGER, -- n
643 * publicExponent INTEGER } -- e
644 */
645 TEST_ASSERT( bitstring.unused_bits == 0 );
646 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
647 MBEDTLS_ASN1_SEQUENCE |
648 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
649 TEST_ASSERT( p + len == end );
650 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
651 goto exit;
652 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
653 goto exit;
654 TEST_ASSERT( p == end );
655 }
656 else
657#endif /* MBEDTLS_RSA_C */
658#if defined(MBEDTLS_ECP_C)
659 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
660 {
661 /* ECPoint ::= ...
662 * -- first 8 bits: 0x04;
663 * -- then x_P as an n-bit string, big endian;
664 * -- then y_P as a n-bit string, big endian,
665 * -- where n is the order of the curve.
666 */
667 TEST_ASSERT( bitstring.unused_bits == 0 );
668 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
669 TEST_ASSERT( p[0] == 4 );
670 }
671 else
672#endif /* MBEDTLS_ECP_C */
673 {
674 char message[40];
675 mbedtls_snprintf( message, sizeof( message ),
676 "No sanity check for public key type=0x%08lx",
677 (unsigned long) type );
678 test_fail( message, __LINE__, __FILE__ );
679 return( 0 );
680 }
681 }
682 else
683
684 {
685 /* No sanity checks for other types */
686 }
687
688 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200689
690exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200691 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200692}
693
694static int exercise_export_key( psa_key_slot_t slot,
695 psa_key_usage_t usage )
696{
697 psa_key_type_t type;
698 size_t bits;
699 uint8_t *exported = NULL;
700 size_t exported_size = 0;
701 size_t exported_length = 0;
702 int ok = 0;
703
704 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 )
705 {
706 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
707 PSA_ERROR_NOT_PERMITTED );
708 return( 1 );
709 }
710
711 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
712 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
713 exported = mbedtls_calloc( 1, exported_size );
714 TEST_ASSERT( exported != NULL );
715
716 TEST_ASSERT( psa_export_key( slot,
717 exported, exported_size,
718 &exported_length ) == PSA_SUCCESS );
719 ok = exported_key_sanity_check( type, bits, exported, exported_length );
720
721exit:
722 mbedtls_free( exported );
723 return( ok );
724}
725
726static int exercise_export_public_key( psa_key_slot_t slot )
727{
728 psa_key_type_t type;
729 psa_key_type_t public_type;
730 size_t bits;
731 uint8_t *exported = NULL;
732 size_t exported_size = 0;
733 size_t exported_length = 0;
734 int ok = 0;
735
736 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
737 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
738 {
739 TEST_ASSERT( psa_export_public_key( slot,
740 NULL, 0, &exported_length ) ==
741 PSA_ERROR_INVALID_ARGUMENT );
742 return( 1 );
743 }
744
745 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
746 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
747 exported = mbedtls_calloc( 1, exported_size );
748 TEST_ASSERT( exported != NULL );
749
750 TEST_ASSERT( psa_export_public_key( slot,
751 exported, exported_size,
752 &exported_length ) == PSA_SUCCESS );
753 ok = exported_key_sanity_check( public_type, bits,
754 exported, exported_length );
755
756exit:
757 mbedtls_free( exported );
758 return( ok );
759}
760
Gilles Peskine02b75072018-07-01 22:31:34 +0200761static int exercise_key( psa_key_slot_t slot,
762 psa_key_usage_t usage,
763 psa_algorithm_t alg )
764{
765 int ok;
766 if( alg == 0 )
767 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
768 else if( PSA_ALG_IS_MAC( alg ) )
769 ok = exercise_mac_key( slot, usage, alg );
770 else if( PSA_ALG_IS_CIPHER( alg ) )
771 ok = exercise_cipher_key( slot, usage, alg );
772 else if( PSA_ALG_IS_AEAD( alg ) )
773 ok = exercise_aead_key( slot, usage, alg );
774 else if( PSA_ALG_IS_SIGN( alg ) )
775 ok = exercise_signature_key( slot, usage, alg );
776 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
777 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200778 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
779 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200780 else
781 {
782 char message[40];
783 mbedtls_snprintf( message, sizeof( message ),
784 "No code to exercise alg=0x%08lx",
785 (unsigned long) alg );
786 test_fail( message, __LINE__, __FILE__ );
787 ok = 0;
788 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200789
790 ok = ok && exercise_export_key( slot, usage );
791 ok = ok && exercise_export_public_key( slot );
792
Gilles Peskine02b75072018-07-01 22:31:34 +0200793 return( ok );
794}
795
Gilles Peskinee59236f2018-01-27 23:32:46 +0100796/* END_HEADER */
797
798/* BEGIN_DEPENDENCIES
799 * depends_on:MBEDTLS_PSA_CRYPTO_C
800 * END_DEPENDENCIES
801 */
802
803/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200804void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100805{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100806 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100807 int i;
808 for( i = 0; i <= 1; i++ )
809 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100810 status = psa_crypto_init( );
811 TEST_ASSERT( status == PSA_SUCCESS );
812 status = psa_crypto_init( );
813 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100814 mbedtls_psa_crypto_free( );
815 }
816}
817/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818
819/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200820void fill_slots( int max_arg )
821{
822 /* Fill all the slots until we run out of memory or out of slots,
823 * or until some limit specified in the test data for the sake of
824 * implementations with an essentially unlimited number of slots.
825 * This test assumes that available slots are numbered from 1. */
826
827 psa_key_slot_t slot;
828 psa_key_slot_t max = 0;
829 psa_key_policy_t policy;
830 uint8_t exported[sizeof( max )];
831 size_t exported_size;
832 psa_status_t status;
833
834 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
835
836 psa_key_policy_init( &policy );
837 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
838
839 for( max = 1; max <= (size_t) max_arg; max++ )
840 {
841 status = psa_set_key_policy( max, &policy );
842 /* Stop filling slots if we run out of memory or out of
843 * available slots. */
844 TEST_ASSERT( status == PSA_SUCCESS ||
845 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
846 status == PSA_ERROR_INVALID_ARGUMENT );
847 if( status != PSA_SUCCESS )
848 break;
849 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
850 (uint8_t*) &max, sizeof( max ) );
851 /* Since psa_set_key_policy succeeded, we know that the slot
852 * number is valid. But we may legitimately run out of memory. */
853 TEST_ASSERT( status == PSA_SUCCESS ||
854 status == PSA_ERROR_INSUFFICIENT_MEMORY );
855 if( status != PSA_SUCCESS )
856 break;
857 }
858 /* `max` is now the first slot number that wasn't filled. */
859 max -= 1;
860
861 for( slot = 1; slot <= max; slot++ )
862 {
863 TEST_ASSERT( psa_export_key( slot,
864 exported, sizeof( exported ),
865 &exported_size ) == PSA_SUCCESS );
866 TEST_ASSERT( exported_size == sizeof( slot ) );
867 TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 );
Gilles Peskine996deb12018-08-01 15:45:45 +0200868 }
869
870exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200871 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200872 mbedtls_psa_crypto_free( );
873}
874/* END_CASE */
875
876/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200877void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878{
879 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200880 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100882
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300884 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
886
Gilles Peskine4abf7412018-06-18 16:35:34 +0200887 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200888 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100889 if( status == PSA_SUCCESS )
890 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
891
892exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893 mbedtls_psa_crypto_free( );
894}
895/* END_CASE */
896
897/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200898void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
899{
900 int slot = 1;
901 size_t bits = bits_arg;
902 psa_status_t expected_status = expected_status_arg;
903 psa_status_t status;
904 psa_key_type_t type =
905 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
906 size_t buffer_size = /* Slight overapproximations */
907 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
908 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
909 unsigned char *p;
910 int ret;
911 size_t length;
912
913 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
914 TEST_ASSERT( buffer != NULL );
915
916 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
917 bits, keypair ) ) >= 0 );
918 length = ret;
919
920 /* Try importing the key */
921 status = psa_import_key( slot, type, p, length );
922 TEST_ASSERT( status == expected_status );
923 if( status == PSA_SUCCESS )
924 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
925
926exit:
927 mbedtls_free( buffer );
928 mbedtls_psa_crypto_free( );
929}
930/* END_CASE */
931
932/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300933void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300934 int type_arg,
935 int alg_arg,
936 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100937 int expected_bits,
938 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200939 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940 int canonical_input )
941{
942 int slot = 1;
943 int slot2 = slot + 1;
944 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200945 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200946 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100948 unsigned char *exported = NULL;
949 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100951 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 size_t reexported_length;
953 psa_key_type_t got_type;
954 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200955 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100956
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100957 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300958 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300959 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100960 exported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100961 TEST_ASSERT( export_size == 0 || exported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100962 if( ! canonical_input )
963 {
964 reexported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100965 TEST_ASSERT( export_size == 0 || reexported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 }
967 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
968
mohammad1603a97cb8c2018-03-28 03:46:26 -0700969 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200970 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700971 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
972
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100973 /* Import the key */
974 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200975 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100976
977 /* Test the key information */
978 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200979 &got_type,
980 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981 TEST_ASSERT( got_type == type );
982 TEST_ASSERT( got_bits == (size_t) expected_bits );
983
984 /* Export the key */
985 status = psa_export_key( slot,
986 exported, export_size,
987 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200988 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100989
990 /* The exported length must be set by psa_export_key() to a value between 0
991 * and export_size. On errors, the exported length must be 0. */
992 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
993 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
994 TEST_ASSERT( exported_length <= export_size );
995
Gilles Peskine3f669c32018-06-21 09:21:51 +0200996 TEST_ASSERT( mem_is_zero( exported + exported_length,
997 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200999 {
1000 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001001 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001002 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001003
Gilles Peskine8f609232018-08-11 01:24:55 +02001004 if( ! exercise_export_key( slot, usage_arg ) )
1005 goto exit;
1006
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 if( canonical_input )
1008 {
Gilles Peskine4abf7412018-06-18 16:35:34 +02001009 TEST_ASSERT( exported_length == data->len );
1010 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011 }
1012 else
1013 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001014 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1015
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001017 exported,
1018 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001020 reexported,
1021 export_size,
1022 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023 TEST_ASSERT( reexported_length == exported_length );
1024 TEST_ASSERT( memcmp( reexported, exported,
1025 exported_length ) == 0 );
1026 }
1027
1028destroy:
1029 /* Destroy the key */
1030 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1031 TEST_ASSERT( psa_get_key_information(
1032 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1033
1034exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001035 mbedtls_free( exported );
1036 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001037 mbedtls_psa_crypto_free( );
1038}
1039/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001040
Moran Pekerf709f4a2018-06-06 17:26:04 +03001041/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001042void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001043 int type_arg,
1044 int alg_arg,
1045 int expected_bits,
1046 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001047 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001048{
1049 int slot = 1;
1050 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001051 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001052 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001053 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001054 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001055 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001056 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001057 psa_key_type_t got_type;
1058 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001059 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001060
Moran Pekerf709f4a2018-06-06 17:26:04 +03001061 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001062 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001063 export_size = (ptrdiff_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001064 exported = mbedtls_calloc( 1, export_size );
1065 TEST_ASSERT( exported != NULL );
1066
1067 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1068
1069 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001070 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001071 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1072
1073 /* Import the key */
1074 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001075 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001076
1077 /* Test the key information */
1078 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001079 &got_type,
1080 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001081 TEST_ASSERT( got_type == type );
1082 TEST_ASSERT( got_bits == (size_t) expected_bits );
1083
1084 /* Export the key */
1085 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001086 exported, export_size,
1087 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001088 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +01001089 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
1090 TEST_ASSERT( mem_is_zero( exported + exported_length,
1091 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001092 if( status != PSA_SUCCESS )
1093 goto destroy;
1094
Moran Pekerf709f4a2018-06-06 17:26:04 +03001095destroy:
1096 /* Destroy the key */
1097 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1098 TEST_ASSERT( psa_get_key_information(
1099 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1100
1101exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001102 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001103 mbedtls_psa_crypto_free( );
1104}
1105/* END_CASE */
1106
Gilles Peskine20035e32018-02-03 22:44:14 +01001107/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001108void import_and_exercise_key( data_t *data,
1109 int type_arg,
1110 int bits_arg,
1111 int alg_arg )
1112{
1113 int slot = 1;
1114 psa_key_type_t type = type_arg;
1115 size_t bits = bits_arg;
1116 psa_algorithm_t alg = alg_arg;
1117 psa_key_usage_t usage =
1118 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1119 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1120 PSA_KEY_USAGE_VERIFY :
1121 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1122 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1123 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1124 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1125 PSA_KEY_USAGE_ENCRYPT :
1126 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001127 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001128 0 );
1129 psa_key_policy_t policy;
1130 psa_key_type_t got_type;
1131 size_t got_bits;
1132 psa_status_t status;
1133
1134 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1135
1136 psa_key_policy_init( &policy );
1137 psa_key_policy_set_usage( &policy, usage, alg );
1138 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1139
1140 /* Import the key */
1141 status = psa_import_key( slot, type, data->x, data->len );
1142 TEST_ASSERT( status == PSA_SUCCESS );
1143
1144 /* Test the key information */
1145 TEST_ASSERT( psa_get_key_information( slot,
1146 &got_type,
1147 &got_bits ) == PSA_SUCCESS );
1148 TEST_ASSERT( got_type == type );
1149 TEST_ASSERT( got_bits == bits );
1150
1151 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001152 if( ! exercise_key( slot, usage, alg ) )
1153 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001154
1155exit:
1156 psa_destroy_key( slot );
1157 mbedtls_psa_crypto_free( );
1158}
1159/* END_CASE */
1160
1161/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001162void key_policy( int usage_arg, int alg_arg )
1163{
1164 int key_slot = 1;
1165 psa_algorithm_t alg = alg_arg;
1166 psa_key_usage_t usage = usage_arg;
1167 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1168 unsigned char key[32] = {0};
1169 psa_key_policy_t policy_set;
1170 psa_key_policy_t policy_get;
1171
1172 memset( key, 0x2a, sizeof( key ) );
1173
1174 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1175
1176 psa_key_policy_init( &policy_set );
1177 psa_key_policy_init( &policy_get );
1178
1179 psa_key_policy_set_usage( &policy_set, usage, alg );
1180
1181 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1182 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1183 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1184
1185 TEST_ASSERT( psa_import_key( key_slot, key_type,
1186 key, sizeof( key ) ) == PSA_SUCCESS );
1187
1188 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1189
1190 TEST_ASSERT( policy_get.usage == policy_set.usage );
1191 TEST_ASSERT( policy_get.alg == policy_set.alg );
1192
1193exit:
1194 psa_destroy_key( key_slot );
1195 mbedtls_psa_crypto_free( );
1196}
1197/* END_CASE */
1198
1199/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001200void mac_key_policy( int policy_usage,
1201 int policy_alg,
1202 int key_type,
1203 data_t *key_data,
1204 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001205{
1206 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001207 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208 psa_mac_operation_t operation;
1209 psa_status_t status;
1210 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001211
1212 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1213
1214 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001215 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001216 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1217
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218 TEST_ASSERT( psa_import_key( key_slot, key_type,
1219 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001220
Gilles Peskine89167cb2018-07-08 20:12:23 +02001221 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222 if( policy_alg == exercise_alg &&
1223 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1224 TEST_ASSERT( status == PSA_SUCCESS );
1225 else
1226 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1227 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001228
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001229 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001230 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001231 if( policy_alg == exercise_alg &&
1232 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001233 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001234 else
1235 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1236
1237exit:
1238 psa_mac_abort( &operation );
1239 psa_destroy_key( key_slot );
1240 mbedtls_psa_crypto_free( );
1241}
1242/* END_CASE */
1243
1244/* BEGIN_CASE */
1245void cipher_key_policy( int policy_usage,
1246 int policy_alg,
1247 int key_type,
1248 data_t *key_data,
1249 int exercise_alg )
1250{
1251 int key_slot = 1;
1252 psa_key_policy_t policy;
1253 psa_cipher_operation_t operation;
1254 psa_status_t status;
1255
1256 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1257
1258 psa_key_policy_init( &policy );
1259 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1260 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1261
1262 TEST_ASSERT( psa_import_key( key_slot, key_type,
1263 key_data->x, key_data->len ) == PSA_SUCCESS );
1264
Gilles Peskinefe119512018-07-08 21:39:34 +02001265 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266 if( policy_alg == exercise_alg &&
1267 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1268 TEST_ASSERT( status == PSA_SUCCESS );
1269 else
1270 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1271 psa_cipher_abort( &operation );
1272
Gilles Peskinefe119512018-07-08 21:39:34 +02001273 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001274 if( policy_alg == exercise_alg &&
1275 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1276 TEST_ASSERT( status == PSA_SUCCESS );
1277 else
1278 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1279
1280exit:
1281 psa_cipher_abort( &operation );
1282 psa_destroy_key( key_slot );
1283 mbedtls_psa_crypto_free( );
1284}
1285/* END_CASE */
1286
1287/* BEGIN_CASE */
1288void aead_key_policy( int policy_usage,
1289 int policy_alg,
1290 int key_type,
1291 data_t *key_data,
1292 int nonce_length_arg,
1293 int tag_length_arg,
1294 int exercise_alg )
1295{
1296 int key_slot = 1;
1297 psa_key_policy_t policy;
1298 psa_status_t status;
1299 unsigned char nonce[16] = {0};
1300 size_t nonce_length = nonce_length_arg;
1301 unsigned char tag[16];
1302 size_t tag_length = tag_length_arg;
1303 size_t output_length;
1304
1305 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1306 TEST_ASSERT( tag_length <= sizeof( tag ) );
1307
1308 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1309
1310 psa_key_policy_init( &policy );
1311 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1312 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1313
1314 TEST_ASSERT( psa_import_key( key_slot, key_type,
1315 key_data->x, key_data->len ) == PSA_SUCCESS );
1316
1317 status = psa_aead_encrypt( key_slot, exercise_alg,
1318 nonce, nonce_length,
1319 NULL, 0,
1320 NULL, 0,
1321 tag, tag_length,
1322 &output_length );
1323 if( policy_alg == exercise_alg &&
1324 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1325 TEST_ASSERT( status == PSA_SUCCESS );
1326 else
1327 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1328
1329 memset( tag, 0, sizeof( tag ) );
1330 status = psa_aead_decrypt( key_slot, exercise_alg,
1331 nonce, nonce_length,
1332 NULL, 0,
1333 tag, tag_length,
1334 NULL, 0,
1335 &output_length );
1336 if( policy_alg == exercise_alg &&
1337 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1338 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1339 else
1340 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1341
1342exit:
1343 psa_destroy_key( key_slot );
1344 mbedtls_psa_crypto_free( );
1345}
1346/* END_CASE */
1347
1348/* BEGIN_CASE */
1349void asymmetric_encryption_key_policy( int policy_usage,
1350 int policy_alg,
1351 int key_type,
1352 data_t *key_data,
1353 int exercise_alg )
1354{
1355 int key_slot = 1;
1356 psa_key_policy_t policy;
1357 psa_status_t status;
1358 size_t key_bits;
1359 size_t buffer_length;
1360 unsigned char *buffer = NULL;
1361 size_t output_length;
1362
1363 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1364
1365 psa_key_policy_init( &policy );
1366 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1367 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1368
1369 TEST_ASSERT( psa_import_key( key_slot, key_type,
1370 key_data->x, key_data->len ) == PSA_SUCCESS );
1371
1372 TEST_ASSERT( psa_get_key_information( key_slot,
1373 NULL,
1374 &key_bits ) == PSA_SUCCESS );
1375 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1376 exercise_alg );
1377 buffer = mbedtls_calloc( 1, buffer_length );
1378 TEST_ASSERT( buffer != NULL );
1379
1380 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1381 NULL, 0,
1382 NULL, 0,
1383 buffer, buffer_length,
1384 &output_length );
1385 if( policy_alg == exercise_alg &&
1386 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1387 TEST_ASSERT( status == PSA_SUCCESS );
1388 else
1389 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1390
1391 memset( buffer, 0, buffer_length );
1392 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1393 buffer, buffer_length,
1394 NULL, 0,
1395 buffer, buffer_length,
1396 &output_length );
1397 if( policy_alg == exercise_alg &&
1398 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1399 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1400 else
1401 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1402
1403exit:
1404 psa_destroy_key( key_slot );
1405 mbedtls_psa_crypto_free( );
1406 mbedtls_free( buffer );
1407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
1411void asymmetric_signature_key_policy( int policy_usage,
1412 int policy_alg,
1413 int key_type,
1414 data_t *key_data,
1415 int exercise_alg )
1416{
1417 int key_slot = 1;
1418 psa_key_policy_t policy;
1419 psa_status_t status;
1420 unsigned char payload[16] = {1};
1421 size_t payload_length = sizeof( payload );
1422 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1423 size_t signature_length;
1424
1425 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1426
1427 psa_key_policy_init( &policy );
1428 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1429 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1430
1431 TEST_ASSERT( psa_import_key( key_slot, key_type,
1432 key_data->x, key_data->len ) == PSA_SUCCESS );
1433
1434 status = psa_asymmetric_sign( key_slot, exercise_alg,
1435 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001436 signature, sizeof( signature ),
1437 &signature_length );
1438 if( policy_alg == exercise_alg &&
1439 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1440 TEST_ASSERT( status == PSA_SUCCESS );
1441 else
1442 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1443
1444 memset( signature, 0, sizeof( signature ) );
1445 status = psa_asymmetric_verify( key_slot, exercise_alg,
1446 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001447 signature, sizeof( signature ) );
1448 if( policy_alg == exercise_alg &&
1449 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1450 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1451 else
1452 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001453
1454exit:
1455 psa_destroy_key( key_slot );
1456 mbedtls_psa_crypto_free( );
1457}
1458/* END_CASE */
1459
1460/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001461void derive_key_policy( int policy_usage,
1462 int policy_alg,
1463 int key_type,
1464 data_t *key_data,
1465 int exercise_alg )
1466{
1467 int key_slot = 1;
1468 psa_key_policy_t policy;
1469 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1470 psa_status_t status;
1471
1472 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1473
1474 psa_key_policy_init( &policy );
1475 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1476 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1477
1478 TEST_ASSERT( psa_import_key( key_slot, key_type,
1479 key_data->x, key_data->len ) == PSA_SUCCESS );
1480
1481 status = psa_key_derivation( &generator, key_slot,
1482 exercise_alg,
1483 NULL, 0,
1484 NULL, 0,
1485 1 );
1486 if( policy_alg == exercise_alg &&
1487 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1488 TEST_ASSERT( status == PSA_SUCCESS );
1489 else
1490 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1491
1492exit:
1493 psa_generator_abort( &generator );
1494 psa_destroy_key( key_slot );
1495 mbedtls_psa_crypto_free( );
1496}
1497/* END_CASE */
1498
1499/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001500void key_lifetime( int lifetime_arg )
1501{
1502 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001503 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001504 unsigned char key[32] = {0};
1505 psa_key_lifetime_t lifetime_set = lifetime_arg;
1506 psa_key_lifetime_t lifetime_get;
1507
1508 memset( key, 0x2a, sizeof( key ) );
1509
1510 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1511
1512 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1513 lifetime_set ) == PSA_SUCCESS );
1514
1515 TEST_ASSERT( psa_import_key( key_slot, key_type,
1516 key, sizeof( key ) ) == PSA_SUCCESS );
1517
1518 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1519 &lifetime_get ) == PSA_SUCCESS );
1520
1521 TEST_ASSERT( lifetime_get == lifetime_set );
1522
1523exit:
1524 psa_destroy_key( key_slot );
1525 mbedtls_psa_crypto_free( );
1526}
1527/* END_CASE */
1528
1529/* BEGIN_CASE */
1530void key_lifetime_set_fail( int key_slot_arg,
1531 int lifetime_arg,
1532 int expected_status_arg )
1533{
1534 psa_key_slot_t key_slot = key_slot_arg;
1535 psa_key_lifetime_t lifetime_set = lifetime_arg;
1536 psa_status_t actual_status;
1537 psa_status_t expected_status = expected_status_arg;
1538
1539 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1540
1541 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1542
1543 if( actual_status == PSA_SUCCESS )
1544 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1545
1546 TEST_ASSERT( expected_status == actual_status );
1547
1548exit:
1549 psa_destroy_key( key_slot );
1550 mbedtls_psa_crypto_free( );
1551}
1552/* END_CASE */
1553
1554/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001555void hash_setup( int alg_arg,
1556 int expected_status_arg )
1557{
1558 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001559 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001560 psa_hash_operation_t operation;
1561 psa_status_t status;
1562
1563 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1564
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001565 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001566 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001567 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001568
1569exit:
1570 mbedtls_psa_crypto_free( );
1571}
1572/* END_CASE */
1573
1574/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001575void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001576{
1577 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001578 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001579 size_t actual_hash_length;
1580 psa_hash_operation_t operation;
1581
Gilles Peskine69c12672018-06-28 00:07:19 +02001582 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1583 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1584
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001585 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001586 TEST_ASSERT( expected_hash != NULL );
1587 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1588 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001589
1590 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1591
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001592 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001593 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001594 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001595 TEST_ASSERT( psa_hash_finish( &operation,
1596 actual_hash, sizeof( actual_hash ),
1597 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001598 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001599 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001600 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001601
1602exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001603 mbedtls_psa_crypto_free( );
1604}
1605/* END_CASE */
1606
1607/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001608void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001609{
1610 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001611 psa_hash_operation_t operation;
1612
Gilles Peskine69c12672018-06-28 00:07:19 +02001613 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1614 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1615
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001616 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001617 TEST_ASSERT( expected_hash != NULL );
1618 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1619 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001620
1621 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1622
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001623 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001624 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001625 input->x,
1626 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001627 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001628 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001629 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001630
1631exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001632 mbedtls_psa_crypto_free( );
1633}
1634/* END_CASE */
1635
1636/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001637void mac_setup( int key_type_arg,
1638 data_t *key,
1639 int alg_arg,
1640 int expected_status_arg )
1641{
1642 int key_slot = 1;
1643 psa_key_type_t key_type = key_type_arg;
1644 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001645 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001646 psa_mac_operation_t operation;
1647 psa_key_policy_t policy;
1648 psa_status_t status;
1649
1650 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1651
1652 psa_key_policy_init( &policy );
1653 psa_key_policy_set_usage( &policy,
1654 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1655 alg );
1656 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1657
1658 TEST_ASSERT( psa_import_key( key_slot, key_type,
1659 key->x, key->len ) == PSA_SUCCESS );
1660
Gilles Peskine89167cb2018-07-08 20:12:23 +02001661 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001662 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001663 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001664
1665exit:
1666 psa_destroy_key( key_slot );
1667 mbedtls_psa_crypto_free( );
1668}
1669/* END_CASE */
1670
1671/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001672void mac_verify( int key_type_arg,
1673 data_t *key,
1674 int alg_arg,
1675 data_t *input,
1676 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001677{
1678 int key_slot = 1;
1679 psa_key_type_t key_type = key_type_arg;
1680 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001681 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001682 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683
Gilles Peskine69c12672018-06-28 00:07:19 +02001684 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1685
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001687 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001688 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001689 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001690 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1691 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692
1693 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1694
mohammad16036df908f2018-04-02 08:34:15 -07001695 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001696 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001697 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1698
Gilles Peskine8c9def32018-02-08 10:02:12 +01001699 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001700 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001701
Gilles Peskine89167cb2018-07-08 20:12:23 +02001702 TEST_ASSERT( psa_mac_verify_setup( &operation,
1703 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001704 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1705 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001706 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001707 TEST_ASSERT( psa_mac_verify_finish( &operation,
1708 expected_mac->x,
1709 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001710
1711exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001712 psa_destroy_key( key_slot );
1713 mbedtls_psa_crypto_free( );
1714}
1715/* END_CASE */
1716
1717/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001718void cipher_setup( int key_type_arg,
1719 data_t *key,
1720 int alg_arg,
1721 int expected_status_arg )
1722{
1723 int key_slot = 1;
1724 psa_key_type_t key_type = key_type_arg;
1725 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001726 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001727 psa_cipher_operation_t operation;
1728 psa_key_policy_t policy;
1729 psa_status_t status;
1730
1731 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1732
1733 psa_key_policy_init( &policy );
1734 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1735 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1736
1737 TEST_ASSERT( psa_import_key( key_slot, key_type,
1738 key->x, key->len ) == PSA_SUCCESS );
1739
Gilles Peskinefe119512018-07-08 21:39:34 +02001740 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001741 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001742 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001743
1744exit:
1745 psa_destroy_key( key_slot );
1746 mbedtls_psa_crypto_free( );
1747}
1748/* END_CASE */
1749
1750/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001751void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001752 data_t *key,
1753 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001754 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001755{
1756 int key_slot = 1;
1757 psa_status_t status;
1758 psa_key_type_t key_type = key_type_arg;
1759 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001760 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001761 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001762 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001763 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001764 size_t output_buffer_size = 0;
1765 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001766 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001767 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001768 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001769
Gilles Peskine50e586b2018-06-08 14:28:46 +02001770 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001771 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001772 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001773 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1774 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1775 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001776
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001777 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1778 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001779
1780 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1781
Moran Pekered346952018-07-05 15:22:45 +03001782 psa_key_policy_init( &policy );
1783 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1784 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1785
Gilles Peskine50e586b2018-06-08 14:28:46 +02001786 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001787 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001788
Gilles Peskinefe119512018-07-08 21:39:34 +02001789 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1790 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001791
Gilles Peskinefe119512018-07-08 21:39:34 +02001792 TEST_ASSERT( psa_cipher_set_iv( &operation,
1793 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001794 output_buffer_size = (size_t) input->len +
1795 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001796 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001797 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001798
Gilles Peskine4abf7412018-06-18 16:35:34 +02001799 TEST_ASSERT( psa_cipher_update( &operation,
1800 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001801 output, output_buffer_size,
1802 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001803 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001804 status = psa_cipher_finish( &operation,
1805 output + function_output_length,
1806 output_buffer_size,
1807 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001808 total_output_length += function_output_length;
1809
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001810 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001811 if( expected_status == PSA_SUCCESS )
1812 {
1813 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001814 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001815 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001816 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001817 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001818
Gilles Peskine50e586b2018-06-08 14:28:46 +02001819exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001820 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001821 psa_destroy_key( key_slot );
1822 mbedtls_psa_crypto_free( );
1823}
1824/* END_CASE */
1825
1826/* BEGIN_CASE */
1827void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001828 data_t *key,
1829 data_t *input,
1830 int first_part_size,
1831 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001832{
1833 int key_slot = 1;
1834 psa_key_type_t key_type = key_type_arg;
1835 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001836 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001837 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001838 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001839 size_t output_buffer_size = 0;
1840 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001841 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001842 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001843 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001844
Gilles Peskine50e586b2018-06-08 14:28:46 +02001845 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001846 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001847 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001848 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1849 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1850 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001851
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001852 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1853 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001854
1855 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1856
Moran Pekered346952018-07-05 15:22:45 +03001857 psa_key_policy_init( &policy );
1858 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1859 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1860
Gilles Peskine50e586b2018-06-08 14:28:46 +02001861 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001862 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001863
Gilles Peskinefe119512018-07-08 21:39:34 +02001864 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1865 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001866
Gilles Peskinefe119512018-07-08 21:39:34 +02001867 TEST_ASSERT( psa_cipher_set_iv( &operation,
1868 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001869 output_buffer_size = (size_t) input->len +
1870 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001871 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001872 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001873
Gilles Peskine4abf7412018-06-18 16:35:34 +02001874 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001875 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001876 output, output_buffer_size,
1877 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001878 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001879 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001880 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001881 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001882 output, output_buffer_size,
1883 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001884 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001885 TEST_ASSERT( psa_cipher_finish( &operation,
1886 output + function_output_length,
1887 output_buffer_size,
1888 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001889 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001890 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1891
Gilles Peskine4abf7412018-06-18 16:35:34 +02001892 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001893 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001894 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001895
1896exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001897 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001898 psa_destroy_key( key_slot );
1899 mbedtls_psa_crypto_free( );
1900}
1901/* END_CASE */
1902
1903/* BEGIN_CASE */
1904void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001905 data_t *key,
1906 data_t *input,
1907 int first_part_size,
1908 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001909{
1910 int key_slot = 1;
1911
1912 psa_key_type_t key_type = key_type_arg;
1913 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001914 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001915 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001916 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001917 size_t output_buffer_size = 0;
1918 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001919 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001920 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001921 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001922
Gilles Peskine50e586b2018-06-08 14:28:46 +02001923 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001924 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001925 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001926 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1927 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1928 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001929
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001930 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1931 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001932
1933 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1934
Moran Pekered346952018-07-05 15:22:45 +03001935 psa_key_policy_init( &policy );
1936 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1937 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1938
Gilles Peskine50e586b2018-06-08 14:28:46 +02001939 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001940 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001941
Gilles Peskinefe119512018-07-08 21:39:34 +02001942 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1943 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001944
Gilles Peskinefe119512018-07-08 21:39:34 +02001945 TEST_ASSERT( psa_cipher_set_iv( &operation,
1946 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001947
mohammad16033d91abe2018-07-03 13:15:54 +03001948 output_buffer_size = (size_t) input->len +
1949 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001950 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001951 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001952
Gilles Peskine4abf7412018-06-18 16:35:34 +02001953 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1954 TEST_ASSERT( psa_cipher_update( &operation,
1955 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001956 output, output_buffer_size,
1957 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001958 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001959 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001960 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001961 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001962 output, output_buffer_size,
1963 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001964 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001965 TEST_ASSERT( psa_cipher_finish( &operation,
1966 output + function_output_length,
1967 output_buffer_size,
1968 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001969 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001970 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1971
Gilles Peskine4abf7412018-06-18 16:35:34 +02001972 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001973 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001974 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001975
1976exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001977 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001978 psa_destroy_key( key_slot );
1979 mbedtls_psa_crypto_free( );
1980}
1981/* END_CASE */
1982
Gilles Peskine50e586b2018-06-08 14:28:46 +02001983/* BEGIN_CASE */
1984void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001985 data_t *key,
1986 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001987 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001988{
1989 int key_slot = 1;
1990 psa_status_t status;
1991 psa_key_type_t key_type = key_type_arg;
1992 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001993 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001994 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001995 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001996 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001997 size_t output_buffer_size = 0;
1998 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001999 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002000 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002001 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002002
Gilles Peskine50e586b2018-06-08 14:28:46 +02002003 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002004 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002005 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002006 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2007 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2008 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002009
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002010 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2011 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002012
2013 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2014
Moran Pekered346952018-07-05 15:22:45 +03002015 psa_key_policy_init( &policy );
2016 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2017 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2018
Gilles Peskine50e586b2018-06-08 14:28:46 +02002019 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002020 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002021
Gilles Peskinefe119512018-07-08 21:39:34 +02002022 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2023 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002024
Gilles Peskinefe119512018-07-08 21:39:34 +02002025 TEST_ASSERT( psa_cipher_set_iv( &operation,
2026 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002027
mohammad16033d91abe2018-07-03 13:15:54 +03002028 output_buffer_size = (size_t) input->len +
2029 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002030 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002031 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002032
Gilles Peskine4abf7412018-06-18 16:35:34 +02002033 TEST_ASSERT( psa_cipher_update( &operation,
2034 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002035 output, output_buffer_size,
2036 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002037 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002038 status = psa_cipher_finish( &operation,
2039 output + function_output_length,
2040 output_buffer_size,
2041 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002042 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002043 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002044
2045 if( expected_status == PSA_SUCCESS )
2046 {
2047 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002048 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002049 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002050 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002051 }
2052
Gilles Peskine50e586b2018-06-08 14:28:46 +02002053exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002054 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002055 psa_destroy_key( key_slot );
2056 mbedtls_psa_crypto_free( );
2057}
2058/* END_CASE */
2059
Gilles Peskine50e586b2018-06-08 14:28:46 +02002060/* BEGIN_CASE */
2061void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002062 data_t *key,
2063 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002064{
2065 int key_slot = 1;
2066 psa_key_type_t key_type = key_type_arg;
2067 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002068 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002069 size_t iv_size = 16;
2070 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002071 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002072 size_t output1_size = 0;
2073 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002074 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002075 size_t output2_size = 0;
2076 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002077 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002078 psa_cipher_operation_t operation1;
2079 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002080 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002081
mohammad1603d7d7ba52018-03-12 18:51:53 +02002082 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002083 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002084 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2085 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002086
mohammad1603d7d7ba52018-03-12 18:51:53 +02002087 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2088
Moran Pekered346952018-07-05 15:22:45 +03002089 psa_key_policy_init( &policy );
2090 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2091 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2092
mohammad1603d7d7ba52018-03-12 18:51:53 +02002093 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002094 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002095
Gilles Peskinefe119512018-07-08 21:39:34 +02002096 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2097 key_slot, alg ) == PSA_SUCCESS );
2098 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2099 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002100
Gilles Peskinefe119512018-07-08 21:39:34 +02002101 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2102 iv, iv_size,
2103 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002104 output1_size = (size_t) input->len +
2105 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002106 output1 = mbedtls_calloc( 1, output1_size );
2107 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002108
Gilles Peskine4abf7412018-06-18 16:35:34 +02002109 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002110 output1, output1_size,
2111 &output1_length ) == PSA_SUCCESS );
2112 TEST_ASSERT( psa_cipher_finish( &operation1,
2113 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002114 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002115
Gilles Peskine048b7f02018-06-08 14:20:49 +02002116 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002117
2118 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2119
2120 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002121 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002122 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002123
Gilles Peskinefe119512018-07-08 21:39:34 +02002124 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2125 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002126 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2127 output2, output2_size,
2128 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002129 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002130 TEST_ASSERT( psa_cipher_finish( &operation2,
2131 output2 + output2_length,
2132 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002133 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002134
Gilles Peskine048b7f02018-06-08 14:20:49 +02002135 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002136
Janos Follath25c4fa82018-07-06 16:23:25 +01002137 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002138
Gilles Peskine4abf7412018-06-18 16:35:34 +02002139 TEST_ASSERT( input->len == output2_length );
2140 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03002141
2142exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002143 mbedtls_free( output1 );
2144 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002145 psa_destroy_key( key_slot );
2146 mbedtls_psa_crypto_free( );
2147}
2148/* END_CASE */
2149
2150/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002151void cipher_verify_output_multipart( int alg_arg,
2152 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002153 data_t *key,
2154 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002155 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002156{
2157 int key_slot = 1;
2158 psa_key_type_t key_type = key_type_arg;
2159 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002160 unsigned char iv[16] = {0};
2161 size_t iv_size = 16;
2162 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002163 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002164 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002165 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002166 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002167 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002168 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002169 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002170 psa_cipher_operation_t operation1;
2171 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002172 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002173
Moran Pekerded84402018-06-06 16:36:50 +03002174 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002175 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002176 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2177 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002178
Moran Pekerded84402018-06-06 16:36:50 +03002179 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2180
Moran Pekered346952018-07-05 15:22:45 +03002181 psa_key_policy_init( &policy );
2182 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2183 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2184
Moran Pekerded84402018-06-06 16:36:50 +03002185 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002186 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002187
Gilles Peskinefe119512018-07-08 21:39:34 +02002188 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2189 key_slot, alg ) == PSA_SUCCESS );
2190 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2191 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002192
Gilles Peskinefe119512018-07-08 21:39:34 +02002193 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2194 iv, iv_size,
2195 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002196 output1_buffer_size = (size_t) input->len +
2197 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002198 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002199 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002200
Gilles Peskine4abf7412018-06-18 16:35:34 +02002201 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002202
itayzafrir3e02b3b2018-06-12 17:06:52 +03002203 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002204 output1, output1_buffer_size,
2205 &function_output_length ) == PSA_SUCCESS );
2206 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002207
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002208 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002209 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002210 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002211 output1, output1_buffer_size,
2212 &function_output_length ) == PSA_SUCCESS );
2213 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002214
Gilles Peskine048b7f02018-06-08 14:20:49 +02002215 TEST_ASSERT( psa_cipher_finish( &operation1,
2216 output1 + output1_length,
2217 output1_buffer_size - output1_length,
2218 &function_output_length ) == PSA_SUCCESS );
2219 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002220
2221 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2222
Gilles Peskine048b7f02018-06-08 14:20:49 +02002223 output2_buffer_size = output1_length;
2224 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002225 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002226
Gilles Peskinefe119512018-07-08 21:39:34 +02002227 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2228 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002229
2230 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002231 output2, output2_buffer_size,
2232 &function_output_length ) == PSA_SUCCESS );
2233 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002234
Gilles Peskine048b7f02018-06-08 14:20:49 +02002235 TEST_ASSERT( psa_cipher_update( &operation2,
2236 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002237 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002238 output2, output2_buffer_size,
2239 &function_output_length ) == PSA_SUCCESS );
2240 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002241
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002242 TEST_ASSERT( psa_cipher_finish( &operation2,
2243 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002244 output2_buffer_size - output2_length,
2245 &function_output_length ) == PSA_SUCCESS );
2246 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002247
Janos Follath25c4fa82018-07-06 16:23:25 +01002248 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002249
Gilles Peskine4abf7412018-06-18 16:35:34 +02002250 TEST_ASSERT( input->len == output2_length );
2251 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002252
2253exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002254 mbedtls_free( output1 );
2255 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002256 psa_destroy_key( key_slot );
2257 mbedtls_psa_crypto_free( );
2258}
2259/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002260
Gilles Peskine20035e32018-02-03 22:44:14 +01002261/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002262void aead_encrypt_decrypt( int key_type_arg,
2263 data_t * key_data,
2264 int alg_arg,
2265 data_t * input_data,
2266 data_t * nonce,
2267 data_t * additional_data,
2268 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002269{
2270 int slot = 1;
2271 psa_key_type_t key_type = key_type_arg;
2272 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002273 unsigned char *output_data = NULL;
2274 size_t output_size = 0;
2275 size_t output_length = 0;
2276 unsigned char *output_data2 = NULL;
2277 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002278 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002279 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002280 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002281
Gilles Peskinea1cac842018-06-11 19:33:02 +02002282 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002283 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002284 TEST_ASSERT( nonce != NULL );
2285 TEST_ASSERT( additional_data != NULL );
2286 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2287 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2288 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2289 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2290
Gilles Peskine4abf7412018-06-18 16:35:34 +02002291 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002292 output_data = mbedtls_calloc( 1, output_size );
2293 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002294
2295 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2296
2297 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002298 psa_key_policy_set_usage( &policy,
2299 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2300 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002301 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2302
2303 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002304 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002305
2306 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002307 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002308 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002309 additional_data->len,
2310 input_data->x, input_data->len,
2311 output_data, output_size,
2312 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002313
2314 if( PSA_SUCCESS == expected_result )
2315 {
2316 output_data2 = mbedtls_calloc( 1, output_length );
2317 TEST_ASSERT( output_data2 != NULL );
2318
2319 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002320 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002321 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002322 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002323 output_data, output_length,
2324 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002325 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002326
itayzafrir3e02b3b2018-06-12 17:06:52 +03002327 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002328 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002329 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002330
Gilles Peskinea1cac842018-06-11 19:33:02 +02002331exit:
2332 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002333 mbedtls_free( output_data );
2334 mbedtls_free( output_data2 );
2335 mbedtls_psa_crypto_free( );
2336}
2337/* END_CASE */
2338
2339/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002340void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002341 int alg_arg, data_t * input_data,
2342 data_t * additional_data, data_t * nonce,
2343 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002344{
2345 int slot = 1;
2346 psa_key_type_t key_type = key_type_arg;
2347 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002348 unsigned char *output_data = NULL;
2349 size_t output_size = 0;
2350 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002351 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002352 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002353
Gilles Peskinea1cac842018-06-11 19:33:02 +02002354 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002355 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002356 TEST_ASSERT( additional_data != NULL );
2357 TEST_ASSERT( nonce != NULL );
2358 TEST_ASSERT( expected_result != NULL );
2359 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2360 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2361 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2362 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2363 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2364
Gilles Peskine4abf7412018-06-18 16:35:34 +02002365 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002366 output_data = mbedtls_calloc( 1, output_size );
2367 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002368
Gilles Peskinea1cac842018-06-11 19:33:02 +02002369 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2370
2371 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002372 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002373 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2374
2375 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002376 key_data->x,
2377 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002378
2379 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002380 nonce->x, nonce->len,
2381 additional_data->x, additional_data->len,
2382 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002383 output_data, output_size,
2384 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002385
itayzafrir3e02b3b2018-06-12 17:06:52 +03002386 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002387 output_length ) == 0 );
2388
Gilles Peskinea1cac842018-06-11 19:33:02 +02002389exit:
2390 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002391 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002392 mbedtls_psa_crypto_free( );
2393}
2394/* END_CASE */
2395
2396/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002397void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002398 int alg_arg, data_t * input_data,
2399 data_t * additional_data, data_t * nonce,
2400 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002401{
2402 int slot = 1;
2403 psa_key_type_t key_type = key_type_arg;
2404 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002405 unsigned char *output_data = NULL;
2406 size_t output_size = 0;
2407 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002408 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002409 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002410 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002411
Gilles Peskinea1cac842018-06-11 19:33:02 +02002412 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002413 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002414 TEST_ASSERT( additional_data != NULL );
2415 TEST_ASSERT( nonce != NULL );
2416 TEST_ASSERT( expected_data != NULL );
2417 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2418 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2419 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2420 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2421 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2422
Gilles Peskine4abf7412018-06-18 16:35:34 +02002423 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002424 output_data = mbedtls_calloc( 1, output_size );
2425 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002426
Gilles Peskinea1cac842018-06-11 19:33:02 +02002427 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2428
2429 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002430 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002431 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2432
2433 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002434 key_data->x,
2435 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002436
2437 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002438 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002439 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002440 additional_data->len,
2441 input_data->x, input_data->len,
2442 output_data, output_size,
2443 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002444
Gilles Peskine2d277862018-06-18 15:41:12 +02002445 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002446 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002447 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002448 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002449 }
2450
Gilles Peskinea1cac842018-06-11 19:33:02 +02002451exit:
2452 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002453 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002454 mbedtls_psa_crypto_free( );
2455}
2456/* END_CASE */
2457
2458/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002459void signature_size( int type_arg,
2460 int bits,
2461 int alg_arg,
2462 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002463{
2464 psa_key_type_t type = type_arg;
2465 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002466 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002467 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2468exit:
2469 ;
2470}
2471/* END_CASE */
2472
2473/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002474void sign_deterministic( int key_type_arg, data_t *key_data,
2475 int alg_arg, data_t *input_data,
2476 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002477{
2478 int slot = 1;
2479 psa_key_type_t key_type = key_type_arg;
2480 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002481 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002482 unsigned char *signature = NULL;
2483 size_t signature_size;
2484 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002485 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002486
Gilles Peskine20035e32018-02-03 22:44:14 +01002487 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002488 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002489 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002490 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2491 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2492 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002493
2494 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2495
mohammad1603a97cb8c2018-03-28 03:46:26 -07002496 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002497 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002498 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2499
Gilles Peskine20035e32018-02-03 22:44:14 +01002500 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002501 key_data->x,
2502 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002503 TEST_ASSERT( psa_get_key_information( slot,
2504 NULL,
2505 &key_bits ) == PSA_SUCCESS );
2506
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002507 /* Allocate a buffer which has the size advertized by the
2508 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002509 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2510 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002511 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002512 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002513 signature = mbedtls_calloc( 1, signature_size );
2514 TEST_ASSERT( signature != NULL );
2515
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002516 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002517 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002518 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002519 signature, signature_size,
2520 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002521 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002522 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002523 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002524 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002525
2526exit:
2527 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002528 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002529 mbedtls_psa_crypto_free( );
2530}
2531/* END_CASE */
2532
2533/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002534void sign_fail( int key_type_arg, data_t *key_data,
2535 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002536 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002537{
2538 int slot = 1;
2539 psa_key_type_t key_type = key_type_arg;
2540 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002541 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002542 psa_status_t actual_status;
2543 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002544 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002545 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002546 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002547
Gilles Peskine20035e32018-02-03 22:44:14 +01002548 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002549 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002550 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2551 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2552
Gilles Peskine20035e32018-02-03 22:44:14 +01002553 signature = mbedtls_calloc( 1, signature_size );
2554 TEST_ASSERT( signature != NULL );
2555
2556 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2557
mohammad1603a97cb8c2018-03-28 03:46:26 -07002558 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002559 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002560 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2561
Gilles Peskine20035e32018-02-03 22:44:14 +01002562 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002563 key_data->x,
2564 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002565
2566 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002567 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002568 signature, signature_size,
2569 &signature_length );
2570 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002571 /* The value of *signature_length is unspecified on error, but
2572 * whatever it is, it should be less than signature_size, so that
2573 * if the caller tries to read *signature_length bytes without
2574 * checking the error code then they don't overflow a buffer. */
2575 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002576
2577exit:
2578 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002579 mbedtls_free( signature );
2580 mbedtls_psa_crypto_free( );
2581}
2582/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002583
2584/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002585void sign_verify( int key_type_arg, data_t *key_data,
2586 int alg_arg, data_t *input_data )
2587{
2588 int slot = 1;
2589 psa_key_type_t key_type = key_type_arg;
2590 psa_algorithm_t alg = alg_arg;
2591 size_t key_bits;
2592 unsigned char *signature = NULL;
2593 size_t signature_size;
2594 size_t signature_length = 0xdeadbeef;
2595 psa_key_policy_t policy;
2596
2597 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2598
2599 psa_key_policy_init( &policy );
2600 psa_key_policy_set_usage( &policy,
2601 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2602 alg );
2603 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2604
2605 TEST_ASSERT( psa_import_key( slot, key_type,
2606 key_data->x,
2607 key_data->len ) == PSA_SUCCESS );
2608 TEST_ASSERT( psa_get_key_information( slot,
2609 NULL,
2610 &key_bits ) == PSA_SUCCESS );
2611
2612 /* Allocate a buffer which has the size advertized by the
2613 * library. */
2614 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2615 key_bits, alg );
2616 TEST_ASSERT( signature_size != 0 );
2617 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2618 signature = mbedtls_calloc( 1, signature_size );
2619 TEST_ASSERT( signature != NULL );
2620
2621 /* Perform the signature. */
2622 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2623 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002624 signature, signature_size,
2625 &signature_length ) == PSA_SUCCESS );
2626 /* Check that the signature length looks sensible. */
2627 TEST_ASSERT( signature_length <= signature_size );
2628 TEST_ASSERT( signature_length > 0 );
2629
2630 /* Use the library to verify that the signature is correct. */
2631 TEST_ASSERT( psa_asymmetric_verify(
2632 slot, alg,
2633 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002634 signature, signature_length ) == PSA_SUCCESS );
2635
2636 if( input_data->len != 0 )
2637 {
2638 /* Flip a bit in the input and verify that the signature is now
2639 * detected as invalid. Flip a bit at the beginning, not at the end,
2640 * because ECDSA may ignore the last few bits of the input. */
2641 input_data->x[0] ^= 1;
2642 TEST_ASSERT( psa_asymmetric_verify(
2643 slot, alg,
2644 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002645 signature,
2646 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2647 }
2648
2649exit:
2650 psa_destroy_key( slot );
2651 mbedtls_free( signature );
2652 mbedtls_psa_crypto_free( );
2653}
2654/* END_CASE */
2655
2656/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002657void asymmetric_verify( int key_type_arg, data_t *key_data,
2658 int alg_arg, data_t *hash_data,
2659 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002660{
2661 int slot = 1;
2662 psa_key_type_t key_type = key_type_arg;
2663 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002664 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002665
Gilles Peskine69c12672018-06-28 00:07:19 +02002666 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2667
itayzafrir5c753392018-05-08 11:18:38 +03002668 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002669 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002670 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002671 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2672 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2673 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002674
2675 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2676
2677 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002678 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002679 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2680
2681 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002682 key_data->x,
2683 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002684
2685 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002686 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002687 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002688 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002689exit:
2690 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002691 mbedtls_psa_crypto_free( );
2692}
2693/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002694
2695/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002696void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2697 int alg_arg, data_t *hash_data,
2698 data_t *signature_data,
2699 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002700{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002701 int slot = 1;
2702 psa_key_type_t key_type = key_type_arg;
2703 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002704 psa_status_t actual_status;
2705 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002706 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002707
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002708 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002709 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002710 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002711 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2712 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2713 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002714
2715 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2716
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002717 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002718 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002719 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2720
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002721 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002722 key_data->x,
2723 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002724
2725 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002726 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002727 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002728 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002729
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002730 TEST_ASSERT( actual_status == expected_status );
2731
2732exit:
2733 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002734 mbedtls_psa_crypto_free( );
2735}
2736/* END_CASE */
2737
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002738/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002739void asymmetric_encrypt( int key_type_arg,
2740 data_t *key_data,
2741 int alg_arg,
2742 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002743 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002744 int expected_output_length_arg,
2745 int expected_status_arg )
2746{
2747 int slot = 1;
2748 psa_key_type_t key_type = key_type_arg;
2749 psa_algorithm_t alg = alg_arg;
2750 size_t expected_output_length = expected_output_length_arg;
2751 size_t key_bits;
2752 unsigned char *output = NULL;
2753 size_t output_size;
2754 size_t output_length = ~0;
2755 psa_status_t actual_status;
2756 psa_status_t expected_status = expected_status_arg;
2757 psa_key_policy_t policy;
2758
2759 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2760
2761 /* Import the key */
2762 psa_key_policy_init( &policy );
2763 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2764 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2765 TEST_ASSERT( psa_import_key( slot, key_type,
2766 key_data->x,
2767 key_data->len ) == PSA_SUCCESS );
2768
2769 /* Determine the maximum output length */
2770 TEST_ASSERT( psa_get_key_information( slot,
2771 NULL,
2772 &key_bits ) == PSA_SUCCESS );
2773 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2774 output = mbedtls_calloc( 1, output_size );
Darryl Green9c862252018-07-24 12:52:44 +01002775 TEST_ASSERT( output_size == 0 || output != NULL );
Gilles Peskine656896e2018-06-29 19:12:28 +02002776
2777 /* Encrypt the input */
2778 actual_status = psa_asymmetric_encrypt( slot, alg,
2779 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002780 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002781 output, output_size,
2782 &output_length );
2783 TEST_ASSERT( actual_status == expected_status );
2784 TEST_ASSERT( output_length == expected_output_length );
2785
Gilles Peskine68428122018-06-30 18:42:41 +02002786 /* If the label is empty, the test framework puts a non-null pointer
2787 * in label->x. Test that a null pointer works as well. */
2788 if( label->len == 0 )
2789 {
2790 output_length = ~0;
2791 memset( output, 0, output_size );
2792 actual_status = psa_asymmetric_encrypt( slot, alg,
2793 input_data->x, input_data->len,
2794 NULL, label->len,
2795 output, output_size,
2796 &output_length );
2797 TEST_ASSERT( actual_status == expected_status );
2798 TEST_ASSERT( output_length == expected_output_length );
2799 }
2800
Gilles Peskine656896e2018-06-29 19:12:28 +02002801exit:
2802 psa_destroy_key( slot );
2803 mbedtls_free( output );
2804 mbedtls_psa_crypto_free( );
2805}
2806/* END_CASE */
2807
2808/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002809void asymmetric_encrypt_decrypt( int key_type_arg,
2810 data_t *key_data,
2811 int alg_arg,
2812 data_t *input_data,
2813 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002814{
2815 int slot = 1;
2816 psa_key_type_t key_type = key_type_arg;
2817 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002818 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002819 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002820 size_t output_size;
2821 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002822 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002823 size_t output2_size;
2824 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002825 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002826
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002827 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002828 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002829 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2830 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2831
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002832 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2833
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002834 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002835 psa_key_policy_set_usage( &policy,
2836 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002837 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002838 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2839
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002840 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002841 key_data->x,
2842 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002843
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002844
2845 /* Determine the maximum ciphertext length */
2846 TEST_ASSERT( psa_get_key_information( slot,
2847 NULL,
2848 &key_bits ) == PSA_SUCCESS );
2849 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2850 output = mbedtls_calloc( 1, output_size );
2851 TEST_ASSERT( output != NULL );
2852 output2_size = input_data->len;
2853 output2 = mbedtls_calloc( 1, output2_size );
2854 TEST_ASSERT( output2 != NULL );
2855
Gilles Peskineeebd7382018-06-08 18:11:54 +02002856 /* We test encryption by checking that encrypt-then-decrypt gives back
2857 * the original plaintext because of the non-optional random
2858 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002859 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002860 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002861 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002862 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002863 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002864 /* We don't know what ciphertext length to expect, but check that
2865 * it looks sensible. */
2866 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002867
Gilles Peskine2d277862018-06-18 15:41:12 +02002868 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002869 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002870 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002871 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002872 &output2_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002873 TEST_ASSERT( output2_length == input_data->len );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002874 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002875 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002876
2877exit:
2878 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002879 mbedtls_free( output );
2880 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002881 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002882}
2883/* END_CASE */
2884
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002885/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002886void asymmetric_decrypt( int key_type_arg,
2887 data_t *key_data,
2888 int alg_arg,
2889 data_t *input_data,
2890 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002891 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002892{
2893 int slot = 1;
2894 psa_key_type_t key_type = key_type_arg;
2895 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002896 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002897 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002898 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002899 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002900
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002901 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002902 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002903 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002904 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2905 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2906 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2907
Gilles Peskine4abf7412018-06-18 16:35:34 +02002908 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002909 output = mbedtls_calloc( 1, output_size );
2910 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002911
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002912 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2913
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002914 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002915 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002916 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2917
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002918 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002919 key_data->x,
2920 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002921
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002922 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002923 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002924 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002925 output,
2926 output_size,
2927 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002928 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002929 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002930
Gilles Peskine68428122018-06-30 18:42:41 +02002931 /* If the label is empty, the test framework puts a non-null pointer
2932 * in label->x. Test that a null pointer works as well. */
2933 if( label->len == 0 )
2934 {
2935 output_length = ~0;
2936 memset( output, 0, output_size );
2937 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2938 input_data->x, input_data->len,
2939 NULL, label->len,
2940 output,
2941 output_size,
2942 &output_length ) == PSA_SUCCESS );
2943 TEST_ASSERT( expected_data->len == output_length );
2944 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
2945 }
2946
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002947exit:
2948 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002949 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002950 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002951}
2952/* END_CASE */
2953
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002954/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002955void asymmetric_decrypt_fail( int key_type_arg,
2956 data_t *key_data,
2957 int alg_arg,
2958 data_t *input_data,
2959 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002960 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002961{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002962 int slot = 1;
2963 psa_key_type_t key_type = key_type_arg;
2964 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002965 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002966 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002967 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002968 psa_status_t actual_status;
2969 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002970 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002971
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002972 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002973 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002974 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2975 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2976
Gilles Peskine4abf7412018-06-18 16:35:34 +02002977 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002978 output = mbedtls_calloc( 1, output_size );
2979 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002980
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002981 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2982
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002983 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002984 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002985 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2986
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002987 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002988 key_data->x,
2989 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002990
Gilles Peskine2d277862018-06-18 15:41:12 +02002991 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002992 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002993 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002994 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002995 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002996 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002997 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002998
Gilles Peskine68428122018-06-30 18:42:41 +02002999 /* If the label is empty, the test framework puts a non-null pointer
3000 * in label->x. Test that a null pointer works as well. */
3001 if( label->len == 0 )
3002 {
3003 output_length = ~0;
3004 memset( output, 0, output_size );
3005 actual_status = psa_asymmetric_decrypt( slot, alg,
3006 input_data->x, input_data->len,
3007 NULL, label->len,
3008 output, output_size,
3009 &output_length );
3010 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003011 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003012 }
3013
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003014exit:
3015 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003016 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003017 mbedtls_psa_crypto_free( );
3018}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003019/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003020
3021/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003022void derive_setup( int key_type_arg,
3023 data_t *key_data,
3024 int alg_arg,
3025 data_t *salt,
3026 data_t *label,
3027 int requested_capacity_arg,
3028 int expected_status_arg )
3029{
3030 psa_key_slot_t slot = 1;
3031 size_t key_type = key_type_arg;
3032 psa_algorithm_t alg = alg_arg;
3033 size_t requested_capacity = requested_capacity_arg;
3034 psa_status_t expected_status = expected_status_arg;
3035 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3036 psa_key_policy_t policy;
3037
3038 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3039
3040 psa_key_policy_init( &policy );
3041 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3042 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3043
3044 TEST_ASSERT( psa_import_key( slot, key_type,
3045 key_data->x,
3046 key_data->len ) == PSA_SUCCESS );
3047
3048 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3049 salt->x, salt->len,
3050 label->x, label->len,
3051 requested_capacity ) == expected_status );
3052
3053exit:
3054 psa_generator_abort( &generator );
3055 psa_destroy_key( slot );
3056 mbedtls_psa_crypto_free( );
3057}
3058/* END_CASE */
3059
3060/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003061void derive_output( int alg_arg,
3062 data_t *key_data,
3063 data_t *salt,
3064 data_t *label,
3065 int requested_capacity_arg,
3066 data_t *expected_output1,
3067 data_t *expected_output2 )
3068{
3069 psa_key_slot_t slot = 1;
3070 psa_algorithm_t alg = alg_arg;
3071 size_t requested_capacity = requested_capacity_arg;
3072 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3073 uint8_t *expected_outputs[2] =
3074 {expected_output1->x, expected_output2->x};
3075 size_t output_sizes[2] =
3076 {expected_output1->len, expected_output2->len};
3077 size_t output_buffer_size = 0;
3078 uint8_t *output_buffer = NULL;
3079 size_t expected_capacity;
3080 size_t current_capacity;
3081 psa_key_policy_t policy;
3082 psa_status_t status;
3083 unsigned i;
3084
3085 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3086 {
3087 if( output_sizes[i] > output_buffer_size )
3088 output_buffer_size = output_sizes[i];
3089 if( output_sizes[i] == 0 )
3090 expected_outputs[i] = NULL;
3091 }
3092 output_buffer = mbedtls_calloc( 1, output_buffer_size );
3093 TEST_ASSERT( output_buffer != NULL );
3094 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3095
3096 psa_key_policy_init( &policy );
3097 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3098 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3099
3100 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3101 key_data->x,
3102 key_data->len ) == PSA_SUCCESS );
3103
3104 /* Extraction phase. */
3105 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3106 salt->x, salt->len,
3107 label->x, label->len,
3108 requested_capacity ) == PSA_SUCCESS );
3109 TEST_ASSERT( psa_get_generator_capacity( &generator,
3110 &current_capacity ) ==
3111 PSA_SUCCESS );
3112 TEST_ASSERT( current_capacity == requested_capacity );
3113 expected_capacity = requested_capacity;
3114
3115 /* Expansion phase. */
3116 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3117 {
3118 /* Read some bytes. */
3119 status = psa_generator_read( &generator,
3120 output_buffer, output_sizes[i] );
3121 if( expected_capacity == 0 && output_sizes[i] == 0 )
3122 {
3123 /* Reading 0 bytes when 0 bytes are available can go either way. */
3124 TEST_ASSERT( status == PSA_SUCCESS ||
3125 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3126 continue;
3127 }
3128 else if( expected_capacity == 0 ||
3129 output_sizes[i] > expected_capacity )
3130 {
3131 /* Capacity exceeded. */
3132 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3133 expected_capacity = 0;
3134 continue;
3135 }
3136 /* Success. Check the read data. */
3137 TEST_ASSERT( status == PSA_SUCCESS );
3138 if( output_sizes[i] != 0 )
3139 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3140 output_sizes[i] ) == 0 );
3141 /* Check the generator status. */
3142 expected_capacity -= output_sizes[i];
3143 TEST_ASSERT( psa_get_generator_capacity( &generator,
3144 &current_capacity ) ==
3145 PSA_SUCCESS );
3146 TEST_ASSERT( expected_capacity == current_capacity );
3147 }
3148 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3149
3150exit:
3151 mbedtls_free( output_buffer );
3152 psa_generator_abort( &generator );
3153 psa_destroy_key( slot );
3154 mbedtls_psa_crypto_free( );
3155}
3156/* END_CASE */
3157
3158/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003159void derive_full( int alg_arg,
3160 data_t *key_data,
3161 data_t *salt,
3162 data_t *label,
3163 int requested_capacity_arg )
3164{
3165 psa_key_slot_t slot = 1;
3166 psa_algorithm_t alg = alg_arg;
3167 size_t requested_capacity = requested_capacity_arg;
3168 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3169 unsigned char output_buffer[16];
3170 size_t expected_capacity = requested_capacity;
3171 size_t current_capacity;
3172 psa_key_policy_t policy;
3173
3174 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3175
3176 psa_key_policy_init( &policy );
3177 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3178 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3179
3180 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3181 key_data->x,
3182 key_data->len ) == PSA_SUCCESS );
3183
3184 /* Extraction phase. */
3185 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3186 salt->x, salt->len,
3187 label->x, label->len,
3188 requested_capacity ) == PSA_SUCCESS );
3189 TEST_ASSERT( psa_get_generator_capacity( &generator,
3190 &current_capacity ) ==
3191 PSA_SUCCESS );
3192 TEST_ASSERT( current_capacity == expected_capacity );
3193
3194 /* Expansion phase. */
3195 while( current_capacity > 0 )
3196 {
3197 size_t read_size = sizeof( output_buffer );
3198 if( read_size > current_capacity )
3199 read_size = current_capacity;
3200 TEST_ASSERT( psa_generator_read( &generator,
3201 output_buffer,
3202 read_size ) == PSA_SUCCESS );
3203 expected_capacity -= read_size;
3204 TEST_ASSERT( psa_get_generator_capacity( &generator,
3205 &current_capacity ) ==
3206 PSA_SUCCESS );
3207 TEST_ASSERT( current_capacity == expected_capacity );
3208 }
3209
3210 /* Check that the generator refuses to go over capacity. */
3211 TEST_ASSERT( psa_generator_read( &generator,
3212 output_buffer,
3213 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3214
3215 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3216
3217exit:
3218 psa_generator_abort( &generator );
3219 psa_destroy_key( slot );
3220 mbedtls_psa_crypto_free( );
3221}
3222/* END_CASE */
3223
3224/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003225void derive_key_exercise( int alg_arg,
3226 data_t *key_data,
3227 data_t *salt,
3228 data_t *label,
3229 int derived_type_arg,
3230 int derived_bits_arg,
3231 int derived_usage_arg,
3232 int derived_alg_arg )
3233{
3234 psa_key_slot_t base_key = 1;
3235 psa_key_slot_t derived_key = 2;
3236 psa_algorithm_t alg = alg_arg;
3237 psa_key_type_t derived_type = derived_type_arg;
3238 size_t derived_bits = derived_bits_arg;
3239 psa_key_usage_t derived_usage = derived_usage_arg;
3240 psa_algorithm_t derived_alg = derived_alg_arg;
3241 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3242 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3243 psa_key_policy_t policy;
3244 psa_key_type_t got_type;
3245 size_t got_bits;
3246
3247 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3248
3249 psa_key_policy_init( &policy );
3250 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3251 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3252 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3253 key_data->x,
3254 key_data->len ) == PSA_SUCCESS );
3255
3256 /* Derive a key. */
3257 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3258 salt->x, salt->len,
3259 label->x, label->len,
3260 capacity ) == PSA_SUCCESS );
3261 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3262 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3263 TEST_ASSERT( psa_generator_import_key( derived_key,
3264 derived_type,
3265 derived_bits,
3266 &generator ) == PSA_SUCCESS );
3267
3268 /* Test the key information */
3269 TEST_ASSERT( psa_get_key_information( derived_key,
3270 &got_type,
3271 &got_bits ) == PSA_SUCCESS );
3272 TEST_ASSERT( got_type == derived_type );
3273 TEST_ASSERT( got_bits == derived_bits );
3274
3275 /* Exercise the derived key. */
3276 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3277 goto exit;
3278
3279exit:
3280 psa_generator_abort( &generator );
3281 psa_destroy_key( base_key );
3282 psa_destroy_key( derived_key );
3283 mbedtls_psa_crypto_free( );
3284}
3285/* END_CASE */
3286
3287/* BEGIN_CASE */
3288void derive_key_export( int alg_arg,
3289 data_t *key_data,
3290 data_t *salt,
3291 data_t *label,
3292 int bytes1_arg,
3293 int bytes2_arg )
3294{
3295 psa_key_slot_t base_key = 1;
3296 psa_key_slot_t derived_key = 2;
3297 psa_algorithm_t alg = alg_arg;
3298 size_t bytes1 = bytes1_arg;
3299 size_t bytes2 = bytes2_arg;
3300 size_t capacity = bytes1 + bytes2;
3301 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3302 uint8_t *output_buffer = mbedtls_calloc( 1, capacity );
3303 uint8_t *export_buffer = mbedtls_calloc( 1, capacity );
3304 psa_key_policy_t policy;
3305 size_t length;
3306
3307 TEST_ASSERT( output_buffer != NULL );
3308 TEST_ASSERT( export_buffer != NULL );
3309 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3310
3311 psa_key_policy_init( &policy );
3312 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3313 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3314 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3315 key_data->x,
3316 key_data->len ) == PSA_SUCCESS );
3317
3318 /* Derive some material and output it. */
3319 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3320 salt->x, salt->len,
3321 label->x, label->len,
3322 capacity ) == PSA_SUCCESS );
3323 TEST_ASSERT( psa_generator_read( &generator,
3324 output_buffer,
3325 capacity ) == PSA_SUCCESS );
3326 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3327
3328 /* Derive the same output again, but this time store it in key objects. */
3329 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3330 salt->x, salt->len,
3331 label->x, label->len,
3332 capacity ) == PSA_SUCCESS );
3333 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3334 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3335 TEST_ASSERT( psa_generator_import_key( derived_key,
3336 PSA_KEY_TYPE_RAW_DATA,
3337 PSA_BYTES_TO_BITS( bytes1 ),
3338 &generator ) == PSA_SUCCESS );
3339 TEST_ASSERT( psa_export_key( derived_key,
3340 export_buffer, bytes1,
3341 &length ) == PSA_SUCCESS );
3342 TEST_ASSERT( length == bytes1 );
3343 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3344 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3345 TEST_ASSERT( psa_generator_import_key( derived_key,
3346 PSA_KEY_TYPE_RAW_DATA,
3347 PSA_BYTES_TO_BITS( bytes2 ),
3348 &generator ) == PSA_SUCCESS );
3349 TEST_ASSERT( psa_export_key( derived_key,
3350 export_buffer + bytes1, bytes2,
3351 &length ) == PSA_SUCCESS );
3352 TEST_ASSERT( length == bytes2 );
3353
3354 /* Compare the outputs from the two runs. */
3355 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3356
3357exit:
3358 mbedtls_free( output_buffer );
3359 mbedtls_free( export_buffer );
3360 psa_generator_abort( &generator );
3361 psa_destroy_key( base_key );
3362 psa_destroy_key( derived_key );
3363 mbedtls_psa_crypto_free( );
3364}
3365/* END_CASE */
3366
3367/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003368void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003369{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003370 size_t bytes = bytes_arg;
3371 const unsigned char trail[] = "don't overwrite me";
3372 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
3373 unsigned char *changed = mbedtls_calloc( 1, bytes );
3374 size_t i;
3375 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003376
Gilles Peskinea50d7392018-06-21 10:22:13 +02003377 TEST_ASSERT( output != NULL );
Darryl Green9c862252018-07-24 12:52:44 +01003378 TEST_ASSERT( bytes == 0 || changed != NULL );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003379 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003380
3381 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3382
Gilles Peskinea50d7392018-06-21 10:22:13 +02003383 /* Run several times, to ensure that every output byte will be
3384 * nonzero at least once with overwhelming probability
3385 * (2^(-8*number_of_runs)). */
3386 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003387 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02003388 memset( output, 0, bytes );
3389 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3390
3391 /* Check that no more than bytes have been overwritten */
3392 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3393
3394 for( i = 0; i < bytes; i++ )
3395 {
3396 if( output[i] != 0 )
3397 ++changed[i];
3398 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003399 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003400
3401 /* Check that every byte was changed to nonzero at least once. This
3402 * validates that psa_generate_random is overwriting every byte of
3403 * the output buffer. */
3404 for( i = 0; i < bytes; i++ )
3405 {
3406 TEST_ASSERT( changed[i] != 0 );
3407 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003408
3409exit:
3410 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003411 mbedtls_free( output );
3412 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003413}
3414/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003415
3416/* BEGIN_CASE */
3417void generate_key( int type_arg,
3418 int bits_arg,
3419 int usage_arg,
3420 int alg_arg,
3421 int expected_status_arg )
3422{
3423 int slot = 1;
3424 psa_key_type_t type = type_arg;
3425 psa_key_usage_t usage = usage_arg;
3426 size_t bits = bits_arg;
3427 psa_algorithm_t alg = alg_arg;
3428 psa_status_t expected_status = expected_status_arg;
3429 psa_key_type_t got_type;
3430 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003431 psa_status_t expected_info_status =
3432 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3433 psa_key_policy_t policy;
3434
3435 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3436
3437 psa_key_policy_init( &policy );
3438 psa_key_policy_set_usage( &policy, usage, alg );
3439 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3440
3441 /* Generate a key */
3442 TEST_ASSERT( psa_generate_key( slot, type, bits,
3443 NULL, 0 ) == expected_status );
3444
3445 /* Test the key information */
3446 TEST_ASSERT( psa_get_key_information( slot,
3447 &got_type,
3448 &got_bits ) == expected_info_status );
3449 if( expected_info_status != PSA_SUCCESS )
3450 goto exit;
3451 TEST_ASSERT( got_type == type );
3452 TEST_ASSERT( got_bits == bits );
3453
Gilles Peskine818ca122018-06-20 18:16:48 +02003454 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003455 if( ! exercise_key( slot, usage, alg ) )
3456 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003457
3458exit:
3459 psa_destroy_key( slot );
3460 mbedtls_psa_crypto_free( );
3461}
3462/* END_CASE */