blob: 4692dbe84e3d434cae3bb38bf6d4f8380bc2e087 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskinefc411f12018-10-25 22:34:48 +020014#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
15
Gilles Peskine96ee5c72018-07-12 17:24:54 +020016#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
17
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#else
Gilles Peskine2d277862018-06-18 15:41:12 +020021#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030022#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023
Jaeden Amerof24c7f82018-06-27 17:20:43 +010024/** An invalid export length that will never be set by psa_export_key(). */
25static const size_t INVALID_EXPORT_LENGTH = ~0U;
26
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027/** Test if a buffer contains a constant byte value.
28 *
29 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020030 *
31 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020032 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 * \param size Size of the buffer in bytes.
34 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020035 * \return 1 if the buffer is all-bits-zero.
36 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039{
40 size_t i;
41 for( i = 0; i < size; i++ )
42 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020043 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020046 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020047}
Gilles Peskine818ca122018-06-20 18:16:48 +020048
Gilles Peskine0b352bc2018-06-28 00:16:11 +020049/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
50static int asn1_write_10x( unsigned char **p,
51 unsigned char *start,
52 size_t bits,
53 unsigned char x )
54{
55 int ret;
56 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020057 if( bits == 0 )
58 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
59 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030061 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020062 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
63 *p -= len;
64 ( *p )[len-1] = x;
65 if( bits % 8 == 0 )
66 ( *p )[1] |= 1;
67 else
68 ( *p )[0] |= 1 << ( bits % 8 );
69 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
70 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
71 MBEDTLS_ASN1_INTEGER ) );
72 return( len );
73}
74
75static int construct_fake_rsa_key( unsigned char *buffer,
76 size_t buffer_size,
77 unsigned char **p,
78 size_t bits,
79 int keypair )
80{
81 size_t half_bits = ( bits + 1 ) / 2;
82 int ret;
83 int len = 0;
84 /* Construct something that looks like a DER encoding of
85 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
86 * RSAPrivateKey ::= SEQUENCE {
87 * version Version,
88 * modulus INTEGER, -- n
89 * publicExponent INTEGER, -- e
90 * privateExponent INTEGER, -- d
91 * prime1 INTEGER, -- p
92 * prime2 INTEGER, -- q
93 * exponent1 INTEGER, -- d mod (p-1)
94 * exponent2 INTEGER, -- d mod (q-1)
95 * coefficient INTEGER, -- (inverse of q) mod p
96 * otherPrimeInfos OtherPrimeInfos OPTIONAL
97 * }
98 * Or, for a public key, the same structure with only
99 * version, modulus and publicExponent.
100 */
101 *p = buffer + buffer_size;
102 if( keypair )
103 {
104 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* q */
111 asn1_write_10x( p, buffer, half_bits, 1 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
113 asn1_write_10x( p, buffer, half_bits, 3 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* d */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 }
117 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
118 asn1_write_10x( p, buffer, 17, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* n */
120 asn1_write_10x( p, buffer, bits, 1 ) );
121 if( keypair )
122 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
123 mbedtls_asn1_write_int( p, buffer, 0 ) );
124 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
125 {
126 const unsigned char tag =
127 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
128 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
129 }
130 return( len );
131}
132
Gilles Peskine818ca122018-06-20 18:16:48 +0200133static int exercise_mac_key( psa_key_slot_t key,
134 psa_key_usage_t usage,
135 psa_algorithm_t alg )
136{
137 psa_mac_operation_t operation;
138 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200139 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200140 size_t mac_length = sizeof( mac );
141
142 if( usage & PSA_KEY_USAGE_SIGN )
143 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200144 TEST_ASSERT( psa_mac_sign_setup( &operation,
145 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200146 TEST_ASSERT( psa_mac_update( &operation,
147 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200148 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200149 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200150 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200151 }
152
153 if( usage & PSA_KEY_USAGE_VERIFY )
154 {
155 psa_status_t verify_status =
156 ( usage & PSA_KEY_USAGE_SIGN ?
157 PSA_SUCCESS :
158 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200159 TEST_ASSERT( psa_mac_verify_setup( &operation,
160 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200161 TEST_ASSERT( psa_mac_update( &operation,
162 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200163 TEST_ASSERT( psa_mac_verify_finish( &operation,
164 mac,
165 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200166 }
167
168 return( 1 );
169
170exit:
171 psa_mac_abort( &operation );
172 return( 0 );
173}
174
175static int exercise_cipher_key( psa_key_slot_t key,
176 psa_key_usage_t usage,
177 psa_algorithm_t alg )
178{
179 psa_cipher_operation_t operation;
180 unsigned char iv[16] = {0};
181 size_t iv_length = sizeof( iv );
182 const unsigned char plaintext[16] = "Hello, world...";
183 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
184 size_t ciphertext_length = sizeof( ciphertext );
185 unsigned char decrypted[sizeof( ciphertext )];
186 size_t part_length;
187
188 if( usage & PSA_KEY_USAGE_ENCRYPT )
189 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200190 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
191 key, alg ) == PSA_SUCCESS );
192 TEST_ASSERT( psa_cipher_generate_iv( &operation,
193 iv, sizeof( iv ),
194 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200195 TEST_ASSERT( psa_cipher_update( &operation,
196 plaintext, sizeof( plaintext ),
197 ciphertext, sizeof( ciphertext ),
198 &ciphertext_length ) == PSA_SUCCESS );
199 TEST_ASSERT( psa_cipher_finish( &operation,
200 ciphertext + ciphertext_length,
201 sizeof( ciphertext ) - ciphertext_length,
202 &part_length ) == PSA_SUCCESS );
203 ciphertext_length += part_length;
204 }
205
206 if( usage & PSA_KEY_USAGE_DECRYPT )
207 {
208 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700209 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200210 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
211 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200212 size_t bits;
213 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
214 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
215 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200216 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
217 key, alg ) == PSA_SUCCESS );
218 TEST_ASSERT( psa_cipher_set_iv( &operation,
219 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200220 TEST_ASSERT( psa_cipher_update( &operation,
221 ciphertext, ciphertext_length,
222 decrypted, sizeof( decrypted ),
223 &part_length ) == PSA_SUCCESS );
224 status = psa_cipher_finish( &operation,
225 decrypted + part_length,
226 sizeof( decrypted ) - part_length,
227 &part_length );
228 /* For a stream cipher, all inputs are valid. For a block cipher,
229 * if the input is some aribtrary data rather than an actual
230 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700231 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700232 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200233 TEST_ASSERT( status == PSA_SUCCESS );
234 else
235 TEST_ASSERT( status == PSA_SUCCESS ||
236 status == PSA_ERROR_INVALID_PADDING );
237 }
238
239 return( 1 );
240
241exit:
242 psa_cipher_abort( &operation );
243 return( 0 );
244}
245
246static int exercise_aead_key( psa_key_slot_t key,
247 psa_key_usage_t usage,
248 psa_algorithm_t alg )
249{
250 unsigned char nonce[16] = {0};
251 size_t nonce_length = sizeof( nonce );
252 unsigned char plaintext[16] = "Hello, world...";
253 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
254 size_t ciphertext_length = sizeof( ciphertext );
255 size_t plaintext_length = sizeof( ciphertext );
256
257 if( usage & PSA_KEY_USAGE_ENCRYPT )
258 {
259 TEST_ASSERT( psa_aead_encrypt( key, alg,
260 nonce, nonce_length,
261 NULL, 0,
262 plaintext, sizeof( plaintext ),
263 ciphertext, sizeof( ciphertext ),
264 &ciphertext_length ) == PSA_SUCCESS );
265 }
266
267 if( usage & PSA_KEY_USAGE_DECRYPT )
268 {
269 psa_status_t verify_status =
270 ( usage & PSA_KEY_USAGE_ENCRYPT ?
271 PSA_SUCCESS :
272 PSA_ERROR_INVALID_SIGNATURE );
273 TEST_ASSERT( psa_aead_decrypt( key, alg,
274 nonce, nonce_length,
275 NULL, 0,
276 ciphertext, ciphertext_length,
277 plaintext, sizeof( plaintext ),
278 &plaintext_length ) == verify_status );
279 }
280
281 return( 1 );
282
283exit:
284 return( 0 );
285}
286
287static int exercise_signature_key( psa_key_slot_t key,
288 psa_key_usage_t usage,
289 psa_algorithm_t alg )
290{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200291 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
292 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200293 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200294 size_t signature_length = sizeof( signature );
295
296 if( usage & PSA_KEY_USAGE_SIGN )
297 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200298 /* Some algorithms require the payload to have the size of
299 * the hash encoded in the algorithm. Use this input size
300 * even for algorithms that allow other input sizes. */
301 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
302 if( hash_alg != 0 )
303 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200304 TEST_ASSERT( psa_asymmetric_sign( key, alg,
305 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 signature, sizeof( signature ),
307 &signature_length ) == PSA_SUCCESS );
308 }
309
310 if( usage & PSA_KEY_USAGE_VERIFY )
311 {
312 psa_status_t verify_status =
313 ( usage & PSA_KEY_USAGE_SIGN ?
314 PSA_SUCCESS :
315 PSA_ERROR_INVALID_SIGNATURE );
316 TEST_ASSERT( psa_asymmetric_verify( key, alg,
317 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200318 signature, signature_length ) ==
319 verify_status );
320 }
321
322 return( 1 );
323
324exit:
325 return( 0 );
326}
327
328static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
329 psa_key_usage_t usage,
330 psa_algorithm_t alg )
331{
332 unsigned char plaintext[256] = "Hello, world...";
333 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
334 size_t ciphertext_length = sizeof( ciphertext );
335 size_t plaintext_length = 16;
336
337 if( usage & PSA_KEY_USAGE_ENCRYPT )
338 {
339 TEST_ASSERT(
340 psa_asymmetric_encrypt( key, alg,
341 plaintext, plaintext_length,
342 NULL, 0,
343 ciphertext, sizeof( ciphertext ),
344 &ciphertext_length ) == PSA_SUCCESS );
345 }
346
347 if( usage & PSA_KEY_USAGE_DECRYPT )
348 {
349 psa_status_t status =
350 psa_asymmetric_decrypt( key, alg,
351 ciphertext, ciphertext_length,
352 NULL, 0,
353 plaintext, sizeof( plaintext ),
354 &plaintext_length );
355 TEST_ASSERT( status == PSA_SUCCESS ||
356 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
357 ( status == PSA_ERROR_INVALID_ARGUMENT ||
358 status == PSA_ERROR_INVALID_PADDING ) ) );
359 }
360
361 return( 1 );
362
363exit:
364 return( 0 );
365}
Gilles Peskine02b75072018-07-01 22:31:34 +0200366
Gilles Peskineea0fb492018-07-12 17:17:20 +0200367static int exercise_key_derivation_key( psa_key_slot_t key,
368 psa_key_usage_t usage,
369 psa_algorithm_t alg )
370{
371 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
372 unsigned char label[16] = "This is a label.";
373 size_t label_length = sizeof( label );
374 unsigned char seed[16] = "abcdefghijklmnop";
375 size_t seed_length = sizeof( seed );
376 unsigned char output[1];
377
378 if( usage & PSA_KEY_USAGE_DERIVE )
379 {
380 TEST_ASSERT( psa_key_derivation( &generator,
381 key, alg,
382 label, label_length,
383 seed, seed_length,
384 sizeof( output ) ) == PSA_SUCCESS );
385 TEST_ASSERT( psa_generator_read( &generator,
386 output,
387 sizeof( output ) ) == PSA_SUCCESS );
388 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
389 }
390
391 return( 1 );
392
393exit:
394 return( 0 );
395}
396
Gilles Peskinec7998b72018-11-07 18:45:02 +0100397/* We need two keys to exercise key agreement. Exercise the
398 * private key against its own public key. */
399static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
400 psa_key_type_t key_slot,
401 psa_algorithm_t alg )
402{
403 psa_key_type_t private_key_type;
404 psa_key_type_t public_key_type;
405 size_t key_bits;
406 uint8_t *public_key = NULL;
407 size_t public_key_length;
408 /* Return UNKNOWN_ERROR if something other than the final call to
409 * psa_key_agreement fails. This isn't fully satisfactory, but it's
410 * good enough: callers will report it as a failed test anyway. */
411 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
412
413 TEST_ASSERT( psa_get_key_information( key_slot,
414 &private_key_type,
415 &key_bits ) == PSA_SUCCESS );
416 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
417 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
418 ASSERT_ALLOC( public_key, public_key_length );
419 TEST_ASSERT( public_key != NULL );
420 TEST_ASSERT( psa_export_public_key( key_slot,
421 public_key, public_key_length,
422 &public_key_length ) == PSA_SUCCESS );
423
424 status = psa_key_agreement( generator, key_slot,
425 public_key, public_key_length,
426 alg );
427exit:
428 mbedtls_free( public_key );
429 return( status );
430}
431
Gilles Peskine01d718c2018-09-18 12:01:02 +0200432static int exercise_key_agreement_key( psa_key_slot_t key,
433 psa_key_usage_t usage,
434 psa_algorithm_t alg )
435{
436 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200437 unsigned char output[1];
438 int ok = 0;
439
440 if( usage & PSA_KEY_USAGE_DERIVE )
441 {
442 /* We need two keys to exercise key agreement. Exercise the
443 * private key against its own public key. */
Gilles Peskinec7998b72018-11-07 18:45:02 +0100444 TEST_ASSERT( key_agreement_with_self( &generator, key, alg ) ==
445 PSA_SUCCESS );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200446 TEST_ASSERT( psa_generator_read( &generator,
447 output,
448 sizeof( output ) ) == PSA_SUCCESS );
449 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
450 }
451 ok = 1;
452
453exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200454 return( ok );
455}
456
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200457static int is_oid_of_key_type( psa_key_type_t type,
458 const uint8_t *oid, size_t oid_length )
459{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200460 const uint8_t *expected_oid = NULL;
461 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200462#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200463 if( PSA_KEY_TYPE_IS_RSA( type ) )
464 {
465 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
466 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
467 }
468 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200469#endif /* MBEDTLS_RSA_C */
470#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200471 if( PSA_KEY_TYPE_IS_ECC( type ) )
472 {
473 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
474 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
475 }
476 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200477#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200478 {
479 char message[40];
480 mbedtls_snprintf( message, sizeof( message ),
481 "OID not known for key type=0x%08lx",
482 (unsigned long) type );
483 test_fail( message, __LINE__, __FILE__ );
484 return( 0 );
485 }
486
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200487 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200488 return( 1 );
489
490exit:
491 return( 0 );
492}
493
494static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
495 size_t min_bits, size_t max_bits,
496 int must_be_odd )
497{
498 size_t len;
499 size_t actual_bits;
500 unsigned char msb;
501 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
502 MBEDTLS_ASN1_INTEGER ) == 0 );
503 /* Tolerate a slight departure from DER encoding:
504 * - 0 may be represented by an empty string or a 1-byte string.
505 * - The sign bit may be used as a value bit. */
506 if( ( len == 1 && ( *p )[0] == 0 ) ||
507 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
508 {
509 ++( *p );
510 --len;
511 }
512 if( min_bits == 0 && len == 0 )
513 return( 1 );
514 msb = ( *p )[0];
515 TEST_ASSERT( msb != 0 );
516 actual_bits = 8 * ( len - 1 );
517 while( msb != 0 )
518 {
519 msb >>= 1;
520 ++actual_bits;
521 }
522 TEST_ASSERT( actual_bits >= min_bits );
523 TEST_ASSERT( actual_bits <= max_bits );
524 if( must_be_odd )
525 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
526 *p += len;
527 return( 1 );
528exit:
529 return( 0 );
530}
531
532static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
533 size_t *len,
534 unsigned char n, unsigned char tag )
535{
536 int ret;
537 ret = mbedtls_asn1_get_tag( p, end, len,
538 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
539 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
540 if( ret != 0 )
541 return( ret );
542 end = *p + *len;
543 ret = mbedtls_asn1_get_tag( p, end, len, tag );
544 if( ret != 0 )
545 return( ret );
546 if( *p + *len != end )
547 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
548 return( 0 );
549}
550
551static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
552 uint8_t *exported, size_t exported_length )
553{
554 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200555 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200556 else
557 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200558
559#if defined(MBEDTLS_DES_C)
560 if( type == PSA_KEY_TYPE_DES )
561 {
562 /* Check the parity bits. */
563 unsigned i;
564 for( i = 0; i < bits / 8; i++ )
565 {
566 unsigned bit_count = 0;
567 unsigned m;
568 for( m = 1; m <= 0x100; m <<= 1 )
569 {
570 if( exported[i] & m )
571 ++bit_count;
572 }
573 TEST_ASSERT( bit_count % 2 != 0 );
574 }
575 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200576 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200577#endif
578
579#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
580 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
581 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200582 uint8_t *p = exported;
583 uint8_t *end = exported + exported_length;
584 size_t len;
585 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200586 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200587 * modulus INTEGER, -- n
588 * publicExponent INTEGER, -- e
589 * privateExponent INTEGER, -- d
590 * prime1 INTEGER, -- p
591 * prime2 INTEGER, -- q
592 * exponent1 INTEGER, -- d mod (p-1)
593 * exponent2 INTEGER, -- d mod (q-1)
594 * coefficient INTEGER, -- (inverse of q) mod p
595 * }
596 */
597 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
598 MBEDTLS_ASN1_SEQUENCE |
599 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
600 TEST_ASSERT( p + len == end );
601 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
604 goto exit;
605 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
606 goto exit;
607 /* Require d to be at least half the size of n. */
608 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
609 goto exit;
610 /* Require p and q to be at most half the size of n, rounded up. */
611 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
612 goto exit;
613 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
614 goto exit;
615 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
616 goto exit;
617 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
618 goto exit;
619 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
620 goto exit;
621 TEST_ASSERT( p == end );
622 }
623 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200624#endif /* MBEDTLS_RSA_C */
625
626#if defined(MBEDTLS_ECP_C)
627 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
628 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100629 /* Just the secret value */
630 TEST_ASSERT( exported_length == PSA_BITS_TO_BYTES( bits ) );
631 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200632 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200633#endif /* MBEDTLS_ECP_C */
634
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200635 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
636 {
637 uint8_t *p = exported;
638 uint8_t *end = exported + exported_length;
639 size_t len;
640 mbedtls_asn1_buf alg;
641 mbedtls_asn1_buf params;
642 mbedtls_asn1_bitstring bitstring;
643 /* SubjectPublicKeyInfo ::= SEQUENCE {
644 * algorithm AlgorithmIdentifier,
645 * subjectPublicKey BIT STRING }
646 * AlgorithmIdentifier ::= SEQUENCE {
647 * algorithm OBJECT IDENTIFIER,
648 * parameters ANY DEFINED BY algorithm OPTIONAL }
649 */
650 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
651 MBEDTLS_ASN1_SEQUENCE |
652 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
653 TEST_ASSERT( p + len == end );
654 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
655 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
656 goto exit;
657 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
658 TEST_ASSERT( p == end );
659 p = bitstring.p;
660#if defined(MBEDTLS_RSA_C)
661 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
662 {
663 /* RSAPublicKey ::= SEQUENCE {
664 * modulus INTEGER, -- n
665 * publicExponent INTEGER } -- e
666 */
667 TEST_ASSERT( bitstring.unused_bits == 0 );
668 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
669 MBEDTLS_ASN1_SEQUENCE |
670 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
671 TEST_ASSERT( p + len == end );
672 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
673 goto exit;
674 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
675 goto exit;
676 TEST_ASSERT( p == end );
677 }
678 else
679#endif /* MBEDTLS_RSA_C */
680#if defined(MBEDTLS_ECP_C)
681 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
682 {
683 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200684 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200685 * -- then x_P as an n-bit string, big endian;
686 * -- then y_P as a n-bit string, big endian,
687 * -- where n is the order of the curve.
688 */
689 TEST_ASSERT( bitstring.unused_bits == 0 );
690 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
691 TEST_ASSERT( p[0] == 4 );
692 }
693 else
694#endif /* MBEDTLS_ECP_C */
695 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100696 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200697 mbedtls_snprintf( message, sizeof( message ),
698 "No sanity check for public key type=0x%08lx",
699 (unsigned long) type );
700 test_fail( message, __LINE__, __FILE__ );
701 return( 0 );
702 }
703 }
704 else
705
706 {
707 /* No sanity checks for other types */
708 }
709
710 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200711
712exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200713 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200714}
715
716static int exercise_export_key( psa_key_slot_t slot,
717 psa_key_usage_t usage )
718{
719 psa_key_type_t type;
720 size_t bits;
721 uint8_t *exported = NULL;
722 size_t exported_size = 0;
723 size_t exported_length = 0;
724 int ok = 0;
725
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200726 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
727
728 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
729 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200730 {
731 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
732 PSA_ERROR_NOT_PERMITTED );
733 return( 1 );
734 }
735
Gilles Peskined14664a2018-08-10 19:07:32 +0200736 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200737 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200738
739 TEST_ASSERT( psa_export_key( slot,
740 exported, exported_size,
741 &exported_length ) == PSA_SUCCESS );
742 ok = exported_key_sanity_check( type, bits, exported, exported_length );
743
744exit:
745 mbedtls_free( exported );
746 return( ok );
747}
748
749static int exercise_export_public_key( psa_key_slot_t slot )
750{
751 psa_key_type_t type;
752 psa_key_type_t public_type;
753 size_t bits;
754 uint8_t *exported = NULL;
755 size_t exported_size = 0;
756 size_t exported_length = 0;
757 int ok = 0;
758
759 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
760 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
761 {
762 TEST_ASSERT( psa_export_public_key( slot,
763 NULL, 0, &exported_length ) ==
764 PSA_ERROR_INVALID_ARGUMENT );
765 return( 1 );
766 }
767
768 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
769 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200770 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200771
772 TEST_ASSERT( psa_export_public_key( slot,
773 exported, exported_size,
774 &exported_length ) == PSA_SUCCESS );
775 ok = exported_key_sanity_check( public_type, bits,
776 exported, exported_length );
777
778exit:
779 mbedtls_free( exported );
780 return( ok );
781}
782
Gilles Peskine02b75072018-07-01 22:31:34 +0200783static int exercise_key( psa_key_slot_t slot,
784 psa_key_usage_t usage,
785 psa_algorithm_t alg )
786{
787 int ok;
788 if( alg == 0 )
789 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
790 else if( PSA_ALG_IS_MAC( alg ) )
791 ok = exercise_mac_key( slot, usage, alg );
792 else if( PSA_ALG_IS_CIPHER( alg ) )
793 ok = exercise_cipher_key( slot, usage, alg );
794 else if( PSA_ALG_IS_AEAD( alg ) )
795 ok = exercise_aead_key( slot, usage, alg );
796 else if( PSA_ALG_IS_SIGN( alg ) )
797 ok = exercise_signature_key( slot, usage, alg );
798 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
799 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200800 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
801 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200802 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
803 ok = exercise_key_agreement_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200804 else
805 {
806 char message[40];
807 mbedtls_snprintf( message, sizeof( message ),
808 "No code to exercise alg=0x%08lx",
809 (unsigned long) alg );
810 test_fail( message, __LINE__, __FILE__ );
811 ok = 0;
812 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200813
814 ok = ok && exercise_export_key( slot, usage );
815 ok = ok && exercise_export_public_key( slot );
816
Gilles Peskine02b75072018-07-01 22:31:34 +0200817 return( ok );
818}
819
Gilles Peskine10df3412018-10-25 22:35:43 +0200820static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
821 psa_algorithm_t alg )
822{
823 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
824 {
825 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
826 PSA_KEY_USAGE_VERIFY :
827 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
828 }
829 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
830 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
831 {
832 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
833 PSA_KEY_USAGE_ENCRYPT :
834 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
835 }
836 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
837 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
838 {
839 return( PSA_KEY_USAGE_DERIVE );
840 }
841 else
842 {
843 return( 0 );
844 }
845
846}
Gilles Peskinee59236f2018-01-27 23:32:46 +0100847/* END_HEADER */
848
849/* BEGIN_DEPENDENCIES
850 * depends_on:MBEDTLS_PSA_CRYPTO_C
851 * END_DEPENDENCIES
852 */
853
854/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200855void static_checks( )
856{
857 size_t max_truncated_mac_size =
858 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
859
860 /* Check that the length for a truncated MAC always fits in the algorithm
861 * encoding. The shifted mask is the maximum truncated value. The
862 * untruncated algorithm may be one byte larger. */
863 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
864}
865/* END_CASE */
866
867/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200868void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100869{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100870 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100871 int i;
872 for( i = 0; i <= 1; i++ )
873 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 status = psa_crypto_init( );
875 TEST_ASSERT( status == PSA_SUCCESS );
876 status = psa_crypto_init( );
877 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100878 mbedtls_psa_crypto_free( );
879 }
880}
881/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100882
883/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200884void fill_slots( int max_arg )
885{
886 /* Fill all the slots until we run out of memory or out of slots,
887 * or until some limit specified in the test data for the sake of
888 * implementations with an essentially unlimited number of slots.
889 * This test assumes that available slots are numbered from 1. */
890
891 psa_key_slot_t slot;
892 psa_key_slot_t max = 0;
893 psa_key_policy_t policy;
894 uint8_t exported[sizeof( max )];
895 size_t exported_size;
896 psa_status_t status;
897
898 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
899
900 psa_key_policy_init( &policy );
901 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
902
903 for( max = 1; max <= (size_t) max_arg; max++ )
904 {
905 status = psa_set_key_policy( max, &policy );
906 /* Stop filling slots if we run out of memory or out of
907 * available slots. */
908 TEST_ASSERT( status == PSA_SUCCESS ||
909 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
910 status == PSA_ERROR_INVALID_ARGUMENT );
911 if( status != PSA_SUCCESS )
912 break;
913 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
914 (uint8_t*) &max, sizeof( max ) );
915 /* Since psa_set_key_policy succeeded, we know that the slot
916 * number is valid. But we may legitimately run out of memory. */
917 TEST_ASSERT( status == PSA_SUCCESS ||
918 status == PSA_ERROR_INSUFFICIENT_MEMORY );
919 if( status != PSA_SUCCESS )
920 break;
921 }
922 /* `max` is now the first slot number that wasn't filled. */
923 max -= 1;
924
925 for( slot = 1; slot <= max; slot++ )
926 {
927 TEST_ASSERT( psa_export_key( slot,
928 exported, sizeof( exported ),
929 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200930 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200931 }
932
933exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200934 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200935 mbedtls_psa_crypto_free( );
936}
937/* END_CASE */
938
939/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200940void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941{
942 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200943 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100944 psa_status_t status;
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 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100948 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
949
Gilles Peskine4abf7412018-06-18 16:35:34 +0200950 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200951 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 if( status == PSA_SUCCESS )
953 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
954
955exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100956 mbedtls_psa_crypto_free( );
957}
958/* END_CASE */
959
960/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200961void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
962{
963 int slot = 1;
964 size_t bits = bits_arg;
965 psa_status_t expected_status = expected_status_arg;
966 psa_status_t status;
967 psa_key_type_t type =
968 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
969 size_t buffer_size = /* Slight overapproximations */
970 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200971 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200972 unsigned char *p;
973 int ret;
974 size_t length;
975
976 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200977 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200978
979 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
980 bits, keypair ) ) >= 0 );
981 length = ret;
982
983 /* Try importing the key */
984 status = psa_import_key( slot, type, p, length );
985 TEST_ASSERT( status == expected_status );
986 if( status == PSA_SUCCESS )
987 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
988
989exit:
990 mbedtls_free( buffer );
991 mbedtls_psa_crypto_free( );
992}
993/* END_CASE */
994
995/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300996void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300997 int type_arg,
998 int alg_arg,
999 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000 int expected_bits,
1001 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001002 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001003 int canonical_input )
1004{
1005 int slot = 1;
1006 int slot2 = slot + 1;
1007 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001008 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001009 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011 unsigned char *exported = NULL;
1012 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001013 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001014 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015 size_t reexported_length;
1016 psa_key_type_t got_type;
1017 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001018 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001021 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001022 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001023 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001025 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001026 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1027
mohammad1603a97cb8c2018-03-28 03:46:26 -07001028 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001029 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001030 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1031
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001032 /* Import the key */
1033 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001034 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001035
1036 /* Test the key information */
1037 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001038 &got_type,
1039 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 TEST_ASSERT( got_type == type );
1041 TEST_ASSERT( got_bits == (size_t) expected_bits );
1042
1043 /* Export the key */
1044 status = psa_export_key( slot,
1045 exported, export_size,
1046 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001047 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001048
1049 /* The exported length must be set by psa_export_key() to a value between 0
1050 * and export_size. On errors, the exported length must be 0. */
1051 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1052 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1053 TEST_ASSERT( exported_length <= export_size );
1054
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001055 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001056 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001058 {
1059 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001061 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062
Gilles Peskine8f609232018-08-11 01:24:55 +02001063 if( ! exercise_export_key( slot, usage_arg ) )
1064 goto exit;
1065
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001067 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001068 else
1069 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001070 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1071
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001072 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001073 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001074 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001075 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001076 reexported,
1077 export_size,
1078 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001079 ASSERT_COMPARE( exported, exported_length,
1080 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001081 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001082 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001083
1084destroy:
1085 /* Destroy the key */
1086 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1087 TEST_ASSERT( psa_get_key_information(
1088 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1089
1090exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001091 mbedtls_free( exported );
1092 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001093 mbedtls_psa_crypto_free( );
1094}
1095/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001096
Moran Pekerf709f4a2018-06-06 17:26:04 +03001097/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001098void import_key_nonempty_slot( )
1099{
1100 int slot = 1;
1101 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1102 psa_status_t status;
1103 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
1104 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1105
1106 /* Import the key */
1107 TEST_ASSERT( psa_import_key( slot, type,
1108 data, sizeof( data ) ) == PSA_SUCCESS );
1109
1110 /* Import the key again */
1111 status = psa_import_key( slot, type, data, sizeof( data ) );
1112 TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT );
1113
1114exit:
1115 mbedtls_psa_crypto_free( );
1116}
1117/* END_CASE */
1118
1119/* BEGIN_CASE */
1120void export_invalid_slot( int slot, int expected_export_status_arg )
1121{
1122 psa_status_t status;
1123 unsigned char *exported = NULL;
1124 size_t export_size = 0;
1125 size_t exported_length = INVALID_EXPORT_LENGTH;
1126 psa_status_t expected_export_status = expected_export_status_arg;
1127
1128 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1129
1130 /* Export the key */
1131 status = psa_export_key( slot,
1132 exported, export_size,
1133 &exported_length );
1134 TEST_ASSERT( status == expected_export_status );
1135
1136exit:
1137 mbedtls_psa_crypto_free( );
1138}
1139/* END_CASE */
1140
1141/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001142void export_with_no_key_activity( )
1143{
1144 int slot = 1;
1145 psa_algorithm_t alg = PSA_ALG_CTR;
1146 psa_status_t status;
1147 psa_key_policy_t policy;
1148 unsigned char *exported = NULL;
1149 size_t export_size = 0;
1150 size_t exported_length = INVALID_EXPORT_LENGTH;
1151
1152 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1153
1154 psa_key_policy_init( &policy );
1155 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1156 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1157
1158 /* Export the key */
1159 status = psa_export_key( slot,
1160 exported, export_size,
1161 &exported_length );
1162 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1163
1164exit:
1165 mbedtls_psa_crypto_free( );
1166}
1167/* END_CASE */
1168
1169/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001170void cipher_with_no_key_activity( )
1171{
1172 int slot = 1;
1173 psa_status_t status;
1174 psa_key_policy_t policy;
1175 psa_cipher_operation_t operation;
1176 int exercise_alg = PSA_ALG_CTR;
1177
1178 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1179
1180 psa_key_policy_init( &policy );
1181 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
1182 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1183
1184 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1185 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1186
1187exit:
1188 psa_cipher_abort( &operation );
1189 mbedtls_psa_crypto_free( );
1190}
1191/* END_CASE */
1192
1193/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001194void export_after_import_failure( data_t *data, int type_arg,
1195 int expected_import_status_arg )
1196{
1197 int slot = 1;
1198 psa_key_type_t type = type_arg;
1199 psa_status_t status;
1200 unsigned char *exported = NULL;
1201 size_t export_size = 0;
1202 psa_status_t expected_import_status = expected_import_status_arg;
1203 size_t exported_length = INVALID_EXPORT_LENGTH;
1204
1205 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1206
1207 /* Import the key - expect failure */
1208 status = psa_import_key( slot, type,
1209 data->x, data->len );
1210 TEST_ASSERT( status == expected_import_status );
1211
1212 /* Export the key */
1213 status = psa_export_key( slot,
1214 exported, export_size,
1215 &exported_length );
1216 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1217
1218exit:
1219 mbedtls_psa_crypto_free( );
1220}
1221/* END_CASE */
1222
1223/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001224void cipher_after_import_failure( data_t *data, int type_arg,
1225 int expected_import_status_arg )
1226{
1227 int slot = 1;
1228 psa_cipher_operation_t operation;
1229 psa_key_type_t type = type_arg;
1230 psa_status_t status;
1231 psa_status_t expected_import_status = expected_import_status_arg;
1232 int exercise_alg = PSA_ALG_CTR;
1233
1234 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1235
1236 /* Import the key - expect failure */
1237 status = psa_import_key( slot, type,
1238 data->x, data->len );
1239 TEST_ASSERT( status == expected_import_status );
1240
1241 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1242 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1243
1244exit:
1245 psa_cipher_abort( &operation );
1246 mbedtls_psa_crypto_free( );
1247}
1248/* END_CASE */
1249
1250/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001251void export_after_destroy_key( data_t *data, int type_arg )
1252{
1253 int slot = 1;
1254 psa_key_type_t type = type_arg;
1255 psa_status_t status;
1256 psa_key_policy_t policy;
1257 psa_algorithm_t alg = PSA_ALG_CTR;
1258 unsigned char *exported = NULL;
1259 size_t export_size = 0;
1260 size_t exported_length = INVALID_EXPORT_LENGTH;
1261
1262 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1263
1264 psa_key_policy_init( &policy );
1265 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1266 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1267 export_size = (ptrdiff_t) data->len;
1268 ASSERT_ALLOC( exported, export_size );
1269
1270 /* Import the key */
1271 TEST_ASSERT( psa_import_key( slot, type,
1272 data->x, data->len ) == PSA_SUCCESS );
1273
1274 TEST_ASSERT( psa_export_key( slot, exported, export_size,
1275 &exported_length ) == PSA_SUCCESS );
1276
1277 /* Destroy the key */
1278 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1279
1280 /* Export the key */
1281 status = psa_export_key( slot, exported, export_size,
1282 &exported_length );
1283 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1284
1285exit:
1286 mbedtls_free( exported );
1287 mbedtls_psa_crypto_free( );
1288}
1289/* END_CASE */
1290
1291/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001292void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001293 int type_arg,
1294 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001295 int export_size_delta,
1296 int expected_export_status_arg,
1297 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001298{
1299 int slot = 1;
1300 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001301 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001302 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001303 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001304 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001305 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001306 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001307 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001308
Moran Pekerf709f4a2018-06-06 17:26:04 +03001309 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1310
1311 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001312 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001313 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1314
1315 /* Import the key */
1316 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001317 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001318
Gilles Peskine49c25912018-10-29 15:15:31 +01001319 /* Export the public key */
1320 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001321 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001322 exported, export_size,
1323 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001324 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001325 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001326 {
1327 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1328 size_t bits;
1329 TEST_ASSERT( psa_get_key_information( slot, NULL, &bits ) ==
1330 PSA_SUCCESS );
1331 TEST_ASSERT( expected_public_key->len <=
1332 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001333 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1334 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001335 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001336
1337exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001338 mbedtls_free( exported );
Gilles Peskine49c25912018-10-29 15:15:31 +01001339 psa_destroy_key( slot );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001340 mbedtls_psa_crypto_free( );
1341}
1342/* END_CASE */
1343
Gilles Peskine20035e32018-02-03 22:44:14 +01001344/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001345void import_and_exercise_key( data_t *data,
1346 int type_arg,
1347 int bits_arg,
1348 int alg_arg )
1349{
1350 int slot = 1;
1351 psa_key_type_t type = type_arg;
1352 size_t bits = bits_arg;
1353 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001354 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001355 psa_key_policy_t policy;
1356 psa_key_type_t got_type;
1357 size_t got_bits;
1358 psa_status_t status;
1359
1360 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1361
1362 psa_key_policy_init( &policy );
1363 psa_key_policy_set_usage( &policy, usage, alg );
1364 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1365
1366 /* Import the key */
1367 status = psa_import_key( slot, type, data->x, data->len );
1368 TEST_ASSERT( status == PSA_SUCCESS );
1369
1370 /* Test the key information */
1371 TEST_ASSERT( psa_get_key_information( slot,
1372 &got_type,
1373 &got_bits ) == PSA_SUCCESS );
1374 TEST_ASSERT( got_type == type );
1375 TEST_ASSERT( got_bits == bits );
1376
1377 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001378 if( ! exercise_key( slot, usage, alg ) )
1379 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001380
1381exit:
1382 psa_destroy_key( slot );
1383 mbedtls_psa_crypto_free( );
1384}
1385/* END_CASE */
1386
1387/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001388void key_policy( int usage_arg, int alg_arg )
1389{
1390 int key_slot = 1;
1391 psa_algorithm_t alg = alg_arg;
1392 psa_key_usage_t usage = usage_arg;
1393 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1394 unsigned char key[32] = {0};
1395 psa_key_policy_t policy_set;
1396 psa_key_policy_t policy_get;
1397
1398 memset( key, 0x2a, sizeof( key ) );
1399
1400 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1401
1402 psa_key_policy_init( &policy_set );
1403 psa_key_policy_init( &policy_get );
1404
1405 psa_key_policy_set_usage( &policy_set, usage, alg );
1406
1407 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1408 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1409 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1410
1411 TEST_ASSERT( psa_import_key( key_slot, key_type,
1412 key, sizeof( key ) ) == PSA_SUCCESS );
1413
1414 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1415
1416 TEST_ASSERT( policy_get.usage == policy_set.usage );
1417 TEST_ASSERT( policy_get.alg == policy_set.alg );
1418
1419exit:
1420 psa_destroy_key( key_slot );
1421 mbedtls_psa_crypto_free( );
1422}
1423/* END_CASE */
1424
1425/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001426void mac_key_policy( int policy_usage,
1427 int policy_alg,
1428 int key_type,
1429 data_t *key_data,
1430 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001431{
1432 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001433 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001434 psa_mac_operation_t operation;
1435 psa_status_t status;
1436 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001437
1438 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1439
1440 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001441 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001442 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1443
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001444 TEST_ASSERT( psa_import_key( key_slot, key_type,
1445 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001446
Gilles Peskine89167cb2018-07-08 20:12:23 +02001447 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001448 if( policy_alg == exercise_alg &&
1449 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1450 TEST_ASSERT( status == PSA_SUCCESS );
1451 else
1452 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1453 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001454
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001455 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001456 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001457 if( policy_alg == exercise_alg &&
1458 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001459 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001460 else
1461 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1462
1463exit:
1464 psa_mac_abort( &operation );
1465 psa_destroy_key( key_slot );
1466 mbedtls_psa_crypto_free( );
1467}
1468/* END_CASE */
1469
1470/* BEGIN_CASE */
1471void cipher_key_policy( int policy_usage,
1472 int policy_alg,
1473 int key_type,
1474 data_t *key_data,
1475 int exercise_alg )
1476{
1477 int key_slot = 1;
1478 psa_key_policy_t policy;
1479 psa_cipher_operation_t operation;
1480 psa_status_t status;
1481
1482 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1483
1484 psa_key_policy_init( &policy );
1485 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1486 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1487
1488 TEST_ASSERT( psa_import_key( key_slot, key_type,
1489 key_data->x, key_data->len ) == PSA_SUCCESS );
1490
Gilles Peskinefe119512018-07-08 21:39:34 +02001491 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001492 if( policy_alg == exercise_alg &&
1493 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1494 TEST_ASSERT( status == PSA_SUCCESS );
1495 else
1496 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1497 psa_cipher_abort( &operation );
1498
Gilles Peskinefe119512018-07-08 21:39:34 +02001499 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001500 if( policy_alg == exercise_alg &&
1501 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1502 TEST_ASSERT( status == PSA_SUCCESS );
1503 else
1504 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1505
1506exit:
1507 psa_cipher_abort( &operation );
1508 psa_destroy_key( key_slot );
1509 mbedtls_psa_crypto_free( );
1510}
1511/* END_CASE */
1512
1513/* BEGIN_CASE */
1514void aead_key_policy( int policy_usage,
1515 int policy_alg,
1516 int key_type,
1517 data_t *key_data,
1518 int nonce_length_arg,
1519 int tag_length_arg,
1520 int exercise_alg )
1521{
1522 int key_slot = 1;
1523 psa_key_policy_t policy;
1524 psa_status_t status;
1525 unsigned char nonce[16] = {0};
1526 size_t nonce_length = nonce_length_arg;
1527 unsigned char tag[16];
1528 size_t tag_length = tag_length_arg;
1529 size_t output_length;
1530
1531 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1532 TEST_ASSERT( tag_length <= sizeof( tag ) );
1533
1534 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1535
1536 psa_key_policy_init( &policy );
1537 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1538 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1539
1540 TEST_ASSERT( psa_import_key( key_slot, key_type,
1541 key_data->x, key_data->len ) == PSA_SUCCESS );
1542
1543 status = psa_aead_encrypt( key_slot, exercise_alg,
1544 nonce, nonce_length,
1545 NULL, 0,
1546 NULL, 0,
1547 tag, tag_length,
1548 &output_length );
1549 if( policy_alg == exercise_alg &&
1550 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1551 TEST_ASSERT( status == PSA_SUCCESS );
1552 else
1553 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1554
1555 memset( tag, 0, sizeof( tag ) );
1556 status = psa_aead_decrypt( key_slot, exercise_alg,
1557 nonce, nonce_length,
1558 NULL, 0,
1559 tag, tag_length,
1560 NULL, 0,
1561 &output_length );
1562 if( policy_alg == exercise_alg &&
1563 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1564 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1565 else
1566 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1567
1568exit:
1569 psa_destroy_key( key_slot );
1570 mbedtls_psa_crypto_free( );
1571}
1572/* END_CASE */
1573
1574/* BEGIN_CASE */
1575void asymmetric_encryption_key_policy( int policy_usage,
1576 int policy_alg,
1577 int key_type,
1578 data_t *key_data,
1579 int exercise_alg )
1580{
1581 int key_slot = 1;
1582 psa_key_policy_t policy;
1583 psa_status_t status;
1584 size_t key_bits;
1585 size_t buffer_length;
1586 unsigned char *buffer = NULL;
1587 size_t output_length;
1588
1589 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1590
1591 psa_key_policy_init( &policy );
1592 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1593 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1594
1595 TEST_ASSERT( psa_import_key( key_slot, key_type,
1596 key_data->x, key_data->len ) == PSA_SUCCESS );
1597
1598 TEST_ASSERT( psa_get_key_information( key_slot,
1599 NULL,
1600 &key_bits ) == PSA_SUCCESS );
1601 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1602 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001603 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001604
1605 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1606 NULL, 0,
1607 NULL, 0,
1608 buffer, buffer_length,
1609 &output_length );
1610 if( policy_alg == exercise_alg &&
1611 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1612 TEST_ASSERT( status == PSA_SUCCESS );
1613 else
1614 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1615
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001616 if( buffer_length != 0 )
1617 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001618 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1619 buffer, buffer_length,
1620 NULL, 0,
1621 buffer, buffer_length,
1622 &output_length );
1623 if( policy_alg == exercise_alg &&
1624 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1625 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1626 else
1627 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1628
1629exit:
1630 psa_destroy_key( key_slot );
1631 mbedtls_psa_crypto_free( );
1632 mbedtls_free( buffer );
1633}
1634/* END_CASE */
1635
1636/* BEGIN_CASE */
1637void asymmetric_signature_key_policy( int policy_usage,
1638 int policy_alg,
1639 int key_type,
1640 data_t *key_data,
1641 int exercise_alg )
1642{
1643 int key_slot = 1;
1644 psa_key_policy_t policy;
1645 psa_status_t status;
1646 unsigned char payload[16] = {1};
1647 size_t payload_length = sizeof( payload );
1648 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1649 size_t signature_length;
1650
1651 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1652
1653 psa_key_policy_init( &policy );
1654 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1655 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1656
1657 TEST_ASSERT( psa_import_key( key_slot, key_type,
1658 key_data->x, key_data->len ) == PSA_SUCCESS );
1659
1660 status = psa_asymmetric_sign( key_slot, exercise_alg,
1661 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001662 signature, sizeof( signature ),
1663 &signature_length );
1664 if( policy_alg == exercise_alg &&
1665 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1666 TEST_ASSERT( status == PSA_SUCCESS );
1667 else
1668 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1669
1670 memset( signature, 0, sizeof( signature ) );
1671 status = psa_asymmetric_verify( key_slot, exercise_alg,
1672 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001673 signature, sizeof( signature ) );
1674 if( policy_alg == exercise_alg &&
1675 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1676 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1677 else
1678 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001679
1680exit:
1681 psa_destroy_key( key_slot );
1682 mbedtls_psa_crypto_free( );
1683}
1684/* END_CASE */
1685
1686/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001687void derive_key_policy( int policy_usage,
1688 int policy_alg,
1689 int key_type,
1690 data_t *key_data,
1691 int exercise_alg )
1692{
1693 int key_slot = 1;
1694 psa_key_policy_t policy;
1695 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1696 psa_status_t status;
1697
1698 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1699
1700 psa_key_policy_init( &policy );
1701 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1702 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1703
1704 TEST_ASSERT( psa_import_key( key_slot, key_type,
1705 key_data->x, key_data->len ) == PSA_SUCCESS );
1706
1707 status = psa_key_derivation( &generator, key_slot,
1708 exercise_alg,
1709 NULL, 0,
1710 NULL, 0,
1711 1 );
1712 if( policy_alg == exercise_alg &&
1713 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1714 TEST_ASSERT( status == PSA_SUCCESS );
1715 else
1716 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1717
1718exit:
1719 psa_generator_abort( &generator );
1720 psa_destroy_key( key_slot );
1721 mbedtls_psa_crypto_free( );
1722}
1723/* END_CASE */
1724
1725/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001726void agreement_key_policy( int policy_usage,
1727 int policy_alg,
1728 int key_type_arg,
1729 data_t *key_data,
1730 int exercise_alg )
1731{
1732 int key_slot = 1;
1733 psa_key_policy_t policy;
1734 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001735 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1736 psa_status_t status;
1737
1738 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1739
1740 psa_key_policy_init( &policy );
1741 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1742 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1743
1744 TEST_ASSERT( psa_import_key( key_slot, key_type,
1745 key_data->x, key_data->len ) == PSA_SUCCESS );
1746
Gilles Peskinec7998b72018-11-07 18:45:02 +01001747 status = key_agreement_with_self( &generator, key_slot, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001748
Gilles Peskine01d718c2018-09-18 12:01:02 +02001749 if( policy_alg == exercise_alg &&
1750 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1751 TEST_ASSERT( status == PSA_SUCCESS );
1752 else
1753 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1754
1755exit:
1756 psa_generator_abort( &generator );
1757 psa_destroy_key( key_slot );
1758 mbedtls_psa_crypto_free( );
1759}
1760/* END_CASE */
1761
1762/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001763void key_lifetime( int lifetime_arg )
1764{
1765 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001766 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001767 unsigned char key[32] = {0};
1768 psa_key_lifetime_t lifetime_set = lifetime_arg;
1769 psa_key_lifetime_t lifetime_get;
1770
1771 memset( key, 0x2a, sizeof( key ) );
1772
1773 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1774
1775 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1776 lifetime_set ) == PSA_SUCCESS );
1777
1778 TEST_ASSERT( psa_import_key( key_slot, key_type,
1779 key, sizeof( key ) ) == PSA_SUCCESS );
1780
1781 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1782 &lifetime_get ) == PSA_SUCCESS );
1783
1784 TEST_ASSERT( lifetime_get == lifetime_set );
1785
1786exit:
1787 psa_destroy_key( key_slot );
1788 mbedtls_psa_crypto_free( );
1789}
1790/* END_CASE */
1791
1792/* BEGIN_CASE */
1793void key_lifetime_set_fail( int key_slot_arg,
1794 int lifetime_arg,
1795 int expected_status_arg )
1796{
1797 psa_key_slot_t key_slot = key_slot_arg;
1798 psa_key_lifetime_t lifetime_set = lifetime_arg;
1799 psa_status_t actual_status;
1800 psa_status_t expected_status = expected_status_arg;
1801
1802 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1803
1804 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1805
1806 if( actual_status == PSA_SUCCESS )
1807 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1808
1809 TEST_ASSERT( expected_status == actual_status );
1810
1811exit:
1812 psa_destroy_key( key_slot );
1813 mbedtls_psa_crypto_free( );
1814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001818void hash_setup( int alg_arg,
1819 int expected_status_arg )
1820{
1821 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001822 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001823 psa_hash_operation_t operation;
1824 psa_status_t status;
1825
1826 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1827
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001828 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001829 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001830 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001831
1832exit:
1833 mbedtls_psa_crypto_free( );
1834}
1835/* END_CASE */
1836
1837/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001838void hash_bad_order( )
1839{
1840 unsigned char input[] = "";
1841 /* SHA-256 hash of an empty string */
1842 unsigned char hash[] = {
1843 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1844 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1845 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1846 size_t hash_len;
1847 psa_hash_operation_t operation;
1848
1849 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1850
1851 /* psa_hash_update without calling psa_hash_setup beforehand */
1852 memset( &operation, 0, sizeof( operation ) );
1853 TEST_ASSERT( psa_hash_update( &operation,
1854 input, sizeof( input ) ) ==
1855 PSA_ERROR_INVALID_ARGUMENT );
1856
1857 /* psa_hash_verify without calling psa_hash_setup beforehand */
1858 memset( &operation, 0, sizeof( operation ) );
1859 TEST_ASSERT( psa_hash_verify( &operation,
1860 hash, sizeof( hash ) ) ==
1861 PSA_ERROR_INVALID_ARGUMENT );
1862
1863 /* psa_hash_finish without calling psa_hash_setup beforehand */
1864 memset( &operation, 0, sizeof( operation ) );
1865 TEST_ASSERT( psa_hash_finish( &operation,
1866 hash, sizeof( hash ), &hash_len ) ==
1867 PSA_ERROR_INVALID_ARGUMENT );
1868
1869exit:
1870 mbedtls_psa_crypto_free( );
1871}
1872/* END_CASE */
1873
itayzafrir27e69452018-11-01 14:26:34 +02001874/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1875void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001876{
1877 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001878 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1879 * appended to it */
1880 unsigned char hash[] = {
1881 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1882 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1883 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001884 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001885 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001886
1887 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1888
itayzafrir27e69452018-11-01 14:26:34 +02001889 /* psa_hash_verify with a smaller hash than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001890 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1891 TEST_ASSERT( psa_hash_verify( &operation,
1892 hash, expected_size - 1 ) ==
1893 PSA_ERROR_INVALID_SIGNATURE );
1894
itayzafrir27e69452018-11-01 14:26:34 +02001895 /* psa_hash_verify with a non-matching hash */
itayzafrirec93d302018-10-18 18:01:10 +03001896 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrirec93d302018-10-18 18:01:10 +03001897 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001898 hash + 1, expected_size ) ==
itayzafrirec93d302018-10-18 18:01:10 +03001899 PSA_ERROR_INVALID_SIGNATURE );
1900
itayzafrir27e69452018-11-01 14:26:34 +02001901 /* psa_hash_verify with a hash longer than expected */
itayzafrir4271df92018-10-24 18:16:19 +03001902 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrir4271df92018-10-24 18:16:19 +03001903 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001904 hash, sizeof( hash ) ) ==
itayzafrir4271df92018-10-24 18:16:19 +03001905 PSA_ERROR_INVALID_SIGNATURE );
1906
itayzafrirec93d302018-10-18 18:01:10 +03001907exit:
1908 mbedtls_psa_crypto_free( );
1909}
1910/* END_CASE */
1911
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001912/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1913void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001914{
1915 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001916 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001917 size_t expected_size = PSA_HASH_SIZE( alg );
1918 psa_hash_operation_t operation;
1919 size_t hash_len;
1920
1921 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1922
itayzafrir58028322018-10-25 10:22:01 +03001923 /* psa_hash_finish with a smaller hash buffer than expected */
1924 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1925 TEST_ASSERT( psa_hash_finish( &operation,
1926 hash, expected_size - 1,
1927 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1928
1929exit:
1930 mbedtls_psa_crypto_free( );
1931}
1932/* END_CASE */
1933
1934/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001935void mac_setup( int key_type_arg,
1936 data_t *key,
1937 int alg_arg,
1938 int expected_status_arg )
1939{
1940 int key_slot = 1;
1941 psa_key_type_t key_type = key_type_arg;
1942 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001943 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001944 psa_mac_operation_t operation;
1945 psa_key_policy_t policy;
1946 psa_status_t status;
1947
1948 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1949
1950 psa_key_policy_init( &policy );
1951 psa_key_policy_set_usage( &policy,
1952 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1953 alg );
1954 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1955
1956 TEST_ASSERT( psa_import_key( key_slot, key_type,
1957 key->x, key->len ) == PSA_SUCCESS );
1958
Gilles Peskine89167cb2018-07-08 20:12:23 +02001959 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001960 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001961 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001962
1963exit:
1964 psa_destroy_key( key_slot );
1965 mbedtls_psa_crypto_free( );
1966}
1967/* END_CASE */
1968
1969/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001970void mac_sign( int key_type_arg,
1971 data_t *key,
1972 int alg_arg,
1973 data_t *input,
1974 data_t *expected_mac )
1975{
1976 int key_slot = 1;
1977 psa_key_type_t key_type = key_type_arg;
1978 psa_algorithm_t alg = alg_arg;
1979 psa_mac_operation_t operation;
1980 psa_key_policy_t policy;
1981 /* Leave a little extra room in the output buffer. At the end of the
1982 * test, we'll check that the implementation didn't overwrite onto
1983 * this extra room. */
1984 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1985 size_t mac_buffer_size =
1986 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1987 size_t mac_length = 0;
1988
1989 memset( actual_mac, '+', sizeof( actual_mac ) );
1990 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1991 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1992
1993 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1994
1995 psa_key_policy_init( &policy );
1996 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1997 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1998
1999 TEST_ASSERT( psa_import_key( key_slot, key_type,
2000 key->x, key->len ) == PSA_SUCCESS );
2001
2002 /* Calculate the MAC. */
2003 TEST_ASSERT( psa_mac_sign_setup( &operation,
2004 key_slot, alg ) == PSA_SUCCESS );
2005 TEST_ASSERT( psa_mac_update( &operation,
2006 input->x, input->len ) == PSA_SUCCESS );
2007 TEST_ASSERT( psa_mac_sign_finish( &operation,
2008 actual_mac, mac_buffer_size,
2009 &mac_length ) == PSA_SUCCESS );
2010
2011 /* Compare with the expected value. */
2012 TEST_ASSERT( mac_length == expected_mac->len );
2013 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
2014
2015 /* Verify that the end of the buffer is untouched. */
2016 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2017 sizeof( actual_mac ) - mac_length ) );
2018
2019exit:
2020 psa_destroy_key( key_slot );
2021 mbedtls_psa_crypto_free( );
2022}
2023/* END_CASE */
2024
2025/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002026void mac_verify( int key_type_arg,
2027 data_t *key,
2028 int alg_arg,
2029 data_t *input,
2030 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031{
2032 int key_slot = 1;
2033 psa_key_type_t key_type = key_type_arg;
2034 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002035 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07002036 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002037
Gilles Peskine69c12672018-06-28 00:07:19 +02002038 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2039
Gilles Peskine8c9def32018-02-08 10:02:12 +01002040 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002041 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002042 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002043 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002044 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2045 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002046
2047 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2048
mohammad16036df908f2018-04-02 08:34:15 -07002049 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002050 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07002051 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2052
Gilles Peskine8c9def32018-02-08 10:02:12 +01002053 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002054 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002055
Gilles Peskine89167cb2018-07-08 20:12:23 +02002056 TEST_ASSERT( psa_mac_verify_setup( &operation,
2057 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002058 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
2059 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002060 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02002061 TEST_ASSERT( psa_mac_verify_finish( &operation,
2062 expected_mac->x,
2063 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002064
2065exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002066 psa_destroy_key( key_slot );
2067 mbedtls_psa_crypto_free( );
2068}
2069/* END_CASE */
2070
2071/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002072void cipher_setup( int key_type_arg,
2073 data_t *key,
2074 int alg_arg,
2075 int expected_status_arg )
2076{
2077 int key_slot = 1;
2078 psa_key_type_t key_type = key_type_arg;
2079 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002080 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002081 psa_cipher_operation_t operation;
2082 psa_key_policy_t policy;
2083 psa_status_t status;
2084
2085 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2086
2087 psa_key_policy_init( &policy );
2088 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2089 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2090
2091 TEST_ASSERT( psa_import_key( key_slot, key_type,
2092 key->x, key->len ) == PSA_SUCCESS );
2093
Gilles Peskinefe119512018-07-08 21:39:34 +02002094 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002095 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002096 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002097
2098exit:
2099 psa_destroy_key( key_slot );
2100 mbedtls_psa_crypto_free( );
2101}
2102/* END_CASE */
2103
2104/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002105void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002106 data_t *key,
2107 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002108 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002109{
2110 int key_slot = 1;
2111 psa_status_t status;
2112 psa_key_type_t key_type = key_type_arg;
2113 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002114 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002115 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002116 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002117 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002118 size_t output_buffer_size = 0;
2119 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002120 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002121 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002122 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002123
Gilles Peskine50e586b2018-06-08 14:28:46 +02002124 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002125 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002126 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002127 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2128 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2129 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002130
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002131 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2132 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002133
2134 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2135
Moran Pekered346952018-07-05 15:22:45 +03002136 psa_key_policy_init( &policy );
2137 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2138 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2139
Gilles Peskine50e586b2018-06-08 14:28:46 +02002140 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002141 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002142
Gilles Peskinefe119512018-07-08 21:39:34 +02002143 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2144 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002145
Gilles Peskinefe119512018-07-08 21:39:34 +02002146 TEST_ASSERT( psa_cipher_set_iv( &operation,
2147 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002148 output_buffer_size = (size_t) input->len +
2149 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002150 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002151
Gilles Peskine4abf7412018-06-18 16:35:34 +02002152 TEST_ASSERT( psa_cipher_update( &operation,
2153 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002154 output, output_buffer_size,
2155 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002156 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002157 status = psa_cipher_finish( &operation,
2158 output + function_output_length,
2159 output_buffer_size,
2160 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002161 total_output_length += function_output_length;
2162
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002163 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002164 if( expected_status == PSA_SUCCESS )
2165 {
2166 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002167 ASSERT_COMPARE( expected_output->x, expected_output->len,
2168 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002169 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002170
Gilles Peskine50e586b2018-06-08 14:28:46 +02002171exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002172 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002173 psa_destroy_key( key_slot );
2174 mbedtls_psa_crypto_free( );
2175}
2176/* END_CASE */
2177
2178/* BEGIN_CASE */
2179void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002180 data_t *key,
2181 data_t *input,
2182 int first_part_size,
2183 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002184{
2185 int key_slot = 1;
2186 psa_key_type_t key_type = key_type_arg;
2187 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002188 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002189 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002190 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002191 size_t output_buffer_size = 0;
2192 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002193 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002194 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002195 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002196
Gilles Peskine50e586b2018-06-08 14:28:46 +02002197 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002198 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002199 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002200 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2201 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2202 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002203
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002204 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2205 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002206
2207 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2208
Moran Pekered346952018-07-05 15:22:45 +03002209 psa_key_policy_init( &policy );
2210 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2211 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2212
Gilles Peskine50e586b2018-06-08 14:28:46 +02002213 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002214 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002215
Gilles Peskinefe119512018-07-08 21:39:34 +02002216 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2217 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002218
Gilles Peskinefe119512018-07-08 21:39:34 +02002219 TEST_ASSERT( psa_cipher_set_iv( &operation,
2220 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002221 output_buffer_size = (size_t) input->len +
2222 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002223 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002224
Gilles Peskine4abf7412018-06-18 16:35:34 +02002225 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002226 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002227 output, output_buffer_size,
2228 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002229 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002230 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002231 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002232 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002233 output, output_buffer_size,
2234 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002235 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002236 TEST_ASSERT( psa_cipher_finish( &operation,
2237 output + function_output_length,
2238 output_buffer_size,
2239 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002240 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002241 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2242
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002243 ASSERT_COMPARE( expected_output->x, expected_output->len,
2244 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002245
2246exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002247 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002248 psa_destroy_key( key_slot );
2249 mbedtls_psa_crypto_free( );
2250}
2251/* END_CASE */
2252
2253/* BEGIN_CASE */
2254void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002255 data_t *key,
2256 data_t *input,
2257 int first_part_size,
2258 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002259{
2260 int key_slot = 1;
2261
2262 psa_key_type_t key_type = key_type_arg;
2263 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002264 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002265 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002266 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002267 size_t output_buffer_size = 0;
2268 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002269 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002270 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002271 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002272
Gilles Peskine50e586b2018-06-08 14:28:46 +02002273 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002274 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002275 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002276 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2277 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2278 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002279
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002280 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2281 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002282
2283 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2284
Moran Pekered346952018-07-05 15:22:45 +03002285 psa_key_policy_init( &policy );
2286 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2287 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2288
Gilles Peskine50e586b2018-06-08 14:28:46 +02002289 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002290 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002291
Gilles Peskinefe119512018-07-08 21:39:34 +02002292 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2293 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002294
Gilles Peskinefe119512018-07-08 21:39:34 +02002295 TEST_ASSERT( psa_cipher_set_iv( &operation,
2296 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002297
mohammad16033d91abe2018-07-03 13:15:54 +03002298 output_buffer_size = (size_t) input->len +
2299 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002300 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002301
Gilles Peskine4abf7412018-06-18 16:35:34 +02002302 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2303 TEST_ASSERT( psa_cipher_update( &operation,
2304 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002305 output, output_buffer_size,
2306 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002307 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002308 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002309 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002310 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002311 output, output_buffer_size,
2312 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002313 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002314 TEST_ASSERT( psa_cipher_finish( &operation,
2315 output + function_output_length,
2316 output_buffer_size,
2317 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002318 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002319 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2320
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002321 ASSERT_COMPARE( expected_output->x, expected_output->len,
2322 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002323
2324exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002325 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002326 psa_destroy_key( key_slot );
2327 mbedtls_psa_crypto_free( );
2328}
2329/* END_CASE */
2330
Gilles Peskine50e586b2018-06-08 14:28:46 +02002331/* BEGIN_CASE */
2332void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002333 data_t *key,
2334 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002335 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002336{
2337 int key_slot = 1;
2338 psa_status_t status;
2339 psa_key_type_t key_type = key_type_arg;
2340 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002341 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002342 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002343 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002344 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002345 size_t output_buffer_size = 0;
2346 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002347 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002348 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002349 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002350
Gilles Peskine50e586b2018-06-08 14:28:46 +02002351 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002352 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002353 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002354 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2355 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2356 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002357
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002358 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2359 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002360
2361 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2362
Moran Pekered346952018-07-05 15:22:45 +03002363 psa_key_policy_init( &policy );
2364 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2365 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2366
Gilles Peskine50e586b2018-06-08 14:28:46 +02002367 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002368 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002369
Gilles Peskinefe119512018-07-08 21:39:34 +02002370 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2371 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002372
Gilles Peskinefe119512018-07-08 21:39:34 +02002373 TEST_ASSERT( psa_cipher_set_iv( &operation,
2374 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002375
mohammad16033d91abe2018-07-03 13:15:54 +03002376 output_buffer_size = (size_t) input->len +
2377 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002378 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002379
Gilles Peskine4abf7412018-06-18 16:35:34 +02002380 TEST_ASSERT( psa_cipher_update( &operation,
2381 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002382 output, output_buffer_size,
2383 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002384 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002385 status = psa_cipher_finish( &operation,
2386 output + function_output_length,
2387 output_buffer_size,
2388 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002389 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002390 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391
2392 if( expected_status == PSA_SUCCESS )
2393 {
2394 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002395 ASSERT_COMPARE( expected_output->x, expected_output->len,
2396 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002397 }
2398
Gilles Peskine50e586b2018-06-08 14:28:46 +02002399exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002400 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002401 psa_destroy_key( key_slot );
2402 mbedtls_psa_crypto_free( );
2403}
2404/* END_CASE */
2405
Gilles Peskine50e586b2018-06-08 14:28:46 +02002406/* BEGIN_CASE */
2407void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002408 data_t *key,
2409 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002410{
2411 int key_slot = 1;
2412 psa_key_type_t key_type = key_type_arg;
2413 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002414 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002415 size_t iv_size = 16;
2416 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002417 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002418 size_t output1_size = 0;
2419 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002420 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002421 size_t output2_size = 0;
2422 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002423 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002424 psa_cipher_operation_t operation1;
2425 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002426 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002427
mohammad1603d7d7ba52018-03-12 18:51:53 +02002428 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002429 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002430 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2431 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002432
mohammad1603d7d7ba52018-03-12 18:51:53 +02002433 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2434
Moran Pekered346952018-07-05 15:22:45 +03002435 psa_key_policy_init( &policy );
2436 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2437 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2438
mohammad1603d7d7ba52018-03-12 18:51:53 +02002439 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002440 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002441
Gilles Peskinefe119512018-07-08 21:39:34 +02002442 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2443 key_slot, alg ) == PSA_SUCCESS );
2444 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2445 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002446
Gilles Peskinefe119512018-07-08 21:39:34 +02002447 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2448 iv, iv_size,
2449 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002450 output1_size = (size_t) input->len +
2451 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002452 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002453
Gilles Peskine4abf7412018-06-18 16:35:34 +02002454 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002455 output1, output1_size,
2456 &output1_length ) == PSA_SUCCESS );
2457 TEST_ASSERT( psa_cipher_finish( &operation1,
2458 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002459 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002460
Gilles Peskine048b7f02018-06-08 14:20:49 +02002461 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002462
2463 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2464
2465 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002466 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002467
Gilles Peskinefe119512018-07-08 21:39:34 +02002468 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2469 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002470 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2471 output2, output2_size,
2472 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002473 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002474 TEST_ASSERT( psa_cipher_finish( &operation2,
2475 output2 + output2_length,
2476 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002477 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002478
Gilles Peskine048b7f02018-06-08 14:20:49 +02002479 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002480
Janos Follath25c4fa82018-07-06 16:23:25 +01002481 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002482
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002483 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002484
2485exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002486 mbedtls_free( output1 );
2487 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002488 psa_destroy_key( key_slot );
2489 mbedtls_psa_crypto_free( );
2490}
2491/* END_CASE */
2492
2493/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002494void cipher_verify_output_multipart( int alg_arg,
2495 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002496 data_t *key,
2497 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002498 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002499{
2500 int key_slot = 1;
2501 psa_key_type_t key_type = key_type_arg;
2502 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002503 unsigned char iv[16] = {0};
2504 size_t iv_size = 16;
2505 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002506 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002507 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002508 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002509 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002510 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002511 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002512 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002513 psa_cipher_operation_t operation1;
2514 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002515 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002516
Moran Pekerded84402018-06-06 16:36:50 +03002517 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002518 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002519 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2520 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002521
Moran Pekerded84402018-06-06 16:36:50 +03002522 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2523
Moran Pekered346952018-07-05 15:22:45 +03002524 psa_key_policy_init( &policy );
2525 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2526 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2527
Moran Pekerded84402018-06-06 16:36:50 +03002528 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002529 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002530
Gilles Peskinefe119512018-07-08 21:39:34 +02002531 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2532 key_slot, alg ) == PSA_SUCCESS );
2533 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2534 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002535
Gilles Peskinefe119512018-07-08 21:39:34 +02002536 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2537 iv, iv_size,
2538 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002539 output1_buffer_size = (size_t) input->len +
2540 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002541 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002542
Gilles Peskine4abf7412018-06-18 16:35:34 +02002543 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002544
itayzafrir3e02b3b2018-06-12 17:06:52 +03002545 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002546 output1, output1_buffer_size,
2547 &function_output_length ) == PSA_SUCCESS );
2548 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002549
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002550 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002551 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002552 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002553 output1, output1_buffer_size,
2554 &function_output_length ) == PSA_SUCCESS );
2555 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002556
Gilles Peskine048b7f02018-06-08 14:20:49 +02002557 TEST_ASSERT( psa_cipher_finish( &operation1,
2558 output1 + output1_length,
2559 output1_buffer_size - output1_length,
2560 &function_output_length ) == PSA_SUCCESS );
2561 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002562
2563 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2564
Gilles Peskine048b7f02018-06-08 14:20:49 +02002565 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002566 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002567
Gilles Peskinefe119512018-07-08 21:39:34 +02002568 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2569 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002570
2571 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002572 output2, output2_buffer_size,
2573 &function_output_length ) == PSA_SUCCESS );
2574 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002575
Gilles Peskine048b7f02018-06-08 14:20:49 +02002576 TEST_ASSERT( psa_cipher_update( &operation2,
2577 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002578 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002579 output2, output2_buffer_size,
2580 &function_output_length ) == PSA_SUCCESS );
2581 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002582
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002583 TEST_ASSERT( psa_cipher_finish( &operation2,
2584 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002585 output2_buffer_size - output2_length,
2586 &function_output_length ) == PSA_SUCCESS );
2587 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002588
Janos Follath25c4fa82018-07-06 16:23:25 +01002589 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002590
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002591 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002592
2593exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002594 mbedtls_free( output1 );
2595 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002596 psa_destroy_key( key_slot );
2597 mbedtls_psa_crypto_free( );
2598}
2599/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002600
Gilles Peskine20035e32018-02-03 22:44:14 +01002601/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002602void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002603 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002604 data_t *nonce,
2605 data_t *additional_data,
2606 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002607 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002608{
2609 int slot = 1;
2610 psa_key_type_t key_type = key_type_arg;
2611 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002612 unsigned char *output_data = NULL;
2613 size_t output_size = 0;
2614 size_t output_length = 0;
2615 unsigned char *output_data2 = NULL;
2616 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002617 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002618 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002619 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002620
Gilles Peskinea1cac842018-06-11 19:33:02 +02002621 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002622 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002623 TEST_ASSERT( nonce != NULL );
2624 TEST_ASSERT( additional_data != NULL );
2625 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2626 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2627 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2628 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2629
Gilles Peskine4abf7412018-06-18 16:35:34 +02002630 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002631 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002632
2633 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2634
2635 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002636 psa_key_policy_set_usage( &policy,
2637 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2638 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002639 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2640
2641 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002642 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002643
2644 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002645 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002646 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002647 additional_data->len,
2648 input_data->x, input_data->len,
2649 output_data, output_size,
2650 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002651
2652 if( PSA_SUCCESS == expected_result )
2653 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002654 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002655
2656 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002657 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002658 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002659 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002660 output_data, output_length,
2661 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002662 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002663
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002664 ASSERT_COMPARE( input_data->x, input_data->len,
2665 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002666 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002667
Gilles Peskinea1cac842018-06-11 19:33:02 +02002668exit:
2669 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002670 mbedtls_free( output_data );
2671 mbedtls_free( output_data2 );
2672 mbedtls_psa_crypto_free( );
2673}
2674/* END_CASE */
2675
2676/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002677void aead_encrypt( int key_type_arg, data_t *key_data,
2678 int alg_arg,
2679 data_t *nonce,
2680 data_t *additional_data,
2681 data_t *input_data,
2682 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002683{
2684 int slot = 1;
2685 psa_key_type_t key_type = key_type_arg;
2686 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002687 unsigned char *output_data = NULL;
2688 size_t output_size = 0;
2689 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002690 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002691 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002692
Gilles Peskinea1cac842018-06-11 19:33:02 +02002693 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002694 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002695 TEST_ASSERT( additional_data != NULL );
2696 TEST_ASSERT( nonce != NULL );
2697 TEST_ASSERT( expected_result != NULL );
2698 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2699 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2700 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2701 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2702 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2703
Gilles Peskine4abf7412018-06-18 16:35:34 +02002704 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002705 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002706
Gilles Peskinea1cac842018-06-11 19:33:02 +02002707 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2708
2709 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002710 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002711 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2712
2713 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002714 key_data->x,
2715 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716
2717 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002718 nonce->x, nonce->len,
2719 additional_data->x, additional_data->len,
2720 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002721 output_data, output_size,
2722 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002723
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002724 ASSERT_COMPARE( expected_result->x, expected_result->len,
2725 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002726
Gilles Peskinea1cac842018-06-11 19:33:02 +02002727exit:
2728 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002729 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002730 mbedtls_psa_crypto_free( );
2731}
2732/* END_CASE */
2733
2734/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002735void aead_decrypt( int key_type_arg, data_t *key_data,
2736 int alg_arg,
2737 data_t *nonce,
2738 data_t *additional_data,
2739 data_t *input_data,
2740 data_t *expected_data,
2741 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002742{
2743 int slot = 1;
2744 psa_key_type_t key_type = key_type_arg;
2745 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002746 unsigned char *output_data = NULL;
2747 size_t output_size = 0;
2748 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002749 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002750 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002751 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002752
Gilles Peskinea1cac842018-06-11 19:33:02 +02002753 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002754 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002755 TEST_ASSERT( additional_data != NULL );
2756 TEST_ASSERT( nonce != NULL );
2757 TEST_ASSERT( expected_data != NULL );
2758 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2759 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2760 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2761 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2762 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2763
Gilles Peskine4abf7412018-06-18 16:35:34 +02002764 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002765 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002766
Gilles Peskinea1cac842018-06-11 19:33:02 +02002767 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2768
2769 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002770 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002771 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2772
2773 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002774 key_data->x,
2775 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002776
2777 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002778 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002779 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002780 additional_data->len,
2781 input_data->x, input_data->len,
2782 output_data, output_size,
2783 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002784
Gilles Peskine2d277862018-06-18 15:41:12 +02002785 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002786 ASSERT_COMPARE( expected_data->x, expected_data->len,
2787 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002788
Gilles Peskinea1cac842018-06-11 19:33:02 +02002789exit:
2790 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002791 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002792 mbedtls_psa_crypto_free( );
2793}
2794/* END_CASE */
2795
2796/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002797void signature_size( int type_arg,
2798 int bits,
2799 int alg_arg,
2800 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002801{
2802 psa_key_type_t type = type_arg;
2803 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002804 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002805 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2806exit:
2807 ;
2808}
2809/* END_CASE */
2810
2811/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002812void sign_deterministic( int key_type_arg, data_t *key_data,
2813 int alg_arg, data_t *input_data,
2814 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002815{
2816 int slot = 1;
2817 psa_key_type_t key_type = key_type_arg;
2818 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002819 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002820 unsigned char *signature = NULL;
2821 size_t signature_size;
2822 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002823 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002824
Gilles Peskine20035e32018-02-03 22:44:14 +01002825 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002826 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002827 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002828 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2829 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2830 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002831
2832 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2833
mohammad1603a97cb8c2018-03-28 03:46:26 -07002834 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002835 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002836 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2837
Gilles Peskine20035e32018-02-03 22:44:14 +01002838 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002839 key_data->x,
2840 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002841 TEST_ASSERT( psa_get_key_information( slot,
2842 NULL,
2843 &key_bits ) == PSA_SUCCESS );
2844
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002845 /* Allocate a buffer which has the size advertized by the
2846 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002847 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2848 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002849 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002850 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002851 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002852
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002853 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002854 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002855 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002856 signature, signature_size,
2857 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002858 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002859 ASSERT_COMPARE( output_data->x, output_data->len,
2860 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002861
2862exit:
2863 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002864 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002865 mbedtls_psa_crypto_free( );
2866}
2867/* END_CASE */
2868
2869/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002870void sign_fail( int key_type_arg, data_t *key_data,
2871 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002872 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002873{
2874 int slot = 1;
2875 psa_key_type_t key_type = key_type_arg;
2876 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002877 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002878 psa_status_t actual_status;
2879 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002880 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002881 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002882 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002883
Gilles Peskine20035e32018-02-03 22:44:14 +01002884 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002885 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002886 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2887 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2888
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002889 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002890
2891 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2892
mohammad1603a97cb8c2018-03-28 03:46:26 -07002893 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002894 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002895 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2896
Gilles Peskine20035e32018-02-03 22:44:14 +01002897 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002898 key_data->x,
2899 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002900
2901 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002902 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002903 signature, signature_size,
2904 &signature_length );
2905 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002906 /* The value of *signature_length is unspecified on error, but
2907 * whatever it is, it should be less than signature_size, so that
2908 * if the caller tries to read *signature_length bytes without
2909 * checking the error code then they don't overflow a buffer. */
2910 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002911
2912exit:
2913 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002914 mbedtls_free( signature );
2915 mbedtls_psa_crypto_free( );
2916}
2917/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002918
2919/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002920void sign_verify( int key_type_arg, data_t *key_data,
2921 int alg_arg, data_t *input_data )
2922{
2923 int slot = 1;
2924 psa_key_type_t key_type = key_type_arg;
2925 psa_algorithm_t alg = alg_arg;
2926 size_t key_bits;
2927 unsigned char *signature = NULL;
2928 size_t signature_size;
2929 size_t signature_length = 0xdeadbeef;
2930 psa_key_policy_t policy;
2931
2932 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2933
2934 psa_key_policy_init( &policy );
2935 psa_key_policy_set_usage( &policy,
2936 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2937 alg );
2938 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2939
2940 TEST_ASSERT( psa_import_key( slot, key_type,
2941 key_data->x,
2942 key_data->len ) == PSA_SUCCESS );
2943 TEST_ASSERT( psa_get_key_information( slot,
2944 NULL,
2945 &key_bits ) == PSA_SUCCESS );
2946
2947 /* Allocate a buffer which has the size advertized by the
2948 * library. */
2949 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2950 key_bits, alg );
2951 TEST_ASSERT( signature_size != 0 );
2952 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002953 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002954
2955 /* Perform the signature. */
2956 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2957 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002958 signature, signature_size,
2959 &signature_length ) == PSA_SUCCESS );
2960 /* Check that the signature length looks sensible. */
2961 TEST_ASSERT( signature_length <= signature_size );
2962 TEST_ASSERT( signature_length > 0 );
2963
2964 /* Use the library to verify that the signature is correct. */
2965 TEST_ASSERT( psa_asymmetric_verify(
2966 slot, alg,
2967 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002968 signature, signature_length ) == PSA_SUCCESS );
2969
2970 if( input_data->len != 0 )
2971 {
2972 /* Flip a bit in the input and verify that the signature is now
2973 * detected as invalid. Flip a bit at the beginning, not at the end,
2974 * because ECDSA may ignore the last few bits of the input. */
2975 input_data->x[0] ^= 1;
2976 TEST_ASSERT( psa_asymmetric_verify(
2977 slot, alg,
2978 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002979 signature,
2980 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2981 }
2982
2983exit:
2984 psa_destroy_key( slot );
2985 mbedtls_free( signature );
2986 mbedtls_psa_crypto_free( );
2987}
2988/* END_CASE */
2989
2990/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002991void asymmetric_verify( int key_type_arg, data_t *key_data,
2992 int alg_arg, data_t *hash_data,
2993 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002994{
2995 int slot = 1;
2996 psa_key_type_t key_type = key_type_arg;
2997 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002998 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002999
Gilles Peskine69c12672018-06-28 00:07:19 +02003000 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3001
itayzafrir5c753392018-05-08 11:18:38 +03003002 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003003 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003004 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003005 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3006 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3007 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003008
3009 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3010
3011 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003012 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03003013 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3014
3015 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003016 key_data->x,
3017 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003018
3019 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003020 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003021 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003022 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003023exit:
3024 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03003025 mbedtls_psa_crypto_free( );
3026}
3027/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003028
3029/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003030void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3031 int alg_arg, data_t *hash_data,
3032 data_t *signature_data,
3033 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003034{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003035 int slot = 1;
3036 psa_key_type_t key_type = key_type_arg;
3037 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003038 psa_status_t actual_status;
3039 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003040 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003041
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003042 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003043 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003044 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003045 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3046 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3047 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003048
3049 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3050
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003051 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003052 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003053 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3054
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003055 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003056 key_data->x,
3057 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003058
3059 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003060 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003061 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003062 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003063
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003064 TEST_ASSERT( actual_status == expected_status );
3065
3066exit:
3067 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003068 mbedtls_psa_crypto_free( );
3069}
3070/* END_CASE */
3071
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003072/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003073void asymmetric_encrypt( int key_type_arg,
3074 data_t *key_data,
3075 int alg_arg,
3076 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003077 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003078 int expected_output_length_arg,
3079 int expected_status_arg )
3080{
3081 int slot = 1;
3082 psa_key_type_t key_type = key_type_arg;
3083 psa_algorithm_t alg = alg_arg;
3084 size_t expected_output_length = expected_output_length_arg;
3085 size_t key_bits;
3086 unsigned char *output = NULL;
3087 size_t output_size;
3088 size_t output_length = ~0;
3089 psa_status_t actual_status;
3090 psa_status_t expected_status = expected_status_arg;
3091 psa_key_policy_t policy;
3092
3093 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3094
3095 /* Import the key */
3096 psa_key_policy_init( &policy );
3097 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
3098 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3099 TEST_ASSERT( psa_import_key( slot, key_type,
3100 key_data->x,
3101 key_data->len ) == PSA_SUCCESS );
3102
3103 /* Determine the maximum output length */
3104 TEST_ASSERT( psa_get_key_information( slot,
3105 NULL,
3106 &key_bits ) == PSA_SUCCESS );
3107 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003108 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003109
3110 /* Encrypt the input */
3111 actual_status = psa_asymmetric_encrypt( slot, alg,
3112 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003113 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003114 output, output_size,
3115 &output_length );
3116 TEST_ASSERT( actual_status == expected_status );
3117 TEST_ASSERT( output_length == expected_output_length );
3118
Gilles Peskine68428122018-06-30 18:42:41 +02003119 /* If the label is empty, the test framework puts a non-null pointer
3120 * in label->x. Test that a null pointer works as well. */
3121 if( label->len == 0 )
3122 {
3123 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003124 if( output_size != 0 )
3125 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003126 actual_status = psa_asymmetric_encrypt( slot, alg,
3127 input_data->x, input_data->len,
3128 NULL, label->len,
3129 output, output_size,
3130 &output_length );
3131 TEST_ASSERT( actual_status == expected_status );
3132 TEST_ASSERT( output_length == expected_output_length );
3133 }
3134
Gilles Peskine656896e2018-06-29 19:12:28 +02003135exit:
3136 psa_destroy_key( slot );
3137 mbedtls_free( output );
3138 mbedtls_psa_crypto_free( );
3139}
3140/* END_CASE */
3141
3142/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003143void asymmetric_encrypt_decrypt( int key_type_arg,
3144 data_t *key_data,
3145 int alg_arg,
3146 data_t *input_data,
3147 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003148{
3149 int slot = 1;
3150 psa_key_type_t key_type = key_type_arg;
3151 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003152 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003153 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003154 size_t output_size;
3155 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003156 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003157 size_t output2_size;
3158 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003159 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003160
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003161 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003162 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003163 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3164 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3165
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3167
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003168 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003169 psa_key_policy_set_usage( &policy,
3170 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003171 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003172 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3173
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003174 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003175 key_data->x,
3176 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003178
3179 /* Determine the maximum ciphertext length */
3180 TEST_ASSERT( psa_get_key_information( slot,
3181 NULL,
3182 &key_bits ) == PSA_SUCCESS );
3183 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003184 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003185 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003186 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003187
Gilles Peskineeebd7382018-06-08 18:11:54 +02003188 /* We test encryption by checking that encrypt-then-decrypt gives back
3189 * the original plaintext because of the non-optional random
3190 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02003191 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003192 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003193 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003194 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003195 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003196 /* We don't know what ciphertext length to expect, but check that
3197 * it looks sensible. */
3198 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003199
Gilles Peskine2d277862018-06-18 15:41:12 +02003200 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003201 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02003202 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003203 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003204 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003205 ASSERT_COMPARE( input_data->x, input_data->len,
3206 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003207
3208exit:
3209 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003210 mbedtls_free( output );
3211 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003213}
3214/* END_CASE */
3215
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003216/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003217void asymmetric_decrypt( int key_type_arg,
3218 data_t *key_data,
3219 int alg_arg,
3220 data_t *input_data,
3221 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003222 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003223{
3224 int slot = 1;
3225 psa_key_type_t key_type = key_type_arg;
3226 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003227 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003228 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003229 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003230 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003231
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003232 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003233 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003234 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003235 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3236 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3237 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3238
Gilles Peskine4abf7412018-06-18 16:35:34 +02003239 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003240 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003241
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3243
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003244 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003245 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003246 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3247
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003248 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003249 key_data->x,
3250 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003251
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003252 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003253 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003254 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003255 output,
3256 output_size,
3257 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003258 ASSERT_COMPARE( expected_data->x, expected_data->len,
3259 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003260
Gilles Peskine68428122018-06-30 18:42:41 +02003261 /* If the label is empty, the test framework puts a non-null pointer
3262 * in label->x. Test that a null pointer works as well. */
3263 if( label->len == 0 )
3264 {
3265 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003266 if( output_size != 0 )
3267 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003268 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
3269 input_data->x, input_data->len,
3270 NULL, label->len,
3271 output,
3272 output_size,
3273 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003274 ASSERT_COMPARE( expected_data->x, expected_data->len,
3275 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003276 }
3277
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003278exit:
3279 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003280 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003282}
3283/* END_CASE */
3284
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003286void asymmetric_decrypt_fail( int key_type_arg,
3287 data_t *key_data,
3288 int alg_arg,
3289 data_t *input_data,
3290 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003291 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003292{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003293 int slot = 1;
3294 psa_key_type_t key_type = key_type_arg;
3295 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003296 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003297 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003298 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003299 psa_status_t actual_status;
3300 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003301 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003303 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003305 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3306 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3307
Gilles Peskine4abf7412018-06-18 16:35:34 +02003308 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003309 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003310
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003311 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3312
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003313 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003314 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003315 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3316
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003317 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003318 key_data->x,
3319 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003320
Gilles Peskine2d277862018-06-18 15:41:12 +02003321 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003322 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003323 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003324 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003325 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003326 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003327 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003328
Gilles Peskine68428122018-06-30 18:42:41 +02003329 /* If the label is empty, the test framework puts a non-null pointer
3330 * in label->x. Test that a null pointer works as well. */
3331 if( label->len == 0 )
3332 {
3333 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003334 if( output_size != 0 )
3335 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003336 actual_status = psa_asymmetric_decrypt( slot, alg,
3337 input_data->x, input_data->len,
3338 NULL, label->len,
3339 output, output_size,
3340 &output_length );
3341 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003342 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003343 }
3344
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003345exit:
3346 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003347 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003348 mbedtls_psa_crypto_free( );
3349}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003350/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003351
3352/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003353void derive_setup( int key_type_arg,
3354 data_t *key_data,
3355 int alg_arg,
3356 data_t *salt,
3357 data_t *label,
3358 int requested_capacity_arg,
3359 int expected_status_arg )
3360{
3361 psa_key_slot_t slot = 1;
3362 size_t key_type = key_type_arg;
3363 psa_algorithm_t alg = alg_arg;
3364 size_t requested_capacity = requested_capacity_arg;
3365 psa_status_t expected_status = expected_status_arg;
3366 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3367 psa_key_policy_t policy;
3368
3369 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3370
3371 psa_key_policy_init( &policy );
3372 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3373 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3374
3375 TEST_ASSERT( psa_import_key( slot, key_type,
3376 key_data->x,
3377 key_data->len ) == PSA_SUCCESS );
3378
3379 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3380 salt->x, salt->len,
3381 label->x, label->len,
3382 requested_capacity ) == expected_status );
3383
3384exit:
3385 psa_generator_abort( &generator );
3386 psa_destroy_key( slot );
3387 mbedtls_psa_crypto_free( );
3388}
3389/* END_CASE */
3390
3391/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003392void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003393{
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003394 psa_key_slot_t base_key = 1;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003395 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003396 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003397 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003398 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003399 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003400 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3401 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3402 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003403 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003404
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003405 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3406
3407 psa_key_policy_init( &policy );
3408 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3409 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3410
3411 TEST_ASSERT( psa_import_key( base_key, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003412 key_data,
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003413 sizeof( key_data ) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003414
3415 /* valid key derivation */
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003416 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003417 NULL, 0,
3418 NULL, 0,
3419 capacity ) == PSA_SUCCESS );
3420
3421 /* state of generator shouldn't allow additional generation */
3422 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3423 NULL, 0,
3424 NULL, 0,
3425 capacity ) == PSA_ERROR_BAD_STATE );
3426
3427 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3428 == PSA_SUCCESS );
3429
3430 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3431 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3432
3433
3434exit:
3435 psa_generator_abort( &generator );
3436 psa_destroy_key( base_key );
3437 mbedtls_psa_crypto_free( );
3438}
3439/* END_CASE */
3440
3441
3442/* BEGIN_CASE */
3443void test_derive_invalid_generator_tests( )
3444{
3445 uint8_t output_buffer[16];
3446 size_t buffer_size = 16;
3447 size_t capacity = 0;
3448 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3449
Nir Sonnenschein50789302018-10-31 12:16:38 +02003450 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003451 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003452
3453 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003454 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003455
Nir Sonnenschein50789302018-10-31 12:16:38 +02003456 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003457
Nir Sonnenschein50789302018-10-31 12:16:38 +02003458 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003459 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003460
Nir Sonnenschein50789302018-10-31 12:16:38 +02003461 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003462 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003463
3464exit:
3465 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003466}
3467/* END_CASE */
3468
3469/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003470void derive_output( int alg_arg,
3471 data_t *key_data,
3472 data_t *salt,
3473 data_t *label,
3474 int requested_capacity_arg,
3475 data_t *expected_output1,
3476 data_t *expected_output2 )
3477{
3478 psa_key_slot_t slot = 1;
3479 psa_algorithm_t alg = alg_arg;
3480 size_t requested_capacity = requested_capacity_arg;
3481 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3482 uint8_t *expected_outputs[2] =
3483 {expected_output1->x, expected_output2->x};
3484 size_t output_sizes[2] =
3485 {expected_output1->len, expected_output2->len};
3486 size_t output_buffer_size = 0;
3487 uint8_t *output_buffer = NULL;
3488 size_t expected_capacity;
3489 size_t current_capacity;
3490 psa_key_policy_t policy;
3491 psa_status_t status;
3492 unsigned i;
3493
3494 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3495 {
3496 if( output_sizes[i] > output_buffer_size )
3497 output_buffer_size = output_sizes[i];
3498 if( output_sizes[i] == 0 )
3499 expected_outputs[i] = NULL;
3500 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003501 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003502 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3503
3504 psa_key_policy_init( &policy );
3505 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3506 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3507
3508 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3509 key_data->x,
3510 key_data->len ) == PSA_SUCCESS );
3511
3512 /* Extraction phase. */
3513 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3514 salt->x, salt->len,
3515 label->x, label->len,
3516 requested_capacity ) == PSA_SUCCESS );
3517 TEST_ASSERT( psa_get_generator_capacity( &generator,
3518 &current_capacity ) ==
3519 PSA_SUCCESS );
3520 TEST_ASSERT( current_capacity == requested_capacity );
3521 expected_capacity = requested_capacity;
3522
3523 /* Expansion phase. */
3524 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3525 {
3526 /* Read some bytes. */
3527 status = psa_generator_read( &generator,
3528 output_buffer, output_sizes[i] );
3529 if( expected_capacity == 0 && output_sizes[i] == 0 )
3530 {
3531 /* Reading 0 bytes when 0 bytes are available can go either way. */
3532 TEST_ASSERT( status == PSA_SUCCESS ||
3533 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3534 continue;
3535 }
3536 else if( expected_capacity == 0 ||
3537 output_sizes[i] > expected_capacity )
3538 {
3539 /* Capacity exceeded. */
3540 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3541 expected_capacity = 0;
3542 continue;
3543 }
3544 /* Success. Check the read data. */
3545 TEST_ASSERT( status == PSA_SUCCESS );
3546 if( output_sizes[i] != 0 )
3547 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3548 output_sizes[i] ) == 0 );
3549 /* Check the generator status. */
3550 expected_capacity -= output_sizes[i];
3551 TEST_ASSERT( psa_get_generator_capacity( &generator,
3552 &current_capacity ) ==
3553 PSA_SUCCESS );
3554 TEST_ASSERT( expected_capacity == current_capacity );
3555 }
3556 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3557
3558exit:
3559 mbedtls_free( output_buffer );
3560 psa_generator_abort( &generator );
3561 psa_destroy_key( slot );
3562 mbedtls_psa_crypto_free( );
3563}
3564/* END_CASE */
3565
3566/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003567void derive_full( int alg_arg,
3568 data_t *key_data,
3569 data_t *salt,
3570 data_t *label,
3571 int requested_capacity_arg )
3572{
3573 psa_key_slot_t slot = 1;
3574 psa_algorithm_t alg = alg_arg;
3575 size_t requested_capacity = requested_capacity_arg;
3576 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3577 unsigned char output_buffer[16];
3578 size_t expected_capacity = requested_capacity;
3579 size_t current_capacity;
3580 psa_key_policy_t policy;
3581
3582 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3583
3584 psa_key_policy_init( &policy );
3585 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3586 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3587
3588 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3589 key_data->x,
3590 key_data->len ) == PSA_SUCCESS );
3591
3592 /* Extraction phase. */
3593 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3594 salt->x, salt->len,
3595 label->x, label->len,
3596 requested_capacity ) == PSA_SUCCESS );
3597 TEST_ASSERT( psa_get_generator_capacity( &generator,
3598 &current_capacity ) ==
3599 PSA_SUCCESS );
3600 TEST_ASSERT( current_capacity == expected_capacity );
3601
3602 /* Expansion phase. */
3603 while( current_capacity > 0 )
3604 {
3605 size_t read_size = sizeof( output_buffer );
3606 if( read_size > current_capacity )
3607 read_size = current_capacity;
3608 TEST_ASSERT( psa_generator_read( &generator,
3609 output_buffer,
3610 read_size ) == PSA_SUCCESS );
3611 expected_capacity -= read_size;
3612 TEST_ASSERT( psa_get_generator_capacity( &generator,
3613 &current_capacity ) ==
3614 PSA_SUCCESS );
3615 TEST_ASSERT( current_capacity == expected_capacity );
3616 }
3617
3618 /* Check that the generator refuses to go over capacity. */
3619 TEST_ASSERT( psa_generator_read( &generator,
3620 output_buffer,
3621 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3622
3623 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3624
3625exit:
3626 psa_generator_abort( &generator );
3627 psa_destroy_key( slot );
3628 mbedtls_psa_crypto_free( );
3629}
3630/* END_CASE */
3631
3632/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003633void derive_key_exercise( int alg_arg,
3634 data_t *key_data,
3635 data_t *salt,
3636 data_t *label,
3637 int derived_type_arg,
3638 int derived_bits_arg,
3639 int derived_usage_arg,
3640 int derived_alg_arg )
3641{
3642 psa_key_slot_t base_key = 1;
3643 psa_key_slot_t derived_key = 2;
3644 psa_algorithm_t alg = alg_arg;
3645 psa_key_type_t derived_type = derived_type_arg;
3646 size_t derived_bits = derived_bits_arg;
3647 psa_key_usage_t derived_usage = derived_usage_arg;
3648 psa_algorithm_t derived_alg = derived_alg_arg;
3649 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3650 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3651 psa_key_policy_t policy;
3652 psa_key_type_t got_type;
3653 size_t got_bits;
3654
3655 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3656
3657 psa_key_policy_init( &policy );
3658 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3659 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3660 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3661 key_data->x,
3662 key_data->len ) == PSA_SUCCESS );
3663
3664 /* Derive a key. */
3665 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3666 salt->x, salt->len,
3667 label->x, label->len,
3668 capacity ) == PSA_SUCCESS );
3669 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3670 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3671 TEST_ASSERT( psa_generator_import_key( derived_key,
3672 derived_type,
3673 derived_bits,
3674 &generator ) == PSA_SUCCESS );
3675
3676 /* Test the key information */
3677 TEST_ASSERT( psa_get_key_information( derived_key,
3678 &got_type,
3679 &got_bits ) == PSA_SUCCESS );
3680 TEST_ASSERT( got_type == derived_type );
3681 TEST_ASSERT( got_bits == derived_bits );
3682
3683 /* Exercise the derived key. */
3684 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3685 goto exit;
3686
3687exit:
3688 psa_generator_abort( &generator );
3689 psa_destroy_key( base_key );
3690 psa_destroy_key( derived_key );
3691 mbedtls_psa_crypto_free( );
3692}
3693/* END_CASE */
3694
3695/* BEGIN_CASE */
3696void derive_key_export( int alg_arg,
3697 data_t *key_data,
3698 data_t *salt,
3699 data_t *label,
3700 int bytes1_arg,
3701 int bytes2_arg )
3702{
3703 psa_key_slot_t base_key = 1;
3704 psa_key_slot_t derived_key = 2;
3705 psa_algorithm_t alg = alg_arg;
3706 size_t bytes1 = bytes1_arg;
3707 size_t bytes2 = bytes2_arg;
3708 size_t capacity = bytes1 + bytes2;
3709 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003710 uint8_t *output_buffer = NULL;
3711 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003712 psa_key_policy_t policy;
3713 size_t length;
3714
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003715 ASSERT_ALLOC( output_buffer, capacity );
3716 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003717 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3718
3719 psa_key_policy_init( &policy );
3720 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3721 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3722 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3723 key_data->x,
3724 key_data->len ) == PSA_SUCCESS );
3725
3726 /* Derive some material and output it. */
3727 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3728 salt->x, salt->len,
3729 label->x, label->len,
3730 capacity ) == PSA_SUCCESS );
3731 TEST_ASSERT( psa_generator_read( &generator,
3732 output_buffer,
3733 capacity ) == PSA_SUCCESS );
3734 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3735
3736 /* Derive the same output again, but this time store it in key objects. */
3737 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3738 salt->x, salt->len,
3739 label->x, label->len,
3740 capacity ) == PSA_SUCCESS );
3741 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3742 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3743 TEST_ASSERT( psa_generator_import_key( derived_key,
3744 PSA_KEY_TYPE_RAW_DATA,
3745 PSA_BYTES_TO_BITS( bytes1 ),
3746 &generator ) == PSA_SUCCESS );
3747 TEST_ASSERT( psa_export_key( derived_key,
3748 export_buffer, bytes1,
3749 &length ) == PSA_SUCCESS );
3750 TEST_ASSERT( length == bytes1 );
3751 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3752 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3753 TEST_ASSERT( psa_generator_import_key( derived_key,
3754 PSA_KEY_TYPE_RAW_DATA,
3755 PSA_BYTES_TO_BITS( bytes2 ),
3756 &generator ) == PSA_SUCCESS );
3757 TEST_ASSERT( psa_export_key( derived_key,
3758 export_buffer + bytes1, bytes2,
3759 &length ) == PSA_SUCCESS );
3760 TEST_ASSERT( length == bytes2 );
3761
3762 /* Compare the outputs from the two runs. */
3763 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3764
3765exit:
3766 mbedtls_free( output_buffer );
3767 mbedtls_free( export_buffer );
3768 psa_generator_abort( &generator );
3769 psa_destroy_key( base_key );
3770 psa_destroy_key( derived_key );
3771 mbedtls_psa_crypto_free( );
3772}
3773/* END_CASE */
3774
3775/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003776void key_agreement_setup( int alg_arg,
3777 int our_key_type_arg, data_t *our_key_data,
3778 data_t *peer_key_data,
3779 int expected_status_arg )
3780{
3781 psa_key_slot_t our_key = 1;
3782 psa_algorithm_t alg = alg_arg;
3783 psa_key_type_t our_key_type = our_key_type_arg;
3784 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3785 psa_key_policy_t policy;
3786
3787 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3788
3789 psa_key_policy_init( &policy );
3790 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3791 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3792 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3793 our_key_data->x,
3794 our_key_data->len ) == PSA_SUCCESS );
3795
3796 TEST_ASSERT( psa_key_agreement( &generator,
3797 our_key,
3798 peer_key_data->x, peer_key_data->len,
3799 alg ) == expected_status_arg );
3800
3801exit:
3802 psa_generator_abort( &generator );
3803 psa_destroy_key( our_key );
3804 mbedtls_psa_crypto_free( );
3805}
3806/* END_CASE */
3807
3808/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003809void key_agreement_capacity( int alg_arg,
3810 int our_key_type_arg, data_t *our_key_data,
3811 data_t *peer_key_data,
3812 int expected_capacity_arg )
3813{
3814 psa_key_slot_t our_key = 1;
3815 psa_algorithm_t alg = alg_arg;
3816 psa_key_type_t our_key_type = our_key_type_arg;
3817 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3818 psa_key_policy_t policy;
3819 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003820 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003821
3822 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3823
3824 psa_key_policy_init( &policy );
3825 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3826 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3827 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3828 our_key_data->x,
3829 our_key_data->len ) == PSA_SUCCESS );
3830
3831 TEST_ASSERT( psa_key_agreement( &generator,
3832 our_key,
3833 peer_key_data->x, peer_key_data->len,
3834 alg ) == PSA_SUCCESS );
3835
Gilles Peskinebf491972018-10-25 22:36:12 +02003836 /* Test the advertized capacity. */
Gilles Peskine59685592018-09-18 12:11:34 +02003837 TEST_ASSERT( psa_get_generator_capacity(
3838 &generator, &actual_capacity ) == PSA_SUCCESS );
3839 TEST_ASSERT( actual_capacity == (size_t) expected_capacity_arg );
3840
Gilles Peskinebf491972018-10-25 22:36:12 +02003841 /* Test the actual capacity by reading the output. */
3842 while( actual_capacity > sizeof( output ) )
3843 {
3844 TEST_ASSERT( psa_generator_read( &generator,
3845 output, sizeof( output ) ) ==
3846 PSA_SUCCESS );
3847 actual_capacity -= sizeof( output );
3848 }
3849 TEST_ASSERT( psa_generator_read( &generator,
3850 output, actual_capacity ) ==
3851 PSA_SUCCESS );
3852 TEST_ASSERT( psa_generator_read( &generator, output, 1 ) ==
3853 PSA_ERROR_INSUFFICIENT_CAPACITY );
3854
Gilles Peskine59685592018-09-18 12:11:34 +02003855exit:
3856 psa_generator_abort( &generator );
3857 psa_destroy_key( our_key );
3858 mbedtls_psa_crypto_free( );
3859}
3860/* END_CASE */
3861
3862/* BEGIN_CASE */
3863void key_agreement_output( int alg_arg,
3864 int our_key_type_arg, data_t *our_key_data,
3865 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003866 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003867{
3868 psa_key_slot_t our_key = 1;
3869 psa_algorithm_t alg = alg_arg;
3870 psa_key_type_t our_key_type = our_key_type_arg;
3871 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3872 psa_key_policy_t policy;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003873 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003874
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003875 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3876 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003877
3878 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3879
3880 psa_key_policy_init( &policy );
3881 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3882 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3883 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3884 our_key_data->x,
3885 our_key_data->len ) == PSA_SUCCESS );
3886
3887 TEST_ASSERT( psa_key_agreement( &generator,
3888 our_key,
3889 peer_key_data->x, peer_key_data->len,
3890 alg ) == PSA_SUCCESS );
3891
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003892 TEST_ASSERT(
3893 psa_generator_read( &generator,
3894 actual_output,
3895 expected_output1->len ) == PSA_SUCCESS );
3896 TEST_ASSERT( memcmp( actual_output, expected_output1->x,
3897 expected_output1->len ) == 0 );
3898 if( expected_output2->len != 0 )
3899 {
3900 TEST_ASSERT(
3901 psa_generator_read( &generator,
3902 actual_output,
3903 expected_output2->len ) == PSA_SUCCESS );
3904 TEST_ASSERT( memcmp( actual_output, expected_output2->x,
3905 expected_output2->len ) == 0 );
3906 }
Gilles Peskine59685592018-09-18 12:11:34 +02003907
3908exit:
3909 psa_generator_abort( &generator );
3910 psa_destroy_key( our_key );
3911 mbedtls_psa_crypto_free( );
3912 mbedtls_free( actual_output );
3913}
3914/* END_CASE */
3915
3916/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003917void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003918{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003919 size_t bytes = bytes_arg;
3920 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003921 unsigned char *output = NULL;
3922 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003923 size_t i;
3924 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003925
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003926 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3927 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003928 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003929
3930 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3931
Gilles Peskinea50d7392018-06-21 10:22:13 +02003932 /* Run several times, to ensure that every output byte will be
3933 * nonzero at least once with overwhelming probability
3934 * (2^(-8*number_of_runs)). */
3935 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003936 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003937 if( bytes != 0 )
3938 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003939 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3940
3941 /* Check that no more than bytes have been overwritten */
3942 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3943
3944 for( i = 0; i < bytes; i++ )
3945 {
3946 if( output[i] != 0 )
3947 ++changed[i];
3948 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003949 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003950
3951 /* Check that every byte was changed to nonzero at least once. This
3952 * validates that psa_generate_random is overwriting every byte of
3953 * the output buffer. */
3954 for( i = 0; i < bytes; i++ )
3955 {
3956 TEST_ASSERT( changed[i] != 0 );
3957 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003958
3959exit:
3960 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003961 mbedtls_free( output );
3962 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003963}
3964/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003965
3966/* BEGIN_CASE */
3967void generate_key( int type_arg,
3968 int bits_arg,
3969 int usage_arg,
3970 int alg_arg,
3971 int expected_status_arg )
3972{
3973 int slot = 1;
3974 psa_key_type_t type = type_arg;
3975 psa_key_usage_t usage = usage_arg;
3976 size_t bits = bits_arg;
3977 psa_algorithm_t alg = alg_arg;
3978 psa_status_t expected_status = expected_status_arg;
3979 psa_key_type_t got_type;
3980 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003981 psa_status_t expected_info_status =
3982 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3983 psa_key_policy_t policy;
3984
3985 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3986
3987 psa_key_policy_init( &policy );
3988 psa_key_policy_set_usage( &policy, usage, alg );
3989 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3990
3991 /* Generate a key */
3992 TEST_ASSERT( psa_generate_key( slot, type, bits,
3993 NULL, 0 ) == expected_status );
3994
3995 /* Test the key information */
3996 TEST_ASSERT( psa_get_key_information( slot,
3997 &got_type,
3998 &got_bits ) == expected_info_status );
3999 if( expected_info_status != PSA_SUCCESS )
4000 goto exit;
4001 TEST_ASSERT( got_type == type );
4002 TEST_ASSERT( got_bits == bits );
4003
Gilles Peskine818ca122018-06-20 18:16:48 +02004004 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02004005 if( ! exercise_key( slot, usage, alg ) )
4006 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004007
4008exit:
4009 psa_destroy_key( slot );
4010 mbedtls_psa_crypto_free( );
4011}
4012/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004013
4014/* BEGIN_CASE */
4015void validate_module_init_generate_random( )
4016{
4017 psa_status_t status;
4018 uint8_t random[10] = { 0 };
4019 status = psa_generate_random( random, sizeof( random ) );
4020 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
4021}
4022/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03004023
4024/* BEGIN_CASE */
4025void validate_module_init_key_based( )
4026{
4027 psa_status_t status;
4028 uint8_t data[10] = { 0 };
4029 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
4030 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
4031}
4032/* END_CASE */
Darryl Greend49a4992018-06-18 17:27:26 +01004033
4034/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4035void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4036 int bits, int usage_arg,
4037 int alg_arg )
4038{
4039 psa_key_slot_t slot = 1;
4040 psa_key_type_t type = (psa_key_type_t) type_arg;
4041 psa_key_type_t type_get;
4042 size_t bits_get;
4043 psa_key_policy_t policy_set;
4044 psa_key_policy_t policy_get;
4045 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4046 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
4047 unsigned char *first_export = NULL;
4048 unsigned char *second_export = NULL;
4049 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4050 size_t first_exported_length;
4051 size_t second_exported_length;
4052
4053 ASSERT_ALLOC( first_export, export_size );
4054 ASSERT_ALLOC( second_export, export_size );
4055
4056 TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS );
4057
4058 TEST_ASSERT( psa_set_key_lifetime(
4059 slot, PSA_KEY_LIFETIME_PERSISTENT ) == PSA_SUCCESS );
4060
4061 psa_key_policy_init( &policy_set );
4062
4063 psa_key_policy_set_usage( &policy_set, policy_usage,
4064 policy_alg );
4065
4066 TEST_ASSERT( psa_set_key_policy( slot, &policy_set ) == PSA_SUCCESS );
4067
4068 /* Import the key */
4069 TEST_ASSERT( psa_import_key( slot, type,
4070 data->x, data->len ) == PSA_SUCCESS );
4071
4072 /* Export the key */
4073 TEST_ASSERT( psa_export_key( slot, first_export, export_size,
4074 &first_exported_length ) == PSA_SUCCESS );
4075
4076 /* Shutdown and restart */
4077 mbedtls_psa_crypto_free();
4078
4079 TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS );
4080
4081 /* Mark slot as persistent again */
4082 TEST_ASSERT( psa_set_key_lifetime(
4083 slot, PSA_KEY_LIFETIME_PERSISTENT ) == PSA_SUCCESS );
4084
4085 /* Check key slot still contains key data */
4086 TEST_ASSERT( psa_get_key_information(
4087 slot, &type_get, &bits_get ) == PSA_SUCCESS );
4088 TEST_ASSERT( type_get == type );
4089 TEST_ASSERT( bits_get == (size_t) bits );
4090
4091 TEST_ASSERT( psa_get_key_policy( slot, &policy_get ) == PSA_SUCCESS );
4092 TEST_ASSERT( psa_key_policy_get_usage(
4093 &policy_get ) == policy_usage );
4094 TEST_ASSERT( psa_key_policy_get_algorithm(
4095 &policy_get ) == policy_alg );
4096
4097 /* Export the key again */
4098 TEST_ASSERT( psa_export_key( slot, second_export, export_size,
4099 &second_exported_length ) == PSA_SUCCESS );
4100
4101 ASSERT_COMPARE( first_export, first_exported_length,
4102 second_export, second_exported_length );
4103
4104 ASSERT_COMPARE( data->x, data->len,
4105 first_export, first_exported_length );
4106
4107exit:
4108 mbedtls_free( first_export );
4109 mbedtls_free( second_export );
4110 psa_destroy_key( slot );
4111 mbedtls_psa_crypto_free();
4112}
4113/* END_CASE */