blob: c40ac5f7de4691187be55f0da408e540c296920c [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskinefc411f12018-10-25 22:34:48 +020014#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
15
Gilles Peskine96ee5c72018-07-12 17:24:54 +020016#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
17
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#else
Gilles Peskine2d277862018-06-18 15:41:12 +020021#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030022#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023
Jaeden Amerof24c7f82018-06-27 17:20:43 +010024/** An invalid export length that will never be set by psa_export_key(). */
25static const size_t INVALID_EXPORT_LENGTH = ~0U;
26
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027/** Test if a buffer contains a constant byte value.
28 *
29 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020030 *
31 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020032 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 * \param size Size of the buffer in bytes.
34 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020035 * \return 1 if the buffer is all-bits-zero.
36 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039{
40 size_t i;
41 for( i = 0; i < size; i++ )
42 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020043 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020046 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020047}
Gilles Peskine818ca122018-06-20 18:16:48 +020048
Gilles Peskine0b352bc2018-06-28 00:16:11 +020049/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
50static int asn1_write_10x( unsigned char **p,
51 unsigned char *start,
52 size_t bits,
53 unsigned char x )
54{
55 int ret;
56 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020057 if( bits == 0 )
58 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
59 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030061 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020062 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
63 *p -= len;
64 ( *p )[len-1] = x;
65 if( bits % 8 == 0 )
66 ( *p )[1] |= 1;
67 else
68 ( *p )[0] |= 1 << ( bits % 8 );
69 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
70 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
71 MBEDTLS_ASN1_INTEGER ) );
72 return( len );
73}
74
75static int construct_fake_rsa_key( unsigned char *buffer,
76 size_t buffer_size,
77 unsigned char **p,
78 size_t bits,
79 int keypair )
80{
81 size_t half_bits = ( bits + 1 ) / 2;
82 int ret;
83 int len = 0;
84 /* Construct something that looks like a DER encoding of
85 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
86 * RSAPrivateKey ::= SEQUENCE {
87 * version Version,
88 * modulus INTEGER, -- n
89 * publicExponent INTEGER, -- e
90 * privateExponent INTEGER, -- d
91 * prime1 INTEGER, -- p
92 * prime2 INTEGER, -- q
93 * exponent1 INTEGER, -- d mod (p-1)
94 * exponent2 INTEGER, -- d mod (q-1)
95 * coefficient INTEGER, -- (inverse of q) mod p
96 * otherPrimeInfos OtherPrimeInfos OPTIONAL
97 * }
98 * Or, for a public key, the same structure with only
99 * version, modulus and publicExponent.
100 */
101 *p = buffer + buffer_size;
102 if( keypair )
103 {
104 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* q */
111 asn1_write_10x( p, buffer, half_bits, 1 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
113 asn1_write_10x( p, buffer, half_bits, 3 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* d */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 }
117 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
118 asn1_write_10x( p, buffer, 17, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* n */
120 asn1_write_10x( p, buffer, bits, 1 ) );
121 if( keypair )
122 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
123 mbedtls_asn1_write_int( p, buffer, 0 ) );
124 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
125 {
126 const unsigned char tag =
127 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
128 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
129 }
130 return( len );
131}
132
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100133static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200134 psa_key_usage_t usage,
135 psa_algorithm_t alg )
136{
137 psa_mac_operation_t operation;
138 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200139 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200140 size_t mac_length = sizeof( mac );
141
142 if( usage & PSA_KEY_USAGE_SIGN )
143 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200144 TEST_ASSERT( psa_mac_sign_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100145 handle, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200146 TEST_ASSERT( psa_mac_update( &operation,
147 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200148 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200149 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200150 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200151 }
152
153 if( usage & PSA_KEY_USAGE_VERIFY )
154 {
155 psa_status_t verify_status =
156 ( usage & PSA_KEY_USAGE_SIGN ?
157 PSA_SUCCESS :
158 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200159 TEST_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100160 handle, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200161 TEST_ASSERT( psa_mac_update( &operation,
162 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200163 TEST_ASSERT( psa_mac_verify_finish( &operation,
164 mac,
165 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200166 }
167
168 return( 1 );
169
170exit:
171 psa_mac_abort( &operation );
172 return( 0 );
173}
174
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100175static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200176 psa_key_usage_t usage,
177 psa_algorithm_t alg )
178{
179 psa_cipher_operation_t operation;
180 unsigned char iv[16] = {0};
181 size_t iv_length = sizeof( iv );
182 const unsigned char plaintext[16] = "Hello, world...";
183 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
184 size_t ciphertext_length = sizeof( ciphertext );
185 unsigned char decrypted[sizeof( ciphertext )];
186 size_t part_length;
187
188 if( usage & PSA_KEY_USAGE_ENCRYPT )
189 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200190 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100191 handle, alg ) == PSA_SUCCESS );
Gilles Peskinefe119512018-07-08 21:39:34 +0200192 TEST_ASSERT( psa_cipher_generate_iv( &operation,
193 iv, sizeof( iv ),
194 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200195 TEST_ASSERT( psa_cipher_update( &operation,
196 plaintext, sizeof( plaintext ),
197 ciphertext, sizeof( ciphertext ),
198 &ciphertext_length ) == PSA_SUCCESS );
199 TEST_ASSERT( psa_cipher_finish( &operation,
200 ciphertext + ciphertext_length,
201 sizeof( ciphertext ) - ciphertext_length,
202 &part_length ) == PSA_SUCCESS );
203 ciphertext_length += part_length;
204 }
205
206 if( usage & PSA_KEY_USAGE_DECRYPT )
207 {
208 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700209 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200210 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
211 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200212 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100213 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200214 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
215 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200216 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100217 handle, alg ) == PSA_SUCCESS );
Gilles Peskinefe119512018-07-08 21:39:34 +0200218 TEST_ASSERT( psa_cipher_set_iv( &operation,
219 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200220 TEST_ASSERT( psa_cipher_update( &operation,
221 ciphertext, ciphertext_length,
222 decrypted, sizeof( decrypted ),
223 &part_length ) == PSA_SUCCESS );
224 status = psa_cipher_finish( &operation,
225 decrypted + part_length,
226 sizeof( decrypted ) - part_length,
227 &part_length );
228 /* For a stream cipher, all inputs are valid. For a block cipher,
229 * if the input is some aribtrary data rather than an actual
230 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700231 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700232 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200233 TEST_ASSERT( status == PSA_SUCCESS );
234 else
235 TEST_ASSERT( status == PSA_SUCCESS ||
236 status == PSA_ERROR_INVALID_PADDING );
237 }
238
239 return( 1 );
240
241exit:
242 psa_cipher_abort( &operation );
243 return( 0 );
244}
245
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100246static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200247 psa_key_usage_t usage,
248 psa_algorithm_t alg )
249{
250 unsigned char nonce[16] = {0};
251 size_t nonce_length = sizeof( nonce );
252 unsigned char plaintext[16] = "Hello, world...";
253 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
254 size_t ciphertext_length = sizeof( ciphertext );
255 size_t plaintext_length = sizeof( ciphertext );
256
257 if( usage & PSA_KEY_USAGE_ENCRYPT )
258 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100259 TEST_ASSERT( psa_aead_encrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200260 nonce, nonce_length,
261 NULL, 0,
262 plaintext, sizeof( plaintext ),
263 ciphertext, sizeof( ciphertext ),
264 &ciphertext_length ) == PSA_SUCCESS );
265 }
266
267 if( usage & PSA_KEY_USAGE_DECRYPT )
268 {
269 psa_status_t verify_status =
270 ( usage & PSA_KEY_USAGE_ENCRYPT ?
271 PSA_SUCCESS :
272 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100273 TEST_ASSERT( psa_aead_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200274 nonce, nonce_length,
275 NULL, 0,
276 ciphertext, ciphertext_length,
277 plaintext, sizeof( plaintext ),
278 &plaintext_length ) == verify_status );
279 }
280
281 return( 1 );
282
283exit:
284 return( 0 );
285}
286
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100287static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200288 psa_key_usage_t usage,
289 psa_algorithm_t alg )
290{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200291 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
292 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200293 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200294 size_t signature_length = sizeof( signature );
295
296 if( usage & PSA_KEY_USAGE_SIGN )
297 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200298 /* Some algorithms require the payload to have the size of
299 * the hash encoded in the algorithm. Use this input size
300 * even for algorithms that allow other input sizes. */
301 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
302 if( hash_alg != 0 )
303 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100304 TEST_ASSERT( psa_asymmetric_sign( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200305 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 signature, sizeof( signature ),
307 &signature_length ) == PSA_SUCCESS );
308 }
309
310 if( usage & PSA_KEY_USAGE_VERIFY )
311 {
312 psa_status_t verify_status =
313 ( usage & PSA_KEY_USAGE_SIGN ?
314 PSA_SUCCESS :
315 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100316 TEST_ASSERT( psa_asymmetric_verify( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200317 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200318 signature, signature_length ) ==
319 verify_status );
320 }
321
322 return( 1 );
323
324exit:
325 return( 0 );
326}
327
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100328static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200329 psa_key_usage_t usage,
330 psa_algorithm_t alg )
331{
332 unsigned char plaintext[256] = "Hello, world...";
333 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
334 size_t ciphertext_length = sizeof( ciphertext );
335 size_t plaintext_length = 16;
336
337 if( usage & PSA_KEY_USAGE_ENCRYPT )
338 {
339 TEST_ASSERT(
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100340 psa_asymmetric_encrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200341 plaintext, plaintext_length,
342 NULL, 0,
343 ciphertext, sizeof( ciphertext ),
344 &ciphertext_length ) == PSA_SUCCESS );
345 }
346
347 if( usage & PSA_KEY_USAGE_DECRYPT )
348 {
349 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100350 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200351 ciphertext, ciphertext_length,
352 NULL, 0,
353 plaintext, sizeof( plaintext ),
354 &plaintext_length );
355 TEST_ASSERT( status == PSA_SUCCESS ||
356 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
357 ( status == PSA_ERROR_INVALID_ARGUMENT ||
358 status == PSA_ERROR_INVALID_PADDING ) ) );
359 }
360
361 return( 1 );
362
363exit:
364 return( 0 );
365}
Gilles Peskine02b75072018-07-01 22:31:34 +0200366
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100367static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200368 psa_key_usage_t usage,
369 psa_algorithm_t alg )
370{
371 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
372 unsigned char label[16] = "This is a label.";
373 size_t label_length = sizeof( label );
374 unsigned char seed[16] = "abcdefghijklmnop";
375 size_t seed_length = sizeof( seed );
376 unsigned char output[1];
377
378 if( usage & PSA_KEY_USAGE_DERIVE )
379 {
380 TEST_ASSERT( psa_key_derivation( &generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100381 handle, alg,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200382 label, label_length,
383 seed, seed_length,
384 sizeof( output ) ) == PSA_SUCCESS );
385 TEST_ASSERT( psa_generator_read( &generator,
386 output,
387 sizeof( output ) ) == PSA_SUCCESS );
388 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
389 }
390
391 return( 1 );
392
393exit:
394 return( 0 );
395}
396
Gilles Peskinec7998b72018-11-07 18:45:02 +0100397/* We need two keys to exercise key agreement. Exercise the
398 * private key against its own public key. */
399static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100400 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100401 psa_algorithm_t alg )
402{
403 psa_key_type_t private_key_type;
404 psa_key_type_t public_key_type;
405 size_t key_bits;
406 uint8_t *public_key = NULL;
407 size_t public_key_length;
408 /* Return UNKNOWN_ERROR if something other than the final call to
409 * psa_key_agreement fails. This isn't fully satisfactory, but it's
410 * good enough: callers will report it as a failed test anyway. */
411 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
412
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100413 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100414 &private_key_type,
415 &key_bits ) == PSA_SUCCESS );
416 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
417 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
418 ASSERT_ALLOC( public_key, public_key_length );
419 TEST_ASSERT( public_key != NULL );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100420 TEST_ASSERT( psa_export_public_key( handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100421 public_key, public_key_length,
422 &public_key_length ) == PSA_SUCCESS );
423
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100424 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100425 public_key, public_key_length,
426 alg );
427exit:
428 mbedtls_free( public_key );
429 return( status );
430}
431
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100432static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200433 psa_key_usage_t usage,
434 psa_algorithm_t alg )
435{
436 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200437 unsigned char output[1];
438 int ok = 0;
439
440 if( usage & PSA_KEY_USAGE_DERIVE )
441 {
442 /* We need two keys to exercise key agreement. Exercise the
443 * private key against its own public key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100444 TEST_ASSERT( key_agreement_with_self( &generator, handle, alg ) ==
Gilles Peskinec7998b72018-11-07 18:45:02 +0100445 PSA_SUCCESS );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200446 TEST_ASSERT( psa_generator_read( &generator,
447 output,
448 sizeof( output ) ) == PSA_SUCCESS );
449 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
450 }
451 ok = 1;
452
453exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200454 return( ok );
455}
456
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200457static int is_oid_of_key_type( psa_key_type_t type,
458 const uint8_t *oid, size_t oid_length )
459{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200460 const uint8_t *expected_oid = NULL;
461 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200462#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200463 if( PSA_KEY_TYPE_IS_RSA( type ) )
464 {
465 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
466 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
467 }
468 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200469#endif /* MBEDTLS_RSA_C */
470#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200471 if( PSA_KEY_TYPE_IS_ECC( type ) )
472 {
473 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
474 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
475 }
476 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200477#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200478 {
479 char message[40];
480 mbedtls_snprintf( message, sizeof( message ),
481 "OID not known for key type=0x%08lx",
482 (unsigned long) type );
483 test_fail( message, __LINE__, __FILE__ );
484 return( 0 );
485 }
486
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200487 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200488 return( 1 );
489
490exit:
491 return( 0 );
492}
493
494static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
495 size_t min_bits, size_t max_bits,
496 int must_be_odd )
497{
498 size_t len;
499 size_t actual_bits;
500 unsigned char msb;
501 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
502 MBEDTLS_ASN1_INTEGER ) == 0 );
503 /* Tolerate a slight departure from DER encoding:
504 * - 0 may be represented by an empty string or a 1-byte string.
505 * - The sign bit may be used as a value bit. */
506 if( ( len == 1 && ( *p )[0] == 0 ) ||
507 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
508 {
509 ++( *p );
510 --len;
511 }
512 if( min_bits == 0 && len == 0 )
513 return( 1 );
514 msb = ( *p )[0];
515 TEST_ASSERT( msb != 0 );
516 actual_bits = 8 * ( len - 1 );
517 while( msb != 0 )
518 {
519 msb >>= 1;
520 ++actual_bits;
521 }
522 TEST_ASSERT( actual_bits >= min_bits );
523 TEST_ASSERT( actual_bits <= max_bits );
524 if( must_be_odd )
525 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
526 *p += len;
527 return( 1 );
528exit:
529 return( 0 );
530}
531
532static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
533 size_t *len,
534 unsigned char n, unsigned char tag )
535{
536 int ret;
537 ret = mbedtls_asn1_get_tag( p, end, len,
538 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
539 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
540 if( ret != 0 )
541 return( ret );
542 end = *p + *len;
543 ret = mbedtls_asn1_get_tag( p, end, len, tag );
544 if( ret != 0 )
545 return( ret );
546 if( *p + *len != end )
547 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
548 return( 0 );
549}
550
551static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
552 uint8_t *exported, size_t exported_length )
553{
554 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200555 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200556 else
557 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200558
559#if defined(MBEDTLS_DES_C)
560 if( type == PSA_KEY_TYPE_DES )
561 {
562 /* Check the parity bits. */
563 unsigned i;
564 for( i = 0; i < bits / 8; i++ )
565 {
566 unsigned bit_count = 0;
567 unsigned m;
568 for( m = 1; m <= 0x100; m <<= 1 )
569 {
570 if( exported[i] & m )
571 ++bit_count;
572 }
573 TEST_ASSERT( bit_count % 2 != 0 );
574 }
575 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200576 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200577#endif
578
579#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
580 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
581 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200582 uint8_t *p = exported;
583 uint8_t *end = exported + exported_length;
584 size_t len;
585 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200586 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200587 * modulus INTEGER, -- n
588 * publicExponent INTEGER, -- e
589 * privateExponent INTEGER, -- d
590 * prime1 INTEGER, -- p
591 * prime2 INTEGER, -- q
592 * exponent1 INTEGER, -- d mod (p-1)
593 * exponent2 INTEGER, -- d mod (q-1)
594 * coefficient INTEGER, -- (inverse of q) mod p
595 * }
596 */
597 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
598 MBEDTLS_ASN1_SEQUENCE |
599 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
600 TEST_ASSERT( p + len == end );
601 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
604 goto exit;
605 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
606 goto exit;
607 /* Require d to be at least half the size of n. */
608 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
609 goto exit;
610 /* Require p and q to be at most half the size of n, rounded up. */
611 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
612 goto exit;
613 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
614 goto exit;
615 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
616 goto exit;
617 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
618 goto exit;
619 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
620 goto exit;
621 TEST_ASSERT( p == end );
622 }
623 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200624#endif /* MBEDTLS_RSA_C */
625
626#if defined(MBEDTLS_ECP_C)
627 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
628 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100629 /* Just the secret value */
630 TEST_ASSERT( exported_length == PSA_BITS_TO_BYTES( bits ) );
631 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200632 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200633#endif /* MBEDTLS_ECP_C */
634
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200635 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
636 {
637 uint8_t *p = exported;
638 uint8_t *end = exported + exported_length;
639 size_t len;
640 mbedtls_asn1_buf alg;
641 mbedtls_asn1_buf params;
642 mbedtls_asn1_bitstring bitstring;
643 /* SubjectPublicKeyInfo ::= SEQUENCE {
644 * algorithm AlgorithmIdentifier,
645 * subjectPublicKey BIT STRING }
646 * AlgorithmIdentifier ::= SEQUENCE {
647 * algorithm OBJECT IDENTIFIER,
648 * parameters ANY DEFINED BY algorithm OPTIONAL }
649 */
650 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
651 MBEDTLS_ASN1_SEQUENCE |
652 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
653 TEST_ASSERT( p + len == end );
654 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
655 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
656 goto exit;
657 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
658 TEST_ASSERT( p == end );
659 p = bitstring.p;
660#if defined(MBEDTLS_RSA_C)
661 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
662 {
663 /* RSAPublicKey ::= SEQUENCE {
664 * modulus INTEGER, -- n
665 * publicExponent INTEGER } -- e
666 */
667 TEST_ASSERT( bitstring.unused_bits == 0 );
668 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
669 MBEDTLS_ASN1_SEQUENCE |
670 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
671 TEST_ASSERT( p + len == end );
672 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
673 goto exit;
674 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
675 goto exit;
676 TEST_ASSERT( p == end );
677 }
678 else
679#endif /* MBEDTLS_RSA_C */
680#if defined(MBEDTLS_ECP_C)
681 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
682 {
683 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200684 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200685 * -- then x_P as an n-bit string, big endian;
686 * -- then y_P as a n-bit string, big endian,
687 * -- where n is the order of the curve.
688 */
689 TEST_ASSERT( bitstring.unused_bits == 0 );
690 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
691 TEST_ASSERT( p[0] == 4 );
692 }
693 else
694#endif /* MBEDTLS_ECP_C */
695 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100696 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200697 mbedtls_snprintf( message, sizeof( message ),
698 "No sanity check for public key type=0x%08lx",
699 (unsigned long) type );
700 test_fail( message, __LINE__, __FILE__ );
701 return( 0 );
702 }
703 }
704 else
705
706 {
707 /* No sanity checks for other types */
708 }
709
710 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200711
712exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200713 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200714}
715
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100716static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200717 psa_key_usage_t usage )
718{
719 psa_key_type_t type;
720 size_t bits;
721 uint8_t *exported = NULL;
722 size_t exported_size = 0;
723 size_t exported_length = 0;
724 int ok = 0;
725
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100726 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) == PSA_SUCCESS );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200727
728 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
729 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200730 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100731 TEST_ASSERT( psa_export_key( handle, NULL, 0, &exported_length ) ==
Gilles Peskined14664a2018-08-10 19:07:32 +0200732 PSA_ERROR_NOT_PERMITTED );
733 return( 1 );
734 }
735
Gilles Peskined14664a2018-08-10 19:07:32 +0200736 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200737 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200738
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100739 TEST_ASSERT( psa_export_key( handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200740 exported, exported_size,
741 &exported_length ) == PSA_SUCCESS );
742 ok = exported_key_sanity_check( type, bits, exported, exported_length );
743
744exit:
745 mbedtls_free( exported );
746 return( ok );
747}
748
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100749static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200750{
751 psa_key_type_t type;
752 psa_key_type_t public_type;
753 size_t bits;
754 uint8_t *exported = NULL;
755 size_t exported_size = 0;
756 size_t exported_length = 0;
757 int ok = 0;
758
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100759 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) == PSA_SUCCESS );
Gilles Peskined14664a2018-08-10 19:07:32 +0200760 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
761 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100762 TEST_ASSERT( psa_export_public_key( handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200763 NULL, 0, &exported_length ) ==
764 PSA_ERROR_INVALID_ARGUMENT );
765 return( 1 );
766 }
767
768 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
769 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200770 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200771
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100772 TEST_ASSERT( psa_export_public_key( handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200773 exported, exported_size,
774 &exported_length ) == PSA_SUCCESS );
775 ok = exported_key_sanity_check( public_type, bits,
776 exported, exported_length );
777
778exit:
779 mbedtls_free( exported );
780 return( ok );
781}
782
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100783static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200784 psa_key_usage_t usage,
785 psa_algorithm_t alg )
786{
787 int ok;
788 if( alg == 0 )
789 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
790 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100791 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200792 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100793 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200794 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100795 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200796 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100797 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200798 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100799 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200800 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100801 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200802 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100803 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200804 else
805 {
806 char message[40];
807 mbedtls_snprintf( message, sizeof( message ),
808 "No code to exercise alg=0x%08lx",
809 (unsigned long) alg );
810 test_fail( message, __LINE__, __FILE__ );
811 ok = 0;
812 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200813
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100814 ok = ok && exercise_export_key( handle, usage );
815 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200816
Gilles Peskine02b75072018-07-01 22:31:34 +0200817 return( ok );
818}
819
Gilles Peskine10df3412018-10-25 22:35:43 +0200820static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
821 psa_algorithm_t alg )
822{
823 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
824 {
825 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
826 PSA_KEY_USAGE_VERIFY :
827 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
828 }
829 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
830 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
831 {
832 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
833 PSA_KEY_USAGE_ENCRYPT :
834 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
835 }
836 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
837 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
838 {
839 return( PSA_KEY_USAGE_DERIVE );
840 }
841 else
842 {
843 return( 0 );
844 }
845
846}
Darryl Green0c6575a2018-11-07 16:05:30 +0000847
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100848/* An overapproximation of the amount of storage needed for a key of the
849 * given type and with the given content. The API doesn't make it easy
850 * to find a good value for the size. The current implementation doesn't
851 * care about the value anyway. */
852#define KEY_BITS_FROM_DATA( type, data ) \
853 ( data )->len
854
Darryl Green0c6575a2018-11-07 16:05:30 +0000855typedef enum {
856 IMPORT_KEY = 0,
857 GENERATE_KEY = 1,
858 DERIVE_KEY = 2
859} generate_method;
860
Gilles Peskinee59236f2018-01-27 23:32:46 +0100861/* END_HEADER */
862
863/* BEGIN_DEPENDENCIES
864 * depends_on:MBEDTLS_PSA_CRYPTO_C
865 * END_DEPENDENCIES
866 */
867
868/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200869void static_checks( )
870{
871 size_t max_truncated_mac_size =
872 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
873
874 /* Check that the length for a truncated MAC always fits in the algorithm
875 * encoding. The shifted mask is the maximum truncated value. The
876 * untruncated algorithm may be one byte larger. */
877 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
878}
879/* END_CASE */
880
881/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200882void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100884 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200885 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300889 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
891
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100892 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
893 &handle ) == PSA_SUCCESS );
894 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200895 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896 if( status == PSA_SUCCESS )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100897 TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100898
899exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100900 mbedtls_psa_crypto_free( );
901}
902/* END_CASE */
903
904/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100905void import_twice( int alg_arg, int usage_arg,
906 int type1_arg, data_t *data1,
907 int expected_import1_status_arg,
908 int type2_arg, data_t *data2,
909 int expected_import2_status_arg )
910{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100911 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100912 psa_algorithm_t alg = alg_arg;
913 psa_key_usage_t usage = usage_arg;
914 psa_key_type_t type1 = type1_arg;
915 psa_status_t expected_import1_status = expected_import1_status_arg;
916 psa_key_type_t type2 = type2_arg;
917 psa_status_t expected_import2_status = expected_import2_status_arg;
918 psa_key_policy_t policy;
919 psa_status_t status;
920
921 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
922
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100923 TEST_ASSERT( psa_allocate_key( type1,
924 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
925 KEY_BITS_FROM_DATA( type2, data2 ) ),
926 &handle ) == PSA_SUCCESS );
Gilles Peskinea4261682018-12-03 11:34:01 +0100927 psa_key_policy_init( &policy );
928 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100929 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea4261682018-12-03 11:34:01 +0100930
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100931 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinea4261682018-12-03 11:34:01 +0100932 TEST_ASSERT( status == expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100933 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinea4261682018-12-03 11:34:01 +0100934 TEST_ASSERT( status == expected_import2_status );
935
936 if( expected_import1_status == PSA_SUCCESS ||
937 expected_import2_status == PSA_SUCCESS )
938 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100939 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100940 }
941
942exit:
943 mbedtls_psa_crypto_free( );
944}
945/* END_CASE */
946
947/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200948void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
949{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100950 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200951 size_t bits = bits_arg;
952 psa_status_t expected_status = expected_status_arg;
953 psa_status_t status;
954 psa_key_type_t type =
955 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
956 size_t buffer_size = /* Slight overapproximations */
957 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200958 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200959 unsigned char *p;
960 int ret;
961 size_t length;
962
963 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200964 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200965
966 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
967 bits, keypair ) ) >= 0 );
968 length = ret;
969
970 /* Try importing the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100971 TEST_ASSERT( psa_allocate_key( type, bits, &handle ) == PSA_SUCCESS );
972 status = psa_import_key( handle, type, p, length );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200973 TEST_ASSERT( status == expected_status );
974 if( status == PSA_SUCCESS )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100975 TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200976
977exit:
978 mbedtls_free( buffer );
979 mbedtls_psa_crypto_free( );
980}
981/* END_CASE */
982
983/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300984void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300985 int type_arg,
986 int alg_arg,
987 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 int expected_bits,
989 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200990 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991 int canonical_input )
992{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100993 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200995 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200996 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 unsigned char *exported = NULL;
999 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001001 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 size_t reexported_length;
1003 psa_key_type_t got_type;
1004 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001005 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001006
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001008 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001009 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001010 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001012 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001013 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1014
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001015 TEST_ASSERT( psa_allocate_key( type, expected_bits, &handle ) ==
1016 PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001017 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001018 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001019 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
1020
1021 TEST_ASSERT( psa_get_key_information(
1022 handle, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001023
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001025 TEST_ASSERT( psa_import_key( handle, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001026 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027
1028 /* Test the key information */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001029 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001030 &got_type,
1031 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001032 TEST_ASSERT( got_type == type );
1033 TEST_ASSERT( got_bits == (size_t) expected_bits );
1034
1035 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001036 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001037 exported, export_size,
1038 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001039 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001040
1041 /* The exported length must be set by psa_export_key() to a value between 0
1042 * and export_size. On errors, the exported length must be 0. */
1043 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1044 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1045 TEST_ASSERT( exported_length <= export_size );
1046
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001047 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001048 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001050 {
1051 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001052 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001053 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001054
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001055 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001056 goto exit;
1057
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001059 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060 else
1061 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001062 psa_key_handle_t handle2;
1063 TEST_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) ==
1064 PSA_SUCCESS );
1065 TEST_ASSERT( psa_set_key_policy( handle2, &policy ) == PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001066
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001067 TEST_ASSERT( psa_import_key( handle2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001068 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001069 exported_length ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001070 TEST_ASSERT( psa_export_key( handle2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001071 reexported,
1072 export_size,
1073 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001074 ASSERT_COMPARE( exported, exported_length,
1075 reexported, reexported_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001076 TEST_ASSERT( psa_close_key( handle2 ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001077 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001078 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001079
1080destroy:
1081 /* Destroy the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001082 TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001083 TEST_ASSERT( psa_get_key_information(
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001084 handle, NULL, NULL ) == PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001085
1086exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001087 mbedtls_free( exported );
1088 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001089 mbedtls_psa_crypto_free( );
1090}
1091/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001092
Moran Pekerf709f4a2018-06-06 17:26:04 +03001093/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001094void import_key_nonempty_slot( )
1095{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001096 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001097 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1098 psa_status_t status;
1099 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
1100 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1101
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001102 TEST_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1103 &handle ) == PSA_SUCCESS );
1104
Moran Peker28a38e62018-11-07 16:18:24 +02001105 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001106 TEST_ASSERT( psa_import_key( handle, type,
Moran Peker28a38e62018-11-07 16:18:24 +02001107 data, sizeof( data ) ) == PSA_SUCCESS );
1108
1109 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001110 status = psa_import_key( handle, type, data, sizeof( data ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001111 TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT );
1112
1113exit:
1114 mbedtls_psa_crypto_free( );
1115}
1116/* END_CASE */
1117
1118/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001119void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001120{
1121 psa_status_t status;
1122 unsigned char *exported = NULL;
1123 size_t export_size = 0;
1124 size_t exported_length = INVALID_EXPORT_LENGTH;
1125 psa_status_t expected_export_status = expected_export_status_arg;
1126
1127 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1128
1129 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001130 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001131 exported, export_size,
1132 &exported_length );
1133 TEST_ASSERT( status == expected_export_status );
1134
1135exit:
1136 mbedtls_psa_crypto_free( );
1137}
1138/* END_CASE */
1139
1140/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001141void export_with_no_key_activity( )
1142{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001143 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001144 psa_algorithm_t alg = PSA_ALG_CTR;
1145 psa_status_t status;
1146 psa_key_policy_t policy;
1147 unsigned char *exported = NULL;
1148 size_t export_size = 0;
1149 size_t exported_length = INVALID_EXPORT_LENGTH;
1150
1151 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1152
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001153 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1154 &handle ) == PSA_SUCCESS );
Moran Peker34550092018-11-07 16:19:34 +02001155 psa_key_policy_init( &policy );
1156 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001157 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Peker34550092018-11-07 16:19:34 +02001158
1159 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001160 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001161 exported, export_size,
1162 &exported_length );
1163 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1164
1165exit:
1166 mbedtls_psa_crypto_free( );
1167}
1168/* END_CASE */
1169
1170/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001171void cipher_with_no_key_activity( )
1172{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001173 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001174 psa_status_t status;
1175 psa_key_policy_t policy;
1176 psa_cipher_operation_t operation;
1177 int exercise_alg = PSA_ALG_CTR;
1178
1179 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1180
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001181 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1182 &handle ) == PSA_SUCCESS );
Moran Pekerce500072018-11-07 16:20:07 +02001183 psa_key_policy_init( &policy );
1184 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001185 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekerce500072018-11-07 16:20:07 +02001186
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001187 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Moran Pekerce500072018-11-07 16:20:07 +02001188 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1189
1190exit:
1191 psa_cipher_abort( &operation );
1192 mbedtls_psa_crypto_free( );
1193}
1194/* END_CASE */
1195
1196/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001197void export_after_import_failure( data_t *data, int type_arg,
1198 int expected_import_status_arg )
1199{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001200 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001201 psa_key_type_t type = type_arg;
1202 psa_status_t status;
1203 unsigned char *exported = NULL;
1204 size_t export_size = 0;
1205 psa_status_t expected_import_status = expected_import_status_arg;
1206 size_t exported_length = INVALID_EXPORT_LENGTH;
1207
1208 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1209
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001210 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1211 &handle ) == PSA_SUCCESS );
1212
Moran Peker34550092018-11-07 16:19:34 +02001213 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001214 status = psa_import_key( handle, type,
Moran Peker34550092018-11-07 16:19:34 +02001215 data->x, data->len );
1216 TEST_ASSERT( status == expected_import_status );
1217
1218 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001219 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001220 exported, export_size,
1221 &exported_length );
1222 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1223
1224exit:
1225 mbedtls_psa_crypto_free( );
1226}
1227/* END_CASE */
1228
1229/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001230void cipher_after_import_failure( data_t *data, int type_arg,
1231 int expected_import_status_arg )
1232{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001233 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001234 psa_cipher_operation_t operation;
1235 psa_key_type_t type = type_arg;
1236 psa_status_t status;
1237 psa_status_t expected_import_status = expected_import_status_arg;
1238 int exercise_alg = PSA_ALG_CTR;
1239
1240 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1241
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001242 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1243 &handle ) == PSA_SUCCESS );
1244
Moran Pekerce500072018-11-07 16:20:07 +02001245 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001246 status = psa_import_key( handle, type,
Moran Pekerce500072018-11-07 16:20:07 +02001247 data->x, data->len );
1248 TEST_ASSERT( status == expected_import_status );
1249
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001250 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Moran Pekerce500072018-11-07 16:20:07 +02001251 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1252
1253exit:
1254 psa_cipher_abort( &operation );
1255 mbedtls_psa_crypto_free( );
1256}
1257/* END_CASE */
1258
1259/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001260void export_after_destroy_key( data_t *data, int type_arg )
1261{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001262 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001263 psa_key_type_t type = type_arg;
1264 psa_status_t status;
1265 psa_key_policy_t policy;
1266 psa_algorithm_t alg = PSA_ALG_CTR;
1267 unsigned char *exported = NULL;
1268 size_t export_size = 0;
1269 size_t exported_length = INVALID_EXPORT_LENGTH;
1270
1271 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1272
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001273 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1274 &handle ) == PSA_SUCCESS );
Moran Peker34550092018-11-07 16:19:34 +02001275 psa_key_policy_init( &policy );
1276 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001277 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Peker34550092018-11-07 16:19:34 +02001278 export_size = (ptrdiff_t) data->len;
1279 ASSERT_ALLOC( exported, export_size );
1280
1281 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001282 TEST_ASSERT( psa_import_key( handle, type,
Moran Peker34550092018-11-07 16:19:34 +02001283 data->x, data->len ) == PSA_SUCCESS );
1284
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001285 TEST_ASSERT( psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001286 &exported_length ) == PSA_SUCCESS );
1287
1288 /* Destroy the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001289 TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS );
Moran Peker34550092018-11-07 16:19:34 +02001290
1291 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001292 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001293 &exported_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001294 TEST_ASSERT( status == PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001295
1296exit:
1297 mbedtls_free( exported );
1298 mbedtls_psa_crypto_free( );
1299}
1300/* END_CASE */
1301
1302/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001303void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001304 int type_arg,
1305 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001306 int export_size_delta,
1307 int expected_export_status_arg,
1308 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001309{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001310 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001311 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001312 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001313 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001314 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001315 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001316 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001317 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001318 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001319
Moran Pekerf709f4a2018-06-06 17:26:04 +03001320 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1321
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001322 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1323 &handle ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001324 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001325 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001326 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001327
1328 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001329 TEST_ASSERT( psa_import_key( handle, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001330 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001331
Gilles Peskine49c25912018-10-29 15:15:31 +01001332 /* Export the public key */
1333 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001334 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001335 exported, export_size,
1336 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001337 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001338 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001339 {
1340 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1341 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001342 TEST_ASSERT( psa_get_key_information( handle, NULL, &bits ) ==
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001343 PSA_SUCCESS );
1344 TEST_ASSERT( expected_public_key->len <=
1345 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001346 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1347 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001348 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001349
1350exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001351 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001352 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001353 mbedtls_psa_crypto_free( );
1354}
1355/* END_CASE */
1356
Gilles Peskine20035e32018-02-03 22:44:14 +01001357/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001358void import_and_exercise_key( data_t *data,
1359 int type_arg,
1360 int bits_arg,
1361 int alg_arg )
1362{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001363 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001364 psa_key_type_t type = type_arg;
1365 size_t bits = bits_arg;
1366 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001367 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001368 psa_key_policy_t policy;
1369 psa_key_type_t got_type;
1370 size_t got_bits;
1371 psa_status_t status;
1372
1373 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1374
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001375 TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1376 &handle ) == PSA_SUCCESS );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001377 psa_key_policy_init( &policy );
1378 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001379 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001380
1381 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001382 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001383 TEST_ASSERT( status == PSA_SUCCESS );
1384
1385 /* Test the key information */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001386 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001387 &got_type,
1388 &got_bits ) == PSA_SUCCESS );
1389 TEST_ASSERT( got_type == type );
1390 TEST_ASSERT( got_bits == bits );
1391
1392 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001393 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001394 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001395
1396exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001397 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001398 mbedtls_psa_crypto_free( );
1399}
1400/* END_CASE */
1401
1402/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001403void key_policy( int usage_arg, int alg_arg )
1404{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001405 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001406 psa_algorithm_t alg = alg_arg;
1407 psa_key_usage_t usage = usage_arg;
1408 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1409 unsigned char key[32] = {0};
1410 psa_key_policy_t policy_set;
1411 psa_key_policy_t policy_get;
1412
1413 memset( key, 0x2a, sizeof( key ) );
1414
1415 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1416
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001417 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1418 &handle ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001419 psa_key_policy_init( &policy_set );
1420 psa_key_policy_init( &policy_get );
Gilles Peskined5b33222018-06-18 22:20:03 +02001421 psa_key_policy_set_usage( &policy_set, usage, alg );
1422
1423 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1424 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001425 TEST_ASSERT( psa_set_key_policy( handle, &policy_set ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001426
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001427 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskined5b33222018-06-18 22:20:03 +02001428 key, sizeof( key ) ) == PSA_SUCCESS );
1429
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001430 TEST_ASSERT( psa_get_key_policy( handle, &policy_get ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001431
1432 TEST_ASSERT( policy_get.usage == policy_set.usage );
1433 TEST_ASSERT( policy_get.alg == policy_set.alg );
1434
1435exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001436 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001437 mbedtls_psa_crypto_free( );
1438}
1439/* END_CASE */
1440
1441/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001442void mac_key_policy( int policy_usage,
1443 int policy_alg,
1444 int key_type,
1445 data_t *key_data,
1446 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001447{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001448 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001449 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001450 psa_mac_operation_t operation;
1451 psa_status_t status;
1452 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001453
1454 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1455
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001456 TEST_ASSERT( psa_allocate_key( key_type,
1457 KEY_BITS_FROM_DATA( key_type, key_data ),
1458 &handle ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001459 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001460 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001461 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001462
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001463 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001464 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001465
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001466 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 if( policy_alg == exercise_alg &&
1468 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1469 TEST_ASSERT( status == PSA_SUCCESS );
1470 else
1471 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1472 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001473
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001474 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001475 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476 if( policy_alg == exercise_alg &&
1477 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001478 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001479 else
1480 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1481
1482exit:
1483 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001484 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001485 mbedtls_psa_crypto_free( );
1486}
1487/* END_CASE */
1488
1489/* BEGIN_CASE */
1490void cipher_key_policy( int policy_usage,
1491 int policy_alg,
1492 int key_type,
1493 data_t *key_data,
1494 int exercise_alg )
1495{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001496 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001497 psa_key_policy_t policy;
1498 psa_cipher_operation_t operation;
1499 psa_status_t status;
1500
1501 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1502
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001503 TEST_ASSERT( psa_allocate_key( key_type,
1504 KEY_BITS_FROM_DATA( key_type, key_data ),
1505 &handle ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001506 psa_key_policy_init( &policy );
1507 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001508 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001509
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001510 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001511 key_data->x, key_data->len ) == PSA_SUCCESS );
1512
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001513 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001514 if( policy_alg == exercise_alg &&
1515 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1516 TEST_ASSERT( status == PSA_SUCCESS );
1517 else
1518 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1519 psa_cipher_abort( &operation );
1520
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001521 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001522 if( policy_alg == exercise_alg &&
1523 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1524 TEST_ASSERT( status == PSA_SUCCESS );
1525 else
1526 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1527
1528exit:
1529 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001530 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001531 mbedtls_psa_crypto_free( );
1532}
1533/* END_CASE */
1534
1535/* BEGIN_CASE */
1536void aead_key_policy( int policy_usage,
1537 int policy_alg,
1538 int key_type,
1539 data_t *key_data,
1540 int nonce_length_arg,
1541 int tag_length_arg,
1542 int exercise_alg )
1543{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001544 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001545 psa_key_policy_t policy;
1546 psa_status_t status;
1547 unsigned char nonce[16] = {0};
1548 size_t nonce_length = nonce_length_arg;
1549 unsigned char tag[16];
1550 size_t tag_length = tag_length_arg;
1551 size_t output_length;
1552
1553 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1554 TEST_ASSERT( tag_length <= sizeof( tag ) );
1555
1556 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1557
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001558 TEST_ASSERT( psa_allocate_key( key_type,
1559 KEY_BITS_FROM_DATA( key_type, key_data ),
1560 &handle ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001561 psa_key_policy_init( &policy );
1562 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001565 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001566 key_data->x, key_data->len ) == PSA_SUCCESS );
1567
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001568 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001569 nonce, nonce_length,
1570 NULL, 0,
1571 NULL, 0,
1572 tag, tag_length,
1573 &output_length );
1574 if( policy_alg == exercise_alg &&
1575 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1576 TEST_ASSERT( status == PSA_SUCCESS );
1577 else
1578 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1579
1580 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001581 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001582 nonce, nonce_length,
1583 NULL, 0,
1584 tag, tag_length,
1585 NULL, 0,
1586 &output_length );
1587 if( policy_alg == exercise_alg &&
1588 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1589 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1590 else
1591 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1592
1593exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001594 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001595 mbedtls_psa_crypto_free( );
1596}
1597/* END_CASE */
1598
1599/* BEGIN_CASE */
1600void asymmetric_encryption_key_policy( int policy_usage,
1601 int policy_alg,
1602 int key_type,
1603 data_t *key_data,
1604 int exercise_alg )
1605{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001606 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001607 psa_key_policy_t policy;
1608 psa_status_t status;
1609 size_t key_bits;
1610 size_t buffer_length;
1611 unsigned char *buffer = NULL;
1612 size_t output_length;
1613
1614 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1615
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001616 TEST_ASSERT( psa_allocate_key( key_type,
1617 KEY_BITS_FROM_DATA( key_type, key_data ),
1618 &handle ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619 psa_key_policy_init( &policy );
1620 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001621 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001622
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001623 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001624 key_data->x, key_data->len ) == PSA_SUCCESS );
1625
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001626 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001627 NULL,
1628 &key_bits ) == PSA_SUCCESS );
1629 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1630 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001631 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001632
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001633 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001634 NULL, 0,
1635 NULL, 0,
1636 buffer, buffer_length,
1637 &output_length );
1638 if( policy_alg == exercise_alg &&
1639 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1640 TEST_ASSERT( status == PSA_SUCCESS );
1641 else
1642 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1643
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001644 if( buffer_length != 0 )
1645 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001646 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001647 buffer, buffer_length,
1648 NULL, 0,
1649 buffer, buffer_length,
1650 &output_length );
1651 if( policy_alg == exercise_alg &&
1652 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1653 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1654 else
1655 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1656
1657exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001658 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001659 mbedtls_psa_crypto_free( );
1660 mbedtls_free( buffer );
1661}
1662/* END_CASE */
1663
1664/* BEGIN_CASE */
1665void asymmetric_signature_key_policy( int policy_usage,
1666 int policy_alg,
1667 int key_type,
1668 data_t *key_data,
1669 int exercise_alg )
1670{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001671 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001672 psa_key_policy_t policy;
1673 psa_status_t status;
1674 unsigned char payload[16] = {1};
1675 size_t payload_length = sizeof( payload );
1676 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1677 size_t signature_length;
1678
1679 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1680
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001681 TEST_ASSERT( psa_allocate_key( key_type,
1682 KEY_BITS_FROM_DATA( key_type, key_data ),
1683 &handle ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001684 psa_key_policy_init( &policy );
1685 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001686 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001687
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001688 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001689 key_data->x, key_data->len ) == PSA_SUCCESS );
1690
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001691 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001692 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001693 signature, sizeof( signature ),
1694 &signature_length );
1695 if( policy_alg == exercise_alg &&
1696 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1697 TEST_ASSERT( status == PSA_SUCCESS );
1698 else
1699 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1700
1701 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001702 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001704 signature, sizeof( signature ) );
1705 if( policy_alg == exercise_alg &&
1706 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1707 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1708 else
1709 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001710
1711exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001712 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001713 mbedtls_psa_crypto_free( );
1714}
1715/* END_CASE */
1716
1717/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001718void derive_key_policy( int policy_usage,
1719 int policy_alg,
1720 int key_type,
1721 data_t *key_data,
1722 int exercise_alg )
1723{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001724 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001725 psa_key_policy_t policy;
1726 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1727 psa_status_t status;
1728
1729 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1730
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001731 TEST_ASSERT( psa_allocate_key( key_type,
1732 KEY_BITS_FROM_DATA( key_type, key_data ),
1733 &handle ) == PSA_SUCCESS );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001734 psa_key_policy_init( &policy );
1735 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001736 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001737
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001738 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001739 key_data->x, key_data->len ) == PSA_SUCCESS );
1740
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001741 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001742 exercise_alg,
1743 NULL, 0,
1744 NULL, 0,
1745 1 );
1746 if( policy_alg == exercise_alg &&
1747 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1748 TEST_ASSERT( status == PSA_SUCCESS );
1749 else
1750 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1751
1752exit:
1753 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001754 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001755 mbedtls_psa_crypto_free( );
1756}
1757/* END_CASE */
1758
1759/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001760void agreement_key_policy( int policy_usage,
1761 int policy_alg,
1762 int key_type_arg,
1763 data_t *key_data,
1764 int exercise_alg )
1765{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001766 psa_key_handle_t handle = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001767 psa_key_policy_t policy;
1768 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001769 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1770 psa_status_t status;
1771
1772 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1773
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001774 TEST_ASSERT( psa_allocate_key( key_type,
1775 KEY_BITS_FROM_DATA( key_type, key_data ),
1776 &handle ) == PSA_SUCCESS );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001777 psa_key_policy_init( &policy );
1778 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001779 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001780
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001781 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine01d718c2018-09-18 12:01:02 +02001782 key_data->x, key_data->len ) == PSA_SUCCESS );
1783
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001784 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001785
Gilles Peskine01d718c2018-09-18 12:01:02 +02001786 if( policy_alg == exercise_alg &&
1787 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1788 TEST_ASSERT( status == PSA_SUCCESS );
1789 else
1790 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1791
1792exit:
1793 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001794 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001795 mbedtls_psa_crypto_free( );
1796}
1797/* END_CASE */
1798
1799/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001800void hash_setup( int alg_arg,
1801 int expected_status_arg )
1802{
1803 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001804 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001805 psa_hash_operation_t operation;
1806 psa_status_t status;
1807
1808 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1809
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001810 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001811 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001812 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001813
1814exit:
1815 mbedtls_psa_crypto_free( );
1816}
1817/* END_CASE */
1818
1819/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001820void hash_bad_order( )
1821{
1822 unsigned char input[] = "";
1823 /* SHA-256 hash of an empty string */
1824 unsigned char hash[] = {
1825 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1826 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1827 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1828 size_t hash_len;
1829 psa_hash_operation_t operation;
1830
1831 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1832
1833 /* psa_hash_update without calling psa_hash_setup beforehand */
1834 memset( &operation, 0, sizeof( operation ) );
1835 TEST_ASSERT( psa_hash_update( &operation,
1836 input, sizeof( input ) ) ==
1837 PSA_ERROR_INVALID_ARGUMENT );
1838
1839 /* psa_hash_verify without calling psa_hash_setup beforehand */
1840 memset( &operation, 0, sizeof( operation ) );
1841 TEST_ASSERT( psa_hash_verify( &operation,
1842 hash, sizeof( hash ) ) ==
1843 PSA_ERROR_INVALID_ARGUMENT );
1844
1845 /* psa_hash_finish without calling psa_hash_setup beforehand */
1846 memset( &operation, 0, sizeof( operation ) );
1847 TEST_ASSERT( psa_hash_finish( &operation,
1848 hash, sizeof( hash ), &hash_len ) ==
1849 PSA_ERROR_INVALID_ARGUMENT );
1850
1851exit:
1852 mbedtls_psa_crypto_free( );
1853}
1854/* END_CASE */
1855
itayzafrir27e69452018-11-01 14:26:34 +02001856/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1857void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001858{
1859 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001860 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1861 * appended to it */
1862 unsigned char hash[] = {
1863 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1864 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1865 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001866 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001867 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001868
1869 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1870
itayzafrir27e69452018-11-01 14:26:34 +02001871 /* psa_hash_verify with a smaller hash than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001872 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1873 TEST_ASSERT( psa_hash_verify( &operation,
1874 hash, expected_size - 1 ) ==
1875 PSA_ERROR_INVALID_SIGNATURE );
1876
itayzafrir27e69452018-11-01 14:26:34 +02001877 /* psa_hash_verify with a non-matching hash */
itayzafrirec93d302018-10-18 18:01:10 +03001878 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrirec93d302018-10-18 18:01:10 +03001879 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001880 hash + 1, expected_size ) ==
itayzafrirec93d302018-10-18 18:01:10 +03001881 PSA_ERROR_INVALID_SIGNATURE );
1882
itayzafrir27e69452018-11-01 14:26:34 +02001883 /* psa_hash_verify with a hash longer than expected */
itayzafrir4271df92018-10-24 18:16:19 +03001884 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrir4271df92018-10-24 18:16:19 +03001885 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001886 hash, sizeof( hash ) ) ==
itayzafrir4271df92018-10-24 18:16:19 +03001887 PSA_ERROR_INVALID_SIGNATURE );
1888
itayzafrirec93d302018-10-18 18:01:10 +03001889exit:
1890 mbedtls_psa_crypto_free( );
1891}
1892/* END_CASE */
1893
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001894/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1895void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001896{
1897 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001898 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001899 size_t expected_size = PSA_HASH_SIZE( alg );
1900 psa_hash_operation_t operation;
1901 size_t hash_len;
1902
1903 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1904
itayzafrir58028322018-10-25 10:22:01 +03001905 /* psa_hash_finish with a smaller hash buffer than expected */
1906 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1907 TEST_ASSERT( psa_hash_finish( &operation,
1908 hash, expected_size - 1,
1909 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1910
1911exit:
1912 mbedtls_psa_crypto_free( );
1913}
1914/* END_CASE */
1915
1916/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001917void mac_setup( int key_type_arg,
1918 data_t *key,
1919 int alg_arg,
1920 int expected_status_arg )
1921{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001922 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001923 psa_key_type_t key_type = key_type_arg;
1924 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001925 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001926 psa_mac_operation_t operation;
1927 psa_key_policy_t policy;
1928 psa_status_t status;
1929
1930 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1931
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001932 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1933 &handle ) == PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001934 psa_key_policy_init( &policy );
1935 psa_key_policy_set_usage( &policy,
1936 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1937 alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001938 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001939
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001940 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001941 key->x, key->len ) == PSA_SUCCESS );
1942
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001943 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001944 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001945 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001946
1947exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001948 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001949 mbedtls_psa_crypto_free( );
1950}
1951/* END_CASE */
1952
1953/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001954void mac_sign( int key_type_arg,
1955 data_t *key,
1956 int alg_arg,
1957 data_t *input,
1958 data_t *expected_mac )
1959{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001960 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001961 psa_key_type_t key_type = key_type_arg;
1962 psa_algorithm_t alg = alg_arg;
1963 psa_mac_operation_t operation;
1964 psa_key_policy_t policy;
1965 /* Leave a little extra room in the output buffer. At the end of the
1966 * test, we'll check that the implementation didn't overwrite onto
1967 * this extra room. */
1968 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1969 size_t mac_buffer_size =
1970 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1971 size_t mac_length = 0;
1972
1973 memset( actual_mac, '+', sizeof( actual_mac ) );
1974 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1975 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1976
1977 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1978
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001979 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1980 &handle ) == PSA_SUCCESS );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001981 psa_key_policy_init( &policy );
1982 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001983 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001984
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001985 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001986 key->x, key->len ) == PSA_SUCCESS );
1987
1988 /* Calculate the MAC. */
1989 TEST_ASSERT( psa_mac_sign_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001990 handle, alg ) == PSA_SUCCESS );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001991 TEST_ASSERT( psa_mac_update( &operation,
1992 input->x, input->len ) == PSA_SUCCESS );
1993 TEST_ASSERT( psa_mac_sign_finish( &operation,
1994 actual_mac, mac_buffer_size,
1995 &mac_length ) == PSA_SUCCESS );
1996
1997 /* Compare with the expected value. */
1998 TEST_ASSERT( mac_length == expected_mac->len );
1999 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
2000
2001 /* Verify that the end of the buffer is untouched. */
2002 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2003 sizeof( actual_mac ) - mac_length ) );
2004
2005exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002006 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002007 mbedtls_psa_crypto_free( );
2008}
2009/* END_CASE */
2010
2011/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002012void mac_verify( int key_type_arg,
2013 data_t *key,
2014 int alg_arg,
2015 data_t *input,
2016 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002017{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002018 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002019 psa_key_type_t key_type = key_type_arg;
2020 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002021 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07002022 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002023
Gilles Peskine69c12672018-06-28 00:07:19 +02002024 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2025
Gilles Peskine8c9def32018-02-08 10:02:12 +01002026 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002027 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002029 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002030 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2031 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002032
2033 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2034
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002035 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2036 &handle ) == PSA_SUCCESS );
mohammad16036df908f2018-04-02 08:34:15 -07002037 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002038 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002039 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
mohammad16036df908f2018-04-02 08:34:15 -07002040
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002041 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002042 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002043
Gilles Peskine89167cb2018-07-08 20:12:23 +02002044 TEST_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002045 handle, alg ) == PSA_SUCCESS );
2046 TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002047 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002048 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02002049 TEST_ASSERT( psa_mac_verify_finish( &operation,
2050 expected_mac->x,
2051 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002052
2053exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002054 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002055 mbedtls_psa_crypto_free( );
2056}
2057/* END_CASE */
2058
2059/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002060void cipher_setup( int key_type_arg,
2061 data_t *key,
2062 int alg_arg,
2063 int expected_status_arg )
2064{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002065 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002066 psa_key_type_t key_type = key_type_arg;
2067 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002068 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002069 psa_cipher_operation_t operation;
2070 psa_key_policy_t policy;
2071 psa_status_t status;
2072
2073 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2074
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002075 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2076 &handle ) == PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002077 psa_key_policy_init( &policy );
2078 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002079 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002080
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002081 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002082 key->x, key->len ) == PSA_SUCCESS );
2083
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002084 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002085 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002086 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002087
2088exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002089 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002090 mbedtls_psa_crypto_free( );
2091}
2092/* END_CASE */
2093
2094/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002095void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002096 data_t *key,
2097 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002098 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002099{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002100 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002101 psa_status_t status;
2102 psa_key_type_t key_type = key_type_arg;
2103 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002104 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002105 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002106 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002107 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002108 size_t output_buffer_size = 0;
2109 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002110 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002111 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002112 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002113
Gilles Peskine50e586b2018-06-08 14:28:46 +02002114 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002115 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002116 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002117 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2118 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2119 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002120
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002121 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2122 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002123
2124 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2125
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002126 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2127 &handle ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002128 psa_key_policy_init( &policy );
2129 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002130 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002131
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002132 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002133 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002134
Gilles Peskinefe119512018-07-08 21:39:34 +02002135 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002136 handle, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002137
Gilles Peskinefe119512018-07-08 21:39:34 +02002138 TEST_ASSERT( psa_cipher_set_iv( &operation,
2139 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002140 output_buffer_size = (size_t) input->len +
2141 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002142 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002143
Gilles Peskine4abf7412018-06-18 16:35:34 +02002144 TEST_ASSERT( psa_cipher_update( &operation,
2145 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002146 output, output_buffer_size,
2147 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002148 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002149 status = psa_cipher_finish( &operation,
2150 output + function_output_length,
2151 output_buffer_size,
2152 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002153 total_output_length += function_output_length;
2154
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002155 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002156 if( expected_status == PSA_SUCCESS )
2157 {
2158 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002159 ASSERT_COMPARE( expected_output->x, expected_output->len,
2160 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002161 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002162
Gilles Peskine50e586b2018-06-08 14:28:46 +02002163exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002164 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002165 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002166 mbedtls_psa_crypto_free( );
2167}
2168/* END_CASE */
2169
2170/* BEGIN_CASE */
2171void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002172 data_t *key,
2173 data_t *input,
2174 int first_part_size,
2175 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002176{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002177 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002178 psa_key_type_t key_type = key_type_arg;
2179 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002181 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002182 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002183 size_t output_buffer_size = 0;
2184 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002185 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002186 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002187 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002188
Gilles Peskine50e586b2018-06-08 14:28:46 +02002189 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002190 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002191 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002192 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2193 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2194 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002195
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002196 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2197 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002198
2199 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2200
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002201 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2202 &handle ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002203 psa_key_policy_init( &policy );
2204 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002205 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002206
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002207 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002208 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002209
Gilles Peskinefe119512018-07-08 21:39:34 +02002210 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002211 handle, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002212
Gilles Peskinefe119512018-07-08 21:39:34 +02002213 TEST_ASSERT( psa_cipher_set_iv( &operation,
2214 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002215 output_buffer_size = (size_t) input->len +
2216 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002217 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002218
Gilles Peskine4abf7412018-06-18 16:35:34 +02002219 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002220 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002221 output, output_buffer_size,
2222 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002223 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002224 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002225 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002226 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002227 output, output_buffer_size,
2228 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002229 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002230 TEST_ASSERT( psa_cipher_finish( &operation,
2231 output + function_output_length,
2232 output_buffer_size,
2233 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002234 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002235 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2236
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002237 ASSERT_COMPARE( expected_output->x, expected_output->len,
2238 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002239
2240exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002241 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002242 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002243 mbedtls_psa_crypto_free( );
2244}
2245/* END_CASE */
2246
2247/* BEGIN_CASE */
2248void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002249 data_t *key,
2250 data_t *input,
2251 int first_part_size,
2252 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002253{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002254 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002255
2256 psa_key_type_t key_type = key_type_arg;
2257 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002258 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002259 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002260 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002261 size_t output_buffer_size = 0;
2262 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002263 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002264 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002265 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002266
Gilles Peskine50e586b2018-06-08 14:28:46 +02002267 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002268 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002269 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002270 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2271 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2272 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002273
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002274 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2275 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002276
2277 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2278
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002279 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2280 &handle ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002281 psa_key_policy_init( &policy );
2282 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002283 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002284
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002285 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002286 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002287
Gilles Peskinefe119512018-07-08 21:39:34 +02002288 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002289 handle, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002290
Gilles Peskinefe119512018-07-08 21:39:34 +02002291 TEST_ASSERT( psa_cipher_set_iv( &operation,
2292 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002293
mohammad16033d91abe2018-07-03 13:15:54 +03002294 output_buffer_size = (size_t) input->len +
2295 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002296 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002297
Gilles Peskine4abf7412018-06-18 16:35:34 +02002298 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2299 TEST_ASSERT( psa_cipher_update( &operation,
2300 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002301 output, output_buffer_size,
2302 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002303 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002304 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002305 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002306 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002307 output, output_buffer_size,
2308 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002309 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002310 TEST_ASSERT( psa_cipher_finish( &operation,
2311 output + function_output_length,
2312 output_buffer_size,
2313 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002314 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002315 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2316
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002317 ASSERT_COMPARE( expected_output->x, expected_output->len,
2318 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002319
2320exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002321 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002322 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002323 mbedtls_psa_crypto_free( );
2324}
2325/* END_CASE */
2326
Gilles Peskine50e586b2018-06-08 14:28:46 +02002327/* BEGIN_CASE */
2328void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002329 data_t *key,
2330 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002331 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002332{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002333 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002334 psa_status_t status;
2335 psa_key_type_t key_type = key_type_arg;
2336 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002337 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002338 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002339 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002340 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002341 size_t output_buffer_size = 0;
2342 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002343 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002344 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002345 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002346
Gilles Peskine50e586b2018-06-08 14:28:46 +02002347 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002348 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002349 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002350 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2351 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2352 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002353
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002354 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2355 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002356
2357 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2358
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002359 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2360 &handle ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002361 psa_key_policy_init( &policy );
2362 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002363 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002364
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002365 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002366 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002367
Gilles Peskinefe119512018-07-08 21:39:34 +02002368 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002369 handle, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002370
Gilles Peskinefe119512018-07-08 21:39:34 +02002371 TEST_ASSERT( psa_cipher_set_iv( &operation,
2372 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002373
mohammad16033d91abe2018-07-03 13:15:54 +03002374 output_buffer_size = (size_t) input->len +
2375 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002376 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002377
Gilles Peskine4abf7412018-06-18 16:35:34 +02002378 TEST_ASSERT( psa_cipher_update( &operation,
2379 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002380 output, output_buffer_size,
2381 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002382 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002383 status = psa_cipher_finish( &operation,
2384 output + function_output_length,
2385 output_buffer_size,
2386 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002387 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002388 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002389
2390 if( expected_status == PSA_SUCCESS )
2391 {
2392 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002393 ASSERT_COMPARE( expected_output->x, expected_output->len,
2394 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002395 }
2396
Gilles Peskine50e586b2018-06-08 14:28:46 +02002397exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002398 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002399 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002400 mbedtls_psa_crypto_free( );
2401}
2402/* END_CASE */
2403
Gilles Peskine50e586b2018-06-08 14:28:46 +02002404/* BEGIN_CASE */
2405void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002406 data_t *key,
2407 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002408{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002409 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002410 psa_key_type_t key_type = key_type_arg;
2411 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002412 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002413 size_t iv_size = 16;
2414 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002415 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002416 size_t output1_size = 0;
2417 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002418 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002419 size_t output2_size = 0;
2420 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002421 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002422 psa_cipher_operation_t operation1;
2423 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002424 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002425
mohammad1603d7d7ba52018-03-12 18:51:53 +02002426 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002427 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002428 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2429 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002430
mohammad1603d7d7ba52018-03-12 18:51:53 +02002431 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2432
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002433 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2434 &handle ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002435 psa_key_policy_init( &policy );
2436 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002437 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002438
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002439 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002440 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002441
Gilles Peskinefe119512018-07-08 21:39:34 +02002442 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002443 handle, alg ) == PSA_SUCCESS );
Gilles Peskinefe119512018-07-08 21:39:34 +02002444 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002445 handle, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002446
Gilles Peskinefe119512018-07-08 21:39:34 +02002447 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2448 iv, iv_size,
2449 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002450 output1_size = (size_t) input->len +
2451 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002452 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002453
Gilles Peskine4abf7412018-06-18 16:35:34 +02002454 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002455 output1, output1_size,
2456 &output1_length ) == PSA_SUCCESS );
2457 TEST_ASSERT( psa_cipher_finish( &operation1,
2458 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002459 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002460
Gilles Peskine048b7f02018-06-08 14:20:49 +02002461 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002462
2463 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2464
2465 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002466 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002467
Gilles Peskinefe119512018-07-08 21:39:34 +02002468 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2469 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002470 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2471 output2, output2_size,
2472 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002473 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002474 TEST_ASSERT( psa_cipher_finish( &operation2,
2475 output2 + output2_length,
2476 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002477 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002478
Gilles Peskine048b7f02018-06-08 14:20:49 +02002479 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002480
Janos Follath25c4fa82018-07-06 16:23:25 +01002481 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002482
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002483 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002484
2485exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002486 mbedtls_free( output1 );
2487 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002488 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002489 mbedtls_psa_crypto_free( );
2490}
2491/* END_CASE */
2492
2493/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002494void cipher_verify_output_multipart( int alg_arg,
2495 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002496 data_t *key,
2497 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002498 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002499{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002500 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002501 psa_key_type_t key_type = key_type_arg;
2502 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002503 unsigned char iv[16] = {0};
2504 size_t iv_size = 16;
2505 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002506 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002507 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002508 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002509 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002510 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002511 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002512 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002513 psa_cipher_operation_t operation1;
2514 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002515 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002516
Moran Pekerded84402018-06-06 16:36:50 +03002517 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002518 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002519 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2520 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002521
Moran Pekerded84402018-06-06 16:36:50 +03002522 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2523
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002524 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2525 &handle ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002526 psa_key_policy_init( &policy );
2527 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002528 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Moran Pekered346952018-07-05 15:22:45 +03002529
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002530 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002531 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002532
Gilles Peskinefe119512018-07-08 21:39:34 +02002533 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002534 handle, alg ) == PSA_SUCCESS );
Gilles Peskinefe119512018-07-08 21:39:34 +02002535 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002536 handle, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002537
Gilles Peskinefe119512018-07-08 21:39:34 +02002538 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2539 iv, iv_size,
2540 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002541 output1_buffer_size = (size_t) input->len +
2542 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002543 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002544
Gilles Peskine4abf7412018-06-18 16:35:34 +02002545 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002546
itayzafrir3e02b3b2018-06-12 17:06:52 +03002547 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002548 output1, output1_buffer_size,
2549 &function_output_length ) == PSA_SUCCESS );
2550 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002551
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002552 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002553 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002554 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002555 output1, output1_buffer_size,
2556 &function_output_length ) == PSA_SUCCESS );
2557 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002558
Gilles Peskine048b7f02018-06-08 14:20:49 +02002559 TEST_ASSERT( psa_cipher_finish( &operation1,
2560 output1 + output1_length,
2561 output1_buffer_size - output1_length,
2562 &function_output_length ) == PSA_SUCCESS );
2563 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002564
2565 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2566
Gilles Peskine048b7f02018-06-08 14:20:49 +02002567 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002568 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002569
Gilles Peskinefe119512018-07-08 21:39:34 +02002570 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2571 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002572
2573 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002574 output2, output2_buffer_size,
2575 &function_output_length ) == PSA_SUCCESS );
2576 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002577
Gilles Peskine048b7f02018-06-08 14:20:49 +02002578 TEST_ASSERT( psa_cipher_update( &operation2,
2579 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002580 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002581 output2, output2_buffer_size,
2582 &function_output_length ) == PSA_SUCCESS );
2583 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002584
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002585 TEST_ASSERT( psa_cipher_finish( &operation2,
2586 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002587 output2_buffer_size - output2_length,
2588 &function_output_length ) == PSA_SUCCESS );
2589 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002590
Janos Follath25c4fa82018-07-06 16:23:25 +01002591 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002592
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002593 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002594
2595exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002596 mbedtls_free( output1 );
2597 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002598 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002599 mbedtls_psa_crypto_free( );
2600}
2601/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002602
Gilles Peskine20035e32018-02-03 22:44:14 +01002603/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002604void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002605 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002606 data_t *nonce,
2607 data_t *additional_data,
2608 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002609 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002610{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002611 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002612 psa_key_type_t key_type = key_type_arg;
2613 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002614 unsigned char *output_data = NULL;
2615 size_t output_size = 0;
2616 size_t output_length = 0;
2617 unsigned char *output_data2 = NULL;
2618 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002619 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002620 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002621 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002622
Gilles Peskinea1cac842018-06-11 19:33:02 +02002623 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002624 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002625 TEST_ASSERT( nonce != NULL );
2626 TEST_ASSERT( additional_data != NULL );
2627 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2628 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2629 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2630 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2631
Gilles Peskine4abf7412018-06-18 16:35:34 +02002632 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002633 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002634
2635 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2636
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002637 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2638 &handle ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002639 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002640 psa_key_policy_set_usage( &policy,
2641 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2642 alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002643 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002644
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002645 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002646 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002647
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002648 TEST_ASSERT( psa_aead_encrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002649 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002650 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002651 additional_data->len,
2652 input_data->x, input_data->len,
2653 output_data, output_size,
2654 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002655
2656 if( PSA_SUCCESS == expected_result )
2657 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002658 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002659
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002660 TEST_ASSERT( psa_aead_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002661 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002662 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002663 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002664 output_data, output_length,
2665 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002666 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002667
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002668 ASSERT_COMPARE( input_data->x, input_data->len,
2669 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002670 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002671
Gilles Peskinea1cac842018-06-11 19:33:02 +02002672exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002673 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002674 mbedtls_free( output_data );
2675 mbedtls_free( output_data2 );
2676 mbedtls_psa_crypto_free( );
2677}
2678/* END_CASE */
2679
2680/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002681void aead_encrypt( int key_type_arg, data_t *key_data,
2682 int alg_arg,
2683 data_t *nonce,
2684 data_t *additional_data,
2685 data_t *input_data,
2686 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002687{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002688 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002689 psa_key_type_t key_type = key_type_arg;
2690 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002691 unsigned char *output_data = NULL;
2692 size_t output_size = 0;
2693 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002694 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002695 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002696
Gilles Peskinea1cac842018-06-11 19:33:02 +02002697 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002698 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002699 TEST_ASSERT( additional_data != NULL );
2700 TEST_ASSERT( nonce != NULL );
2701 TEST_ASSERT( expected_result != NULL );
2702 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2703 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2704 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2705 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2706 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2707
Gilles Peskine4abf7412018-06-18 16:35:34 +02002708 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002709 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002710
Gilles Peskinea1cac842018-06-11 19:33:02 +02002711 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2712
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002713 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2714 &handle ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002715 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002717 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002718
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002719 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002720 key_data->x,
2721 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002722
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002723 TEST_ASSERT( psa_aead_encrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002724 nonce->x, nonce->len,
2725 additional_data->x, additional_data->len,
2726 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002727 output_data, output_size,
2728 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002729
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002730 ASSERT_COMPARE( expected_result->x, expected_result->len,
2731 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002732
Gilles Peskinea1cac842018-06-11 19:33:02 +02002733exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002734 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002735 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002736 mbedtls_psa_crypto_free( );
2737}
2738/* END_CASE */
2739
2740/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002741void aead_decrypt( int key_type_arg, data_t *key_data,
2742 int alg_arg,
2743 data_t *nonce,
2744 data_t *additional_data,
2745 data_t *input_data,
2746 data_t *expected_data,
2747 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002748{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002749 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002750 psa_key_type_t key_type = key_type_arg;
2751 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002752 unsigned char *output_data = NULL;
2753 size_t output_size = 0;
2754 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002755 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002756 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002757 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002758
Gilles Peskinea1cac842018-06-11 19:33:02 +02002759 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002760 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002761 TEST_ASSERT( additional_data != NULL );
2762 TEST_ASSERT( nonce != NULL );
2763 TEST_ASSERT( expected_data != NULL );
2764 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2765 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2766 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2767 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2768 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2769
Gilles Peskine4abf7412018-06-18 16:35:34 +02002770 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002771 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002772
Gilles Peskinea1cac842018-06-11 19:33:02 +02002773 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2774
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002775 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2776 &handle ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002777 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002778 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002779 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002780
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002781 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002782 key_data->x,
2783 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002784
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002785 TEST_ASSERT( psa_aead_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002786 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002787 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002788 additional_data->len,
2789 input_data->x, input_data->len,
2790 output_data, output_size,
2791 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002792
Gilles Peskine2d277862018-06-18 15:41:12 +02002793 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002794 ASSERT_COMPARE( expected_data->x, expected_data->len,
2795 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002796
Gilles Peskinea1cac842018-06-11 19:33:02 +02002797exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002798 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002799 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002800 mbedtls_psa_crypto_free( );
2801}
2802/* END_CASE */
2803
2804/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002805void signature_size( int type_arg,
2806 int bits,
2807 int alg_arg,
2808 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002809{
2810 psa_key_type_t type = type_arg;
2811 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002812 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002813 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2814exit:
2815 ;
2816}
2817/* END_CASE */
2818
2819/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002820void sign_deterministic( int key_type_arg, data_t *key_data,
2821 int alg_arg, data_t *input_data,
2822 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002823{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002824 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002825 psa_key_type_t key_type = key_type_arg;
2826 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002827 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002828 unsigned char *signature = NULL;
2829 size_t signature_size;
2830 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002831 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002832
Gilles Peskine20035e32018-02-03 22:44:14 +01002833 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002834 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002835 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002836 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2837 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2838 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002839
2840 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2841
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002842 TEST_ASSERT( psa_allocate_key( key_type,
2843 KEY_BITS_FROM_DATA( key_type, key_data ),
2844 &handle ) == PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002845 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002846 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002847 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002848
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002849 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002850 key_data->x,
2851 key_data->len ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002852 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002853 NULL,
2854 &key_bits ) == PSA_SUCCESS );
2855
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002856 /* Allocate a buffer which has the size advertized by the
2857 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002858 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2859 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002860 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002861 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002862 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002863
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002864 /* Perform the signature. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002865 TEST_ASSERT( psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002866 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002867 signature, signature_size,
2868 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002869 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002870 ASSERT_COMPARE( output_data->x, output_data->len,
2871 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002872
2873exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002874 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002875 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002876 mbedtls_psa_crypto_free( );
2877}
2878/* END_CASE */
2879
2880/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002881void sign_fail( int key_type_arg, data_t *key_data,
2882 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002883 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002884{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002885 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002886 psa_key_type_t key_type = key_type_arg;
2887 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002888 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002889 psa_status_t actual_status;
2890 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002891 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002892 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002893 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002894
Gilles Peskine20035e32018-02-03 22:44:14 +01002895 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002896 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002897 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2898 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2899
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002900 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002901
2902 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2903
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002904 TEST_ASSERT( psa_allocate_key( key_type,
2905 KEY_BITS_FROM_DATA( key_type, key_data ),
2906 &handle ) == PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002907 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002908 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002909 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002910
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002911 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002912 key_data->x,
2913 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002914
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002915 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002916 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002917 signature, signature_size,
2918 &signature_length );
2919 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002920 /* The value of *signature_length is unspecified on error, but
2921 * whatever it is, it should be less than signature_size, so that
2922 * if the caller tries to read *signature_length bytes without
2923 * checking the error code then they don't overflow a buffer. */
2924 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002925
2926exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002927 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002928 mbedtls_free( signature );
2929 mbedtls_psa_crypto_free( );
2930}
2931/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002932
2933/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002934void sign_verify( int key_type_arg, data_t *key_data,
2935 int alg_arg, data_t *input_data )
2936{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002937 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002938 psa_key_type_t key_type = key_type_arg;
2939 psa_algorithm_t alg = alg_arg;
2940 size_t key_bits;
2941 unsigned char *signature = NULL;
2942 size_t signature_size;
2943 size_t signature_length = 0xdeadbeef;
2944 psa_key_policy_t policy;
2945
2946 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2947
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002948 TEST_ASSERT( psa_allocate_key( key_type,
2949 KEY_BITS_FROM_DATA( key_type, key_data ),
2950 &handle ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002951 psa_key_policy_init( &policy );
2952 psa_key_policy_set_usage( &policy,
2953 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2954 alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002955 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002956
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002957 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02002958 key_data->x,
2959 key_data->len ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002960 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine9911b022018-06-29 17:30:48 +02002961 NULL,
2962 &key_bits ) == PSA_SUCCESS );
2963
2964 /* Allocate a buffer which has the size advertized by the
2965 * library. */
2966 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2967 key_bits, alg );
2968 TEST_ASSERT( signature_size != 0 );
2969 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002970 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002971
2972 /* Perform the signature. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002973 TEST_ASSERT( psa_asymmetric_sign( handle, alg,
Gilles Peskine9911b022018-06-29 17:30:48 +02002974 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002975 signature, signature_size,
2976 &signature_length ) == PSA_SUCCESS );
2977 /* Check that the signature length looks sensible. */
2978 TEST_ASSERT( signature_length <= signature_size );
2979 TEST_ASSERT( signature_length > 0 );
2980
2981 /* Use the library to verify that the signature is correct. */
2982 TEST_ASSERT( psa_asymmetric_verify(
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002983 handle, alg,
Gilles Peskine9911b022018-06-29 17:30:48 +02002984 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002985 signature, signature_length ) == PSA_SUCCESS );
2986
2987 if( input_data->len != 0 )
2988 {
2989 /* Flip a bit in the input and verify that the signature is now
2990 * detected as invalid. Flip a bit at the beginning, not at the end,
2991 * because ECDSA may ignore the last few bits of the input. */
2992 input_data->x[0] ^= 1;
2993 TEST_ASSERT( psa_asymmetric_verify(
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002994 handle, alg,
Gilles Peskine9911b022018-06-29 17:30:48 +02002995 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002996 signature,
2997 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2998 }
2999
3000exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003001 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003002 mbedtls_free( signature );
3003 mbedtls_psa_crypto_free( );
3004}
3005/* END_CASE */
3006
3007/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003008void asymmetric_verify( int key_type_arg, data_t *key_data,
3009 int alg_arg, data_t *hash_data,
3010 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003011{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003012 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003013 psa_key_type_t key_type = key_type_arg;
3014 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003015 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03003016
Gilles Peskine69c12672018-06-28 00:07:19 +02003017 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3018
itayzafrir5c753392018-05-08 11:18:38 +03003019 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003020 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003021 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003022 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3023 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3024 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003025
3026 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3027
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003028 TEST_ASSERT( psa_allocate_key( key_type,
3029 KEY_BITS_FROM_DATA( key_type, key_data ),
3030 &handle ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003031 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003032 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003033 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003034
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003035 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003036 key_data->x,
3037 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003038
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003039 TEST_ASSERT( psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003040 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003041 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003042 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03003043exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003044 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003045 mbedtls_psa_crypto_free( );
3046}
3047/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003048
3049/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003050void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3051 int alg_arg, data_t *hash_data,
3052 data_t *signature_data,
3053 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003054{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003055 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003056 psa_key_type_t key_type = key_type_arg;
3057 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003058 psa_status_t actual_status;
3059 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003060 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003061
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003062 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003063 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003064 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003065 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3066 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3067 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003068
3069 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3070
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003071 TEST_ASSERT( psa_allocate_key( key_type,
3072 KEY_BITS_FROM_DATA( key_type, key_data ),
3073 &handle ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003074 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003075 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003076 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003077
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003078 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003079 key_data->x,
3080 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003081
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003082 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003083 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003084 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003085 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003086
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003087 TEST_ASSERT( actual_status == expected_status );
3088
3089exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003090 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003091 mbedtls_psa_crypto_free( );
3092}
3093/* END_CASE */
3094
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003095/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003096void asymmetric_encrypt( int key_type_arg,
3097 data_t *key_data,
3098 int alg_arg,
3099 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003100 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003101 int expected_output_length_arg,
3102 int expected_status_arg )
3103{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003104 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003105 psa_key_type_t key_type = key_type_arg;
3106 psa_algorithm_t alg = alg_arg;
3107 size_t expected_output_length = expected_output_length_arg;
3108 size_t key_bits;
3109 unsigned char *output = NULL;
3110 size_t output_size;
3111 size_t output_length = ~0;
3112 psa_status_t actual_status;
3113 psa_status_t expected_status = expected_status_arg;
3114 psa_key_policy_t policy;
3115
3116 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3117
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003118
Gilles Peskine656896e2018-06-29 19:12:28 +02003119 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003120 TEST_ASSERT( psa_allocate_key( key_type,
3121 KEY_BITS_FROM_DATA( key_type, key_data ),
3122 &handle ) == PSA_SUCCESS );
Gilles Peskine656896e2018-06-29 19:12:28 +02003123 psa_key_policy_init( &policy );
3124 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003125 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
3126 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine656896e2018-06-29 19:12:28 +02003127 key_data->x,
3128 key_data->len ) == PSA_SUCCESS );
3129
3130 /* Determine the maximum output length */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003131 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine656896e2018-06-29 19:12:28 +02003132 NULL,
3133 &key_bits ) == PSA_SUCCESS );
3134 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003135 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003136
3137 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003138 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003139 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003140 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003141 output, output_size,
3142 &output_length );
3143 TEST_ASSERT( actual_status == expected_status );
3144 TEST_ASSERT( output_length == expected_output_length );
3145
Gilles Peskine68428122018-06-30 18:42:41 +02003146 /* If the label is empty, the test framework puts a non-null pointer
3147 * in label->x. Test that a null pointer works as well. */
3148 if( label->len == 0 )
3149 {
3150 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003151 if( output_size != 0 )
3152 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003153 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003154 input_data->x, input_data->len,
3155 NULL, label->len,
3156 output, output_size,
3157 &output_length );
3158 TEST_ASSERT( actual_status == expected_status );
3159 TEST_ASSERT( output_length == expected_output_length );
3160 }
3161
Gilles Peskine656896e2018-06-29 19:12:28 +02003162exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003163 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003164 mbedtls_free( output );
3165 mbedtls_psa_crypto_free( );
3166}
3167/* END_CASE */
3168
3169/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003170void asymmetric_encrypt_decrypt( int key_type_arg,
3171 data_t *key_data,
3172 int alg_arg,
3173 data_t *input_data,
3174 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003175{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003176 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177 psa_key_type_t key_type = key_type_arg;
3178 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003179 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003181 size_t output_size;
3182 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003183 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003184 size_t output2_size;
3185 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003186 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003188 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003189 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003190 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3191 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3192
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003193 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3194
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003195 TEST_ASSERT( psa_allocate_key( key_type,
3196 KEY_BITS_FROM_DATA( key_type, key_data ),
3197 &handle ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003198 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003199 psa_key_policy_set_usage( &policy,
3200 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003201 alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003202 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003203
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003204 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003205 key_data->x,
3206 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003207
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003208
3209 /* Determine the maximum ciphertext length */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003210 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003211 NULL,
3212 &key_bits ) == PSA_SUCCESS );
3213 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003214 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003215 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003216 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003217
Gilles Peskineeebd7382018-06-08 18:11:54 +02003218 /* We test encryption by checking that encrypt-then-decrypt gives back
3219 * the original plaintext because of the non-optional random
3220 * part of encryption process which prevents using fixed vectors. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003221 TEST_ASSERT( psa_asymmetric_encrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003222 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003223 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003224 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003225 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003226 /* We don't know what ciphertext length to expect, but check that
3227 * it looks sensible. */
3228 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003229
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003230 TEST_ASSERT( psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003231 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02003232 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003233 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003234 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003235 ASSERT_COMPARE( input_data->x, input_data->len,
3236 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237
3238exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003239 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003240 mbedtls_free( output );
3241 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003243}
3244/* END_CASE */
3245
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003246/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003247void asymmetric_decrypt( int key_type_arg,
3248 data_t *key_data,
3249 int alg_arg,
3250 data_t *input_data,
3251 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003252 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003254 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003255 psa_key_type_t key_type = key_type_arg;
3256 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003258 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003259 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003260 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003261
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003263 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003264 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003265 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3266 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3267 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3268
Gilles Peskine4abf7412018-06-18 16:35:34 +02003269 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003270 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003271
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003272 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3273
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003274 TEST_ASSERT( psa_allocate_key( key_type,
3275 KEY_BITS_FROM_DATA( key_type, key_data ),
3276 &handle ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003277 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003278 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003279 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003280
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003281 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003282 key_data->x,
3283 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003284
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003285 TEST_ASSERT( psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003286 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003287 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003288 output,
3289 output_size,
3290 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003291 ASSERT_COMPARE( expected_data->x, expected_data->len,
3292 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003293
Gilles Peskine68428122018-06-30 18:42:41 +02003294 /* If the label is empty, the test framework puts a non-null pointer
3295 * in label->x. Test that a null pointer works as well. */
3296 if( label->len == 0 )
3297 {
3298 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003299 if( output_size != 0 )
3300 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003301 TEST_ASSERT( psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003302 input_data->x, input_data->len,
3303 NULL, label->len,
3304 output,
3305 output_size,
3306 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003307 ASSERT_COMPARE( expected_data->x, expected_data->len,
3308 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003309 }
3310
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003311exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003312 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003313 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003314 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315}
3316/* END_CASE */
3317
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003318/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003319void asymmetric_decrypt_fail( int key_type_arg,
3320 data_t *key_data,
3321 int alg_arg,
3322 data_t *input_data,
3323 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003324 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003325{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003326 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003327 psa_key_type_t key_type = key_type_arg;
3328 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003330 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003331 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003332 psa_status_t actual_status;
3333 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003334 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003335
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003336 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003337 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003338 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3339 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3340
Gilles Peskine4abf7412018-06-18 16:35:34 +02003341 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003342 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003343
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003344 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3345
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003346 TEST_ASSERT( psa_allocate_key( key_type,
3347 KEY_BITS_FROM_DATA( key_type, key_data ),
3348 &handle ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003349 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003350 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003351 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003352
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003353 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003354 key_data->x,
3355 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003356
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003357 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003358 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003359 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003360 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003361 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003362 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003363 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003364
Gilles Peskine68428122018-06-30 18:42:41 +02003365 /* If the label is empty, the test framework puts a non-null pointer
3366 * in label->x. Test that a null pointer works as well. */
3367 if( label->len == 0 )
3368 {
3369 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003370 if( output_size != 0 )
3371 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003372 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003373 input_data->x, input_data->len,
3374 NULL, label->len,
3375 output, output_size,
3376 &output_length );
3377 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003378 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003379 }
3380
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003381exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003382 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003383 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003384 mbedtls_psa_crypto_free( );
3385}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003386/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003387
3388/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003389void derive_setup( int key_type_arg,
3390 data_t *key_data,
3391 int alg_arg,
3392 data_t *salt,
3393 data_t *label,
3394 int requested_capacity_arg,
3395 int expected_status_arg )
3396{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003397 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003398 size_t key_type = key_type_arg;
3399 psa_algorithm_t alg = alg_arg;
3400 size_t requested_capacity = requested_capacity_arg;
3401 psa_status_t expected_status = expected_status_arg;
3402 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3403 psa_key_policy_t policy;
3404
3405 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3406
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003407 TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3408 &handle ) == PSA_SUCCESS );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003409 psa_key_policy_init( &policy );
3410 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003411 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003412
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003413 TEST_ASSERT( psa_import_key( handle, key_type,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003414 key_data->x,
3415 key_data->len ) == PSA_SUCCESS );
3416
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003417 TEST_ASSERT( psa_key_derivation( &generator, handle, alg,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003418 salt->x, salt->len,
3419 label->x, label->len,
3420 requested_capacity ) == expected_status );
3421
3422exit:
3423 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003424 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003425 mbedtls_psa_crypto_free( );
3426}
3427/* END_CASE */
3428
3429/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003430void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003431{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003432 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003433 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003434 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003435 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003436 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003437 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003438 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3439 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3440 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003441 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003442
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003443 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3444
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003445 TEST_ASSERT( psa_allocate_key( key_type,
3446 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3447 &handle ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003448 psa_key_policy_init( &policy );
3449 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003450 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003451
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003452 TEST_ASSERT( psa_import_key( handle, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003453 key_data,
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003454 sizeof( key_data ) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003455
3456 /* valid key derivation */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003457 TEST_ASSERT( psa_key_derivation( &generator, handle, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003458 NULL, 0,
3459 NULL, 0,
3460 capacity ) == PSA_SUCCESS );
3461
3462 /* state of generator shouldn't allow additional generation */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003463 TEST_ASSERT( psa_key_derivation( &generator, handle, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003464 NULL, 0,
3465 NULL, 0,
3466 capacity ) == PSA_ERROR_BAD_STATE );
3467
3468 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3469 == PSA_SUCCESS );
3470
3471 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3472 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3473
3474
3475exit:
3476 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003477 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003478 mbedtls_psa_crypto_free( );
3479}
3480/* END_CASE */
3481
3482
3483/* BEGIN_CASE */
3484void test_derive_invalid_generator_tests( )
3485{
3486 uint8_t output_buffer[16];
3487 size_t buffer_size = 16;
3488 size_t capacity = 0;
3489 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3490
Nir Sonnenschein50789302018-10-31 12:16:38 +02003491 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003492 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003493
3494 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003495 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003496
Nir Sonnenschein50789302018-10-31 12:16:38 +02003497 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003498
Nir Sonnenschein50789302018-10-31 12:16:38 +02003499 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003500 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003501
Nir Sonnenschein50789302018-10-31 12:16:38 +02003502 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003503 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003504
3505exit:
3506 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003507}
3508/* END_CASE */
3509
3510/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003511void derive_output( int alg_arg,
3512 data_t *key_data,
3513 data_t *salt,
3514 data_t *label,
3515 int requested_capacity_arg,
3516 data_t *expected_output1,
3517 data_t *expected_output2 )
3518{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003519 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003520 psa_algorithm_t alg = alg_arg;
3521 size_t requested_capacity = requested_capacity_arg;
3522 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3523 uint8_t *expected_outputs[2] =
3524 {expected_output1->x, expected_output2->x};
3525 size_t output_sizes[2] =
3526 {expected_output1->len, expected_output2->len};
3527 size_t output_buffer_size = 0;
3528 uint8_t *output_buffer = NULL;
3529 size_t expected_capacity;
3530 size_t current_capacity;
3531 psa_key_policy_t policy;
3532 psa_status_t status;
3533 unsigned i;
3534
3535 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3536 {
3537 if( output_sizes[i] > output_buffer_size )
3538 output_buffer_size = output_sizes[i];
3539 if( output_sizes[i] == 0 )
3540 expected_outputs[i] = NULL;
3541 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003542 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003543 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3544
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003545 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3546 PSA_BYTES_TO_BITS( key_data->len ),
3547 &handle ) == PSA_SUCCESS );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003548 psa_key_policy_init( &policy );
3549 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003550 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003551
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003552 TEST_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003553 key_data->x,
3554 key_data->len ) == PSA_SUCCESS );
3555
3556 /* Extraction phase. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003557 TEST_ASSERT( psa_key_derivation( &generator, handle, alg,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003558 salt->x, salt->len,
3559 label->x, label->len,
3560 requested_capacity ) == PSA_SUCCESS );
3561 TEST_ASSERT( psa_get_generator_capacity( &generator,
3562 &current_capacity ) ==
3563 PSA_SUCCESS );
3564 TEST_ASSERT( current_capacity == requested_capacity );
3565 expected_capacity = requested_capacity;
3566
3567 /* Expansion phase. */
3568 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3569 {
3570 /* Read some bytes. */
3571 status = psa_generator_read( &generator,
3572 output_buffer, output_sizes[i] );
3573 if( expected_capacity == 0 && output_sizes[i] == 0 )
3574 {
3575 /* Reading 0 bytes when 0 bytes are available can go either way. */
3576 TEST_ASSERT( status == PSA_SUCCESS ||
3577 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3578 continue;
3579 }
3580 else if( expected_capacity == 0 ||
3581 output_sizes[i] > expected_capacity )
3582 {
3583 /* Capacity exceeded. */
3584 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3585 expected_capacity = 0;
3586 continue;
3587 }
3588 /* Success. Check the read data. */
3589 TEST_ASSERT( status == PSA_SUCCESS );
3590 if( output_sizes[i] != 0 )
3591 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3592 output_sizes[i] ) == 0 );
3593 /* Check the generator status. */
3594 expected_capacity -= output_sizes[i];
3595 TEST_ASSERT( psa_get_generator_capacity( &generator,
3596 &current_capacity ) ==
3597 PSA_SUCCESS );
3598 TEST_ASSERT( expected_capacity == current_capacity );
3599 }
3600 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3601
3602exit:
3603 mbedtls_free( output_buffer );
3604 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003605 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003606 mbedtls_psa_crypto_free( );
3607}
3608/* END_CASE */
3609
3610/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003611void derive_full( int alg_arg,
3612 data_t *key_data,
3613 data_t *salt,
3614 data_t *label,
3615 int requested_capacity_arg )
3616{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003617 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003618 psa_algorithm_t alg = alg_arg;
3619 size_t requested_capacity = requested_capacity_arg;
3620 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3621 unsigned char output_buffer[16];
3622 size_t expected_capacity = requested_capacity;
3623 size_t current_capacity;
3624 psa_key_policy_t policy;
3625
3626 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3627
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003628 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3629 PSA_BYTES_TO_BITS( key_data->len ),
3630 &handle ) == PSA_SUCCESS );
Gilles Peskined54931c2018-07-17 21:06:59 +02003631 psa_key_policy_init( &policy );
3632 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003633 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskined54931c2018-07-17 21:06:59 +02003634
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003635 TEST_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
Gilles Peskined54931c2018-07-17 21:06:59 +02003636 key_data->x,
3637 key_data->len ) == PSA_SUCCESS );
3638
3639 /* Extraction phase. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003640 TEST_ASSERT( psa_key_derivation( &generator, handle, alg,
Gilles Peskined54931c2018-07-17 21:06:59 +02003641 salt->x, salt->len,
3642 label->x, label->len,
3643 requested_capacity ) == PSA_SUCCESS );
3644 TEST_ASSERT( psa_get_generator_capacity( &generator,
3645 &current_capacity ) ==
3646 PSA_SUCCESS );
3647 TEST_ASSERT( current_capacity == expected_capacity );
3648
3649 /* Expansion phase. */
3650 while( current_capacity > 0 )
3651 {
3652 size_t read_size = sizeof( output_buffer );
3653 if( read_size > current_capacity )
3654 read_size = current_capacity;
3655 TEST_ASSERT( psa_generator_read( &generator,
3656 output_buffer,
3657 read_size ) == PSA_SUCCESS );
3658 expected_capacity -= read_size;
3659 TEST_ASSERT( psa_get_generator_capacity( &generator,
3660 &current_capacity ) ==
3661 PSA_SUCCESS );
3662 TEST_ASSERT( current_capacity == expected_capacity );
3663 }
3664
3665 /* Check that the generator refuses to go over capacity. */
3666 TEST_ASSERT( psa_generator_read( &generator,
3667 output_buffer,
3668 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3669
3670 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3671
3672exit:
3673 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003674 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003675 mbedtls_psa_crypto_free( );
3676}
3677/* END_CASE */
3678
3679/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003680void derive_key_exercise( int alg_arg,
3681 data_t *key_data,
3682 data_t *salt,
3683 data_t *label,
3684 int derived_type_arg,
3685 int derived_bits_arg,
3686 int derived_usage_arg,
3687 int derived_alg_arg )
3688{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003689 psa_key_handle_t base_handle = 0;
3690 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003691 psa_algorithm_t alg = alg_arg;
3692 psa_key_type_t derived_type = derived_type_arg;
3693 size_t derived_bits = derived_bits_arg;
3694 psa_key_usage_t derived_usage = derived_usage_arg;
3695 psa_algorithm_t derived_alg = derived_alg_arg;
3696 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3697 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3698 psa_key_policy_t policy;
3699 psa_key_type_t got_type;
3700 size_t got_bits;
3701
3702 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3703
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003704 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3705 PSA_BYTES_TO_BITS( key_data->len ),
3706 &base_handle ) == PSA_SUCCESS );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003707 psa_key_policy_init( &policy );
3708 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003709 TEST_ASSERT( psa_set_key_policy( base_handle, &policy ) == PSA_SUCCESS );
3710 TEST_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003711 key_data->x,
3712 key_data->len ) == PSA_SUCCESS );
3713
3714 /* Derive a key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003715 TEST_ASSERT( psa_key_derivation( &generator, base_handle, alg,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003716 salt->x, salt->len,
3717 label->x, label->len,
3718 capacity ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003719 TEST_ASSERT( psa_allocate_key( derived_type, derived_bits,
3720 &derived_handle ) == PSA_SUCCESS );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003721 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003722 TEST_ASSERT( psa_set_key_policy( derived_handle, &policy ) == PSA_SUCCESS );
3723 TEST_ASSERT( psa_generator_import_key( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003724 derived_type,
3725 derived_bits,
3726 &generator ) == PSA_SUCCESS );
3727
3728 /* Test the key information */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003729 TEST_ASSERT( psa_get_key_information( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003730 &got_type,
3731 &got_bits ) == PSA_SUCCESS );
3732 TEST_ASSERT( got_type == derived_type );
3733 TEST_ASSERT( got_bits == derived_bits );
3734
3735 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003736 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003737 goto exit;
3738
3739exit:
3740 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003741 psa_destroy_key( base_handle );
3742 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003743 mbedtls_psa_crypto_free( );
3744}
3745/* END_CASE */
3746
3747/* BEGIN_CASE */
3748void derive_key_export( int alg_arg,
3749 data_t *key_data,
3750 data_t *salt,
3751 data_t *label,
3752 int bytes1_arg,
3753 int bytes2_arg )
3754{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003755 psa_key_handle_t base_handle = 0;
3756 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003757 psa_algorithm_t alg = alg_arg;
3758 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003759 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003760 size_t bytes2 = bytes2_arg;
3761 size_t capacity = bytes1 + bytes2;
3762 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003763 uint8_t *output_buffer = NULL;
3764 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003765 psa_key_policy_t policy;
3766 size_t length;
3767
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003768 ASSERT_ALLOC( output_buffer, capacity );
3769 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003770 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3771
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003772 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3773 PSA_BYTES_TO_BITS( key_data->len ),
3774 &base_handle ) == PSA_SUCCESS );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003775 psa_key_policy_init( &policy );
3776 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003777 TEST_ASSERT( psa_set_key_policy( base_handle, &policy ) == PSA_SUCCESS );
3778 TEST_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003779 key_data->x,
3780 key_data->len ) == PSA_SUCCESS );
3781
3782 /* Derive some material and output it. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003783 TEST_ASSERT( psa_key_derivation( &generator, base_handle, alg,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003784 salt->x, salt->len,
3785 label->x, label->len,
3786 capacity ) == PSA_SUCCESS );
3787 TEST_ASSERT( psa_generator_read( &generator,
3788 output_buffer,
3789 capacity ) == PSA_SUCCESS );
3790 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3791
3792 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003793 TEST_ASSERT( psa_key_derivation( &generator, base_handle, alg,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003794 salt->x, salt->len,
3795 label->x, label->len,
3796 capacity ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003797 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3798 &derived_handle ) == PSA_SUCCESS );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003799 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003800 TEST_ASSERT( psa_set_key_policy( derived_handle, &policy ) == PSA_SUCCESS );
3801 TEST_ASSERT( psa_generator_import_key( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003802 PSA_KEY_TYPE_RAW_DATA,
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003803 derived_bits,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003804 &generator ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003805 TEST_ASSERT( psa_export_key( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003806 export_buffer, bytes1,
3807 &length ) == PSA_SUCCESS );
3808 TEST_ASSERT( length == bytes1 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003809 TEST_ASSERT( psa_destroy_key( derived_handle ) == PSA_SUCCESS );
3810 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3811 PSA_BYTES_TO_BITS( bytes2 ),
3812 &derived_handle ) == PSA_SUCCESS );
3813 TEST_ASSERT( psa_set_key_policy( derived_handle, &policy ) == PSA_SUCCESS );
3814 TEST_ASSERT( psa_generator_import_key( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003815 PSA_KEY_TYPE_RAW_DATA,
3816 PSA_BYTES_TO_BITS( bytes2 ),
3817 &generator ) == PSA_SUCCESS );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003818 TEST_ASSERT( psa_export_key( derived_handle,
Gilles Peskine0386fba2018-07-12 17:29:22 +02003819 export_buffer + bytes1, bytes2,
3820 &length ) == PSA_SUCCESS );
3821 TEST_ASSERT( length == bytes2 );
3822
3823 /* Compare the outputs from the two runs. */
3824 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3825
3826exit:
3827 mbedtls_free( output_buffer );
3828 mbedtls_free( export_buffer );
3829 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003830 psa_destroy_key( base_handle );
3831 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003832 mbedtls_psa_crypto_free( );
3833}
3834/* END_CASE */
3835
3836/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003837void key_agreement_setup( int alg_arg,
3838 int our_key_type_arg, data_t *our_key_data,
3839 data_t *peer_key_data,
3840 int expected_status_arg )
3841{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003842 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003843 psa_algorithm_t alg = alg_arg;
3844 psa_key_type_t our_key_type = our_key_type_arg;
3845 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3846 psa_key_policy_t policy;
3847
3848 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3849
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003850 TEST_ASSERT( psa_allocate_key( our_key_type,
3851 KEY_BITS_FROM_DATA( our_key_type,
3852 our_key_data ),
3853 &our_key ) == PSA_SUCCESS );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003854 psa_key_policy_init( &policy );
3855 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3856 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3857 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3858 our_key_data->x,
3859 our_key_data->len ) == PSA_SUCCESS );
3860
3861 TEST_ASSERT( psa_key_agreement( &generator,
3862 our_key,
3863 peer_key_data->x, peer_key_data->len,
3864 alg ) == expected_status_arg );
3865
3866exit:
3867 psa_generator_abort( &generator );
3868 psa_destroy_key( our_key );
3869 mbedtls_psa_crypto_free( );
3870}
3871/* END_CASE */
3872
3873/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003874void key_agreement_capacity( int alg_arg,
3875 int our_key_type_arg, data_t *our_key_data,
3876 data_t *peer_key_data,
3877 int expected_capacity_arg )
3878{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003879 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003880 psa_algorithm_t alg = alg_arg;
3881 psa_key_type_t our_key_type = our_key_type_arg;
3882 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3883 psa_key_policy_t policy;
3884 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003885 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003886
3887 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3888
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003889 TEST_ASSERT( psa_allocate_key( our_key_type,
3890 KEY_BITS_FROM_DATA( our_key_type,
3891 our_key_data ),
3892 &our_key ) == PSA_SUCCESS );
Gilles Peskine59685592018-09-18 12:11:34 +02003893 psa_key_policy_init( &policy );
3894 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3895 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3896 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3897 our_key_data->x,
3898 our_key_data->len ) == PSA_SUCCESS );
3899
3900 TEST_ASSERT( psa_key_agreement( &generator,
3901 our_key,
3902 peer_key_data->x, peer_key_data->len,
3903 alg ) == PSA_SUCCESS );
3904
Gilles Peskinebf491972018-10-25 22:36:12 +02003905 /* Test the advertized capacity. */
Gilles Peskine59685592018-09-18 12:11:34 +02003906 TEST_ASSERT( psa_get_generator_capacity(
3907 &generator, &actual_capacity ) == PSA_SUCCESS );
3908 TEST_ASSERT( actual_capacity == (size_t) expected_capacity_arg );
3909
Gilles Peskinebf491972018-10-25 22:36:12 +02003910 /* Test the actual capacity by reading the output. */
3911 while( actual_capacity > sizeof( output ) )
3912 {
3913 TEST_ASSERT( psa_generator_read( &generator,
3914 output, sizeof( output ) ) ==
3915 PSA_SUCCESS );
3916 actual_capacity -= sizeof( output );
3917 }
3918 TEST_ASSERT( psa_generator_read( &generator,
3919 output, actual_capacity ) ==
3920 PSA_SUCCESS );
3921 TEST_ASSERT( psa_generator_read( &generator, output, 1 ) ==
3922 PSA_ERROR_INSUFFICIENT_CAPACITY );
3923
Gilles Peskine59685592018-09-18 12:11:34 +02003924exit:
3925 psa_generator_abort( &generator );
3926 psa_destroy_key( our_key );
3927 mbedtls_psa_crypto_free( );
3928}
3929/* END_CASE */
3930
3931/* BEGIN_CASE */
3932void key_agreement_output( int alg_arg,
3933 int our_key_type_arg, data_t *our_key_data,
3934 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003935 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003936{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003937 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003938 psa_algorithm_t alg = alg_arg;
3939 psa_key_type_t our_key_type = our_key_type_arg;
3940 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3941 psa_key_policy_t policy;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003942 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003943
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003944 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3945 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003946
3947 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3948
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003949 TEST_ASSERT( psa_allocate_key( our_key_type,
3950 KEY_BITS_FROM_DATA( our_key_type,
3951 our_key_data ),
3952 &our_key ) == PSA_SUCCESS );
Gilles Peskine59685592018-09-18 12:11:34 +02003953 psa_key_policy_init( &policy );
3954 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3955 TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS );
3956 TEST_ASSERT( psa_import_key( our_key, our_key_type,
3957 our_key_data->x,
3958 our_key_data->len ) == PSA_SUCCESS );
3959
3960 TEST_ASSERT( psa_key_agreement( &generator,
3961 our_key,
3962 peer_key_data->x, peer_key_data->len,
3963 alg ) == PSA_SUCCESS );
3964
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003965 TEST_ASSERT(
3966 psa_generator_read( &generator,
3967 actual_output,
3968 expected_output1->len ) == PSA_SUCCESS );
3969 TEST_ASSERT( memcmp( actual_output, expected_output1->x,
3970 expected_output1->len ) == 0 );
3971 if( expected_output2->len != 0 )
3972 {
3973 TEST_ASSERT(
3974 psa_generator_read( &generator,
3975 actual_output,
3976 expected_output2->len ) == PSA_SUCCESS );
3977 TEST_ASSERT( memcmp( actual_output, expected_output2->x,
3978 expected_output2->len ) == 0 );
3979 }
Gilles Peskine59685592018-09-18 12:11:34 +02003980
3981exit:
3982 psa_generator_abort( &generator );
3983 psa_destroy_key( our_key );
3984 mbedtls_psa_crypto_free( );
3985 mbedtls_free( actual_output );
3986}
3987/* END_CASE */
3988
3989/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003990void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003991{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003992 size_t bytes = bytes_arg;
3993 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003994 unsigned char *output = NULL;
3995 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003996 size_t i;
3997 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003998
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003999 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4000 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004001 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004002
4003 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
4004
Gilles Peskinea50d7392018-06-21 10:22:13 +02004005 /* Run several times, to ensure that every output byte will be
4006 * nonzero at least once with overwhelming probability
4007 * (2^(-8*number_of_runs)). */
4008 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004009 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004010 if( bytes != 0 )
4011 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004012 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
4013
4014 /* Check that no more than bytes have been overwritten */
4015 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
4016
4017 for( i = 0; i < bytes; i++ )
4018 {
4019 if( output[i] != 0 )
4020 ++changed[i];
4021 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004022 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004023
4024 /* Check that every byte was changed to nonzero at least once. This
4025 * validates that psa_generate_random is overwriting every byte of
4026 * the output buffer. */
4027 for( i = 0; i < bytes; i++ )
4028 {
4029 TEST_ASSERT( changed[i] != 0 );
4030 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004031
4032exit:
4033 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004034 mbedtls_free( output );
4035 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004036}
4037/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004038
4039/* BEGIN_CASE */
4040void generate_key( int type_arg,
4041 int bits_arg,
4042 int usage_arg,
4043 int alg_arg,
4044 int expected_status_arg )
4045{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004046 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004047 psa_key_type_t type = type_arg;
4048 psa_key_usage_t usage = usage_arg;
4049 size_t bits = bits_arg;
4050 psa_algorithm_t alg = alg_arg;
4051 psa_status_t expected_status = expected_status_arg;
4052 psa_key_type_t got_type;
4053 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004054 psa_status_t expected_info_status =
4055 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
4056 psa_key_policy_t policy;
4057
4058 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
4059
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004060 TEST_ASSERT( psa_allocate_key( type, bits, &handle ) == PSA_SUCCESS );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004061 psa_key_policy_init( &policy );
4062 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004063 TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004064
4065 /* Generate a key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004066 TEST_ASSERT( psa_generate_key( handle, type, bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004067 NULL, 0 ) == expected_status );
4068
4069 /* Test the key information */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004070 TEST_ASSERT( psa_get_key_information( handle,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004071 &got_type,
4072 &got_bits ) == expected_info_status );
4073 if( expected_info_status != PSA_SUCCESS )
4074 goto exit;
4075 TEST_ASSERT( got_type == type );
4076 TEST_ASSERT( got_bits == bits );
4077
Gilles Peskine818ca122018-06-20 18:16:48 +02004078 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004079 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004080 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004081
4082exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004083 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004084 mbedtls_psa_crypto_free( );
4085}
4086/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004087
Darryl Greend49a4992018-06-18 17:27:26 +01004088/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4089void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4090 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004091 int alg_arg, int generation_method,
4092 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004093{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004094 psa_key_handle_t handle = 0;
4095 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004096 psa_key_type_t type = (psa_key_type_t) type_arg;
4097 psa_key_type_t type_get;
4098 size_t bits_get;
4099 psa_key_policy_t policy_set;
4100 psa_key_policy_t policy_get;
4101 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4102 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004103 psa_key_policy_t base_policy_set;
4104 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4105 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004106 unsigned char *first_export = NULL;
4107 unsigned char *second_export = NULL;
4108 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4109 size_t first_exported_length;
4110 size_t second_exported_length;
4111
4112 ASSERT_ALLOC( first_export, export_size );
4113 ASSERT_ALLOC( second_export, export_size );
4114
4115 TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS );
4116
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004117 TEST_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4118 type, bits,
4119 &handle ) == PSA_SUCCESS );
Darryl Greend49a4992018-06-18 17:27:26 +01004120 psa_key_policy_init( &policy_set );
Darryl Greend49a4992018-06-18 17:27:26 +01004121 psa_key_policy_set_usage( &policy_set, policy_usage,
4122 policy_alg );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004123 TEST_ASSERT( psa_set_key_policy( handle, &policy_set ) == PSA_SUCCESS );
Darryl Greend49a4992018-06-18 17:27:26 +01004124
Darryl Green0c6575a2018-11-07 16:05:30 +00004125 switch( generation_method )
4126 {
4127 case IMPORT_KEY:
4128 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004129 TEST_ASSERT( psa_import_key( handle, type,
Darryl Green0c6575a2018-11-07 16:05:30 +00004130 data->x, data->len ) == PSA_SUCCESS );
4131 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004132
Darryl Green0c6575a2018-11-07 16:05:30 +00004133 case GENERATE_KEY:
4134 /* Generate a key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004135 TEST_ASSERT( psa_generate_key( handle, type, bits,
Darryl Green0c6575a2018-11-07 16:05:30 +00004136 NULL, 0 ) == PSA_SUCCESS );
4137 break;
4138
4139 case DERIVE_KEY:
4140 /* Create base key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004141 TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
4142 PSA_BYTES_TO_BITS( data->len ),
4143 &base_key ) == PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00004144 psa_key_policy_init( &base_policy_set );
Darryl Green0c6575a2018-11-07 16:05:30 +00004145 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4146 base_policy_alg );
4147 TEST_ASSERT( psa_set_key_policy(
4148 base_key, &base_policy_set ) == PSA_SUCCESS );
4149 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4150 data->x, data->len ) == PSA_SUCCESS );
4151 /* Derive a key. */
4152 TEST_ASSERT( psa_key_derivation( &generator, base_key,
4153 base_policy_alg,
4154 NULL, 0, NULL, 0,
4155 export_size ) == PSA_SUCCESS );
4156 TEST_ASSERT( psa_generator_import_key(
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004157 handle, PSA_KEY_TYPE_RAW_DATA,
Darryl Green0c6575a2018-11-07 16:05:30 +00004158 bits, &generator ) == PSA_SUCCESS );
4159 break;
4160 }
Darryl Greend49a4992018-06-18 17:27:26 +01004161
4162 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004163 TEST_ASSERT( psa_export_key( handle, first_export, export_size,
Darryl Green0c6575a2018-11-07 16:05:30 +00004164 &first_exported_length ) == export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004165
4166 /* Shutdown and restart */
4167 mbedtls_psa_crypto_free();
Darryl Greend49a4992018-06-18 17:27:26 +01004168 TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS );
4169
Darryl Greend49a4992018-06-18 17:27:26 +01004170 /* Check key slot still contains key data */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004171 TEST_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4172 &handle ) == PSA_SUCCESS );
Darryl Greend49a4992018-06-18 17:27:26 +01004173 TEST_ASSERT( psa_get_key_information(
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004174 handle, &type_get, &bits_get ) == PSA_SUCCESS );
Darryl Greend49a4992018-06-18 17:27:26 +01004175 TEST_ASSERT( type_get == type );
4176 TEST_ASSERT( bits_get == (size_t) bits );
4177
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004178 TEST_ASSERT( psa_get_key_policy( handle, &policy_get ) == PSA_SUCCESS );
Darryl Greend49a4992018-06-18 17:27:26 +01004179 TEST_ASSERT( psa_key_policy_get_usage(
4180 &policy_get ) == policy_usage );
4181 TEST_ASSERT( psa_key_policy_get_algorithm(
4182 &policy_get ) == policy_alg );
4183
4184 /* Export the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004185 TEST_ASSERT( psa_export_key( handle, second_export, export_size,
Darryl Green0c6575a2018-11-07 16:05:30 +00004186 &second_exported_length ) == export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004187
Darryl Green0c6575a2018-11-07 16:05:30 +00004188 if( export_status == PSA_SUCCESS )
4189 {
4190 ASSERT_COMPARE( first_export, first_exported_length,
4191 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004192
Darryl Green0c6575a2018-11-07 16:05:30 +00004193 switch( generation_method )
4194 {
4195 case IMPORT_KEY:
4196 ASSERT_COMPARE( data->x, data->len,
4197 first_export, first_exported_length );
4198 break;
4199 default:
4200 break;
4201 }
4202 }
4203
4204 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004205 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004206 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004207
4208exit:
4209 mbedtls_free( first_export );
4210 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004211 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004212 mbedtls_psa_crypto_free();
4213}
4214/* END_CASE */