blob: ea1547e1f0d816633250fdcf3573679dd5c86c20 [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 +0200392static int is_oid_of_key_type( psa_key_type_t type,
393 const uint8_t *oid, size_t oid_length )
394{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200395 const uint8_t *expected_oid = NULL;
396 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200397#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200398 if( PSA_KEY_TYPE_IS_RSA( type ) )
399 {
400 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
401 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
402 }
403 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200404#endif /* MBEDTLS_RSA_C */
405#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200406 if( PSA_KEY_TYPE_IS_ECC( type ) )
407 {
408 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
409 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
410 }
411 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200412#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200413 {
414 char message[40];
415 mbedtls_snprintf( message, sizeof( message ),
416 "OID not known for key type=0x%08lx",
417 (unsigned long) type );
418 test_fail( message, __LINE__, __FILE__ );
419 return( 0 );
420 }
421
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200422 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200423 return( 1 );
424
425exit:
426 return( 0 );
427}
428
429static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
430 size_t min_bits, size_t max_bits,
431 int must_be_odd )
432{
433 size_t len;
434 size_t actual_bits;
435 unsigned char msb;
436 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
437 MBEDTLS_ASN1_INTEGER ) == 0 );
438 /* Tolerate a slight departure from DER encoding:
439 * - 0 may be represented by an empty string or a 1-byte string.
440 * - The sign bit may be used as a value bit. */
441 if( ( len == 1 && ( *p )[0] == 0 ) ||
442 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
443 {
444 ++( *p );
445 --len;
446 }
447 if( min_bits == 0 && len == 0 )
448 return( 1 );
449 msb = ( *p )[0];
450 TEST_ASSERT( msb != 0 );
451 actual_bits = 8 * ( len - 1 );
452 while( msb != 0 )
453 {
454 msb >>= 1;
455 ++actual_bits;
456 }
457 TEST_ASSERT( actual_bits >= min_bits );
458 TEST_ASSERT( actual_bits <= max_bits );
459 if( must_be_odd )
460 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
461 *p += len;
462 return( 1 );
463exit:
464 return( 0 );
465}
466
467static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
468 size_t *len,
469 unsigned char n, unsigned char tag )
470{
471 int ret;
472 ret = mbedtls_asn1_get_tag( p, end, len,
473 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
474 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
475 if( ret != 0 )
476 return( ret );
477 end = *p + *len;
478 ret = mbedtls_asn1_get_tag( p, end, len, tag );
479 if( ret != 0 )
480 return( ret );
481 if( *p + *len != end )
482 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
483 return( 0 );
484}
485
486static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
487 uint8_t *exported, size_t exported_length )
488{
489 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200490 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200491 else
492 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200493
494#if defined(MBEDTLS_DES_C)
495 if( type == PSA_KEY_TYPE_DES )
496 {
497 /* Check the parity bits. */
498 unsigned i;
499 for( i = 0; i < bits / 8; i++ )
500 {
501 unsigned bit_count = 0;
502 unsigned m;
503 for( m = 1; m <= 0x100; m <<= 1 )
504 {
505 if( exported[i] & m )
506 ++bit_count;
507 }
508 TEST_ASSERT( bit_count % 2 != 0 );
509 }
510 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200511 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200512#endif
513
514#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
515 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
516 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200517 uint8_t *p = exported;
518 uint8_t *end = exported + exported_length;
519 size_t len;
520 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200521 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200522 * modulus INTEGER, -- n
523 * publicExponent INTEGER, -- e
524 * privateExponent INTEGER, -- d
525 * prime1 INTEGER, -- p
526 * prime2 INTEGER, -- q
527 * exponent1 INTEGER, -- d mod (p-1)
528 * exponent2 INTEGER, -- d mod (q-1)
529 * coefficient INTEGER, -- (inverse of q) mod p
530 * }
531 */
532 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
533 MBEDTLS_ASN1_SEQUENCE |
534 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
535 TEST_ASSERT( p + len == end );
536 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
537 goto exit;
538 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
539 goto exit;
540 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
541 goto exit;
542 /* Require d to be at least half the size of n. */
543 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
544 goto exit;
545 /* Require p and q to be at most half the size of n, rounded up. */
546 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
547 goto exit;
548 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
549 goto exit;
550 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
551 goto exit;
552 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
553 goto exit;
554 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
555 goto exit;
556 TEST_ASSERT( p == end );
557 }
558 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200559#endif /* MBEDTLS_RSA_C */
560
561#if defined(MBEDTLS_ECP_C)
562 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
563 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200564 uint8_t *p = exported;
565 uint8_t *end = exported + exported_length;
566 size_t len;
567 int version;
568 /* ECPrivateKey ::= SEQUENCE {
569 * version INTEGER, -- must be 1
570 * privateKey OCTET STRING,
571 * -- `ceiling(log_{256}(n))`-byte string, big endian,
572 * -- where n is the order of the curve.
573 * parameters ECParameters {{ NamedCurve }}, -- mandatory
574 * publicKey BIT STRING -- mandatory
575 * }
576 */
577 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
578 MBEDTLS_ASN1_SEQUENCE |
579 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
580 TEST_ASSERT( p + len == end );
581 TEST_ASSERT( mbedtls_asn1_get_int( &p, end, &version ) == 0 );
582 TEST_ASSERT( version == 1 );
583 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
584 MBEDTLS_ASN1_OCTET_STRING ) == 0 );
585 /* Bug in Mbed TLS: the length of the octet string depends on the value */
586 // TEST_ASSERT( len == PSA_BITS_TO_BYTES( bits ) );
587 p += len;
588 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 0,
589 MBEDTLS_ASN1_OID ) == 0 );
590 p += len;
Gilles Peskinec6290c02018-08-13 17:24:59 +0200591 /* publicKey: ECPoint in uncompressed representation (as below) */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200592 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 1,
593 MBEDTLS_ASN1_BIT_STRING ) == 0 );
594 TEST_ASSERT( p + len == end );
595 TEST_ASSERT( p[0] == 0 ); /* 0 unused bits in the bit string */
596 ++p;
597 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
598 TEST_ASSERT( p[0] == 4 );
599 }
600 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200601#endif /* MBEDTLS_ECP_C */
602
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200603 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
604 {
605 uint8_t *p = exported;
606 uint8_t *end = exported + exported_length;
607 size_t len;
608 mbedtls_asn1_buf alg;
609 mbedtls_asn1_buf params;
610 mbedtls_asn1_bitstring bitstring;
611 /* SubjectPublicKeyInfo ::= SEQUENCE {
612 * algorithm AlgorithmIdentifier,
613 * subjectPublicKey BIT STRING }
614 * AlgorithmIdentifier ::= SEQUENCE {
615 * algorithm OBJECT IDENTIFIER,
616 * parameters ANY DEFINED BY algorithm OPTIONAL }
617 */
618 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
619 MBEDTLS_ASN1_SEQUENCE |
620 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
621 TEST_ASSERT( p + len == end );
622 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
623 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
624 goto exit;
625 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
626 TEST_ASSERT( p == end );
627 p = bitstring.p;
628#if defined(MBEDTLS_RSA_C)
629 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
630 {
631 /* RSAPublicKey ::= SEQUENCE {
632 * modulus INTEGER, -- n
633 * publicExponent INTEGER } -- e
634 */
635 TEST_ASSERT( bitstring.unused_bits == 0 );
636 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
637 MBEDTLS_ASN1_SEQUENCE |
638 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
639 TEST_ASSERT( p + len == end );
640 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
641 goto exit;
642 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
643 goto exit;
644 TEST_ASSERT( p == end );
645 }
646 else
647#endif /* MBEDTLS_RSA_C */
648#if defined(MBEDTLS_ECP_C)
649 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
650 {
651 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200652 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200653 * -- then x_P as an n-bit string, big endian;
654 * -- then y_P as a n-bit string, big endian,
655 * -- where n is the order of the curve.
656 */
657 TEST_ASSERT( bitstring.unused_bits == 0 );
658 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
659 TEST_ASSERT( p[0] == 4 );
660 }
661 else
662#endif /* MBEDTLS_ECP_C */
663 {
664 char message[40];
665 mbedtls_snprintf( message, sizeof( message ),
666 "No sanity check for public key type=0x%08lx",
667 (unsigned long) type );
668 test_fail( message, __LINE__, __FILE__ );
669 return( 0 );
670 }
671 }
672 else
673
674 {
675 /* No sanity checks for other types */
676 }
677
678 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200679
680exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200681 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200682}
683
684static int exercise_export_key( psa_key_slot_t slot,
685 psa_key_usage_t usage )
686{
687 psa_key_type_t type;
688 size_t bits;
689 uint8_t *exported = NULL;
690 size_t exported_size = 0;
691 size_t exported_length = 0;
692 int ok = 0;
693
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200694 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
695
696 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
697 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200698 {
699 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
700 PSA_ERROR_NOT_PERMITTED );
701 return( 1 );
702 }
703
Gilles Peskined14664a2018-08-10 19:07:32 +0200704 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200705 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200706
707 TEST_ASSERT( psa_export_key( slot,
708 exported, exported_size,
709 &exported_length ) == PSA_SUCCESS );
710 ok = exported_key_sanity_check( type, bits, exported, exported_length );
711
712exit:
713 mbedtls_free( exported );
714 return( ok );
715}
716
717static int exercise_export_public_key( psa_key_slot_t slot )
718{
719 psa_key_type_t type;
720 psa_key_type_t public_type;
721 size_t bits;
722 uint8_t *exported = NULL;
723 size_t exported_size = 0;
724 size_t exported_length = 0;
725 int ok = 0;
726
727 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
728 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
729 {
730 TEST_ASSERT( psa_export_public_key( slot,
731 NULL, 0, &exported_length ) ==
732 PSA_ERROR_INVALID_ARGUMENT );
733 return( 1 );
734 }
735
736 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
737 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200738 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200739
740 TEST_ASSERT( psa_export_public_key( slot,
741 exported, exported_size,
742 &exported_length ) == PSA_SUCCESS );
743 ok = exported_key_sanity_check( public_type, bits,
744 exported, exported_length );
745
746exit:
747 mbedtls_free( exported );
748 return( ok );
749}
750
Gilles Peskine02b75072018-07-01 22:31:34 +0200751static int exercise_key( psa_key_slot_t slot,
752 psa_key_usage_t usage,
753 psa_algorithm_t alg )
754{
755 int ok;
756 if( alg == 0 )
757 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
758 else if( PSA_ALG_IS_MAC( alg ) )
759 ok = exercise_mac_key( slot, usage, alg );
760 else if( PSA_ALG_IS_CIPHER( alg ) )
761 ok = exercise_cipher_key( slot, usage, alg );
762 else if( PSA_ALG_IS_AEAD( alg ) )
763 ok = exercise_aead_key( slot, usage, alg );
764 else if( PSA_ALG_IS_SIGN( alg ) )
765 ok = exercise_signature_key( slot, usage, alg );
766 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
767 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200768 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
769 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200770 else
771 {
772 char message[40];
773 mbedtls_snprintf( message, sizeof( message ),
774 "No code to exercise alg=0x%08lx",
775 (unsigned long) alg );
776 test_fail( message, __LINE__, __FILE__ );
777 ok = 0;
778 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200779
780 ok = ok && exercise_export_key( slot, usage );
781 ok = ok && exercise_export_public_key( slot );
782
Gilles Peskine02b75072018-07-01 22:31:34 +0200783 return( ok );
784}
785
Gilles Peskinee59236f2018-01-27 23:32:46 +0100786/* END_HEADER */
787
788/* BEGIN_DEPENDENCIES
789 * depends_on:MBEDTLS_PSA_CRYPTO_C
790 * END_DEPENDENCIES
791 */
792
793/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200794void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100795{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100796 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100797 int i;
798 for( i = 0; i <= 1; i++ )
799 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800 status = psa_crypto_init( );
801 TEST_ASSERT( status == PSA_SUCCESS );
802 status = psa_crypto_init( );
803 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100804 mbedtls_psa_crypto_free( );
805 }
806}
807/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808
809/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200810void fill_slots( int max_arg )
811{
812 /* Fill all the slots until we run out of memory or out of slots,
813 * or until some limit specified in the test data for the sake of
814 * implementations with an essentially unlimited number of slots.
815 * This test assumes that available slots are numbered from 1. */
816
817 psa_key_slot_t slot;
818 psa_key_slot_t max = 0;
819 psa_key_policy_t policy;
820 uint8_t exported[sizeof( max )];
821 size_t exported_size;
822 psa_status_t status;
823
824 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
825
826 psa_key_policy_init( &policy );
827 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
828
829 for( max = 1; max <= (size_t) max_arg; max++ )
830 {
831 status = psa_set_key_policy( max, &policy );
832 /* Stop filling slots if we run out of memory or out of
833 * available slots. */
834 TEST_ASSERT( status == PSA_SUCCESS ||
835 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
836 status == PSA_ERROR_INVALID_ARGUMENT );
837 if( status != PSA_SUCCESS )
838 break;
839 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
840 (uint8_t*) &max, sizeof( max ) );
841 /* Since psa_set_key_policy succeeded, we know that the slot
842 * number is valid. But we may legitimately run out of memory. */
843 TEST_ASSERT( status == PSA_SUCCESS ||
844 status == PSA_ERROR_INSUFFICIENT_MEMORY );
845 if( status != PSA_SUCCESS )
846 break;
847 }
848 /* `max` is now the first slot number that wasn't filled. */
849 max -= 1;
850
851 for( slot = 1; slot <= max; slot++ )
852 {
853 TEST_ASSERT( psa_export_key( slot,
854 exported, sizeof( exported ),
855 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200856 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200857 }
858
859exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200860 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200861 mbedtls_psa_crypto_free( );
862}
863/* END_CASE */
864
865/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200866void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867{
868 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200869 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100870 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300873 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
875
Gilles Peskine4abf7412018-06-18 16:35:34 +0200876 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200877 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878 if( status == PSA_SUCCESS )
879 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
880
881exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100882 mbedtls_psa_crypto_free( );
883}
884/* END_CASE */
885
886/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200887void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
888{
889 int slot = 1;
890 size_t bits = bits_arg;
891 psa_status_t expected_status = expected_status_arg;
892 psa_status_t status;
893 psa_key_type_t type =
894 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
895 size_t buffer_size = /* Slight overapproximations */
896 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200897 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200898 unsigned char *p;
899 int ret;
900 size_t length;
901
902 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200903 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200904
905 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
906 bits, keypair ) ) >= 0 );
907 length = ret;
908
909 /* Try importing the key */
910 status = psa_import_key( slot, type, p, length );
911 TEST_ASSERT( status == expected_status );
912 if( status == PSA_SUCCESS )
913 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
914
915exit:
916 mbedtls_free( buffer );
917 mbedtls_psa_crypto_free( );
918}
919/* END_CASE */
920
921/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300922void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300923 int type_arg,
924 int alg_arg,
925 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100926 int expected_bits,
927 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200928 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100929 int canonical_input )
930{
931 int slot = 1;
932 int slot2 = slot + 1;
933 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200934 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200935 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100936 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100937 unsigned char *exported = NULL;
938 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100940 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941 size_t reexported_length;
942 psa_key_type_t got_type;
943 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200944 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300947 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300948 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200949 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200951 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
953
mohammad1603a97cb8c2018-03-28 03:46:26 -0700954 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200955 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700956 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
957
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958 /* Import the key */
959 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200960 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961
962 /* Test the key information */
963 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200964 &got_type,
965 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 TEST_ASSERT( got_type == type );
967 TEST_ASSERT( got_bits == (size_t) expected_bits );
968
969 /* Export the key */
970 status = psa_export_key( slot,
971 exported, export_size,
972 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200973 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100974
975 /* The exported length must be set by psa_export_key() to a value between 0
976 * and export_size. On errors, the exported length must be 0. */
977 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
978 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
979 TEST_ASSERT( exported_length <= export_size );
980
Gilles Peskine3f669c32018-06-21 09:21:51 +0200981 TEST_ASSERT( mem_is_zero( exported + exported_length,
982 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200984 {
985 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200987 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988
Gilles Peskine8f609232018-08-11 01:24:55 +0200989 if( ! exercise_export_key( slot, usage_arg ) )
990 goto exit;
991
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200993 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994 else
995 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700996 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
997
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200999 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001000 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001001 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001002 reexported,
1003 export_size,
1004 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001005 ASSERT_COMPARE( exported, exported_length,
1006 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 }
1008
1009destroy:
1010 /* Destroy the key */
1011 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1012 TEST_ASSERT( psa_get_key_information(
1013 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1014
1015exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001016 mbedtls_free( exported );
1017 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018 mbedtls_psa_crypto_free( );
1019}
1020/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001021
Moran Pekerf709f4a2018-06-06 17:26:04 +03001022/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001023void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001024 int type_arg,
1025 int alg_arg,
1026 int expected_bits,
1027 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001028 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001029{
1030 int slot = 1;
1031 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001032 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001033 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001034 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001035 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001036 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001037 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001038 psa_key_type_t got_type;
1039 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001040 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001041
Moran Pekerf709f4a2018-06-06 17:26:04 +03001042 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001043 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001044 export_size = (ptrdiff_t) data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001045 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001046
1047 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1048
1049 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001050 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001051 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1052
1053 /* Import the key */
1054 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001055 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001056
1057 /* Test the key information */
1058 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001059 &got_type,
1060 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001061 TEST_ASSERT( got_type == type );
1062 TEST_ASSERT( got_bits == (size_t) expected_bits );
1063
1064 /* Export the key */
1065 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001066 exported, export_size,
1067 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001068 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +01001069 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
1070 TEST_ASSERT( mem_is_zero( exported + exported_length,
1071 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001072 if( status != PSA_SUCCESS )
1073 goto destroy;
1074
Moran Pekerf709f4a2018-06-06 17:26:04 +03001075destroy:
1076 /* Destroy the key */
1077 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1078 TEST_ASSERT( psa_get_key_information(
1079 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1080
1081exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001082 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001083 mbedtls_psa_crypto_free( );
1084}
1085/* END_CASE */
1086
Gilles Peskine20035e32018-02-03 22:44:14 +01001087/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001088void import_and_exercise_key( data_t *data,
1089 int type_arg,
1090 int bits_arg,
1091 int alg_arg )
1092{
1093 int slot = 1;
1094 psa_key_type_t type = type_arg;
1095 size_t bits = bits_arg;
1096 psa_algorithm_t alg = alg_arg;
1097 psa_key_usage_t usage =
1098 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1099 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1100 PSA_KEY_USAGE_VERIFY :
1101 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1102 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1103 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1104 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1105 PSA_KEY_USAGE_ENCRYPT :
1106 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001107 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001108 0 );
1109 psa_key_policy_t policy;
1110 psa_key_type_t got_type;
1111 size_t got_bits;
1112 psa_status_t status;
1113
1114 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1115
1116 psa_key_policy_init( &policy );
1117 psa_key_policy_set_usage( &policy, usage, alg );
1118 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1119
1120 /* Import the key */
1121 status = psa_import_key( slot, type, data->x, data->len );
1122 TEST_ASSERT( status == PSA_SUCCESS );
1123
1124 /* Test the key information */
1125 TEST_ASSERT( psa_get_key_information( slot,
1126 &got_type,
1127 &got_bits ) == PSA_SUCCESS );
1128 TEST_ASSERT( got_type == type );
1129 TEST_ASSERT( got_bits == bits );
1130
1131 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001132 if( ! exercise_key( slot, usage, alg ) )
1133 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001134
1135exit:
1136 psa_destroy_key( slot );
1137 mbedtls_psa_crypto_free( );
1138}
1139/* END_CASE */
1140
1141/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001142void key_policy( int usage_arg, int alg_arg )
1143{
1144 int key_slot = 1;
1145 psa_algorithm_t alg = alg_arg;
1146 psa_key_usage_t usage = usage_arg;
1147 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1148 unsigned char key[32] = {0};
1149 psa_key_policy_t policy_set;
1150 psa_key_policy_t policy_get;
1151
1152 memset( key, 0x2a, sizeof( key ) );
1153
1154 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1155
1156 psa_key_policy_init( &policy_set );
1157 psa_key_policy_init( &policy_get );
1158
1159 psa_key_policy_set_usage( &policy_set, usage, alg );
1160
1161 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1162 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1163 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1164
1165 TEST_ASSERT( psa_import_key( key_slot, key_type,
1166 key, sizeof( key ) ) == PSA_SUCCESS );
1167
1168 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1169
1170 TEST_ASSERT( policy_get.usage == policy_set.usage );
1171 TEST_ASSERT( policy_get.alg == policy_set.alg );
1172
1173exit:
1174 psa_destroy_key( key_slot );
1175 mbedtls_psa_crypto_free( );
1176}
1177/* END_CASE */
1178
1179/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001180void mac_key_policy( int policy_usage,
1181 int policy_alg,
1182 int key_type,
1183 data_t *key_data,
1184 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001185{
1186 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001187 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001188 psa_mac_operation_t operation;
1189 psa_status_t status;
1190 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001191
1192 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1193
1194 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001195 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001196 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1197
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001198 TEST_ASSERT( psa_import_key( key_slot, key_type,
1199 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001200
Gilles Peskine89167cb2018-07-08 20:12:23 +02001201 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001202 if( policy_alg == exercise_alg &&
1203 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1204 TEST_ASSERT( status == PSA_SUCCESS );
1205 else
1206 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1207 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001208
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001210 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211 if( policy_alg == exercise_alg &&
1212 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001213 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214 else
1215 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1216
1217exit:
1218 psa_mac_abort( &operation );
1219 psa_destroy_key( key_slot );
1220 mbedtls_psa_crypto_free( );
1221}
1222/* END_CASE */
1223
1224/* BEGIN_CASE */
1225void cipher_key_policy( int policy_usage,
1226 int policy_alg,
1227 int key_type,
1228 data_t *key_data,
1229 int exercise_alg )
1230{
1231 int key_slot = 1;
1232 psa_key_policy_t policy;
1233 psa_cipher_operation_t operation;
1234 psa_status_t status;
1235
1236 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1237
1238 psa_key_policy_init( &policy );
1239 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1240 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1241
1242 TEST_ASSERT( psa_import_key( key_slot, key_type,
1243 key_data->x, key_data->len ) == PSA_SUCCESS );
1244
Gilles Peskinefe119512018-07-08 21:39:34 +02001245 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001246 if( policy_alg == exercise_alg &&
1247 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1248 TEST_ASSERT( status == PSA_SUCCESS );
1249 else
1250 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1251 psa_cipher_abort( &operation );
1252
Gilles Peskinefe119512018-07-08 21:39:34 +02001253 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001254 if( policy_alg == exercise_alg &&
1255 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1256 TEST_ASSERT( status == PSA_SUCCESS );
1257 else
1258 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1259
1260exit:
1261 psa_cipher_abort( &operation );
1262 psa_destroy_key( key_slot );
1263 mbedtls_psa_crypto_free( );
1264}
1265/* END_CASE */
1266
1267/* BEGIN_CASE */
1268void aead_key_policy( int policy_usage,
1269 int policy_alg,
1270 int key_type,
1271 data_t *key_data,
1272 int nonce_length_arg,
1273 int tag_length_arg,
1274 int exercise_alg )
1275{
1276 int key_slot = 1;
1277 psa_key_policy_t policy;
1278 psa_status_t status;
1279 unsigned char nonce[16] = {0};
1280 size_t nonce_length = nonce_length_arg;
1281 unsigned char tag[16];
1282 size_t tag_length = tag_length_arg;
1283 size_t output_length;
1284
1285 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1286 TEST_ASSERT( tag_length <= sizeof( tag ) );
1287
1288 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1289
1290 psa_key_policy_init( &policy );
1291 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1292 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1293
1294 TEST_ASSERT( psa_import_key( key_slot, key_type,
1295 key_data->x, key_data->len ) == PSA_SUCCESS );
1296
1297 status = psa_aead_encrypt( key_slot, exercise_alg,
1298 nonce, nonce_length,
1299 NULL, 0,
1300 NULL, 0,
1301 tag, tag_length,
1302 &output_length );
1303 if( policy_alg == exercise_alg &&
1304 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1305 TEST_ASSERT( status == PSA_SUCCESS );
1306 else
1307 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1308
1309 memset( tag, 0, sizeof( tag ) );
1310 status = psa_aead_decrypt( key_slot, exercise_alg,
1311 nonce, nonce_length,
1312 NULL, 0,
1313 tag, tag_length,
1314 NULL, 0,
1315 &output_length );
1316 if( policy_alg == exercise_alg &&
1317 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1318 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1319 else
1320 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1321
1322exit:
1323 psa_destroy_key( key_slot );
1324 mbedtls_psa_crypto_free( );
1325}
1326/* END_CASE */
1327
1328/* BEGIN_CASE */
1329void asymmetric_encryption_key_policy( int policy_usage,
1330 int policy_alg,
1331 int key_type,
1332 data_t *key_data,
1333 int exercise_alg )
1334{
1335 int key_slot = 1;
1336 psa_key_policy_t policy;
1337 psa_status_t status;
1338 size_t key_bits;
1339 size_t buffer_length;
1340 unsigned char *buffer = NULL;
1341 size_t output_length;
1342
1343 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1344
1345 psa_key_policy_init( &policy );
1346 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1347 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1348
1349 TEST_ASSERT( psa_import_key( key_slot, key_type,
1350 key_data->x, key_data->len ) == PSA_SUCCESS );
1351
1352 TEST_ASSERT( psa_get_key_information( key_slot,
1353 NULL,
1354 &key_bits ) == PSA_SUCCESS );
1355 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1356 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001357 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001358
1359 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1360 NULL, 0,
1361 NULL, 0,
1362 buffer, buffer_length,
1363 &output_length );
1364 if( policy_alg == exercise_alg &&
1365 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1366 TEST_ASSERT( status == PSA_SUCCESS );
1367 else
1368 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1369
1370 memset( buffer, 0, buffer_length );
1371 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1372 buffer, buffer_length,
1373 NULL, 0,
1374 buffer, buffer_length,
1375 &output_length );
1376 if( policy_alg == exercise_alg &&
1377 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1378 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1379 else
1380 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1381
1382exit:
1383 psa_destroy_key( key_slot );
1384 mbedtls_psa_crypto_free( );
1385 mbedtls_free( buffer );
1386}
1387/* END_CASE */
1388
1389/* BEGIN_CASE */
1390void asymmetric_signature_key_policy( int policy_usage,
1391 int policy_alg,
1392 int key_type,
1393 data_t *key_data,
1394 int exercise_alg )
1395{
1396 int key_slot = 1;
1397 psa_key_policy_t policy;
1398 psa_status_t status;
1399 unsigned char payload[16] = {1};
1400 size_t payload_length = sizeof( payload );
1401 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1402 size_t signature_length;
1403
1404 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1405
1406 psa_key_policy_init( &policy );
1407 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1408 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1409
1410 TEST_ASSERT( psa_import_key( key_slot, key_type,
1411 key_data->x, key_data->len ) == PSA_SUCCESS );
1412
1413 status = psa_asymmetric_sign( key_slot, exercise_alg,
1414 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001415 signature, sizeof( signature ),
1416 &signature_length );
1417 if( policy_alg == exercise_alg &&
1418 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1419 TEST_ASSERT( status == PSA_SUCCESS );
1420 else
1421 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1422
1423 memset( signature, 0, sizeof( signature ) );
1424 status = psa_asymmetric_verify( key_slot, exercise_alg,
1425 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001426 signature, sizeof( signature ) );
1427 if( policy_alg == exercise_alg &&
1428 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1429 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1430 else
1431 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001432
1433exit:
1434 psa_destroy_key( key_slot );
1435 mbedtls_psa_crypto_free( );
1436}
1437/* END_CASE */
1438
1439/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001440void derive_key_policy( int policy_usage,
1441 int policy_alg,
1442 int key_type,
1443 data_t *key_data,
1444 int exercise_alg )
1445{
1446 int key_slot = 1;
1447 psa_key_policy_t policy;
1448 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1449 psa_status_t status;
1450
1451 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1452
1453 psa_key_policy_init( &policy );
1454 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1455 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1456
1457 TEST_ASSERT( psa_import_key( key_slot, key_type,
1458 key_data->x, key_data->len ) == PSA_SUCCESS );
1459
1460 status = psa_key_derivation( &generator, key_slot,
1461 exercise_alg,
1462 NULL, 0,
1463 NULL, 0,
1464 1 );
1465 if( policy_alg == exercise_alg &&
1466 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1467 TEST_ASSERT( status == PSA_SUCCESS );
1468 else
1469 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1470
1471exit:
1472 psa_generator_abort( &generator );
1473 psa_destroy_key( key_slot );
1474 mbedtls_psa_crypto_free( );
1475}
1476/* END_CASE */
1477
1478/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001479void key_lifetime( int lifetime_arg )
1480{
1481 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001482 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001483 unsigned char key[32] = {0};
1484 psa_key_lifetime_t lifetime_set = lifetime_arg;
1485 psa_key_lifetime_t lifetime_get;
1486
1487 memset( key, 0x2a, sizeof( key ) );
1488
1489 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1490
1491 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1492 lifetime_set ) == PSA_SUCCESS );
1493
1494 TEST_ASSERT( psa_import_key( key_slot, key_type,
1495 key, sizeof( key ) ) == PSA_SUCCESS );
1496
1497 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1498 &lifetime_get ) == PSA_SUCCESS );
1499
1500 TEST_ASSERT( lifetime_get == lifetime_set );
1501
1502exit:
1503 psa_destroy_key( key_slot );
1504 mbedtls_psa_crypto_free( );
1505}
1506/* END_CASE */
1507
1508/* BEGIN_CASE */
1509void key_lifetime_set_fail( int key_slot_arg,
1510 int lifetime_arg,
1511 int expected_status_arg )
1512{
1513 psa_key_slot_t key_slot = key_slot_arg;
1514 psa_key_lifetime_t lifetime_set = lifetime_arg;
1515 psa_status_t actual_status;
1516 psa_status_t expected_status = expected_status_arg;
1517
1518 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1519
1520 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1521
1522 if( actual_status == PSA_SUCCESS )
1523 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1524
1525 TEST_ASSERT( expected_status == actual_status );
1526
1527exit:
1528 psa_destroy_key( key_slot );
1529 mbedtls_psa_crypto_free( );
1530}
1531/* END_CASE */
1532
1533/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001534void hash_setup( int alg_arg,
1535 int expected_status_arg )
1536{
1537 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001538 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001539 psa_hash_operation_t operation;
1540 psa_status_t status;
1541
1542 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1543
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001544 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001545 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001546 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001547
1548exit:
1549 mbedtls_psa_crypto_free( );
1550}
1551/* END_CASE */
1552
1553/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001554void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001555{
1556 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001557 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001558 size_t actual_hash_length;
1559 psa_hash_operation_t operation;
1560
Gilles Peskine69c12672018-06-28 00:07:19 +02001561 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1562 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1563
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001564 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001565 TEST_ASSERT( expected_hash != NULL );
1566 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1567 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001568
1569 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1570
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001571 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001572 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001573 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001574 TEST_ASSERT( psa_hash_finish( &operation,
1575 actual_hash, sizeof( actual_hash ),
1576 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001577 ASSERT_COMPARE( expected_hash->x, expected_hash->len,
1578 actual_hash, actual_hash_length );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001579
1580exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001581 mbedtls_psa_crypto_free( );
1582}
1583/* END_CASE */
1584
1585/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001586void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001587{
1588 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001589 psa_hash_operation_t operation;
1590
Gilles Peskine69c12672018-06-28 00:07:19 +02001591 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1592 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1593
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001594 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001595 TEST_ASSERT( expected_hash != NULL );
1596 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1597 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001598
1599 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1600
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001601 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001602 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001603 input->x,
1604 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001605 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001606 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001607 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001608
1609exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001610 mbedtls_psa_crypto_free( );
1611}
1612/* END_CASE */
1613
1614/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001615void mac_setup( int key_type_arg,
1616 data_t *key,
1617 int alg_arg,
1618 int expected_status_arg )
1619{
1620 int key_slot = 1;
1621 psa_key_type_t key_type = key_type_arg;
1622 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001623 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001624 psa_mac_operation_t operation;
1625 psa_key_policy_t policy;
1626 psa_status_t status;
1627
1628 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1629
1630 psa_key_policy_init( &policy );
1631 psa_key_policy_set_usage( &policy,
1632 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1633 alg );
1634 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1635
1636 TEST_ASSERT( psa_import_key( key_slot, key_type,
1637 key->x, key->len ) == PSA_SUCCESS );
1638
Gilles Peskine89167cb2018-07-08 20:12:23 +02001639 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001640 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001641 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001642
1643exit:
1644 psa_destroy_key( key_slot );
1645 mbedtls_psa_crypto_free( );
1646}
1647/* END_CASE */
1648
1649/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001650void mac_verify( int key_type_arg,
1651 data_t *key,
1652 int alg_arg,
1653 data_t *input,
1654 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001655{
1656 int key_slot = 1;
1657 psa_key_type_t key_type = key_type_arg;
1658 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001659 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001660 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001661
Gilles Peskine69c12672018-06-28 00:07:19 +02001662 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1663
Gilles Peskine8c9def32018-02-08 10:02:12 +01001664 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001665 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001666 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001667 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001668 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1669 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001670
1671 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1672
mohammad16036df908f2018-04-02 08:34:15 -07001673 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001674 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001675 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1676
Gilles Peskine8c9def32018-02-08 10:02:12 +01001677 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001678 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001679
Gilles Peskine89167cb2018-07-08 20:12:23 +02001680 TEST_ASSERT( psa_mac_verify_setup( &operation,
1681 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001682 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1683 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001684 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001685 TEST_ASSERT( psa_mac_verify_finish( &operation,
1686 expected_mac->x,
1687 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001688
1689exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690 psa_destroy_key( key_slot );
1691 mbedtls_psa_crypto_free( );
1692}
1693/* END_CASE */
1694
1695/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001696void cipher_setup( int key_type_arg,
1697 data_t *key,
1698 int alg_arg,
1699 int expected_status_arg )
1700{
1701 int key_slot = 1;
1702 psa_key_type_t key_type = key_type_arg;
1703 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001704 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001705 psa_cipher_operation_t operation;
1706 psa_key_policy_t policy;
1707 psa_status_t status;
1708
1709 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1710
1711 psa_key_policy_init( &policy );
1712 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1713 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1714
1715 TEST_ASSERT( psa_import_key( key_slot, key_type,
1716 key->x, key->len ) == PSA_SUCCESS );
1717
Gilles Peskinefe119512018-07-08 21:39:34 +02001718 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001719 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001720 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001721
1722exit:
1723 psa_destroy_key( key_slot );
1724 mbedtls_psa_crypto_free( );
1725}
1726/* END_CASE */
1727
1728/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001729void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001730 data_t *key,
1731 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001732 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001733{
1734 int key_slot = 1;
1735 psa_status_t status;
1736 psa_key_type_t key_type = key_type_arg;
1737 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001738 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001739 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001740 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001741 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001742 size_t output_buffer_size = 0;
1743 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001744 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001745 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001746 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001747
Gilles Peskine50e586b2018-06-08 14:28:46 +02001748 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001749 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001750 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001751 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1752 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1753 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001754
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001755 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1756 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001757
1758 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1759
Moran Pekered346952018-07-05 15:22:45 +03001760 psa_key_policy_init( &policy );
1761 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1762 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1763
Gilles Peskine50e586b2018-06-08 14:28:46 +02001764 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001765 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001766
Gilles Peskinefe119512018-07-08 21:39:34 +02001767 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1768 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001769
Gilles Peskinefe119512018-07-08 21:39:34 +02001770 TEST_ASSERT( psa_cipher_set_iv( &operation,
1771 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001772 output_buffer_size = (size_t) input->len +
1773 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001774 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001775
Gilles Peskine4abf7412018-06-18 16:35:34 +02001776 TEST_ASSERT( psa_cipher_update( &operation,
1777 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001778 output, output_buffer_size,
1779 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001780 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001781 status = psa_cipher_finish( &operation,
1782 output + function_output_length,
1783 output_buffer_size,
1784 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001785 total_output_length += function_output_length;
1786
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001787 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001788 if( expected_status == PSA_SUCCESS )
1789 {
1790 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001791 ASSERT_COMPARE( expected_output->x, expected_output->len,
1792 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001793 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001794
Gilles Peskine50e586b2018-06-08 14:28:46 +02001795exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001796 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001797 psa_destroy_key( key_slot );
1798 mbedtls_psa_crypto_free( );
1799}
1800/* END_CASE */
1801
1802/* BEGIN_CASE */
1803void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001804 data_t *key,
1805 data_t *input,
1806 int first_part_size,
1807 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001808{
1809 int key_slot = 1;
1810 psa_key_type_t key_type = key_type_arg;
1811 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001812 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001813 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001814 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001815 size_t output_buffer_size = 0;
1816 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001817 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001818 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001819 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001820
Gilles Peskine50e586b2018-06-08 14:28:46 +02001821 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001822 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001823 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001824 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1825 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1826 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001827
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001828 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1829 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001830
1831 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1832
Moran Pekered346952018-07-05 15:22:45 +03001833 psa_key_policy_init( &policy );
1834 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1835 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1836
Gilles Peskine50e586b2018-06-08 14:28:46 +02001837 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001838 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001839
Gilles Peskinefe119512018-07-08 21:39:34 +02001840 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1841 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001842
Gilles Peskinefe119512018-07-08 21:39:34 +02001843 TEST_ASSERT( psa_cipher_set_iv( &operation,
1844 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001845 output_buffer_size = (size_t) input->len +
1846 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001847 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001848
Gilles Peskine4abf7412018-06-18 16:35:34 +02001849 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001850 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001851 output, output_buffer_size,
1852 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001853 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001854 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001855 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001856 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001857 output, output_buffer_size,
1858 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001859 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001860 TEST_ASSERT( psa_cipher_finish( &operation,
1861 output + function_output_length,
1862 output_buffer_size,
1863 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001864 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001865 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1866
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001867 ASSERT_COMPARE( expected_output->x, expected_output->len,
1868 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001869
1870exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001871 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001872 psa_destroy_key( key_slot );
1873 mbedtls_psa_crypto_free( );
1874}
1875/* END_CASE */
1876
1877/* BEGIN_CASE */
1878void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001879 data_t *key,
1880 data_t *input,
1881 int first_part_size,
1882 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001883{
1884 int key_slot = 1;
1885
1886 psa_key_type_t key_type = key_type_arg;
1887 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001888 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001889 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001890 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001891 size_t output_buffer_size = 0;
1892 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001893 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001894 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001895 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001896
Gilles Peskine50e586b2018-06-08 14:28:46 +02001897 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001898 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001899 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001900 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1901 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1902 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001903
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001904 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1905 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001906
1907 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1908
Moran Pekered346952018-07-05 15:22:45 +03001909 psa_key_policy_init( &policy );
1910 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1911 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1912
Gilles Peskine50e586b2018-06-08 14:28:46 +02001913 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001914 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001915
Gilles Peskinefe119512018-07-08 21:39:34 +02001916 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1917 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001918
Gilles Peskinefe119512018-07-08 21:39:34 +02001919 TEST_ASSERT( psa_cipher_set_iv( &operation,
1920 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001921
mohammad16033d91abe2018-07-03 13:15:54 +03001922 output_buffer_size = (size_t) input->len +
1923 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001924 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001925
Gilles Peskine4abf7412018-06-18 16:35:34 +02001926 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1927 TEST_ASSERT( psa_cipher_update( &operation,
1928 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001929 output, output_buffer_size,
1930 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001931 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001932 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001933 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001934 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001935 output, output_buffer_size,
1936 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001937 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001938 TEST_ASSERT( psa_cipher_finish( &operation,
1939 output + function_output_length,
1940 output_buffer_size,
1941 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001942 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001943 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1944
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001945 ASSERT_COMPARE( expected_output->x, expected_output->len,
1946 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001947
1948exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001949 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001950 psa_destroy_key( key_slot );
1951 mbedtls_psa_crypto_free( );
1952}
1953/* END_CASE */
1954
Gilles Peskine50e586b2018-06-08 14:28:46 +02001955/* BEGIN_CASE */
1956void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001957 data_t *key,
1958 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001959 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001960{
1961 int key_slot = 1;
1962 psa_status_t status;
1963 psa_key_type_t key_type = key_type_arg;
1964 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001965 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001966 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001967 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001968 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001969 size_t output_buffer_size = 0;
1970 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001971 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001972 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001973 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001974
Gilles Peskine50e586b2018-06-08 14:28:46 +02001975 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001976 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001977 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001978 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1979 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1980 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001981
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001982 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1983 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001984
1985 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1986
Moran Pekered346952018-07-05 15:22:45 +03001987 psa_key_policy_init( &policy );
1988 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1989 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1990
Gilles Peskine50e586b2018-06-08 14:28:46 +02001991 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001992 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001993
Gilles Peskinefe119512018-07-08 21:39:34 +02001994 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1995 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001996
Gilles Peskinefe119512018-07-08 21:39:34 +02001997 TEST_ASSERT( psa_cipher_set_iv( &operation,
1998 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001999
mohammad16033d91abe2018-07-03 13:15:54 +03002000 output_buffer_size = (size_t) input->len +
2001 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002002 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002003
Gilles Peskine4abf7412018-06-18 16:35:34 +02002004 TEST_ASSERT( psa_cipher_update( &operation,
2005 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002006 output, output_buffer_size,
2007 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002008 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002009 status = psa_cipher_finish( &operation,
2010 output + function_output_length,
2011 output_buffer_size,
2012 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002013 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002014 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002015
2016 if( expected_status == PSA_SUCCESS )
2017 {
2018 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002019 ASSERT_COMPARE( expected_output->x, expected_output->len,
2020 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002021 }
2022
Gilles Peskine50e586b2018-06-08 14:28:46 +02002023exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002024 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002025 psa_destroy_key( key_slot );
2026 mbedtls_psa_crypto_free( );
2027}
2028/* END_CASE */
2029
Gilles Peskine50e586b2018-06-08 14:28:46 +02002030/* BEGIN_CASE */
2031void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002032 data_t *key,
2033 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002034{
2035 int key_slot = 1;
2036 psa_key_type_t key_type = key_type_arg;
2037 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002038 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002039 size_t iv_size = 16;
2040 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002041 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002042 size_t output1_size = 0;
2043 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002044 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002045 size_t output2_size = 0;
2046 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002047 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002048 psa_cipher_operation_t operation1;
2049 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002050 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002051
mohammad1603d7d7ba52018-03-12 18:51:53 +02002052 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002053 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002054 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2055 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002056
mohammad1603d7d7ba52018-03-12 18:51:53 +02002057 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2058
Moran Pekered346952018-07-05 15:22:45 +03002059 psa_key_policy_init( &policy );
2060 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2061 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2062
mohammad1603d7d7ba52018-03-12 18:51:53 +02002063 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002064 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002065
Gilles Peskinefe119512018-07-08 21:39:34 +02002066 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2067 key_slot, alg ) == PSA_SUCCESS );
2068 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2069 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002070
Gilles Peskinefe119512018-07-08 21:39:34 +02002071 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2072 iv, iv_size,
2073 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002074 output1_size = (size_t) input->len +
2075 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002076 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002077
Gilles Peskine4abf7412018-06-18 16:35:34 +02002078 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002079 output1, output1_size,
2080 &output1_length ) == PSA_SUCCESS );
2081 TEST_ASSERT( psa_cipher_finish( &operation1,
2082 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002083 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002084
Gilles Peskine048b7f02018-06-08 14:20:49 +02002085 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002086
2087 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2088
2089 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002090 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002091
Gilles Peskinefe119512018-07-08 21:39:34 +02002092 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2093 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002094 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2095 output2, output2_size,
2096 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002097 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002098 TEST_ASSERT( psa_cipher_finish( &operation2,
2099 output2 + output2_length,
2100 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002101 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002102
Gilles Peskine048b7f02018-06-08 14:20:49 +02002103 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002104
Janos Follath25c4fa82018-07-06 16:23:25 +01002105 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002106
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002107 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002108
2109exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002110 mbedtls_free( output1 );
2111 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002112 psa_destroy_key( key_slot );
2113 mbedtls_psa_crypto_free( );
2114}
2115/* END_CASE */
2116
2117/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002118void cipher_verify_output_multipart( int alg_arg,
2119 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002120 data_t *key,
2121 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002122 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002123{
2124 int key_slot = 1;
2125 psa_key_type_t key_type = key_type_arg;
2126 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002127 unsigned char iv[16] = {0};
2128 size_t iv_size = 16;
2129 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002130 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002131 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002132 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002133 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002134 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002135 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002136 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002137 psa_cipher_operation_t operation1;
2138 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002139 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002140
Moran Pekerded84402018-06-06 16:36:50 +03002141 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002142 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002143 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2144 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002145
Moran Pekerded84402018-06-06 16:36:50 +03002146 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2147
Moran Pekered346952018-07-05 15:22:45 +03002148 psa_key_policy_init( &policy );
2149 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2150 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2151
Moran Pekerded84402018-06-06 16:36:50 +03002152 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002153 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002154
Gilles Peskinefe119512018-07-08 21:39:34 +02002155 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2156 key_slot, alg ) == PSA_SUCCESS );
2157 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2158 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002159
Gilles Peskinefe119512018-07-08 21:39:34 +02002160 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2161 iv, iv_size,
2162 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002163 output1_buffer_size = (size_t) input->len +
2164 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002165 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002166
Gilles Peskine4abf7412018-06-18 16:35:34 +02002167 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002168
itayzafrir3e02b3b2018-06-12 17:06:52 +03002169 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002170 output1, output1_buffer_size,
2171 &function_output_length ) == PSA_SUCCESS );
2172 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002173
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002174 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002175 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002176 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002177 output1, output1_buffer_size,
2178 &function_output_length ) == PSA_SUCCESS );
2179 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002180
Gilles Peskine048b7f02018-06-08 14:20:49 +02002181 TEST_ASSERT( psa_cipher_finish( &operation1,
2182 output1 + output1_length,
2183 output1_buffer_size - output1_length,
2184 &function_output_length ) == PSA_SUCCESS );
2185 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002186
2187 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2188
Gilles Peskine048b7f02018-06-08 14:20:49 +02002189 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002190 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002191
Gilles Peskinefe119512018-07-08 21:39:34 +02002192 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2193 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002194
2195 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002196 output2, output2_buffer_size,
2197 &function_output_length ) == PSA_SUCCESS );
2198 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002199
Gilles Peskine048b7f02018-06-08 14:20:49 +02002200 TEST_ASSERT( psa_cipher_update( &operation2,
2201 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002202 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002203 output2, output2_buffer_size,
2204 &function_output_length ) == PSA_SUCCESS );
2205 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002206
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002207 TEST_ASSERT( psa_cipher_finish( &operation2,
2208 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002209 output2_buffer_size - output2_length,
2210 &function_output_length ) == PSA_SUCCESS );
2211 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002212
Janos Follath25c4fa82018-07-06 16:23:25 +01002213 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002214
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002215 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002216
2217exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002218 mbedtls_free( output1 );
2219 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002220 psa_destroy_key( key_slot );
2221 mbedtls_psa_crypto_free( );
2222}
2223/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002224
Gilles Peskine20035e32018-02-03 22:44:14 +01002225/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002226void aead_encrypt_decrypt( int key_type_arg,
2227 data_t * key_data,
2228 int alg_arg,
2229 data_t * input_data,
2230 data_t * nonce,
2231 data_t * additional_data,
2232 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002233{
2234 int slot = 1;
2235 psa_key_type_t key_type = key_type_arg;
2236 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002237 unsigned char *output_data = NULL;
2238 size_t output_size = 0;
2239 size_t output_length = 0;
2240 unsigned char *output_data2 = NULL;
2241 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002242 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002243 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002244 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002245
Gilles Peskinea1cac842018-06-11 19:33:02 +02002246 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002247 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002248 TEST_ASSERT( nonce != NULL );
2249 TEST_ASSERT( additional_data != NULL );
2250 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2251 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2252 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2253 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2254
Gilles Peskine4abf7412018-06-18 16:35:34 +02002255 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002256 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002257
2258 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2259
2260 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002261 psa_key_policy_set_usage( &policy,
2262 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2263 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002264 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2265
2266 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002267 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002268
2269 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002270 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002271 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002272 additional_data->len,
2273 input_data->x, input_data->len,
2274 output_data, output_size,
2275 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002276
2277 if( PSA_SUCCESS == expected_result )
2278 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002279 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002280
2281 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002282 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002283 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002284 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002285 output_data, output_length,
2286 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002287 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002288
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002289 ASSERT_COMPARE( input_data->x, input_data->len,
2290 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002291 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002292
Gilles Peskinea1cac842018-06-11 19:33:02 +02002293exit:
2294 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002295 mbedtls_free( output_data );
2296 mbedtls_free( output_data2 );
2297 mbedtls_psa_crypto_free( );
2298}
2299/* END_CASE */
2300
2301/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002302void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002303 int alg_arg, data_t * input_data,
2304 data_t * additional_data, data_t * nonce,
2305 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002306{
2307 int slot = 1;
2308 psa_key_type_t key_type = key_type_arg;
2309 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002310 unsigned char *output_data = NULL;
2311 size_t output_size = 0;
2312 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002313 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002314 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002315
Gilles Peskinea1cac842018-06-11 19:33:02 +02002316 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002317 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002318 TEST_ASSERT( additional_data != NULL );
2319 TEST_ASSERT( nonce != NULL );
2320 TEST_ASSERT( expected_result != NULL );
2321 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2322 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2323 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2324 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2325 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2326
Gilles Peskine4abf7412018-06-18 16:35:34 +02002327 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002328 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002329
Gilles Peskinea1cac842018-06-11 19:33:02 +02002330 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2331
2332 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002333 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002334 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2335
2336 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002337 key_data->x,
2338 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002339
2340 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002341 nonce->x, nonce->len,
2342 additional_data->x, additional_data->len,
2343 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002344 output_data, output_size,
2345 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002346
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002347 ASSERT_COMPARE( expected_result->x, expected_result->len,
2348 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002349
Gilles Peskinea1cac842018-06-11 19:33:02 +02002350exit:
2351 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002352 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002353 mbedtls_psa_crypto_free( );
2354}
2355/* END_CASE */
2356
2357/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002358void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002359 int alg_arg, data_t * input_data,
2360 data_t * additional_data, data_t * nonce,
2361 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002362{
2363 int slot = 1;
2364 psa_key_type_t key_type = key_type_arg;
2365 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002366 unsigned char *output_data = NULL;
2367 size_t output_size = 0;
2368 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002369 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002370 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002371 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002372
Gilles Peskinea1cac842018-06-11 19:33:02 +02002373 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002374 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002375 TEST_ASSERT( additional_data != NULL );
2376 TEST_ASSERT( nonce != NULL );
2377 TEST_ASSERT( expected_data != NULL );
2378 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2379 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2380 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2381 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2382 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2383
Gilles Peskine4abf7412018-06-18 16:35:34 +02002384 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002385 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002386
Gilles Peskinea1cac842018-06-11 19:33:02 +02002387 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2388
2389 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002390 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002391 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2392
2393 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002394 key_data->x,
2395 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002396
2397 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002398 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002399 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002400 additional_data->len,
2401 input_data->x, input_data->len,
2402 output_data, output_size,
2403 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002404
Gilles Peskine2d277862018-06-18 15:41:12 +02002405 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002406 ASSERT_COMPARE( expected_data->x, expected_data->len,
2407 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002408
Gilles Peskinea1cac842018-06-11 19:33:02 +02002409exit:
2410 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002411 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002412 mbedtls_psa_crypto_free( );
2413}
2414/* END_CASE */
2415
2416/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002417void signature_size( int type_arg,
2418 int bits,
2419 int alg_arg,
2420 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002421{
2422 psa_key_type_t type = type_arg;
2423 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002424 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002425 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2426exit:
2427 ;
2428}
2429/* END_CASE */
2430
2431/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002432void sign_deterministic( int key_type_arg, data_t *key_data,
2433 int alg_arg, data_t *input_data,
2434 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002435{
2436 int slot = 1;
2437 psa_key_type_t key_type = key_type_arg;
2438 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002439 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002440 unsigned char *signature = NULL;
2441 size_t signature_size;
2442 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002443 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002444
Gilles Peskine20035e32018-02-03 22:44:14 +01002445 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002446 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002447 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002448 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2449 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2450 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002451
2452 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2453
mohammad1603a97cb8c2018-03-28 03:46:26 -07002454 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002455 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002456 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2457
Gilles Peskine20035e32018-02-03 22:44:14 +01002458 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002459 key_data->x,
2460 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002461 TEST_ASSERT( psa_get_key_information( slot,
2462 NULL,
2463 &key_bits ) == PSA_SUCCESS );
2464
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002465 /* Allocate a buffer which has the size advertized by the
2466 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002467 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2468 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002469 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002470 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002471 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002472
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002473 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002474 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002475 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002476 signature, signature_size,
2477 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002478 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002479 ASSERT_COMPARE( output_data->x, output_data->len,
2480 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002481
2482exit:
2483 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002484 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002485 mbedtls_psa_crypto_free( );
2486}
2487/* END_CASE */
2488
2489/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002490void sign_fail( int key_type_arg, data_t *key_data,
2491 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002492 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002493{
2494 int slot = 1;
2495 psa_key_type_t key_type = key_type_arg;
2496 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002497 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002498 psa_status_t actual_status;
2499 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002500 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002501 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002502 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002503
Gilles Peskine20035e32018-02-03 22:44:14 +01002504 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002505 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002506 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2507 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2508
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002509 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002510
2511 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2512
mohammad1603a97cb8c2018-03-28 03:46:26 -07002513 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002514 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002515 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2516
Gilles Peskine20035e32018-02-03 22:44:14 +01002517 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002518 key_data->x,
2519 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002520
2521 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002522 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002523 signature, signature_size,
2524 &signature_length );
2525 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002526 /* The value of *signature_length is unspecified on error, but
2527 * whatever it is, it should be less than signature_size, so that
2528 * if the caller tries to read *signature_length bytes without
2529 * checking the error code then they don't overflow a buffer. */
2530 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002531
2532exit:
2533 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002534 mbedtls_free( signature );
2535 mbedtls_psa_crypto_free( );
2536}
2537/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002538
2539/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002540void sign_verify( int key_type_arg, data_t *key_data,
2541 int alg_arg, data_t *input_data )
2542{
2543 int slot = 1;
2544 psa_key_type_t key_type = key_type_arg;
2545 psa_algorithm_t alg = alg_arg;
2546 size_t key_bits;
2547 unsigned char *signature = NULL;
2548 size_t signature_size;
2549 size_t signature_length = 0xdeadbeef;
2550 psa_key_policy_t policy;
2551
2552 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2553
2554 psa_key_policy_init( &policy );
2555 psa_key_policy_set_usage( &policy,
2556 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2557 alg );
2558 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2559
2560 TEST_ASSERT( psa_import_key( slot, key_type,
2561 key_data->x,
2562 key_data->len ) == PSA_SUCCESS );
2563 TEST_ASSERT( psa_get_key_information( slot,
2564 NULL,
2565 &key_bits ) == PSA_SUCCESS );
2566
2567 /* Allocate a buffer which has the size advertized by the
2568 * library. */
2569 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2570 key_bits, alg );
2571 TEST_ASSERT( signature_size != 0 );
2572 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002573 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002574
2575 /* Perform the signature. */
2576 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2577 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002578 signature, signature_size,
2579 &signature_length ) == PSA_SUCCESS );
2580 /* Check that the signature length looks sensible. */
2581 TEST_ASSERT( signature_length <= signature_size );
2582 TEST_ASSERT( signature_length > 0 );
2583
2584 /* Use the library to verify that the signature is correct. */
2585 TEST_ASSERT( psa_asymmetric_verify(
2586 slot, alg,
2587 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002588 signature, signature_length ) == PSA_SUCCESS );
2589
2590 if( input_data->len != 0 )
2591 {
2592 /* Flip a bit in the input and verify that the signature is now
2593 * detected as invalid. Flip a bit at the beginning, not at the end,
2594 * because ECDSA may ignore the last few bits of the input. */
2595 input_data->x[0] ^= 1;
2596 TEST_ASSERT( psa_asymmetric_verify(
2597 slot, alg,
2598 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002599 signature,
2600 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2601 }
2602
2603exit:
2604 psa_destroy_key( slot );
2605 mbedtls_free( signature );
2606 mbedtls_psa_crypto_free( );
2607}
2608/* END_CASE */
2609
2610/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002611void asymmetric_verify( int key_type_arg, data_t *key_data,
2612 int alg_arg, data_t *hash_data,
2613 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002614{
2615 int slot = 1;
2616 psa_key_type_t key_type = key_type_arg;
2617 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002618 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002619
Gilles Peskine69c12672018-06-28 00:07:19 +02002620 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2621
itayzafrir5c753392018-05-08 11:18:38 +03002622 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002623 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002624 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002625 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2626 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2627 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002628
2629 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2630
2631 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002632 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002633 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2634
2635 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002636 key_data->x,
2637 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002638
2639 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002640 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002641 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002642 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002643exit:
2644 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002645 mbedtls_psa_crypto_free( );
2646}
2647/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002648
2649/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002650void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2651 int alg_arg, data_t *hash_data,
2652 data_t *signature_data,
2653 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002654{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002655 int slot = 1;
2656 psa_key_type_t key_type = key_type_arg;
2657 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002658 psa_status_t actual_status;
2659 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002660 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002661
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002662 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002663 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002664 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002665 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2666 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2667 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002668
2669 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2670
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002671 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002672 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002673 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2674
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002675 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002676 key_data->x,
2677 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002678
2679 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002680 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002681 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002682 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002683
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002684 TEST_ASSERT( actual_status == expected_status );
2685
2686exit:
2687 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002688 mbedtls_psa_crypto_free( );
2689}
2690/* END_CASE */
2691
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002692/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002693void asymmetric_encrypt( int key_type_arg,
2694 data_t *key_data,
2695 int alg_arg,
2696 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002697 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002698 int expected_output_length_arg,
2699 int expected_status_arg )
2700{
2701 int slot = 1;
2702 psa_key_type_t key_type = key_type_arg;
2703 psa_algorithm_t alg = alg_arg;
2704 size_t expected_output_length = expected_output_length_arg;
2705 size_t key_bits;
2706 unsigned char *output = NULL;
2707 size_t output_size;
2708 size_t output_length = ~0;
2709 psa_status_t actual_status;
2710 psa_status_t expected_status = expected_status_arg;
2711 psa_key_policy_t policy;
2712
2713 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2714
2715 /* Import the key */
2716 psa_key_policy_init( &policy );
2717 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2718 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2719 TEST_ASSERT( psa_import_key( slot, key_type,
2720 key_data->x,
2721 key_data->len ) == PSA_SUCCESS );
2722
2723 /* Determine the maximum output length */
2724 TEST_ASSERT( psa_get_key_information( slot,
2725 NULL,
2726 &key_bits ) == PSA_SUCCESS );
2727 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002728 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002729
2730 /* Encrypt the input */
2731 actual_status = psa_asymmetric_encrypt( slot, alg,
2732 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002733 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002734 output, output_size,
2735 &output_length );
2736 TEST_ASSERT( actual_status == expected_status );
2737 TEST_ASSERT( output_length == expected_output_length );
2738
Gilles Peskine68428122018-06-30 18:42:41 +02002739 /* If the label is empty, the test framework puts a non-null pointer
2740 * in label->x. Test that a null pointer works as well. */
2741 if( label->len == 0 )
2742 {
2743 output_length = ~0;
2744 memset( output, 0, output_size );
2745 actual_status = psa_asymmetric_encrypt( slot, alg,
2746 input_data->x, input_data->len,
2747 NULL, label->len,
2748 output, output_size,
2749 &output_length );
2750 TEST_ASSERT( actual_status == expected_status );
2751 TEST_ASSERT( output_length == expected_output_length );
2752 }
2753
Gilles Peskine656896e2018-06-29 19:12:28 +02002754exit:
2755 psa_destroy_key( slot );
2756 mbedtls_free( output );
2757 mbedtls_psa_crypto_free( );
2758}
2759/* END_CASE */
2760
2761/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002762void asymmetric_encrypt_decrypt( int key_type_arg,
2763 data_t *key_data,
2764 int alg_arg,
2765 data_t *input_data,
2766 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002767{
2768 int slot = 1;
2769 psa_key_type_t key_type = key_type_arg;
2770 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002771 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002772 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002773 size_t output_size;
2774 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002775 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002776 size_t output2_size;
2777 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002778 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002779
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002780 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002781 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002782 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2783 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2784
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002785 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2786
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002787 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002788 psa_key_policy_set_usage( &policy,
2789 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002790 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002791 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2792
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002793 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002794 key_data->x,
2795 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002796
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002797
2798 /* Determine the maximum ciphertext length */
2799 TEST_ASSERT( psa_get_key_information( slot,
2800 NULL,
2801 &key_bits ) == PSA_SUCCESS );
2802 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002803 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002804 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002805 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002806
Gilles Peskineeebd7382018-06-08 18:11:54 +02002807 /* We test encryption by checking that encrypt-then-decrypt gives back
2808 * the original plaintext because of the non-optional random
2809 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002810 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002811 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002812 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002813 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002814 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002815 /* We don't know what ciphertext length to expect, but check that
2816 * it looks sensible. */
2817 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002818
Gilles Peskine2d277862018-06-18 15:41:12 +02002819 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002820 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002821 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002822 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002823 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002824 ASSERT_COMPARE( input_data->x, input_data->len,
2825 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002826
2827exit:
2828 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002829 mbedtls_free( output );
2830 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002831 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002832}
2833/* END_CASE */
2834
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002835/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002836void asymmetric_decrypt( int key_type_arg,
2837 data_t *key_data,
2838 int alg_arg,
2839 data_t *input_data,
2840 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002841 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002842{
2843 int slot = 1;
2844 psa_key_type_t key_type = key_type_arg;
2845 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002846 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002847 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002848 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002849 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002850
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002851 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002852 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002853 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002854 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2855 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2856 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2857
Gilles Peskine4abf7412018-06-18 16:35:34 +02002858 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002859 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002860
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002861 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2862
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002863 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002864 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002865 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2866
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002867 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002868 key_data->x,
2869 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002870
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002871 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002872 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002873 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002874 output,
2875 output_size,
2876 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002877 ASSERT_COMPARE( expected_data->x, expected_data->len,
2878 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002879
Gilles Peskine68428122018-06-30 18:42:41 +02002880 /* If the label is empty, the test framework puts a non-null pointer
2881 * in label->x. Test that a null pointer works as well. */
2882 if( label->len == 0 )
2883 {
2884 output_length = ~0;
2885 memset( output, 0, output_size );
2886 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2887 input_data->x, input_data->len,
2888 NULL, label->len,
2889 output,
2890 output_size,
2891 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002892 ASSERT_COMPARE( expected_data->x, expected_data->len,
2893 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02002894 }
2895
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002896exit:
2897 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002898 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002899 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002900}
2901/* END_CASE */
2902
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002903/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002904void asymmetric_decrypt_fail( int key_type_arg,
2905 data_t *key_data,
2906 int alg_arg,
2907 data_t *input_data,
2908 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002909 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002910{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002911 int slot = 1;
2912 psa_key_type_t key_type = key_type_arg;
2913 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002914 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002915 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002916 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002917 psa_status_t actual_status;
2918 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002919 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002920
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002921 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002922 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002923 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2924 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2925
Gilles Peskine4abf7412018-06-18 16:35:34 +02002926 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002927 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002928
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002929 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2930
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002931 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002932 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002933 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2934
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002935 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002936 key_data->x,
2937 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002938
Gilles Peskine2d277862018-06-18 15:41:12 +02002939 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002940 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002941 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002942 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002943 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002944 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002945 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002946
Gilles Peskine68428122018-06-30 18:42:41 +02002947 /* If the label is empty, the test framework puts a non-null pointer
2948 * in label->x. Test that a null pointer works as well. */
2949 if( label->len == 0 )
2950 {
2951 output_length = ~0;
2952 memset( output, 0, output_size );
2953 actual_status = psa_asymmetric_decrypt( slot, alg,
2954 input_data->x, input_data->len,
2955 NULL, label->len,
2956 output, output_size,
2957 &output_length );
2958 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002959 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002960 }
2961
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002962exit:
2963 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002964 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002965 mbedtls_psa_crypto_free( );
2966}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002967/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002968
2969/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002970void derive_setup( int key_type_arg,
2971 data_t *key_data,
2972 int alg_arg,
2973 data_t *salt,
2974 data_t *label,
2975 int requested_capacity_arg,
2976 int expected_status_arg )
2977{
2978 psa_key_slot_t slot = 1;
2979 size_t key_type = key_type_arg;
2980 psa_algorithm_t alg = alg_arg;
2981 size_t requested_capacity = requested_capacity_arg;
2982 psa_status_t expected_status = expected_status_arg;
2983 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2984 psa_key_policy_t policy;
2985
2986 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2987
2988 psa_key_policy_init( &policy );
2989 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2990 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2991
2992 TEST_ASSERT( psa_import_key( slot, key_type,
2993 key_data->x,
2994 key_data->len ) == PSA_SUCCESS );
2995
2996 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2997 salt->x, salt->len,
2998 label->x, label->len,
2999 requested_capacity ) == expected_status );
3000
3001exit:
3002 psa_generator_abort( &generator );
3003 psa_destroy_key( slot );
3004 mbedtls_psa_crypto_free( );
3005}
3006/* END_CASE */
3007
3008/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003009void derive_output( int alg_arg,
3010 data_t *key_data,
3011 data_t *salt,
3012 data_t *label,
3013 int requested_capacity_arg,
3014 data_t *expected_output1,
3015 data_t *expected_output2 )
3016{
3017 psa_key_slot_t slot = 1;
3018 psa_algorithm_t alg = alg_arg;
3019 size_t requested_capacity = requested_capacity_arg;
3020 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3021 uint8_t *expected_outputs[2] =
3022 {expected_output1->x, expected_output2->x};
3023 size_t output_sizes[2] =
3024 {expected_output1->len, expected_output2->len};
3025 size_t output_buffer_size = 0;
3026 uint8_t *output_buffer = NULL;
3027 size_t expected_capacity;
3028 size_t current_capacity;
3029 psa_key_policy_t policy;
3030 psa_status_t status;
3031 unsigned i;
3032
3033 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3034 {
3035 if( output_sizes[i] > output_buffer_size )
3036 output_buffer_size = output_sizes[i];
3037 if( output_sizes[i] == 0 )
3038 expected_outputs[i] = NULL;
3039 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003040 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003041 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3042
3043 psa_key_policy_init( &policy );
3044 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3045 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3046
3047 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3048 key_data->x,
3049 key_data->len ) == PSA_SUCCESS );
3050
3051 /* Extraction phase. */
3052 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3053 salt->x, salt->len,
3054 label->x, label->len,
3055 requested_capacity ) == PSA_SUCCESS );
3056 TEST_ASSERT( psa_get_generator_capacity( &generator,
3057 &current_capacity ) ==
3058 PSA_SUCCESS );
3059 TEST_ASSERT( current_capacity == requested_capacity );
3060 expected_capacity = requested_capacity;
3061
3062 /* Expansion phase. */
3063 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3064 {
3065 /* Read some bytes. */
3066 status = psa_generator_read( &generator,
3067 output_buffer, output_sizes[i] );
3068 if( expected_capacity == 0 && output_sizes[i] == 0 )
3069 {
3070 /* Reading 0 bytes when 0 bytes are available can go either way. */
3071 TEST_ASSERT( status == PSA_SUCCESS ||
3072 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3073 continue;
3074 }
3075 else if( expected_capacity == 0 ||
3076 output_sizes[i] > expected_capacity )
3077 {
3078 /* Capacity exceeded. */
3079 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3080 expected_capacity = 0;
3081 continue;
3082 }
3083 /* Success. Check the read data. */
3084 TEST_ASSERT( status == PSA_SUCCESS );
3085 if( output_sizes[i] != 0 )
3086 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3087 output_sizes[i] ) == 0 );
3088 /* Check the generator status. */
3089 expected_capacity -= output_sizes[i];
3090 TEST_ASSERT( psa_get_generator_capacity( &generator,
3091 &current_capacity ) ==
3092 PSA_SUCCESS );
3093 TEST_ASSERT( expected_capacity == current_capacity );
3094 }
3095 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3096
3097exit:
3098 mbedtls_free( output_buffer );
3099 psa_generator_abort( &generator );
3100 psa_destroy_key( slot );
3101 mbedtls_psa_crypto_free( );
3102}
3103/* END_CASE */
3104
3105/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003106void derive_full( int alg_arg,
3107 data_t *key_data,
3108 data_t *salt,
3109 data_t *label,
3110 int requested_capacity_arg )
3111{
3112 psa_key_slot_t slot = 1;
3113 psa_algorithm_t alg = alg_arg;
3114 size_t requested_capacity = requested_capacity_arg;
3115 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3116 unsigned char output_buffer[16];
3117 size_t expected_capacity = requested_capacity;
3118 size_t current_capacity;
3119 psa_key_policy_t policy;
3120
3121 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3122
3123 psa_key_policy_init( &policy );
3124 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3125 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3126
3127 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3128 key_data->x,
3129 key_data->len ) == PSA_SUCCESS );
3130
3131 /* Extraction phase. */
3132 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3133 salt->x, salt->len,
3134 label->x, label->len,
3135 requested_capacity ) == PSA_SUCCESS );
3136 TEST_ASSERT( psa_get_generator_capacity( &generator,
3137 &current_capacity ) ==
3138 PSA_SUCCESS );
3139 TEST_ASSERT( current_capacity == expected_capacity );
3140
3141 /* Expansion phase. */
3142 while( current_capacity > 0 )
3143 {
3144 size_t read_size = sizeof( output_buffer );
3145 if( read_size > current_capacity )
3146 read_size = current_capacity;
3147 TEST_ASSERT( psa_generator_read( &generator,
3148 output_buffer,
3149 read_size ) == PSA_SUCCESS );
3150 expected_capacity -= read_size;
3151 TEST_ASSERT( psa_get_generator_capacity( &generator,
3152 &current_capacity ) ==
3153 PSA_SUCCESS );
3154 TEST_ASSERT( current_capacity == expected_capacity );
3155 }
3156
3157 /* Check that the generator refuses to go over capacity. */
3158 TEST_ASSERT( psa_generator_read( &generator,
3159 output_buffer,
3160 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3161
3162 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3163
3164exit:
3165 psa_generator_abort( &generator );
3166 psa_destroy_key( slot );
3167 mbedtls_psa_crypto_free( );
3168}
3169/* END_CASE */
3170
3171/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003172void derive_key_exercise( int alg_arg,
3173 data_t *key_data,
3174 data_t *salt,
3175 data_t *label,
3176 int derived_type_arg,
3177 int derived_bits_arg,
3178 int derived_usage_arg,
3179 int derived_alg_arg )
3180{
3181 psa_key_slot_t base_key = 1;
3182 psa_key_slot_t derived_key = 2;
3183 psa_algorithm_t alg = alg_arg;
3184 psa_key_type_t derived_type = derived_type_arg;
3185 size_t derived_bits = derived_bits_arg;
3186 psa_key_usage_t derived_usage = derived_usage_arg;
3187 psa_algorithm_t derived_alg = derived_alg_arg;
3188 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3189 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3190 psa_key_policy_t policy;
3191 psa_key_type_t got_type;
3192 size_t got_bits;
3193
3194 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3195
3196 psa_key_policy_init( &policy );
3197 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3198 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3199 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3200 key_data->x,
3201 key_data->len ) == PSA_SUCCESS );
3202
3203 /* Derive a key. */
3204 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3205 salt->x, salt->len,
3206 label->x, label->len,
3207 capacity ) == PSA_SUCCESS );
3208 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3209 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3210 TEST_ASSERT( psa_generator_import_key( derived_key,
3211 derived_type,
3212 derived_bits,
3213 &generator ) == PSA_SUCCESS );
3214
3215 /* Test the key information */
3216 TEST_ASSERT( psa_get_key_information( derived_key,
3217 &got_type,
3218 &got_bits ) == PSA_SUCCESS );
3219 TEST_ASSERT( got_type == derived_type );
3220 TEST_ASSERT( got_bits == derived_bits );
3221
3222 /* Exercise the derived key. */
3223 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3224 goto exit;
3225
3226exit:
3227 psa_generator_abort( &generator );
3228 psa_destroy_key( base_key );
3229 psa_destroy_key( derived_key );
3230 mbedtls_psa_crypto_free( );
3231}
3232/* END_CASE */
3233
3234/* BEGIN_CASE */
3235void derive_key_export( int alg_arg,
3236 data_t *key_data,
3237 data_t *salt,
3238 data_t *label,
3239 int bytes1_arg,
3240 int bytes2_arg )
3241{
3242 psa_key_slot_t base_key = 1;
3243 psa_key_slot_t derived_key = 2;
3244 psa_algorithm_t alg = alg_arg;
3245 size_t bytes1 = bytes1_arg;
3246 size_t bytes2 = bytes2_arg;
3247 size_t capacity = bytes1 + bytes2;
3248 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003249 uint8_t *output_buffer = NULL;
3250 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003251 psa_key_policy_t policy;
3252 size_t length;
3253
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003254 ASSERT_ALLOC( output_buffer, capacity );
3255 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003256 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3257
3258 psa_key_policy_init( &policy );
3259 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3260 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3261 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3262 key_data->x,
3263 key_data->len ) == PSA_SUCCESS );
3264
3265 /* Derive some material and output it. */
3266 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3267 salt->x, salt->len,
3268 label->x, label->len,
3269 capacity ) == PSA_SUCCESS );
3270 TEST_ASSERT( psa_generator_read( &generator,
3271 output_buffer,
3272 capacity ) == PSA_SUCCESS );
3273 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3274
3275 /* Derive the same output again, but this time store it in key objects. */
3276 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3277 salt->x, salt->len,
3278 label->x, label->len,
3279 capacity ) == PSA_SUCCESS );
3280 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3281 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3282 TEST_ASSERT( psa_generator_import_key( derived_key,
3283 PSA_KEY_TYPE_RAW_DATA,
3284 PSA_BYTES_TO_BITS( bytes1 ),
3285 &generator ) == PSA_SUCCESS );
3286 TEST_ASSERT( psa_export_key( derived_key,
3287 export_buffer, bytes1,
3288 &length ) == PSA_SUCCESS );
3289 TEST_ASSERT( length == bytes1 );
3290 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3291 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3292 TEST_ASSERT( psa_generator_import_key( derived_key,
3293 PSA_KEY_TYPE_RAW_DATA,
3294 PSA_BYTES_TO_BITS( bytes2 ),
3295 &generator ) == PSA_SUCCESS );
3296 TEST_ASSERT( psa_export_key( derived_key,
3297 export_buffer + bytes1, bytes2,
3298 &length ) == PSA_SUCCESS );
3299 TEST_ASSERT( length == bytes2 );
3300
3301 /* Compare the outputs from the two runs. */
3302 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3303
3304exit:
3305 mbedtls_free( output_buffer );
3306 mbedtls_free( export_buffer );
3307 psa_generator_abort( &generator );
3308 psa_destroy_key( base_key );
3309 psa_destroy_key( derived_key );
3310 mbedtls_psa_crypto_free( );
3311}
3312/* END_CASE */
3313
3314/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003315void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003316{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003317 size_t bytes = bytes_arg;
3318 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003319 unsigned char *output = NULL;
3320 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003321 size_t i;
3322 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003323
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003324 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3325 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003326 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003327
3328 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3329
Gilles Peskinea50d7392018-06-21 10:22:13 +02003330 /* Run several times, to ensure that every output byte will be
3331 * nonzero at least once with overwhelming probability
3332 * (2^(-8*number_of_runs)). */
3333 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003334 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02003335 memset( output, 0, bytes );
3336 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3337
3338 /* Check that no more than bytes have been overwritten */
3339 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3340
3341 for( i = 0; i < bytes; i++ )
3342 {
3343 if( output[i] != 0 )
3344 ++changed[i];
3345 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003346 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003347
3348 /* Check that every byte was changed to nonzero at least once. This
3349 * validates that psa_generate_random is overwriting every byte of
3350 * the output buffer. */
3351 for( i = 0; i < bytes; i++ )
3352 {
3353 TEST_ASSERT( changed[i] != 0 );
3354 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003355
3356exit:
3357 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003358 mbedtls_free( output );
3359 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003360}
3361/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003362
3363/* BEGIN_CASE */
3364void generate_key( int type_arg,
3365 int bits_arg,
3366 int usage_arg,
3367 int alg_arg,
3368 int expected_status_arg )
3369{
3370 int slot = 1;
3371 psa_key_type_t type = type_arg;
3372 psa_key_usage_t usage = usage_arg;
3373 size_t bits = bits_arg;
3374 psa_algorithm_t alg = alg_arg;
3375 psa_status_t expected_status = expected_status_arg;
3376 psa_key_type_t got_type;
3377 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003378 psa_status_t expected_info_status =
3379 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3380 psa_key_policy_t policy;
3381
3382 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3383
3384 psa_key_policy_init( &policy );
3385 psa_key_policy_set_usage( &policy, usage, alg );
3386 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3387
3388 /* Generate a key */
3389 TEST_ASSERT( psa_generate_key( slot, type, bits,
3390 NULL, 0 ) == expected_status );
3391
3392 /* Test the key information */
3393 TEST_ASSERT( psa_get_key_information( slot,
3394 &got_type,
3395 &got_bits ) == expected_info_status );
3396 if( expected_info_status != PSA_SUCCESS )
3397 goto exit;
3398 TEST_ASSERT( got_type == type );
3399 TEST_ASSERT( got_bits == bits );
3400
Gilles Peskine818ca122018-06-20 18:16:48 +02003401 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003402 if( ! exercise_key( slot, usage, alg ) )
3403 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003404
3405exit:
3406 psa_destroy_key( slot );
3407 mbedtls_psa_crypto_free( );
3408}
3409/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003410
3411/* BEGIN_CASE */
3412void validate_module_init_generate_random( )
3413{
3414 psa_status_t status;
3415 uint8_t random[10] = { 0 };
3416 status = psa_generate_random( random, sizeof( random ) );
3417 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3418}
3419/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003420
3421/* BEGIN_CASE */
3422void validate_module_init_key_based( )
3423{
3424 psa_status_t status;
3425 uint8_t data[10] = { 0 };
3426 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3427 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3428}
3429/* END_CASE */