blob: 3e957b8f13b68f5d71c06c88d4bd5d6bb2b04a40 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskinefc411f12018-10-25 22:34:48 +020014#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
15
itayzafrir3e02b3b2018-06-12 17:06:52 +030016#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020017#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#else
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100131static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200132 psa_key_usage_t usage,
133 psa_algorithm_t alg )
134{
135 psa_mac_operation_t operation;
136 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200137 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200138 size_t mac_length = sizeof( mac );
139
140 if( usage & PSA_KEY_USAGE_SIGN )
141 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100142 PSA_ASSERT( psa_mac_sign_setup( &operation,
143 handle, alg ) );
144 PSA_ASSERT( psa_mac_update( &operation,
145 input, sizeof( input ) ) );
146 PSA_ASSERT( psa_mac_sign_finish( &operation,
147 mac, sizeof( mac ),
148 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200149 }
150
151 if( usage & PSA_KEY_USAGE_VERIFY )
152 {
153 psa_status_t verify_status =
154 ( usage & PSA_KEY_USAGE_SIGN ?
155 PSA_SUCCESS :
156 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100157 PSA_ASSERT( psa_mac_verify_setup( &operation,
158 handle, alg ) );
159 PSA_ASSERT( psa_mac_update( &operation,
160 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100161 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
162 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200163 }
164
165 return( 1 );
166
167exit:
168 psa_mac_abort( &operation );
169 return( 0 );
170}
171
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100172static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200173 psa_key_usage_t usage,
174 psa_algorithm_t alg )
175{
176 psa_cipher_operation_t operation;
177 unsigned char iv[16] = {0};
178 size_t iv_length = sizeof( iv );
179 const unsigned char plaintext[16] = "Hello, world...";
180 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
181 size_t ciphertext_length = sizeof( ciphertext );
182 unsigned char decrypted[sizeof( ciphertext )];
183 size_t part_length;
184
185 if( usage & PSA_KEY_USAGE_ENCRYPT )
186 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100187 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
188 handle, alg ) );
189 PSA_ASSERT( psa_cipher_generate_iv( &operation,
190 iv, sizeof( iv ),
191 &iv_length ) );
192 PSA_ASSERT( psa_cipher_update( &operation,
193 plaintext, sizeof( plaintext ),
194 ciphertext, sizeof( ciphertext ),
195 &ciphertext_length ) );
196 PSA_ASSERT( psa_cipher_finish( &operation,
197 ciphertext + ciphertext_length,
198 sizeof( ciphertext ) - ciphertext_length,
199 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200200 ciphertext_length += part_length;
201 }
202
203 if( usage & PSA_KEY_USAGE_DECRYPT )
204 {
205 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700206 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200207 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
208 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200209 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100210 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200211 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
212 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100213 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
214 handle, alg ) );
215 PSA_ASSERT( psa_cipher_set_iv( &operation,
216 iv, iv_length ) );
217 PSA_ASSERT( psa_cipher_update( &operation,
218 ciphertext, ciphertext_length,
219 decrypted, sizeof( decrypted ),
220 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200221 status = psa_cipher_finish( &operation,
222 decrypted + part_length,
223 sizeof( decrypted ) - part_length,
224 &part_length );
225 /* For a stream cipher, all inputs are valid. For a block cipher,
226 * if the input is some aribtrary data rather than an actual
227 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700228 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700229 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100230 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200231 else
232 TEST_ASSERT( status == PSA_SUCCESS ||
233 status == PSA_ERROR_INVALID_PADDING );
234 }
235
236 return( 1 );
237
238exit:
239 psa_cipher_abort( &operation );
240 return( 0 );
241}
242
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100243static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200244 psa_key_usage_t usage,
245 psa_algorithm_t alg )
246{
247 unsigned char nonce[16] = {0};
248 size_t nonce_length = sizeof( nonce );
249 unsigned char plaintext[16] = "Hello, world...";
250 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
251 size_t ciphertext_length = sizeof( ciphertext );
252 size_t plaintext_length = sizeof( ciphertext );
253
254 if( usage & PSA_KEY_USAGE_ENCRYPT )
255 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100256 PSA_ASSERT( psa_aead_encrypt( handle, alg,
257 nonce, nonce_length,
258 NULL, 0,
259 plaintext, sizeof( plaintext ),
260 ciphertext, sizeof( ciphertext ),
261 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200262 }
263
264 if( usage & PSA_KEY_USAGE_DECRYPT )
265 {
266 psa_status_t verify_status =
267 ( usage & PSA_KEY_USAGE_ENCRYPT ?
268 PSA_SUCCESS :
269 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100270 TEST_EQUAL( psa_aead_decrypt( handle, alg,
271 nonce, nonce_length,
272 NULL, 0,
273 ciphertext, ciphertext_length,
274 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100275 &plaintext_length ),
276 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200277 }
278
279 return( 1 );
280
281exit:
282 return( 0 );
283}
284
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100285static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200286 psa_key_usage_t usage,
287 psa_algorithm_t alg )
288{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200289 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
290 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200291 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 size_t signature_length = sizeof( signature );
293
294 if( usage & PSA_KEY_USAGE_SIGN )
295 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200296 /* Some algorithms require the payload to have the size of
297 * the hash encoded in the algorithm. Use this input size
298 * even for algorithms that allow other input sizes. */
299 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
300 if( hash_alg != 0 )
301 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100302 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
303 payload, payload_length,
304 signature, sizeof( signature ),
305 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 }
307
308 if( usage & PSA_KEY_USAGE_VERIFY )
309 {
310 psa_status_t verify_status =
311 ( usage & PSA_KEY_USAGE_SIGN ?
312 PSA_SUCCESS :
313 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100314 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
315 payload, payload_length,
316 signature, signature_length ),
317 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200318 }
319
320 return( 1 );
321
322exit:
323 return( 0 );
324}
325
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100326static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200327 psa_key_usage_t usage,
328 psa_algorithm_t alg )
329{
330 unsigned char plaintext[256] = "Hello, world...";
331 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
332 size_t ciphertext_length = sizeof( ciphertext );
333 size_t plaintext_length = 16;
334
335 if( usage & PSA_KEY_USAGE_ENCRYPT )
336 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100337 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
338 plaintext, plaintext_length,
339 NULL, 0,
340 ciphertext, sizeof( ciphertext ),
341 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200342 }
343
344 if( usage & PSA_KEY_USAGE_DECRYPT )
345 {
346 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100347 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200348 ciphertext, ciphertext_length,
349 NULL, 0,
350 plaintext, sizeof( plaintext ),
351 &plaintext_length );
352 TEST_ASSERT( status == PSA_SUCCESS ||
353 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
354 ( status == PSA_ERROR_INVALID_ARGUMENT ||
355 status == PSA_ERROR_INVALID_PADDING ) ) );
356 }
357
358 return( 1 );
359
360exit:
361 return( 0 );
362}
Gilles Peskine02b75072018-07-01 22:31:34 +0200363
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100364static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200365 psa_key_usage_t usage,
366 psa_algorithm_t alg )
367{
368 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
369 unsigned char label[16] = "This is a label.";
370 size_t label_length = sizeof( label );
371 unsigned char seed[16] = "abcdefghijklmnop";
372 size_t seed_length = sizeof( seed );
373 unsigned char output[1];
374
375 if( usage & PSA_KEY_USAGE_DERIVE )
376 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100377 PSA_ASSERT( psa_key_derivation( &generator,
378 handle, alg,
379 label, label_length,
380 seed, seed_length,
381 sizeof( output ) ) );
382 PSA_ASSERT( psa_generator_read( &generator,
383 output,
384 sizeof( output ) ) );
385 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200386 }
387
388 return( 1 );
389
390exit:
391 return( 0 );
392}
393
Gilles Peskinec7998b72018-11-07 18:45:02 +0100394/* We need two keys to exercise key agreement. Exercise the
395 * private key against its own public key. */
396static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100397 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100398 psa_algorithm_t alg )
399{
400 psa_key_type_t private_key_type;
401 psa_key_type_t public_key_type;
402 size_t key_bits;
403 uint8_t *public_key = NULL;
404 size_t public_key_length;
405 /* Return UNKNOWN_ERROR if something other than the final call to
406 * psa_key_agreement fails. This isn't fully satisfactory, but it's
407 * good enough: callers will report it as a failed test anyway. */
408 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
409
Gilles Peskine8817f612018-12-18 00:18:46 +0100410 PSA_ASSERT( psa_get_key_information( handle,
411 &private_key_type,
412 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100413 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
414 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
415 ASSERT_ALLOC( public_key, public_key_length );
416 TEST_ASSERT( public_key != NULL );
Gilles Peskine8817f612018-12-18 00:18:46 +0100417 PSA_ASSERT( psa_export_public_key( handle,
418 public_key, public_key_length,
419 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100420
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100421 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100422 public_key, public_key_length,
423 alg );
424exit:
425 mbedtls_free( public_key );
426 return( status );
427}
428
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100429static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200430 psa_key_usage_t usage,
431 psa_algorithm_t alg )
432{
433 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200434 unsigned char output[1];
435 int ok = 0;
436
437 if( usage & PSA_KEY_USAGE_DERIVE )
438 {
439 /* We need two keys to exercise key agreement. Exercise the
440 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100441 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
442 PSA_ASSERT( psa_generator_read( &generator,
443 output,
444 sizeof( output ) ) );
445 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200446 }
447 ok = 1;
448
449exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200450 return( ok );
451}
452
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200453static int is_oid_of_key_type( psa_key_type_t type,
454 const uint8_t *oid, size_t oid_length )
455{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200456 const uint8_t *expected_oid = NULL;
457 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200458#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200459 if( PSA_KEY_TYPE_IS_RSA( type ) )
460 {
461 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
462 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
463 }
464 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200465#endif /* MBEDTLS_RSA_C */
466#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200467 if( PSA_KEY_TYPE_IS_ECC( type ) )
468 {
469 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
470 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
471 }
472 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200473#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200474 {
475 char message[40];
476 mbedtls_snprintf( message, sizeof( message ),
477 "OID not known for key type=0x%08lx",
478 (unsigned long) type );
479 test_fail( message, __LINE__, __FILE__ );
480 return( 0 );
481 }
482
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200483 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200484 return( 1 );
485
486exit:
487 return( 0 );
488}
489
490static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
491 size_t min_bits, size_t max_bits,
492 int must_be_odd )
493{
494 size_t len;
495 size_t actual_bits;
496 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100497 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100498 MBEDTLS_ASN1_INTEGER ),
499 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200500 /* Tolerate a slight departure from DER encoding:
501 * - 0 may be represented by an empty string or a 1-byte string.
502 * - The sign bit may be used as a value bit. */
503 if( ( len == 1 && ( *p )[0] == 0 ) ||
504 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
505 {
506 ++( *p );
507 --len;
508 }
509 if( min_bits == 0 && len == 0 )
510 return( 1 );
511 msb = ( *p )[0];
512 TEST_ASSERT( msb != 0 );
513 actual_bits = 8 * ( len - 1 );
514 while( msb != 0 )
515 {
516 msb >>= 1;
517 ++actual_bits;
518 }
519 TEST_ASSERT( actual_bits >= min_bits );
520 TEST_ASSERT( actual_bits <= max_bits );
521 if( must_be_odd )
522 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
523 *p += len;
524 return( 1 );
525exit:
526 return( 0 );
527}
528
529static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
530 size_t *len,
531 unsigned char n, unsigned char tag )
532{
533 int ret;
534 ret = mbedtls_asn1_get_tag( p, end, len,
535 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
536 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
537 if( ret != 0 )
538 return( ret );
539 end = *p + *len;
540 ret = mbedtls_asn1_get_tag( p, end, len, tag );
541 if( ret != 0 )
542 return( ret );
543 if( *p + *len != end )
544 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
545 return( 0 );
546}
547
548static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
549 uint8_t *exported, size_t exported_length )
550{
551 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100552 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200553 else
554 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200555
556#if defined(MBEDTLS_DES_C)
557 if( type == PSA_KEY_TYPE_DES )
558 {
559 /* Check the parity bits. */
560 unsigned i;
561 for( i = 0; i < bits / 8; i++ )
562 {
563 unsigned bit_count = 0;
564 unsigned m;
565 for( m = 1; m <= 0x100; m <<= 1 )
566 {
567 if( exported[i] & m )
568 ++bit_count;
569 }
570 TEST_ASSERT( bit_count % 2 != 0 );
571 }
572 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200573 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200574#endif
575
576#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
577 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
578 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200579 uint8_t *p = exported;
580 uint8_t *end = exported + exported_length;
581 size_t len;
582 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200583 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200584 * modulus INTEGER, -- n
585 * publicExponent INTEGER, -- e
586 * privateExponent INTEGER, -- d
587 * prime1 INTEGER, -- p
588 * prime2 INTEGER, -- q
589 * exponent1 INTEGER, -- d mod (p-1)
590 * exponent2 INTEGER, -- d mod (q-1)
591 * coefficient INTEGER, -- (inverse of q) mod p
592 * }
593 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100594 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
595 MBEDTLS_ASN1_SEQUENCE |
596 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
597 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200598 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
599 goto exit;
600 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
601 goto exit;
602 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
603 goto exit;
604 /* Require d to be at least half the size of n. */
605 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
606 goto exit;
607 /* Require p and q to be at most half the size of n, rounded up. */
608 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
609 goto exit;
610 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
611 goto exit;
612 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
613 goto exit;
614 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
615 goto exit;
616 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
617 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100618 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100619 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200620 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200621#endif /* MBEDTLS_RSA_C */
622
623#if defined(MBEDTLS_ECP_C)
624 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
625 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100626 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100627 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100628 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200629 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200630#endif /* MBEDTLS_ECP_C */
631
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200632 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
633 {
634 uint8_t *p = exported;
635 uint8_t *end = exported + exported_length;
636 size_t len;
637 mbedtls_asn1_buf alg;
638 mbedtls_asn1_buf params;
639 mbedtls_asn1_bitstring bitstring;
640 /* SubjectPublicKeyInfo ::= SEQUENCE {
641 * algorithm AlgorithmIdentifier,
642 * subjectPublicKey BIT STRING }
643 * AlgorithmIdentifier ::= SEQUENCE {
644 * algorithm OBJECT IDENTIFIER,
645 * parameters ANY DEFINED BY algorithm OPTIONAL }
646 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100647 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
648 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100649 MBEDTLS_ASN1_CONSTRUCTED ),
650 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100651 TEST_EQUAL( p + len, end );
652 TEST_EQUAL( mbedtls_asn1_get_alg( &p, end, &alg, &params ), 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200653 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
654 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100655 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, end, &bitstring ), 0 );
656 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200657 p = bitstring.p;
658#if defined(MBEDTLS_RSA_C)
659 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
660 {
661 /* RSAPublicKey ::= SEQUENCE {
662 * modulus INTEGER, -- n
663 * publicExponent INTEGER } -- e
664 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100665 TEST_EQUAL( bitstring.unused_bits, 0 );
666 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
667 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100668 MBEDTLS_ASN1_CONSTRUCTED ),
669 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100670 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200671 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
672 goto exit;
673 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
674 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100675 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200676 }
677 else
678#endif /* MBEDTLS_RSA_C */
679#if defined(MBEDTLS_ECP_C)
680 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
681 {
682 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200683 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200684 * -- then x_P as an n-bit string, big endian;
685 * -- then y_P as a n-bit string, big endian,
686 * -- where n is the order of the curve.
687 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100688 TEST_EQUAL( bitstring.unused_bits, 0 );
689 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
690 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200691 }
692 else
693#endif /* MBEDTLS_ECP_C */
694 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100695 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200696 mbedtls_snprintf( message, sizeof( message ),
697 "No sanity check for public key type=0x%08lx",
698 (unsigned long) type );
699 test_fail( message, __LINE__, __FILE__ );
700 return( 0 );
701 }
702 }
703 else
704
705 {
706 /* No sanity checks for other types */
707 }
708
709 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200710
711exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200712 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200713}
714
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100715static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200716 psa_key_usage_t usage )
717{
718 psa_key_type_t type;
719 size_t bits;
720 uint8_t *exported = NULL;
721 size_t exported_size = 0;
722 size_t exported_length = 0;
723 int ok = 0;
724
Gilles Peskine8817f612018-12-18 00:18:46 +0100725 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200726
727 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
728 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200729 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100730 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
731 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200732 return( 1 );
733 }
734
Gilles Peskined14664a2018-08-10 19:07:32 +0200735 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200736 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200737
Gilles Peskine8817f612018-12-18 00:18:46 +0100738 PSA_ASSERT( psa_export_key( handle,
739 exported, exported_size,
740 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200741 ok = exported_key_sanity_check( type, bits, exported, exported_length );
742
743exit:
744 mbedtls_free( exported );
745 return( ok );
746}
747
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100748static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200749{
750 psa_key_type_t type;
751 psa_key_type_t public_type;
752 size_t bits;
753 uint8_t *exported = NULL;
754 size_t exported_size = 0;
755 size_t exported_length = 0;
756 int ok = 0;
757
Gilles Peskine8817f612018-12-18 00:18:46 +0100758 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200759 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
760 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100761 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100762 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200763 return( 1 );
764 }
765
766 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
767 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200768 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200769
Gilles Peskine8817f612018-12-18 00:18:46 +0100770 PSA_ASSERT( psa_export_public_key( handle,
771 exported, exported_size,
772 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200773 ok = exported_key_sanity_check( public_type, bits,
774 exported, exported_length );
775
776exit:
777 mbedtls_free( exported );
778 return( ok );
779}
780
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100781static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200782 psa_key_usage_t usage,
783 psa_algorithm_t alg )
784{
785 int ok;
786 if( alg == 0 )
787 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
788 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100789 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200790 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100791 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200792 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100793 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200794 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100795 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200796 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100797 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200798 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100799 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200800 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100801 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200802 else
803 {
804 char message[40];
805 mbedtls_snprintf( message, sizeof( message ),
806 "No code to exercise alg=0x%08lx",
807 (unsigned long) alg );
808 test_fail( message, __LINE__, __FILE__ );
809 ok = 0;
810 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200811
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100812 ok = ok && exercise_export_key( handle, usage );
813 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200814
Gilles Peskine02b75072018-07-01 22:31:34 +0200815 return( ok );
816}
817
Gilles Peskine10df3412018-10-25 22:35:43 +0200818static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
819 psa_algorithm_t alg )
820{
821 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
822 {
823 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
824 PSA_KEY_USAGE_VERIFY :
825 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
826 }
827 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
828 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
829 {
830 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
831 PSA_KEY_USAGE_ENCRYPT :
832 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
833 }
834 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
835 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
836 {
837 return( PSA_KEY_USAGE_DERIVE );
838 }
839 else
840 {
841 return( 0 );
842 }
843
844}
Darryl Green0c6575a2018-11-07 16:05:30 +0000845
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100846/* An overapproximation of the amount of storage needed for a key of the
847 * given type and with the given content. The API doesn't make it easy
848 * to find a good value for the size. The current implementation doesn't
849 * care about the value anyway. */
850#define KEY_BITS_FROM_DATA( type, data ) \
851 ( data )->len
852
Darryl Green0c6575a2018-11-07 16:05:30 +0000853typedef enum {
854 IMPORT_KEY = 0,
855 GENERATE_KEY = 1,
856 DERIVE_KEY = 2
857} generate_method;
858
Gilles Peskinee59236f2018-01-27 23:32:46 +0100859/* END_HEADER */
860
861/* BEGIN_DEPENDENCIES
862 * depends_on:MBEDTLS_PSA_CRYPTO_C
863 * END_DEPENDENCIES
864 */
865
866/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200867void static_checks( )
868{
869 size_t max_truncated_mac_size =
870 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
871
872 /* Check that the length for a truncated MAC always fits in the algorithm
873 * encoding. The shifted mask is the maximum truncated value. The
874 * untruncated algorithm may be one byte larger. */
875 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
876}
877/* END_CASE */
878
879/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200880void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100882 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200883 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300887 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100889
Gilles Peskine8817f612018-12-18 00:18:46 +0100890 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
891 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100892 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100893 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100894 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100895 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896
897exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100898 mbedtls_psa_crypto_free( );
899}
900/* END_CASE */
901
902/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100903void import_twice( int alg_arg, int usage_arg,
904 int type1_arg, data_t *data1,
905 int expected_import1_status_arg,
906 int type2_arg, data_t *data2,
907 int expected_import2_status_arg )
908{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100909 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100910 psa_algorithm_t alg = alg_arg;
911 psa_key_usage_t usage = usage_arg;
912 psa_key_type_t type1 = type1_arg;
913 psa_status_t expected_import1_status = expected_import1_status_arg;
914 psa_key_type_t type2 = type2_arg;
915 psa_status_t expected_import2_status = expected_import2_status_arg;
916 psa_key_policy_t policy;
917 psa_status_t status;
918
Gilles Peskine8817f612018-12-18 00:18:46 +0100919 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100920
Gilles Peskine8817f612018-12-18 00:18:46 +0100921 PSA_ASSERT( psa_allocate_key( type1,
922 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
923 KEY_BITS_FROM_DATA( type2, data2 ) ),
924 &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100925 psa_key_policy_init( &policy );
926 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100927 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100928
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100929 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100930 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100931 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100932 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100933
934 if( expected_import1_status == PSA_SUCCESS ||
935 expected_import2_status == PSA_SUCCESS )
936 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100937 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100938 }
939
940exit:
941 mbedtls_psa_crypto_free( );
942}
943/* END_CASE */
944
945/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200946void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
947{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100948 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200949 size_t bits = bits_arg;
950 psa_status_t expected_status = expected_status_arg;
951 psa_status_t status;
952 psa_key_type_t type =
953 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
954 size_t buffer_size = /* Slight overapproximations */
955 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200956 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200957 unsigned char *p;
958 int ret;
959 size_t length;
960
Gilles Peskine8817f612018-12-18 00:18:46 +0100961 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200962 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200963
964 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
965 bits, keypair ) ) >= 0 );
966 length = ret;
967
968 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100969 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100970 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100971 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200972 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100973 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200974
975exit:
976 mbedtls_free( buffer );
977 mbedtls_psa_crypto_free( );
978}
979/* END_CASE */
980
981/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300982void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300983 int type_arg,
984 int alg_arg,
985 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 int expected_bits,
987 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200988 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 int canonical_input )
990{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100991 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200993 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200994 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 unsigned char *exported = NULL;
997 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100999 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000 size_t reexported_length;
1001 psa_key_type_t got_type;
1002 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001003 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001006 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001007 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001008 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001010 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001011 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012
Gilles Peskine8817f612018-12-18 00:18:46 +01001013 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001014 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001015 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001016 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001017
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001018 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1019 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001020
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001021 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001022 PSA_ASSERT( psa_import_key( handle, type,
1023 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024
1025 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001026 PSA_ASSERT( psa_get_key_information( handle,
1027 &got_type,
1028 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001029 TEST_EQUAL( got_type, type );
1030 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031
1032 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001033 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 exported, export_size,
1035 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001036 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001037
1038 /* The exported length must be set by psa_export_key() to a value between 0
1039 * and export_size. On errors, the exported length must be 0. */
1040 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1041 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1042 TEST_ASSERT( exported_length <= export_size );
1043
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001044 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001045 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001046 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001047 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001048 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001050 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001051
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001052 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001053 goto exit;
1054
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001055 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001056 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057 else
1058 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001059 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001060 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1061 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001062
Gilles Peskine8817f612018-12-18 00:18:46 +01001063 PSA_ASSERT( psa_import_key( handle2, type,
1064 exported,
1065 exported_length ) );
1066 PSA_ASSERT( psa_export_key( handle2,
1067 reexported,
1068 export_size,
1069 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001070 ASSERT_COMPARE( exported, exported_length,
1071 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001072 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001073 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001074 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001075
1076destroy:
1077 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001078 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001079 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1080 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001081
1082exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001083 mbedtls_free( exported );
1084 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001085 mbedtls_psa_crypto_free( );
1086}
1087/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001088
Moran Pekerf709f4a2018-06-06 17:26:04 +03001089/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001090void import_key_nonempty_slot( )
1091{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001092 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001093 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1094 psa_status_t status;
1095 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001096 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001097
Gilles Peskine8817f612018-12-18 00:18:46 +01001098 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1099 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001100
Moran Peker28a38e62018-11-07 16:18:24 +02001101 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001102 PSA_ASSERT( psa_import_key( handle, type,
1103 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001104
1105 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001106 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001107 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001108
1109exit:
1110 mbedtls_psa_crypto_free( );
1111}
1112/* END_CASE */
1113
1114/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001115void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001116{
1117 psa_status_t status;
1118 unsigned char *exported = NULL;
1119 size_t export_size = 0;
1120 size_t exported_length = INVALID_EXPORT_LENGTH;
1121 psa_status_t expected_export_status = expected_export_status_arg;
1122
Gilles Peskine8817f612018-12-18 00:18:46 +01001123 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001124
1125 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001126 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001127 exported, export_size,
1128 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001129 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001130
1131exit:
1132 mbedtls_psa_crypto_free( );
1133}
1134/* END_CASE */
1135
1136/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001137void export_with_no_key_activity( )
1138{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001139 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001140 psa_algorithm_t alg = PSA_ALG_CTR;
1141 psa_status_t status;
1142 psa_key_policy_t policy;
1143 unsigned char *exported = NULL;
1144 size_t export_size = 0;
1145 size_t exported_length = INVALID_EXPORT_LENGTH;
1146
Gilles Peskine8817f612018-12-18 00:18:46 +01001147 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001148
Gilles Peskine8817f612018-12-18 00:18:46 +01001149 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1150 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001151 psa_key_policy_init( &policy );
1152 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001153 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001154
1155 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001156 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001157 exported, export_size,
1158 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001159 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001160
1161exit:
1162 mbedtls_psa_crypto_free( );
1163}
1164/* END_CASE */
1165
1166/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001167void cipher_with_no_key_activity( )
1168{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001169 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001170 psa_status_t status;
1171 psa_key_policy_t policy;
1172 psa_cipher_operation_t operation;
1173 int exercise_alg = PSA_ALG_CTR;
1174
Gilles Peskine8817f612018-12-18 00:18:46 +01001175 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001176
Gilles Peskine8817f612018-12-18 00:18:46 +01001177 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1178 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001179 psa_key_policy_init( &policy );
1180 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001181 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001182
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001183 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001184 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001185
1186exit:
1187 psa_cipher_abort( &operation );
1188 mbedtls_psa_crypto_free( );
1189}
1190/* END_CASE */
1191
1192/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001193void export_after_import_failure( data_t *data, int type_arg,
1194 int expected_import_status_arg )
1195{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001196 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001197 psa_key_type_t type = type_arg;
1198 psa_status_t status;
1199 unsigned char *exported = NULL;
1200 size_t export_size = 0;
1201 psa_status_t expected_import_status = expected_import_status_arg;
1202 size_t exported_length = INVALID_EXPORT_LENGTH;
1203
Gilles Peskine8817f612018-12-18 00:18:46 +01001204 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001205
Gilles Peskine8817f612018-12-18 00:18:46 +01001206 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1207 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001208
Moran Peker34550092018-11-07 16:19:34 +02001209 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001210 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001211 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001212 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001213
1214 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001215 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001216 exported, export_size,
1217 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001218 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001219
1220exit:
1221 mbedtls_psa_crypto_free( );
1222}
1223/* END_CASE */
1224
1225/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001226void cipher_after_import_failure( data_t *data, int type_arg,
1227 int expected_import_status_arg )
1228{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001229 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001230 psa_cipher_operation_t operation;
1231 psa_key_type_t type = type_arg;
1232 psa_status_t status;
1233 psa_status_t expected_import_status = expected_import_status_arg;
1234 int exercise_alg = PSA_ALG_CTR;
1235
Gilles Peskine8817f612018-12-18 00:18:46 +01001236 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001237
Gilles Peskine8817f612018-12-18 00:18:46 +01001238 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1239 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001240
Moran Pekerce500072018-11-07 16:20:07 +02001241 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001242 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001243 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001244 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001245
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001246 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001247 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001248
1249exit:
1250 psa_cipher_abort( &operation );
1251 mbedtls_psa_crypto_free( );
1252}
1253/* END_CASE */
1254
1255/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001256void export_after_destroy_key( data_t *data, int type_arg )
1257{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001258 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001259 psa_key_type_t type = type_arg;
1260 psa_status_t status;
1261 psa_key_policy_t policy;
1262 psa_algorithm_t alg = PSA_ALG_CTR;
1263 unsigned char *exported = NULL;
1264 size_t export_size = 0;
1265 size_t exported_length = INVALID_EXPORT_LENGTH;
1266
Gilles Peskine8817f612018-12-18 00:18:46 +01001267 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001268
Gilles Peskine8817f612018-12-18 00:18:46 +01001269 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1270 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001271 psa_key_policy_init( &policy );
1272 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001273 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001274 export_size = (ptrdiff_t) data->len;
1275 ASSERT_ALLOC( exported, export_size );
1276
1277 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001278 PSA_ASSERT( psa_import_key( handle, type,
1279 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001280
Gilles Peskine8817f612018-12-18 00:18:46 +01001281 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1282 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001283
1284 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001285 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001286
1287 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001288 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001289 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001290 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001291
1292exit:
1293 mbedtls_free( exported );
1294 mbedtls_psa_crypto_free( );
1295}
1296/* END_CASE */
1297
1298/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001299void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001300 int type_arg,
1301 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001302 int export_size_delta,
1303 int expected_export_status_arg,
1304 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001305{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001306 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001307 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001308 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001309 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001310 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001311 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001312 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001313 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001314 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001315
Gilles Peskine8817f612018-12-18 00:18:46 +01001316 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001317
Gilles Peskine8817f612018-12-18 00:18:46 +01001318 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1319 &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001320 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001321 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001322 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001323
1324 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001325 PSA_ASSERT( psa_import_key( handle, type,
1326 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001327
Gilles Peskine49c25912018-10-29 15:15:31 +01001328 /* Export the public key */
1329 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001330 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001331 exported, export_size,
1332 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001333 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001334 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001335 {
1336 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1337 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001338 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001339 TEST_ASSERT( expected_public_key->len <=
1340 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001341 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1342 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001343 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001344
1345exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001346 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001347 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001348 mbedtls_psa_crypto_free( );
1349}
1350/* END_CASE */
1351
Gilles Peskine20035e32018-02-03 22:44:14 +01001352/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001353void import_and_exercise_key( data_t *data,
1354 int type_arg,
1355 int bits_arg,
1356 int alg_arg )
1357{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001358 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001359 psa_key_type_t type = type_arg;
1360 size_t bits = bits_arg;
1361 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001362 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001363 psa_key_policy_t policy;
1364 psa_key_type_t got_type;
1365 size_t got_bits;
1366 psa_status_t status;
1367
Gilles Peskine8817f612018-12-18 00:18:46 +01001368 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001369
Gilles Peskine8817f612018-12-18 00:18:46 +01001370 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1371 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001372 psa_key_policy_init( &policy );
1373 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001374 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001375
1376 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001377 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001378 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001379
1380 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001381 PSA_ASSERT( psa_get_key_information( handle,
1382 &got_type,
1383 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001384 TEST_EQUAL( got_type, type );
1385 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001386
1387 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001388 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001389 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001390
1391exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001392 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001393 mbedtls_psa_crypto_free( );
1394}
1395/* END_CASE */
1396
1397/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001398void key_policy( int usage_arg, int alg_arg )
1399{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001400 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001401 psa_algorithm_t alg = alg_arg;
1402 psa_key_usage_t usage = usage_arg;
1403 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1404 unsigned char key[32] = {0};
1405 psa_key_policy_t policy_set;
1406 psa_key_policy_t policy_get;
1407
1408 memset( key, 0x2a, sizeof( key ) );
1409
Gilles Peskine8817f612018-12-18 00:18:46 +01001410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001411
Gilles Peskine8817f612018-12-18 00:18:46 +01001412 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1413 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001414 psa_key_policy_init( &policy_set );
1415 psa_key_policy_init( &policy_get );
Gilles Peskined5b33222018-06-18 22:20:03 +02001416 psa_key_policy_set_usage( &policy_set, usage, alg );
1417
Gilles Peskinefe11b722018-12-18 00:24:04 +01001418 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1419 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001420 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001421
Gilles Peskine8817f612018-12-18 00:18:46 +01001422 PSA_ASSERT( psa_import_key( handle, key_type,
1423 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001424
Gilles Peskine8817f612018-12-18 00:18:46 +01001425 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001426
Gilles Peskinefe11b722018-12-18 00:24:04 +01001427 TEST_EQUAL( policy_get.usage, policy_set.usage );
1428 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001429
1430exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001431 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001432 mbedtls_psa_crypto_free( );
1433}
1434/* END_CASE */
1435
1436/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001437void mac_key_policy( int policy_usage,
1438 int policy_alg,
1439 int key_type,
1440 data_t *key_data,
1441 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001442{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001443 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001444 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001445 psa_mac_operation_t operation;
1446 psa_status_t status;
1447 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001448
Gilles Peskine8817f612018-12-18 00:18:46 +01001449 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001450
Gilles Peskine8817f612018-12-18 00:18:46 +01001451 PSA_ASSERT( psa_allocate_key( key_type,
1452 KEY_BITS_FROM_DATA( key_type, key_data ),
1453 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001454 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001455 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001456 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001457
Gilles Peskine8817f612018-12-18 00:18:46 +01001458 PSA_ASSERT( psa_import_key( handle, key_type,
1459 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001460
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001461 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001462 if( policy_alg == exercise_alg &&
1463 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001464 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001465 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001466 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001468
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001469 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001470 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001471 if( policy_alg == exercise_alg &&
1472 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001473 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001474 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001475 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476
1477exit:
1478 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001479 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001480 mbedtls_psa_crypto_free( );
1481}
1482/* END_CASE */
1483
1484/* BEGIN_CASE */
1485void cipher_key_policy( int policy_usage,
1486 int policy_alg,
1487 int key_type,
1488 data_t *key_data,
1489 int exercise_alg )
1490{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001491 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001492 psa_key_policy_t policy;
1493 psa_cipher_operation_t operation;
1494 psa_status_t status;
1495
Gilles Peskine8817f612018-12-18 00:18:46 +01001496 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001497
Gilles Peskine8817f612018-12-18 00:18:46 +01001498 PSA_ASSERT( psa_allocate_key( key_type,
1499 KEY_BITS_FROM_DATA( key_type, key_data ),
1500 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001501 psa_key_policy_init( &policy );
1502 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001503 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001504
Gilles Peskine8817f612018-12-18 00:18:46 +01001505 PSA_ASSERT( psa_import_key( handle, key_type,
1506 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001507
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001508 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001509 if( policy_alg == exercise_alg &&
1510 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001511 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001512 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001513 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001514 psa_cipher_abort( &operation );
1515
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001516 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001517 if( policy_alg == exercise_alg &&
1518 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001519 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001520 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001521 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001522
1523exit:
1524 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001525 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001526 mbedtls_psa_crypto_free( );
1527}
1528/* END_CASE */
1529
1530/* BEGIN_CASE */
1531void aead_key_policy( int policy_usage,
1532 int policy_alg,
1533 int key_type,
1534 data_t *key_data,
1535 int nonce_length_arg,
1536 int tag_length_arg,
1537 int exercise_alg )
1538{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001539 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001540 psa_key_policy_t policy;
1541 psa_status_t status;
1542 unsigned char nonce[16] = {0};
1543 size_t nonce_length = nonce_length_arg;
1544 unsigned char tag[16];
1545 size_t tag_length = tag_length_arg;
1546 size_t output_length;
1547
1548 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1549 TEST_ASSERT( tag_length <= sizeof( tag ) );
1550
Gilles Peskine8817f612018-12-18 00:18:46 +01001551 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001552
Gilles Peskine8817f612018-12-18 00:18:46 +01001553 PSA_ASSERT( psa_allocate_key( key_type,
1554 KEY_BITS_FROM_DATA( key_type, key_data ),
1555 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001556 psa_key_policy_init( &policy );
1557 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001558 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001559
Gilles Peskine8817f612018-12-18 00:18:46 +01001560 PSA_ASSERT( psa_import_key( handle, key_type,
1561 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001562
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564 nonce, nonce_length,
1565 NULL, 0,
1566 NULL, 0,
1567 tag, tag_length,
1568 &output_length );
1569 if( policy_alg == exercise_alg &&
1570 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001572 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001573 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001574
1575 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001576 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 nonce, nonce_length,
1578 NULL, 0,
1579 tag, tag_length,
1580 NULL, 0,
1581 &output_length );
1582 if( policy_alg == exercise_alg &&
1583 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001584 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001585 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001586 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001587
1588exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001589 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001590 mbedtls_psa_crypto_free( );
1591}
1592/* END_CASE */
1593
1594/* BEGIN_CASE */
1595void asymmetric_encryption_key_policy( int policy_usage,
1596 int policy_alg,
1597 int key_type,
1598 data_t *key_data,
1599 int exercise_alg )
1600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001601 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001602 psa_key_policy_t policy;
1603 psa_status_t status;
1604 size_t key_bits;
1605 size_t buffer_length;
1606 unsigned char *buffer = NULL;
1607 size_t output_length;
1608
Gilles Peskine8817f612018-12-18 00:18:46 +01001609 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001610
Gilles Peskine8817f612018-12-18 00:18:46 +01001611 PSA_ASSERT( psa_allocate_key( key_type,
1612 KEY_BITS_FROM_DATA( key_type, key_data ),
1613 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614 psa_key_policy_init( &policy );
1615 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001616 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001617
Gilles Peskine8817f612018-12-18 00:18:46 +01001618 PSA_ASSERT( psa_import_key( handle, key_type,
1619 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001620
Gilles Peskine8817f612018-12-18 00:18:46 +01001621 PSA_ASSERT( psa_get_key_information( handle,
1622 NULL,
1623 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001624 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1625 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001626 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001627
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001628 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001629 NULL, 0,
1630 NULL, 0,
1631 buffer, buffer_length,
1632 &output_length );
1633 if( policy_alg == exercise_alg &&
1634 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001635 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001636 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001637 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001638
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001639 if( buffer_length != 0 )
1640 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001641 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001642 buffer, buffer_length,
1643 NULL, 0,
1644 buffer, buffer_length,
1645 &output_length );
1646 if( policy_alg == exercise_alg &&
1647 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001648 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001649 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001650 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001651
1652exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001653 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001654 mbedtls_psa_crypto_free( );
1655 mbedtls_free( buffer );
1656}
1657/* END_CASE */
1658
1659/* BEGIN_CASE */
1660void asymmetric_signature_key_policy( int policy_usage,
1661 int policy_alg,
1662 int key_type,
1663 data_t *key_data,
1664 int exercise_alg )
1665{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001666 psa_key_handle_t handle = 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001667 psa_key_policy_t policy;
1668 psa_status_t status;
1669 unsigned char payload[16] = {1};
1670 size_t payload_length = sizeof( payload );
1671 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1672 size_t signature_length;
1673
Gilles Peskine8817f612018-12-18 00:18:46 +01001674 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001675
Gilles Peskine8817f612018-12-18 00:18:46 +01001676 PSA_ASSERT( psa_allocate_key( key_type,
1677 KEY_BITS_FROM_DATA( key_type, key_data ),
1678 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001679 psa_key_policy_init( &policy );
1680 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001681 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001682
Gilles Peskine8817f612018-12-18 00:18:46 +01001683 PSA_ASSERT( psa_import_key( handle, key_type,
1684 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001686 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001687 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001688 signature, sizeof( signature ),
1689 &signature_length );
1690 if( policy_alg == exercise_alg &&
1691 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001692 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001693 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001694 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001695
1696 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001697 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001698 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001699 signature, sizeof( signature ) );
1700 if( policy_alg == exercise_alg &&
1701 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001704 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001705
1706exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001707 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001708 mbedtls_psa_crypto_free( );
1709}
1710/* END_CASE */
1711
1712/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001713void derive_key_policy( int policy_usage,
1714 int policy_alg,
1715 int key_type,
1716 data_t *key_data,
1717 int exercise_alg )
1718{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001719 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001720 psa_key_policy_t policy;
1721 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1722 psa_status_t status;
1723
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001725
Gilles Peskine8817f612018-12-18 00:18:46 +01001726 PSA_ASSERT( psa_allocate_key( key_type,
1727 KEY_BITS_FROM_DATA( key_type, key_data ),
1728 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001729 psa_key_policy_init( &policy );
1730 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001731 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001732
Gilles Peskine8817f612018-12-18 00:18:46 +01001733 PSA_ASSERT( psa_import_key( handle, key_type,
1734 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001735
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001736 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001737 exercise_alg,
1738 NULL, 0,
1739 NULL, 0,
1740 1 );
1741 if( policy_alg == exercise_alg &&
1742 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001743 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001744 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001745 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001746
1747exit:
1748 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001749 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001750 mbedtls_psa_crypto_free( );
1751}
1752/* END_CASE */
1753
1754/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001755void agreement_key_policy( int policy_usage,
1756 int policy_alg,
1757 int key_type_arg,
1758 data_t *key_data,
1759 int exercise_alg )
1760{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001761 psa_key_handle_t handle = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001762 psa_key_policy_t policy;
1763 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001764 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1765 psa_status_t status;
1766
Gilles Peskine8817f612018-12-18 00:18:46 +01001767 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001768
Gilles Peskine8817f612018-12-18 00:18:46 +01001769 PSA_ASSERT( psa_allocate_key( key_type,
1770 KEY_BITS_FROM_DATA( key_type, key_data ),
1771 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001772 psa_key_policy_init( &policy );
1773 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001774 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001775
Gilles Peskine8817f612018-12-18 00:18:46 +01001776 PSA_ASSERT( psa_import_key( handle, key_type,
1777 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001778
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001779 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001780
Gilles Peskine01d718c2018-09-18 12:01:02 +02001781 if( policy_alg == exercise_alg &&
1782 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001783 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001784 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001785 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001786
1787exit:
1788 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001789 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001790 mbedtls_psa_crypto_free( );
1791}
1792/* END_CASE */
1793
1794/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001795void hash_setup( int alg_arg,
1796 int expected_status_arg )
1797{
1798 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001799 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001800 psa_hash_operation_t operation;
1801 psa_status_t status;
1802
Gilles Peskine8817f612018-12-18 00:18:46 +01001803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001804
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001805 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001806 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001807 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001808
1809exit:
1810 mbedtls_psa_crypto_free( );
1811}
1812/* END_CASE */
1813
1814/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001815void hash_bad_order( )
1816{
1817 unsigned char input[] = "";
1818 /* SHA-256 hash of an empty string */
1819 unsigned char hash[] = {
1820 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1821 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1822 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1823 size_t hash_len;
1824 psa_hash_operation_t operation;
1825
Gilles Peskine8817f612018-12-18 00:18:46 +01001826 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001827
1828 /* psa_hash_update without calling psa_hash_setup beforehand */
1829 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001830 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001831 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001832
1833 /* psa_hash_verify without calling psa_hash_setup beforehand */
1834 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001835 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001836 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001837
1838 /* psa_hash_finish without calling psa_hash_setup beforehand */
1839 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001840 TEST_EQUAL( psa_hash_finish( &operation,
1841 hash, sizeof( hash ), &hash_len ),
1842 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001843
1844exit:
1845 mbedtls_psa_crypto_free( );
1846}
1847/* END_CASE */
1848
itayzafrir27e69452018-11-01 14:26:34 +02001849/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1850void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001851{
1852 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001853 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1854 * appended to it */
1855 unsigned char hash[] = {
1856 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1857 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1858 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001859 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001860 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001861
Gilles Peskine8817f612018-12-18 00:18:46 +01001862 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001863
itayzafrir27e69452018-11-01 14:26:34 +02001864 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001865 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001866 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001867 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001868
itayzafrir27e69452018-11-01 14:26:34 +02001869 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001870 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001871 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001872 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001873
itayzafrir27e69452018-11-01 14:26:34 +02001874 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001875 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001876 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001877 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001878
itayzafrirec93d302018-10-18 18:01:10 +03001879exit:
1880 mbedtls_psa_crypto_free( );
1881}
1882/* END_CASE */
1883
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001884/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1885void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001886{
1887 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001888 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001889 size_t expected_size = PSA_HASH_SIZE( alg );
1890 psa_hash_operation_t operation;
1891 size_t hash_len;
1892
Gilles Peskine8817f612018-12-18 00:18:46 +01001893 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001894
itayzafrir58028322018-10-25 10:22:01 +03001895 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001896 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001897 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001898 hash, expected_size - 1, &hash_len ),
1899 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001900
1901exit:
1902 mbedtls_psa_crypto_free( );
1903}
1904/* END_CASE */
1905
1906/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001907void mac_setup( int key_type_arg,
1908 data_t *key,
1909 int alg_arg,
1910 int expected_status_arg )
1911{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001912 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001913 psa_key_type_t key_type = key_type_arg;
1914 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001915 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001916 psa_mac_operation_t operation;
1917 psa_key_policy_t policy;
1918 psa_status_t status;
1919
Gilles Peskine8817f612018-12-18 00:18:46 +01001920 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001921
Gilles Peskine8817f612018-12-18 00:18:46 +01001922 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1923 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001924 psa_key_policy_init( &policy );
1925 psa_key_policy_set_usage( &policy,
1926 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1927 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001928 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001929
Gilles Peskine8817f612018-12-18 00:18:46 +01001930 PSA_ASSERT( psa_import_key( handle, key_type,
1931 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001932
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001933 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001934 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001935 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001936
1937exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001938 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001939 mbedtls_psa_crypto_free( );
1940}
1941/* END_CASE */
1942
1943/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001944void mac_sign( int key_type_arg,
1945 data_t *key,
1946 int alg_arg,
1947 data_t *input,
1948 data_t *expected_mac )
1949{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001950 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001951 psa_key_type_t key_type = key_type_arg;
1952 psa_algorithm_t alg = alg_arg;
1953 psa_mac_operation_t operation;
1954 psa_key_policy_t policy;
1955 /* Leave a little extra room in the output buffer. At the end of the
1956 * test, we'll check that the implementation didn't overwrite onto
1957 * this extra room. */
1958 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1959 size_t mac_buffer_size =
1960 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1961 size_t mac_length = 0;
1962
1963 memset( actual_mac, '+', sizeof( actual_mac ) );
1964 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1965 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1966
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001968
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1970 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001971 psa_key_policy_init( &policy );
1972 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001973 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001974
Gilles Peskine8817f612018-12-18 00:18:46 +01001975 PSA_ASSERT( psa_import_key( handle, key_type,
1976 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001977
1978 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01001979 PSA_ASSERT( psa_mac_sign_setup( &operation,
1980 handle, alg ) );
1981 PSA_ASSERT( psa_mac_update( &operation,
1982 input->x, input->len ) );
1983 PSA_ASSERT( psa_mac_sign_finish( &operation,
1984 actual_mac, mac_buffer_size,
1985 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001986
1987 /* Compare with the expected value. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001988 TEST_EQUAL( mac_length, expected_mac->len );
1989 TEST_EQUAL( memcmp( actual_mac, expected_mac->x, mac_length ), 0 );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001990
1991 /* Verify that the end of the buffer is untouched. */
1992 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1993 sizeof( actual_mac ) - mac_length ) );
1994
1995exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001996 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001997 mbedtls_psa_crypto_free( );
1998}
1999/* END_CASE */
2000
2001/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002002void mac_verify( int key_type_arg,
2003 data_t *key,
2004 int alg_arg,
2005 data_t *input,
2006 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002007{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002008 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002009 psa_key_type_t key_type = key_type_arg;
2010 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002011 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07002012 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002013
Gilles Peskine69c12672018-06-28 00:07:19 +02002014 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2015
Gilles Peskine8c9def32018-02-08 10:02:12 +01002016 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002017 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002019 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002020 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2021 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022
Gilles Peskine8817f612018-12-18 00:18:46 +01002023 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024
Gilles Peskine8817f612018-12-18 00:18:46 +01002025 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2026 &handle ) );
mohammad16036df908f2018-04-02 08:34:15 -07002027 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002028 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002029 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002030
Gilles Peskine8817f612018-12-18 00:18:46 +01002031 PSA_ASSERT( psa_import_key( handle, key_type,
2032 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002033
Gilles Peskine8817f612018-12-18 00:18:46 +01002034 PSA_ASSERT( psa_mac_verify_setup( &operation,
2035 handle, alg ) );
2036 PSA_ASSERT( psa_destroy_key( handle ) );
2037 PSA_ASSERT( psa_mac_update( &operation,
2038 input->x, input->len ) );
2039 PSA_ASSERT( psa_mac_verify_finish( &operation,
2040 expected_mac->x,
2041 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002042
2043exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002044 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002045 mbedtls_psa_crypto_free( );
2046}
2047/* END_CASE */
2048
2049/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002050void cipher_setup( int key_type_arg,
2051 data_t *key,
2052 int alg_arg,
2053 int expected_status_arg )
2054{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002055 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002056 psa_key_type_t key_type = key_type_arg;
2057 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002058 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002059 psa_cipher_operation_t operation;
2060 psa_key_policy_t policy;
2061 psa_status_t status;
2062
Gilles Peskine8817f612018-12-18 00:18:46 +01002063 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002064
Gilles Peskine8817f612018-12-18 00:18:46 +01002065 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2066 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002067 psa_key_policy_init( &policy );
2068 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002069 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002070
Gilles Peskine8817f612018-12-18 00:18:46 +01002071 PSA_ASSERT( psa_import_key( handle, key_type,
2072 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002073
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002074 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002075 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002076 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002077
2078exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002079 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002080 mbedtls_psa_crypto_free( );
2081}
2082/* END_CASE */
2083
2084/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002085void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002086 data_t *key,
2087 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002088 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002089{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002090 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002091 psa_status_t status;
2092 psa_key_type_t key_type = key_type_arg;
2093 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002094 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002095 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002096 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002097 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002098 size_t output_buffer_size = 0;
2099 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002100 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002101 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002102 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002103
Gilles Peskine50e586b2018-06-08 14:28:46 +02002104 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002105 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002106 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002107 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2108 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2109 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002110
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002111 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2112 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002113
Gilles Peskine8817f612018-12-18 00:18:46 +01002114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002115
Gilles Peskine8817f612018-12-18 00:18:46 +01002116 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2117 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002118 psa_key_policy_init( &policy );
2119 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002120 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002121
Gilles Peskine8817f612018-12-18 00:18:46 +01002122 PSA_ASSERT( psa_import_key( handle, key_type,
2123 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002124
Gilles Peskine8817f612018-12-18 00:18:46 +01002125 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2126 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002127
Gilles Peskine8817f612018-12-18 00:18:46 +01002128 PSA_ASSERT( psa_cipher_set_iv( &operation,
2129 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002130 output_buffer_size = ( (size_t) input->len +
2131 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002132 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002133
Gilles Peskine8817f612018-12-18 00:18:46 +01002134 PSA_ASSERT( psa_cipher_update( &operation,
2135 input->x, input->len,
2136 output, output_buffer_size,
2137 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002138 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002139 status = psa_cipher_finish( &operation,
2140 output + function_output_length,
2141 output_buffer_size,
2142 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002143 total_output_length += function_output_length;
2144
Gilles Peskinefe11b722018-12-18 00:24:04 +01002145 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002146 if( expected_status == PSA_SUCCESS )
2147 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002148 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002149 ASSERT_COMPARE( expected_output->x, expected_output->len,
2150 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002151 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002152
Gilles Peskine50e586b2018-06-08 14:28:46 +02002153exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002154 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002155 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002156 mbedtls_psa_crypto_free( );
2157}
2158/* END_CASE */
2159
2160/* BEGIN_CASE */
2161void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002162 data_t *key,
2163 data_t *input,
2164 int first_part_size,
2165 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002166{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002167 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002168 psa_key_type_t key_type = key_type_arg;
2169 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002170 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002171 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002172 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002173 size_t output_buffer_size = 0;
2174 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002175 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002176 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002177 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002178
Gilles Peskine50e586b2018-06-08 14:28:46 +02002179 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002181 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002182 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2183 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2184 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002185
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002186 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2187 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002188
Gilles Peskine8817f612018-12-18 00:18:46 +01002189 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002190
Gilles Peskine8817f612018-12-18 00:18:46 +01002191 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2192 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002193 psa_key_policy_init( &policy );
2194 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002195 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002196
Gilles Peskine8817f612018-12-18 00:18:46 +01002197 PSA_ASSERT( psa_import_key( handle, key_type,
2198 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002199
Gilles Peskine8817f612018-12-18 00:18:46 +01002200 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2201 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002202
Gilles Peskine8817f612018-12-18 00:18:46 +01002203 PSA_ASSERT( psa_cipher_set_iv( &operation,
2204 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002205 output_buffer_size = ( (size_t) input->len +
2206 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002207 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002208
Gilles Peskine4abf7412018-06-18 16:35:34 +02002209 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002210 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2211 output, output_buffer_size,
2212 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002213 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002214 PSA_ASSERT( psa_cipher_update( &operation,
2215 input->x + first_part_size,
2216 input->len - first_part_size,
2217 output, output_buffer_size,
2218 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002219 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002220 PSA_ASSERT( psa_cipher_finish( &operation,
2221 output + function_output_length,
2222 output_buffer_size,
2223 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002224 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002225 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002226
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002227 ASSERT_COMPARE( expected_output->x, expected_output->len,
2228 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002229
2230exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002231 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002232 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002233 mbedtls_psa_crypto_free( );
2234}
2235/* END_CASE */
2236
2237/* BEGIN_CASE */
2238void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002239 data_t *key,
2240 data_t *input,
2241 int first_part_size,
2242 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002243{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002244 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002245
2246 psa_key_type_t key_type = key_type_arg;
2247 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002248 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002249 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002250 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002251 size_t output_buffer_size = 0;
2252 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002253 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002254 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002255 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002256
Gilles Peskine50e586b2018-06-08 14:28:46 +02002257 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002258 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002259 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002260 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2261 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2262 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002263
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002264 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2265 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002266
Gilles Peskine8817f612018-12-18 00:18:46 +01002267 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002268
Gilles Peskine8817f612018-12-18 00:18:46 +01002269 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2270 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002271 psa_key_policy_init( &policy );
2272 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002273 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002274
Gilles Peskine8817f612018-12-18 00:18:46 +01002275 PSA_ASSERT( psa_import_key( handle, key_type,
2276 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002277
Gilles Peskine8817f612018-12-18 00:18:46 +01002278 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2279 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002280
Gilles Peskine8817f612018-12-18 00:18:46 +01002281 PSA_ASSERT( psa_cipher_set_iv( &operation,
2282 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002283
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002284 output_buffer_size = ( (size_t) input->len +
2285 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002286 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002287
Gilles Peskine4abf7412018-06-18 16:35:34 +02002288 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002289 PSA_ASSERT( psa_cipher_update( &operation,
2290 input->x, first_part_size,
2291 output, output_buffer_size,
2292 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002293 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002294 PSA_ASSERT( psa_cipher_update( &operation,
2295 input->x + first_part_size,
2296 input->len - first_part_size,
2297 output, output_buffer_size,
2298 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002299 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002300 PSA_ASSERT( psa_cipher_finish( &operation,
2301 output + function_output_length,
2302 output_buffer_size,
2303 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002304 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002305 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002306
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002307 ASSERT_COMPARE( expected_output->x, expected_output->len,
2308 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002309
2310exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002311 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002312 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002313 mbedtls_psa_crypto_free( );
2314}
2315/* END_CASE */
2316
Gilles Peskine50e586b2018-06-08 14:28:46 +02002317/* BEGIN_CASE */
2318void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002319 data_t *key,
2320 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002321 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002322{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002323 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002324 psa_status_t status;
2325 psa_key_type_t key_type = key_type_arg;
2326 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002327 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002328 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002329 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002330 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002331 size_t output_buffer_size = 0;
2332 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002333 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002334 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002335 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002336
Gilles Peskine50e586b2018-06-08 14:28:46 +02002337 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002338 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002339 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002340 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2341 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2342 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002343
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002344 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2345 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002346
Gilles Peskine8817f612018-12-18 00:18:46 +01002347 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002348
Gilles Peskine8817f612018-12-18 00:18:46 +01002349 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2350 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002351 psa_key_policy_init( &policy );
2352 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002353 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002354
Gilles Peskine8817f612018-12-18 00:18:46 +01002355 PSA_ASSERT( psa_import_key( handle, key_type,
2356 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002357
Gilles Peskine8817f612018-12-18 00:18:46 +01002358 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2359 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002360
Gilles Peskine8817f612018-12-18 00:18:46 +01002361 PSA_ASSERT( psa_cipher_set_iv( &operation,
2362 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002363
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002364 output_buffer_size = ( (size_t) input->len +
2365 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002366 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002367
Gilles Peskine8817f612018-12-18 00:18:46 +01002368 PSA_ASSERT( psa_cipher_update( &operation,
2369 input->x, input->len,
2370 output, output_buffer_size,
2371 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002372 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002373 status = psa_cipher_finish( &operation,
2374 output + function_output_length,
2375 output_buffer_size,
2376 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002377 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002378 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002379
2380 if( expected_status == PSA_SUCCESS )
2381 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002382 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002383 ASSERT_COMPARE( expected_output->x, expected_output->len,
2384 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002385 }
2386
Gilles Peskine50e586b2018-06-08 14:28:46 +02002387exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002388 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002389 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002390 mbedtls_psa_crypto_free( );
2391}
2392/* END_CASE */
2393
Gilles Peskine50e586b2018-06-08 14:28:46 +02002394/* BEGIN_CASE */
2395void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002396 data_t *key,
2397 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002398{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002399 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002400 psa_key_type_t key_type = key_type_arg;
2401 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002402 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002403 size_t iv_size = 16;
2404 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002405 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002406 size_t output1_size = 0;
2407 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002408 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002409 size_t output2_size = 0;
2410 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002411 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002412 psa_cipher_operation_t operation1;
2413 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002414 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002415
mohammad1603d7d7ba52018-03-12 18:51:53 +02002416 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002417 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002418 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2419 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002420
Gilles Peskine8817f612018-12-18 00:18:46 +01002421 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002422
Gilles Peskine8817f612018-12-18 00:18:46 +01002423 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2424 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002425 psa_key_policy_init( &policy );
2426 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002427 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002428
Gilles Peskine8817f612018-12-18 00:18:46 +01002429 PSA_ASSERT( psa_import_key( handle, key_type,
2430 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002431
Gilles Peskine8817f612018-12-18 00:18:46 +01002432 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2433 handle, alg ) );
2434 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2435 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002436
Gilles Peskine8817f612018-12-18 00:18:46 +01002437 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2438 iv, iv_size,
2439 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002440 output1_size = ( (size_t) input->len +
2441 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002442 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002443
Gilles Peskine8817f612018-12-18 00:18:46 +01002444 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2445 output1, output1_size,
2446 &output1_length ) );
2447 PSA_ASSERT( psa_cipher_finish( &operation1,
2448 output1 + output1_length, output1_size,
2449 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002450
Gilles Peskine048b7f02018-06-08 14:20:49 +02002451 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002452
Gilles Peskine8817f612018-12-18 00:18:46 +01002453 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002454
2455 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002456 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002457
Gilles Peskine8817f612018-12-18 00:18:46 +01002458 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2459 iv, iv_length ) );
2460 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2461 output2, output2_size,
2462 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002463 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002464 PSA_ASSERT( psa_cipher_finish( &operation2,
2465 output2 + output2_length,
2466 output2_size,
2467 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002468
Gilles Peskine048b7f02018-06-08 14:20:49 +02002469 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002470
Gilles Peskine8817f612018-12-18 00:18:46 +01002471 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002472
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002473 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002474
2475exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002476 mbedtls_free( output1 );
2477 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002478 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002479 mbedtls_psa_crypto_free( );
2480}
2481/* END_CASE */
2482
2483/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002484void cipher_verify_output_multipart( int alg_arg,
2485 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002486 data_t *key,
2487 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002488 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002489{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002490 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002491 psa_key_type_t key_type = key_type_arg;
2492 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002493 unsigned char iv[16] = {0};
2494 size_t iv_size = 16;
2495 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002496 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002497 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002498 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002499 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002500 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002501 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002502 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002503 psa_cipher_operation_t operation1;
2504 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002505 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002506
Moran Pekerded84402018-06-06 16:36:50 +03002507 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002508 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002509 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2510 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002511
Gilles Peskine8817f612018-12-18 00:18:46 +01002512 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002513
Gilles Peskine8817f612018-12-18 00:18:46 +01002514 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2515 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002516 psa_key_policy_init( &policy );
2517 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002518 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002519
Gilles Peskine8817f612018-12-18 00:18:46 +01002520 PSA_ASSERT( psa_import_key( handle, key_type,
2521 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002522
Gilles Peskine8817f612018-12-18 00:18:46 +01002523 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2524 handle, alg ) );
2525 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2526 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002527
Gilles Peskine8817f612018-12-18 00:18:46 +01002528 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2529 iv, iv_size,
2530 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002531 output1_buffer_size = ( (size_t) input->len +
2532 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002533 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002534
Gilles Peskine4abf7412018-06-18 16:35:34 +02002535 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002536
Gilles Peskine8817f612018-12-18 00:18:46 +01002537 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2538 output1, output1_buffer_size,
2539 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002540 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002541
Gilles Peskine8817f612018-12-18 00:18:46 +01002542 PSA_ASSERT( psa_cipher_update( &operation1,
2543 input->x + first_part_size,
2544 input->len - first_part_size,
2545 output1, output1_buffer_size,
2546 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002547 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002548
Gilles Peskine8817f612018-12-18 00:18:46 +01002549 PSA_ASSERT( psa_cipher_finish( &operation1,
2550 output1 + output1_length,
2551 output1_buffer_size - output1_length,
2552 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002553 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002554
Gilles Peskine8817f612018-12-18 00:18:46 +01002555 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002556
Gilles Peskine048b7f02018-06-08 14:20:49 +02002557 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002558 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002559
Gilles Peskine8817f612018-12-18 00:18:46 +01002560 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2561 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002562
Gilles Peskine8817f612018-12-18 00:18:46 +01002563 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2564 output2, output2_buffer_size,
2565 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002566 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002567
Gilles Peskine8817f612018-12-18 00:18:46 +01002568 PSA_ASSERT( psa_cipher_update( &operation2,
2569 output1 + first_part_size,
2570 output1_length - first_part_size,
2571 output2, output2_buffer_size,
2572 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002573 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002574
Gilles Peskine8817f612018-12-18 00:18:46 +01002575 PSA_ASSERT( psa_cipher_finish( &operation2,
2576 output2 + output2_length,
2577 output2_buffer_size - output2_length,
2578 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002579 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002580
Gilles Peskine8817f612018-12-18 00:18:46 +01002581 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002582
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002583 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002584
2585exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002586 mbedtls_free( output1 );
2587 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002588 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002589 mbedtls_psa_crypto_free( );
2590}
2591/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002592
Gilles Peskine20035e32018-02-03 22:44:14 +01002593/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002594void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002595 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002596 data_t *nonce,
2597 data_t *additional_data,
2598 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002599 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002601 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002602 psa_key_type_t key_type = key_type_arg;
2603 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002604 unsigned char *output_data = NULL;
2605 size_t output_size = 0;
2606 size_t output_length = 0;
2607 unsigned char *output_data2 = NULL;
2608 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002609 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002610 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002611 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002612
Gilles Peskinea1cac842018-06-11 19:33:02 +02002613 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002614 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002615 TEST_ASSERT( nonce != NULL );
2616 TEST_ASSERT( additional_data != NULL );
2617 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2618 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2619 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2620 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2621
Gilles Peskine4abf7412018-06-18 16:35:34 +02002622 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002623 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002626
Gilles Peskine8817f612018-12-18 00:18:46 +01002627 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2628 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002629 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002630 psa_key_policy_set_usage( &policy,
2631 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2632 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002633 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002634
Gilles Peskine8817f612018-12-18 00:18:46 +01002635 PSA_ASSERT( psa_import_key( handle, key_type,
2636 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002637
Gilles Peskinefe11b722018-12-18 00:24:04 +01002638 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2639 nonce->x, nonce->len,
2640 additional_data->x,
2641 additional_data->len,
2642 input_data->x, input_data->len,
2643 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002644 &output_length ),
2645 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002646
2647 if( PSA_SUCCESS == expected_result )
2648 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002649 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002650
Gilles Peskinefe11b722018-12-18 00:24:04 +01002651 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2652 nonce->x, nonce->len,
2653 additional_data->x,
2654 additional_data->len,
2655 output_data, output_length,
2656 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002657 &output_length2 ),
2658 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002659
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002660 ASSERT_COMPARE( input_data->x, input_data->len,
2661 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002662 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002663
Gilles Peskinea1cac842018-06-11 19:33:02 +02002664exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002665 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002666 mbedtls_free( output_data );
2667 mbedtls_free( output_data2 );
2668 mbedtls_psa_crypto_free( );
2669}
2670/* END_CASE */
2671
2672/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002673void aead_encrypt( int key_type_arg, data_t *key_data,
2674 int alg_arg,
2675 data_t *nonce,
2676 data_t *additional_data,
2677 data_t *input_data,
2678 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002679{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002680 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002681 psa_key_type_t key_type = key_type_arg;
2682 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002683 unsigned char *output_data = NULL;
2684 size_t output_size = 0;
2685 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002686 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002687 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002688
Gilles Peskinea1cac842018-06-11 19:33:02 +02002689 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002690 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002691 TEST_ASSERT( additional_data != NULL );
2692 TEST_ASSERT( nonce != NULL );
2693 TEST_ASSERT( expected_result != NULL );
2694 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2695 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2696 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2697 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2698 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2699
Gilles Peskine4abf7412018-06-18 16:35:34 +02002700 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002701 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002702
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002704
Gilles Peskine8817f612018-12-18 00:18:46 +01002705 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2706 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002707 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002708 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002709 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002710
Gilles Peskine8817f612018-12-18 00:18:46 +01002711 PSA_ASSERT( psa_import_key( handle, key_type,
2712 key_data->x,
2713 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002714
Gilles Peskine8817f612018-12-18 00:18:46 +01002715 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2716 nonce->x, nonce->len,
2717 additional_data->x, additional_data->len,
2718 input_data->x, input_data->len,
2719 output_data, output_size,
2720 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002721
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002722 ASSERT_COMPARE( expected_result->x, expected_result->len,
2723 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002724
Gilles Peskinea1cac842018-06-11 19:33:02 +02002725exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002726 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002727 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002728 mbedtls_psa_crypto_free( );
2729}
2730/* END_CASE */
2731
2732/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002733void aead_decrypt( int key_type_arg, data_t *key_data,
2734 int alg_arg,
2735 data_t *nonce,
2736 data_t *additional_data,
2737 data_t *input_data,
2738 data_t *expected_data,
2739 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002740{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002741 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002742 psa_key_type_t key_type = key_type_arg;
2743 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002744 unsigned char *output_data = NULL;
2745 size_t output_size = 0;
2746 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002747 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002748 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002749 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002750
Gilles Peskinea1cac842018-06-11 19:33:02 +02002751 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002752 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002753 TEST_ASSERT( additional_data != NULL );
2754 TEST_ASSERT( nonce != NULL );
2755 TEST_ASSERT( expected_data != NULL );
2756 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2757 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2758 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2759 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2760 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2761
Gilles Peskine4abf7412018-06-18 16:35:34 +02002762 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002763 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002764
Gilles Peskine8817f612018-12-18 00:18:46 +01002765 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002766
Gilles Peskine8817f612018-12-18 00:18:46 +01002767 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2768 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002769 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002770 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002771 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002772
Gilles Peskine8817f612018-12-18 00:18:46 +01002773 PSA_ASSERT( psa_import_key( handle, key_type,
2774 key_data->x,
2775 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002776
Gilles Peskinefe11b722018-12-18 00:24:04 +01002777 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2778 nonce->x, nonce->len,
2779 additional_data->x,
2780 additional_data->len,
2781 input_data->x, input_data->len,
2782 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002783 &output_length ),
2784 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002785
Gilles Peskine2d277862018-06-18 15:41:12 +02002786 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002787 ASSERT_COMPARE( expected_data->x, expected_data->len,
2788 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002789
Gilles Peskinea1cac842018-06-11 19:33:02 +02002790exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002791 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002792 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002793 mbedtls_psa_crypto_free( );
2794}
2795/* END_CASE */
2796
2797/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002798void signature_size( int type_arg,
2799 int bits,
2800 int alg_arg,
2801 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002802{
2803 psa_key_type_t type = type_arg;
2804 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002805 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002806 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002807exit:
2808 ;
2809}
2810/* END_CASE */
2811
2812/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002813void sign_deterministic( int key_type_arg, data_t *key_data,
2814 int alg_arg, data_t *input_data,
2815 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002816{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002817 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002818 psa_key_type_t key_type = key_type_arg;
2819 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002820 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002821 unsigned char *signature = NULL;
2822 size_t signature_size;
2823 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002824 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002825
Gilles Peskine20035e32018-02-03 22:44:14 +01002826 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002827 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002828 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002829 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2830 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2831 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002832
Gilles Peskine8817f612018-12-18 00:18:46 +01002833 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002834
Gilles Peskine8817f612018-12-18 00:18:46 +01002835 PSA_ASSERT( psa_allocate_key( key_type,
2836 KEY_BITS_FROM_DATA( key_type, key_data ),
2837 &handle ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002838 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002839 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002840 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002841
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 PSA_ASSERT( psa_import_key( handle, key_type,
2843 key_data->x,
2844 key_data->len ) );
2845 PSA_ASSERT( psa_get_key_information( handle,
2846 NULL,
2847 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002848
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002849 /* Allocate a buffer which has the size advertized by the
2850 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002851 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2852 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002853 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002854 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002855 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002856
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002857 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002858 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2859 input_data->x, input_data->len,
2860 signature, signature_size,
2861 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002862 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002863 ASSERT_COMPARE( output_data->x, output_data->len,
2864 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002865
2866exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002867 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002868 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002869 mbedtls_psa_crypto_free( );
2870}
2871/* END_CASE */
2872
2873/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002874void sign_fail( int key_type_arg, data_t *key_data,
2875 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002876 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002877{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002878 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002879 psa_key_type_t key_type = key_type_arg;
2880 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002881 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002882 psa_status_t actual_status;
2883 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002884 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002885 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002886 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002887
Gilles Peskine20035e32018-02-03 22:44:14 +01002888 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002889 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002890 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2891 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2892
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002893 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002894
Gilles Peskine8817f612018-12-18 00:18:46 +01002895 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002896
Gilles Peskine8817f612018-12-18 00:18:46 +01002897 PSA_ASSERT( psa_allocate_key( key_type,
2898 KEY_BITS_FROM_DATA( key_type, key_data ),
2899 &handle ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002900 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002901 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002902 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002903
Gilles Peskine8817f612018-12-18 00:18:46 +01002904 PSA_ASSERT( psa_import_key( handle, key_type,
2905 key_data->x,
2906 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002907
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002908 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002909 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002910 signature, signature_size,
2911 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002912 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002913 /* The value of *signature_length is unspecified on error, but
2914 * whatever it is, it should be less than signature_size, so that
2915 * if the caller tries to read *signature_length bytes without
2916 * checking the error code then they don't overflow a buffer. */
2917 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002918
2919exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002920 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002921 mbedtls_free( signature );
2922 mbedtls_psa_crypto_free( );
2923}
2924/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002925
2926/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002927void sign_verify( int key_type_arg, data_t *key_data,
2928 int alg_arg, data_t *input_data )
2929{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002930 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002931 psa_key_type_t key_type = key_type_arg;
2932 psa_algorithm_t alg = alg_arg;
2933 size_t key_bits;
2934 unsigned char *signature = NULL;
2935 size_t signature_size;
2936 size_t signature_length = 0xdeadbeef;
2937 psa_key_policy_t policy;
2938
Gilles Peskine8817f612018-12-18 00:18:46 +01002939 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002940
Gilles Peskine8817f612018-12-18 00:18:46 +01002941 PSA_ASSERT( psa_allocate_key( key_type,
2942 KEY_BITS_FROM_DATA( key_type, key_data ),
2943 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002944 psa_key_policy_init( &policy );
2945 psa_key_policy_set_usage( &policy,
2946 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2947 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002948 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002949
Gilles Peskine8817f612018-12-18 00:18:46 +01002950 PSA_ASSERT( psa_import_key( handle, key_type,
2951 key_data->x,
2952 key_data->len ) );
2953 PSA_ASSERT( psa_get_key_information( handle,
2954 NULL,
2955 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002956
2957 /* Allocate a buffer which has the size advertized by the
2958 * library. */
2959 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2960 key_bits, alg );
2961 TEST_ASSERT( signature_size != 0 );
2962 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002963 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002964
2965 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002966 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2967 input_data->x, input_data->len,
2968 signature, signature_size,
2969 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002970 /* Check that the signature length looks sensible. */
2971 TEST_ASSERT( signature_length <= signature_size );
2972 TEST_ASSERT( signature_length > 0 );
2973
2974 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002975 PSA_ASSERT( psa_asymmetric_verify(
2976 handle, alg,
2977 input_data->x, input_data->len,
2978 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002979
2980 if( input_data->len != 0 )
2981 {
2982 /* Flip a bit in the input and verify that the signature is now
2983 * detected as invalid. Flip a bit at the beginning, not at the end,
2984 * because ECDSA may ignore the last few bits of the input. */
2985 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002986 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2987 input_data->x, input_data->len,
2988 signature, signature_length ),
2989 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002990 }
2991
2992exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002993 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002994 mbedtls_free( signature );
2995 mbedtls_psa_crypto_free( );
2996}
2997/* END_CASE */
2998
2999/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003000void asymmetric_verify( int key_type_arg, data_t *key_data,
3001 int alg_arg, data_t *hash_data,
3002 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003003{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003004 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003005 psa_key_type_t key_type = key_type_arg;
3006 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003007 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03003008
Gilles Peskine69c12672018-06-28 00:07:19 +02003009 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3010
itayzafrir5c753392018-05-08 11:18:38 +03003011 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003012 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03003013 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003014 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3015 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3016 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003017
Gilles Peskine8817f612018-12-18 00:18:46 +01003018 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003019
Gilles Peskine8817f612018-12-18 00:18:46 +01003020 PSA_ASSERT( psa_allocate_key( key_type,
3021 KEY_BITS_FROM_DATA( key_type, key_data ),
3022 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003023 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003024 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003025 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003026
Gilles Peskine8817f612018-12-18 00:18:46 +01003027 PSA_ASSERT( psa_import_key( handle, key_type,
3028 key_data->x,
3029 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003030
Gilles Peskine8817f612018-12-18 00:18:46 +01003031 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3032 hash_data->x, hash_data->len,
3033 signature_data->x,
3034 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003035exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003036 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003037 mbedtls_psa_crypto_free( );
3038}
3039/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003040
3041/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003042void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3043 int alg_arg, data_t *hash_data,
3044 data_t *signature_data,
3045 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003046{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003047 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003048 psa_key_type_t key_type = key_type_arg;
3049 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003050 psa_status_t actual_status;
3051 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003052 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003053
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003054 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003055 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003056 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003057 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3058 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
3059 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003060
Gilles Peskine8817f612018-12-18 00:18:46 +01003061 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003062
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 PSA_ASSERT( psa_allocate_key( key_type,
3064 KEY_BITS_FROM_DATA( key_type, key_data ),
3065 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003066 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003067 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003068 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003069
Gilles Peskine8817f612018-12-18 00:18:46 +01003070 PSA_ASSERT( psa_import_key( handle, key_type,
3071 key_data->x,
3072 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003073
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003074 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003075 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003076 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003077 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078
Gilles Peskinefe11b722018-12-18 00:24:04 +01003079 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003080
3081exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003082 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003083 mbedtls_psa_crypto_free( );
3084}
3085/* END_CASE */
3086
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003087/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003088void asymmetric_encrypt( int key_type_arg,
3089 data_t *key_data,
3090 int alg_arg,
3091 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003092 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003093 int expected_output_length_arg,
3094 int expected_status_arg )
3095{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003096 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003097 psa_key_type_t key_type = key_type_arg;
3098 psa_algorithm_t alg = alg_arg;
3099 size_t expected_output_length = expected_output_length_arg;
3100 size_t key_bits;
3101 unsigned char *output = NULL;
3102 size_t output_size;
3103 size_t output_length = ~0;
3104 psa_status_t actual_status;
3105 psa_status_t expected_status = expected_status_arg;
3106 psa_key_policy_t policy;
3107
Gilles Peskine8817f612018-12-18 00:18:46 +01003108 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003109
Gilles Peskine656896e2018-06-29 19:12:28 +02003110 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003111 PSA_ASSERT( psa_allocate_key( key_type,
3112 KEY_BITS_FROM_DATA( key_type, key_data ),
3113 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003114 psa_key_policy_init( &policy );
3115 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003116 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3117 PSA_ASSERT( psa_import_key( handle, key_type,
3118 key_data->x,
3119 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003120
3121 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003122 PSA_ASSERT( psa_get_key_information( handle,
3123 NULL,
3124 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003125 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003126 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003127
3128 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003129 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003130 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003131 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003132 output, output_size,
3133 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003134 TEST_EQUAL( actual_status, expected_status );
3135 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003136
Gilles Peskine68428122018-06-30 18:42:41 +02003137 /* If the label is empty, the test framework puts a non-null pointer
3138 * in label->x. Test that a null pointer works as well. */
3139 if( label->len == 0 )
3140 {
3141 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003142 if( output_size != 0 )
3143 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003144 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003145 input_data->x, input_data->len,
3146 NULL, label->len,
3147 output, output_size,
3148 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003149 TEST_EQUAL( actual_status, expected_status );
3150 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003151 }
3152
Gilles Peskine656896e2018-06-29 19:12:28 +02003153exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003154 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003155 mbedtls_free( output );
3156 mbedtls_psa_crypto_free( );
3157}
3158/* END_CASE */
3159
3160/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003161void asymmetric_encrypt_decrypt( int key_type_arg,
3162 data_t *key_data,
3163 int alg_arg,
3164 data_t *input_data,
3165 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003167 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003168 psa_key_type_t key_type = key_type_arg;
3169 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003170 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003171 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003172 size_t output_size;
3173 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003174 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003175 size_t output2_size;
3176 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003177 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003181 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3182 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3183
Gilles Peskine8817f612018-12-18 00:18:46 +01003184 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003185
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_allocate_key( key_type,
3187 KEY_BITS_FROM_DATA( key_type, key_data ),
3188 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003189 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003190 psa_key_policy_set_usage( &policy,
3191 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003192 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003193 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003194
Gilles Peskine8817f612018-12-18 00:18:46 +01003195 PSA_ASSERT( psa_import_key( handle, key_type,
3196 key_data->x,
3197 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003198
3199 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003200 PSA_ASSERT( psa_get_key_information( handle,
3201 NULL,
3202 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003203 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003204 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003205 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003206 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003207
Gilles Peskineeebd7382018-06-08 18:11:54 +02003208 /* We test encryption by checking that encrypt-then-decrypt gives back
3209 * the original plaintext because of the non-optional random
3210 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3212 input_data->x, input_data->len,
3213 label->x, label->len,
3214 output, output_size,
3215 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003216 /* We don't know what ciphertext length to expect, but check that
3217 * it looks sensible. */
3218 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003219
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3221 output, output_length,
3222 label->x, label->len,
3223 output2, output2_size,
3224 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003225 ASSERT_COMPARE( input_data->x, input_data->len,
3226 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003227
3228exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003229 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003230 mbedtls_free( output );
3231 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003232 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003233}
3234/* END_CASE */
3235
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003236/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003237void asymmetric_decrypt( int key_type_arg,
3238 data_t *key_data,
3239 int alg_arg,
3240 data_t *input_data,
3241 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003242 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003243{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003244 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003245 psa_key_type_t key_type = key_type_arg;
3246 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003248 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003249 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003250 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003251
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003254 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003255 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3256 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3257 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3258
Gilles Peskine4abf7412018-06-18 16:35:34 +02003259 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003260 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003261
Gilles Peskine8817f612018-12-18 00:18:46 +01003262 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003263
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_allocate_key( key_type,
3265 KEY_BITS_FROM_DATA( key_type, key_data ),
3266 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003267 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003268 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003269 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003270
Gilles Peskine8817f612018-12-18 00:18:46 +01003271 PSA_ASSERT( psa_import_key( handle, key_type,
3272 key_data->x,
3273 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003274
Gilles Peskine8817f612018-12-18 00:18:46 +01003275 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3276 input_data->x, input_data->len,
3277 label->x, label->len,
3278 output,
3279 output_size,
3280 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003281 ASSERT_COMPARE( expected_data->x, expected_data->len,
3282 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283
Gilles Peskine68428122018-06-30 18:42:41 +02003284 /* If the label is empty, the test framework puts a non-null pointer
3285 * in label->x. Test that a null pointer works as well. */
3286 if( label->len == 0 )
3287 {
3288 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003289 if( output_size != 0 )
3290 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003291 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3292 input_data->x, input_data->len,
3293 NULL, label->len,
3294 output,
3295 output_size,
3296 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003297 ASSERT_COMPARE( expected_data->x, expected_data->len,
3298 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003299 }
3300
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003301exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003302 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003303 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003305}
3306/* END_CASE */
3307
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003309void asymmetric_decrypt_fail( int key_type_arg,
3310 data_t *key_data,
3311 int alg_arg,
3312 data_t *input_data,
3313 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003314 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003316 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003317 psa_key_type_t key_type = key_type_arg;
3318 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003319 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003320 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003321 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003322 psa_status_t actual_status;
3323 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003324 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003325
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003326 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003327 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003328 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3329 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3330
Gilles Peskine4abf7412018-06-18 16:35:34 +02003331 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003332 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003333
Gilles Peskine8817f612018-12-18 00:18:46 +01003334 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003335
Gilles Peskine8817f612018-12-18 00:18:46 +01003336 PSA_ASSERT( psa_allocate_key( key_type,
3337 KEY_BITS_FROM_DATA( key_type, key_data ),
3338 &handle ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003339 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003340 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003341 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003342
Gilles Peskine8817f612018-12-18 00:18:46 +01003343 PSA_ASSERT( psa_import_key( handle, key_type,
3344 key_data->x,
3345 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003346
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003347 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003348 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003349 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003350 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003351 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003352 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003353 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003354
Gilles Peskine68428122018-06-30 18:42:41 +02003355 /* If the label is empty, the test framework puts a non-null pointer
3356 * in label->x. Test that a null pointer works as well. */
3357 if( label->len == 0 )
3358 {
3359 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003360 if( output_size != 0 )
3361 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003362 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003363 input_data->x, input_data->len,
3364 NULL, label->len,
3365 output, output_size,
3366 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003367 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003368 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003369 }
3370
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003371exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003372 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003373 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003374 mbedtls_psa_crypto_free( );
3375}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003376/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003377
3378/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003379void derive_setup( int key_type_arg,
3380 data_t *key_data,
3381 int alg_arg,
3382 data_t *salt,
3383 data_t *label,
3384 int requested_capacity_arg,
3385 int expected_status_arg )
3386{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003387 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003388 size_t key_type = key_type_arg;
3389 psa_algorithm_t alg = alg_arg;
3390 size_t requested_capacity = requested_capacity_arg;
3391 psa_status_t expected_status = expected_status_arg;
3392 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3393 psa_key_policy_t policy;
3394
Gilles Peskine8817f612018-12-18 00:18:46 +01003395 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003396
Gilles Peskine8817f612018-12-18 00:18:46 +01003397 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3398 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003399 psa_key_policy_init( &policy );
3400 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003401 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003402
Gilles Peskine8817f612018-12-18 00:18:46 +01003403 PSA_ASSERT( psa_import_key( handle, key_type,
3404 key_data->x,
3405 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003406
Gilles Peskinefe11b722018-12-18 00:24:04 +01003407 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3408 salt->x, salt->len,
3409 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003410 requested_capacity ),
3411 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003412
3413exit:
3414 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003415 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003416 mbedtls_psa_crypto_free( );
3417}
3418/* END_CASE */
3419
3420/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003421void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003422{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003423 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003424 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003425 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003426 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003427 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003428 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003429 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3430 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3431 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003432 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003433
Gilles Peskine8817f612018-12-18 00:18:46 +01003434 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003435
Gilles Peskine8817f612018-12-18 00:18:46 +01003436 PSA_ASSERT( psa_allocate_key( key_type,
3437 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3438 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003439 psa_key_policy_init( &policy );
3440 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003441 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003442
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 PSA_ASSERT( psa_import_key( handle, key_type,
3444 key_data,
3445 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003446
3447 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003448 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3449 NULL, 0,
3450 NULL, 0,
3451 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003452
3453 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003454 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3455 NULL, 0,
3456 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003457 capacity ),
3458 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003459
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003460 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003461
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003462 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3463 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003464
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003465exit:
3466 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003467 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003468 mbedtls_psa_crypto_free( );
3469}
3470/* END_CASE */
3471
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003472/* BEGIN_CASE */
3473void test_derive_invalid_generator_tests( )
3474{
3475 uint8_t output_buffer[16];
3476 size_t buffer_size = 16;
3477 size_t capacity = 0;
3478 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3479
Nir Sonnenschein50789302018-10-31 12:16:38 +02003480 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003481 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003482
3483 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003484 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003485
Gilles Peskine8817f612018-12-18 00:18:46 +01003486 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003487
Nir Sonnenschein50789302018-10-31 12:16:38 +02003488 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003489 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003490
Nir Sonnenschein50789302018-10-31 12:16:38 +02003491 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003492 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003493
3494exit:
3495 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003496}
3497/* END_CASE */
3498
3499/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003500void derive_output( int alg_arg,
3501 data_t *key_data,
3502 data_t *salt,
3503 data_t *label,
3504 int requested_capacity_arg,
3505 data_t *expected_output1,
3506 data_t *expected_output2 )
3507{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003508 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003509 psa_algorithm_t alg = alg_arg;
3510 size_t requested_capacity = requested_capacity_arg;
3511 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3512 uint8_t *expected_outputs[2] =
3513 {expected_output1->x, expected_output2->x};
3514 size_t output_sizes[2] =
3515 {expected_output1->len, expected_output2->len};
3516 size_t output_buffer_size = 0;
3517 uint8_t *output_buffer = NULL;
3518 size_t expected_capacity;
3519 size_t current_capacity;
3520 psa_key_policy_t policy;
3521 psa_status_t status;
3522 unsigned i;
3523
3524 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3525 {
3526 if( output_sizes[i] > output_buffer_size )
3527 output_buffer_size = output_sizes[i];
3528 if( output_sizes[i] == 0 )
3529 expected_outputs[i] = NULL;
3530 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003531 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003532 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003533
Gilles Peskine8817f612018-12-18 00:18:46 +01003534 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3535 PSA_BYTES_TO_BITS( key_data->len ),
3536 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003537 psa_key_policy_init( &policy );
3538 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003539 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003540
Gilles Peskine8817f612018-12-18 00:18:46 +01003541 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3542 key_data->x,
3543 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003544
3545 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3547 salt->x, salt->len,
3548 label->x, label->len,
3549 requested_capacity ) );
3550 PSA_ASSERT( psa_get_generator_capacity( &generator,
3551 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003552 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003553 expected_capacity = requested_capacity;
3554
3555 /* Expansion phase. */
3556 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3557 {
3558 /* Read some bytes. */
3559 status = psa_generator_read( &generator,
3560 output_buffer, output_sizes[i] );
3561 if( expected_capacity == 0 && output_sizes[i] == 0 )
3562 {
3563 /* Reading 0 bytes when 0 bytes are available can go either way. */
3564 TEST_ASSERT( status == PSA_SUCCESS ||
3565 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3566 continue;
3567 }
3568 else if( expected_capacity == 0 ||
3569 output_sizes[i] > expected_capacity )
3570 {
3571 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003572 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003573 expected_capacity = 0;
3574 continue;
3575 }
3576 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003577 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003578 if( output_sizes[i] != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01003579 TEST_EQUAL( memcmp( output_buffer, expected_outputs[i],
3580 output_sizes[i] ), 0 );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003581 /* Check the generator status. */
3582 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003583 PSA_ASSERT( psa_get_generator_capacity( &generator,
3584 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003585 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003586 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003587 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003588
3589exit:
3590 mbedtls_free( output_buffer );
3591 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003592 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003593 mbedtls_psa_crypto_free( );
3594}
3595/* END_CASE */
3596
3597/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003598void derive_full( int alg_arg,
3599 data_t *key_data,
3600 data_t *salt,
3601 data_t *label,
3602 int requested_capacity_arg )
3603{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003604 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003605 psa_algorithm_t alg = alg_arg;
3606 size_t requested_capacity = requested_capacity_arg;
3607 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3608 unsigned char output_buffer[16];
3609 size_t expected_capacity = requested_capacity;
3610 size_t current_capacity;
3611 psa_key_policy_t policy;
3612
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003614
Gilles Peskine8817f612018-12-18 00:18:46 +01003615 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3616 PSA_BYTES_TO_BITS( key_data->len ),
3617 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003618 psa_key_policy_init( &policy );
3619 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003620 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003621
Gilles Peskine8817f612018-12-18 00:18:46 +01003622 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3623 key_data->x,
3624 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003625
3626 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003627 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3628 salt->x, salt->len,
3629 label->x, label->len,
3630 requested_capacity ) );
3631 PSA_ASSERT( psa_get_generator_capacity( &generator,
3632 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003633 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003634
3635 /* Expansion phase. */
3636 while( current_capacity > 0 )
3637 {
3638 size_t read_size = sizeof( output_buffer );
3639 if( read_size > current_capacity )
3640 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003641 PSA_ASSERT( psa_generator_read( &generator,
3642 output_buffer,
3643 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003644 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003645 PSA_ASSERT( psa_get_generator_capacity( &generator,
3646 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003647 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003648 }
3649
3650 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003651 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3652 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003653
Gilles Peskine8817f612018-12-18 00:18:46 +01003654 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003655
3656exit:
3657 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003658 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003659 mbedtls_psa_crypto_free( );
3660}
3661/* END_CASE */
3662
3663/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003664void derive_key_exercise( int alg_arg,
3665 data_t *key_data,
3666 data_t *salt,
3667 data_t *label,
3668 int derived_type_arg,
3669 int derived_bits_arg,
3670 int derived_usage_arg,
3671 int derived_alg_arg )
3672{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003673 psa_key_handle_t base_handle = 0;
3674 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003675 psa_algorithm_t alg = alg_arg;
3676 psa_key_type_t derived_type = derived_type_arg;
3677 size_t derived_bits = derived_bits_arg;
3678 psa_key_usage_t derived_usage = derived_usage_arg;
3679 psa_algorithm_t derived_alg = derived_alg_arg;
3680 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3681 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3682 psa_key_policy_t policy;
3683 psa_key_type_t got_type;
3684 size_t got_bits;
3685
Gilles Peskine8817f612018-12-18 00:18:46 +01003686 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003687
Gilles Peskine8817f612018-12-18 00:18:46 +01003688 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3689 PSA_BYTES_TO_BITS( key_data->len ),
3690 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003691 psa_key_policy_init( &policy );
3692 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003693 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3694 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3695 key_data->x,
3696 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003697
3698 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003699 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3700 salt->x, salt->len,
3701 label->x, label->len,
3702 capacity ) );
3703 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3704 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003705 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003706 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3707 PSA_ASSERT( psa_generator_import_key( derived_handle,
3708 derived_type,
3709 derived_bits,
3710 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003711
3712 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003713 PSA_ASSERT( psa_get_key_information( derived_handle,
3714 &got_type,
3715 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003716 TEST_EQUAL( got_type, derived_type );
3717 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003718
3719 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003720 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003721 goto exit;
3722
3723exit:
3724 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003725 psa_destroy_key( base_handle );
3726 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003727 mbedtls_psa_crypto_free( );
3728}
3729/* END_CASE */
3730
3731/* BEGIN_CASE */
3732void derive_key_export( int alg_arg,
3733 data_t *key_data,
3734 data_t *salt,
3735 data_t *label,
3736 int bytes1_arg,
3737 int bytes2_arg )
3738{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003739 psa_key_handle_t base_handle = 0;
3740 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003741 psa_algorithm_t alg = alg_arg;
3742 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003743 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003744 size_t bytes2 = bytes2_arg;
3745 size_t capacity = bytes1 + bytes2;
3746 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003747 uint8_t *output_buffer = NULL;
3748 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003749 psa_key_policy_t policy;
3750 size_t length;
3751
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003752 ASSERT_ALLOC( output_buffer, capacity );
3753 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003754 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003755
Gilles Peskine8817f612018-12-18 00:18:46 +01003756 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3757 PSA_BYTES_TO_BITS( key_data->len ),
3758 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003759 psa_key_policy_init( &policy );
3760 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003761 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3762 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3763 key_data->x,
3764 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003765
3766 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003767 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3768 salt->x, salt->len,
3769 label->x, label->len,
3770 capacity ) );
3771 PSA_ASSERT( psa_generator_read( &generator,
3772 output_buffer,
3773 capacity ) );
3774 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003775
3776 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003777 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3778 salt->x, salt->len,
3779 label->x, label->len,
3780 capacity ) );
3781 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3782 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003783 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003784 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3785 PSA_ASSERT( psa_generator_import_key( derived_handle,
3786 PSA_KEY_TYPE_RAW_DATA,
3787 derived_bits,
3788 &generator ) );
3789 PSA_ASSERT( psa_export_key( derived_handle,
3790 export_buffer, bytes1,
3791 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003792 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003793 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3794 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3795 PSA_BYTES_TO_BITS( bytes2 ),
3796 &derived_handle ) );
3797 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3798 PSA_ASSERT( psa_generator_import_key( derived_handle,
3799 PSA_KEY_TYPE_RAW_DATA,
3800 PSA_BYTES_TO_BITS( bytes2 ),
3801 &generator ) );
3802 PSA_ASSERT( psa_export_key( derived_handle,
3803 export_buffer + bytes1, bytes2,
3804 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003805 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003806
3807 /* Compare the outputs from the two runs. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003808 TEST_EQUAL( memcmp( output_buffer, export_buffer, capacity ), 0 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003809
3810exit:
3811 mbedtls_free( output_buffer );
3812 mbedtls_free( export_buffer );
3813 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003814 psa_destroy_key( base_handle );
3815 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003816 mbedtls_psa_crypto_free( );
3817}
3818/* END_CASE */
3819
3820/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003821void key_agreement_setup( int alg_arg,
3822 int our_key_type_arg, data_t *our_key_data,
3823 data_t *peer_key_data,
3824 int expected_status_arg )
3825{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003826 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003827 psa_algorithm_t alg = alg_arg;
3828 psa_key_type_t our_key_type = our_key_type_arg;
3829 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3830 psa_key_policy_t policy;
3831
Gilles Peskine8817f612018-12-18 00:18:46 +01003832 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003833
Gilles Peskine8817f612018-12-18 00:18:46 +01003834 PSA_ASSERT( psa_allocate_key( our_key_type,
3835 KEY_BITS_FROM_DATA( our_key_type,
3836 our_key_data ),
3837 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003838 psa_key_policy_init( &policy );
3839 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003840 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3841 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3842 our_key_data->x,
3843 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003844
Gilles Peskinefe11b722018-12-18 00:24:04 +01003845 TEST_EQUAL( psa_key_agreement( &generator,
3846 our_key,
3847 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003848 alg ),
3849 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003850
3851exit:
3852 psa_generator_abort( &generator );
3853 psa_destroy_key( our_key );
3854 mbedtls_psa_crypto_free( );
3855}
3856/* END_CASE */
3857
3858/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003859void key_agreement_capacity( int alg_arg,
3860 int our_key_type_arg, data_t *our_key_data,
3861 data_t *peer_key_data,
3862 int expected_capacity_arg )
3863{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003864 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003865 psa_algorithm_t alg = alg_arg;
3866 psa_key_type_t our_key_type = our_key_type_arg;
3867 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3868 psa_key_policy_t policy;
3869 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003870 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003871
Gilles Peskine8817f612018-12-18 00:18:46 +01003872 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003873
Gilles Peskine8817f612018-12-18 00:18:46 +01003874 PSA_ASSERT( psa_allocate_key( our_key_type,
3875 KEY_BITS_FROM_DATA( our_key_type,
3876 our_key_data ),
3877 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003878 psa_key_policy_init( &policy );
3879 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003880 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3881 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3882 our_key_data->x,
3883 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003884
Gilles Peskine8817f612018-12-18 00:18:46 +01003885 PSA_ASSERT( psa_key_agreement( &generator,
3886 our_key,
3887 peer_key_data->x, peer_key_data->len,
3888 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003889
Gilles Peskinebf491972018-10-25 22:36:12 +02003890 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003891 PSA_ASSERT( psa_get_generator_capacity(
3892 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003893 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003894
Gilles Peskinebf491972018-10-25 22:36:12 +02003895 /* Test the actual capacity by reading the output. */
3896 while( actual_capacity > sizeof( output ) )
3897 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003898 PSA_ASSERT( psa_generator_read( &generator,
3899 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003900 actual_capacity -= sizeof( output );
3901 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003902 PSA_ASSERT( psa_generator_read( &generator,
3903 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003904 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3905 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003906
Gilles Peskine59685592018-09-18 12:11:34 +02003907exit:
3908 psa_generator_abort( &generator );
3909 psa_destroy_key( our_key );
3910 mbedtls_psa_crypto_free( );
3911}
3912/* END_CASE */
3913
3914/* BEGIN_CASE */
3915void key_agreement_output( int alg_arg,
3916 int our_key_type_arg, data_t *our_key_data,
3917 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003918 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003919{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003920 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003921 psa_algorithm_t alg = alg_arg;
3922 psa_key_type_t our_key_type = our_key_type_arg;
3923 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3924 psa_key_policy_t policy;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003925 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003926
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003927 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3928 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003929
Gilles Peskine8817f612018-12-18 00:18:46 +01003930 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003931
Gilles Peskine8817f612018-12-18 00:18:46 +01003932 PSA_ASSERT( psa_allocate_key( our_key_type,
3933 KEY_BITS_FROM_DATA( our_key_type,
3934 our_key_data ),
3935 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003936 psa_key_policy_init( &policy );
3937 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003938 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3939 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3940 our_key_data->x,
3941 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003942
Gilles Peskine8817f612018-12-18 00:18:46 +01003943 PSA_ASSERT( psa_key_agreement( &generator,
3944 our_key,
3945 peer_key_data->x, peer_key_data->len,
3946 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003947
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003948 PSA_ASSERT( psa_generator_read( &generator,
3949 actual_output,
3950 expected_output1->len ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003951 TEST_EQUAL( memcmp( actual_output, expected_output1->x,
3952 expected_output1->len ), 0 );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003953 if( expected_output2->len != 0 )
3954 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003955 PSA_ASSERT( psa_generator_read( &generator,
3956 actual_output,
3957 expected_output2->len ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003958 TEST_EQUAL( memcmp( actual_output, expected_output2->x,
3959 expected_output2->len ), 0 );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003960 }
Gilles Peskine59685592018-09-18 12:11:34 +02003961
3962exit:
3963 psa_generator_abort( &generator );
3964 psa_destroy_key( our_key );
3965 mbedtls_psa_crypto_free( );
3966 mbedtls_free( actual_output );
3967}
3968/* END_CASE */
3969
3970/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003971void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003972{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003973 size_t bytes = bytes_arg;
3974 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003975 unsigned char *output = NULL;
3976 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003977 size_t i;
3978 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003979
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003980 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3981 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003982 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003983
Gilles Peskine8817f612018-12-18 00:18:46 +01003984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003985
Gilles Peskinea50d7392018-06-21 10:22:13 +02003986 /* Run several times, to ensure that every output byte will be
3987 * nonzero at least once with overwhelming probability
3988 * (2^(-8*number_of_runs)). */
3989 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003990 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003991 if( bytes != 0 )
3992 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003993 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003994
3995 /* Check that no more than bytes have been overwritten */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003996 TEST_EQUAL( memcmp( output + bytes, trail, sizeof( trail ) ), 0 );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003997
3998 for( i = 0; i < bytes; i++ )
3999 {
4000 if( output[i] != 0 )
4001 ++changed[i];
4002 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004003 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004004
4005 /* Check that every byte was changed to nonzero at least once. This
4006 * validates that psa_generate_random is overwriting every byte of
4007 * the output buffer. */
4008 for( i = 0; i < bytes; i++ )
4009 {
4010 TEST_ASSERT( changed[i] != 0 );
4011 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004012
4013exit:
4014 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004015 mbedtls_free( output );
4016 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004017}
4018/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004019
4020/* BEGIN_CASE */
4021void generate_key( int type_arg,
4022 int bits_arg,
4023 int usage_arg,
4024 int alg_arg,
4025 int expected_status_arg )
4026{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004027 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004028 psa_key_type_t type = type_arg;
4029 psa_key_usage_t usage = usage_arg;
4030 size_t bits = bits_arg;
4031 psa_algorithm_t alg = alg_arg;
4032 psa_status_t expected_status = expected_status_arg;
4033 psa_key_type_t got_type;
4034 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004035 psa_status_t expected_info_status =
4036 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
4037 psa_key_policy_t policy;
4038
Gilles Peskine8817f612018-12-18 00:18:46 +01004039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004040
Gilles Peskine8817f612018-12-18 00:18:46 +01004041 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004042 psa_key_policy_init( &policy );
4043 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004045
4046 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004047 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4048 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004049
4050 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004051 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4052 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004053 if( expected_info_status != PSA_SUCCESS )
4054 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004055 TEST_EQUAL( got_type, type );
4056 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004057
Gilles Peskine818ca122018-06-20 18:16:48 +02004058 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004059 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004060 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004061
4062exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004063 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004064 mbedtls_psa_crypto_free( );
4065}
4066/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004067
Darryl Greend49a4992018-06-18 17:27:26 +01004068/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4069void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4070 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004071 int alg_arg, int generation_method,
4072 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004073{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004074 psa_key_handle_t handle = 0;
4075 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004076 psa_key_type_t type = (psa_key_type_t) type_arg;
4077 psa_key_type_t type_get;
4078 size_t bits_get;
4079 psa_key_policy_t policy_set;
4080 psa_key_policy_t policy_get;
4081 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4082 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004083 psa_key_policy_t base_policy_set;
4084 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4085 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004086 unsigned char *first_export = NULL;
4087 unsigned char *second_export = NULL;
4088 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4089 size_t first_exported_length;
4090 size_t second_exported_length;
4091
4092 ASSERT_ALLOC( first_export, export_size );
4093 ASSERT_ALLOC( second_export, export_size );
4094
Gilles Peskine8817f612018-12-18 00:18:46 +01004095 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004096
Gilles Peskine8817f612018-12-18 00:18:46 +01004097 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4098 type, bits,
4099 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004100 psa_key_policy_init( &policy_set );
Darryl Greend49a4992018-06-18 17:27:26 +01004101 psa_key_policy_set_usage( &policy_set, policy_usage,
4102 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004103 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004104
Darryl Green0c6575a2018-11-07 16:05:30 +00004105 switch( generation_method )
4106 {
4107 case IMPORT_KEY:
4108 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004109 PSA_ASSERT( psa_import_key( handle, type,
4110 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004111 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004112
Darryl Green0c6575a2018-11-07 16:05:30 +00004113 case GENERATE_KEY:
4114 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004115 PSA_ASSERT( psa_generate_key( handle, type, bits,
4116 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004117 break;
4118
4119 case DERIVE_KEY:
4120 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004121 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
4122 PSA_BYTES_TO_BITS( data->len ),
4123 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004124 psa_key_policy_init( &base_policy_set );
Darryl Green0c6575a2018-11-07 16:05:30 +00004125 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4126 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004127 PSA_ASSERT( psa_set_key_policy(
4128 base_key, &base_policy_set ) );
4129 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4130 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004131 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004132 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4133 base_policy_alg,
4134 NULL, 0, NULL, 0,
4135 export_size ) );
4136 PSA_ASSERT( psa_generator_import_key(
4137 handle, PSA_KEY_TYPE_RAW_DATA,
4138 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004139 break;
4140 }
Darryl Greend49a4992018-06-18 17:27:26 +01004141
4142 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004143 TEST_EQUAL( psa_export_key( handle,
4144 first_export, export_size,
4145 &first_exported_length ),
4146 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004147
4148 /* Shutdown and restart */
4149 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004150 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004151
Darryl Greend49a4992018-06-18 17:27:26 +01004152 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004153 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4154 &handle ) );
4155 PSA_ASSERT( psa_get_key_information(
4156 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004157 TEST_EQUAL( type_get, type );
4158 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004159
Gilles Peskine8817f612018-12-18 00:18:46 +01004160 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004161 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4162 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004163
4164 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004165 TEST_EQUAL( psa_export_key( handle,
4166 second_export, export_size,
4167 &second_exported_length ),
4168 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004169
Darryl Green0c6575a2018-11-07 16:05:30 +00004170 if( export_status == PSA_SUCCESS )
4171 {
4172 ASSERT_COMPARE( first_export, first_exported_length,
4173 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004174
Darryl Green0c6575a2018-11-07 16:05:30 +00004175 switch( generation_method )
4176 {
4177 case IMPORT_KEY:
4178 ASSERT_COMPARE( data->x, data->len,
4179 first_export, first_exported_length );
4180 break;
4181 default:
4182 break;
4183 }
4184 }
4185
4186 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004187 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004188 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004189
4190exit:
4191 mbedtls_free( first_export );
4192 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004193 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004194 mbedtls_psa_crypto_free();
4195}
4196/* END_CASE */