blob: b04f6a390bd6d10f47bb37fe6697c905f9b63674 [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
1004 if( canonical_input )
1005 {
Gilles Peskine4abf7412018-06-18 16:35:34 +02001006 TEST_ASSERT( exported_length == data->len );
1007 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008 }
1009 else
1010 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001011 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1012
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001013 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001014 exported,
1015 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001017 reexported,
1018 export_size,
1019 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020 TEST_ASSERT( reexported_length == exported_length );
1021 TEST_ASSERT( memcmp( reexported, exported,
1022 exported_length ) == 0 );
1023 }
1024
1025destroy:
1026 /* Destroy the key */
1027 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1028 TEST_ASSERT( psa_get_key_information(
1029 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1030
1031exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001032 mbedtls_free( exported );
1033 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 mbedtls_psa_crypto_free( );
1035}
1036/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001037
Moran Pekerf709f4a2018-06-06 17:26:04 +03001038/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001039void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001040 int type_arg,
1041 int alg_arg,
1042 int expected_bits,
1043 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001044 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001045{
1046 int slot = 1;
1047 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001048 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001049 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001050 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001051 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001052 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001053 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001054 psa_key_type_t got_type;
1055 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001056 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001057
Moran Pekerf709f4a2018-06-06 17:26:04 +03001058 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001059 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001060 export_size = (ptrdiff_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001061 exported = mbedtls_calloc( 1, export_size );
1062 TEST_ASSERT( exported != NULL );
1063
1064 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1065
1066 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001067 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001068 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1069
1070 /* Import the key */
1071 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001072 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001073
1074 /* Test the key information */
1075 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001076 &got_type,
1077 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001078 TEST_ASSERT( got_type == type );
1079 TEST_ASSERT( got_bits == (size_t) expected_bits );
1080
1081 /* Export the key */
1082 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001083 exported, export_size,
1084 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001085 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +01001086 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
1087 TEST_ASSERT( mem_is_zero( exported + exported_length,
1088 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001089 if( status != PSA_SUCCESS )
1090 goto destroy;
1091
Moran Pekerf709f4a2018-06-06 17:26:04 +03001092destroy:
1093 /* Destroy the key */
1094 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1095 TEST_ASSERT( psa_get_key_information(
1096 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1097
1098exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001099 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001100 mbedtls_psa_crypto_free( );
1101}
1102/* END_CASE */
1103
Gilles Peskine20035e32018-02-03 22:44:14 +01001104/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001105void import_and_exercise_key( data_t *data,
1106 int type_arg,
1107 int bits_arg,
1108 int alg_arg )
1109{
1110 int slot = 1;
1111 psa_key_type_t type = type_arg;
1112 size_t bits = bits_arg;
1113 psa_algorithm_t alg = alg_arg;
1114 psa_key_usage_t usage =
1115 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1116 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1117 PSA_KEY_USAGE_VERIFY :
1118 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1119 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1120 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1121 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1122 PSA_KEY_USAGE_ENCRYPT :
1123 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001124 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001125 0 );
1126 psa_key_policy_t policy;
1127 psa_key_type_t got_type;
1128 size_t got_bits;
1129 psa_status_t status;
1130
1131 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1132
1133 psa_key_policy_init( &policy );
1134 psa_key_policy_set_usage( &policy, usage, alg );
1135 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1136
1137 /* Import the key */
1138 status = psa_import_key( slot, type, data->x, data->len );
1139 TEST_ASSERT( status == PSA_SUCCESS );
1140
1141 /* Test the key information */
1142 TEST_ASSERT( psa_get_key_information( slot,
1143 &got_type,
1144 &got_bits ) == PSA_SUCCESS );
1145 TEST_ASSERT( got_type == type );
1146 TEST_ASSERT( got_bits == bits );
1147
1148 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001149 if( ! exercise_key( slot, usage, alg ) )
1150 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001151
1152exit:
1153 psa_destroy_key( slot );
1154 mbedtls_psa_crypto_free( );
1155}
1156/* END_CASE */
1157
1158/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001159void key_policy( int usage_arg, int alg_arg )
1160{
1161 int key_slot = 1;
1162 psa_algorithm_t alg = alg_arg;
1163 psa_key_usage_t usage = usage_arg;
1164 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1165 unsigned char key[32] = {0};
1166 psa_key_policy_t policy_set;
1167 psa_key_policy_t policy_get;
1168
1169 memset( key, 0x2a, sizeof( key ) );
1170
1171 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1172
1173 psa_key_policy_init( &policy_set );
1174 psa_key_policy_init( &policy_get );
1175
1176 psa_key_policy_set_usage( &policy_set, usage, alg );
1177
1178 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1179 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1180 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1181
1182 TEST_ASSERT( psa_import_key( key_slot, key_type,
1183 key, sizeof( key ) ) == PSA_SUCCESS );
1184
1185 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1186
1187 TEST_ASSERT( policy_get.usage == policy_set.usage );
1188 TEST_ASSERT( policy_get.alg == policy_set.alg );
1189
1190exit:
1191 psa_destroy_key( key_slot );
1192 mbedtls_psa_crypto_free( );
1193}
1194/* END_CASE */
1195
1196/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001197void mac_key_policy( int policy_usage,
1198 int policy_alg,
1199 int key_type,
1200 data_t *key_data,
1201 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001202{
1203 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001204 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001205 psa_mac_operation_t operation;
1206 psa_status_t status;
1207 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001208
1209 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1210
1211 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001212 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001213 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1214
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001215 TEST_ASSERT( psa_import_key( key_slot, key_type,
1216 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001217
Gilles Peskine89167cb2018-07-08 20:12:23 +02001218 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219 if( policy_alg == exercise_alg &&
1220 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1221 TEST_ASSERT( status == PSA_SUCCESS );
1222 else
1223 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1224 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001225
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001226 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001227 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001228 if( policy_alg == exercise_alg &&
1229 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001230 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001231 else
1232 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1233
1234exit:
1235 psa_mac_abort( &operation );
1236 psa_destroy_key( key_slot );
1237 mbedtls_psa_crypto_free( );
1238}
1239/* END_CASE */
1240
1241/* BEGIN_CASE */
1242void cipher_key_policy( int policy_usage,
1243 int policy_alg,
1244 int key_type,
1245 data_t *key_data,
1246 int exercise_alg )
1247{
1248 int key_slot = 1;
1249 psa_key_policy_t policy;
1250 psa_cipher_operation_t operation;
1251 psa_status_t status;
1252
1253 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1254
1255 psa_key_policy_init( &policy );
1256 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1257 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1258
1259 TEST_ASSERT( psa_import_key( key_slot, key_type,
1260 key_data->x, key_data->len ) == PSA_SUCCESS );
1261
Gilles Peskinefe119512018-07-08 21:39:34 +02001262 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001263 if( policy_alg == exercise_alg &&
1264 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1265 TEST_ASSERT( status == PSA_SUCCESS );
1266 else
1267 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1268 psa_cipher_abort( &operation );
1269
Gilles Peskinefe119512018-07-08 21:39:34 +02001270 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001271 if( policy_alg == exercise_alg &&
1272 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1273 TEST_ASSERT( status == PSA_SUCCESS );
1274 else
1275 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1276
1277exit:
1278 psa_cipher_abort( &operation );
1279 psa_destroy_key( key_slot );
1280 mbedtls_psa_crypto_free( );
1281}
1282/* END_CASE */
1283
1284/* BEGIN_CASE */
1285void aead_key_policy( int policy_usage,
1286 int policy_alg,
1287 int key_type,
1288 data_t *key_data,
1289 int nonce_length_arg,
1290 int tag_length_arg,
1291 int exercise_alg )
1292{
1293 int key_slot = 1;
1294 psa_key_policy_t policy;
1295 psa_status_t status;
1296 unsigned char nonce[16] = {0};
1297 size_t nonce_length = nonce_length_arg;
1298 unsigned char tag[16];
1299 size_t tag_length = tag_length_arg;
1300 size_t output_length;
1301
1302 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1303 TEST_ASSERT( tag_length <= sizeof( tag ) );
1304
1305 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1306
1307 psa_key_policy_init( &policy );
1308 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1309 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1310
1311 TEST_ASSERT( psa_import_key( key_slot, key_type,
1312 key_data->x, key_data->len ) == PSA_SUCCESS );
1313
1314 status = psa_aead_encrypt( key_slot, exercise_alg,
1315 nonce, nonce_length,
1316 NULL, 0,
1317 NULL, 0,
1318 tag, tag_length,
1319 &output_length );
1320 if( policy_alg == exercise_alg &&
1321 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1322 TEST_ASSERT( status == PSA_SUCCESS );
1323 else
1324 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1325
1326 memset( tag, 0, sizeof( tag ) );
1327 status = psa_aead_decrypt( key_slot, exercise_alg,
1328 nonce, nonce_length,
1329 NULL, 0,
1330 tag, tag_length,
1331 NULL, 0,
1332 &output_length );
1333 if( policy_alg == exercise_alg &&
1334 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1335 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1336 else
1337 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1338
1339exit:
1340 psa_destroy_key( key_slot );
1341 mbedtls_psa_crypto_free( );
1342}
1343/* END_CASE */
1344
1345/* BEGIN_CASE */
1346void asymmetric_encryption_key_policy( int policy_usage,
1347 int policy_alg,
1348 int key_type,
1349 data_t *key_data,
1350 int exercise_alg )
1351{
1352 int key_slot = 1;
1353 psa_key_policy_t policy;
1354 psa_status_t status;
1355 size_t key_bits;
1356 size_t buffer_length;
1357 unsigned char *buffer = NULL;
1358 size_t output_length;
1359
1360 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1361
1362 psa_key_policy_init( &policy );
1363 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1364 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1365
1366 TEST_ASSERT( psa_import_key( key_slot, key_type,
1367 key_data->x, key_data->len ) == PSA_SUCCESS );
1368
1369 TEST_ASSERT( psa_get_key_information( key_slot,
1370 NULL,
1371 &key_bits ) == PSA_SUCCESS );
1372 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1373 exercise_alg );
1374 buffer = mbedtls_calloc( 1, buffer_length );
1375 TEST_ASSERT( buffer != NULL );
1376
1377 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1378 NULL, 0,
1379 NULL, 0,
1380 buffer, buffer_length,
1381 &output_length );
1382 if( policy_alg == exercise_alg &&
1383 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1384 TEST_ASSERT( status == PSA_SUCCESS );
1385 else
1386 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1387
1388 memset( buffer, 0, buffer_length );
1389 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1390 buffer, buffer_length,
1391 NULL, 0,
1392 buffer, buffer_length,
1393 &output_length );
1394 if( policy_alg == exercise_alg &&
1395 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1396 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1397 else
1398 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1399
1400exit:
1401 psa_destroy_key( key_slot );
1402 mbedtls_psa_crypto_free( );
1403 mbedtls_free( buffer );
1404}
1405/* END_CASE */
1406
1407/* BEGIN_CASE */
1408void asymmetric_signature_key_policy( int policy_usage,
1409 int policy_alg,
1410 int key_type,
1411 data_t *key_data,
1412 int exercise_alg )
1413{
1414 int key_slot = 1;
1415 psa_key_policy_t policy;
1416 psa_status_t status;
1417 unsigned char payload[16] = {1};
1418 size_t payload_length = sizeof( payload );
1419 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1420 size_t signature_length;
1421
1422 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1423
1424 psa_key_policy_init( &policy );
1425 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1426 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1427
1428 TEST_ASSERT( psa_import_key( key_slot, key_type,
1429 key_data->x, key_data->len ) == PSA_SUCCESS );
1430
1431 status = psa_asymmetric_sign( key_slot, exercise_alg,
1432 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001433 signature, sizeof( signature ),
1434 &signature_length );
1435 if( policy_alg == exercise_alg &&
1436 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1437 TEST_ASSERT( status == PSA_SUCCESS );
1438 else
1439 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1440
1441 memset( signature, 0, sizeof( signature ) );
1442 status = psa_asymmetric_verify( key_slot, exercise_alg,
1443 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001444 signature, sizeof( signature ) );
1445 if( policy_alg == exercise_alg &&
1446 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1447 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1448 else
1449 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001450
1451exit:
1452 psa_destroy_key( key_slot );
1453 mbedtls_psa_crypto_free( );
1454}
1455/* END_CASE */
1456
1457/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001458void derive_key_policy( int policy_usage,
1459 int policy_alg,
1460 int key_type,
1461 data_t *key_data,
1462 int exercise_alg )
1463{
1464 int key_slot = 1;
1465 psa_key_policy_t policy;
1466 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1467 psa_status_t status;
1468
1469 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1470
1471 psa_key_policy_init( &policy );
1472 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1473 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1474
1475 TEST_ASSERT( psa_import_key( key_slot, key_type,
1476 key_data->x, key_data->len ) == PSA_SUCCESS );
1477
1478 status = psa_key_derivation( &generator, key_slot,
1479 exercise_alg,
1480 NULL, 0,
1481 NULL, 0,
1482 1 );
1483 if( policy_alg == exercise_alg &&
1484 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1485 TEST_ASSERT( status == PSA_SUCCESS );
1486 else
1487 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1488
1489exit:
1490 psa_generator_abort( &generator );
1491 psa_destroy_key( key_slot );
1492 mbedtls_psa_crypto_free( );
1493}
1494/* END_CASE */
1495
1496/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001497void key_lifetime( int lifetime_arg )
1498{
1499 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001500 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001501 unsigned char key[32] = {0};
1502 psa_key_lifetime_t lifetime_set = lifetime_arg;
1503 psa_key_lifetime_t lifetime_get;
1504
1505 memset( key, 0x2a, sizeof( key ) );
1506
1507 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1508
1509 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1510 lifetime_set ) == PSA_SUCCESS );
1511
1512 TEST_ASSERT( psa_import_key( key_slot, key_type,
1513 key, sizeof( key ) ) == PSA_SUCCESS );
1514
1515 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1516 &lifetime_get ) == PSA_SUCCESS );
1517
1518 TEST_ASSERT( lifetime_get == lifetime_set );
1519
1520exit:
1521 psa_destroy_key( key_slot );
1522 mbedtls_psa_crypto_free( );
1523}
1524/* END_CASE */
1525
1526/* BEGIN_CASE */
1527void key_lifetime_set_fail( int key_slot_arg,
1528 int lifetime_arg,
1529 int expected_status_arg )
1530{
1531 psa_key_slot_t key_slot = key_slot_arg;
1532 psa_key_lifetime_t lifetime_set = lifetime_arg;
1533 psa_status_t actual_status;
1534 psa_status_t expected_status = expected_status_arg;
1535
1536 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1537
1538 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1539
1540 if( actual_status == PSA_SUCCESS )
1541 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1542
1543 TEST_ASSERT( expected_status == actual_status );
1544
1545exit:
1546 psa_destroy_key( key_slot );
1547 mbedtls_psa_crypto_free( );
1548}
1549/* END_CASE */
1550
1551/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001552void hash_setup( int alg_arg,
1553 int expected_status_arg )
1554{
1555 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001556 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001557 psa_hash_operation_t operation;
1558 psa_status_t status;
1559
1560 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1561
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001562 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001563 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001564 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001565
1566exit:
1567 mbedtls_psa_crypto_free( );
1568}
1569/* END_CASE */
1570
1571/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001572void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001573{
1574 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001575 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001576 size_t actual_hash_length;
1577 psa_hash_operation_t operation;
1578
Gilles Peskine69c12672018-06-28 00:07:19 +02001579 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1580 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1581
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001582 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001583 TEST_ASSERT( expected_hash != NULL );
1584 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1585 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001586
1587 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1588
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001589 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001590 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001591 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001592 TEST_ASSERT( psa_hash_finish( &operation,
1593 actual_hash, sizeof( actual_hash ),
1594 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001595 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001596 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001597 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001598
1599exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001600 mbedtls_psa_crypto_free( );
1601}
1602/* END_CASE */
1603
1604/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001605void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001606{
1607 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001608 psa_hash_operation_t operation;
1609
Gilles Peskine69c12672018-06-28 00:07:19 +02001610 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1611 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1612
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001613 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001614 TEST_ASSERT( expected_hash != NULL );
1615 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1616 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001617
1618 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1619
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001620 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001621 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001622 input->x,
1623 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001624 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001625 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001626 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001627
1628exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001629 mbedtls_psa_crypto_free( );
1630}
1631/* END_CASE */
1632
1633/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001634void mac_setup( int key_type_arg,
1635 data_t *key,
1636 int alg_arg,
1637 int expected_status_arg )
1638{
1639 int key_slot = 1;
1640 psa_key_type_t key_type = key_type_arg;
1641 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001642 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001643 psa_mac_operation_t operation;
1644 psa_key_policy_t policy;
1645 psa_status_t status;
1646
1647 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1648
1649 psa_key_policy_init( &policy );
1650 psa_key_policy_set_usage( &policy,
1651 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1652 alg );
1653 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1654
1655 TEST_ASSERT( psa_import_key( key_slot, key_type,
1656 key->x, key->len ) == PSA_SUCCESS );
1657
Gilles Peskine89167cb2018-07-08 20:12:23 +02001658 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001659 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001660 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001661
1662exit:
1663 psa_destroy_key( key_slot );
1664 mbedtls_psa_crypto_free( );
1665}
1666/* END_CASE */
1667
1668/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001669void mac_verify( int key_type_arg,
1670 data_t *key,
1671 int alg_arg,
1672 data_t *input,
1673 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001674{
1675 int key_slot = 1;
1676 psa_key_type_t key_type = key_type_arg;
1677 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001679 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001680
Gilles Peskine69c12672018-06-28 00:07:19 +02001681 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1682
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001684 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001685 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001686 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001687 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1688 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001689
1690 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1691
mohammad16036df908f2018-04-02 08:34:15 -07001692 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001693 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001694 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1695
Gilles Peskine8c9def32018-02-08 10:02:12 +01001696 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001697 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001698
Gilles Peskine89167cb2018-07-08 20:12:23 +02001699 TEST_ASSERT( psa_mac_verify_setup( &operation,
1700 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001701 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1702 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001703 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001704 TEST_ASSERT( psa_mac_verify_finish( &operation,
1705 expected_mac->x,
1706 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001707
1708exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001709 psa_destroy_key( key_slot );
1710 mbedtls_psa_crypto_free( );
1711}
1712/* END_CASE */
1713
1714/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001715void cipher_setup( int key_type_arg,
1716 data_t *key,
1717 int alg_arg,
1718 int expected_status_arg )
1719{
1720 int key_slot = 1;
1721 psa_key_type_t key_type = key_type_arg;
1722 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001723 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001724 psa_cipher_operation_t operation;
1725 psa_key_policy_t policy;
1726 psa_status_t status;
1727
1728 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1729
1730 psa_key_policy_init( &policy );
1731 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1732 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1733
1734 TEST_ASSERT( psa_import_key( key_slot, key_type,
1735 key->x, key->len ) == PSA_SUCCESS );
1736
Gilles Peskinefe119512018-07-08 21:39:34 +02001737 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001738 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001739 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001740
1741exit:
1742 psa_destroy_key( key_slot );
1743 mbedtls_psa_crypto_free( );
1744}
1745/* END_CASE */
1746
1747/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001748void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001749 data_t *key,
1750 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001751 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001752{
1753 int key_slot = 1;
1754 psa_status_t status;
1755 psa_key_type_t key_type = key_type_arg;
1756 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001757 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001758 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001759 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001760 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001761 size_t output_buffer_size = 0;
1762 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001763 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001764 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001765 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001766
Gilles Peskine50e586b2018-06-08 14:28:46 +02001767 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001768 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001769 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001770 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1771 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1772 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001773
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001774 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1775 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001776
1777 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1778
Moran Pekered346952018-07-05 15:22:45 +03001779 psa_key_policy_init( &policy );
1780 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1781 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1782
Gilles Peskine50e586b2018-06-08 14:28:46 +02001783 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001784 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001785
Gilles Peskinefe119512018-07-08 21:39:34 +02001786 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1787 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001788
Gilles Peskinefe119512018-07-08 21:39:34 +02001789 TEST_ASSERT( psa_cipher_set_iv( &operation,
1790 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001791 output_buffer_size = (size_t) input->len +
1792 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001793 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001794 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001795
Gilles Peskine4abf7412018-06-18 16:35:34 +02001796 TEST_ASSERT( psa_cipher_update( &operation,
1797 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001798 output, output_buffer_size,
1799 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001800 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001801 status = psa_cipher_finish( &operation,
1802 output + function_output_length,
1803 output_buffer_size,
1804 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001805 total_output_length += function_output_length;
1806
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001807 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001808 if( expected_status == PSA_SUCCESS )
1809 {
1810 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001811 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001812 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001813 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001814 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001815
Gilles Peskine50e586b2018-06-08 14:28:46 +02001816exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001817 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001818 psa_destroy_key( key_slot );
1819 mbedtls_psa_crypto_free( );
1820}
1821/* END_CASE */
1822
1823/* BEGIN_CASE */
1824void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001825 data_t *key,
1826 data_t *input,
1827 int first_part_size,
1828 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001829{
1830 int key_slot = 1;
1831 psa_key_type_t key_type = key_type_arg;
1832 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001833 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001834 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001835 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001836 size_t output_buffer_size = 0;
1837 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001838 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001839 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001840 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001841
Gilles Peskine50e586b2018-06-08 14:28:46 +02001842 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001843 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001844 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001845 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1846 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1847 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001848
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001849 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1850 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001851
1852 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1853
Moran Pekered346952018-07-05 15:22:45 +03001854 psa_key_policy_init( &policy );
1855 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1856 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1857
Gilles Peskine50e586b2018-06-08 14:28:46 +02001858 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001859 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001860
Gilles Peskinefe119512018-07-08 21:39:34 +02001861 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1862 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001863
Gilles Peskinefe119512018-07-08 21:39:34 +02001864 TEST_ASSERT( psa_cipher_set_iv( &operation,
1865 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001866 output_buffer_size = (size_t) input->len +
1867 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001868 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001869 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001870
Gilles Peskine4abf7412018-06-18 16:35:34 +02001871 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001872 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001873 output, output_buffer_size,
1874 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001875 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001876 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001877 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001878 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001879 output, output_buffer_size,
1880 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001881 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001882 TEST_ASSERT( psa_cipher_finish( &operation,
1883 output + function_output_length,
1884 output_buffer_size,
1885 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001886 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001887 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1888
Gilles Peskine4abf7412018-06-18 16:35:34 +02001889 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001890 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001891 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001892
1893exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001894 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001895 psa_destroy_key( key_slot );
1896 mbedtls_psa_crypto_free( );
1897}
1898/* END_CASE */
1899
1900/* BEGIN_CASE */
1901void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001902 data_t *key,
1903 data_t *input,
1904 int first_part_size,
1905 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001906{
1907 int key_slot = 1;
1908
1909 psa_key_type_t key_type = key_type_arg;
1910 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001911 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001912 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001913 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001914 size_t output_buffer_size = 0;
1915 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001916 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001917 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001918 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001919
Gilles Peskine50e586b2018-06-08 14:28:46 +02001920 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001921 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001922 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001923 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1924 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1925 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001926
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001927 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1928 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001929
1930 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1931
Moran Pekered346952018-07-05 15:22:45 +03001932 psa_key_policy_init( &policy );
1933 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1934 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1935
Gilles Peskine50e586b2018-06-08 14:28:46 +02001936 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001937 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001938
Gilles Peskinefe119512018-07-08 21:39:34 +02001939 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1940 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001941
Gilles Peskinefe119512018-07-08 21:39:34 +02001942 TEST_ASSERT( psa_cipher_set_iv( &operation,
1943 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001944
mohammad16033d91abe2018-07-03 13:15:54 +03001945 output_buffer_size = (size_t) input->len +
1946 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001947 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001948 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001949
Gilles Peskine4abf7412018-06-18 16:35:34 +02001950 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1951 TEST_ASSERT( psa_cipher_update( &operation,
1952 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001953 output, output_buffer_size,
1954 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001955 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001956 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001957 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001958 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001959 output, output_buffer_size,
1960 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001961 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001962 TEST_ASSERT( psa_cipher_finish( &operation,
1963 output + function_output_length,
1964 output_buffer_size,
1965 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001966 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001967 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1968
Gilles Peskine4abf7412018-06-18 16:35:34 +02001969 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001970 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001971 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001972
1973exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001974 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001975 psa_destroy_key( key_slot );
1976 mbedtls_psa_crypto_free( );
1977}
1978/* END_CASE */
1979
Gilles Peskine50e586b2018-06-08 14:28:46 +02001980/* BEGIN_CASE */
1981void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001982 data_t *key,
1983 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001984 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001985{
1986 int key_slot = 1;
1987 psa_status_t status;
1988 psa_key_type_t key_type = key_type_arg;
1989 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001990 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001991 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001992 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001993 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001994 size_t output_buffer_size = 0;
1995 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001996 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001997 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001998 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001999
Gilles Peskine50e586b2018-06-08 14:28:46 +02002000 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002001 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002002 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002003 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2004 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2005 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002006
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002007 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2008 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002009
2010 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2011
Moran Pekered346952018-07-05 15:22:45 +03002012 psa_key_policy_init( &policy );
2013 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2014 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2015
Gilles Peskine50e586b2018-06-08 14:28:46 +02002016 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002017 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002018
Gilles Peskinefe119512018-07-08 21:39:34 +02002019 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2020 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002021
Gilles Peskinefe119512018-07-08 21:39:34 +02002022 TEST_ASSERT( psa_cipher_set_iv( &operation,
2023 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002024
mohammad16033d91abe2018-07-03 13:15:54 +03002025 output_buffer_size = (size_t) input->len +
2026 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002027 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002028 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002029
Gilles Peskine4abf7412018-06-18 16:35:34 +02002030 TEST_ASSERT( psa_cipher_update( &operation,
2031 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002032 output, output_buffer_size,
2033 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002034 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002035 status = psa_cipher_finish( &operation,
2036 output + function_output_length,
2037 output_buffer_size,
2038 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002039 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002040 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002041
2042 if( expected_status == PSA_SUCCESS )
2043 {
2044 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002045 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002046 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002047 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002048 }
2049
Gilles Peskine50e586b2018-06-08 14:28:46 +02002050exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002051 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002052 psa_destroy_key( key_slot );
2053 mbedtls_psa_crypto_free( );
2054}
2055/* END_CASE */
2056
Gilles Peskine50e586b2018-06-08 14:28:46 +02002057/* BEGIN_CASE */
2058void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002059 data_t *key,
2060 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002061{
2062 int key_slot = 1;
2063 psa_key_type_t key_type = key_type_arg;
2064 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002065 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002066 size_t iv_size = 16;
2067 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002068 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002069 size_t output1_size = 0;
2070 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002071 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002072 size_t output2_size = 0;
2073 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002074 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002075 psa_cipher_operation_t operation1;
2076 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002077 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002078
mohammad1603d7d7ba52018-03-12 18:51:53 +02002079 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002080 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002081 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2082 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002083
mohammad1603d7d7ba52018-03-12 18:51:53 +02002084 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2085
Moran Pekered346952018-07-05 15:22:45 +03002086 psa_key_policy_init( &policy );
2087 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2088 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2089
mohammad1603d7d7ba52018-03-12 18:51:53 +02002090 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002091 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002092
Gilles Peskinefe119512018-07-08 21:39:34 +02002093 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2094 key_slot, alg ) == PSA_SUCCESS );
2095 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2096 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002097
Gilles Peskinefe119512018-07-08 21:39:34 +02002098 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2099 iv, iv_size,
2100 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002101 output1_size = (size_t) input->len +
2102 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002103 output1 = mbedtls_calloc( 1, output1_size );
2104 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002105
Gilles Peskine4abf7412018-06-18 16:35:34 +02002106 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002107 output1, output1_size,
2108 &output1_length ) == PSA_SUCCESS );
2109 TEST_ASSERT( psa_cipher_finish( &operation1,
2110 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002111 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002112
Gilles Peskine048b7f02018-06-08 14:20:49 +02002113 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002114
2115 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2116
2117 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002118 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002119 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002120
Gilles Peskinefe119512018-07-08 21:39:34 +02002121 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2122 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002123 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2124 output2, output2_size,
2125 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002126 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002127 TEST_ASSERT( psa_cipher_finish( &operation2,
2128 output2 + output2_length,
2129 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002130 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002131
Gilles Peskine048b7f02018-06-08 14:20:49 +02002132 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002133
Janos Follath25c4fa82018-07-06 16:23:25 +01002134 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002135
Gilles Peskine4abf7412018-06-18 16:35:34 +02002136 TEST_ASSERT( input->len == output2_length );
2137 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03002138
2139exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002140 mbedtls_free( output1 );
2141 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002142 psa_destroy_key( key_slot );
2143 mbedtls_psa_crypto_free( );
2144}
2145/* END_CASE */
2146
2147/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002148void cipher_verify_output_multipart( int alg_arg,
2149 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002150 data_t *key,
2151 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002152 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002153{
2154 int key_slot = 1;
2155 psa_key_type_t key_type = key_type_arg;
2156 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002157 unsigned char iv[16] = {0};
2158 size_t iv_size = 16;
2159 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002160 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002161 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002162 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002163 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002164 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002165 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002166 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002167 psa_cipher_operation_t operation1;
2168 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002169 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002170
Moran Pekerded84402018-06-06 16:36:50 +03002171 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002172 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002173 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2174 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002175
Moran Pekerded84402018-06-06 16:36:50 +03002176 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2177
Moran Pekered346952018-07-05 15:22:45 +03002178 psa_key_policy_init( &policy );
2179 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2180 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2181
Moran Pekerded84402018-06-06 16:36:50 +03002182 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002183 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002184
Gilles Peskinefe119512018-07-08 21:39:34 +02002185 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2186 key_slot, alg ) == PSA_SUCCESS );
2187 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2188 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002189
Gilles Peskinefe119512018-07-08 21:39:34 +02002190 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2191 iv, iv_size,
2192 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002193 output1_buffer_size = (size_t) input->len +
2194 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002195 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002196 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002197
Gilles Peskine4abf7412018-06-18 16:35:34 +02002198 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002199
itayzafrir3e02b3b2018-06-12 17:06:52 +03002200 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002201 output1, output1_buffer_size,
2202 &function_output_length ) == PSA_SUCCESS );
2203 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002204
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002205 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002206 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002207 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002208 output1, output1_buffer_size,
2209 &function_output_length ) == PSA_SUCCESS );
2210 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002211
Gilles Peskine048b7f02018-06-08 14:20:49 +02002212 TEST_ASSERT( psa_cipher_finish( &operation1,
2213 output1 + output1_length,
2214 output1_buffer_size - output1_length,
2215 &function_output_length ) == PSA_SUCCESS );
2216 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002217
2218 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2219
Gilles Peskine048b7f02018-06-08 14:20:49 +02002220 output2_buffer_size = output1_length;
2221 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002222 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002223
Gilles Peskinefe119512018-07-08 21:39:34 +02002224 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2225 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002226
2227 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002228 output2, output2_buffer_size,
2229 &function_output_length ) == PSA_SUCCESS );
2230 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002231
Gilles Peskine048b7f02018-06-08 14:20:49 +02002232 TEST_ASSERT( psa_cipher_update( &operation2,
2233 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002234 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002235 output2, output2_buffer_size,
2236 &function_output_length ) == PSA_SUCCESS );
2237 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002238
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002239 TEST_ASSERT( psa_cipher_finish( &operation2,
2240 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002241 output2_buffer_size - output2_length,
2242 &function_output_length ) == PSA_SUCCESS );
2243 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002244
Janos Follath25c4fa82018-07-06 16:23:25 +01002245 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002246
Gilles Peskine4abf7412018-06-18 16:35:34 +02002247 TEST_ASSERT( input->len == output2_length );
2248 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002249
2250exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002251 mbedtls_free( output1 );
2252 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002253 psa_destroy_key( key_slot );
2254 mbedtls_psa_crypto_free( );
2255}
2256/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002257
Gilles Peskine20035e32018-02-03 22:44:14 +01002258/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002259void aead_encrypt_decrypt( int key_type_arg,
2260 data_t * key_data,
2261 int alg_arg,
2262 data_t * input_data,
2263 data_t * nonce,
2264 data_t * additional_data,
2265 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002266{
2267 int slot = 1;
2268 psa_key_type_t key_type = key_type_arg;
2269 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002270 unsigned char *output_data = NULL;
2271 size_t output_size = 0;
2272 size_t output_length = 0;
2273 unsigned char *output_data2 = NULL;
2274 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002275 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002276 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002277 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002278
Gilles Peskinea1cac842018-06-11 19:33:02 +02002279 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002280 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002281 TEST_ASSERT( nonce != NULL );
2282 TEST_ASSERT( additional_data != NULL );
2283 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2284 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2285 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2286 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2287
Gilles Peskine4abf7412018-06-18 16:35:34 +02002288 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002289 output_data = mbedtls_calloc( 1, output_size );
2290 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002291
2292 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2293
2294 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002295 psa_key_policy_set_usage( &policy,
2296 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2297 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002298 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2299
2300 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002301 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002302
2303 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002304 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002305 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002306 additional_data->len,
2307 input_data->x, input_data->len,
2308 output_data, output_size,
2309 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002310
2311 if( PSA_SUCCESS == expected_result )
2312 {
2313 output_data2 = mbedtls_calloc( 1, output_length );
2314 TEST_ASSERT( output_data2 != NULL );
2315
2316 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002317 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002318 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002319 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002320 output_data, output_length,
2321 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002322 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002323
itayzafrir3e02b3b2018-06-12 17:06:52 +03002324 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002325 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002326 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002327
Gilles Peskinea1cac842018-06-11 19:33:02 +02002328exit:
2329 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002330 mbedtls_free( output_data );
2331 mbedtls_free( output_data2 );
2332 mbedtls_psa_crypto_free( );
2333}
2334/* END_CASE */
2335
2336/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002337void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002338 int alg_arg, data_t * input_data,
2339 data_t * additional_data, data_t * nonce,
2340 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002341{
2342 int slot = 1;
2343 psa_key_type_t key_type = key_type_arg;
2344 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002345 unsigned char *output_data = NULL;
2346 size_t output_size = 0;
2347 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002348 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002349 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002350
Gilles Peskinea1cac842018-06-11 19:33:02 +02002351 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002352 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002353 TEST_ASSERT( additional_data != NULL );
2354 TEST_ASSERT( nonce != NULL );
2355 TEST_ASSERT( expected_result != NULL );
2356 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2357 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2358 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2359 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2360 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2361
Gilles Peskine4abf7412018-06-18 16:35:34 +02002362 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002363 output_data = mbedtls_calloc( 1, output_size );
2364 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002365
Gilles Peskinea1cac842018-06-11 19:33:02 +02002366 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2367
2368 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002369 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002370 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2371
2372 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002373 key_data->x,
2374 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002375
2376 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002377 nonce->x, nonce->len,
2378 additional_data->x, additional_data->len,
2379 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002380 output_data, output_size,
2381 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002382
itayzafrir3e02b3b2018-06-12 17:06:52 +03002383 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002384 output_length ) == 0 );
2385
Gilles Peskinea1cac842018-06-11 19:33:02 +02002386exit:
2387 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002388 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002389 mbedtls_psa_crypto_free( );
2390}
2391/* END_CASE */
2392
2393/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002394void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002395 int alg_arg, data_t * input_data,
2396 data_t * additional_data, data_t * nonce,
2397 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002398{
2399 int slot = 1;
2400 psa_key_type_t key_type = key_type_arg;
2401 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002402 unsigned char *output_data = NULL;
2403 size_t output_size = 0;
2404 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002405 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002406 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002407 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002408
Gilles Peskinea1cac842018-06-11 19:33:02 +02002409 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002410 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002411 TEST_ASSERT( additional_data != NULL );
2412 TEST_ASSERT( nonce != NULL );
2413 TEST_ASSERT( expected_data != NULL );
2414 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2415 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2416 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2417 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2418 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2419
Gilles Peskine4abf7412018-06-18 16:35:34 +02002420 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002421 output_data = mbedtls_calloc( 1, output_size );
2422 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002423
Gilles Peskinea1cac842018-06-11 19:33:02 +02002424 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2425
2426 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002427 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002428 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2429
2430 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002431 key_data->x,
2432 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002433
2434 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002435 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002436 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002437 additional_data->len,
2438 input_data->x, input_data->len,
2439 output_data, output_size,
2440 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002441
Gilles Peskine2d277862018-06-18 15:41:12 +02002442 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002443 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002444 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002445 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002446 }
2447
Gilles Peskinea1cac842018-06-11 19:33:02 +02002448exit:
2449 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002450 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002451 mbedtls_psa_crypto_free( );
2452}
2453/* END_CASE */
2454
2455/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002456void signature_size( int type_arg,
2457 int bits,
2458 int alg_arg,
2459 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002460{
2461 psa_key_type_t type = type_arg;
2462 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002463 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002464 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2465exit:
2466 ;
2467}
2468/* END_CASE */
2469
2470/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002471void sign_deterministic( int key_type_arg, data_t *key_data,
2472 int alg_arg, data_t *input_data,
2473 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002474{
2475 int slot = 1;
2476 psa_key_type_t key_type = key_type_arg;
2477 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002478 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002479 unsigned char *signature = NULL;
2480 size_t signature_size;
2481 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002482 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002483
Gilles Peskine20035e32018-02-03 22:44:14 +01002484 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002485 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002486 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002487 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2488 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2489 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002490
2491 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2492
mohammad1603a97cb8c2018-03-28 03:46:26 -07002493 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002494 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002495 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2496
Gilles Peskine20035e32018-02-03 22:44:14 +01002497 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002498 key_data->x,
2499 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002500 TEST_ASSERT( psa_get_key_information( slot,
2501 NULL,
2502 &key_bits ) == PSA_SUCCESS );
2503
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002504 /* Allocate a buffer which has the size advertized by the
2505 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002506 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2507 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002508 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002509 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002510 signature = mbedtls_calloc( 1, signature_size );
2511 TEST_ASSERT( signature != NULL );
2512
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002513 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002514 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002515 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002516 signature, signature_size,
2517 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002518 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002519 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002520 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002521 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002522
2523exit:
2524 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002525 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002526 mbedtls_psa_crypto_free( );
2527}
2528/* END_CASE */
2529
2530/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002531void sign_fail( int key_type_arg, data_t *key_data,
2532 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002533 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002534{
2535 int slot = 1;
2536 psa_key_type_t key_type = key_type_arg;
2537 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002538 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002539 psa_status_t actual_status;
2540 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002541 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002542 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002543 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002544
Gilles Peskine20035e32018-02-03 22:44:14 +01002545 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002546 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002547 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2548 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2549
Gilles Peskine20035e32018-02-03 22:44:14 +01002550 signature = mbedtls_calloc( 1, signature_size );
2551 TEST_ASSERT( signature != NULL );
2552
2553 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2554
mohammad1603a97cb8c2018-03-28 03:46:26 -07002555 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002556 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002557 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2558
Gilles Peskine20035e32018-02-03 22:44:14 +01002559 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002560 key_data->x,
2561 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002562
2563 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002564 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002565 signature, signature_size,
2566 &signature_length );
2567 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002568 /* The value of *signature_length is unspecified on error, but
2569 * whatever it is, it should be less than signature_size, so that
2570 * if the caller tries to read *signature_length bytes without
2571 * checking the error code then they don't overflow a buffer. */
2572 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002573
2574exit:
2575 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002576 mbedtls_free( signature );
2577 mbedtls_psa_crypto_free( );
2578}
2579/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002580
2581/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002582void sign_verify( int key_type_arg, data_t *key_data,
2583 int alg_arg, data_t *input_data )
2584{
2585 int slot = 1;
2586 psa_key_type_t key_type = key_type_arg;
2587 psa_algorithm_t alg = alg_arg;
2588 size_t key_bits;
2589 unsigned char *signature = NULL;
2590 size_t signature_size;
2591 size_t signature_length = 0xdeadbeef;
2592 psa_key_policy_t policy;
2593
2594 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2595
2596 psa_key_policy_init( &policy );
2597 psa_key_policy_set_usage( &policy,
2598 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2599 alg );
2600 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2601
2602 TEST_ASSERT( psa_import_key( slot, key_type,
2603 key_data->x,
2604 key_data->len ) == PSA_SUCCESS );
2605 TEST_ASSERT( psa_get_key_information( slot,
2606 NULL,
2607 &key_bits ) == PSA_SUCCESS );
2608
2609 /* Allocate a buffer which has the size advertized by the
2610 * library. */
2611 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2612 key_bits, alg );
2613 TEST_ASSERT( signature_size != 0 );
2614 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2615 signature = mbedtls_calloc( 1, signature_size );
2616 TEST_ASSERT( signature != NULL );
2617
2618 /* Perform the signature. */
2619 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2620 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002621 signature, signature_size,
2622 &signature_length ) == PSA_SUCCESS );
2623 /* Check that the signature length looks sensible. */
2624 TEST_ASSERT( signature_length <= signature_size );
2625 TEST_ASSERT( signature_length > 0 );
2626
2627 /* Use the library to verify that the signature is correct. */
2628 TEST_ASSERT( psa_asymmetric_verify(
2629 slot, alg,
2630 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002631 signature, signature_length ) == PSA_SUCCESS );
2632
2633 if( input_data->len != 0 )
2634 {
2635 /* Flip a bit in the input and verify that the signature is now
2636 * detected as invalid. Flip a bit at the beginning, not at the end,
2637 * because ECDSA may ignore the last few bits of the input. */
2638 input_data->x[0] ^= 1;
2639 TEST_ASSERT( psa_asymmetric_verify(
2640 slot, alg,
2641 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002642 signature,
2643 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2644 }
2645
2646exit:
2647 psa_destroy_key( slot );
2648 mbedtls_free( signature );
2649 mbedtls_psa_crypto_free( );
2650}
2651/* END_CASE */
2652
2653/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002654void asymmetric_verify( int key_type_arg, data_t *key_data,
2655 int alg_arg, data_t *hash_data,
2656 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002657{
2658 int slot = 1;
2659 psa_key_type_t key_type = key_type_arg;
2660 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002661 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002662
Gilles Peskine69c12672018-06-28 00:07:19 +02002663 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2664
itayzafrir5c753392018-05-08 11:18:38 +03002665 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002666 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002667 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002668 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2669 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2670 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002671
2672 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2673
2674 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002675 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002676 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2677
2678 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002679 key_data->x,
2680 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002681
2682 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002683 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002684 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002685 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002686exit:
2687 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002688 mbedtls_psa_crypto_free( );
2689}
2690/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002691
2692/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002693void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2694 int alg_arg, data_t *hash_data,
2695 data_t *signature_data,
2696 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002697{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002698 int slot = 1;
2699 psa_key_type_t key_type = key_type_arg;
2700 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002701 psa_status_t actual_status;
2702 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002703 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002704
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002705 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002706 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002707 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002708 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2709 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2710 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002711
2712 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2713
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002714 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002715 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002716 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2717
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002718 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002719 key_data->x,
2720 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002721
2722 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002723 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002724 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002725 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002726
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002727 TEST_ASSERT( actual_status == expected_status );
2728
2729exit:
2730 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002731 mbedtls_psa_crypto_free( );
2732}
2733/* END_CASE */
2734
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002735/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002736void asymmetric_encrypt( int key_type_arg,
2737 data_t *key_data,
2738 int alg_arg,
2739 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002740 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002741 int expected_output_length_arg,
2742 int expected_status_arg )
2743{
2744 int slot = 1;
2745 psa_key_type_t key_type = key_type_arg;
2746 psa_algorithm_t alg = alg_arg;
2747 size_t expected_output_length = expected_output_length_arg;
2748 size_t key_bits;
2749 unsigned char *output = NULL;
2750 size_t output_size;
2751 size_t output_length = ~0;
2752 psa_status_t actual_status;
2753 psa_status_t expected_status = expected_status_arg;
2754 psa_key_policy_t policy;
2755
2756 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2757
2758 /* Import the key */
2759 psa_key_policy_init( &policy );
2760 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2761 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2762 TEST_ASSERT( psa_import_key( slot, key_type,
2763 key_data->x,
2764 key_data->len ) == PSA_SUCCESS );
2765
2766 /* Determine the maximum output length */
2767 TEST_ASSERT( psa_get_key_information( slot,
2768 NULL,
2769 &key_bits ) == PSA_SUCCESS );
2770 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2771 output = mbedtls_calloc( 1, output_size );
Darryl Green9c862252018-07-24 12:52:44 +01002772 TEST_ASSERT( output_size == 0 || output != NULL );
Gilles Peskine656896e2018-06-29 19:12:28 +02002773
2774 /* Encrypt the input */
2775 actual_status = psa_asymmetric_encrypt( slot, alg,
2776 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002777 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002778 output, output_size,
2779 &output_length );
2780 TEST_ASSERT( actual_status == expected_status );
2781 TEST_ASSERT( output_length == expected_output_length );
2782
Gilles Peskine68428122018-06-30 18:42:41 +02002783 /* If the label is empty, the test framework puts a non-null pointer
2784 * in label->x. Test that a null pointer works as well. */
2785 if( label->len == 0 )
2786 {
2787 output_length = ~0;
2788 memset( output, 0, output_size );
2789 actual_status = psa_asymmetric_encrypt( slot, alg,
2790 input_data->x, input_data->len,
2791 NULL, label->len,
2792 output, output_size,
2793 &output_length );
2794 TEST_ASSERT( actual_status == expected_status );
2795 TEST_ASSERT( output_length == expected_output_length );
2796 }
2797
Gilles Peskine656896e2018-06-29 19:12:28 +02002798exit:
2799 psa_destroy_key( slot );
2800 mbedtls_free( output );
2801 mbedtls_psa_crypto_free( );
2802}
2803/* END_CASE */
2804
2805/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002806void asymmetric_encrypt_decrypt( int key_type_arg,
2807 data_t *key_data,
2808 int alg_arg,
2809 data_t *input_data,
2810 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002811{
2812 int slot = 1;
2813 psa_key_type_t key_type = key_type_arg;
2814 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002815 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002816 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002817 size_t output_size;
2818 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002819 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002820 size_t output2_size;
2821 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002822 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002823
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002824 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002825 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002826 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2827 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2828
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002829 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2830
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002831 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002832 psa_key_policy_set_usage( &policy,
2833 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002834 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002835 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2836
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002837 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002838 key_data->x,
2839 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002840
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002841
2842 /* Determine the maximum ciphertext length */
2843 TEST_ASSERT( psa_get_key_information( slot,
2844 NULL,
2845 &key_bits ) == PSA_SUCCESS );
2846 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2847 output = mbedtls_calloc( 1, output_size );
2848 TEST_ASSERT( output != NULL );
2849 output2_size = input_data->len;
2850 output2 = mbedtls_calloc( 1, output2_size );
2851 TEST_ASSERT( output2 != NULL );
2852
Gilles Peskineeebd7382018-06-08 18:11:54 +02002853 /* We test encryption by checking that encrypt-then-decrypt gives back
2854 * the original plaintext because of the non-optional random
2855 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002856 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002857 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002858 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002859 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002860 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002861 /* We don't know what ciphertext length to expect, but check that
2862 * it looks sensible. */
2863 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002864
Gilles Peskine2d277862018-06-18 15:41:12 +02002865 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002866 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002867 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002868 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002869 &output2_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002870 TEST_ASSERT( output2_length == input_data->len );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002871 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002872 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002873
2874exit:
2875 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002876 mbedtls_free( output );
2877 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002878 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002879}
2880/* END_CASE */
2881
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002882/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002883void asymmetric_decrypt( int key_type_arg,
2884 data_t *key_data,
2885 int alg_arg,
2886 data_t *input_data,
2887 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002888 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002889{
2890 int slot = 1;
2891 psa_key_type_t key_type = key_type_arg;
2892 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002893 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002894 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002895 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002896 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002897
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002898 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002899 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002900 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002901 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2902 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2903 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2904
Gilles Peskine4abf7412018-06-18 16:35:34 +02002905 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002906 output = mbedtls_calloc( 1, output_size );
2907 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002908
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002909 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2910
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002911 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002912 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002913 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2914
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002915 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002916 key_data->x,
2917 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002918
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002919 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002920 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002921 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002922 output,
2923 output_size,
2924 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002925 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002926 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002927
Gilles Peskine68428122018-06-30 18:42:41 +02002928 /* If the label is empty, the test framework puts a non-null pointer
2929 * in label->x. Test that a null pointer works as well. */
2930 if( label->len == 0 )
2931 {
2932 output_length = ~0;
2933 memset( output, 0, output_size );
2934 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2935 input_data->x, input_data->len,
2936 NULL, label->len,
2937 output,
2938 output_size,
2939 &output_length ) == PSA_SUCCESS );
2940 TEST_ASSERT( expected_data->len == output_length );
2941 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
2942 }
2943
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002944exit:
2945 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002946 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002947 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002948}
2949/* END_CASE */
2950
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002951/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002952void asymmetric_decrypt_fail( int key_type_arg,
2953 data_t *key_data,
2954 int alg_arg,
2955 data_t *input_data,
2956 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002957 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002958{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002959 int slot = 1;
2960 psa_key_type_t key_type = key_type_arg;
2961 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002962 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002963 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002964 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002965 psa_status_t actual_status;
2966 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002967 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002968
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002969 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002970 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002971 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2972 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2973
Gilles Peskine4abf7412018-06-18 16:35:34 +02002974 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002975 output = mbedtls_calloc( 1, output_size );
2976 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002977
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002978 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2979
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002980 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002981 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002982 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2983
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002984 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002985 key_data->x,
2986 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002987
Gilles Peskine2d277862018-06-18 15:41:12 +02002988 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002989 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002990 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002991 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002992 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002993 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002994 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002995
Gilles Peskine68428122018-06-30 18:42:41 +02002996 /* If the label is empty, the test framework puts a non-null pointer
2997 * in label->x. Test that a null pointer works as well. */
2998 if( label->len == 0 )
2999 {
3000 output_length = ~0;
3001 memset( output, 0, output_size );
3002 actual_status = psa_asymmetric_decrypt( slot, alg,
3003 input_data->x, input_data->len,
3004 NULL, label->len,
3005 output, output_size,
3006 &output_length );
3007 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003008 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003009 }
3010
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003011exit:
3012 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003013 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003014 mbedtls_psa_crypto_free( );
3015}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003016/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003017
3018/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003019void derive_setup( int key_type_arg,
3020 data_t *key_data,
3021 int alg_arg,
3022 data_t *salt,
3023 data_t *label,
3024 int requested_capacity_arg,
3025 int expected_status_arg )
3026{
3027 psa_key_slot_t slot = 1;
3028 size_t key_type = key_type_arg;
3029 psa_algorithm_t alg = alg_arg;
3030 size_t requested_capacity = requested_capacity_arg;
3031 psa_status_t expected_status = expected_status_arg;
3032 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3033 psa_key_policy_t policy;
3034
3035 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3036
3037 psa_key_policy_init( &policy );
3038 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3039 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3040
3041 TEST_ASSERT( psa_import_key( slot, key_type,
3042 key_data->x,
3043 key_data->len ) == PSA_SUCCESS );
3044
3045 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3046 salt->x, salt->len,
3047 label->x, label->len,
3048 requested_capacity ) == expected_status );
3049
3050exit:
3051 psa_generator_abort( &generator );
3052 psa_destroy_key( slot );
3053 mbedtls_psa_crypto_free( );
3054}
3055/* END_CASE */
3056
3057/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003058void derive_output( int alg_arg,
3059 data_t *key_data,
3060 data_t *salt,
3061 data_t *label,
3062 int requested_capacity_arg,
3063 data_t *expected_output1,
3064 data_t *expected_output2 )
3065{
3066 psa_key_slot_t slot = 1;
3067 psa_algorithm_t alg = alg_arg;
3068 size_t requested_capacity = requested_capacity_arg;
3069 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3070 uint8_t *expected_outputs[2] =
3071 {expected_output1->x, expected_output2->x};
3072 size_t output_sizes[2] =
3073 {expected_output1->len, expected_output2->len};
3074 size_t output_buffer_size = 0;
3075 uint8_t *output_buffer = NULL;
3076 size_t expected_capacity;
3077 size_t current_capacity;
3078 psa_key_policy_t policy;
3079 psa_status_t status;
3080 unsigned i;
3081
3082 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3083 {
3084 if( output_sizes[i] > output_buffer_size )
3085 output_buffer_size = output_sizes[i];
3086 if( output_sizes[i] == 0 )
3087 expected_outputs[i] = NULL;
3088 }
3089 output_buffer = mbedtls_calloc( 1, output_buffer_size );
3090 TEST_ASSERT( output_buffer != NULL );
3091 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3092
3093 psa_key_policy_init( &policy );
3094 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3095 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3096
3097 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3098 key_data->x,
3099 key_data->len ) == PSA_SUCCESS );
3100
3101 /* Extraction phase. */
3102 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3103 salt->x, salt->len,
3104 label->x, label->len,
3105 requested_capacity ) == PSA_SUCCESS );
3106 TEST_ASSERT( psa_get_generator_capacity( &generator,
3107 &current_capacity ) ==
3108 PSA_SUCCESS );
3109 TEST_ASSERT( current_capacity == requested_capacity );
3110 expected_capacity = requested_capacity;
3111
3112 /* Expansion phase. */
3113 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3114 {
3115 /* Read some bytes. */
3116 status = psa_generator_read( &generator,
3117 output_buffer, output_sizes[i] );
3118 if( expected_capacity == 0 && output_sizes[i] == 0 )
3119 {
3120 /* Reading 0 bytes when 0 bytes are available can go either way. */
3121 TEST_ASSERT( status == PSA_SUCCESS ||
3122 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3123 continue;
3124 }
3125 else if( expected_capacity == 0 ||
3126 output_sizes[i] > expected_capacity )
3127 {
3128 /* Capacity exceeded. */
3129 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3130 expected_capacity = 0;
3131 continue;
3132 }
3133 /* Success. Check the read data. */
3134 TEST_ASSERT( status == PSA_SUCCESS );
3135 if( output_sizes[i] != 0 )
3136 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3137 output_sizes[i] ) == 0 );
3138 /* Check the generator status. */
3139 expected_capacity -= output_sizes[i];
3140 TEST_ASSERT( psa_get_generator_capacity( &generator,
3141 &current_capacity ) ==
3142 PSA_SUCCESS );
3143 TEST_ASSERT( expected_capacity == current_capacity );
3144 }
3145 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3146
3147exit:
3148 mbedtls_free( output_buffer );
3149 psa_generator_abort( &generator );
3150 psa_destroy_key( slot );
3151 mbedtls_psa_crypto_free( );
3152}
3153/* END_CASE */
3154
3155/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003156void derive_full( int alg_arg,
3157 data_t *key_data,
3158 data_t *salt,
3159 data_t *label,
3160 int requested_capacity_arg )
3161{
3162 psa_key_slot_t slot = 1;
3163 psa_algorithm_t alg = alg_arg;
3164 size_t requested_capacity = requested_capacity_arg;
3165 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3166 unsigned char output_buffer[16];
3167 size_t expected_capacity = requested_capacity;
3168 size_t current_capacity;
3169 psa_key_policy_t policy;
3170
3171 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3172
3173 psa_key_policy_init( &policy );
3174 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3175 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3176
3177 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3178 key_data->x,
3179 key_data->len ) == PSA_SUCCESS );
3180
3181 /* Extraction phase. */
3182 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3183 salt->x, salt->len,
3184 label->x, label->len,
3185 requested_capacity ) == PSA_SUCCESS );
3186 TEST_ASSERT( psa_get_generator_capacity( &generator,
3187 &current_capacity ) ==
3188 PSA_SUCCESS );
3189 TEST_ASSERT( current_capacity == expected_capacity );
3190
3191 /* Expansion phase. */
3192 while( current_capacity > 0 )
3193 {
3194 size_t read_size = sizeof( output_buffer );
3195 if( read_size > current_capacity )
3196 read_size = current_capacity;
3197 TEST_ASSERT( psa_generator_read( &generator,
3198 output_buffer,
3199 read_size ) == PSA_SUCCESS );
3200 expected_capacity -= read_size;
3201 TEST_ASSERT( psa_get_generator_capacity( &generator,
3202 &current_capacity ) ==
3203 PSA_SUCCESS );
3204 TEST_ASSERT( current_capacity == expected_capacity );
3205 }
3206
3207 /* Check that the generator refuses to go over capacity. */
3208 TEST_ASSERT( psa_generator_read( &generator,
3209 output_buffer,
3210 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3211
3212 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3213
3214exit:
3215 psa_generator_abort( &generator );
3216 psa_destroy_key( slot );
3217 mbedtls_psa_crypto_free( );
3218}
3219/* END_CASE */
3220
3221/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003222void derive_key_exercise( int alg_arg,
3223 data_t *key_data,
3224 data_t *salt,
3225 data_t *label,
3226 int derived_type_arg,
3227 int derived_bits_arg,
3228 int derived_usage_arg,
3229 int derived_alg_arg )
3230{
3231 psa_key_slot_t base_key = 1;
3232 psa_key_slot_t derived_key = 2;
3233 psa_algorithm_t alg = alg_arg;
3234 psa_key_type_t derived_type = derived_type_arg;
3235 size_t derived_bits = derived_bits_arg;
3236 psa_key_usage_t derived_usage = derived_usage_arg;
3237 psa_algorithm_t derived_alg = derived_alg_arg;
3238 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3239 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3240 psa_key_policy_t policy;
3241 psa_key_type_t got_type;
3242 size_t got_bits;
3243
3244 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3245
3246 psa_key_policy_init( &policy );
3247 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3248 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3249 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3250 key_data->x,
3251 key_data->len ) == PSA_SUCCESS );
3252
3253 /* Derive a key. */
3254 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3255 salt->x, salt->len,
3256 label->x, label->len,
3257 capacity ) == PSA_SUCCESS );
3258 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3259 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3260 TEST_ASSERT( psa_generator_import_key( derived_key,
3261 derived_type,
3262 derived_bits,
3263 &generator ) == PSA_SUCCESS );
3264
3265 /* Test the key information */
3266 TEST_ASSERT( psa_get_key_information( derived_key,
3267 &got_type,
3268 &got_bits ) == PSA_SUCCESS );
3269 TEST_ASSERT( got_type == derived_type );
3270 TEST_ASSERT( got_bits == derived_bits );
3271
3272 /* Exercise the derived key. */
3273 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3274 goto exit;
3275
3276exit:
3277 psa_generator_abort( &generator );
3278 psa_destroy_key( base_key );
3279 psa_destroy_key( derived_key );
3280 mbedtls_psa_crypto_free( );
3281}
3282/* END_CASE */
3283
3284/* BEGIN_CASE */
3285void derive_key_export( int alg_arg,
3286 data_t *key_data,
3287 data_t *salt,
3288 data_t *label,
3289 int bytes1_arg,
3290 int bytes2_arg )
3291{
3292 psa_key_slot_t base_key = 1;
3293 psa_key_slot_t derived_key = 2;
3294 psa_algorithm_t alg = alg_arg;
3295 size_t bytes1 = bytes1_arg;
3296 size_t bytes2 = bytes2_arg;
3297 size_t capacity = bytes1 + bytes2;
3298 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3299 uint8_t *output_buffer = mbedtls_calloc( 1, capacity );
3300 uint8_t *export_buffer = mbedtls_calloc( 1, capacity );
3301 psa_key_policy_t policy;
3302 size_t length;
3303
3304 TEST_ASSERT( output_buffer != NULL );
3305 TEST_ASSERT( export_buffer != NULL );
3306 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3307
3308 psa_key_policy_init( &policy );
3309 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3310 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3311 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3312 key_data->x,
3313 key_data->len ) == PSA_SUCCESS );
3314
3315 /* Derive some material and output it. */
3316 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3317 salt->x, salt->len,
3318 label->x, label->len,
3319 capacity ) == PSA_SUCCESS );
3320 TEST_ASSERT( psa_generator_read( &generator,
3321 output_buffer,
3322 capacity ) == PSA_SUCCESS );
3323 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3324
3325 /* Derive the same output again, but this time store it in key objects. */
3326 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3327 salt->x, salt->len,
3328 label->x, label->len,
3329 capacity ) == PSA_SUCCESS );
3330 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3331 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3332 TEST_ASSERT( psa_generator_import_key( derived_key,
3333 PSA_KEY_TYPE_RAW_DATA,
3334 PSA_BYTES_TO_BITS( bytes1 ),
3335 &generator ) == PSA_SUCCESS );
3336 TEST_ASSERT( psa_export_key( derived_key,
3337 export_buffer, bytes1,
3338 &length ) == PSA_SUCCESS );
3339 TEST_ASSERT( length == bytes1 );
3340 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3341 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3342 TEST_ASSERT( psa_generator_import_key( derived_key,
3343 PSA_KEY_TYPE_RAW_DATA,
3344 PSA_BYTES_TO_BITS( bytes2 ),
3345 &generator ) == PSA_SUCCESS );
3346 TEST_ASSERT( psa_export_key( derived_key,
3347 export_buffer + bytes1, bytes2,
3348 &length ) == PSA_SUCCESS );
3349 TEST_ASSERT( length == bytes2 );
3350
3351 /* Compare the outputs from the two runs. */
3352 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3353
3354exit:
3355 mbedtls_free( output_buffer );
3356 mbedtls_free( export_buffer );
3357 psa_generator_abort( &generator );
3358 psa_destroy_key( base_key );
3359 psa_destroy_key( derived_key );
3360 mbedtls_psa_crypto_free( );
3361}
3362/* END_CASE */
3363
3364/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003365void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003366{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003367 size_t bytes = bytes_arg;
3368 const unsigned char trail[] = "don't overwrite me";
3369 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
3370 unsigned char *changed = mbedtls_calloc( 1, bytes );
3371 size_t i;
3372 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003373
Gilles Peskinea50d7392018-06-21 10:22:13 +02003374 TEST_ASSERT( output != NULL );
Darryl Green9c862252018-07-24 12:52:44 +01003375 TEST_ASSERT( bytes == 0 || changed != NULL );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003376 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003377
3378 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3379
Gilles Peskinea50d7392018-06-21 10:22:13 +02003380 /* Run several times, to ensure that every output byte will be
3381 * nonzero at least once with overwhelming probability
3382 * (2^(-8*number_of_runs)). */
3383 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003384 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02003385 memset( output, 0, bytes );
3386 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3387
3388 /* Check that no more than bytes have been overwritten */
3389 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3390
3391 for( i = 0; i < bytes; i++ )
3392 {
3393 if( output[i] != 0 )
3394 ++changed[i];
3395 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003396 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003397
3398 /* Check that every byte was changed to nonzero at least once. This
3399 * validates that psa_generate_random is overwriting every byte of
3400 * the output buffer. */
3401 for( i = 0; i < bytes; i++ )
3402 {
3403 TEST_ASSERT( changed[i] != 0 );
3404 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003405
3406exit:
3407 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003408 mbedtls_free( output );
3409 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003410}
3411/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003412
3413/* BEGIN_CASE */
3414void generate_key( int type_arg,
3415 int bits_arg,
3416 int usage_arg,
3417 int alg_arg,
3418 int expected_status_arg )
3419{
3420 int slot = 1;
3421 psa_key_type_t type = type_arg;
3422 psa_key_usage_t usage = usage_arg;
3423 size_t bits = bits_arg;
3424 psa_algorithm_t alg = alg_arg;
3425 psa_status_t expected_status = expected_status_arg;
3426 psa_key_type_t got_type;
3427 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003428 psa_status_t expected_info_status =
3429 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3430 psa_key_policy_t policy;
3431
3432 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3433
3434 psa_key_policy_init( &policy );
3435 psa_key_policy_set_usage( &policy, usage, alg );
3436 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3437
3438 /* Generate a key */
3439 TEST_ASSERT( psa_generate_key( slot, type, bits,
3440 NULL, 0 ) == expected_status );
3441
3442 /* Test the key information */
3443 TEST_ASSERT( psa_get_key_information( slot,
3444 &got_type,
3445 &got_bits ) == expected_info_status );
3446 if( expected_info_status != PSA_SUCCESS )
3447 goto exit;
3448 TEST_ASSERT( got_type == type );
3449 TEST_ASSERT( got_bits == bits );
3450
Gilles Peskine818ca122018-06-20 18:16:48 +02003451 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003452 if( ! exercise_key( slot, usage, alg ) )
3453 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003454
3455exit:
3456 psa_destroy_key( slot );
3457 mbedtls_psa_crypto_free( );
3458}
3459/* END_CASE */