blob: 12195ece7552931aee6434b3ce90931d601c0429 [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
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinef426e0f2019-02-25 17:42:03 +010017/* A hash algorithm that is known to be supported.
18 *
19 * This is used in some smoke tests.
20 */
21#if defined(MBEDTLS_MD2_C)
22#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
23#elif defined(MBEDTLS_MD4_C)
24#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
25#elif defined(MBEDTLS_MD5_C)
26#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
27/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
28 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
29 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
30 * implausible anyway. */
31#elif defined(MBEDTLS_SHA1_C)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
33#elif defined(MBEDTLS_SHA256_C)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
35#elif defined(MBEDTLS_SHA512_C)
36#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
37#elif defined(MBEDTLS_SHA3_C)
38#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
39#else
40#undef KNOWN_SUPPORTED_HASH_ALG
41#endif
42
43/* A block cipher that is known to be supported.
44 *
45 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
46 */
47#if defined(MBEDTLS_AES_C)
48#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
49#elif defined(MBEDTLS_ARIA_C)
50#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
51#elif defined(MBEDTLS_CAMELLIA_C)
52#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
53#undef KNOWN_SUPPORTED_BLOCK_CIPHER
54#endif
55
56/* A MAC mode that is known to be supported.
57 *
58 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
59 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
60 *
61 * This is used in some smoke tests.
62 */
63#if defined(KNOWN_SUPPORTED_HASH_ALG)
64#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
65#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
66#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
67#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
68#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
69#else
70#undef KNOWN_SUPPORTED_MAC_ALG
71#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
72#endif
73
74/* A cipher algorithm and key type that are known to be supported.
75 *
76 * This is used in some smoke tests.
77 */
78#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
79#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
80#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
81#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
82#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
83#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
84#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
85#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
86#else
87#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
88#endif
89#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
90#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
92#elif defined(MBEDTLS_RC4_C)
93#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
94#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
95#else
96#undef KNOWN_SUPPORTED_CIPHER_ALG
97#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
98#endif
99
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200100/** Test if a buffer contains a constant byte value.
101 *
102 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200103 *
104 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200105 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200106 * \param size Size of the buffer in bytes.
107 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200108 * \return 1 if the buffer is all-bits-zero.
109 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200110 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200111static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200112{
113 size_t i;
114 for( i = 0; i < size; i++ )
115 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200116 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200117 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200118 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200119 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200120}
Gilles Peskine818ca122018-06-20 18:16:48 +0200121
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200122/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
123static int asn1_write_10x( unsigned char **p,
124 unsigned char *start,
125 size_t bits,
126 unsigned char x )
127{
128 int ret;
129 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200130 if( bits == 0 )
131 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
132 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200133 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300134 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200135 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
136 *p -= len;
137 ( *p )[len-1] = x;
138 if( bits % 8 == 0 )
139 ( *p )[1] |= 1;
140 else
141 ( *p )[0] |= 1 << ( bits % 8 );
142 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
143 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
144 MBEDTLS_ASN1_INTEGER ) );
145 return( len );
146}
147
148static int construct_fake_rsa_key( unsigned char *buffer,
149 size_t buffer_size,
150 unsigned char **p,
151 size_t bits,
152 int keypair )
153{
154 size_t half_bits = ( bits + 1 ) / 2;
155 int ret;
156 int len = 0;
157 /* Construct something that looks like a DER encoding of
158 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
159 * RSAPrivateKey ::= SEQUENCE {
160 * version Version,
161 * modulus INTEGER, -- n
162 * publicExponent INTEGER, -- e
163 * privateExponent INTEGER, -- d
164 * prime1 INTEGER, -- p
165 * prime2 INTEGER, -- q
166 * exponent1 INTEGER, -- d mod (p-1)
167 * exponent2 INTEGER, -- d mod (q-1)
168 * coefficient INTEGER, -- (inverse of q) mod p
169 * otherPrimeInfos OtherPrimeInfos OPTIONAL
170 * }
171 * Or, for a public key, the same structure with only
172 * version, modulus and publicExponent.
173 */
174 *p = buffer + buffer_size;
175 if( keypair )
176 {
177 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
178 asn1_write_10x( p, buffer, half_bits, 1 ) );
179 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
180 asn1_write_10x( p, buffer, half_bits, 1 ) );
181 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
182 asn1_write_10x( p, buffer, half_bits, 1 ) );
183 MBEDTLS_ASN1_CHK_ADD( len, /* q */
184 asn1_write_10x( p, buffer, half_bits, 1 ) );
185 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
186 asn1_write_10x( p, buffer, half_bits, 3 ) );
187 MBEDTLS_ASN1_CHK_ADD( len, /* d */
188 asn1_write_10x( p, buffer, bits, 1 ) );
189 }
190 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
191 asn1_write_10x( p, buffer, 17, 1 ) );
192 MBEDTLS_ASN1_CHK_ADD( len, /* n */
193 asn1_write_10x( p, buffer, bits, 1 ) );
194 if( keypair )
195 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
196 mbedtls_asn1_write_int( p, buffer, 0 ) );
197 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
198 {
199 const unsigned char tag =
200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
201 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
202 }
203 return( len );
204}
205
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100206int exercise_mac_setup( psa_key_type_t key_type,
207 const unsigned char *key_bytes,
208 size_t key_length,
209 psa_algorithm_t alg,
210 psa_mac_operation_t *operation,
211 psa_status_t *status )
212{
213 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100215
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
217 psa_set_key_algorithm( &attributes, alg );
218 psa_set_key_type( &attributes, key_type );
219 PSA_ASSERT( psa_import_key( &attributes, &handle, key_bytes, key_length ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100220
221 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 /* Whether setup succeeded or failed, abort must succeed. */
223 PSA_ASSERT( psa_mac_abort( operation ) );
224 /* If setup failed, reproduce the failure, so that the caller can
225 * test the resulting state of the operation object. */
226 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100227 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100228 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
229 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230 }
231
232 psa_destroy_key( handle );
233 return( 1 );
234
235exit:
236 psa_destroy_key( handle );
237 return( 0 );
238}
239
240int exercise_cipher_setup( psa_key_type_t key_type,
241 const unsigned char *key_bytes,
242 size_t key_length,
243 psa_algorithm_t alg,
244 psa_cipher_operation_t *operation,
245 psa_status_t *status )
246{
247 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100249
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
251 psa_set_key_algorithm( &attributes, alg );
252 psa_set_key_type( &attributes, key_type );
253 PSA_ASSERT( psa_import_key( &attributes, &handle, key_bytes, key_length ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100254
255 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100256 /* Whether setup succeeded or failed, abort must succeed. */
257 PSA_ASSERT( psa_cipher_abort( operation ) );
258 /* If setup failed, reproduce the failure, so that the caller can
259 * test the resulting state of the operation object. */
260 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100261 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100262 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
263 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100264 }
265
266 psa_destroy_key( handle );
267 return( 1 );
268
269exit:
270 psa_destroy_key( handle );
271 return( 0 );
272}
273
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100274static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200275 psa_key_usage_t usage,
276 psa_algorithm_t alg )
277{
Jaeden Amero769ce272019-01-04 11:48:03 +0000278 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200279 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200280 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200281 size_t mac_length = sizeof( mac );
282
283 if( usage & PSA_KEY_USAGE_SIGN )
284 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100285 PSA_ASSERT( psa_mac_sign_setup( &operation,
286 handle, alg ) );
287 PSA_ASSERT( psa_mac_update( &operation,
288 input, sizeof( input ) ) );
289 PSA_ASSERT( psa_mac_sign_finish( &operation,
290 mac, sizeof( mac ),
291 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 }
293
294 if( usage & PSA_KEY_USAGE_VERIFY )
295 {
296 psa_status_t verify_status =
297 ( usage & PSA_KEY_USAGE_SIGN ?
298 PSA_SUCCESS :
299 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100300 PSA_ASSERT( psa_mac_verify_setup( &operation,
301 handle, alg ) );
302 PSA_ASSERT( psa_mac_update( &operation,
303 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100304 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
305 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 }
307
308 return( 1 );
309
310exit:
311 psa_mac_abort( &operation );
312 return( 0 );
313}
314
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100315static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200316 psa_key_usage_t usage,
317 psa_algorithm_t alg )
318{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000319 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200320 unsigned char iv[16] = {0};
321 size_t iv_length = sizeof( iv );
322 const unsigned char plaintext[16] = "Hello, world...";
323 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 unsigned char decrypted[sizeof( ciphertext )];
326 size_t part_length;
327
328 if( usage & PSA_KEY_USAGE_ENCRYPT )
329 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100330 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
331 handle, alg ) );
332 PSA_ASSERT( psa_cipher_generate_iv( &operation,
333 iv, sizeof( iv ),
334 &iv_length ) );
335 PSA_ASSERT( psa_cipher_update( &operation,
336 plaintext, sizeof( plaintext ),
337 ciphertext, sizeof( ciphertext ),
338 &ciphertext_length ) );
339 PSA_ASSERT( psa_cipher_finish( &operation,
340 ciphertext + ciphertext_length,
341 sizeof( ciphertext ) - ciphertext_length,
342 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200343 ciphertext_length += part_length;
344 }
345
346 if( usage & PSA_KEY_USAGE_DECRYPT )
347 {
348 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200350 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
351 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
354 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
355 * have this macro yet. */
356 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
357 psa_get_key_type( &attributes ) );
358 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200359 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100360 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
361 handle, alg ) );
362 PSA_ASSERT( psa_cipher_set_iv( &operation,
363 iv, iv_length ) );
364 PSA_ASSERT( psa_cipher_update( &operation,
365 ciphertext, ciphertext_length,
366 decrypted, sizeof( decrypted ),
367 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200368 status = psa_cipher_finish( &operation,
369 decrypted + part_length,
370 sizeof( decrypted ) - part_length,
371 &part_length );
372 /* For a stream cipher, all inputs are valid. For a block cipher,
373 * if the input is some aribtrary data rather than an actual
374 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200375 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200376 TEST_ASSERT( status == PSA_SUCCESS ||
377 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200378 else
379 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200380 }
381
382 return( 1 );
383
384exit:
385 psa_cipher_abort( &operation );
386 return( 0 );
387}
388
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200390 psa_key_usage_t usage,
391 psa_algorithm_t alg )
392{
393 unsigned char nonce[16] = {0};
394 size_t nonce_length = sizeof( nonce );
395 unsigned char plaintext[16] = "Hello, world...";
396 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
397 size_t ciphertext_length = sizeof( ciphertext );
398 size_t plaintext_length = sizeof( ciphertext );
399
400 if( usage & PSA_KEY_USAGE_ENCRYPT )
401 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_aead_encrypt( handle, alg,
403 nonce, nonce_length,
404 NULL, 0,
405 plaintext, sizeof( plaintext ),
406 ciphertext, sizeof( ciphertext ),
407 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200408 }
409
410 if( usage & PSA_KEY_USAGE_DECRYPT )
411 {
412 psa_status_t verify_status =
413 ( usage & PSA_KEY_USAGE_ENCRYPT ?
414 PSA_SUCCESS :
415 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100416 TEST_EQUAL( psa_aead_decrypt( handle, alg,
417 nonce, nonce_length,
418 NULL, 0,
419 ciphertext, ciphertext_length,
420 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100421 &plaintext_length ),
422 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200423 }
424
425 return( 1 );
426
427exit:
428 return( 0 );
429}
430
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100431static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200432 psa_key_usage_t usage,
433 psa_algorithm_t alg )
434{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200435 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
436 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200437 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200438 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100439 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
440
441 /* If the policy allows signing with any hash, just pick one. */
442 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
443 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100444#if defined(KNOWN_SUPPORTED_HASH_ALG)
445 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
446 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100447#else
448 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100449 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100450#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100451 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200452
453 if( usage & PSA_KEY_USAGE_SIGN )
454 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200455 /* Some algorithms require the payload to have the size of
456 * the hash encoded in the algorithm. Use this input size
457 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200458 if( hash_alg != 0 )
459 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100460 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
461 payload, payload_length,
462 signature, sizeof( signature ),
463 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200464 }
465
466 if( usage & PSA_KEY_USAGE_VERIFY )
467 {
468 psa_status_t verify_status =
469 ( usage & PSA_KEY_USAGE_SIGN ?
470 PSA_SUCCESS :
471 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
473 payload, payload_length,
474 signature, signature_length ),
475 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200476 }
477
478 return( 1 );
479
480exit:
481 return( 0 );
482}
483
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100484static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200485 psa_key_usage_t usage,
486 psa_algorithm_t alg )
487{
488 unsigned char plaintext[256] = "Hello, world...";
489 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
490 size_t ciphertext_length = sizeof( ciphertext );
491 size_t plaintext_length = 16;
492
493 if( usage & PSA_KEY_USAGE_ENCRYPT )
494 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100495 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
496 plaintext, plaintext_length,
497 NULL, 0,
498 ciphertext, sizeof( ciphertext ),
499 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200500 }
501
502 if( usage & PSA_KEY_USAGE_DECRYPT )
503 {
504 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100505 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200506 ciphertext, ciphertext_length,
507 NULL, 0,
508 plaintext, sizeof( plaintext ),
509 &plaintext_length );
510 TEST_ASSERT( status == PSA_SUCCESS ||
511 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
512 ( status == PSA_ERROR_INVALID_ARGUMENT ||
513 status == PSA_ERROR_INVALID_PADDING ) ) );
514 }
515
516 return( 1 );
517
518exit:
519 return( 0 );
520}
Gilles Peskine02b75072018-07-01 22:31:34 +0200521
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100522static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200523 psa_key_usage_t usage,
524 psa_algorithm_t alg )
525{
526 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
527 unsigned char label[16] = "This is a label.";
528 size_t label_length = sizeof( label );
529 unsigned char seed[16] = "abcdefghijklmnop";
530 size_t seed_length = sizeof( seed );
531 unsigned char output[1];
532
533 if( usage & PSA_KEY_USAGE_DERIVE )
534 {
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100535 if( PSA_ALG_IS_HKDF( alg ) )
536 {
537 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
538 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
539 PSA_KDF_STEP_SALT,
540 label,
541 label_length ) );
542 PSA_ASSERT( psa_key_derivation_input_key( &generator,
543 PSA_KDF_STEP_SECRET,
544 handle ) );
545 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
546 PSA_KDF_STEP_INFO,
547 seed,
548 seed_length ) );
549 }
550 else
551 {
552 // legacy
553 PSA_ASSERT( psa_key_derivation( &generator,
554 handle, alg,
555 label, label_length,
556 seed, seed_length,
557 sizeof( output ) ) );
558 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100559 PSA_ASSERT( psa_generator_read( &generator,
560 output,
561 sizeof( output ) ) );
562 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200563 }
564
565 return( 1 );
566
567exit:
568 return( 0 );
569}
570
Gilles Peskinec7998b72018-11-07 18:45:02 +0100571/* We need two keys to exercise key agreement. Exercise the
572 * private key against its own public key. */
573static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +0100574 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100575{
576 psa_key_type_t private_key_type;
577 psa_key_type_t public_key_type;
578 size_t key_bits;
579 uint8_t *public_key = NULL;
580 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200581 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinec7998b72018-11-07 18:45:02 +0100582 * psa_key_agreement fails. This isn't fully satisfactory, but it's
583 * good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200584 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100586
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200587 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
588 private_key_type = psa_get_key_type( &attributes );
589 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100590 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
591 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
592 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100593 PSA_ASSERT( psa_export_public_key( handle,
594 public_key, public_key_length,
595 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100596
Gilles Peskine969c5d62019-01-16 15:53:06 +0100597 status = psa_key_agreement( generator, PSA_KDF_STEP_SECRET, handle,
598 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100599exit:
600 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200601 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100602 return( status );
603}
604
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200605/* We need two keys to exercise key agreement. Exercise the
606 * private key against its own public key. */
607static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
608 psa_key_handle_t handle )
609{
610 psa_key_type_t private_key_type;
611 psa_key_type_t public_key_type;
612 size_t key_bits;
613 uint8_t *public_key = NULL;
614 size_t public_key_length;
615 uint8_t output[1024];
616 size_t output_length;
617 /* Return GENERIC_ERROR if something other than the final call to
618 * psa_key_agreement fails. This isn't fully satisfactory, but it's
619 * good enough: callers will report it as a failed test anyway. */
620 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200622
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200623 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
624 private_key_type = psa_get_key_type( &attributes );
625 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200626 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
627 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
628 ASSERT_ALLOC( public_key, public_key_length );
629 PSA_ASSERT( psa_export_public_key( handle,
630 public_key, public_key_length,
631 &public_key_length ) );
632
633 status = psa_key_agreement_raw_shared_secret(
634 alg, handle,
635 public_key, public_key_length,
636 output, sizeof( output ), &output_length );
637exit:
638 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200639 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200640 return( status );
641}
642
643static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
644 psa_key_usage_t usage,
645 psa_algorithm_t alg )
646{
647 int ok = 0;
648
649 if( usage & PSA_KEY_USAGE_DERIVE )
650 {
651 /* We need two keys to exercise key agreement. Exercise the
652 * private key against its own public key. */
653 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
654 }
655 ok = 1;
656
657exit:
658 return( ok );
659}
660
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100661static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200662 psa_key_usage_t usage,
663 psa_algorithm_t alg )
664{
665 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200666 unsigned char output[1];
667 int ok = 0;
668
669 if( usage & PSA_KEY_USAGE_DERIVE )
670 {
671 /* We need two keys to exercise key agreement. Exercise the
672 * private key against its own public key. */
Gilles Peskine969c5d62019-01-16 15:53:06 +0100673 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
674 PSA_ASSERT( key_agreement_with_self( &generator, handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_generator_read( &generator,
676 output,
677 sizeof( output ) ) );
678 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200679 }
680 ok = 1;
681
682exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200683 return( ok );
684}
685
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200686static int is_oid_of_key_type( psa_key_type_t type,
687 const uint8_t *oid, size_t oid_length )
688{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200689 const uint8_t *expected_oid = NULL;
690 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200691#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200692 if( PSA_KEY_TYPE_IS_RSA( type ) )
693 {
694 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
695 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
696 }
697 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200698#endif /* MBEDTLS_RSA_C */
699#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200700 if( PSA_KEY_TYPE_IS_ECC( type ) )
701 {
702 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
703 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
704 }
705 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200706#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200707 {
708 char message[40];
709 mbedtls_snprintf( message, sizeof( message ),
710 "OID not known for key type=0x%08lx",
711 (unsigned long) type );
712 test_fail( message, __LINE__, __FILE__ );
713 return( 0 );
714 }
715
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200716 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200717 return( 1 );
718
719exit:
720 return( 0 );
721}
722
723static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
724 size_t min_bits, size_t max_bits,
725 int must_be_odd )
726{
727 size_t len;
728 size_t actual_bits;
729 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100730 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100731 MBEDTLS_ASN1_INTEGER ),
732 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200733 /* Tolerate a slight departure from DER encoding:
734 * - 0 may be represented by an empty string or a 1-byte string.
735 * - The sign bit may be used as a value bit. */
736 if( ( len == 1 && ( *p )[0] == 0 ) ||
737 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
738 {
739 ++( *p );
740 --len;
741 }
742 if( min_bits == 0 && len == 0 )
743 return( 1 );
744 msb = ( *p )[0];
745 TEST_ASSERT( msb != 0 );
746 actual_bits = 8 * ( len - 1 );
747 while( msb != 0 )
748 {
749 msb >>= 1;
750 ++actual_bits;
751 }
752 TEST_ASSERT( actual_bits >= min_bits );
753 TEST_ASSERT( actual_bits <= max_bits );
754 if( must_be_odd )
755 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
756 *p += len;
757 return( 1 );
758exit:
759 return( 0 );
760}
761
762static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
763 size_t *len,
764 unsigned char n, unsigned char tag )
765{
766 int ret;
767 ret = mbedtls_asn1_get_tag( p, end, len,
768 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
769 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
770 if( ret != 0 )
771 return( ret );
772 end = *p + *len;
773 ret = mbedtls_asn1_get_tag( p, end, len, tag );
774 if( ret != 0 )
775 return( ret );
776 if( *p + *len != end )
777 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
778 return( 0 );
779}
780
781static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
782 uint8_t *exported, size_t exported_length )
783{
784 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100785 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200786 else
787 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200788
789#if defined(MBEDTLS_DES_C)
790 if( type == PSA_KEY_TYPE_DES )
791 {
792 /* Check the parity bits. */
793 unsigned i;
794 for( i = 0; i < bits / 8; i++ )
795 {
796 unsigned bit_count = 0;
797 unsigned m;
798 for( m = 1; m <= 0x100; m <<= 1 )
799 {
800 if( exported[i] & m )
801 ++bit_count;
802 }
803 TEST_ASSERT( bit_count % 2 != 0 );
804 }
805 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200806 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200807#endif
808
809#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
810 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
811 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200812 uint8_t *p = exported;
813 uint8_t *end = exported + exported_length;
814 size_t len;
815 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200816 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200817 * modulus INTEGER, -- n
818 * publicExponent INTEGER, -- e
819 * privateExponent INTEGER, -- d
820 * prime1 INTEGER, -- p
821 * prime2 INTEGER, -- q
822 * exponent1 INTEGER, -- d mod (p-1)
823 * exponent2 INTEGER, -- d mod (q-1)
824 * coefficient INTEGER, -- (inverse of q) mod p
825 * }
826 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100827 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
828 MBEDTLS_ASN1_SEQUENCE |
829 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
830 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200831 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
832 goto exit;
833 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
834 goto exit;
835 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
836 goto exit;
837 /* Require d to be at least half the size of n. */
838 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
839 goto exit;
840 /* Require p and q to be at most half the size of n, rounded up. */
841 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
842 goto exit;
843 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
844 goto exit;
845 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
846 goto exit;
847 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
848 goto exit;
849 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
850 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100851 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100852 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200853 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200854#endif /* MBEDTLS_RSA_C */
855
856#if defined(MBEDTLS_ECP_C)
857 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
858 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100859 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100860 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100861 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200862 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200863#endif /* MBEDTLS_ECP_C */
864
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200865 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
866 {
867 uint8_t *p = exported;
868 uint8_t *end = exported + exported_length;
869 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200870#if defined(MBEDTLS_RSA_C)
871 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
872 {
873 /* RSAPublicKey ::= SEQUENCE {
874 * modulus INTEGER, -- n
875 * publicExponent INTEGER } -- e
876 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100877 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
878 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100879 MBEDTLS_ASN1_CONSTRUCTED ),
880 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100881 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200882 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
883 goto exit;
884 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
885 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100886 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200887 }
888 else
889#endif /* MBEDTLS_RSA_C */
890#if defined(MBEDTLS_ECP_C)
891 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
892 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000893 /* The representation of an ECC public key is:
894 * - The byte 0x04;
895 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
896 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
897 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000898 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100899 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
900 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200901 }
902 else
903#endif /* MBEDTLS_ECP_C */
904 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100905 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200906 mbedtls_snprintf( message, sizeof( message ),
907 "No sanity check for public key type=0x%08lx",
908 (unsigned long) type );
909 test_fail( message, __LINE__, __FILE__ );
910 return( 0 );
911 }
912 }
913 else
914
915 {
916 /* No sanity checks for other types */
917 }
918
919 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200920
921exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200922 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200923}
924
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100925static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200926 psa_key_usage_t usage )
927{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200929 uint8_t *exported = NULL;
930 size_t exported_size = 0;
931 size_t exported_length = 0;
932 int ok = 0;
933
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200934 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200935
936 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200937 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200938 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100939 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
940 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200941 ok = 1;
942 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +0200943 }
944
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200945 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
946 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200947 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200948
Gilles Peskine8817f612018-12-18 00:18:46 +0100949 PSA_ASSERT( psa_export_key( handle,
950 exported, exported_size,
951 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200952 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
953 psa_get_key_bits( &attributes ),
954 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +0200955
956exit:
957 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200958 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200959 return( ok );
960}
961
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100962static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200963{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200965 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +0200966 uint8_t *exported = NULL;
967 size_t exported_size = 0;
968 size_t exported_length = 0;
969 int ok = 0;
970
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200971 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
972 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200973 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100974 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100975 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200976 return( 1 );
977 }
978
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200979 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(
980 psa_get_key_type( &attributes ) );
981 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
982 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200983 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200984
Gilles Peskine8817f612018-12-18 00:18:46 +0100985 PSA_ASSERT( psa_export_public_key( handle,
986 exported, exported_size,
987 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200988 ok = exported_key_sanity_check( public_type,
989 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +0200990 exported, exported_length );
991
992exit:
993 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200994 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200995 return( ok );
996}
997
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100998/** Do smoke tests on a key.
999 *
1000 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1001 * sign/verify, or derivation) that is permitted according to \p usage.
1002 * \p usage and \p alg should correspond to the expected policy on the
1003 * key.
1004 *
1005 * Export the key if permitted by \p usage, and check that the output
1006 * looks sensible. If \p usage forbids export, check that
1007 * \p psa_export_key correctly rejects the attempt. If the key is
1008 * asymmetric, also check \p psa_export_public_key.
1009 *
1010 * If the key fails the tests, this function calls the test framework's
1011 * `test_fail` function and returns false. Otherwise this function returns
1012 * true. Therefore it should be used as follows:
1013 * ```
1014 * if( ! exercise_key( ... ) ) goto exit;
1015 * ```
1016 *
1017 * \param handle The key to exercise. It should be capable of performing
1018 * \p alg.
1019 * \param usage The usage flags to assume.
1020 * \param alg The algorithm to exercise.
1021 *
1022 * \retval 0 The key failed the smoke tests.
1023 * \retval 1 The key passed the smoke tests.
1024 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001025static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001026 psa_key_usage_t usage,
1027 psa_algorithm_t alg )
1028{
1029 int ok;
1030 if( alg == 0 )
1031 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1032 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001033 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001034 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001035 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001036 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001038 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001039 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001040 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001041 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001042 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001043 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001044 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1045 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001046 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001047 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001048 else
1049 {
1050 char message[40];
1051 mbedtls_snprintf( message, sizeof( message ),
1052 "No code to exercise alg=0x%08lx",
1053 (unsigned long) alg );
1054 test_fail( message, __LINE__, __FILE__ );
1055 ok = 0;
1056 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001057
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001058 ok = ok && exercise_export_key( handle, usage );
1059 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001060
Gilles Peskine02b75072018-07-01 22:31:34 +02001061 return( ok );
1062}
1063
Gilles Peskine10df3412018-10-25 22:35:43 +02001064static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1065 psa_algorithm_t alg )
1066{
1067 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1068 {
1069 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1070 PSA_KEY_USAGE_VERIFY :
1071 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1072 }
1073 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1074 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1075 {
1076 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1077 PSA_KEY_USAGE_ENCRYPT :
1078 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1079 }
1080 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1081 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1082 {
1083 return( PSA_KEY_USAGE_DERIVE );
1084 }
1085 else
1086 {
1087 return( 0 );
1088 }
1089
1090}
Darryl Green0c6575a2018-11-07 16:05:30 +00001091
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001092static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1093{
1094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1095 uint8_t buffer[1];
1096 size_t length;
1097 int ok = 0;
1098
1099 psa_make_key_persistent( &attributes, 0x6964, PSA_KEY_LIFETIME_PERSISTENT );
1100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1101 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1102 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1103 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1104 PSA_ERROR_INVALID_HANDLE );
1105 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001106 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001107 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1108 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1109 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1110 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1111
1112 TEST_EQUAL( psa_export_key( handle,
1113 buffer, sizeof( buffer ), &length ),
1114 PSA_ERROR_INVALID_HANDLE );
1115 TEST_EQUAL( psa_export_public_key( handle,
1116 buffer, sizeof( buffer ), &length ),
1117 PSA_ERROR_INVALID_HANDLE );
1118
1119 TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
1120 TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE );
1121
1122 ok = 1;
1123
1124exit:
1125 psa_reset_key_attributes( &attributes );
1126 return( ok );
1127}
1128
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001129/* An overapproximation of the amount of storage needed for a key of the
1130 * given type and with the given content. The API doesn't make it easy
1131 * to find a good value for the size. The current implementation doesn't
1132 * care about the value anyway. */
1133#define KEY_BITS_FROM_DATA( type, data ) \
1134 ( data )->len
1135
Darryl Green0c6575a2018-11-07 16:05:30 +00001136typedef enum {
1137 IMPORT_KEY = 0,
1138 GENERATE_KEY = 1,
1139 DERIVE_KEY = 2
1140} generate_method;
1141
Gilles Peskinee59236f2018-01-27 23:32:46 +01001142/* END_HEADER */
1143
1144/* BEGIN_DEPENDENCIES
1145 * depends_on:MBEDTLS_PSA_CRYPTO_C
1146 * END_DEPENDENCIES
1147 */
1148
1149/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001150void static_checks( )
1151{
1152 size_t max_truncated_mac_size =
1153 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1154
1155 /* Check that the length for a truncated MAC always fits in the algorithm
1156 * encoding. The shifted mask is the maximum truncated value. The
1157 * untruncated algorithm may be one byte larger. */
1158 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1159}
1160/* END_CASE */
1161
1162/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001163void attributes_set_get( int id_arg, int lifetime_arg,
1164 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001165 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001166{
Gilles Peskine4747d192019-04-17 15:05:45 +02001167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001168 psa_key_id_t id = id_arg;
1169 psa_key_lifetime_t lifetime = lifetime_arg;
1170 psa_key_usage_t usage_flags = usage_flags_arg;
1171 psa_algorithm_t alg = alg_arg;
1172 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001173 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001174
1175 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1176 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1177 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1178 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1179 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001180 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001181
1182 psa_make_key_persistent( &attributes, id, lifetime );
1183 psa_set_key_usage_flags( &attributes, usage_flags );
1184 psa_set_key_algorithm( &attributes, alg );
1185 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001186 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001187
1188 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1189 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1190 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1191 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1192 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001193 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001194
1195 psa_reset_key_attributes( &attributes );
1196
1197 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1198 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1199 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1200 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1201 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001202 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001203}
1204/* END_CASE */
1205
1206/* BEGIN_CASE */
1207void import( data_t *data, int type_arg, int expected_status_arg )
1208{
1209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1210 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001211 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001212 psa_key_type_t type = type_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001213 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001214 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001215
Gilles Peskine8817f612018-12-18 00:18:46 +01001216 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001217
Gilles Peskine4747d192019-04-17 15:05:45 +02001218 psa_set_key_type( &attributes, type );
1219 status = psa_import_key( &attributes, &handle, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001220 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001221 if( status != PSA_SUCCESS )
1222 goto exit;
1223
1224 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1225 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1226
1227 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001228 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001229
1230exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001231 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001232 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233 mbedtls_psa_crypto_free( );
1234}
1235/* END_CASE */
1236
1237/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001238void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1239{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001240 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001241 size_t bits = bits_arg;
1242 psa_status_t expected_status = expected_status_arg;
1243 psa_status_t status;
1244 psa_key_type_t type =
1245 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1246 size_t buffer_size = /* Slight overapproximations */
1247 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001248 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001249 unsigned char *p;
1250 int ret;
1251 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001253
Gilles Peskine8817f612018-12-18 00:18:46 +01001254 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001255 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001256
1257 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1258 bits, keypair ) ) >= 0 );
1259 length = ret;
1260
1261 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001262 psa_set_key_type( &attributes, type );
1263 status = psa_import_key( &attributes, &handle, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001264 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001265 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001266 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001267
1268exit:
1269 mbedtls_free( buffer );
1270 mbedtls_psa_crypto_free( );
1271}
1272/* END_CASE */
1273
1274/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001275void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001276 int type_arg,
1277 int alg_arg,
1278 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001279 int expected_bits,
1280 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001281 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001282 int canonical_input )
1283{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001284 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001286 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001287 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001288 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001289 unsigned char *exported = NULL;
1290 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001291 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001292 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001293 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001294 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001295 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001296
Moran Pekercb088e72018-07-17 17:36:59 +03001297 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001298 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001300 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001301 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302
Gilles Peskine4747d192019-04-17 15:05:45 +02001303 psa_set_key_usage_flags( &attributes, usage_arg );
1304 psa_set_key_algorithm( &attributes, alg );
1305 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001306
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001308 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001309
1310 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001311 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1312 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1313 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001314
1315 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001316 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001317 exported, export_size,
1318 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001319 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001320
1321 /* The exported length must be set by psa_export_key() to a value between 0
1322 * and export_size. On errors, the exported length must be 0. */
1323 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1324 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1325 TEST_ASSERT( exported_length <= export_size );
1326
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001327 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001328 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001330 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001331 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001332 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001333 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001334
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001335 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001336 goto exit;
1337
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001338 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001339 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340 else
1341 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001342 psa_key_handle_t handle2;
Gilles Peskine4747d192019-04-17 15:05:45 +02001343 PSA_ASSERT( psa_import_key( &attributes, &handle2,
1344 exported, exported_length ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001345 PSA_ASSERT( psa_export_key( handle2,
1346 reexported,
1347 export_size,
1348 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001349 ASSERT_COMPARE( exported, exported_length,
1350 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001351 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001353 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001354
1355destroy:
1356 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001357 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001358 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001359
1360exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001361 mbedtls_free( exported );
1362 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001363 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364 mbedtls_psa_crypto_free( );
1365}
1366/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001367
Moran Pekerf709f4a2018-06-06 17:26:04 +03001368/* BEGIN_CASE */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001369void invalid_handle( int handle )
Moran Peker28a38e62018-11-07 16:18:24 +02001370{
Gilles Peskine8817f612018-12-18 00:18:46 +01001371 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001372 test_operations_on_invalid_handle( handle );
Moran Peker28a38e62018-11-07 16:18:24 +02001373
1374exit:
1375 mbedtls_psa_crypto_free( );
1376}
1377/* END_CASE */
1378
1379/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001380void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001381 int type_arg,
1382 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001383 int export_size_delta,
1384 int expected_export_status_arg,
1385 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001386{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001387 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001388 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001389 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001390 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001391 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001392 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001393 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001394 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001396
Gilles Peskine8817f612018-12-18 00:18:46 +01001397 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001398
Gilles Peskine4747d192019-04-17 15:05:45 +02001399 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1400 psa_set_key_algorithm( &attributes, alg );
1401 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001402
1403 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001404 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001405
Gilles Peskine49c25912018-10-29 15:15:31 +01001406 /* Export the public key */
1407 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001408 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001409 exported, export_size,
1410 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001411 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001412 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001413 {
1414 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1415 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001416 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1417 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001418 TEST_ASSERT( expected_public_key->len <=
1419 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001420 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1421 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001422 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001423
1424exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001425 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001426 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001427 psa_reset_key_attributes( &attributes );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001428 mbedtls_psa_crypto_free( );
1429}
1430/* END_CASE */
1431
Gilles Peskine20035e32018-02-03 22:44:14 +01001432/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001433void import_and_exercise_key( data_t *data,
1434 int type_arg,
1435 int bits_arg,
1436 int alg_arg )
1437{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001438 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001439 psa_key_type_t type = type_arg;
1440 size_t bits = bits_arg;
1441 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001442 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001444 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001445
Gilles Peskine8817f612018-12-18 00:18:46 +01001446 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001447
Gilles Peskine4747d192019-04-17 15:05:45 +02001448 psa_set_key_usage_flags( &attributes, usage );
1449 psa_set_key_algorithm( &attributes, alg );
1450 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001451
1452 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001453 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001454
1455 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001456 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1457 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1458 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001459
1460 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001461 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001462 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001463
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001464 PSA_ASSERT( psa_destroy_key( handle ) );
1465 test_operations_on_invalid_handle( handle );
1466
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001467exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001468 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001469 psa_reset_key_attributes( &got_attributes );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001470 mbedtls_psa_crypto_free( );
1471}
1472/* END_CASE */
1473
1474/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001475void key_policy( int usage_arg, int alg_arg )
1476{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001477 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001478 psa_algorithm_t alg = alg_arg;
1479 psa_key_usage_t usage = usage_arg;
1480 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1481 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001483
1484 memset( key, 0x2a, sizeof( key ) );
1485
Gilles Peskine8817f612018-12-18 00:18:46 +01001486 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001487
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001488 psa_set_key_usage_flags( &attributes, usage );
1489 psa_set_key_algorithm( &attributes, alg );
1490 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001491
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001492 PSA_ASSERT( psa_import_key( &attributes, &handle, key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001493
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001494 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1495 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1496 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1497 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001498
1499exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001500 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001501 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001502 mbedtls_psa_crypto_free( );
1503}
1504/* END_CASE */
1505
1506/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001507void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001508{
1509 /* Test each valid way of initializing the object, except for `= {0}`, as
1510 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1511 * though it's OK by the C standard. We could test for this, but we'd need
1512 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001513 psa_key_attributes_t func = psa_key_attributes_init( );
1514 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1515 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001516
1517 memset( &zero, 0, sizeof( zero ) );
1518
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001519 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1520 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1521 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001522
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001523 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1524 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1525 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1526
1527 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1528 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1529 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1530
1531 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1532 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1533 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1534
1535 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1536 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1537 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001538}
1539/* END_CASE */
1540
1541/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001542void mac_key_policy( int policy_usage,
1543 int policy_alg,
1544 int key_type,
1545 data_t *key_data,
1546 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001547{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001548 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001549 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001550 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001551 psa_status_t status;
1552 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001553
Gilles Peskine8817f612018-12-18 00:18:46 +01001554 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001555
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001556 psa_set_key_usage_flags( &attributes, policy_usage );
1557 psa_set_key_algorithm( &attributes, policy_alg );
1558 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001559
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001560 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001561 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001562
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564 if( policy_alg == exercise_alg &&
1565 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001566 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001567 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001568 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001569 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001570
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001571 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001572 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001573 if( policy_alg == exercise_alg &&
1574 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001575 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001576 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001577 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001578
1579exit:
1580 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001581 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001582 mbedtls_psa_crypto_free( );
1583}
1584/* END_CASE */
1585
1586/* BEGIN_CASE */
1587void cipher_key_policy( int policy_usage,
1588 int policy_alg,
1589 int key_type,
1590 data_t *key_data,
1591 int exercise_alg )
1592{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001593 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001594 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001595 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001596 psa_status_t status;
1597
Gilles Peskine8817f612018-12-18 00:18:46 +01001598 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001599
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001600 psa_set_key_usage_flags( &attributes, policy_usage );
1601 psa_set_key_algorithm( &attributes, policy_alg );
1602 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001604 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001605 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001606
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001607 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001608 if( policy_alg == exercise_alg &&
1609 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001611 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001612 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001613 psa_cipher_abort( &operation );
1614
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001615 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616 if( policy_alg == exercise_alg &&
1617 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001618 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001620 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001621
1622exit:
1623 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001624 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001625 mbedtls_psa_crypto_free( );
1626}
1627/* END_CASE */
1628
1629/* BEGIN_CASE */
1630void aead_key_policy( int policy_usage,
1631 int policy_alg,
1632 int key_type,
1633 data_t *key_data,
1634 int nonce_length_arg,
1635 int tag_length_arg,
1636 int exercise_alg )
1637{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001638 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001639 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001640 psa_status_t status;
1641 unsigned char nonce[16] = {0};
1642 size_t nonce_length = nonce_length_arg;
1643 unsigned char tag[16];
1644 size_t tag_length = tag_length_arg;
1645 size_t output_length;
1646
1647 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1648 TEST_ASSERT( tag_length <= sizeof( tag ) );
1649
Gilles Peskine8817f612018-12-18 00:18:46 +01001650 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001651
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001652 psa_set_key_usage_flags( &attributes, policy_usage );
1653 psa_set_key_algorithm( &attributes, policy_alg );
1654 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001655
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001656 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001657 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001658
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001659 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001660 nonce, nonce_length,
1661 NULL, 0,
1662 NULL, 0,
1663 tag, tag_length,
1664 &output_length );
1665 if( policy_alg == exercise_alg &&
1666 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001667 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001668 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001669 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001670
1671 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001672 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001673 nonce, nonce_length,
1674 NULL, 0,
1675 tag, tag_length,
1676 NULL, 0,
1677 &output_length );
1678 if( policy_alg == exercise_alg &&
1679 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001680 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001681 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001682 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001683
1684exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001685 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001686 mbedtls_psa_crypto_free( );
1687}
1688/* END_CASE */
1689
1690/* BEGIN_CASE */
1691void asymmetric_encryption_key_policy( int policy_usage,
1692 int policy_alg,
1693 int key_type,
1694 data_t *key_data,
1695 int exercise_alg )
1696{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001697 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001699 psa_status_t status;
1700 size_t key_bits;
1701 size_t buffer_length;
1702 unsigned char *buffer = NULL;
1703 size_t output_length;
1704
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001706
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001707 psa_set_key_usage_flags( &attributes, policy_usage );
1708 psa_set_key_algorithm( &attributes, policy_alg );
1709 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001710
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001711 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001712 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001713
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001714 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1715 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001716 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1717 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001718 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001719
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001720 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001721 NULL, 0,
1722 NULL, 0,
1723 buffer, buffer_length,
1724 &output_length );
1725 if( policy_alg == exercise_alg &&
1726 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001727 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001728 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001729 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001730
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001731 if( buffer_length != 0 )
1732 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001733 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001734 buffer, buffer_length,
1735 NULL, 0,
1736 buffer, buffer_length,
1737 &output_length );
1738 if( policy_alg == exercise_alg &&
1739 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001740 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001741 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001742 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001743
1744exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001745 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001746 psa_reset_key_attributes( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001747 mbedtls_psa_crypto_free( );
1748 mbedtls_free( buffer );
1749}
1750/* END_CASE */
1751
1752/* BEGIN_CASE */
1753void asymmetric_signature_key_policy( int policy_usage,
1754 int policy_alg,
1755 int key_type,
1756 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001757 int exercise_alg,
1758 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001759{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001760 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001761 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001762 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001763 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1764 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1765 * compatible with the policy and `payload_length_arg` is supposed to be
1766 * a valid input length to sign. If `payload_length_arg <= 0`,
1767 * `exercise_alg` is supposed to be forbidden by the policy. */
1768 int compatible_alg = payload_length_arg > 0;
1769 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001770 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1771 size_t signature_length;
1772
Gilles Peskine8817f612018-12-18 00:18:46 +01001773 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001774
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001775 psa_set_key_usage_flags( &attributes, policy_usage );
1776 psa_set_key_algorithm( &attributes, policy_alg );
1777 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001778
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001779 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001781
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001782 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001783 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001784 signature, sizeof( signature ),
1785 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001786 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001787 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001788 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001789 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001790
1791 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001792 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001793 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001794 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001795 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001796 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001797 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001798 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001799
1800exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001801 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001802 mbedtls_psa_crypto_free( );
1803}
1804/* END_CASE */
1805
1806/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001807void derive_key_policy( int policy_usage,
1808 int policy_alg,
1809 int key_type,
1810 data_t *key_data,
1811 int exercise_alg )
1812{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001813 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001815 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1816 psa_status_t status;
1817
Gilles Peskine8817f612018-12-18 00:18:46 +01001818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001819
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001820 psa_set_key_usage_flags( &attributes, policy_usage );
1821 psa_set_key_algorithm( &attributes, policy_alg );
1822 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001823
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001824 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001825 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001826
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001827 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001828 exercise_alg,
1829 NULL, 0,
1830 NULL, 0,
1831 1 );
1832 if( policy_alg == exercise_alg &&
1833 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001834 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001835 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001836 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001837
1838exit:
1839 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001840 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001841 mbedtls_psa_crypto_free( );
1842}
1843/* END_CASE */
1844
1845/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001846void agreement_key_policy( int policy_usage,
1847 int policy_alg,
1848 int key_type_arg,
1849 data_t *key_data,
1850 int exercise_alg )
1851{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001852 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001854 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001855 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1856 psa_status_t status;
1857
Gilles Peskine8817f612018-12-18 00:18:46 +01001858 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001859
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001860 psa_set_key_usage_flags( &attributes, policy_usage );
1861 psa_set_key_algorithm( &attributes, policy_alg );
1862 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001863
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001864 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001865 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001866
Gilles Peskine969c5d62019-01-16 15:53:06 +01001867 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
1868 status = key_agreement_with_self( &generator, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001869
Gilles Peskine01d718c2018-09-18 12:01:02 +02001870 if( policy_alg == exercise_alg &&
1871 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001872 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001873 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001874 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001875
1876exit:
1877 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001878 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001879 mbedtls_psa_crypto_free( );
1880}
1881/* END_CASE */
1882
1883/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001884void raw_agreement_key_policy( int policy_usage,
1885 int policy_alg,
1886 int key_type_arg,
1887 data_t *key_data,
1888 int exercise_alg )
1889{
1890 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001892 psa_key_type_t key_type = key_type_arg;
1893 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1894 psa_status_t status;
1895
1896 PSA_ASSERT( psa_crypto_init( ) );
1897
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001898 psa_set_key_usage_flags( &attributes, policy_usage );
1899 psa_set_key_algorithm( &attributes, policy_alg );
1900 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001901
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001902 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001903 key_data->x, key_data->len ) );
1904
1905 status = raw_key_agreement_with_self( exercise_alg, handle );
1906
1907 if( policy_alg == exercise_alg &&
1908 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1909 PSA_ASSERT( status );
1910 else
1911 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1912
1913exit:
1914 psa_generator_abort( &generator );
1915 psa_destroy_key( handle );
1916 mbedtls_psa_crypto_free( );
1917}
1918/* END_CASE */
1919
1920/* BEGIN_CASE */
Gilles Peskineca25db92019-04-19 11:43:08 +02001921void copy_key( int source_usage_arg, int source_alg_arg,
1922 int type_arg, data_t *material,
1923 int copy_attributes,
1924 int target_usage_arg, int target_alg_arg,
1925 int expected_status_arg,
1926 int expected_usage_arg, int expected_alg_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001927{
Gilles Peskineca25db92019-04-19 11:43:08 +02001928 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1929 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001930 psa_key_usage_t expected_usage = expected_usage_arg;
1931 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02001932 psa_key_handle_t source_handle = 0;
1933 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001934 uint8_t *export_buffer = NULL;
1935
Gilles Peskine57ab7212019-01-28 13:03:09 +01001936 PSA_ASSERT( psa_crypto_init( ) );
1937
Gilles Peskineca25db92019-04-19 11:43:08 +02001938 /* Prepare the source key. */
1939 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1940 psa_set_key_algorithm( &source_attributes, source_alg_arg );
1941 psa_set_key_type( &source_attributes, type_arg );
1942 PSA_ASSERT( psa_import_key( &source_attributes, &source_handle,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001943 material->x, material->len ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001944 /* Retrieve the key size. */
1945 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001946
Gilles Peskineca25db92019-04-19 11:43:08 +02001947 /* Prepare the target attributes. */
1948 if( copy_attributes )
1949 target_attributes = source_attributes;
1950 if( target_usage_arg != -1 )
1951 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1952 if( target_alg_arg != -1 )
1953 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001954
1955 /* Copy the key. */
Gilles Peskineca25db92019-04-19 11:43:08 +02001956 TEST_EQUAL( psa_copy_key( source_handle,
1957 &target_attributes, &target_handle ),
1958 expected_status_arg );
1959 if( expected_status_arg != PSA_SUCCESS )
1960 {
1961 TEST_EQUAL( target_handle, 0 );
1962 goto exit;
1963 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001964
1965 /* Destroy the source to ensure that this doesn't affect the target. */
1966 PSA_ASSERT( psa_destroy_key( source_handle ) );
1967
1968 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02001969 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
1970 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1971 psa_get_key_type( &target_attributes ) );
1972 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1973 psa_get_key_bits( &target_attributes ) );
1974 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1975 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001976 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1977 {
1978 size_t length;
1979 ASSERT_ALLOC( export_buffer, material->len );
1980 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
1981 material->len, &length ) );
1982 ASSERT_COMPARE( material->x, material->len,
1983 export_buffer, length );
1984 }
1985 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
1986 goto exit;
1987
1988 PSA_ASSERT( psa_close_key( target_handle ) );
1989
1990exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001991 psa_reset_key_attributes( &source_attributes );
1992 psa_reset_key_attributes( &target_attributes );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001993 mbedtls_psa_crypto_free( );
1994 mbedtls_free( export_buffer );
1995}
1996/* END_CASE */
1997
1998/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001999void hash_operation_init( )
2000{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002001 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002002 /* Test each valid way of initializing the object, except for `= {0}`, as
2003 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2004 * though it's OK by the C standard. We could test for this, but we'd need
2005 * to supress the Clang warning for the test. */
2006 psa_hash_operation_t func = psa_hash_operation_init( );
2007 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2008 psa_hash_operation_t zero;
2009
2010 memset( &zero, 0, sizeof( zero ) );
2011
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002012 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002013 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2014 PSA_ERROR_BAD_STATE );
2015 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2016 PSA_ERROR_BAD_STATE );
2017 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2018 PSA_ERROR_BAD_STATE );
2019
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002020 /* A default hash operation should be abortable without error. */
2021 PSA_ASSERT( psa_hash_abort( &func ) );
2022 PSA_ASSERT( psa_hash_abort( &init ) );
2023 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002024}
2025/* END_CASE */
2026
2027/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002028void hash_setup( int alg_arg,
2029 int expected_status_arg )
2030{
2031 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002032 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002033 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002034 psa_status_t status;
2035
Gilles Peskine8817f612018-12-18 00:18:46 +01002036 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002037
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002038 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002039 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002040
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002041 /* Whether setup succeeded or failed, abort must succeed. */
2042 PSA_ASSERT( psa_hash_abort( &operation ) );
2043
2044 /* If setup failed, reproduce the failure, so as to
2045 * test the resulting state of the operation object. */
2046 if( status != PSA_SUCCESS )
2047 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2048
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002049 /* Now the operation object should be reusable. */
2050#if defined(KNOWN_SUPPORTED_HASH_ALG)
2051 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2052 PSA_ASSERT( psa_hash_abort( &operation ) );
2053#endif
2054
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002055exit:
2056 mbedtls_psa_crypto_free( );
2057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002061void hash_bad_order( )
2062{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002063 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002064 unsigned char input[] = "";
2065 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002066 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002067 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2068 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2069 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002070 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002071 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002072 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002073
Gilles Peskine8817f612018-12-18 00:18:46 +01002074 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002075
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002076 /* Call setup twice in a row. */
2077 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2078 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2079 PSA_ERROR_BAD_STATE );
2080 PSA_ASSERT( psa_hash_abort( &operation ) );
2081
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002082 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002083 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002084 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002085 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002086
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002087 /* Call update after finish. */
2088 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2089 PSA_ASSERT( psa_hash_finish( &operation,
2090 hash, sizeof( hash ), &hash_len ) );
2091 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002092 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002093 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002094
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002095 /* Call verify without calling setup beforehand. */
2096 TEST_EQUAL( psa_hash_verify( &operation,
2097 valid_hash, sizeof( valid_hash ) ),
2098 PSA_ERROR_BAD_STATE );
2099 PSA_ASSERT( psa_hash_abort( &operation ) );
2100
2101 /* Call verify after finish. */
2102 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2103 PSA_ASSERT( psa_hash_finish( &operation,
2104 hash, sizeof( hash ), &hash_len ) );
2105 TEST_EQUAL( psa_hash_verify( &operation,
2106 valid_hash, sizeof( valid_hash ) ),
2107 PSA_ERROR_BAD_STATE );
2108 PSA_ASSERT( psa_hash_abort( &operation ) );
2109
2110 /* Call verify twice in a row. */
2111 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2112 PSA_ASSERT( psa_hash_verify( &operation,
2113 valid_hash, sizeof( valid_hash ) ) );
2114 TEST_EQUAL( psa_hash_verify( &operation,
2115 valid_hash, sizeof( valid_hash ) ),
2116 PSA_ERROR_BAD_STATE );
2117 PSA_ASSERT( psa_hash_abort( &operation ) );
2118
2119 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002120 TEST_EQUAL( psa_hash_finish( &operation,
2121 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002122 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002123 PSA_ASSERT( psa_hash_abort( &operation ) );
2124
2125 /* Call finish twice in a row. */
2126 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2127 PSA_ASSERT( psa_hash_finish( &operation,
2128 hash, sizeof( hash ), &hash_len ) );
2129 TEST_EQUAL( psa_hash_finish( &operation,
2130 hash, sizeof( hash ), &hash_len ),
2131 PSA_ERROR_BAD_STATE );
2132 PSA_ASSERT( psa_hash_abort( &operation ) );
2133
2134 /* Call finish after calling verify. */
2135 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2136 PSA_ASSERT( psa_hash_verify( &operation,
2137 valid_hash, sizeof( valid_hash ) ) );
2138 TEST_EQUAL( psa_hash_finish( &operation,
2139 hash, sizeof( hash ), &hash_len ),
2140 PSA_ERROR_BAD_STATE );
2141 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002142
2143exit:
2144 mbedtls_psa_crypto_free( );
2145}
2146/* END_CASE */
2147
itayzafrir27e69452018-11-01 14:26:34 +02002148/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2149void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002150{
2151 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002152 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2153 * appended to it */
2154 unsigned char hash[] = {
2155 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2156 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2157 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002158 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002159 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002160
Gilles Peskine8817f612018-12-18 00:18:46 +01002161 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002162
itayzafrir27e69452018-11-01 14:26:34 +02002163 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002164 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002165 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002166 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002167
itayzafrir27e69452018-11-01 14:26:34 +02002168 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002169 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002170 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002171 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002172
itayzafrir27e69452018-11-01 14:26:34 +02002173 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002174 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002175 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002176 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002177
itayzafrirec93d302018-10-18 18:01:10 +03002178exit:
2179 mbedtls_psa_crypto_free( );
2180}
2181/* END_CASE */
2182
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002183/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2184void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002185{
2186 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002187 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002188 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002189 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002190 size_t hash_len;
2191
Gilles Peskine8817f612018-12-18 00:18:46 +01002192 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002193
itayzafrir58028322018-10-25 10:22:01 +03002194 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002195 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002196 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002197 hash, expected_size - 1, &hash_len ),
2198 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002199
2200exit:
2201 mbedtls_psa_crypto_free( );
2202}
2203/* END_CASE */
2204
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002205/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2206void hash_clone_source_state( )
2207{
2208 psa_algorithm_t alg = PSA_ALG_SHA_256;
2209 unsigned char hash[PSA_HASH_MAX_SIZE];
2210 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2211 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2212 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2213 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2214 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2215 size_t hash_len;
2216
2217 PSA_ASSERT( psa_crypto_init( ) );
2218 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2219
2220 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2221 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2222 PSA_ASSERT( psa_hash_finish( &op_finished,
2223 hash, sizeof( hash ), &hash_len ) );
2224 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2225 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2226
2227 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2228 PSA_ERROR_BAD_STATE );
2229
2230 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2231 PSA_ASSERT( psa_hash_finish( &op_init,
2232 hash, sizeof( hash ), &hash_len ) );
2233 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2234 PSA_ASSERT( psa_hash_finish( &op_finished,
2235 hash, sizeof( hash ), &hash_len ) );
2236 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2237 PSA_ASSERT( psa_hash_finish( &op_aborted,
2238 hash, sizeof( hash ), &hash_len ) );
2239
2240exit:
2241 psa_hash_abort( &op_source );
2242 psa_hash_abort( &op_init );
2243 psa_hash_abort( &op_setup );
2244 psa_hash_abort( &op_finished );
2245 psa_hash_abort( &op_aborted );
2246 mbedtls_psa_crypto_free( );
2247}
2248/* END_CASE */
2249
2250/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2251void hash_clone_target_state( )
2252{
2253 psa_algorithm_t alg = PSA_ALG_SHA_256;
2254 unsigned char hash[PSA_HASH_MAX_SIZE];
2255 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2256 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2257 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2258 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2259 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2260 size_t hash_len;
2261
2262 PSA_ASSERT( psa_crypto_init( ) );
2263
2264 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2265 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2266 PSA_ASSERT( psa_hash_finish( &op_finished,
2267 hash, sizeof( hash ), &hash_len ) );
2268 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2269 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2270
2271 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2272 PSA_ASSERT( psa_hash_finish( &op_target,
2273 hash, sizeof( hash ), &hash_len ) );
2274
2275 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2276 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2277 PSA_ERROR_BAD_STATE );
2278 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2279 PSA_ERROR_BAD_STATE );
2280
2281exit:
2282 psa_hash_abort( &op_target );
2283 psa_hash_abort( &op_init );
2284 psa_hash_abort( &op_setup );
2285 psa_hash_abort( &op_finished );
2286 psa_hash_abort( &op_aborted );
2287 mbedtls_psa_crypto_free( );
2288}
2289/* END_CASE */
2290
itayzafrir58028322018-10-25 10:22:01 +03002291/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002292void mac_operation_init( )
2293{
Jaeden Amero252ef282019-02-15 14:05:35 +00002294 const uint8_t input[1] = { 0 };
2295
Jaeden Amero769ce272019-01-04 11:48:03 +00002296 /* Test each valid way of initializing the object, except for `= {0}`, as
2297 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2298 * though it's OK by the C standard. We could test for this, but we'd need
2299 * to supress the Clang warning for the test. */
2300 psa_mac_operation_t func = psa_mac_operation_init( );
2301 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2302 psa_mac_operation_t zero;
2303
2304 memset( &zero, 0, sizeof( zero ) );
2305
Jaeden Amero252ef282019-02-15 14:05:35 +00002306 /* A freshly-initialized MAC operation should not be usable. */
2307 TEST_EQUAL( psa_mac_update( &func,
2308 input, sizeof( input ) ),
2309 PSA_ERROR_BAD_STATE );
2310 TEST_EQUAL( psa_mac_update( &init,
2311 input, sizeof( input ) ),
2312 PSA_ERROR_BAD_STATE );
2313 TEST_EQUAL( psa_mac_update( &zero,
2314 input, sizeof( input ) ),
2315 PSA_ERROR_BAD_STATE );
2316
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002317 /* A default MAC operation should be abortable without error. */
2318 PSA_ASSERT( psa_mac_abort( &func ) );
2319 PSA_ASSERT( psa_mac_abort( &init ) );
2320 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002321}
2322/* END_CASE */
2323
2324/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002325void mac_setup( int key_type_arg,
2326 data_t *key,
2327 int alg_arg,
2328 int expected_status_arg )
2329{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002330 psa_key_type_t key_type = key_type_arg;
2331 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002332 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002333 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002334 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2335#if defined(KNOWN_SUPPORTED_MAC_ALG)
2336 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2337#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002338
Gilles Peskine8817f612018-12-18 00:18:46 +01002339 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002341 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2342 &operation, &status ) )
2343 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002344 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002346 /* The operation object should be reusable. */
2347#if defined(KNOWN_SUPPORTED_MAC_ALG)
2348 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2349 smoke_test_key_data,
2350 sizeof( smoke_test_key_data ),
2351 KNOWN_SUPPORTED_MAC_ALG,
2352 &operation, &status ) )
2353 goto exit;
2354 TEST_EQUAL( status, PSA_SUCCESS );
2355#endif
2356
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002357exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002358 mbedtls_psa_crypto_free( );
2359}
2360/* END_CASE */
2361
2362/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002363void mac_bad_order( )
2364{
2365 psa_key_handle_t handle = 0;
2366 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2367 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2368 const uint8_t key[] = {
2369 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2370 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2371 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002373 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2374 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2375 size_t sign_mac_length = 0;
2376 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2377 const uint8_t verify_mac[] = {
2378 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2379 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2380 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2381
2382 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002383 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2384 psa_set_key_algorithm( &attributes, alg );
2385 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002387 PSA_ASSERT( psa_import_key( &attributes, &handle,
Jaeden Amero252ef282019-02-15 14:05:35 +00002388 key, sizeof(key) ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002389
Jaeden Amero252ef282019-02-15 14:05:35 +00002390 /* Call update without calling setup beforehand. */
2391 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2392 PSA_ERROR_BAD_STATE );
2393 PSA_ASSERT( psa_mac_abort( &operation ) );
2394
2395 /* Call sign finish without calling setup beforehand. */
2396 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2397 &sign_mac_length),
2398 PSA_ERROR_BAD_STATE );
2399 PSA_ASSERT( psa_mac_abort( &operation ) );
2400
2401 /* Call verify finish without calling setup beforehand. */
2402 TEST_EQUAL( psa_mac_verify_finish( &operation,
2403 verify_mac, sizeof( verify_mac ) ),
2404 PSA_ERROR_BAD_STATE );
2405 PSA_ASSERT( psa_mac_abort( &operation ) );
2406
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002407 /* Call setup twice in a row. */
2408 PSA_ASSERT( psa_mac_sign_setup( &operation,
2409 handle, alg ) );
2410 TEST_EQUAL( psa_mac_sign_setup( &operation,
2411 handle, alg ),
2412 PSA_ERROR_BAD_STATE );
2413 PSA_ASSERT( psa_mac_abort( &operation ) );
2414
Jaeden Amero252ef282019-02-15 14:05:35 +00002415 /* Call update after sign finish. */
2416 PSA_ASSERT( psa_mac_sign_setup( &operation,
2417 handle, alg ) );
2418 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2419 PSA_ASSERT( psa_mac_sign_finish( &operation,
2420 sign_mac, sizeof( sign_mac ),
2421 &sign_mac_length ) );
2422 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2423 PSA_ERROR_BAD_STATE );
2424 PSA_ASSERT( psa_mac_abort( &operation ) );
2425
2426 /* Call update after verify finish. */
2427 PSA_ASSERT( psa_mac_verify_setup( &operation,
2428 handle, alg ) );
2429 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2430 PSA_ASSERT( psa_mac_verify_finish( &operation,
2431 verify_mac, sizeof( verify_mac ) ) );
2432 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2433 PSA_ERROR_BAD_STATE );
2434 PSA_ASSERT( psa_mac_abort( &operation ) );
2435
2436 /* Call sign finish twice in a row. */
2437 PSA_ASSERT( psa_mac_sign_setup( &operation,
2438 handle, alg ) );
2439 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2440 PSA_ASSERT( psa_mac_sign_finish( &operation,
2441 sign_mac, sizeof( sign_mac ),
2442 &sign_mac_length ) );
2443 TEST_EQUAL( psa_mac_sign_finish( &operation,
2444 sign_mac, sizeof( sign_mac ),
2445 &sign_mac_length ),
2446 PSA_ERROR_BAD_STATE );
2447 PSA_ASSERT( psa_mac_abort( &operation ) );
2448
2449 /* Call verify finish twice in a row. */
2450 PSA_ASSERT( psa_mac_verify_setup( &operation,
2451 handle, alg ) );
2452 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2453 PSA_ASSERT( psa_mac_verify_finish( &operation,
2454 verify_mac, sizeof( verify_mac ) ) );
2455 TEST_EQUAL( psa_mac_verify_finish( &operation,
2456 verify_mac, sizeof( verify_mac ) ),
2457 PSA_ERROR_BAD_STATE );
2458 PSA_ASSERT( psa_mac_abort( &operation ) );
2459
2460 /* Setup sign but try verify. */
2461 PSA_ASSERT( psa_mac_sign_setup( &operation,
2462 handle, alg ) );
2463 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2464 TEST_EQUAL( psa_mac_verify_finish( &operation,
2465 verify_mac, sizeof( verify_mac ) ),
2466 PSA_ERROR_BAD_STATE );
2467 PSA_ASSERT( psa_mac_abort( &operation ) );
2468
2469 /* Setup verify but try sign. */
2470 PSA_ASSERT( psa_mac_verify_setup( &operation,
2471 handle, alg ) );
2472 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2473 TEST_EQUAL( psa_mac_sign_finish( &operation,
2474 sign_mac, sizeof( sign_mac ),
2475 &sign_mac_length ),
2476 PSA_ERROR_BAD_STATE );
2477 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002478
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002479exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002480 mbedtls_psa_crypto_free( );
2481}
2482/* END_CASE */
2483
2484/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002485void mac_sign( int key_type_arg,
2486 data_t *key,
2487 int alg_arg,
2488 data_t *input,
2489 data_t *expected_mac )
2490{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002491 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002492 psa_key_type_t key_type = key_type_arg;
2493 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002494 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002496 /* Leave a little extra room in the output buffer. At the end of the
2497 * test, we'll check that the implementation didn't overwrite onto
2498 * this extra room. */
2499 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2500 size_t mac_buffer_size =
2501 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2502 size_t mac_length = 0;
2503
2504 memset( actual_mac, '+', sizeof( actual_mac ) );
2505 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2506 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2507
Gilles Peskine8817f612018-12-18 00:18:46 +01002508 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002509
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002510 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2511 psa_set_key_algorithm( &attributes, alg );
2512 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002513
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002514 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002515 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002516
2517 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002518 PSA_ASSERT( psa_mac_sign_setup( &operation,
2519 handle, alg ) );
2520 PSA_ASSERT( psa_mac_update( &operation,
2521 input->x, input->len ) );
2522 PSA_ASSERT( psa_mac_sign_finish( &operation,
2523 actual_mac, mac_buffer_size,
2524 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002525
2526 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002527 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2528 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002529
2530 /* Verify that the end of the buffer is untouched. */
2531 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2532 sizeof( actual_mac ) - mac_length ) );
2533
2534exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002535 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002536 mbedtls_psa_crypto_free( );
2537}
2538/* END_CASE */
2539
2540/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002541void mac_verify( int key_type_arg,
2542 data_t *key,
2543 int alg_arg,
2544 data_t *input,
2545 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002547 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 psa_key_type_t key_type = key_type_arg;
2549 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002550 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552
Gilles Peskine69c12672018-06-28 00:07:19 +02002553 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2554
Gilles Peskine8817f612018-12-18 00:18:46 +01002555 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002557 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2558 psa_set_key_algorithm( &attributes, alg );
2559 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002560
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002561 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002562 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002563
Gilles Peskine8817f612018-12-18 00:18:46 +01002564 PSA_ASSERT( psa_mac_verify_setup( &operation,
2565 handle, alg ) );
2566 PSA_ASSERT( psa_destroy_key( handle ) );
2567 PSA_ASSERT( psa_mac_update( &operation,
2568 input->x, input->len ) );
2569 PSA_ASSERT( psa_mac_verify_finish( &operation,
2570 expected_mac->x,
2571 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572
2573exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002574 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575 mbedtls_psa_crypto_free( );
2576}
2577/* END_CASE */
2578
2579/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002580void cipher_operation_init( )
2581{
Jaeden Ameroab439972019-02-15 14:12:05 +00002582 const uint8_t input[1] = { 0 };
2583 unsigned char output[1] = { 0 };
2584 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002585 /* Test each valid way of initializing the object, except for `= {0}`, as
2586 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2587 * though it's OK by the C standard. We could test for this, but we'd need
2588 * to supress the Clang warning for the test. */
2589 psa_cipher_operation_t func = psa_cipher_operation_init( );
2590 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2591 psa_cipher_operation_t zero;
2592
2593 memset( &zero, 0, sizeof( zero ) );
2594
Jaeden Ameroab439972019-02-15 14:12:05 +00002595 /* A freshly-initialized cipher operation should not be usable. */
2596 TEST_EQUAL( psa_cipher_update( &func,
2597 input, sizeof( input ),
2598 output, sizeof( output ),
2599 &output_length ),
2600 PSA_ERROR_BAD_STATE );
2601 TEST_EQUAL( psa_cipher_update( &init,
2602 input, sizeof( input ),
2603 output, sizeof( output ),
2604 &output_length ),
2605 PSA_ERROR_BAD_STATE );
2606 TEST_EQUAL( psa_cipher_update( &zero,
2607 input, sizeof( input ),
2608 output, sizeof( output ),
2609 &output_length ),
2610 PSA_ERROR_BAD_STATE );
2611
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002612 /* A default cipher operation should be abortable without error. */
2613 PSA_ASSERT( psa_cipher_abort( &func ) );
2614 PSA_ASSERT( psa_cipher_abort( &init ) );
2615 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002616}
2617/* END_CASE */
2618
2619/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002620void cipher_setup( int key_type_arg,
2621 data_t *key,
2622 int alg_arg,
2623 int expected_status_arg )
2624{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002625 psa_key_type_t key_type = key_type_arg;
2626 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002627 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002628 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002629 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002630#if defined(KNOWN_SUPPORTED_MAC_ALG)
2631 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2632#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002633
Gilles Peskine8817f612018-12-18 00:18:46 +01002634 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002635
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002636 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2637 &operation, &status ) )
2638 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002639 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002640
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002641 /* The operation object should be reusable. */
2642#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2643 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2644 smoke_test_key_data,
2645 sizeof( smoke_test_key_data ),
2646 KNOWN_SUPPORTED_CIPHER_ALG,
2647 &operation, &status ) )
2648 goto exit;
2649 TEST_EQUAL( status, PSA_SUCCESS );
2650#endif
2651
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002652exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002653 mbedtls_psa_crypto_free( );
2654}
2655/* END_CASE */
2656
2657/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002658void cipher_bad_order( )
2659{
2660 psa_key_handle_t handle = 0;
2661 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2662 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002664 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2665 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2666 const uint8_t key[] = {
2667 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2668 0xaa, 0xaa, 0xaa, 0xaa };
2669 const uint8_t text[] = {
2670 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2671 0xbb, 0xbb, 0xbb, 0xbb };
2672 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2673 size_t length = 0;
2674
2675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002676 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2677 psa_set_key_algorithm( &attributes, alg );
2678 psa_set_key_type( &attributes, key_type );
2679 PSA_ASSERT( psa_import_key( &attributes, &handle,
Jaeden Ameroab439972019-02-15 14:12:05 +00002680 key, sizeof(key) ) );
2681
2682
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002683 /* Call encrypt setup twice in a row. */
2684 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2685 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2686 PSA_ERROR_BAD_STATE );
2687 PSA_ASSERT( psa_cipher_abort( &operation ) );
2688
2689 /* Call decrypt setup twice in a row. */
2690 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2691 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2692 PSA_ERROR_BAD_STATE );
2693 PSA_ASSERT( psa_cipher_abort( &operation ) );
2694
Jaeden Ameroab439972019-02-15 14:12:05 +00002695 /* Generate an IV without calling setup beforehand. */
2696 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2697 buffer, sizeof( buffer ),
2698 &length ),
2699 PSA_ERROR_BAD_STATE );
2700 PSA_ASSERT( psa_cipher_abort( &operation ) );
2701
2702 /* Generate an IV twice in a row. */
2703 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2704 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2705 buffer, sizeof( buffer ),
2706 &length ) );
2707 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2708 buffer, sizeof( buffer ),
2709 &length ),
2710 PSA_ERROR_BAD_STATE );
2711 PSA_ASSERT( psa_cipher_abort( &operation ) );
2712
2713 /* Generate an IV after it's already set. */
2714 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2715 PSA_ASSERT( psa_cipher_set_iv( &operation,
2716 iv, sizeof( iv ) ) );
2717 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2718 buffer, sizeof( buffer ),
2719 &length ),
2720 PSA_ERROR_BAD_STATE );
2721 PSA_ASSERT( psa_cipher_abort( &operation ) );
2722
2723 /* Set an IV without calling setup beforehand. */
2724 TEST_EQUAL( psa_cipher_set_iv( &operation,
2725 iv, sizeof( iv ) ),
2726 PSA_ERROR_BAD_STATE );
2727 PSA_ASSERT( psa_cipher_abort( &operation ) );
2728
2729 /* Set an IV after it's already set. */
2730 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2731 PSA_ASSERT( psa_cipher_set_iv( &operation,
2732 iv, sizeof( iv ) ) );
2733 TEST_EQUAL( psa_cipher_set_iv( &operation,
2734 iv, sizeof( iv ) ),
2735 PSA_ERROR_BAD_STATE );
2736 PSA_ASSERT( psa_cipher_abort( &operation ) );
2737
2738 /* Set an IV after it's already generated. */
2739 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2740 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2741 buffer, sizeof( buffer ),
2742 &length ) );
2743 TEST_EQUAL( psa_cipher_set_iv( &operation,
2744 iv, sizeof( iv ) ),
2745 PSA_ERROR_BAD_STATE );
2746 PSA_ASSERT( psa_cipher_abort( &operation ) );
2747
2748 /* Call update without calling setup beforehand. */
2749 TEST_EQUAL( psa_cipher_update( &operation,
2750 text, sizeof( text ),
2751 buffer, sizeof( buffer ),
2752 &length ),
2753 PSA_ERROR_BAD_STATE );
2754 PSA_ASSERT( psa_cipher_abort( &operation ) );
2755
2756 /* Call update without an IV where an IV is required. */
2757 TEST_EQUAL( psa_cipher_update( &operation,
2758 text, sizeof( text ),
2759 buffer, sizeof( buffer ),
2760 &length ),
2761 PSA_ERROR_BAD_STATE );
2762 PSA_ASSERT( psa_cipher_abort( &operation ) );
2763
2764 /* Call update after finish. */
2765 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2766 PSA_ASSERT( psa_cipher_set_iv( &operation,
2767 iv, sizeof( iv ) ) );
2768 PSA_ASSERT( psa_cipher_finish( &operation,
2769 buffer, sizeof( buffer ), &length ) );
2770 TEST_EQUAL( psa_cipher_update( &operation,
2771 text, sizeof( text ),
2772 buffer, sizeof( buffer ),
2773 &length ),
2774 PSA_ERROR_BAD_STATE );
2775 PSA_ASSERT( psa_cipher_abort( &operation ) );
2776
2777 /* Call finish without calling setup beforehand. */
2778 TEST_EQUAL( psa_cipher_finish( &operation,
2779 buffer, sizeof( buffer ), &length ),
2780 PSA_ERROR_BAD_STATE );
2781 PSA_ASSERT( psa_cipher_abort( &operation ) );
2782
2783 /* Call finish without an IV where an IV is required. */
2784 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2785 /* Not calling update means we are encrypting an empty buffer, which is OK
2786 * for cipher modes with padding. */
2787 TEST_EQUAL( psa_cipher_finish( &operation,
2788 buffer, sizeof( buffer ), &length ),
2789 PSA_ERROR_BAD_STATE );
2790 PSA_ASSERT( psa_cipher_abort( &operation ) );
2791
2792 /* Call finish twice in a row. */
2793 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2794 PSA_ASSERT( psa_cipher_set_iv( &operation,
2795 iv, sizeof( iv ) ) );
2796 PSA_ASSERT( psa_cipher_finish( &operation,
2797 buffer, sizeof( buffer ), &length ) );
2798 TEST_EQUAL( psa_cipher_finish( &operation,
2799 buffer, sizeof( buffer ), &length ),
2800 PSA_ERROR_BAD_STATE );
2801 PSA_ASSERT( psa_cipher_abort( &operation ) );
2802
2803exit:
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002804 mbedtls_psa_crypto_free( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805}
2806/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807
Gilles Peskine50e586b2018-06-08 14:28:46 +02002808/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02002810 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002811 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002812 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002813{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002814 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815 psa_status_t status;
2816 psa_key_type_t key_type = key_type_arg;
2817 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002818 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002819 unsigned char *output = NULL;
2820 size_t output_buffer_size = 0;
2821 size_t function_output_length = 0;
2822 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002823 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002825
Gilles Peskine8817f612018-12-18 00:18:46 +01002826 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002827
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002828 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2829 psa_set_key_algorithm( &attributes, alg );
2830 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002831
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002832 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002833 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834
Gilles Peskine8817f612018-12-18 00:18:46 +01002835 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2836 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837
Gilles Peskine423005e2019-05-06 15:22:57 +02002838 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002839 output_buffer_size = ( (size_t) input->len +
2840 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002841 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002842
Gilles Peskine8817f612018-12-18 00:18:46 +01002843 PSA_ASSERT( psa_cipher_update( &operation,
2844 input->x, input->len,
2845 output, output_buffer_size,
2846 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002847 total_output_length += function_output_length;
2848 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002849 output + total_output_length,
2850 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002851 &function_output_length );
2852 total_output_length += function_output_length;
2853
Gilles Peskinefe11b722018-12-18 00:24:04 +01002854 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002855 if( expected_status == PSA_SUCCESS )
2856 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002857 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002858 ASSERT_COMPARE( expected_output->x, expected_output->len,
2859 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002860 }
2861
2862exit:
2863 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002864 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002865 mbedtls_psa_crypto_free( );
2866}
2867/* END_CASE */
2868
2869/* BEGIN_CASE */
2870void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02002871 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002872 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002873 int first_part_size_arg,
2874 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875 data_t *expected_output )
2876{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002877 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002878 psa_key_type_t key_type = key_type_arg;
2879 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002880 size_t first_part_size = first_part_size_arg;
2881 size_t output1_length = output1_length_arg;
2882 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002883 unsigned char *output = NULL;
2884 size_t output_buffer_size = 0;
2885 size_t function_output_length = 0;
2886 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002887 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002888 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889
Gilles Peskine8817f612018-12-18 00:18:46 +01002890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002891
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002892 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2893 psa_set_key_algorithm( &attributes, alg );
2894 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002895
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002896 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002897 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898
Gilles Peskine8817f612018-12-18 00:18:46 +01002899 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2900 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901
Gilles Peskine423005e2019-05-06 15:22:57 +02002902 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002903 output_buffer_size = ( (size_t) input->len +
2904 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002905 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002906
Gilles Peskinee0866522019-02-19 19:44:00 +01002907 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002908 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2909 output, output_buffer_size,
2910 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002911 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002912 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002913 PSA_ASSERT( psa_cipher_update( &operation,
2914 input->x + first_part_size,
2915 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002916 output + total_output_length,
2917 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002918 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002919 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002920 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002921 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002922 output + total_output_length,
2923 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002924 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002925 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002926 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002928 ASSERT_COMPARE( expected_output->x, expected_output->len,
2929 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002930
2931exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002932 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002933 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934 mbedtls_psa_crypto_free( );
2935}
2936/* END_CASE */
2937
2938/* BEGIN_CASE */
2939void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02002940 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002941 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002942 int first_part_size_arg,
2943 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002944 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002945{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002946 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002947
2948 psa_key_type_t key_type = key_type_arg;
2949 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002950 size_t first_part_size = first_part_size_arg;
2951 size_t output1_length = output1_length_arg;
2952 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002953 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002954 size_t output_buffer_size = 0;
2955 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002956 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002957 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002958 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002959
Gilles Peskine8817f612018-12-18 00:18:46 +01002960 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002962 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2963 psa_set_key_algorithm( &attributes, alg );
2964 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002965
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002966 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002967 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002968
Gilles Peskine8817f612018-12-18 00:18:46 +01002969 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2970 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002971
Gilles Peskine423005e2019-05-06 15:22:57 +02002972 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002973
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002974 output_buffer_size = ( (size_t) input->len +
2975 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002976 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002977
Gilles Peskinee0866522019-02-19 19:44:00 +01002978 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002979 PSA_ASSERT( psa_cipher_update( &operation,
2980 input->x, first_part_size,
2981 output, output_buffer_size,
2982 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002983 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002984 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002985 PSA_ASSERT( psa_cipher_update( &operation,
2986 input->x + first_part_size,
2987 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002988 output + total_output_length,
2989 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002991 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002992 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002993 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002994 output + total_output_length,
2995 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002997 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002998 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002999
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003000 ASSERT_COMPARE( expected_output->x, expected_output->len,
3001 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003002
3003exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003004 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003005 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003006 mbedtls_psa_crypto_free( );
3007}
3008/* END_CASE */
3009
Gilles Peskine50e586b2018-06-08 14:28:46 +02003010/* BEGIN_CASE */
3011void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003012 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003013 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003014 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003015{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003016 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003017 psa_status_t status;
3018 psa_key_type_t key_type = key_type_arg;
3019 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003020 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003021 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003022 size_t output_buffer_size = 0;
3023 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003024 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003025 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003027
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003029
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003030 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3031 psa_set_key_algorithm( &attributes, alg );
3032 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003033
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003034 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003035 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003036
Gilles Peskine8817f612018-12-18 00:18:46 +01003037 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3038 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003039
Gilles Peskine423005e2019-05-06 15:22:57 +02003040 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003041
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003042 output_buffer_size = ( (size_t) input->len +
3043 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003044 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003045
Gilles Peskine8817f612018-12-18 00:18:46 +01003046 PSA_ASSERT( psa_cipher_update( &operation,
3047 input->x, input->len,
3048 output, output_buffer_size,
3049 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003050 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003051 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003052 output + total_output_length,
3053 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003054 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003055 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003056 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003057
3058 if( expected_status == PSA_SUCCESS )
3059 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003061 ASSERT_COMPARE( expected_output->x, expected_output->len,
3062 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003063 }
3064
Gilles Peskine50e586b2018-06-08 14:28:46 +02003065exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003066 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003067 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003068 mbedtls_psa_crypto_free( );
3069}
3070/* END_CASE */
3071
Gilles Peskine50e586b2018-06-08 14:28:46 +02003072/* BEGIN_CASE */
3073void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003074 data_t *key,
3075 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003076{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003077 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003078 psa_key_type_t key_type = key_type_arg;
3079 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003080 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003081 size_t iv_size = 16;
3082 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003083 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003084 size_t output1_size = 0;
3085 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003086 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003087 size_t output2_size = 0;
3088 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003089 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003090 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3091 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003093
Gilles Peskine8817f612018-12-18 00:18:46 +01003094 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003096 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3097 psa_set_key_algorithm( &attributes, alg );
3098 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003099
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003100 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003101 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003102
Gilles Peskine8817f612018-12-18 00:18:46 +01003103 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3104 handle, alg ) );
3105 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3106 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003107
Gilles Peskine8817f612018-12-18 00:18:46 +01003108 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3109 iv, iv_size,
3110 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003111 output1_size = ( (size_t) input->len +
3112 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003113 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003114
Gilles Peskine8817f612018-12-18 00:18:46 +01003115 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3116 output1, output1_size,
3117 &output1_length ) );
3118 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003119 output1 + output1_length,
3120 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003121 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003122
Gilles Peskine048b7f02018-06-08 14:20:49 +02003123 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003124
Gilles Peskine8817f612018-12-18 00:18:46 +01003125 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003126
3127 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003128 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003129
Gilles Peskine8817f612018-12-18 00:18:46 +01003130 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3131 iv, iv_length ) );
3132 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3133 output2, output2_size,
3134 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003135 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003136 PSA_ASSERT( psa_cipher_finish( &operation2,
3137 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003138 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003139 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003140
Gilles Peskine048b7f02018-06-08 14:20:49 +02003141 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003142
Gilles Peskine8817f612018-12-18 00:18:46 +01003143 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003144
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003145 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003146
3147exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003148 mbedtls_free( output1 );
3149 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003150 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003151 mbedtls_psa_crypto_free( );
3152}
3153/* END_CASE */
3154
3155/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003156void cipher_verify_output_multipart( int alg_arg,
3157 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003158 data_t *key,
3159 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003160 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003161{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003162 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003163 psa_key_type_t key_type = key_type_arg;
3164 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003165 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003166 unsigned char iv[16] = {0};
3167 size_t iv_size = 16;
3168 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003169 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003170 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003171 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003172 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003173 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003174 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003175 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003176 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3177 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003179
Gilles Peskine8817f612018-12-18 00:18:46 +01003180 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003181
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003182 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3183 psa_set_key_algorithm( &attributes, alg );
3184 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003186 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003187 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003188
Gilles Peskine8817f612018-12-18 00:18:46 +01003189 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3190 handle, alg ) );
3191 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3192 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003193
Gilles Peskine8817f612018-12-18 00:18:46 +01003194 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3195 iv, iv_size,
3196 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003197 output1_buffer_size = ( (size_t) input->len +
3198 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003199 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003200
Gilles Peskinee0866522019-02-19 19:44:00 +01003201 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003202
Gilles Peskine8817f612018-12-18 00:18:46 +01003203 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3204 output1, output1_buffer_size,
3205 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003206 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003207
Gilles Peskine8817f612018-12-18 00:18:46 +01003208 PSA_ASSERT( psa_cipher_update( &operation1,
3209 input->x + first_part_size,
3210 input->len - first_part_size,
3211 output1, output1_buffer_size,
3212 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003213 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_cipher_finish( &operation1,
3216 output1 + output1_length,
3217 output1_buffer_size - output1_length,
3218 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003219 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003220
Gilles Peskine8817f612018-12-18 00:18:46 +01003221 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003222
Gilles Peskine048b7f02018-06-08 14:20:49 +02003223 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003224 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003225
Gilles Peskine8817f612018-12-18 00:18:46 +01003226 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3227 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003228
Gilles Peskine8817f612018-12-18 00:18:46 +01003229 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3230 output2, output2_buffer_size,
3231 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003232 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003233
Gilles Peskine8817f612018-12-18 00:18:46 +01003234 PSA_ASSERT( psa_cipher_update( &operation2,
3235 output1 + first_part_size,
3236 output1_length - first_part_size,
3237 output2, output2_buffer_size,
3238 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003239 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003240
Gilles Peskine8817f612018-12-18 00:18:46 +01003241 PSA_ASSERT( psa_cipher_finish( &operation2,
3242 output2 + output2_length,
3243 output2_buffer_size - output2_length,
3244 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003245 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003246
Gilles Peskine8817f612018-12-18 00:18:46 +01003247 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003248
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003249 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003250
3251exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003252 mbedtls_free( output1 );
3253 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003254 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003255 mbedtls_psa_crypto_free( );
3256}
3257/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003258
Gilles Peskine20035e32018-02-03 22:44:14 +01003259/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003260void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003261 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003262 data_t *nonce,
3263 data_t *additional_data,
3264 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003265 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003266{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003267 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003268 psa_key_type_t key_type = key_type_arg;
3269 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003270 unsigned char *output_data = NULL;
3271 size_t output_size = 0;
3272 size_t output_length = 0;
3273 unsigned char *output_data2 = NULL;
3274 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003275 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003276 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003278
Gilles Peskine4abf7412018-06-18 16:35:34 +02003279 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003280 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003281
Gilles Peskine8817f612018-12-18 00:18:46 +01003282 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003283
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003284 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3285 psa_set_key_algorithm( &attributes, alg );
3286 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003287
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003288 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003289 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003290
Gilles Peskinefe11b722018-12-18 00:24:04 +01003291 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3292 nonce->x, nonce->len,
3293 additional_data->x,
3294 additional_data->len,
3295 input_data->x, input_data->len,
3296 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003297 &output_length ),
3298 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003299
3300 if( PSA_SUCCESS == expected_result )
3301 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003302 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003303
Gilles Peskinefe11b722018-12-18 00:24:04 +01003304 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3305 nonce->x, nonce->len,
3306 additional_data->x,
3307 additional_data->len,
3308 output_data, output_length,
3309 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003310 &output_length2 ),
3311 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003312
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003313 ASSERT_COMPARE( input_data->x, input_data->len,
3314 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003315 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003316
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003318 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003319 mbedtls_free( output_data );
3320 mbedtls_free( output_data2 );
3321 mbedtls_psa_crypto_free( );
3322}
3323/* END_CASE */
3324
3325/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003326void aead_encrypt( int key_type_arg, data_t *key_data,
3327 int alg_arg,
3328 data_t *nonce,
3329 data_t *additional_data,
3330 data_t *input_data,
3331 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003332{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003333 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334 psa_key_type_t key_type = key_type_arg;
3335 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336 unsigned char *output_data = NULL;
3337 size_t output_size = 0;
3338 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341
Gilles Peskine4abf7412018-06-18 16:35:34 +02003342 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003343 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003344
Gilles Peskine8817f612018-12-18 00:18:46 +01003345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003346
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3348 psa_set_key_algorithm( &attributes, alg );
3349 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003350
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003351 PSA_ASSERT( psa_import_key( &attributes, &handle,
3352 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003353
Gilles Peskine8817f612018-12-18 00:18:46 +01003354 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3355 nonce->x, nonce->len,
3356 additional_data->x, additional_data->len,
3357 input_data->x, input_data->len,
3358 output_data, output_size,
3359 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003360
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003361 ASSERT_COMPARE( expected_result->x, expected_result->len,
3362 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003363
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003365 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003366 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003367 mbedtls_psa_crypto_free( );
3368}
3369/* END_CASE */
3370
3371/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003372void aead_decrypt( int key_type_arg, data_t *key_data,
3373 int alg_arg,
3374 data_t *nonce,
3375 data_t *additional_data,
3376 data_t *input_data,
3377 data_t *expected_data,
3378 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003379{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003380 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003381 psa_key_type_t key_type = key_type_arg;
3382 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383 unsigned char *output_data = NULL;
3384 size_t output_size = 0;
3385 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003386 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003387 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003388 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003389
Gilles Peskine4abf7412018-06-18 16:35:34 +02003390 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003391 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003392
Gilles Peskine8817f612018-12-18 00:18:46 +01003393 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003394
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003395 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3396 psa_set_key_algorithm( &attributes, alg );
3397 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003399 PSA_ASSERT( psa_import_key( &attributes, &handle,
3400 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003401
Gilles Peskinefe11b722018-12-18 00:24:04 +01003402 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3403 nonce->x, nonce->len,
3404 additional_data->x,
3405 additional_data->len,
3406 input_data->x, input_data->len,
3407 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003408 &output_length ),
3409 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003410
Gilles Peskine2d277862018-06-18 15:41:12 +02003411 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003412 ASSERT_COMPARE( expected_data->x, expected_data->len,
3413 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414
Gilles Peskinea1cac842018-06-11 19:33:02 +02003415exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003416 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418 mbedtls_psa_crypto_free( );
3419}
3420/* END_CASE */
3421
3422/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003423void signature_size( int type_arg,
3424 int bits,
3425 int alg_arg,
3426 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003427{
3428 psa_key_type_t type = type_arg;
3429 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003430 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003431 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003432exit:
3433 ;
3434}
3435/* END_CASE */
3436
3437/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003438void sign_deterministic( int key_type_arg, data_t *key_data,
3439 int alg_arg, data_t *input_data,
3440 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003441{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003442 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003443 psa_key_type_t key_type = key_type_arg;
3444 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003445 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003446 unsigned char *signature = NULL;
3447 size_t signature_size;
3448 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003450
Gilles Peskine8817f612018-12-18 00:18:46 +01003451 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003452
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003453 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3454 psa_set_key_algorithm( &attributes, alg );
3455 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003456
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003457 PSA_ASSERT( psa_import_key( &attributes, &handle,
3458 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003459 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3460 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003461
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003462 /* Allocate a buffer which has the size advertized by the
3463 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003464 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3465 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003466 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003467 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003468 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003469
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003470 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003471 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3472 input_data->x, input_data->len,
3473 signature, signature_size,
3474 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003475 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003476 ASSERT_COMPARE( output_data->x, output_data->len,
3477 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003478
3479exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003480 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003481 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003482 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003483 mbedtls_psa_crypto_free( );
3484}
3485/* END_CASE */
3486
3487/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003488void sign_fail( int key_type_arg, data_t *key_data,
3489 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003490 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003491{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003492 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003493 psa_key_type_t key_type = key_type_arg;
3494 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003495 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003496 psa_status_t actual_status;
3497 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003498 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003499 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003501
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003502 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003503
Gilles Peskine8817f612018-12-18 00:18:46 +01003504 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003505
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003506 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3507 psa_set_key_algorithm( &attributes, alg );
3508 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003509
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003510 PSA_ASSERT( psa_import_key( &attributes, &handle,
3511 key_data->x, key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003512
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003513 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003514 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003515 signature, signature_size,
3516 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003517 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003518 /* The value of *signature_length is unspecified on error, but
3519 * whatever it is, it should be less than signature_size, so that
3520 * if the caller tries to read *signature_length bytes without
3521 * checking the error code then they don't overflow a buffer. */
3522 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003523
3524exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003525 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003526 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003527 mbedtls_free( signature );
3528 mbedtls_psa_crypto_free( );
3529}
3530/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003531
3532/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003533void sign_verify( int key_type_arg, data_t *key_data,
3534 int alg_arg, data_t *input_data )
3535{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003536 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003537 psa_key_type_t key_type = key_type_arg;
3538 psa_algorithm_t alg = alg_arg;
3539 size_t key_bits;
3540 unsigned char *signature = NULL;
3541 size_t signature_size;
3542 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003543 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003544
Gilles Peskine8817f612018-12-18 00:18:46 +01003545 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003546
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003547 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3548 psa_set_key_algorithm( &attributes, alg );
3549 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003550
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003551 PSA_ASSERT( psa_import_key( &attributes, &handle,
3552 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003553 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3554 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003555
3556 /* Allocate a buffer which has the size advertized by the
3557 * library. */
3558 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3559 key_bits, alg );
3560 TEST_ASSERT( signature_size != 0 );
3561 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003562 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003563
3564 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003565 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3566 input_data->x, input_data->len,
3567 signature, signature_size,
3568 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003569 /* Check that the signature length looks sensible. */
3570 TEST_ASSERT( signature_length <= signature_size );
3571 TEST_ASSERT( signature_length > 0 );
3572
3573 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003574 PSA_ASSERT( psa_asymmetric_verify(
3575 handle, alg,
3576 input_data->x, input_data->len,
3577 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003578
3579 if( input_data->len != 0 )
3580 {
3581 /* Flip a bit in the input and verify that the signature is now
3582 * detected as invalid. Flip a bit at the beginning, not at the end,
3583 * because ECDSA may ignore the last few bits of the input. */
3584 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003585 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3586 input_data->x, input_data->len,
3587 signature, signature_length ),
3588 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003589 }
3590
3591exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003592 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003593 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003594 mbedtls_free( signature );
3595 mbedtls_psa_crypto_free( );
3596}
3597/* END_CASE */
3598
3599/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003600void asymmetric_verify( int key_type_arg, data_t *key_data,
3601 int alg_arg, data_t *hash_data,
3602 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003603{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003604 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003605 psa_key_type_t key_type = key_type_arg;
3606 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003608
Gilles Peskine69c12672018-06-28 00:07:19 +02003609 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3610
Gilles Peskine8817f612018-12-18 00:18:46 +01003611 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003612
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003613 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3614 psa_set_key_algorithm( &attributes, alg );
3615 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003616
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003617 PSA_ASSERT( psa_import_key( &attributes, &handle,
3618 key_data->x, key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003619
Gilles Peskine8817f612018-12-18 00:18:46 +01003620 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3621 hash_data->x, hash_data->len,
3622 signature_data->x,
3623 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003624exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003625 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003626 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003627 mbedtls_psa_crypto_free( );
3628}
3629/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003630
3631/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003632void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3633 int alg_arg, data_t *hash_data,
3634 data_t *signature_data,
3635 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003636{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003637 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003638 psa_key_type_t key_type = key_type_arg;
3639 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003640 psa_status_t actual_status;
3641 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643
Gilles Peskine8817f612018-12-18 00:18:46 +01003644 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003645
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003646 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3647 psa_set_key_algorithm( &attributes, alg );
3648 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003649
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003650 PSA_ASSERT( psa_import_key( &attributes, &handle,
3651 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003652
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003653 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003654 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003655 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003656 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003657
Gilles Peskinefe11b722018-12-18 00:24:04 +01003658 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659
3660exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003661 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003662 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003663 mbedtls_psa_crypto_free( );
3664}
3665/* END_CASE */
3666
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003667/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003668void asymmetric_encrypt( int key_type_arg,
3669 data_t *key_data,
3670 int alg_arg,
3671 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003672 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003673 int expected_output_length_arg,
3674 int expected_status_arg )
3675{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003676 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003677 psa_key_type_t key_type = key_type_arg;
3678 psa_algorithm_t alg = alg_arg;
3679 size_t expected_output_length = expected_output_length_arg;
3680 size_t key_bits;
3681 unsigned char *output = NULL;
3682 size_t output_size;
3683 size_t output_length = ~0;
3684 psa_status_t actual_status;
3685 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003686 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003687
Gilles Peskine8817f612018-12-18 00:18:46 +01003688 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003689
Gilles Peskine656896e2018-06-29 19:12:28 +02003690 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003691 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3692 psa_set_key_algorithm( &attributes, alg );
3693 psa_set_key_type( &attributes, key_type );
3694 PSA_ASSERT( psa_import_key( &attributes, &handle,
3695 key_data->x, key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003696
3697 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003698 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3699 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003700 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003701 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003702
3703 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003704 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003705 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003706 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003707 output, output_size,
3708 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003709 TEST_EQUAL( actual_status, expected_status );
3710 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003711
Gilles Peskine68428122018-06-30 18:42:41 +02003712 /* If the label is empty, the test framework puts a non-null pointer
3713 * in label->x. Test that a null pointer works as well. */
3714 if( label->len == 0 )
3715 {
3716 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003717 if( output_size != 0 )
3718 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003719 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003720 input_data->x, input_data->len,
3721 NULL, label->len,
3722 output, output_size,
3723 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003724 TEST_EQUAL( actual_status, expected_status );
3725 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003726 }
3727
Gilles Peskine656896e2018-06-29 19:12:28 +02003728exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003729 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003730 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003731 mbedtls_free( output );
3732 mbedtls_psa_crypto_free( );
3733}
3734/* END_CASE */
3735
3736/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003737void asymmetric_encrypt_decrypt( int key_type_arg,
3738 data_t *key_data,
3739 int alg_arg,
3740 data_t *input_data,
3741 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003742{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003743 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003744 psa_key_type_t key_type = key_type_arg;
3745 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003746 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003747 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003748 size_t output_size;
3749 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003750 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003751 size_t output2_size;
3752 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003754
Gilles Peskine8817f612018-12-18 00:18:46 +01003755 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003756
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003757 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3758 psa_set_key_algorithm( &attributes, alg );
3759 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003760
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003761 PSA_ASSERT( psa_import_key( &attributes, &handle,
3762 key_data->x, key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003763
3764 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003765 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3766 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003767 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003768 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003769 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003770 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003771
Gilles Peskineeebd7382018-06-08 18:11:54 +02003772 /* We test encryption by checking that encrypt-then-decrypt gives back
3773 * the original plaintext because of the non-optional random
3774 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003775 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3776 input_data->x, input_data->len,
3777 label->x, label->len,
3778 output, output_size,
3779 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003780 /* We don't know what ciphertext length to expect, but check that
3781 * it looks sensible. */
3782 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003783
Gilles Peskine8817f612018-12-18 00:18:46 +01003784 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3785 output, output_length,
3786 label->x, label->len,
3787 output2, output2_size,
3788 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003789 ASSERT_COMPARE( input_data->x, input_data->len,
3790 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003791
3792exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003793 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003794 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003795 mbedtls_free( output );
3796 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003797 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003798}
3799/* END_CASE */
3800
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003801/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003802void asymmetric_decrypt( int key_type_arg,
3803 data_t *key_data,
3804 int alg_arg,
3805 data_t *input_data,
3806 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003807 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003808{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003809 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003810 psa_key_type_t key_type = key_type_arg;
3811 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003812 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003813 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003814 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003815 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003816
Jaeden Amero412654a2019-02-06 12:57:46 +00003817 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003818 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003819
Gilles Peskine8817f612018-12-18 00:18:46 +01003820 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003821
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003822 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3823 psa_set_key_algorithm( &attributes, alg );
3824 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003826 PSA_ASSERT( psa_import_key( &attributes, &handle,
3827 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003828
Gilles Peskine8817f612018-12-18 00:18:46 +01003829 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3830 input_data->x, input_data->len,
3831 label->x, label->len,
3832 output,
3833 output_size,
3834 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003835 ASSERT_COMPARE( expected_data->x, expected_data->len,
3836 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003837
Gilles Peskine68428122018-06-30 18:42:41 +02003838 /* If the label is empty, the test framework puts a non-null pointer
3839 * in label->x. Test that a null pointer works as well. */
3840 if( label->len == 0 )
3841 {
3842 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003843 if( output_size != 0 )
3844 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003845 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3846 input_data->x, input_data->len,
3847 NULL, label->len,
3848 output,
3849 output_size,
3850 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003851 ASSERT_COMPARE( expected_data->x, expected_data->len,
3852 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003853 }
3854
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003855exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003856 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003857 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003858 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003859 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003860}
3861/* END_CASE */
3862
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003863/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003864void asymmetric_decrypt_fail( int key_type_arg,
3865 data_t *key_data,
3866 int alg_arg,
3867 data_t *input_data,
3868 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003869 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003870 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003872 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003873 psa_key_type_t key_type = key_type_arg;
3874 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003876 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003877 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003878 psa_status_t actual_status;
3879 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003882 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003883
Gilles Peskine8817f612018-12-18 00:18:46 +01003884 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003886 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3887 psa_set_key_algorithm( &attributes, alg );
3888 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003889
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003890 PSA_ASSERT( psa_import_key( &attributes, &handle,
3891 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003892
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003893 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003894 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003895 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003896 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003897 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003898 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003899 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003900
Gilles Peskine68428122018-06-30 18:42:41 +02003901 /* If the label is empty, the test framework puts a non-null pointer
3902 * in label->x. Test that a null pointer works as well. */
3903 if( label->len == 0 )
3904 {
3905 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003906 if( output_size != 0 )
3907 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003908 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003909 input_data->x, input_data->len,
3910 NULL, label->len,
3911 output, output_size,
3912 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003913 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003914 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003915 }
3916
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003917exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003918 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003919 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003920 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003921 mbedtls_psa_crypto_free( );
3922}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003923/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003924
3925/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003926void crypto_generator_init( )
3927{
3928 /* Test each valid way of initializing the object, except for `= {0}`, as
3929 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3930 * though it's OK by the C standard. We could test for this, but we'd need
3931 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003932 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003933 psa_crypto_generator_t func = psa_crypto_generator_init( );
3934 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3935 psa_crypto_generator_t zero;
3936
3937 memset( &zero, 0, sizeof( zero ) );
3938
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003939 /* A default generator should not be able to report its capacity. */
3940 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
3941 PSA_ERROR_BAD_STATE );
3942 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
3943 PSA_ERROR_BAD_STATE );
3944 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
3945 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003946
3947 /* A default generator should be abortable without error. */
3948 PSA_ASSERT( psa_generator_abort(&func) );
3949 PSA_ASSERT( psa_generator_abort(&init) );
3950 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003951}
3952/* END_CASE */
3953
3954/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003955void derive_setup( int key_type_arg,
3956 data_t *key_data,
3957 int alg_arg,
3958 data_t *salt,
3959 data_t *label,
3960 int requested_capacity_arg,
3961 int expected_status_arg )
3962{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003963 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003964 size_t key_type = key_type_arg;
3965 psa_algorithm_t alg = alg_arg;
3966 size_t requested_capacity = requested_capacity_arg;
3967 psa_status_t expected_status = expected_status_arg;
3968 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003969 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003970
Gilles Peskine8817f612018-12-18 00:18:46 +01003971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003972
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003973 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3974 psa_set_key_algorithm( &attributes, alg );
3975 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003976
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003977 PSA_ASSERT( psa_import_key( &attributes, &handle,
3978 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003979
Gilles Peskinefe11b722018-12-18 00:24:04 +01003980 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3981 salt->x, salt->len,
3982 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003983 requested_capacity ),
3984 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003985
3986exit:
3987 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003988 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003989 mbedtls_psa_crypto_free( );
3990}
3991/* END_CASE */
3992
3993/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003994void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003995{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003996 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003997 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003998 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003999 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004000 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004001 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004002 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4003 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4004 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004005 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004006
Gilles Peskine8817f612018-12-18 00:18:46 +01004007 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004008
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004009 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4010 psa_set_key_algorithm( &attributes, alg );
4011 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004012
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004013 PSA_ASSERT( psa_import_key( &attributes, &handle,
4014 key_data, sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004015
4016 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004017 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4018 NULL, 0,
4019 NULL, 0,
4020 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004021
4022 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004023 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4024 NULL, 0,
4025 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004026 capacity ),
4027 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004028
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004029 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004030
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004031 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004032 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004033
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004034exit:
4035 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004036 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004037 mbedtls_psa_crypto_free( );
4038}
4039/* END_CASE */
4040
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004041/* BEGIN_CASE */
4042void test_derive_invalid_generator_tests( )
4043{
4044 uint8_t output_buffer[16];
4045 size_t buffer_size = 16;
4046 size_t capacity = 0;
4047 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4048
Nir Sonnenschein50789302018-10-31 12:16:38 +02004049 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004050 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004051
4052 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004053 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004054
Gilles Peskine8817f612018-12-18 00:18:46 +01004055 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004056
Nir Sonnenschein50789302018-10-31 12:16:38 +02004057 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004058 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004059
Nir Sonnenschein50789302018-10-31 12:16:38 +02004060 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004061 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004062
4063exit:
4064 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004065}
4066/* END_CASE */
4067
4068/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004069void derive_output( int alg_arg,
4070 data_t *key_data,
4071 data_t *salt,
4072 data_t *label,
4073 int requested_capacity_arg,
4074 data_t *expected_output1,
4075 data_t *expected_output2 )
4076{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004077 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004078 psa_algorithm_t alg = alg_arg;
4079 size_t requested_capacity = requested_capacity_arg;
4080 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4081 uint8_t *expected_outputs[2] =
4082 {expected_output1->x, expected_output2->x};
4083 size_t output_sizes[2] =
4084 {expected_output1->len, expected_output2->len};
4085 size_t output_buffer_size = 0;
4086 uint8_t *output_buffer = NULL;
4087 size_t expected_capacity;
4088 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004089 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004090 psa_status_t status;
4091 unsigned i;
4092
4093 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4094 {
4095 if( output_sizes[i] > output_buffer_size )
4096 output_buffer_size = output_sizes[i];
4097 if( output_sizes[i] == 0 )
4098 expected_outputs[i] = NULL;
4099 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004100 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004101 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004102
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004103 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4104 psa_set_key_algorithm( &attributes, alg );
4105 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004106
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004107 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004108 key_data->x, key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004109
4110 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004111 if( PSA_ALG_IS_HKDF( alg ) )
4112 {
4113 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4114 PSA_ASSERT( psa_set_generator_capacity( &generator,
4115 requested_capacity ) );
4116 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4117 PSA_KDF_STEP_SALT,
4118 salt->x, salt->len ) );
4119 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4120 PSA_KDF_STEP_SECRET,
4121 handle ) );
4122 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4123 PSA_KDF_STEP_INFO,
4124 label->x, label->len ) );
4125 }
4126 else
4127 {
4128 // legacy
4129 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4130 salt->x, salt->len,
4131 label->x, label->len,
4132 requested_capacity ) );
4133 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004134 PSA_ASSERT( psa_get_generator_capacity( &generator,
4135 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004136 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004137 expected_capacity = requested_capacity;
4138
4139 /* Expansion phase. */
4140 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4141 {
4142 /* Read some bytes. */
4143 status = psa_generator_read( &generator,
4144 output_buffer, output_sizes[i] );
4145 if( expected_capacity == 0 && output_sizes[i] == 0 )
4146 {
4147 /* Reading 0 bytes when 0 bytes are available can go either way. */
4148 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004149 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004150 continue;
4151 }
4152 else if( expected_capacity == 0 ||
4153 output_sizes[i] > expected_capacity )
4154 {
4155 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004156 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004157 expected_capacity = 0;
4158 continue;
4159 }
4160 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004161 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004162 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004163 ASSERT_COMPARE( output_buffer, output_sizes[i],
4164 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004165 /* Check the generator status. */
4166 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004167 PSA_ASSERT( psa_get_generator_capacity( &generator,
4168 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004169 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004170 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004171 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004172
4173exit:
4174 mbedtls_free( output_buffer );
4175 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004176 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004177 mbedtls_psa_crypto_free( );
4178}
4179/* END_CASE */
4180
4181/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004182void derive_full( int alg_arg,
4183 data_t *key_data,
4184 data_t *salt,
4185 data_t *label,
4186 int requested_capacity_arg )
4187{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004188 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004189 psa_algorithm_t alg = alg_arg;
4190 size_t requested_capacity = requested_capacity_arg;
4191 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4192 unsigned char output_buffer[16];
4193 size_t expected_capacity = requested_capacity;
4194 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004195 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004196
Gilles Peskine8817f612018-12-18 00:18:46 +01004197 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004198
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004199 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4200 psa_set_key_algorithm( &attributes, alg );
4201 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004202
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004203 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004204 key_data->x, key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004205
4206 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004207 if( PSA_ALG_IS_HKDF( alg ) )
4208 {
4209 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4210 PSA_ASSERT( psa_set_generator_capacity( &generator,
4211 requested_capacity ) );
4212 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4213 PSA_KDF_STEP_SALT,
4214 salt->x, salt->len ) );
4215 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4216 PSA_KDF_STEP_SECRET,
4217 handle ) );
4218 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4219 PSA_KDF_STEP_INFO,
4220 label->x, label->len ) );
4221 }
4222 else
4223 {
4224 // legacy
4225 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4226 salt->x, salt->len,
4227 label->x, label->len,
4228 requested_capacity ) );
4229 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004230 PSA_ASSERT( psa_get_generator_capacity( &generator,
4231 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004232 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004233
4234 /* Expansion phase. */
4235 while( current_capacity > 0 )
4236 {
4237 size_t read_size = sizeof( output_buffer );
4238 if( read_size > current_capacity )
4239 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004240 PSA_ASSERT( psa_generator_read( &generator,
4241 output_buffer,
4242 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004243 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004244 PSA_ASSERT( psa_get_generator_capacity( &generator,
4245 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004246 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004247 }
4248
4249 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004250 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004251 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004252
Gilles Peskine8817f612018-12-18 00:18:46 +01004253 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004254
4255exit:
4256 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004257 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004258 mbedtls_psa_crypto_free( );
4259}
4260/* END_CASE */
4261
4262/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004263void derive_key_exercise( int alg_arg,
4264 data_t *key_data,
4265 data_t *salt,
4266 data_t *label,
4267 int derived_type_arg,
4268 int derived_bits_arg,
4269 int derived_usage_arg,
4270 int derived_alg_arg )
4271{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004272 psa_key_handle_t base_handle = 0;
4273 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004274 psa_algorithm_t alg = alg_arg;
4275 psa_key_type_t derived_type = derived_type_arg;
4276 size_t derived_bits = derived_bits_arg;
4277 psa_key_usage_t derived_usage = derived_usage_arg;
4278 psa_algorithm_t derived_alg = derived_alg_arg;
4279 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4280 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004282 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004283
Gilles Peskine8817f612018-12-18 00:18:46 +01004284 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004285
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004286 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4287 psa_set_key_algorithm( &attributes, alg );
4288 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
4289 PSA_ASSERT( psa_import_key( &attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004290 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004291
4292 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004293 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4294 salt->x, salt->len,
4295 label->x, label->len,
4296 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004297 psa_set_key_usage_flags( &attributes, derived_usage );
4298 psa_set_key_algorithm( &attributes, derived_alg );
4299 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004300 psa_set_key_bits( &attributes, derived_bits );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004301 PSA_ASSERT( psa_generate_derived_key( &attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004302 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004303
4304 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004305 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4306 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4307 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004308
4309 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004310 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004311 goto exit;
4312
4313exit:
4314 psa_generator_abort( &generator );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004315 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004316 psa_destroy_key( base_handle );
4317 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004318 mbedtls_psa_crypto_free( );
4319}
4320/* END_CASE */
4321
4322/* BEGIN_CASE */
4323void derive_key_export( int alg_arg,
4324 data_t *key_data,
4325 data_t *salt,
4326 data_t *label,
4327 int bytes1_arg,
4328 int bytes2_arg )
4329{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004330 psa_key_handle_t base_handle = 0;
4331 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004332 psa_algorithm_t alg = alg_arg;
4333 size_t bytes1 = bytes1_arg;
4334 size_t bytes2 = bytes2_arg;
4335 size_t capacity = bytes1 + bytes2;
4336 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004337 uint8_t *output_buffer = NULL;
4338 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004339 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4340 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004341 size_t length;
4342
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004343 ASSERT_ALLOC( output_buffer, capacity );
4344 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004346
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004347 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4348 psa_set_key_algorithm( &base_attributes, alg );
4349 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4350 PSA_ASSERT( psa_import_key( &base_attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004351 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004352
4353 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004354 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4355 salt->x, salt->len,
4356 label->x, label->len,
4357 capacity ) );
4358 PSA_ASSERT( psa_generator_read( &generator,
4359 output_buffer,
4360 capacity ) );
4361 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004362
4363 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004364 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4365 salt->x, salt->len,
4366 label->x, label->len,
4367 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004368 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4369 psa_set_key_algorithm( &derived_attributes, 0 );
4370 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004371 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004372 PSA_ASSERT( psa_generate_derived_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004373 &generator ) );
4374 PSA_ASSERT( psa_export_key( derived_handle,
4375 export_buffer, bytes1,
4376 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004377 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004378 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004379 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004380 PSA_ASSERT( psa_generate_derived_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004381 &generator ) );
4382 PSA_ASSERT( psa_export_key( derived_handle,
4383 export_buffer + bytes1, bytes2,
4384 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004385 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004386
4387 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004388 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4389 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004390
4391exit:
4392 mbedtls_free( output_buffer );
4393 mbedtls_free( export_buffer );
4394 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004395 psa_destroy_key( base_handle );
4396 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004397 mbedtls_psa_crypto_free( );
4398}
4399/* END_CASE */
4400
4401/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004402void key_agreement_setup( int alg_arg,
4403 int our_key_type_arg, data_t *our_key_data,
4404 data_t *peer_key_data,
4405 int expected_status_arg )
4406{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004407 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004408 psa_algorithm_t alg = alg_arg;
4409 psa_key_type_t our_key_type = our_key_type_arg;
4410 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004412 psa_status_t expected_status = expected_status_arg;
4413 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004414
Gilles Peskine8817f612018-12-18 00:18:46 +01004415 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004416
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004417 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4418 psa_set_key_algorithm( &attributes, alg );
4419 psa_set_key_type( &attributes, our_key_type );
4420 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004421 our_key_data->x, our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004422
Gilles Peskine77f40d82019-04-11 21:27:06 +02004423 /* The tests currently include inputs that should fail at either step.
4424 * Test cases that fail at the setup step should be changed to call
4425 * key_derivation_setup instead, and this function should be renamed
4426 * to key_agreement_fail. */
4427 status = psa_key_derivation_setup( &generator, alg );
4428 if( status == PSA_SUCCESS )
4429 {
4430 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
4431 our_key,
4432 peer_key_data->x, peer_key_data->len ),
4433 expected_status );
4434 }
4435 else
4436 {
4437 TEST_ASSERT( status == expected_status );
4438 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004439
4440exit:
4441 psa_generator_abort( &generator );
4442 psa_destroy_key( our_key );
4443 mbedtls_psa_crypto_free( );
4444}
4445/* END_CASE */
4446
4447/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004448void raw_key_agreement( int alg_arg,
4449 int our_key_type_arg, data_t *our_key_data,
4450 data_t *peer_key_data,
4451 data_t *expected_output )
4452{
4453 psa_key_handle_t our_key = 0;
4454 psa_algorithm_t alg = alg_arg;
4455 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004456 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004457 unsigned char *output = NULL;
4458 size_t output_length = ~0;
4459
4460 ASSERT_ALLOC( output, expected_output->len );
4461 PSA_ASSERT( psa_crypto_init( ) );
4462
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004463 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4464 psa_set_key_algorithm( &attributes, alg );
4465 psa_set_key_type( &attributes, our_key_type );
4466 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004467 our_key_data->x, our_key_data->len ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004468
4469 PSA_ASSERT( psa_key_agreement_raw_shared_secret(
4470 alg, our_key,
4471 peer_key_data->x, peer_key_data->len,
4472 output, expected_output->len, &output_length ) );
4473 ASSERT_COMPARE( output, output_length,
4474 expected_output->x, expected_output->len );
4475
4476exit:
4477 mbedtls_free( output );
4478 psa_destroy_key( our_key );
4479 mbedtls_psa_crypto_free( );
4480}
4481/* END_CASE */
4482
4483/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004484void key_agreement_capacity( int alg_arg,
4485 int our_key_type_arg, data_t *our_key_data,
4486 data_t *peer_key_data,
4487 int expected_capacity_arg )
4488{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004489 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004490 psa_algorithm_t alg = alg_arg;
4491 psa_key_type_t our_key_type = our_key_type_arg;
4492 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004494 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004495 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004496
Gilles Peskine8817f612018-12-18 00:18:46 +01004497 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004498
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004499 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4500 psa_set_key_algorithm( &attributes, alg );
4501 psa_set_key_type( &attributes, our_key_type );
4502 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004503 our_key_data->x, our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004504
Gilles Peskine969c5d62019-01-16 15:53:06 +01004505 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4506 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004507 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004508 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004509 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4510 {
4511 /* The test data is for info="" */
4512 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4513 PSA_KDF_STEP_INFO,
4514 NULL, 0 ) );
4515 }
Gilles Peskine59685592018-09-18 12:11:34 +02004516
Gilles Peskinebf491972018-10-25 22:36:12 +02004517 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004518 PSA_ASSERT( psa_get_generator_capacity(
4519 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004520 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004521
Gilles Peskinebf491972018-10-25 22:36:12 +02004522 /* Test the actual capacity by reading the output. */
4523 while( actual_capacity > sizeof( output ) )
4524 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004525 PSA_ASSERT( psa_generator_read( &generator,
4526 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004527 actual_capacity -= sizeof( output );
4528 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004529 PSA_ASSERT( psa_generator_read( &generator,
4530 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004531 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004532 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004533
Gilles Peskine59685592018-09-18 12:11:34 +02004534exit:
4535 psa_generator_abort( &generator );
4536 psa_destroy_key( our_key );
4537 mbedtls_psa_crypto_free( );
4538}
4539/* END_CASE */
4540
4541/* BEGIN_CASE */
4542void key_agreement_output( int alg_arg,
4543 int our_key_type_arg, data_t *our_key_data,
4544 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004545 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004546{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004547 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004548 psa_algorithm_t alg = alg_arg;
4549 psa_key_type_t our_key_type = our_key_type_arg;
4550 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004552 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004553
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004554 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4555 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004556
Gilles Peskine8817f612018-12-18 00:18:46 +01004557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004558
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004559 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4560 psa_set_key_algorithm( &attributes, alg );
4561 psa_set_key_type( &attributes, our_key_type );
4562 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004563 our_key_data->x, our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004564
Gilles Peskine969c5d62019-01-16 15:53:06 +01004565 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4566 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004567 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004568 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004569 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4570 {
4571 /* The test data is for info="" */
4572 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4573 PSA_KDF_STEP_INFO,
4574 NULL, 0 ) );
4575 }
Gilles Peskine59685592018-09-18 12:11:34 +02004576
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004577 PSA_ASSERT( psa_generator_read( &generator,
4578 actual_output,
4579 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004580 ASSERT_COMPARE( actual_output, expected_output1->len,
4581 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004582 if( expected_output2->len != 0 )
4583 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004584 PSA_ASSERT( psa_generator_read( &generator,
4585 actual_output,
4586 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004587 ASSERT_COMPARE( actual_output, expected_output2->len,
4588 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004589 }
Gilles Peskine59685592018-09-18 12:11:34 +02004590
4591exit:
4592 psa_generator_abort( &generator );
4593 psa_destroy_key( our_key );
4594 mbedtls_psa_crypto_free( );
4595 mbedtls_free( actual_output );
4596}
4597/* END_CASE */
4598
4599/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004600void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004601{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004602 size_t bytes = bytes_arg;
4603 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004604 unsigned char *output = NULL;
4605 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004606 size_t i;
4607 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004608
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004609 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4610 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004611 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004612
Gilles Peskine8817f612018-12-18 00:18:46 +01004613 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004614
Gilles Peskinea50d7392018-06-21 10:22:13 +02004615 /* Run several times, to ensure that every output byte will be
4616 * nonzero at least once with overwhelming probability
4617 * (2^(-8*number_of_runs)). */
4618 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004619 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004620 if( bytes != 0 )
4621 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004622 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004623
4624 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004625 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4626 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004627
4628 for( i = 0; i < bytes; i++ )
4629 {
4630 if( output[i] != 0 )
4631 ++changed[i];
4632 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004633 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004634
4635 /* Check that every byte was changed to nonzero at least once. This
4636 * validates that psa_generate_random is overwriting every byte of
4637 * the output buffer. */
4638 for( i = 0; i < bytes; i++ )
4639 {
4640 TEST_ASSERT( changed[i] != 0 );
4641 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004642
4643exit:
4644 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004645 mbedtls_free( output );
4646 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004647}
4648/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004649
4650/* BEGIN_CASE */
4651void generate_key( int type_arg,
4652 int bits_arg,
4653 int usage_arg,
4654 int alg_arg,
4655 int expected_status_arg )
4656{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004657 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004658 psa_key_type_t type = type_arg;
4659 psa_key_usage_t usage = usage_arg;
4660 size_t bits = bits_arg;
4661 psa_algorithm_t alg = alg_arg;
4662 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004664 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004665
Gilles Peskine8817f612018-12-18 00:18:46 +01004666 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004667
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004668 psa_set_key_usage_flags( &attributes, usage );
4669 psa_set_key_algorithm( &attributes, alg );
4670 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004671 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004672
4673 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004674 TEST_EQUAL( psa_generate_random_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004675 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004676 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004677
4678 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004679 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4680 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4681 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004682
Gilles Peskine818ca122018-06-20 18:16:48 +02004683 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004684 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004685 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004686
4687exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004688 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004689 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004690 mbedtls_psa_crypto_free( );
4691}
4692/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004693
Gilles Peskinee56e8782019-04-26 17:34:02 +02004694/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4695void generate_key_rsa( int bits_arg,
4696 data_t *e_arg,
4697 int expected_status_arg )
4698{
4699 psa_key_handle_t handle = 0;
4700 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEYPAIR;
4701 size_t bits = bits_arg;
4702 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4703 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4704 psa_status_t expected_status = expected_status_arg;
4705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4706 uint8_t *exported = NULL;
4707 size_t exported_size =
4708 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4709 size_t exported_length = SIZE_MAX;
4710 uint8_t *e_read_buffer = NULL;
4711 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004712 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004713 size_t e_read_length = SIZE_MAX;
4714
4715 if( e_arg->len == 0 ||
4716 ( e_arg->len == 3 &&
4717 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4718 {
4719 is_default_public_exponent = 1;
4720 e_read_size = 0;
4721 }
4722 ASSERT_ALLOC( e_read_buffer, e_read_size );
4723 ASSERT_ALLOC( exported, exported_size );
4724
4725 PSA_ASSERT( psa_crypto_init( ) );
4726
4727 psa_set_key_usage_flags( &attributes, usage );
4728 psa_set_key_algorithm( &attributes, alg );
4729 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4730 e_arg->x, e_arg->len ) );
4731 psa_set_key_bits( &attributes, bits );
4732
4733 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004734 TEST_EQUAL( psa_generate_random_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004735 if( expected_status != PSA_SUCCESS )
4736 goto exit;
4737
4738 /* Test the key information */
4739 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4740 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4741 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4742 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4743 e_read_buffer, e_read_size,
4744 &e_read_length ) );
4745 if( is_default_public_exponent )
4746 TEST_EQUAL( e_read_length, 0 );
4747 else
4748 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4749
4750 /* Do something with the key according to its type and permitted usage. */
4751 if( ! exercise_key( handle, usage, alg ) )
4752 goto exit;
4753
4754 /* Export the key and check the public exponent. */
4755 PSA_ASSERT( psa_export_public_key( handle,
4756 exported, exported_size,
4757 &exported_length ) );
4758 {
4759 uint8_t *p = exported;
4760 uint8_t *end = exported + exported_length;
4761 size_t len;
4762 /* RSAPublicKey ::= SEQUENCE {
4763 * modulus INTEGER, -- n
4764 * publicExponent INTEGER } -- e
4765 */
4766 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4767 MBEDTLS_ASN1_SEQUENCE |
4768 MBEDTLS_ASN1_CONSTRUCTED ) );
4769 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
4770 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4771 MBEDTLS_ASN1_INTEGER ) );
4772 if( len >= 1 && p[0] == 0 )
4773 {
4774 ++p;
4775 --len;
4776 }
4777 if( e_arg->len == 0 )
4778 {
4779 TEST_EQUAL( len, 3 );
4780 TEST_EQUAL( p[0], 1 );
4781 TEST_EQUAL( p[1], 0 );
4782 TEST_EQUAL( p[2], 1 );
4783 }
4784 else
4785 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4786 }
4787
4788exit:
4789 psa_reset_key_attributes( &attributes );
4790 psa_destroy_key( handle );
4791 mbedtls_psa_crypto_free( );
4792 mbedtls_free( e_read_buffer );
4793 mbedtls_free( exported );
4794}
4795/* END_CASE */
4796
Darryl Greend49a4992018-06-18 17:27:26 +01004797/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004798void persistent_key_load_key_from_storage( data_t *data,
4799 int type_arg, int bits_arg,
4800 int usage_flags_arg, int alg_arg,
4801 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004802{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004803 psa_key_id_t key_id = 1;
4804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004805 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004806 psa_key_handle_t base_key = 0;
4807 psa_key_type_t type = type_arg;
4808 size_t bits = bits_arg;
4809 psa_key_usage_t usage_flags = usage_flags_arg;
4810 psa_algorithm_t alg = alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004811 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004812 unsigned char *first_export = NULL;
4813 unsigned char *second_export = NULL;
4814 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4815 size_t first_exported_length;
4816 size_t second_exported_length;
4817
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004818 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4819 {
4820 ASSERT_ALLOC( first_export, export_size );
4821 ASSERT_ALLOC( second_export, export_size );
4822 }
Darryl Greend49a4992018-06-18 17:27:26 +01004823
Gilles Peskine8817f612018-12-18 00:18:46 +01004824 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004825
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004826 psa_make_key_persistent( &attributes, key_id, PSA_KEY_LIFETIME_PERSISTENT );
4827 psa_set_key_usage_flags( &attributes, usage_flags );
4828 psa_set_key_algorithm( &attributes, alg );
4829 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004830 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004831
Darryl Green0c6575a2018-11-07 16:05:30 +00004832 switch( generation_method )
4833 {
4834 case IMPORT_KEY:
4835 /* Import the key */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004836 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004837 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004838 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004839
Darryl Green0c6575a2018-11-07 16:05:30 +00004840 case GENERATE_KEY:
4841 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004842 PSA_ASSERT( psa_generate_random_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004843 break;
4844
4845 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004846 {
4847 /* Create base key */
4848 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4849 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4850 psa_set_key_usage_flags( &base_attributes,
4851 PSA_KEY_USAGE_DERIVE );
4852 psa_set_key_algorithm( &base_attributes, derive_alg );
4853 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4854 PSA_ASSERT( psa_import_key( &base_attributes, &base_key,
4855 data->x, data->len ) );
4856 /* Derive a key. */
4857 PSA_ASSERT( psa_key_derivation_setup( &generator, derive_alg ) );
4858 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4859 PSA_KDF_STEP_SECRET,
4860 base_key ) );
4861 PSA_ASSERT( psa_key_derivation_input_bytes(
4862 &generator, PSA_KDF_STEP_INFO,
4863 NULL, 0 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004864 PSA_ASSERT( psa_generate_derived_key( &attributes, &handle,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004865 &generator ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004866 PSA_ASSERT( psa_generator_abort( &generator ) );
4867 PSA_ASSERT( psa_destroy_key( base_key ) );
4868 base_key = 0;
4869 }
Darryl Green0c6575a2018-11-07 16:05:30 +00004870 break;
4871 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004872 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004873
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004874 /* Export the key if permitted by the key policy. */
4875 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4876 {
4877 PSA_ASSERT( psa_export_key( handle,
4878 first_export, export_size,
4879 &first_exported_length ) );
4880 if( generation_method == IMPORT_KEY )
4881 ASSERT_COMPARE( data->x, data->len,
4882 first_export, first_exported_length );
4883 }
Darryl Greend49a4992018-06-18 17:27:26 +01004884
4885 /* Shutdown and restart */
4886 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004887 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004888
Darryl Greend49a4992018-06-18 17:27:26 +01004889 /* Check key slot still contains key data */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004890 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
Gilles Peskine8817f612018-12-18 00:18:46 +01004891 &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004892 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4893 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
4894 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4895 PSA_KEY_LIFETIME_PERSISTENT );
4896 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4897 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4898 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4899 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004900
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004901 /* Export the key again if permitted by the key policy. */
4902 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004903 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004904 PSA_ASSERT( psa_export_key( handle,
4905 second_export, export_size,
4906 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004907 ASSERT_COMPARE( first_export, first_exported_length,
4908 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004909 }
4910
4911 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004912 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004913 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004914
4915exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004916 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004917 mbedtls_free( first_export );
4918 mbedtls_free( second_export );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004919 psa_generator_abort( &generator );
4920 psa_destroy_key( base_key );
4921 if( handle == 0 )
4922 {
4923 /* In case there was a test failure after creating the persistent key
4924 * but while it was not open, try to re-open the persistent key
4925 * to delete it. */
4926 psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle );
4927 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004928 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004929 mbedtls_psa_crypto_free();
4930}
4931/* END_CASE */