blob: 11e4dbb0bc2f40375b9c42ca11e9fa222417aa16 [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
itayzafrir3e02b3b2018-06-12 17:06:52 +030016#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020017#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#else
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100131static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200132 psa_key_usage_t usage,
133 psa_algorithm_t alg )
134{
135 psa_mac_operation_t operation;
136 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200137 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200138 size_t mac_length = sizeof( mac );
139
140 if( usage & PSA_KEY_USAGE_SIGN )
141 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200142 TEST_ASSERT( psa_mac_sign_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100143 handle, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200144 TEST_ASSERT( psa_mac_update( &operation,
145 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200146 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200147 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200148 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200149 }
150
151 if( usage & PSA_KEY_USAGE_VERIFY )
152 {
153 psa_status_t verify_status =
154 ( usage & PSA_KEY_USAGE_SIGN ?
155 PSA_SUCCESS :
156 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200157 TEST_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100158 handle, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200159 TEST_ASSERT( psa_mac_update( &operation,
160 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200161 TEST_ASSERT( psa_mac_verify_finish( &operation,
162 mac,
163 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200164 }
165
166 return( 1 );
167
168exit:
169 psa_mac_abort( &operation );
170 return( 0 );
171}
172
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100173static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200174 psa_key_usage_t usage,
175 psa_algorithm_t alg )
176{
177 psa_cipher_operation_t operation;
178 unsigned char iv[16] = {0};
179 size_t iv_length = sizeof( iv );
180 const unsigned char plaintext[16] = "Hello, world...";
181 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
182 size_t ciphertext_length = sizeof( ciphertext );
183 unsigned char decrypted[sizeof( ciphertext )];
184 size_t part_length;
185
186 if( usage & PSA_KEY_USAGE_ENCRYPT )
187 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200188 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100189 handle, alg ) == PSA_SUCCESS );
Gilles Peskinefe119512018-07-08 21:39:34 +0200190 TEST_ASSERT( psa_cipher_generate_iv( &operation,
191 iv, sizeof( iv ),
192 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200193 TEST_ASSERT( psa_cipher_update( &operation,
194 plaintext, sizeof( plaintext ),
195 ciphertext, sizeof( ciphertext ),
196 &ciphertext_length ) == PSA_SUCCESS );
197 TEST_ASSERT( psa_cipher_finish( &operation,
198 ciphertext + ciphertext_length,
199 sizeof( ciphertext ) - ciphertext_length,
200 &part_length ) == PSA_SUCCESS );
201 ciphertext_length += part_length;
202 }
203
204 if( usage & PSA_KEY_USAGE_DECRYPT )
205 {
206 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700207 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200208 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
209 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200210 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100211 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200212 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
213 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200214 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100215 handle, alg ) == PSA_SUCCESS );
Gilles Peskinefe119512018-07-08 21:39:34 +0200216 TEST_ASSERT( psa_cipher_set_iv( &operation,
217 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200218 TEST_ASSERT( psa_cipher_update( &operation,
219 ciphertext, ciphertext_length,
220 decrypted, sizeof( decrypted ),
221 &part_length ) == PSA_SUCCESS );
222 status = psa_cipher_finish( &operation,
223 decrypted + part_length,
224 sizeof( decrypted ) - part_length,
225 &part_length );
226 /* For a stream cipher, all inputs are valid. For a block cipher,
227 * if the input is some aribtrary data rather than an actual
228 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700229 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700230 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200231 TEST_ASSERT( status == PSA_SUCCESS );
232 else
233 TEST_ASSERT( status == PSA_SUCCESS ||
234 status == PSA_ERROR_INVALID_PADDING );
235 }
236
237 return( 1 );
238
239exit:
240 psa_cipher_abort( &operation );
241 return( 0 );
242}
243
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100244static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200245 psa_key_usage_t usage,
246 psa_algorithm_t alg )
247{
248 unsigned char nonce[16] = {0};
249 size_t nonce_length = sizeof( nonce );
250 unsigned char plaintext[16] = "Hello, world...";
251 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
252 size_t ciphertext_length = sizeof( ciphertext );
253 size_t plaintext_length = sizeof( ciphertext );
254
255 if( usage & PSA_KEY_USAGE_ENCRYPT )
256 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100257 TEST_ASSERT( psa_aead_encrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200258 nonce, nonce_length,
259 NULL, 0,
260 plaintext, sizeof( plaintext ),
261 ciphertext, sizeof( ciphertext ),
262 &ciphertext_length ) == PSA_SUCCESS );
263 }
264
265 if( usage & PSA_KEY_USAGE_DECRYPT )
266 {
267 psa_status_t verify_status =
268 ( usage & PSA_KEY_USAGE_ENCRYPT ?
269 PSA_SUCCESS :
270 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100271 TEST_ASSERT( psa_aead_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200272 nonce, nonce_length,
273 NULL, 0,
274 ciphertext, ciphertext_length,
275 plaintext, sizeof( plaintext ),
276 &plaintext_length ) == verify_status );
277 }
278
279 return( 1 );
280
281exit:
282 return( 0 );
283}
284
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100285static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200286 psa_key_usage_t usage,
287 psa_algorithm_t alg )
288{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200289 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
290 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200291 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 size_t signature_length = sizeof( signature );
293
294 if( usage & PSA_KEY_USAGE_SIGN )
295 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200296 /* Some algorithms require the payload to have the size of
297 * the hash encoded in the algorithm. Use this input size
298 * even for algorithms that allow other input sizes. */
299 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
300 if( hash_alg != 0 )
301 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100302 TEST_ASSERT( psa_asymmetric_sign( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200303 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200304 signature, sizeof( signature ),
305 &signature_length ) == PSA_SUCCESS );
306 }
307
308 if( usage & PSA_KEY_USAGE_VERIFY )
309 {
310 psa_status_t verify_status =
311 ( usage & PSA_KEY_USAGE_SIGN ?
312 PSA_SUCCESS :
313 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100314 TEST_ASSERT( psa_asymmetric_verify( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200315 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200316 signature, signature_length ) ==
317 verify_status );
318 }
319
320 return( 1 );
321
322exit:
323 return( 0 );
324}
325
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100326static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200327 psa_key_usage_t usage,
328 psa_algorithm_t alg )
329{
330 unsigned char plaintext[256] = "Hello, world...";
331 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
332 size_t ciphertext_length = sizeof( ciphertext );
333 size_t plaintext_length = 16;
334
335 if( usage & PSA_KEY_USAGE_ENCRYPT )
336 {
337 TEST_ASSERT(
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100338 psa_asymmetric_encrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200339 plaintext, plaintext_length,
340 NULL, 0,
341 ciphertext, sizeof( ciphertext ),
342 &ciphertext_length ) == PSA_SUCCESS );
343 }
344
345 if( usage & PSA_KEY_USAGE_DECRYPT )
346 {
347 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100348 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200349 ciphertext, ciphertext_length,
350 NULL, 0,
351 plaintext, sizeof( plaintext ),
352 &plaintext_length );
353 TEST_ASSERT( status == PSA_SUCCESS ||
354 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
355 ( status == PSA_ERROR_INVALID_ARGUMENT ||
356 status == PSA_ERROR_INVALID_PADDING ) ) );
357 }
358
359 return( 1 );
360
361exit:
362 return( 0 );
363}
Gilles Peskine02b75072018-07-01 22:31:34 +0200364
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100365static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200366 psa_key_usage_t usage,
367 psa_algorithm_t alg )
368{
369 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
370 unsigned char label[16] = "This is a label.";
371 size_t label_length = sizeof( label );
372 unsigned char seed[16] = "abcdefghijklmnop";
373 size_t seed_length = sizeof( seed );
374 unsigned char output[1];
375
376 if( usage & PSA_KEY_USAGE_DERIVE )
377 {
378 TEST_ASSERT( psa_key_derivation( &generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100379 handle, alg,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200380 label, label_length,
381 seed, seed_length,
382 sizeof( output ) ) == PSA_SUCCESS );
383 TEST_ASSERT( psa_generator_read( &generator,
384 output,
385 sizeof( output ) ) == PSA_SUCCESS );
386 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
387 }
388
389 return( 1 );
390
391exit:
392 return( 0 );
393}
394
Gilles Peskinec7998b72018-11-07 18:45:02 +0100395/* We need two keys to exercise key agreement. Exercise the
396 * private key against its own public key. */
397static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100398 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100399 psa_algorithm_t alg )
400{
401 psa_key_type_t private_key_type;
402 psa_key_type_t public_key_type;
403 size_t key_bits;
404 uint8_t *public_key = NULL;
405 size_t public_key_length;
406 /* Return UNKNOWN_ERROR if something other than the final call to
407 * psa_key_agreement fails. This isn't fully satisfactory, but it's
408 * good enough: callers will report it as a failed test anyway. */
409 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
410
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100411 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100412 &private_key_type,
413 &key_bits ) == PSA_SUCCESS );
414 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
415 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
416 ASSERT_ALLOC( public_key, public_key_length );
417 TEST_ASSERT( public_key != NULL );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100418 TEST_ASSERT( psa_export_public_key( handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100419 public_key, public_key_length,
420 &public_key_length ) == PSA_SUCCESS );
421
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100422 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100423 public_key, public_key_length,
424 alg );
425exit:
426 mbedtls_free( public_key );
427 return( status );
428}
429
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100430static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200431 psa_key_usage_t usage,
432 psa_algorithm_t alg )
433{
434 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200435 unsigned char output[1];
436 int ok = 0;
437
438 if( usage & PSA_KEY_USAGE_DERIVE )
439 {
440 /* We need two keys to exercise key agreement. Exercise the
441 * private key against its own public key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100442 TEST_ASSERT( key_agreement_with_self( &generator, handle, alg ) ==
Gilles Peskinec7998b72018-11-07 18:45:02 +0100443 PSA_SUCCESS );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200444 TEST_ASSERT( psa_generator_read( &generator,
445 output,
446 sizeof( output ) ) == PSA_SUCCESS );
447 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
448 }
449 ok = 1;
450
451exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200452 return( ok );
453}
454
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200455static int is_oid_of_key_type( psa_key_type_t type,
456 const uint8_t *oid, size_t oid_length )
457{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200458 const uint8_t *expected_oid = NULL;
459 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200460#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200461 if( PSA_KEY_TYPE_IS_RSA( type ) )
462 {
463 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
464 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
465 }
466 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200467#endif /* MBEDTLS_RSA_C */
468#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200469 if( PSA_KEY_TYPE_IS_ECC( type ) )
470 {
471 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
472 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
473 }
474 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200475#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200476 {
477 char message[40];
478 mbedtls_snprintf( message, sizeof( message ),
479 "OID not known for key type=0x%08lx",
480 (unsigned long) type );
481 test_fail( message, __LINE__, __FILE__ );
482 return( 0 );
483 }
484
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200485 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200486 return( 1 );
487
488exit:
489 return( 0 );
490}
491
492static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
493 size_t min_bits, size_t max_bits,
494 int must_be_odd )
495{
496 size_t len;
497 size_t actual_bits;
498 unsigned char msb;
499 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
500 MBEDTLS_ASN1_INTEGER ) == 0 );
501 /* Tolerate a slight departure from DER encoding:
502 * - 0 may be represented by an empty string or a 1-byte string.
503 * - The sign bit may be used as a value bit. */
504 if( ( len == 1 && ( *p )[0] == 0 ) ||
505 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
506 {
507 ++( *p );
508 --len;
509 }
510 if( min_bits == 0 && len == 0 )
511 return( 1 );
512 msb = ( *p )[0];
513 TEST_ASSERT( msb != 0 );
514 actual_bits = 8 * ( len - 1 );
515 while( msb != 0 )
516 {
517 msb >>= 1;
518 ++actual_bits;
519 }
520 TEST_ASSERT( actual_bits >= min_bits );
521 TEST_ASSERT( actual_bits <= max_bits );
522 if( must_be_odd )
523 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
524 *p += len;
525 return( 1 );
526exit:
527 return( 0 );
528}
529
530static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
531 size_t *len,
532 unsigned char n, unsigned char tag )
533{
534 int ret;
535 ret = mbedtls_asn1_get_tag( p, end, len,
536 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
537 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
538 if( ret != 0 )
539 return( ret );
540 end = *p + *len;
541 ret = mbedtls_asn1_get_tag( p, end, len, tag );
542 if( ret != 0 )
543 return( ret );
544 if( *p + *len != end )
545 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
546 return( 0 );
547}
548
549static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
550 uint8_t *exported, size_t exported_length )
551{
552 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200553 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200554 else
555 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200556
557#if defined(MBEDTLS_DES_C)
558 if( type == PSA_KEY_TYPE_DES )
559 {
560 /* Check the parity bits. */
561 unsigned i;
562 for( i = 0; i < bits / 8; i++ )
563 {
564 unsigned bit_count = 0;
565 unsigned m;
566 for( m = 1; m <= 0x100; m <<= 1 )
567 {
568 if( exported[i] & m )
569 ++bit_count;
570 }
571 TEST_ASSERT( bit_count % 2 != 0 );
572 }
573 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200574 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200575#endif
576
577#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
578 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
579 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200580 uint8_t *p = exported;
581 uint8_t *end = exported + exported_length;
582 size_t len;
583 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200584 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200585 * modulus INTEGER, -- n
586 * publicExponent INTEGER, -- e
587 * privateExponent INTEGER, -- d
588 * prime1 INTEGER, -- p
589 * prime2 INTEGER, -- q
590 * exponent1 INTEGER, -- d mod (p-1)
591 * exponent2 INTEGER, -- d mod (q-1)
592 * coefficient INTEGER, -- (inverse of q) mod p
593 * }
594 */
595 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
596 MBEDTLS_ASN1_SEQUENCE |
597 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
598 TEST_ASSERT( p + len == end );
599 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
600 goto exit;
601 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
604 goto exit;
605 /* Require d to be at least half the size of n. */
606 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
607 goto exit;
608 /* Require p and q to be at most half the size of n, rounded up. */
609 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
610 goto exit;
611 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
612 goto exit;
613 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
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 TEST_ASSERT( p == end );
620 }
621 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200622#endif /* MBEDTLS_RSA_C */
623
624#if defined(MBEDTLS_ECP_C)
625 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
626 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100627 /* Just the secret value */
628 TEST_ASSERT( exported_length == PSA_BITS_TO_BYTES( bits ) );
629 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200630 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200631#endif /* MBEDTLS_ECP_C */
632
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200633 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
634 {
635 uint8_t *p = exported;
636 uint8_t *end = exported + exported_length;
637 size_t len;
638 mbedtls_asn1_buf alg;
639 mbedtls_asn1_buf params;
640 mbedtls_asn1_bitstring bitstring;
641 /* SubjectPublicKeyInfo ::= SEQUENCE {
642 * algorithm AlgorithmIdentifier,
643 * subjectPublicKey BIT STRING }
644 * AlgorithmIdentifier ::= SEQUENCE {
645 * algorithm OBJECT IDENTIFIER,
646 * parameters ANY DEFINED BY algorithm OPTIONAL }
647 */
648 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
649 MBEDTLS_ASN1_SEQUENCE |
650 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
651 TEST_ASSERT( p + len == end );
652 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
653 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
654 goto exit;
655 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
656 TEST_ASSERT( p == end );
657 p = bitstring.p;
658#if defined(MBEDTLS_RSA_C)
659 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
660 {
661 /* RSAPublicKey ::= SEQUENCE {
662 * modulus INTEGER, -- n
663 * publicExponent INTEGER } -- e
664 */
665 TEST_ASSERT( bitstring.unused_bits == 0 );
666 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
667 MBEDTLS_ASN1_SEQUENCE |
668 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
669 TEST_ASSERT( p + len == end );
670 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
671 goto exit;
672 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
673 goto exit;
674 TEST_ASSERT( p == end );
675 }
676 else
677#endif /* MBEDTLS_RSA_C */
678#if defined(MBEDTLS_ECP_C)
679 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
680 {
681 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200682 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200683 * -- then x_P as an n-bit string, big endian;
684 * -- then y_P as a n-bit string, big endian,
685 * -- where n is the order of the curve.
686 */
687 TEST_ASSERT( bitstring.unused_bits == 0 );
688 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
689 TEST_ASSERT( p[0] == 4 );
690 }
691 else
692#endif /* MBEDTLS_ECP_C */
693 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100694 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200695 mbedtls_snprintf( message, sizeof( message ),
696 "No sanity check for public key type=0x%08lx",
697 (unsigned long) type );
698 test_fail( message, __LINE__, __FILE__ );
699 return( 0 );
700 }
701 }
702 else
703
704 {
705 /* No sanity checks for other types */
706 }
707
708 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200709
710exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200711 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200712}
713
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100714static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200715 psa_key_usage_t usage )
716{
717 psa_key_type_t type;
718 size_t bits;
719 uint8_t *exported = NULL;
720 size_t exported_size = 0;
721 size_t exported_length = 0;
722 int ok = 0;
723
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100724 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) == PSA_SUCCESS );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200725
726 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
727 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200728 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100729 TEST_ASSERT( psa_export_key( handle, NULL, 0, &exported_length ) ==
Gilles Peskined14664a2018-08-10 19:07:32 +0200730 PSA_ERROR_NOT_PERMITTED );
731 return( 1 );
732 }
733
Gilles Peskined14664a2018-08-10 19:07:32 +0200734 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200735 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200736
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100737 TEST_ASSERT( psa_export_key( handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200738 exported, exported_size,
739 &exported_length ) == PSA_SUCCESS );
740 ok = exported_key_sanity_check( type, bits, exported, exported_length );
741
742exit:
743 mbedtls_free( exported );
744 return( ok );
745}
746
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100747static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200748{
749 psa_key_type_t type;
750 psa_key_type_t public_type;
751 size_t bits;
752 uint8_t *exported = NULL;
753 size_t exported_size = 0;
754 size_t exported_length = 0;
755 int ok = 0;
756
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100757 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) == PSA_SUCCESS );
Gilles Peskined14664a2018-08-10 19:07:32 +0200758 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
759 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100760 TEST_ASSERT( psa_export_public_key( handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200761 NULL, 0, &exported_length ) ==
762 PSA_ERROR_INVALID_ARGUMENT );
763 return( 1 );
764 }
765
766 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
767 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200768 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200769
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100770 TEST_ASSERT( psa_export_public_key( handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200771 exported, exported_size,
772 &exported_length ) == PSA_SUCCESS );
773 ok = exported_key_sanity_check( public_type, bits,
774 exported, exported_length );
775
776exit:
777 mbedtls_free( exported );
778 return( ok );
779}
780
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100781static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200782 psa_key_usage_t usage,
783 psa_algorithm_t alg )
784{
785 int ok;
786 if( alg == 0 )
787 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
788 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100789 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200790 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100791 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200792 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100793 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200794 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100795 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200796 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100797 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200798 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100799 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200800 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100801 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200802 else
803 {
804 char message[40];
805 mbedtls_snprintf( message, sizeof( message ),
806 "No code to exercise alg=0x%08lx",
807 (unsigned long) alg );
808 test_fail( message, __LINE__, __FILE__ );
809 ok = 0;
810 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200811
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100812 ok = ok && exercise_export_key( handle, usage );
813 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200814
Gilles Peskine02b75072018-07-01 22:31:34 +0200815 return( ok );
816}
817
Gilles Peskine10df3412018-10-25 22:35:43 +0200818static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
819 psa_algorithm_t alg )
820{
821 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
822 {
823 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
824 PSA_KEY_USAGE_VERIFY :
825 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
826 }
827 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
828 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
829 {
830 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
831 PSA_KEY_USAGE_ENCRYPT :
832 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
833 }
834 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
835 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
836 {
837 return( PSA_KEY_USAGE_DERIVE );
838 }
839 else
840 {
841 return( 0 );
842 }
843
844}
Darryl Green0c6575a2018-11-07 16:05:30 +0000845
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100846/* An overapproximation of the amount of storage needed for a key of the
847 * given type and with the given content. The API doesn't make it easy
848 * to find a good value for the size. The current implementation doesn't
849 * care about the value anyway. */
850#define KEY_BITS_FROM_DATA( type, data ) \
851 ( data )->len
852
Darryl Green0c6575a2018-11-07 16:05:30 +0000853typedef enum {
854 IMPORT_KEY = 0,
855 GENERATE_KEY = 1,
856 DERIVE_KEY = 2
857} generate_method;
858
Gilles Peskinee59236f2018-01-27 23:32:46 +0100859/* END_HEADER */
860
861/* BEGIN_DEPENDENCIES
862 * depends_on:MBEDTLS_PSA_CRYPTO_C
863 * END_DEPENDENCIES
864 */
865
866/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200867void static_checks( )
868{
869 size_t max_truncated_mac_size =
870 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
871
872 /* Check that the length for a truncated MAC always fits in the algorithm
873 * encoding. The shifted mask is the maximum truncated value. The
874 * untruncated algorithm may be one byte larger. */
875 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
876}
877/* END_CASE */
878
879/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200880void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100882 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200883 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300887 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
889
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100890 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
891 &handle ) == PSA_SUCCESS );
892 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200893 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100894 if( status == PSA_SUCCESS )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100895 TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896
897exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100898 mbedtls_psa_crypto_free( );
899}
900/* END_CASE */
901
902/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100903void import_twice( int alg_arg, int usage_arg,
904 int type1_arg, data_t *data1,
905 int expected_import1_status_arg,
906 int type2_arg, data_t *data2,
907 int expected_import2_status_arg )
908{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100909 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100910 psa_algorithm_t alg = alg_arg;
911 psa_key_usage_t usage = usage_arg;
912 psa_key_type_t type1 = type1_arg;
913 psa_status_t expected_import1_status = expected_import1_status_arg;
914 psa_key_type_t type2 = type2_arg;
915 psa_status_t expected_import2_status = expected_import2_status_arg;
916 psa_key_policy_t policy;
917 psa_status_t status;
918
919 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
920
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100921 TEST_ASSERT( psa_allocate_key( type1,
922 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
923 KEY_BITS_FROM_DATA( type2, data2 ) ),
924 &handle ) == PSA_SUCCESS );
Gilles Peskinea4261682018-12-03 11:34:01 +0100925 psa_key_policy_init( &policy );
926 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100927 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea4261682018-12-03 11:34:01 +0100928
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100929 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinea4261682018-12-03 11:34:01 +0100930 TEST_ASSERT( status == expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100931 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinea4261682018-12-03 11:34:01 +0100932 TEST_ASSERT( status == expected_import2_status );
933
934 if( expected_import1_status == PSA_SUCCESS ||
935 expected_import2_status == PSA_SUCCESS )
936 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100937 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100938 }
939
940exit:
941 mbedtls_psa_crypto_free( );
942}
943/* END_CASE */
944
945/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200946void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
947{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100948 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200949 size_t bits = bits_arg;
950 psa_status_t expected_status = expected_status_arg;
951 psa_status_t status;
952 psa_key_type_t type =
953 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
954 size_t buffer_size = /* Slight overapproximations */
955 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200956 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200957 unsigned char *p;
958 int ret;
959 size_t length;
960
961 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200962 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200963
964 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
965 bits, keypair ) ) >= 0 );
966 length = ret;
967
968 /* Try importing the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100969 TEST_ASSERT( psa_allocate_key( type, bits, &handle ) == PSA_SUCCESS );
970 status = psa_import_key( handle, type, p, length );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200971 TEST_ASSERT( status == expected_status );
972 if( status == PSA_SUCCESS )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100973 TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200974
975exit:
976 mbedtls_free( buffer );
977 mbedtls_psa_crypto_free( );
978}
979/* END_CASE */
980
981/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300982void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300983 int type_arg,
984 int alg_arg,
985 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 int expected_bits,
987 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200988 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 int canonical_input )
990{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100991 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200993 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200994 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 unsigned char *exported = NULL;
997 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100999 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000 size_t reexported_length;
1001 psa_key_type_t got_type;
1002 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001003 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001006 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001007 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001008 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001010 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1012
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001013 TEST_ASSERT( psa_allocate_key( type, expected_bits, &handle ) ==
1014 PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001015 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001016 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001017 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
1018
1019 TEST_ASSERT( psa_get_key_information(
1020 handle, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001021
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001023 TEST_ASSERT( psa_import_key( handle, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001024 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001025
1026 /* Test the key information */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001027 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001028 &got_type,
1029 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001030 TEST_ASSERT( got_type == type );
1031 TEST_ASSERT( got_bits == (size_t) expected_bits );
1032
1033 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001034 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001035 exported, export_size,
1036 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001037 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001038
1039 /* The exported length must be set by psa_export_key() to a value between 0
1040 * and export_size. On errors, the exported length must be 0. */
1041 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1042 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1043 TEST_ASSERT( exported_length <= export_size );
1044
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001045 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001046 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001047 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001048 {
1049 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001050 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001051 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001052
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001053 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001054 goto exit;
1055
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001056 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001057 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058 else
1059 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001060 psa_key_handle_t handle2;
1061 TEST_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) ==
1062 PSA_SUCCESS );
1063 TEST_ASSERT( psa_set_key_policy( handle2, &policy ) == PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001064
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001065 TEST_ASSERT( psa_import_key( handle2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001066 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001067 exported_length ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001068 TEST_ASSERT( psa_export_key( handle2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001069 reexported,
1070 export_size,
1071 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001072 ASSERT_COMPARE( exported, exported_length,
1073 reexported, reexported_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001074 TEST_ASSERT( psa_close_key( handle2 ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001075 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001076 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001077
1078destroy:
1079 /* Destroy the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001080 TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001081 TEST_ASSERT( psa_get_key_information(
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001082 handle, NULL, NULL ) == PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001083
1084exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001085 mbedtls_free( exported );
1086 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087 mbedtls_psa_crypto_free( );
1088}
1089/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001090
Moran Pekerf709f4a2018-06-06 17:26:04 +03001091/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001092void import_key_nonempty_slot( )
1093{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001094 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001095 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1096 psa_status_t status;
1097 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
1098 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1099
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001100 TEST_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1101 &handle ) == PSA_SUCCESS );
1102
Moran Peker28a38e62018-11-07 16:18:24 +02001103 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001104 TEST_ASSERT( psa_import_key( handle, type,
Moran Peker28a38e62018-11-07 16:18:24 +02001105 data, sizeof( data ) ) == PSA_SUCCESS );
1106
1107 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001108 status = psa_import_key( handle, type, data, sizeof( data ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001109 TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT );
1110
1111exit:
1112 mbedtls_psa_crypto_free( );
1113}
1114/* END_CASE */
1115
1116/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001117void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001118{
1119 psa_status_t status;
1120 unsigned char *exported = NULL;
1121 size_t export_size = 0;
1122 size_t exported_length = INVALID_EXPORT_LENGTH;
1123 psa_status_t expected_export_status = expected_export_status_arg;
1124
1125 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1126
1127 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001128 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001129 exported, export_size,
1130 &exported_length );
1131 TEST_ASSERT( status == expected_export_status );
1132
1133exit:
1134 mbedtls_psa_crypto_free( );
1135}
1136/* END_CASE */
1137
1138/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001139void export_with_no_key_activity( )
1140{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001141 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001142 psa_algorithm_t alg = PSA_ALG_CTR;
1143 psa_status_t status;
1144 psa_key_policy_t policy;
1145 unsigned char *exported = NULL;
1146 size_t export_size = 0;
1147 size_t exported_length = INVALID_EXPORT_LENGTH;
1148
1149 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1150
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001151 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1152 &handle ) == PSA_SUCCESS );
Moran Peker34550092018-11-07 16:19:34 +02001153 psa_key_policy_init( &policy );
1154 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001155 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Peker34550092018-11-07 16:19:34 +02001156
1157 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001158 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001159 exported, export_size,
1160 &exported_length );
1161 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1162
1163exit:
1164 mbedtls_psa_crypto_free( );
1165}
1166/* END_CASE */
1167
1168/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001169void cipher_with_no_key_activity( )
1170{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001171 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001172 psa_status_t status;
1173 psa_key_policy_t policy;
1174 psa_cipher_operation_t operation;
1175 int exercise_alg = PSA_ALG_CTR;
1176
1177 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1178
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001179 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1180 &handle ) == PSA_SUCCESS );
Moran Pekerce500072018-11-07 16:20:07 +02001181 psa_key_policy_init( &policy );
1182 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001183 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekerce500072018-11-07 16:20:07 +02001184
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001185 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Moran Pekerce500072018-11-07 16:20:07 +02001186 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1187
1188exit:
1189 psa_cipher_abort( &operation );
1190 mbedtls_psa_crypto_free( );
1191}
1192/* END_CASE */
1193
1194/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001195void export_after_import_failure( data_t *data, int type_arg,
1196 int expected_import_status_arg )
1197{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001198 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001199 psa_key_type_t type = type_arg;
1200 psa_status_t status;
1201 unsigned char *exported = NULL;
1202 size_t export_size = 0;
1203 psa_status_t expected_import_status = expected_import_status_arg;
1204 size_t exported_length = INVALID_EXPORT_LENGTH;
1205
1206 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1207
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001208 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1209 &handle ) == PSA_SUCCESS );
1210
Moran Peker34550092018-11-07 16:19:34 +02001211 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001212 status = psa_import_key( handle, type,
Moran Peker34550092018-11-07 16:19:34 +02001213 data->x, data->len );
1214 TEST_ASSERT( status == expected_import_status );
1215
1216 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001217 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001218 exported, export_size,
1219 &exported_length );
1220 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1221
1222exit:
1223 mbedtls_psa_crypto_free( );
1224}
1225/* END_CASE */
1226
1227/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001228void cipher_after_import_failure( data_t *data, int type_arg,
1229 int expected_import_status_arg )
1230{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001231 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001232 psa_cipher_operation_t operation;
1233 psa_key_type_t type = type_arg;
1234 psa_status_t status;
1235 psa_status_t expected_import_status = expected_import_status_arg;
1236 int exercise_alg = PSA_ALG_CTR;
1237
1238 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1239
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001240 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1241 &handle ) == PSA_SUCCESS );
1242
Moran Pekerce500072018-11-07 16:20:07 +02001243 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001244 status = psa_import_key( handle, type,
Moran Pekerce500072018-11-07 16:20:07 +02001245 data->x, data->len );
1246 TEST_ASSERT( status == expected_import_status );
1247
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001248 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Moran Pekerce500072018-11-07 16:20:07 +02001249 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1250
1251exit:
1252 psa_cipher_abort( &operation );
1253 mbedtls_psa_crypto_free( );
1254}
1255/* END_CASE */
1256
1257/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001258void export_after_destroy_key( data_t *data, int type_arg )
1259{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001260 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001261 psa_key_type_t type = type_arg;
1262 psa_status_t status;
1263 psa_key_policy_t policy;
1264 psa_algorithm_t alg = PSA_ALG_CTR;
1265 unsigned char *exported = NULL;
1266 size_t export_size = 0;
1267 size_t exported_length = INVALID_EXPORT_LENGTH;
1268
1269 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1270
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001271 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1272 &handle ) == PSA_SUCCESS );
Moran Peker34550092018-11-07 16:19:34 +02001273 psa_key_policy_init( &policy );
1274 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001275 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Peker34550092018-11-07 16:19:34 +02001276 export_size = (ptrdiff_t) data->len;
1277 ASSERT_ALLOC( exported, export_size );
1278
1279 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001280 TEST_ASSERT( psa_import_key( handle, type,
Moran Peker34550092018-11-07 16:19:34 +02001281 data->x, data->len ) == PSA_SUCCESS );
1282
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001283 TEST_ASSERT( psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001284 &exported_length ) == PSA_SUCCESS );
1285
1286 /* Destroy the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001287 TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS );
Moran Peker34550092018-11-07 16:19:34 +02001288
1289 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001290 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001291 &exported_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001292 TEST_ASSERT( status == PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001293
1294exit:
1295 mbedtls_free( exported );
1296 mbedtls_psa_crypto_free( );
1297}
1298/* END_CASE */
1299
1300/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001301void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001302 int type_arg,
1303 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001304 int export_size_delta,
1305 int expected_export_status_arg,
1306 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001307{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001308 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001309 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001310 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001311 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001312 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001313 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001314 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001315 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001316 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001317
Moran Pekerf709f4a2018-06-06 17:26:04 +03001318 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1319
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001320 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1321 &handle ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001322 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001323 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001324 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001325
1326 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001327 TEST_ASSERT( psa_import_key( handle, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001328 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001329
Gilles Peskine49c25912018-10-29 15:15:31 +01001330 /* Export the public key */
1331 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001332 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001333 exported, export_size,
1334 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001335 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001336 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001337 {
1338 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1339 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001340 TEST_ASSERT( psa_get_key_information( handle, NULL, &bits ) ==
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001341 PSA_SUCCESS );
1342 TEST_ASSERT( expected_public_key->len <=
1343 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001344 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1345 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001346 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001347
1348exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001349 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001350 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001351 mbedtls_psa_crypto_free( );
1352}
1353/* END_CASE */
1354
Gilles Peskine20035e32018-02-03 22:44:14 +01001355/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001356void import_and_exercise_key( data_t *data,
1357 int type_arg,
1358 int bits_arg,
1359 int alg_arg )
1360{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001361 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001362 psa_key_type_t type = type_arg;
1363 size_t bits = bits_arg;
1364 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001365 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001366 psa_key_policy_t policy;
1367 psa_key_type_t got_type;
1368 size_t got_bits;
1369 psa_status_t status;
1370
1371 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1372
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001373 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1374 &handle ) == PSA_SUCCESS );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001375 psa_key_policy_init( &policy );
1376 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001377 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001378
1379 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001380 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001381 TEST_ASSERT( status == PSA_SUCCESS );
1382
1383 /* Test the key information */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001384 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001385 &got_type,
1386 &got_bits ) == PSA_SUCCESS );
1387 TEST_ASSERT( got_type == type );
1388 TEST_ASSERT( got_bits == bits );
1389
1390 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001391 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001392 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001393
1394exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001395 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001396 mbedtls_psa_crypto_free( );
1397}
1398/* END_CASE */
1399
1400/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001401void key_policy( int usage_arg, int alg_arg )
1402{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001403 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001404 psa_algorithm_t alg = alg_arg;
1405 psa_key_usage_t usage = usage_arg;
1406 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1407 unsigned char key[32] = {0};
1408 psa_key_policy_t policy_set;
1409 psa_key_policy_t policy_get;
1410
1411 memset( key, 0x2a, sizeof( key ) );
1412
1413 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1414
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001415 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1416 &handle ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001417 psa_key_policy_init( &policy_set );
1418 psa_key_policy_init( &policy_get );
Gilles Peskined5b33222018-06-18 22:20:03 +02001419 psa_key_policy_set_usage( &policy_set, usage, alg );
1420
1421 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1422 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001423 TEST_ASSERT( psa_set_key_policy( handle, &policy_set ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001424
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001425 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskined5b33222018-06-18 22:20:03 +02001426 key, sizeof( key ) ) == PSA_SUCCESS );
1427
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001428 TEST_ASSERT( psa_get_key_policy( handle, &policy_get ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001429
1430 TEST_ASSERT( policy_get.usage == policy_set.usage );
1431 TEST_ASSERT( policy_get.alg == policy_set.alg );
1432
1433exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001434 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001435 mbedtls_psa_crypto_free( );
1436}
1437/* END_CASE */
1438
1439/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001440void mac_key_policy( int policy_usage,
1441 int policy_alg,
1442 int key_type,
1443 data_t *key_data,
1444 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001446 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001447 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001448 psa_mac_operation_t operation;
1449 psa_status_t status;
1450 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001451
1452 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1453
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001454 TEST_ASSERT( psa_allocate_key( key_type,
1455 KEY_BITS_FROM_DATA( key_type, key_data ),
1456 &handle ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001457 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001458 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001459 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001460
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001461 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001462 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001463
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001464 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001465 if( policy_alg == exercise_alg &&
1466 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1467 TEST_ASSERT( status == PSA_SUCCESS );
1468 else
1469 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1470 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001471
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001472 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001473 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001474 if( policy_alg == exercise_alg &&
1475 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001476 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001477 else
1478 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1479
1480exit:
1481 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001482 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001483 mbedtls_psa_crypto_free( );
1484}
1485/* END_CASE */
1486
1487/* BEGIN_CASE */
1488void cipher_key_policy( int policy_usage,
1489 int policy_alg,
1490 int key_type,
1491 data_t *key_data,
1492 int exercise_alg )
1493{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001494 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001495 psa_key_policy_t policy;
1496 psa_cipher_operation_t operation;
1497 psa_status_t status;
1498
1499 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1500
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001501 TEST_ASSERT( psa_allocate_key( key_type,
1502 KEY_BITS_FROM_DATA( key_type, key_data ),
1503 &handle ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001504 psa_key_policy_init( &policy );
1505 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001506 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001507
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001508 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001509 key_data->x, key_data->len ) == PSA_SUCCESS );
1510
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001511 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001512 if( policy_alg == exercise_alg &&
1513 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1514 TEST_ASSERT( status == PSA_SUCCESS );
1515 else
1516 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1517 psa_cipher_abort( &operation );
1518
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001519 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001520 if( policy_alg == exercise_alg &&
1521 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1522 TEST_ASSERT( status == PSA_SUCCESS );
1523 else
1524 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1525
1526exit:
1527 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001528 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001529 mbedtls_psa_crypto_free( );
1530}
1531/* END_CASE */
1532
1533/* BEGIN_CASE */
1534void aead_key_policy( int policy_usage,
1535 int policy_alg,
1536 int key_type,
1537 data_t *key_data,
1538 int nonce_length_arg,
1539 int tag_length_arg,
1540 int exercise_alg )
1541{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001542 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001543 psa_key_policy_t policy;
1544 psa_status_t status;
1545 unsigned char nonce[16] = {0};
1546 size_t nonce_length = nonce_length_arg;
1547 unsigned char tag[16];
1548 size_t tag_length = tag_length_arg;
1549 size_t output_length;
1550
1551 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1552 TEST_ASSERT( tag_length <= sizeof( tag ) );
1553
1554 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1555
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001556 TEST_ASSERT( psa_allocate_key( key_type,
1557 KEY_BITS_FROM_DATA( key_type, key_data ),
1558 &handle ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001559 psa_key_policy_init( &policy );
1560 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001561 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001562
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564 key_data->x, key_data->len ) == PSA_SUCCESS );
1565
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001566 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001567 nonce, nonce_length,
1568 NULL, 0,
1569 NULL, 0,
1570 tag, tag_length,
1571 &output_length );
1572 if( policy_alg == exercise_alg &&
1573 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1574 TEST_ASSERT( status == PSA_SUCCESS );
1575 else
1576 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1577
1578 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001579 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001580 nonce, nonce_length,
1581 NULL, 0,
1582 tag, tag_length,
1583 NULL, 0,
1584 &output_length );
1585 if( policy_alg == exercise_alg &&
1586 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1587 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1588 else
1589 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1590
1591exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001592 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001593 mbedtls_psa_crypto_free( );
1594}
1595/* END_CASE */
1596
1597/* BEGIN_CASE */
1598void asymmetric_encryption_key_policy( int policy_usage,
1599 int policy_alg,
1600 int key_type,
1601 data_t *key_data,
1602 int exercise_alg )
1603{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001604 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001605 psa_key_policy_t policy;
1606 psa_status_t status;
1607 size_t key_bits;
1608 size_t buffer_length;
1609 unsigned char *buffer = NULL;
1610 size_t output_length;
1611
1612 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1613
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001614 TEST_ASSERT( psa_allocate_key( key_type,
1615 KEY_BITS_FROM_DATA( key_type, key_data ),
1616 &handle ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001617 psa_key_policy_init( &policy );
1618 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001619 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001620
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001621 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001622 key_data->x, key_data->len ) == PSA_SUCCESS );
1623
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001624 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001625 NULL,
1626 &key_bits ) == PSA_SUCCESS );
1627 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1628 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001629 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001630
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001631 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001632 NULL, 0,
1633 NULL, 0,
1634 buffer, buffer_length,
1635 &output_length );
1636 if( policy_alg == exercise_alg &&
1637 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1638 TEST_ASSERT( status == PSA_SUCCESS );
1639 else
1640 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1641
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001642 if( buffer_length != 0 )
1643 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001644 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001645 buffer, buffer_length,
1646 NULL, 0,
1647 buffer, buffer_length,
1648 &output_length );
1649 if( policy_alg == exercise_alg &&
1650 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1651 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1652 else
1653 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1654
1655exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001656 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001657 mbedtls_psa_crypto_free( );
1658 mbedtls_free( buffer );
1659}
1660/* END_CASE */
1661
1662/* BEGIN_CASE */
1663void asymmetric_signature_key_policy( int policy_usage,
1664 int policy_alg,
1665 int key_type,
1666 data_t *key_data,
1667 int exercise_alg )
1668{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001669 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001670 psa_key_policy_t policy;
1671 psa_status_t status;
1672 unsigned char payload[16] = {1};
1673 size_t payload_length = sizeof( payload );
1674 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1675 size_t signature_length;
1676
1677 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1678
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001679 TEST_ASSERT( psa_allocate_key( key_type,
1680 KEY_BITS_FROM_DATA( key_type, key_data ),
1681 &handle ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001682 psa_key_policy_init( &policy );
1683 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001684 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001686 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001687 key_data->x, key_data->len ) == PSA_SUCCESS );
1688
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001689 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001690 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001691 signature, sizeof( signature ),
1692 &signature_length );
1693 if( policy_alg == exercise_alg &&
1694 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1695 TEST_ASSERT( status == PSA_SUCCESS );
1696 else
1697 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1698
1699 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001700 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001701 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001702 signature, sizeof( signature ) );
1703 if( policy_alg == exercise_alg &&
1704 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1705 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1706 else
1707 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001708
1709exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001710 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001711 mbedtls_psa_crypto_free( );
1712}
1713/* END_CASE */
1714
1715/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001716void derive_key_policy( int policy_usage,
1717 int policy_alg,
1718 int key_type,
1719 data_t *key_data,
1720 int exercise_alg )
1721{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001722 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001723 psa_key_policy_t policy;
1724 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1725 psa_status_t status;
1726
1727 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1728
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001729 TEST_ASSERT( psa_allocate_key( key_type,
1730 KEY_BITS_FROM_DATA( key_type, key_data ),
1731 &handle ) == PSA_SUCCESS );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001732 psa_key_policy_init( &policy );
1733 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001734 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001735
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001736 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001737 key_data->x, key_data->len ) == PSA_SUCCESS );
1738
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001739 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001740 exercise_alg,
1741 NULL, 0,
1742 NULL, 0,
1743 1 );
1744 if( policy_alg == exercise_alg &&
1745 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1746 TEST_ASSERT( status == PSA_SUCCESS );
1747 else
1748 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1749
1750exit:
1751 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001752 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001753 mbedtls_psa_crypto_free( );
1754}
1755/* END_CASE */
1756
1757/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001758void agreement_key_policy( int policy_usage,
1759 int policy_alg,
1760 int key_type_arg,
1761 data_t *key_data,
1762 int exercise_alg )
1763{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001764 psa_key_handle_t handle = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001765 psa_key_policy_t policy;
1766 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001767 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1768 psa_status_t status;
1769
1770 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1771
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001772 TEST_ASSERT( psa_allocate_key( key_type,
1773 KEY_BITS_FROM_DATA( key_type, key_data ),
1774 &handle ) == PSA_SUCCESS );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001775 psa_key_policy_init( &policy );
1776 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001777 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001778
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001779 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine01d718c2018-09-18 12:01:02 +02001780 key_data->x, key_data->len ) == PSA_SUCCESS );
1781
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001782 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001783
Gilles Peskine01d718c2018-09-18 12:01:02 +02001784 if( policy_alg == exercise_alg &&
1785 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1786 TEST_ASSERT( status == PSA_SUCCESS );
1787 else
1788 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1789
1790exit:
1791 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001792 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001793 mbedtls_psa_crypto_free( );
1794}
1795/* END_CASE */
1796
1797/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001798void hash_setup( int alg_arg,
1799 int expected_status_arg )
1800{
1801 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001802 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001803 psa_hash_operation_t operation;
1804 psa_status_t status;
1805
1806 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1807
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001808 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001809 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001810 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001811
1812exit:
1813 mbedtls_psa_crypto_free( );
1814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001818void hash_bad_order( )
1819{
1820 unsigned char input[] = "";
1821 /* SHA-256 hash of an empty string */
1822 unsigned char hash[] = {
1823 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1824 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1825 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1826 size_t hash_len;
1827 psa_hash_operation_t operation;
1828
1829 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1830
1831 /* psa_hash_update without calling psa_hash_setup beforehand */
1832 memset( &operation, 0, sizeof( operation ) );
1833 TEST_ASSERT( psa_hash_update( &operation,
1834 input, sizeof( input ) ) ==
1835 PSA_ERROR_INVALID_ARGUMENT );
1836
1837 /* psa_hash_verify without calling psa_hash_setup beforehand */
1838 memset( &operation, 0, sizeof( operation ) );
1839 TEST_ASSERT( psa_hash_verify( &operation,
1840 hash, sizeof( hash ) ) ==
1841 PSA_ERROR_INVALID_ARGUMENT );
1842
1843 /* psa_hash_finish without calling psa_hash_setup beforehand */
1844 memset( &operation, 0, sizeof( operation ) );
1845 TEST_ASSERT( psa_hash_finish( &operation,
1846 hash, sizeof( hash ), &hash_len ) ==
1847 PSA_ERROR_INVALID_ARGUMENT );
1848
1849exit:
1850 mbedtls_psa_crypto_free( );
1851}
1852/* END_CASE */
1853
itayzafrir27e69452018-11-01 14:26:34 +02001854/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1855void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001856{
1857 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001858 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1859 * appended to it */
1860 unsigned char hash[] = {
1861 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1862 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1863 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001864 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001865 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001866
1867 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1868
itayzafrir27e69452018-11-01 14:26:34 +02001869 /* psa_hash_verify with a smaller hash than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001870 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1871 TEST_ASSERT( psa_hash_verify( &operation,
1872 hash, expected_size - 1 ) ==
1873 PSA_ERROR_INVALID_SIGNATURE );
1874
itayzafrir27e69452018-11-01 14:26:34 +02001875 /* psa_hash_verify with a non-matching hash */
itayzafrirec93d302018-10-18 18:01:10 +03001876 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrirec93d302018-10-18 18:01:10 +03001877 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001878 hash + 1, expected_size ) ==
itayzafrirec93d302018-10-18 18:01:10 +03001879 PSA_ERROR_INVALID_SIGNATURE );
1880
itayzafrir27e69452018-11-01 14:26:34 +02001881 /* psa_hash_verify with a hash longer than expected */
itayzafrir4271df92018-10-24 18:16:19 +03001882 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrir4271df92018-10-24 18:16:19 +03001883 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001884 hash, sizeof( hash ) ) ==
itayzafrir4271df92018-10-24 18:16:19 +03001885 PSA_ERROR_INVALID_SIGNATURE );
1886
itayzafrirec93d302018-10-18 18:01:10 +03001887exit:
1888 mbedtls_psa_crypto_free( );
1889}
1890/* END_CASE */
1891
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001892/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1893void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001894{
1895 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001896 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001897 size_t expected_size = PSA_HASH_SIZE( alg );
1898 psa_hash_operation_t operation;
1899 size_t hash_len;
1900
1901 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1902
itayzafrir58028322018-10-25 10:22:01 +03001903 /* psa_hash_finish with a smaller hash buffer than expected */
1904 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1905 TEST_ASSERT( psa_hash_finish( &operation,
1906 hash, expected_size - 1,
1907 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1908
1909exit:
1910 mbedtls_psa_crypto_free( );
1911}
1912/* END_CASE */
1913
1914/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001915void mac_setup( int key_type_arg,
1916 data_t *key,
1917 int alg_arg,
1918 int expected_status_arg )
1919{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001920 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001921 psa_key_type_t key_type = key_type_arg;
1922 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001923 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001924 psa_mac_operation_t operation;
1925 psa_key_policy_t policy;
1926 psa_status_t status;
1927
1928 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1929
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001930 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1931 &handle ) == PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001932 psa_key_policy_init( &policy );
1933 psa_key_policy_set_usage( &policy,
1934 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1935 alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001936 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001937
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001938 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001939 key->x, key->len ) == PSA_SUCCESS );
1940
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001941 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001942 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001943 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001944
1945exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001946 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001947 mbedtls_psa_crypto_free( );
1948}
1949/* END_CASE */
1950
1951/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001952void mac_sign( int key_type_arg,
1953 data_t *key,
1954 int alg_arg,
1955 data_t *input,
1956 data_t *expected_mac )
1957{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001958 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001959 psa_key_type_t key_type = key_type_arg;
1960 psa_algorithm_t alg = alg_arg;
1961 psa_mac_operation_t operation;
1962 psa_key_policy_t policy;
1963 /* Leave a little extra room in the output buffer. At the end of the
1964 * test, we'll check that the implementation didn't overwrite onto
1965 * this extra room. */
1966 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1967 size_t mac_buffer_size =
1968 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1969 size_t mac_length = 0;
1970
1971 memset( actual_mac, '+', sizeof( actual_mac ) );
1972 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1973 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1974
1975 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1976
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001977 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1978 &handle ) == PSA_SUCCESS );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001979 psa_key_policy_init( &policy );
1980 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001981 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001982
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001983 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001984 key->x, key->len ) == PSA_SUCCESS );
1985
1986 /* Calculate the MAC. */
1987 TEST_ASSERT( psa_mac_sign_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001988 handle, alg ) == PSA_SUCCESS );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001989 TEST_ASSERT( psa_mac_update( &operation,
1990 input->x, input->len ) == PSA_SUCCESS );
1991 TEST_ASSERT( psa_mac_sign_finish( &operation,
1992 actual_mac, mac_buffer_size,
1993 &mac_length ) == PSA_SUCCESS );
1994
1995 /* Compare with the expected value. */
1996 TEST_ASSERT( mac_length == expected_mac->len );
1997 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
1998
1999 /* Verify that the end of the buffer is untouched. */
2000 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2001 sizeof( actual_mac ) - mac_length ) );
2002
2003exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002004 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005 mbedtls_psa_crypto_free( );
2006}
2007/* END_CASE */
2008
2009/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002010void mac_verify( int key_type_arg,
2011 data_t *key,
2012 int alg_arg,
2013 data_t *input,
2014 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002015{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002016 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002017 psa_key_type_t key_type = key_type_arg;
2018 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002019 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07002020 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002021
Gilles Peskine69c12672018-06-28 00:07:19 +02002022 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2023
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002025 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002026 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002027 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002028 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2029 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002030
2031 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2032
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002033 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2034 &handle ) == PSA_SUCCESS );
mohammad16036df908f2018-04-02 08:34:15 -07002035 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002036 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002037 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
mohammad16036df908f2018-04-02 08:34:15 -07002038
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002039 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002040 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002041
Gilles Peskine89167cb2018-07-08 20:12:23 +02002042 TEST_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002043 handle, alg ) == PSA_SUCCESS );
2044 TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002045 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002046 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02002047 TEST_ASSERT( psa_mac_verify_finish( &operation,
2048 expected_mac->x,
2049 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002050
2051exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002052 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002053 mbedtls_psa_crypto_free( );
2054}
2055/* END_CASE */
2056
2057/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002058void cipher_setup( int key_type_arg,
2059 data_t *key,
2060 int alg_arg,
2061 int expected_status_arg )
2062{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002063 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002064 psa_key_type_t key_type = key_type_arg;
2065 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002066 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002067 psa_cipher_operation_t operation;
2068 psa_key_policy_t policy;
2069 psa_status_t status;
2070
2071 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2072
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002073 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2074 &handle ) == PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002075 psa_key_policy_init( &policy );
2076 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002077 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002078
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002079 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002080 key->x, key->len ) == PSA_SUCCESS );
2081
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002082 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002083 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002084 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002085
2086exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002087 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002088 mbedtls_psa_crypto_free( );
2089}
2090/* END_CASE */
2091
2092/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002093void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002094 data_t *key,
2095 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002096 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002097{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002098 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002099 psa_status_t status;
2100 psa_key_type_t key_type = key_type_arg;
2101 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002102 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002103 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002104 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002105 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002106 size_t output_buffer_size = 0;
2107 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002108 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002109 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002110 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002111
Gilles Peskine50e586b2018-06-08 14:28:46 +02002112 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002113 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002114 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002115 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2116 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2117 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002118
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002119 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2120 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002121
2122 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2123
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002124 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2125 &handle ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002126 psa_key_policy_init( &policy );
2127 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002128 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002129
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002130 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002131 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002132
Gilles Peskinefe119512018-07-08 21:39:34 +02002133 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002134 handle, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002135
Gilles Peskinefe119512018-07-08 21:39:34 +02002136 TEST_ASSERT( psa_cipher_set_iv( &operation,
2137 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002138 output_buffer_size = ( (size_t) input->len +
2139 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002140 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002141
Gilles Peskine4abf7412018-06-18 16:35:34 +02002142 TEST_ASSERT( psa_cipher_update( &operation,
2143 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002144 output, output_buffer_size,
2145 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002146 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002147 status = psa_cipher_finish( &operation,
2148 output + function_output_length,
2149 output_buffer_size,
2150 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002151 total_output_length += function_output_length;
2152
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002153 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002154 if( expected_status == PSA_SUCCESS )
2155 {
2156 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002157 ASSERT_COMPARE( expected_output->x, expected_output->len,
2158 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002159 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002160
Gilles Peskine50e586b2018-06-08 14:28:46 +02002161exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002162 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002163 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002164 mbedtls_psa_crypto_free( );
2165}
2166/* END_CASE */
2167
2168/* BEGIN_CASE */
2169void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002170 data_t *key,
2171 data_t *input,
2172 int first_part_size,
2173 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002174{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002175 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002176 psa_key_type_t key_type = key_type_arg;
2177 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002178 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002179 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002180 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002181 size_t output_buffer_size = 0;
2182 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002183 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002184 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002185 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002186
Gilles Peskine50e586b2018-06-08 14:28:46 +02002187 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002188 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002189 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002190 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2191 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2192 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002193
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002194 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2195 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002196
2197 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2198
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002199 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2200 &handle ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002201 psa_key_policy_init( &policy );
2202 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002203 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002204
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002205 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002206 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002207
Gilles Peskinefe119512018-07-08 21:39:34 +02002208 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002209 handle, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002210
Gilles Peskinefe119512018-07-08 21:39:34 +02002211 TEST_ASSERT( psa_cipher_set_iv( &operation,
2212 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002213 output_buffer_size = ( (size_t) input->len +
2214 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002215 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002216
Gilles Peskine4abf7412018-06-18 16:35:34 +02002217 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002218 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002219 output, output_buffer_size,
2220 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002221 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002222 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002223 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002224 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002225 output, output_buffer_size,
2226 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002227 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002228 TEST_ASSERT( psa_cipher_finish( &operation,
2229 output + function_output_length,
2230 output_buffer_size,
2231 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002232 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002233 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2234
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002235 ASSERT_COMPARE( expected_output->x, expected_output->len,
2236 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002237
2238exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002239 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002240 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002241 mbedtls_psa_crypto_free( );
2242}
2243/* END_CASE */
2244
2245/* BEGIN_CASE */
2246void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002247 data_t *key,
2248 data_t *input,
2249 int first_part_size,
2250 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002251{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002252 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002253
2254 psa_key_type_t key_type = key_type_arg;
2255 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002256 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002257 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002258 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002259 size_t output_buffer_size = 0;
2260 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002261 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002262 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002263 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002264
Gilles Peskine50e586b2018-06-08 14:28:46 +02002265 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002266 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002267 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002268 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2269 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2270 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002271
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002272 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2273 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002274
2275 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2276
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002277 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2278 &handle ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002279 psa_key_policy_init( &policy );
2280 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002281 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002282
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002283 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002284 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002285
Gilles Peskinefe119512018-07-08 21:39:34 +02002286 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002287 handle, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002288
Gilles Peskinefe119512018-07-08 21:39:34 +02002289 TEST_ASSERT( psa_cipher_set_iv( &operation,
2290 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002291
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002292 output_buffer_size = ( (size_t) input->len +
2293 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002294 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002295
Gilles Peskine4abf7412018-06-18 16:35:34 +02002296 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2297 TEST_ASSERT( psa_cipher_update( &operation,
2298 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002299 output, output_buffer_size,
2300 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002301 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002302 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002303 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002304 input->len - 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_finish( &operation,
2309 output + function_output_length,
2310 output_buffer_size,
2311 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002312 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002313 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2314
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002315 ASSERT_COMPARE( expected_output->x, expected_output->len,
2316 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002317
2318exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002319 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002320 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002321 mbedtls_psa_crypto_free( );
2322}
2323/* END_CASE */
2324
Gilles Peskine50e586b2018-06-08 14:28:46 +02002325/* BEGIN_CASE */
2326void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002327 data_t *key,
2328 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002329 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002330{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002331 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002332 psa_status_t status;
2333 psa_key_type_t key_type = key_type_arg;
2334 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002335 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002336 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002337 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002338 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002339 size_t output_buffer_size = 0;
2340 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002341 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002342 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002343 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002344
Gilles Peskine50e586b2018-06-08 14:28:46 +02002345 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002346 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002347 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002348 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2349 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2350 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002351
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002352 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2353 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002354
2355 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2356
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002357 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2358 &handle ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002359 psa_key_policy_init( &policy );
2360 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002361 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002362
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002363 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002364 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002365
Gilles Peskinefe119512018-07-08 21:39:34 +02002366 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002367 handle, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002368
Gilles Peskinefe119512018-07-08 21:39:34 +02002369 TEST_ASSERT( psa_cipher_set_iv( &operation,
2370 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002371
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002372 output_buffer_size = ( (size_t) input->len +
2373 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002374 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002375
Gilles Peskine4abf7412018-06-18 16:35:34 +02002376 TEST_ASSERT( psa_cipher_update( &operation,
2377 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002378 output, output_buffer_size,
2379 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002380 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002381 status = psa_cipher_finish( &operation,
2382 output + function_output_length,
2383 output_buffer_size,
2384 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002385 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002386 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002387
2388 if( expected_status == PSA_SUCCESS )
2389 {
2390 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002391 ASSERT_COMPARE( expected_output->x, expected_output->len,
2392 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002393 }
2394
Gilles Peskine50e586b2018-06-08 14:28:46 +02002395exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002396 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002397 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002398 mbedtls_psa_crypto_free( );
2399}
2400/* END_CASE */
2401
Gilles Peskine50e586b2018-06-08 14:28:46 +02002402/* BEGIN_CASE */
2403void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002404 data_t *key,
2405 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002406{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002407 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002408 psa_key_type_t key_type = key_type_arg;
2409 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002410 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002411 size_t iv_size = 16;
2412 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002413 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002414 size_t output1_size = 0;
2415 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002416 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002417 size_t output2_size = 0;
2418 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002419 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002420 psa_cipher_operation_t operation1;
2421 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002422 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002423
mohammad1603d7d7ba52018-03-12 18:51:53 +02002424 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002425 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002426 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2427 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002428
mohammad1603d7d7ba52018-03-12 18:51:53 +02002429 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2430
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002431 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2432 &handle ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002433 psa_key_policy_init( &policy );
2434 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002435 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002436
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002437 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002438 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002439
Gilles Peskinefe119512018-07-08 21:39:34 +02002440 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002441 handle, alg ) == PSA_SUCCESS );
Gilles Peskinefe119512018-07-08 21:39:34 +02002442 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002443 handle, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002444
Gilles Peskinefe119512018-07-08 21:39:34 +02002445 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2446 iv, iv_size,
2447 &iv_length ) == PSA_SUCCESS );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002448 output1_size = ( (size_t) input->len +
2449 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002450 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002451
Gilles Peskine4abf7412018-06-18 16:35:34 +02002452 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002453 output1, output1_size,
2454 &output1_length ) == PSA_SUCCESS );
2455 TEST_ASSERT( psa_cipher_finish( &operation1,
2456 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002457 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002458
Gilles Peskine048b7f02018-06-08 14:20:49 +02002459 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002460
2461 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2462
2463 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002464 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002465
Gilles Peskinefe119512018-07-08 21:39:34 +02002466 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2467 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002468 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2469 output2, output2_size,
2470 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002471 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002472 TEST_ASSERT( psa_cipher_finish( &operation2,
2473 output2 + output2_length,
2474 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002475 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002476
Gilles Peskine048b7f02018-06-08 14:20:49 +02002477 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002478
Janos Follath25c4fa82018-07-06 16:23:25 +01002479 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002480
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002481 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002482
2483exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002484 mbedtls_free( output1 );
2485 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002486 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002487 mbedtls_psa_crypto_free( );
2488}
2489/* END_CASE */
2490
2491/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002492void cipher_verify_output_multipart( int alg_arg,
2493 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002494 data_t *key,
2495 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002496 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002497{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002498 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002499 psa_key_type_t key_type = key_type_arg;
2500 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002501 unsigned char iv[16] = {0};
2502 size_t iv_size = 16;
2503 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002504 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002505 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002506 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002507 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002508 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002509 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002510 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002511 psa_cipher_operation_t operation1;
2512 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002513 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002514
Moran Pekerded84402018-06-06 16:36:50 +03002515 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002516 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002517 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2518 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002519
Moran Pekerded84402018-06-06 16:36:50 +03002520 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2521
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002522 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2523 &handle ) == PSA_SUCCESS );
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 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002526 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002527
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002528 TEST_ASSERT( psa_import_key( handle, 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,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002532 handle, alg ) == PSA_SUCCESS );
Gilles Peskinefe119512018-07-08 21:39:34 +02002533 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002534 handle, 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 );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002539 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 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002596 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002597 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{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002609 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002610 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
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002635 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2636 &handle ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002637 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002638 psa_key_policy_set_usage( &policy,
2639 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2640 alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002641 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002642
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002643 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002644 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002645
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002646 TEST_ASSERT( psa_aead_encrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002647 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002648 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002649 additional_data->len,
2650 input_data->x, input_data->len,
2651 output_data, output_size,
2652 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002653
2654 if( PSA_SUCCESS == expected_result )
2655 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002656 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002657
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002658 TEST_ASSERT( psa_aead_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002659 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002660 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002661 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002662 output_data, output_length,
2663 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002664 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002665
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002666 ASSERT_COMPARE( input_data->x, input_data->len,
2667 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002668 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002669
Gilles Peskinea1cac842018-06-11 19:33:02 +02002670exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002671 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002672 mbedtls_free( output_data );
2673 mbedtls_free( output_data2 );
2674 mbedtls_psa_crypto_free( );
2675}
2676/* END_CASE */
2677
2678/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002679void aead_encrypt( int key_type_arg, data_t *key_data,
2680 int alg_arg,
2681 data_t *nonce,
2682 data_t *additional_data,
2683 data_t *input_data,
2684 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002685{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002686 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002687 psa_key_type_t key_type = key_type_arg;
2688 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002689 unsigned char *output_data = NULL;
2690 size_t output_size = 0;
2691 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002692 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002693 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002694
Gilles Peskinea1cac842018-06-11 19:33:02 +02002695 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002696 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002697 TEST_ASSERT( additional_data != NULL );
2698 TEST_ASSERT( nonce != NULL );
2699 TEST_ASSERT( expected_result != NULL );
2700 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2701 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2702 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2703 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2704 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2705
Gilles Peskine4abf7412018-06-18 16:35:34 +02002706 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002707 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002708
Gilles Peskinea1cac842018-06-11 19:33:02 +02002709 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2710
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002711 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2712 &handle ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002713 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002714 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002715 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002717 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002718 key_data->x,
2719 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002720
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002721 TEST_ASSERT( psa_aead_encrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002722 nonce->x, nonce->len,
2723 additional_data->x, additional_data->len,
2724 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002725 output_data, output_size,
2726 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002727
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002728 ASSERT_COMPARE( expected_result->x, expected_result->len,
2729 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002730
Gilles Peskinea1cac842018-06-11 19:33:02 +02002731exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002732 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002733 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002734 mbedtls_psa_crypto_free( );
2735}
2736/* END_CASE */
2737
2738/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002739void aead_decrypt( int key_type_arg, data_t *key_data,
2740 int alg_arg,
2741 data_t *nonce,
2742 data_t *additional_data,
2743 data_t *input_data,
2744 data_t *expected_data,
2745 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002746{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002747 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002748 psa_key_type_t key_type = key_type_arg;
2749 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002750 unsigned char *output_data = NULL;
2751 size_t output_size = 0;
2752 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002753 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002754 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002755 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002756
Gilles Peskinea1cac842018-06-11 19:33:02 +02002757 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002758 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002759 TEST_ASSERT( additional_data != NULL );
2760 TEST_ASSERT( nonce != NULL );
2761 TEST_ASSERT( expected_data != NULL );
2762 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2763 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2764 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2765 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2766 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2767
Gilles Peskine4abf7412018-06-18 16:35:34 +02002768 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002769 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002770
Gilles Peskinea1cac842018-06-11 19:33:02 +02002771 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2772
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002773 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2774 &handle ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002775 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002776 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002777 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002778
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002779 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002780 key_data->x,
2781 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002782
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002783 TEST_ASSERT( psa_aead_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002784 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002785 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002786 additional_data->len,
2787 input_data->x, input_data->len,
2788 output_data, output_size,
2789 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002790
Gilles Peskine2d277862018-06-18 15:41:12 +02002791 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002792 ASSERT_COMPARE( expected_data->x, expected_data->len,
2793 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002794
Gilles Peskinea1cac842018-06-11 19:33:02 +02002795exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002796 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002797 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002798 mbedtls_psa_crypto_free( );
2799}
2800/* END_CASE */
2801
2802/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002803void signature_size( int type_arg,
2804 int bits,
2805 int alg_arg,
2806 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002807{
2808 psa_key_type_t type = type_arg;
2809 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002810 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002811 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2812exit:
2813 ;
2814}
2815/* END_CASE */
2816
2817/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002818void sign_deterministic( int key_type_arg, data_t *key_data,
2819 int alg_arg, data_t *input_data,
2820 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002821{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002822 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002823 psa_key_type_t key_type = key_type_arg;
2824 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002825 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002826 unsigned char *signature = NULL;
2827 size_t signature_size;
2828 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002829 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002830
Gilles Peskine20035e32018-02-03 22:44:14 +01002831 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002832 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002833 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002834 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2835 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2836 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002837
2838 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2839
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002840 TEST_ASSERT( psa_allocate_key( key_type,
2841 KEY_BITS_FROM_DATA( key_type, key_data ),
2842 &handle ) == PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002843 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002844 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002845 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002846
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002847 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002848 key_data->x,
2849 key_data->len ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002850 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002851 NULL,
2852 &key_bits ) == PSA_SUCCESS );
2853
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002854 /* Allocate a buffer which has the size advertized by the
2855 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002856 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2857 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002858 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002859 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002860 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002861
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002862 /* Perform the signature. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002863 TEST_ASSERT( psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002864 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002865 signature, signature_size,
2866 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002867 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002868 ASSERT_COMPARE( output_data->x, output_data->len,
2869 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002870
2871exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002872 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002873 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002874 mbedtls_psa_crypto_free( );
2875}
2876/* END_CASE */
2877
2878/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002879void sign_fail( int key_type_arg, data_t *key_data,
2880 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002881 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002882{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002883 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002884 psa_key_type_t key_type = key_type_arg;
2885 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002886 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002887 psa_status_t actual_status;
2888 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002889 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002890 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002891 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002892
Gilles Peskine20035e32018-02-03 22:44:14 +01002893 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002894 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002895 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2896 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2897
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002898 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002899
2900 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2901
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002902 TEST_ASSERT( psa_allocate_key( key_type,
2903 KEY_BITS_FROM_DATA( key_type, key_data ),
2904 &handle ) == PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002905 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002906 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002907 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002908
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002909 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002910 key_data->x,
2911 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002912
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002913 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002914 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002915 signature, signature_size,
2916 &signature_length );
2917 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002918 /* The value of *signature_length is unspecified on error, but
2919 * whatever it is, it should be less than signature_size, so that
2920 * if the caller tries to read *signature_length bytes without
2921 * checking the error code then they don't overflow a buffer. */
2922 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002923
2924exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002925 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002926 mbedtls_free( signature );
2927 mbedtls_psa_crypto_free( );
2928}
2929/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002930
2931/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002932void sign_verify( int key_type_arg, data_t *key_data,
2933 int alg_arg, data_t *input_data )
2934{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002935 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002936 psa_key_type_t key_type = key_type_arg;
2937 psa_algorithm_t alg = alg_arg;
2938 size_t key_bits;
2939 unsigned char *signature = NULL;
2940 size_t signature_size;
2941 size_t signature_length = 0xdeadbeef;
2942 psa_key_policy_t policy;
2943
2944 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2945
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002946 TEST_ASSERT( psa_allocate_key( key_type,
2947 KEY_BITS_FROM_DATA( key_type, key_data ),
2948 &handle ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002949 psa_key_policy_init( &policy );
2950 psa_key_policy_set_usage( &policy,
2951 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2952 alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002953 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002954
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002955 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02002956 key_data->x,
2957 key_data->len ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002958 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine9911b022018-06-29 17:30:48 +02002959 NULL,
2960 &key_bits ) == PSA_SUCCESS );
2961
2962 /* Allocate a buffer which has the size advertized by the
2963 * library. */
2964 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2965 key_bits, alg );
2966 TEST_ASSERT( signature_size != 0 );
2967 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002968 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002969
2970 /* Perform the signature. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002971 TEST_ASSERT( psa_asymmetric_sign( handle, alg,
Gilles Peskine9911b022018-06-29 17:30:48 +02002972 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002973 signature, signature_size,
2974 &signature_length ) == PSA_SUCCESS );
2975 /* Check that the signature length looks sensible. */
2976 TEST_ASSERT( signature_length <= signature_size );
2977 TEST_ASSERT( signature_length > 0 );
2978
2979 /* Use the library to verify that the signature is correct. */
2980 TEST_ASSERT( psa_asymmetric_verify(
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002981 handle, alg,
Gilles Peskine9911b022018-06-29 17:30:48 +02002982 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002983 signature, signature_length ) == PSA_SUCCESS );
2984
2985 if( input_data->len != 0 )
2986 {
2987 /* Flip a bit in the input and verify that the signature is now
2988 * detected as invalid. Flip a bit at the beginning, not at the end,
2989 * because ECDSA may ignore the last few bits of the input. */
2990 input_data->x[0] ^= 1;
2991 TEST_ASSERT( psa_asymmetric_verify(
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002992 handle, alg,
Gilles Peskine9911b022018-06-29 17:30:48 +02002993 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002994 signature,
2995 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2996 }
2997
2998exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002999 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003000 mbedtls_free( signature );
3001 mbedtls_psa_crypto_free( );
3002}
3003/* END_CASE */
3004
3005/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003006void asymmetric_verify( int key_type_arg, data_t *key_data,
3007 int alg_arg, data_t *hash_data,
3008 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003009{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003010 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003011 psa_key_type_t key_type = key_type_arg;
3012 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003013 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03003014
Gilles Peskine69c12672018-06-28 00:07:19 +02003015 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3016
itayzafrir5c753392018-05-08 11:18:38 +03003017 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003018 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003019 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003020 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3021 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3022 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003023
3024 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3025
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003026 TEST_ASSERT( psa_allocate_key( key_type,
3027 KEY_BITS_FROM_DATA( key_type, key_data ),
3028 &handle ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003029 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003030 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003031 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003032
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003033 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003034 key_data->x,
3035 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003036
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003037 TEST_ASSERT( psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003038 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003039 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003040 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003041exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003042 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003043 mbedtls_psa_crypto_free( );
3044}
3045/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003046
3047/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003048void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3049 int alg_arg, data_t *hash_data,
3050 data_t *signature_data,
3051 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003052{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003053 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003054 psa_key_type_t key_type = key_type_arg;
3055 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003056 psa_status_t actual_status;
3057 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003058 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003059
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003060 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003061 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003062 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003063 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3064 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3065 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003066
3067 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3068
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003069 TEST_ASSERT( psa_allocate_key( key_type,
3070 KEY_BITS_FROM_DATA( key_type, key_data ),
3071 &handle ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003072 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003073 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003074 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003075
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003076 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003077 key_data->x,
3078 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003079
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003080 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003081 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003082 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003083 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003084
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085 TEST_ASSERT( actual_status == expected_status );
3086
3087exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003088 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003089 mbedtls_psa_crypto_free( );
3090}
3091/* END_CASE */
3092
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003094void asymmetric_encrypt( int key_type_arg,
3095 data_t *key_data,
3096 int alg_arg,
3097 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003098 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003099 int expected_output_length_arg,
3100 int expected_status_arg )
3101{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003102 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003103 psa_key_type_t key_type = key_type_arg;
3104 psa_algorithm_t alg = alg_arg;
3105 size_t expected_output_length = expected_output_length_arg;
3106 size_t key_bits;
3107 unsigned char *output = NULL;
3108 size_t output_size;
3109 size_t output_length = ~0;
3110 psa_status_t actual_status;
3111 psa_status_t expected_status = expected_status_arg;
3112 psa_key_policy_t policy;
3113
3114 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3115
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003116
Gilles Peskine656896e2018-06-29 19:12:28 +02003117 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003118 TEST_ASSERT( psa_allocate_key( key_type,
3119 KEY_BITS_FROM_DATA( key_type, key_data ),
3120 &handle ) == PSA_SUCCESS );
Gilles Peskine656896e2018-06-29 19:12:28 +02003121 psa_key_policy_init( &policy );
3122 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003123 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
3124 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine656896e2018-06-29 19:12:28 +02003125 key_data->x,
3126 key_data->len ) == PSA_SUCCESS );
3127
3128 /* Determine the maximum output length */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003129 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine656896e2018-06-29 19:12:28 +02003130 NULL,
3131 &key_bits ) == PSA_SUCCESS );
3132 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003133 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003134
3135 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003136 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003137 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003138 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003139 output, output_size,
3140 &output_length );
3141 TEST_ASSERT( actual_status == expected_status );
3142 TEST_ASSERT( output_length == expected_output_length );
3143
Gilles Peskine68428122018-06-30 18:42:41 +02003144 /* If the label is empty, the test framework puts a non-null pointer
3145 * in label->x. Test that a null pointer works as well. */
3146 if( label->len == 0 )
3147 {
3148 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003149 if( output_size != 0 )
3150 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003151 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003152 input_data->x, input_data->len,
3153 NULL, label->len,
3154 output, output_size,
3155 &output_length );
3156 TEST_ASSERT( actual_status == expected_status );
3157 TEST_ASSERT( output_length == expected_output_length );
3158 }
3159
Gilles Peskine656896e2018-06-29 19:12:28 +02003160exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003161 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003162 mbedtls_free( output );
3163 mbedtls_psa_crypto_free( );
3164}
3165/* END_CASE */
3166
3167/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003168void asymmetric_encrypt_decrypt( int key_type_arg,
3169 data_t *key_data,
3170 int alg_arg,
3171 data_t *input_data,
3172 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003173{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003174 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003175 psa_key_type_t key_type = key_type_arg;
3176 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003177 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003179 size_t output_size;
3180 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003181 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003182 size_t output2_size;
3183 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003184 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003185
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003186 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003188 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3189 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3190
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3192
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003193 TEST_ASSERT( psa_allocate_key( key_type,
3194 KEY_BITS_FROM_DATA( key_type, key_data ),
3195 &handle ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003196 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003197 psa_key_policy_set_usage( &policy,
3198 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003199 alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003200 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003201
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003202 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003203 key_data->x,
3204 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003205
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003206
3207 /* Determine the maximum ciphertext length */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003208 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003209 NULL,
3210 &key_bits ) == PSA_SUCCESS );
3211 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003212 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003213 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003214 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003215
Gilles Peskineeebd7382018-06-08 18:11:54 +02003216 /* We test encryption by checking that encrypt-then-decrypt gives back
3217 * the original plaintext because of the non-optional random
3218 * part of encryption process which prevents using fixed vectors. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003219 TEST_ASSERT( psa_asymmetric_encrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003220 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003221 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003222 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003223 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003224 /* We don't know what ciphertext length to expect, but check that
3225 * it looks sensible. */
3226 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003227
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003228 TEST_ASSERT( psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003229 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02003230 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003231 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003232 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003233 ASSERT_COMPARE( input_data->x, input_data->len,
3234 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003235
3236exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003237 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003238 mbedtls_free( output );
3239 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003240 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003241}
3242/* END_CASE */
3243
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003244/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003245void asymmetric_decrypt( int key_type_arg,
3246 data_t *key_data,
3247 int alg_arg,
3248 data_t *input_data,
3249 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003250 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003251{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003252 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253 psa_key_type_t key_type = key_type_arg;
3254 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003255 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003256 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003257 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003258 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003260 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003261 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003263 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3264 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3265 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3266
Gilles Peskine4abf7412018-06-18 16:35:34 +02003267 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003268 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003269
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3271
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003272 TEST_ASSERT( psa_allocate_key( key_type,
3273 KEY_BITS_FROM_DATA( key_type, key_data ),
3274 &handle ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003275 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003276 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003277 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003278
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003279 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003280 key_data->x,
3281 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003282
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003283 TEST_ASSERT( psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003284 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003285 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003286 output,
3287 output_size,
3288 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003289 ASSERT_COMPARE( expected_data->x, expected_data->len,
3290 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003291
Gilles Peskine68428122018-06-30 18:42:41 +02003292 /* If the label is empty, the test framework puts a non-null pointer
3293 * in label->x. Test that a null pointer works as well. */
3294 if( label->len == 0 )
3295 {
3296 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003297 if( output_size != 0 )
3298 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003299 TEST_ASSERT( psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003300 input_data->x, input_data->len,
3301 NULL, label->len,
3302 output,
3303 output_size,
3304 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003305 ASSERT_COMPARE( expected_data->x, expected_data->len,
3306 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003307 }
3308
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003309exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003310 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003311 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003313}
3314/* END_CASE */
3315
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003316/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003317void asymmetric_decrypt_fail( int key_type_arg,
3318 data_t *key_data,
3319 int alg_arg,
3320 data_t *input_data,
3321 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003322 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003323{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003324 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003325 psa_key_type_t key_type = key_type_arg;
3326 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003327 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003328 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003329 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003330 psa_status_t actual_status;
3331 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003332 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003333
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003334 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003335 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003336 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3337 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3338
Gilles Peskine4abf7412018-06-18 16:35:34 +02003339 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003340 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003341
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003342 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3343
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003344 TEST_ASSERT( psa_allocate_key( key_type,
3345 KEY_BITS_FROM_DATA( key_type, key_data ),
3346 &handle ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003347 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003348 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003349 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003350
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003351 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003352 key_data->x,
3353 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003354
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003355 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003356 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003357 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003358 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003359 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003360 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003361 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003362
Gilles Peskine68428122018-06-30 18:42:41 +02003363 /* If the label is empty, the test framework puts a non-null pointer
3364 * in label->x. Test that a null pointer works as well. */
3365 if( label->len == 0 )
3366 {
3367 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003368 if( output_size != 0 )
3369 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003370 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003371 input_data->x, input_data->len,
3372 NULL, label->len,
3373 output, output_size,
3374 &output_length );
3375 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003376 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003377 }
3378
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003379exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003380 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003381 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003382 mbedtls_psa_crypto_free( );
3383}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003384/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003385
3386/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003387void derive_setup( int key_type_arg,
3388 data_t *key_data,
3389 int alg_arg,
3390 data_t *salt,
3391 data_t *label,
3392 int requested_capacity_arg,
3393 int expected_status_arg )
3394{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003395 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003396 size_t key_type = key_type_arg;
3397 psa_algorithm_t alg = alg_arg;
3398 size_t requested_capacity = requested_capacity_arg;
3399 psa_status_t expected_status = expected_status_arg;
3400 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3401 psa_key_policy_t policy;
3402
3403 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3404
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003405 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3406 &handle ) == PSA_SUCCESS );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003407 psa_key_policy_init( &policy );
3408 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003409 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003410
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003411 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003412 key_data->x,
3413 key_data->len ) == PSA_SUCCESS );
3414
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003415 TEST_ASSERT( psa_key_derivation( &generator, handle, alg,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003416 salt->x, salt->len,
3417 label->x, label->len,
3418 requested_capacity ) == expected_status );
3419
3420exit:
3421 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003422 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003423 mbedtls_psa_crypto_free( );
3424}
3425/* END_CASE */
3426
3427/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003428void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003429{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003430 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003431 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003432 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003433 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003434 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003435 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003436 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3437 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3438 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003439 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003440
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003441 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3442
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003443 TEST_ASSERT( psa_allocate_key( key_type,
3444 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3445 &handle ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003446 psa_key_policy_init( &policy );
3447 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003448 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003449
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003450 TEST_ASSERT( psa_import_key( handle, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003451 key_data,
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003452 sizeof( key_data ) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003453
3454 /* valid key derivation */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003455 TEST_ASSERT( psa_key_derivation( &generator, handle, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003456 NULL, 0,
3457 NULL, 0,
3458 capacity ) == PSA_SUCCESS );
3459
3460 /* state of generator shouldn't allow additional generation */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003461 TEST_ASSERT( psa_key_derivation( &generator, handle, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003462 NULL, 0,
3463 NULL, 0,
3464 capacity ) == PSA_ERROR_BAD_STATE );
3465
3466 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3467 == PSA_SUCCESS );
3468
3469 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3470 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3471
3472
3473exit:
3474 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003475 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003476 mbedtls_psa_crypto_free( );
3477}
3478/* END_CASE */
3479
3480
3481/* BEGIN_CASE */
3482void test_derive_invalid_generator_tests( )
3483{
3484 uint8_t output_buffer[16];
3485 size_t buffer_size = 16;
3486 size_t capacity = 0;
3487 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3488
Nir Sonnenschein50789302018-10-31 12:16:38 +02003489 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003490 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003491
3492 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003493 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003494
Nir Sonnenschein50789302018-10-31 12:16:38 +02003495 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003496
Nir Sonnenschein50789302018-10-31 12:16:38 +02003497 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003498 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003499
Nir Sonnenschein50789302018-10-31 12:16:38 +02003500 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003501 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003502
3503exit:
3504 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003505}
3506/* END_CASE */
3507
3508/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003509void derive_output( int alg_arg,
3510 data_t *key_data,
3511 data_t *salt,
3512 data_t *label,
3513 int requested_capacity_arg,
3514 data_t *expected_output1,
3515 data_t *expected_output2 )
3516{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003517 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003518 psa_algorithm_t alg = alg_arg;
3519 size_t requested_capacity = requested_capacity_arg;
3520 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3521 uint8_t *expected_outputs[2] =
3522 {expected_output1->x, expected_output2->x};
3523 size_t output_sizes[2] =
3524 {expected_output1->len, expected_output2->len};
3525 size_t output_buffer_size = 0;
3526 uint8_t *output_buffer = NULL;
3527 size_t expected_capacity;
3528 size_t current_capacity;
3529 psa_key_policy_t policy;
3530 psa_status_t status;
3531 unsigned i;
3532
3533 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3534 {
3535 if( output_sizes[i] > output_buffer_size )
3536 output_buffer_size = output_sizes[i];
3537 if( output_sizes[i] == 0 )
3538 expected_outputs[i] = NULL;
3539 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003540 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003541 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3542
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003543 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3544 PSA_BYTES_TO_BITS( key_data->len ),
3545 &handle ) == PSA_SUCCESS );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003546 psa_key_policy_init( &policy );
3547 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003548 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003549
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003550 TEST_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003551 key_data->x,
3552 key_data->len ) == PSA_SUCCESS );
3553
3554 /* Extraction phase. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003555 TEST_ASSERT( psa_key_derivation( &generator, handle, alg,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003556 salt->x, salt->len,
3557 label->x, label->len,
3558 requested_capacity ) == PSA_SUCCESS );
3559 TEST_ASSERT( psa_get_generator_capacity( &generator,
3560 &current_capacity ) ==
3561 PSA_SUCCESS );
3562 TEST_ASSERT( current_capacity == requested_capacity );
3563 expected_capacity = requested_capacity;
3564
3565 /* Expansion phase. */
3566 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3567 {
3568 /* Read some bytes. */
3569 status = psa_generator_read( &generator,
3570 output_buffer, output_sizes[i] );
3571 if( expected_capacity == 0 && output_sizes[i] == 0 )
3572 {
3573 /* Reading 0 bytes when 0 bytes are available can go either way. */
3574 TEST_ASSERT( status == PSA_SUCCESS ||
3575 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3576 continue;
3577 }
3578 else if( expected_capacity == 0 ||
3579 output_sizes[i] > expected_capacity )
3580 {
3581 /* Capacity exceeded. */
3582 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3583 expected_capacity = 0;
3584 continue;
3585 }
3586 /* Success. Check the read data. */
3587 TEST_ASSERT( status == PSA_SUCCESS );
3588 if( output_sizes[i] != 0 )
3589 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3590 output_sizes[i] ) == 0 );
3591 /* Check the generator status. */
3592 expected_capacity -= output_sizes[i];
3593 TEST_ASSERT( psa_get_generator_capacity( &generator,
3594 &current_capacity ) ==
3595 PSA_SUCCESS );
3596 TEST_ASSERT( expected_capacity == current_capacity );
3597 }
3598 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3599
3600exit:
3601 mbedtls_free( output_buffer );
3602 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003603 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003604 mbedtls_psa_crypto_free( );
3605}
3606/* END_CASE */
3607
3608/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003609void derive_full( int alg_arg,
3610 data_t *key_data,
3611 data_t *salt,
3612 data_t *label,
3613 int requested_capacity_arg )
3614{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003615 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003616 psa_algorithm_t alg = alg_arg;
3617 size_t requested_capacity = requested_capacity_arg;
3618 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3619 unsigned char output_buffer[16];
3620 size_t expected_capacity = requested_capacity;
3621 size_t current_capacity;
3622 psa_key_policy_t policy;
3623
3624 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3625
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003626 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3627 PSA_BYTES_TO_BITS( key_data->len ),
3628 &handle ) == PSA_SUCCESS );
Gilles Peskined54931c2018-07-17 21:06:59 +02003629 psa_key_policy_init( &policy );
3630 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003631 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskined54931c2018-07-17 21:06:59 +02003632
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003633 TEST_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
Gilles Peskined54931c2018-07-17 21:06:59 +02003634 key_data->x,
3635 key_data->len ) == PSA_SUCCESS );
3636
3637 /* Extraction phase. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003638 TEST_ASSERT( psa_key_derivation( &generator, handle, alg,
Gilles Peskined54931c2018-07-17 21:06:59 +02003639 salt->x, salt->len,
3640 label->x, label->len,
3641 requested_capacity ) == PSA_SUCCESS );
3642 TEST_ASSERT( psa_get_generator_capacity( &generator,
3643 &current_capacity ) ==
3644 PSA_SUCCESS );
3645 TEST_ASSERT( current_capacity == expected_capacity );
3646
3647 /* Expansion phase. */
3648 while( current_capacity > 0 )
3649 {
3650 size_t read_size = sizeof( output_buffer );
3651 if( read_size > current_capacity )
3652 read_size = current_capacity;
3653 TEST_ASSERT( psa_generator_read( &generator,
3654 output_buffer,
3655 read_size ) == PSA_SUCCESS );
3656 expected_capacity -= read_size;
3657 TEST_ASSERT( psa_get_generator_capacity( &generator,
3658 &current_capacity ) ==
3659 PSA_SUCCESS );
3660 TEST_ASSERT( current_capacity == expected_capacity );
3661 }
3662
3663 /* Check that the generator refuses to go over capacity. */
3664 TEST_ASSERT( psa_generator_read( &generator,
3665 output_buffer,
3666 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3667
3668 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3669
3670exit:
3671 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003672 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003673 mbedtls_psa_crypto_free( );
3674}
3675/* END_CASE */
3676
3677/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003678void derive_key_exercise( int alg_arg,
3679 data_t *key_data,
3680 data_t *salt,
3681 data_t *label,
3682 int derived_type_arg,
3683 int derived_bits_arg,
3684 int derived_usage_arg,
3685 int derived_alg_arg )
3686{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003687 psa_key_handle_t base_handle = 0;
3688 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003689 psa_algorithm_t alg = alg_arg;
3690 psa_key_type_t derived_type = derived_type_arg;
3691 size_t derived_bits = derived_bits_arg;
3692 psa_key_usage_t derived_usage = derived_usage_arg;
3693 psa_algorithm_t derived_alg = derived_alg_arg;
3694 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3695 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3696 psa_key_policy_t policy;
3697 psa_key_type_t got_type;
3698 size_t got_bits;
3699
3700 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3701
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003702 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3703 PSA_BYTES_TO_BITS( key_data->len ),
3704 &base_handle ) == PSA_SUCCESS );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003705 psa_key_policy_init( &policy );
3706 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003707 TEST_ASSERT( psa_set_key_policy( base_handle, &policy ) == PSA_SUCCESS );
3708 TEST_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003709 key_data->x,
3710 key_data->len ) == PSA_SUCCESS );
3711
3712 /* Derive a key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003713 TEST_ASSERT( psa_key_derivation( &generator, base_handle, alg,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003714 salt->x, salt->len,
3715 label->x, label->len,
3716 capacity ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003717 TEST_ASSERT( psa_allocate_key( derived_type, derived_bits,
3718 &derived_handle ) == PSA_SUCCESS );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003719 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003720 TEST_ASSERT( psa_set_key_policy( derived_handle, &policy ) == PSA_SUCCESS );
3721 TEST_ASSERT( psa_generator_import_key( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003722 derived_type,
3723 derived_bits,
3724 &generator ) == PSA_SUCCESS );
3725
3726 /* Test the key information */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003727 TEST_ASSERT( psa_get_key_information( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003728 &got_type,
3729 &got_bits ) == PSA_SUCCESS );
3730 TEST_ASSERT( got_type == derived_type );
3731 TEST_ASSERT( got_bits == derived_bits );
3732
3733 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003734 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003735 goto exit;
3736
3737exit:
3738 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003739 psa_destroy_key( base_handle );
3740 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003741 mbedtls_psa_crypto_free( );
3742}
3743/* END_CASE */
3744
3745/* BEGIN_CASE */
3746void derive_key_export( int alg_arg,
3747 data_t *key_data,
3748 data_t *salt,
3749 data_t *label,
3750 int bytes1_arg,
3751 int bytes2_arg )
3752{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003753 psa_key_handle_t base_handle = 0;
3754 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003755 psa_algorithm_t alg = alg_arg;
3756 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003757 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003758 size_t bytes2 = bytes2_arg;
3759 size_t capacity = bytes1 + bytes2;
3760 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003761 uint8_t *output_buffer = NULL;
3762 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003763 psa_key_policy_t policy;
3764 size_t length;
3765
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003766 ASSERT_ALLOC( output_buffer, capacity );
3767 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003768 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3769
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003770 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3771 PSA_BYTES_TO_BITS( key_data->len ),
3772 &base_handle ) == PSA_SUCCESS );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003773 psa_key_policy_init( &policy );
3774 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003775 TEST_ASSERT( psa_set_key_policy( base_handle, &policy ) == PSA_SUCCESS );
3776 TEST_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003777 key_data->x,
3778 key_data->len ) == PSA_SUCCESS );
3779
3780 /* Derive some material and output it. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003781 TEST_ASSERT( psa_key_derivation( &generator, base_handle, alg,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003782 salt->x, salt->len,
3783 label->x, label->len,
3784 capacity ) == PSA_SUCCESS );
3785 TEST_ASSERT( psa_generator_read( &generator,
3786 output_buffer,
3787 capacity ) == PSA_SUCCESS );
3788 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3789
3790 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003791 TEST_ASSERT( psa_key_derivation( &generator, base_handle, alg,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003792 salt->x, salt->len,
3793 label->x, label->len,
3794 capacity ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003795 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3796 &derived_handle ) == PSA_SUCCESS );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003797 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003798 TEST_ASSERT( psa_set_key_policy( derived_handle, &policy ) == PSA_SUCCESS );
3799 TEST_ASSERT( psa_generator_import_key( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003800 PSA_KEY_TYPE_RAW_DATA,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003801 derived_bits,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003802 &generator ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003803 TEST_ASSERT( psa_export_key( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003804 export_buffer, bytes1,
3805 &length ) == PSA_SUCCESS );
3806 TEST_ASSERT( length == bytes1 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003807 TEST_ASSERT( psa_destroy_key( derived_handle ) == PSA_SUCCESS );
3808 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3809 PSA_BYTES_TO_BITS( bytes2 ),
3810 &derived_handle ) == PSA_SUCCESS );
3811 TEST_ASSERT( psa_set_key_policy( derived_handle, &policy ) == PSA_SUCCESS );
3812 TEST_ASSERT( psa_generator_import_key( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003813 PSA_KEY_TYPE_RAW_DATA,
3814 PSA_BYTES_TO_BITS( bytes2 ),
3815 &generator ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003816 TEST_ASSERT( psa_export_key( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003817 export_buffer + bytes1, bytes2,
3818 &length ) == PSA_SUCCESS );
3819 TEST_ASSERT( length == bytes2 );
3820
3821 /* Compare the outputs from the two runs. */
3822 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3823
3824exit:
3825 mbedtls_free( output_buffer );
3826 mbedtls_free( export_buffer );
3827 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003828 psa_destroy_key( base_handle );
3829 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003830 mbedtls_psa_crypto_free( );
3831}
3832/* END_CASE */
3833
3834/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003835void key_agreement_setup( int alg_arg,
3836 int our_key_type_arg, data_t *our_key_data,
3837 data_t *peer_key_data,
3838 int expected_status_arg )
3839{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003840 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003841 psa_algorithm_t alg = alg_arg;
3842 psa_key_type_t our_key_type = our_key_type_arg;
3843 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3844 psa_key_policy_t policy;
3845
3846 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3847
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003848 TEST_ASSERT( psa_allocate_key( our_key_type,
3849 KEY_BITS_FROM_DATA( our_key_type,
3850 our_key_data ),
3851 &our_key ) == PSA_SUCCESS );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003852 psa_key_policy_init( &policy );
3853 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3854 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3855 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3856 our_key_data->x,
3857 our_key_data->len ) == PSA_SUCCESS );
3858
3859 TEST_ASSERT( psa_key_agreement( &generator,
3860 our_key,
3861 peer_key_data->x, peer_key_data->len,
3862 alg ) == expected_status_arg );
3863
3864exit:
3865 psa_generator_abort( &generator );
3866 psa_destroy_key( our_key );
3867 mbedtls_psa_crypto_free( );
3868}
3869/* END_CASE */
3870
3871/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003872void key_agreement_capacity( int alg_arg,
3873 int our_key_type_arg, data_t *our_key_data,
3874 data_t *peer_key_data,
3875 int expected_capacity_arg )
3876{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003877 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003878 psa_algorithm_t alg = alg_arg;
3879 psa_key_type_t our_key_type = our_key_type_arg;
3880 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3881 psa_key_policy_t policy;
3882 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003883 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003884
3885 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3886
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003887 TEST_ASSERT( psa_allocate_key( our_key_type,
3888 KEY_BITS_FROM_DATA( our_key_type,
3889 our_key_data ),
3890 &our_key ) == PSA_SUCCESS );
Gilles Peskine59685592018-09-18 12:11:34 +02003891 psa_key_policy_init( &policy );
3892 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3893 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3894 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3895 our_key_data->x,
3896 our_key_data->len ) == PSA_SUCCESS );
3897
3898 TEST_ASSERT( psa_key_agreement( &generator,
3899 our_key,
3900 peer_key_data->x, peer_key_data->len,
3901 alg ) == PSA_SUCCESS );
3902
Gilles Peskinebf491972018-10-25 22:36:12 +02003903 /* Test the advertized capacity. */
Gilles Peskine59685592018-09-18 12:11:34 +02003904 TEST_ASSERT( psa_get_generator_capacity(
3905 &generator, &actual_capacity ) == PSA_SUCCESS );
3906 TEST_ASSERT( actual_capacity == (size_t) expected_capacity_arg );
3907
Gilles Peskinebf491972018-10-25 22:36:12 +02003908 /* Test the actual capacity by reading the output. */
3909 while( actual_capacity > sizeof( output ) )
3910 {
3911 TEST_ASSERT( psa_generator_read( &generator,
3912 output, sizeof( output ) ) ==
3913 PSA_SUCCESS );
3914 actual_capacity -= sizeof( output );
3915 }
3916 TEST_ASSERT( psa_generator_read( &generator,
3917 output, actual_capacity ) ==
3918 PSA_SUCCESS );
3919 TEST_ASSERT( psa_generator_read( &generator, output, 1 ) ==
3920 PSA_ERROR_INSUFFICIENT_CAPACITY );
3921
Gilles Peskine59685592018-09-18 12:11:34 +02003922exit:
3923 psa_generator_abort( &generator );
3924 psa_destroy_key( our_key );
3925 mbedtls_psa_crypto_free( );
3926}
3927/* END_CASE */
3928
3929/* BEGIN_CASE */
3930void key_agreement_output( int alg_arg,
3931 int our_key_type_arg, data_t *our_key_data,
3932 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003933 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003934{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003935 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003936 psa_algorithm_t alg = alg_arg;
3937 psa_key_type_t our_key_type = our_key_type_arg;
3938 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3939 psa_key_policy_t policy;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003940 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003941
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003942 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3943 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003944
3945 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3946
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003947 TEST_ASSERT( psa_allocate_key( our_key_type,
3948 KEY_BITS_FROM_DATA( our_key_type,
3949 our_key_data ),
3950 &our_key ) == PSA_SUCCESS );
Gilles Peskine59685592018-09-18 12:11:34 +02003951 psa_key_policy_init( &policy );
3952 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3953 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3954 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3955 our_key_data->x,
3956 our_key_data->len ) == PSA_SUCCESS );
3957
3958 TEST_ASSERT( psa_key_agreement( &generator,
3959 our_key,
3960 peer_key_data->x, peer_key_data->len,
3961 alg ) == PSA_SUCCESS );
3962
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003963 TEST_ASSERT(
3964 psa_generator_read( &generator,
3965 actual_output,
3966 expected_output1->len ) == PSA_SUCCESS );
3967 TEST_ASSERT( memcmp( actual_output, expected_output1->x,
3968 expected_output1->len ) == 0 );
3969 if( expected_output2->len != 0 )
3970 {
3971 TEST_ASSERT(
3972 psa_generator_read( &generator,
3973 actual_output,
3974 expected_output2->len ) == PSA_SUCCESS );
3975 TEST_ASSERT( memcmp( actual_output, expected_output2->x,
3976 expected_output2->len ) == 0 );
3977 }
Gilles Peskine59685592018-09-18 12:11:34 +02003978
3979exit:
3980 psa_generator_abort( &generator );
3981 psa_destroy_key( our_key );
3982 mbedtls_psa_crypto_free( );
3983 mbedtls_free( actual_output );
3984}
3985/* END_CASE */
3986
3987/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003988void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003989{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003990 size_t bytes = bytes_arg;
3991 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003992 unsigned char *output = NULL;
3993 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003994 size_t i;
3995 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003996
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003997 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3998 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003999 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004000
4001 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
4002
Gilles Peskinea50d7392018-06-21 10:22:13 +02004003 /* Run several times, to ensure that every output byte will be
4004 * nonzero at least once with overwhelming probability
4005 * (2^(-8*number_of_runs)). */
4006 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004007 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004008 if( bytes != 0 )
4009 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004010 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
4011
4012 /* Check that no more than bytes have been overwritten */
4013 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
4014
4015 for( i = 0; i < bytes; i++ )
4016 {
4017 if( output[i] != 0 )
4018 ++changed[i];
4019 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004020 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004021
4022 /* Check that every byte was changed to nonzero at least once. This
4023 * validates that psa_generate_random is overwriting every byte of
4024 * the output buffer. */
4025 for( i = 0; i < bytes; i++ )
4026 {
4027 TEST_ASSERT( changed[i] != 0 );
4028 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004029
4030exit:
4031 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004032 mbedtls_free( output );
4033 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004034}
4035/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004036
4037/* BEGIN_CASE */
4038void generate_key( int type_arg,
4039 int bits_arg,
4040 int usage_arg,
4041 int alg_arg,
4042 int expected_status_arg )
4043{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004044 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004045 psa_key_type_t type = type_arg;
4046 psa_key_usage_t usage = usage_arg;
4047 size_t bits = bits_arg;
4048 psa_algorithm_t alg = alg_arg;
4049 psa_status_t expected_status = expected_status_arg;
4050 psa_key_type_t got_type;
4051 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004052 psa_status_t expected_info_status =
4053 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
4054 psa_key_policy_t policy;
4055
4056 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
4057
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004058 TEST_ASSERT( psa_allocate_key( type, bits, &handle ) == PSA_SUCCESS );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004059 psa_key_policy_init( &policy );
4060 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004061 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004062
4063 /* Generate a key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004064 TEST_ASSERT( psa_generate_key( handle, type, bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004065 NULL, 0 ) == expected_status );
4066
4067 /* Test the key information */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004068 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004069 &got_type,
4070 &got_bits ) == expected_info_status );
4071 if( expected_info_status != PSA_SUCCESS )
4072 goto exit;
4073 TEST_ASSERT( got_type == type );
4074 TEST_ASSERT( got_bits == bits );
4075
Gilles Peskine818ca122018-06-20 18:16:48 +02004076 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004077 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004078 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004079
4080exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004081 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004082 mbedtls_psa_crypto_free( );
4083}
4084/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004085
Darryl Greend49a4992018-06-18 17:27:26 +01004086/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4087void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4088 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004089 int alg_arg, int generation_method,
4090 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004091{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004092 psa_key_handle_t handle = 0;
4093 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004094 psa_key_type_t type = (psa_key_type_t) type_arg;
4095 psa_key_type_t type_get;
4096 size_t bits_get;
4097 psa_key_policy_t policy_set;
4098 psa_key_policy_t policy_get;
4099 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4100 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004101 psa_key_policy_t base_policy_set;
4102 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4103 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004104 unsigned char *first_export = NULL;
4105 unsigned char *second_export = NULL;
4106 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4107 size_t first_exported_length;
4108 size_t second_exported_length;
4109
4110 ASSERT_ALLOC( first_export, export_size );
4111 ASSERT_ALLOC( second_export, export_size );
4112
4113 TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS );
4114
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004115 TEST_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4116 type, bits,
4117 &handle ) == PSA_SUCCESS );
Darryl Greend49a4992018-06-18 17:27:26 +01004118 psa_key_policy_init( &policy_set );
Darryl Greend49a4992018-06-18 17:27:26 +01004119 psa_key_policy_set_usage( &policy_set, policy_usage,
4120 policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004121 TEST_ASSERT( psa_set_key_policy( handle, &policy_set ) == PSA_SUCCESS );
Darryl Greend49a4992018-06-18 17:27:26 +01004122
Darryl Green0c6575a2018-11-07 16:05:30 +00004123 switch( generation_method )
4124 {
4125 case IMPORT_KEY:
4126 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004127 TEST_ASSERT( psa_import_key( handle, type,
Darryl Green0c6575a2018-11-07 16:05:30 +00004128 data->x, data->len ) == PSA_SUCCESS );
4129 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004130
Darryl Green0c6575a2018-11-07 16:05:30 +00004131 case GENERATE_KEY:
4132 /* Generate a key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004133 TEST_ASSERT( psa_generate_key( handle, type, bits,
Darryl Green0c6575a2018-11-07 16:05:30 +00004134 NULL, 0 ) == PSA_SUCCESS );
4135 break;
4136
4137 case DERIVE_KEY:
4138 /* Create base key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004139 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
4140 PSA_BYTES_TO_BITS( data->len ),
4141 &base_key ) == PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00004142 psa_key_policy_init( &base_policy_set );
Darryl Green0c6575a2018-11-07 16:05:30 +00004143 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4144 base_policy_alg );
4145 TEST_ASSERT( psa_set_key_policy(
4146 base_key, &base_policy_set ) == PSA_SUCCESS );
4147 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4148 data->x, data->len ) == PSA_SUCCESS );
4149 /* Derive a key. */
4150 TEST_ASSERT( psa_key_derivation( &generator, base_key,
4151 base_policy_alg,
4152 NULL, 0, NULL, 0,
4153 export_size ) == PSA_SUCCESS );
4154 TEST_ASSERT( psa_generator_import_key(
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004155 handle, PSA_KEY_TYPE_RAW_DATA,
Darryl Green0c6575a2018-11-07 16:05:30 +00004156 bits, &generator ) == PSA_SUCCESS );
4157 break;
4158 }
Darryl Greend49a4992018-06-18 17:27:26 +01004159
4160 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004161 TEST_ASSERT( psa_export_key( handle, first_export, export_size,
Darryl Green0c6575a2018-11-07 16:05:30 +00004162 &first_exported_length ) == export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004163
4164 /* Shutdown and restart */
4165 mbedtls_psa_crypto_free();
Darryl Greend49a4992018-06-18 17:27:26 +01004166 TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS );
4167
Darryl Greend49a4992018-06-18 17:27:26 +01004168 /* Check key slot still contains key data */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004169 TEST_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4170 &handle ) == PSA_SUCCESS );
Darryl Greend49a4992018-06-18 17:27:26 +01004171 TEST_ASSERT( psa_get_key_information(
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004172 handle, &type_get, &bits_get ) == PSA_SUCCESS );
Darryl Greend49a4992018-06-18 17:27:26 +01004173 TEST_ASSERT( type_get == type );
4174 TEST_ASSERT( bits_get == (size_t) bits );
4175
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004176 TEST_ASSERT( psa_get_key_policy( handle, &policy_get ) == PSA_SUCCESS );
Darryl Greend49a4992018-06-18 17:27:26 +01004177 TEST_ASSERT( psa_key_policy_get_usage(
4178 &policy_get ) == policy_usage );
4179 TEST_ASSERT( psa_key_policy_get_algorithm(
4180 &policy_get ) == policy_alg );
4181
4182 /* Export the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004183 TEST_ASSERT( psa_export_key( handle, second_export, export_size,
Darryl Green0c6575a2018-11-07 16:05:30 +00004184 &second_exported_length ) == export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004185
Darryl Green0c6575a2018-11-07 16:05:30 +00004186 if( export_status == PSA_SUCCESS )
4187 {
4188 ASSERT_COMPARE( first_export, first_exported_length,
4189 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004190
Darryl Green0c6575a2018-11-07 16:05:30 +00004191 switch( generation_method )
4192 {
4193 case IMPORT_KEY:
4194 ASSERT_COMPARE( data->x, data->len,
4195 first_export, first_exported_length );
4196 break;
4197 default:
4198 break;
4199 }
4200 }
4201
4202 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004203 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004204 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004205
4206exit:
4207 mbedtls_free( first_export );
4208 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004209 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004210 mbedtls_psa_crypto_free();
4211}
4212/* END_CASE */