blob: da118bba34b2cf194bc2becb3e777f016ff9a858 [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 Peskine003a4a92019-05-14 16:09:40 +02003275 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
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 Peskine003a4a92019-05-14 16:09:40 +02003280 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3281 * should be exact. */
3282 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3283 TEST_EQUAL( output_size,
3284 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003285 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003286
Gilles Peskine8817f612018-12-18 00:18:46 +01003287 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003288
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003289 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3290 psa_set_key_algorithm( &attributes, alg );
3291 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003292
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003293 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003294 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003295
Gilles Peskinefe11b722018-12-18 00:24:04 +01003296 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3297 nonce->x, nonce->len,
3298 additional_data->x,
3299 additional_data->len,
3300 input_data->x, input_data->len,
3301 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003302 &output_length ),
3303 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003304
3305 if( PSA_SUCCESS == expected_result )
3306 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003307 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003308
Gilles Peskine003a4a92019-05-14 16:09:40 +02003309 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3310 * should be exact. */
3311 TEST_EQUAL( input_data->len,
3312 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3313
Gilles Peskinefe11b722018-12-18 00:24:04 +01003314 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3315 nonce->x, nonce->len,
3316 additional_data->x,
3317 additional_data->len,
3318 output_data, output_length,
3319 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003320 &output_length2 ),
3321 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003322
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003323 ASSERT_COMPARE( input_data->x, input_data->len,
3324 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003325 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003326
Gilles Peskinea1cac842018-06-11 19:33:02 +02003327exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003328 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329 mbedtls_free( output_data );
3330 mbedtls_free( output_data2 );
3331 mbedtls_psa_crypto_free( );
3332}
3333/* END_CASE */
3334
3335/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003336void aead_encrypt( int key_type_arg, data_t *key_data,
3337 int alg_arg,
3338 data_t *nonce,
3339 data_t *additional_data,
3340 data_t *input_data,
3341 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003343 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344 psa_key_type_t key_type = key_type_arg;
3345 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003346 unsigned char *output_data = NULL;
3347 size_t output_size = 0;
3348 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003349 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351
Gilles Peskine4abf7412018-06-18 16:35:34 +02003352 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003353 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3354 * should be exact. */
3355 TEST_EQUAL( output_size,
3356 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003357 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003358
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003360
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3362 psa_set_key_algorithm( &attributes, alg );
3363 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003365 PSA_ASSERT( psa_import_key( &attributes, &handle,
3366 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003367
Gilles Peskine8817f612018-12-18 00:18:46 +01003368 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3369 nonce->x, nonce->len,
3370 additional_data->x, additional_data->len,
3371 input_data->x, input_data->len,
3372 output_data, output_size,
3373 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003374
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003375 ASSERT_COMPARE( expected_result->x, expected_result->len,
3376 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003377
Gilles Peskinea1cac842018-06-11 19:33:02 +02003378exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003379 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003380 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003381 mbedtls_psa_crypto_free( );
3382}
3383/* END_CASE */
3384
3385/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003386void aead_decrypt( int key_type_arg, data_t *key_data,
3387 int alg_arg,
3388 data_t *nonce,
3389 data_t *additional_data,
3390 data_t *input_data,
3391 data_t *expected_data,
3392 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003393{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003394 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003395 psa_key_type_t key_type = key_type_arg;
3396 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003397 unsigned char *output_data = NULL;
3398 size_t output_size = 0;
3399 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003400 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003402 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403
Gilles Peskine003a4a92019-05-14 16:09:40 +02003404 output_size = input_data->len - tag_length;
3405 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3406 * should be exact. */
3407 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3408 TEST_EQUAL( output_size,
3409 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003410 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003411
Gilles Peskine8817f612018-12-18 00:18:46 +01003412 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003413
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003414 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3415 psa_set_key_algorithm( &attributes, alg );
3416 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003418 PSA_ASSERT( psa_import_key( &attributes, &handle,
3419 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420
Gilles Peskinefe11b722018-12-18 00:24:04 +01003421 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3422 nonce->x, nonce->len,
3423 additional_data->x,
3424 additional_data->len,
3425 input_data->x, input_data->len,
3426 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003427 &output_length ),
3428 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003429
Gilles Peskine2d277862018-06-18 15:41:12 +02003430 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003431 ASSERT_COMPARE( expected_data->x, expected_data->len,
3432 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003433
Gilles Peskinea1cac842018-06-11 19:33:02 +02003434exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003435 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003436 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003437 mbedtls_psa_crypto_free( );
3438}
3439/* END_CASE */
3440
3441/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003442void signature_size( int type_arg,
3443 int bits,
3444 int alg_arg,
3445 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003446{
3447 psa_key_type_t type = type_arg;
3448 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003449 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003450 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003451exit:
3452 ;
3453}
3454/* END_CASE */
3455
3456/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003457void sign_deterministic( int key_type_arg, data_t *key_data,
3458 int alg_arg, data_t *input_data,
3459 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003460{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003461 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003462 psa_key_type_t key_type = key_type_arg;
3463 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003464 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003465 unsigned char *signature = NULL;
3466 size_t signature_size;
3467 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003469
Gilles Peskine8817f612018-12-18 00:18:46 +01003470 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003471
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3473 psa_set_key_algorithm( &attributes, alg );
3474 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003475
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003476 PSA_ASSERT( psa_import_key( &attributes, &handle,
3477 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003478 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3479 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003480
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003481 /* Allocate a buffer which has the size advertized by the
3482 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003483 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3484 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003485 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003486 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003487 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003488
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003489 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003490 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3491 input_data->x, input_data->len,
3492 signature, signature_size,
3493 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003494 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003495 ASSERT_COMPARE( output_data->x, output_data->len,
3496 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003497
3498exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003499 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003500 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003501 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003502 mbedtls_psa_crypto_free( );
3503}
3504/* END_CASE */
3505
3506/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003507void sign_fail( int key_type_arg, data_t *key_data,
3508 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003509 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003510{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003511 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003512 psa_key_type_t key_type = key_type_arg;
3513 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003514 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003515 psa_status_t actual_status;
3516 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003517 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003518 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003520
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003521 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003522
Gilles Peskine8817f612018-12-18 00:18:46 +01003523 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003524
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003525 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3526 psa_set_key_algorithm( &attributes, alg );
3527 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003528
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003529 PSA_ASSERT( psa_import_key( &attributes, &handle,
3530 key_data->x, key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003531
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003532 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003533 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003534 signature, signature_size,
3535 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003536 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003537 /* The value of *signature_length is unspecified on error, but
3538 * whatever it is, it should be less than signature_size, so that
3539 * if the caller tries to read *signature_length bytes without
3540 * checking the error code then they don't overflow a buffer. */
3541 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003542
3543exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003544 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003545 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003546 mbedtls_free( signature );
3547 mbedtls_psa_crypto_free( );
3548}
3549/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003550
3551/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003552void sign_verify( int key_type_arg, data_t *key_data,
3553 int alg_arg, data_t *input_data )
3554{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003555 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003556 psa_key_type_t key_type = key_type_arg;
3557 psa_algorithm_t alg = alg_arg;
3558 size_t key_bits;
3559 unsigned char *signature = NULL;
3560 size_t signature_size;
3561 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003562 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003563
Gilles Peskine8817f612018-12-18 00:18:46 +01003564 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003565
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003566 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3567 psa_set_key_algorithm( &attributes, alg );
3568 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003569
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003570 PSA_ASSERT( psa_import_key( &attributes, &handle,
3571 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003572 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3573 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003574
3575 /* Allocate a buffer which has the size advertized by the
3576 * library. */
3577 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3578 key_bits, alg );
3579 TEST_ASSERT( signature_size != 0 );
3580 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003581 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003582
3583 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003584 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3585 input_data->x, input_data->len,
3586 signature, signature_size,
3587 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003588 /* Check that the signature length looks sensible. */
3589 TEST_ASSERT( signature_length <= signature_size );
3590 TEST_ASSERT( signature_length > 0 );
3591
3592 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003593 PSA_ASSERT( psa_asymmetric_verify(
3594 handle, alg,
3595 input_data->x, input_data->len,
3596 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003597
3598 if( input_data->len != 0 )
3599 {
3600 /* Flip a bit in the input and verify that the signature is now
3601 * detected as invalid. Flip a bit at the beginning, not at the end,
3602 * because ECDSA may ignore the last few bits of the input. */
3603 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003604 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3605 input_data->x, input_data->len,
3606 signature, signature_length ),
3607 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003608 }
3609
3610exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003611 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003612 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003613 mbedtls_free( signature );
3614 mbedtls_psa_crypto_free( );
3615}
3616/* END_CASE */
3617
3618/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003619void asymmetric_verify( int key_type_arg, data_t *key_data,
3620 int alg_arg, data_t *hash_data,
3621 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003622{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003623 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003624 psa_key_type_t key_type = key_type_arg;
3625 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003627
Gilles Peskine69c12672018-06-28 00:07:19 +02003628 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3629
Gilles Peskine8817f612018-12-18 00:18:46 +01003630 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003631
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003632 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3633 psa_set_key_algorithm( &attributes, alg );
3634 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003635
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003636 PSA_ASSERT( psa_import_key( &attributes, &handle,
3637 key_data->x, key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003638
Gilles Peskine8817f612018-12-18 00:18:46 +01003639 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3640 hash_data->x, hash_data->len,
3641 signature_data->x,
3642 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003643exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003644 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003645 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003646 mbedtls_psa_crypto_free( );
3647}
3648/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003649
3650/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003651void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3652 int alg_arg, data_t *hash_data,
3653 data_t *signature_data,
3654 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003655{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003656 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003657 psa_key_type_t key_type = key_type_arg;
3658 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659 psa_status_t actual_status;
3660 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003661 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662
Gilles Peskine8817f612018-12-18 00:18:46 +01003663 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003665 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3666 psa_set_key_algorithm( &attributes, alg );
3667 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003668
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003669 PSA_ASSERT( psa_import_key( &attributes, &handle,
3670 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003671
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003672 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003673 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003674 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003675 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003676
Gilles Peskinefe11b722018-12-18 00:24:04 +01003677 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003678
3679exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003680 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003681 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003682 mbedtls_psa_crypto_free( );
3683}
3684/* END_CASE */
3685
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003686/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003687void asymmetric_encrypt( int key_type_arg,
3688 data_t *key_data,
3689 int alg_arg,
3690 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003691 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003692 int expected_output_length_arg,
3693 int expected_status_arg )
3694{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003695 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003696 psa_key_type_t key_type = key_type_arg;
3697 psa_algorithm_t alg = alg_arg;
3698 size_t expected_output_length = expected_output_length_arg;
3699 size_t key_bits;
3700 unsigned char *output = NULL;
3701 size_t output_size;
3702 size_t output_length = ~0;
3703 psa_status_t actual_status;
3704 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003706
Gilles Peskine8817f612018-12-18 00:18:46 +01003707 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003708
Gilles Peskine656896e2018-06-29 19:12:28 +02003709 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003710 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3711 psa_set_key_algorithm( &attributes, alg );
3712 psa_set_key_type( &attributes, key_type );
3713 PSA_ASSERT( psa_import_key( &attributes, &handle,
3714 key_data->x, key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003715
3716 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003717 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3718 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003719 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003720 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003721
3722 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003723 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003724 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003725 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003726 output, output_size,
3727 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003728 TEST_EQUAL( actual_status, expected_status );
3729 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003730
Gilles Peskine68428122018-06-30 18:42:41 +02003731 /* If the label is empty, the test framework puts a non-null pointer
3732 * in label->x. Test that a null pointer works as well. */
3733 if( label->len == 0 )
3734 {
3735 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003736 if( output_size != 0 )
3737 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003738 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003739 input_data->x, input_data->len,
3740 NULL, label->len,
3741 output, output_size,
3742 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003743 TEST_EQUAL( actual_status, expected_status );
3744 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003745 }
3746
Gilles Peskine656896e2018-06-29 19:12:28 +02003747exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003748 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003749 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003750 mbedtls_free( output );
3751 mbedtls_psa_crypto_free( );
3752}
3753/* END_CASE */
3754
3755/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003756void asymmetric_encrypt_decrypt( int key_type_arg,
3757 data_t *key_data,
3758 int alg_arg,
3759 data_t *input_data,
3760 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003761{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003762 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003763 psa_key_type_t key_type = key_type_arg;
3764 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003765 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003766 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003767 size_t output_size;
3768 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003769 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003770 size_t output2_size;
3771 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003773
Gilles Peskine8817f612018-12-18 00:18:46 +01003774 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003775
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003776 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3777 psa_set_key_algorithm( &attributes, alg );
3778 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003779
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003780 PSA_ASSERT( psa_import_key( &attributes, &handle,
3781 key_data->x, key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003782
3783 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003784 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3785 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003786 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003787 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003788 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003789 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003790
Gilles Peskineeebd7382018-06-08 18:11:54 +02003791 /* We test encryption by checking that encrypt-then-decrypt gives back
3792 * the original plaintext because of the non-optional random
3793 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003794 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3795 input_data->x, input_data->len,
3796 label->x, label->len,
3797 output, output_size,
3798 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003799 /* We don't know what ciphertext length to expect, but check that
3800 * it looks sensible. */
3801 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003802
Gilles Peskine8817f612018-12-18 00:18:46 +01003803 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3804 output, output_length,
3805 label->x, label->len,
3806 output2, output2_size,
3807 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003808 ASSERT_COMPARE( input_data->x, input_data->len,
3809 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003810
3811exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003812 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003813 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003814 mbedtls_free( output );
3815 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003816 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003817}
3818/* END_CASE */
3819
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003820/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003821void asymmetric_decrypt( int key_type_arg,
3822 data_t *key_data,
3823 int alg_arg,
3824 data_t *input_data,
3825 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003826 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003827{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003828 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003829 psa_key_type_t key_type = key_type_arg;
3830 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003831 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003832 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003833 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003834 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003835
Jaeden Amero412654a2019-02-06 12:57:46 +00003836 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003837 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003838
Gilles Peskine8817f612018-12-18 00:18:46 +01003839 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003840
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003841 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3842 psa_set_key_algorithm( &attributes, alg );
3843 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003844
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003845 PSA_ASSERT( psa_import_key( &attributes, &handle,
3846 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003847
Gilles Peskine8817f612018-12-18 00:18:46 +01003848 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3849 input_data->x, input_data->len,
3850 label->x, label->len,
3851 output,
3852 output_size,
3853 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003854 ASSERT_COMPARE( expected_data->x, expected_data->len,
3855 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003856
Gilles Peskine68428122018-06-30 18:42:41 +02003857 /* If the label is empty, the test framework puts a non-null pointer
3858 * in label->x. Test that a null pointer works as well. */
3859 if( label->len == 0 )
3860 {
3861 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003862 if( output_size != 0 )
3863 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003864 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3865 input_data->x, input_data->len,
3866 NULL, label->len,
3867 output,
3868 output_size,
3869 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003870 ASSERT_COMPARE( expected_data->x, expected_data->len,
3871 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003872 }
3873
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003874exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003875 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003876 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003877 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003878 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003879}
3880/* END_CASE */
3881
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003882/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003883void asymmetric_decrypt_fail( int key_type_arg,
3884 data_t *key_data,
3885 int alg_arg,
3886 data_t *input_data,
3887 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003888 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003889 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003890{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003891 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003892 psa_key_type_t key_type = key_type_arg;
3893 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003894 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003895 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003896 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003897 psa_status_t actual_status;
3898 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003899 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003900
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003901 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003902
Gilles Peskine8817f612018-12-18 00:18:46 +01003903 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003904
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003905 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3906 psa_set_key_algorithm( &attributes, alg );
3907 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003908
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003909 PSA_ASSERT( psa_import_key( &attributes, &handle,
3910 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003911
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003912 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003913 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003914 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003915 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003916 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003917 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003918 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003919
Gilles Peskine68428122018-06-30 18:42:41 +02003920 /* If the label is empty, the test framework puts a non-null pointer
3921 * in label->x. Test that a null pointer works as well. */
3922 if( label->len == 0 )
3923 {
3924 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003925 if( output_size != 0 )
3926 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003927 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003928 input_data->x, input_data->len,
3929 NULL, label->len,
3930 output, output_size,
3931 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003932 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003933 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003934 }
3935
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003936exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003937 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003938 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003939 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940 mbedtls_psa_crypto_free( );
3941}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003942/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003943
3944/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003945void crypto_generator_init( )
3946{
3947 /* Test each valid way of initializing the object, except for `= {0}`, as
3948 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3949 * though it's OK by the C standard. We could test for this, but we'd need
3950 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003951 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003952 psa_crypto_generator_t func = psa_crypto_generator_init( );
3953 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3954 psa_crypto_generator_t zero;
3955
3956 memset( &zero, 0, sizeof( zero ) );
3957
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003958 /* A default generator should not be able to report its capacity. */
3959 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
3960 PSA_ERROR_BAD_STATE );
3961 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
3962 PSA_ERROR_BAD_STATE );
3963 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
3964 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003965
3966 /* A default generator should be abortable without error. */
3967 PSA_ASSERT( psa_generator_abort(&func) );
3968 PSA_ASSERT( psa_generator_abort(&init) );
3969 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003970}
3971/* END_CASE */
3972
3973/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003974void derive_setup( int key_type_arg,
3975 data_t *key_data,
3976 int alg_arg,
3977 data_t *salt,
3978 data_t *label,
3979 int requested_capacity_arg,
3980 int expected_status_arg )
3981{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003982 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003983 size_t key_type = key_type_arg;
3984 psa_algorithm_t alg = alg_arg;
3985 size_t requested_capacity = requested_capacity_arg;
3986 psa_status_t expected_status = expected_status_arg;
3987 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003989
Gilles Peskine8817f612018-12-18 00:18:46 +01003990 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003991
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003992 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3993 psa_set_key_algorithm( &attributes, alg );
3994 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003995
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003996 PSA_ASSERT( psa_import_key( &attributes, &handle,
3997 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003998
Gilles Peskinefe11b722018-12-18 00:24:04 +01003999 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4000 salt->x, salt->len,
4001 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004002 requested_capacity ),
4003 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004004
4005exit:
4006 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004007 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004008 mbedtls_psa_crypto_free( );
4009}
4010/* END_CASE */
4011
4012/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004013void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004014{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004015 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004016 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004017 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004018 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004019 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004020 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004021 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4022 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4023 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004025
Gilles Peskine8817f612018-12-18 00:18:46 +01004026 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004027
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004028 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4029 psa_set_key_algorithm( &attributes, alg );
4030 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004031
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004032 PSA_ASSERT( psa_import_key( &attributes, &handle,
4033 key_data, sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004034
4035 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004036 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4037 NULL, 0,
4038 NULL, 0,
4039 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004040
4041 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004042 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4043 NULL, 0,
4044 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004045 capacity ),
4046 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004047
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004048 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004049
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004050 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004051 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004052
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004053exit:
4054 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004055 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004056 mbedtls_psa_crypto_free( );
4057}
4058/* END_CASE */
4059
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004060/* BEGIN_CASE */
4061void test_derive_invalid_generator_tests( )
4062{
4063 uint8_t output_buffer[16];
4064 size_t buffer_size = 16;
4065 size_t capacity = 0;
4066 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4067
Nir Sonnenschein50789302018-10-31 12:16:38 +02004068 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004069 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004070
4071 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004072 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004073
Gilles Peskine8817f612018-12-18 00:18:46 +01004074 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004075
Nir Sonnenschein50789302018-10-31 12:16:38 +02004076 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004077 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004078
Nir Sonnenschein50789302018-10-31 12:16:38 +02004079 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004080 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004081
4082exit:
4083 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004084}
4085/* END_CASE */
4086
4087/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004088void derive_output( int alg_arg,
4089 data_t *key_data,
4090 data_t *salt,
4091 data_t *label,
4092 int requested_capacity_arg,
4093 data_t *expected_output1,
4094 data_t *expected_output2 )
4095{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004096 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004097 psa_algorithm_t alg = alg_arg;
4098 size_t requested_capacity = requested_capacity_arg;
4099 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4100 uint8_t *expected_outputs[2] =
4101 {expected_output1->x, expected_output2->x};
4102 size_t output_sizes[2] =
4103 {expected_output1->len, expected_output2->len};
4104 size_t output_buffer_size = 0;
4105 uint8_t *output_buffer = NULL;
4106 size_t expected_capacity;
4107 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004109 psa_status_t status;
4110 unsigned i;
4111
4112 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4113 {
4114 if( output_sizes[i] > output_buffer_size )
4115 output_buffer_size = output_sizes[i];
4116 if( output_sizes[i] == 0 )
4117 expected_outputs[i] = NULL;
4118 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004119 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004121
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004122 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4123 psa_set_key_algorithm( &attributes, alg );
4124 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004125
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004126 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004127 key_data->x, key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004128
4129 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004130 if( PSA_ALG_IS_HKDF( alg ) )
4131 {
4132 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4133 PSA_ASSERT( psa_set_generator_capacity( &generator,
4134 requested_capacity ) );
4135 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4136 PSA_KDF_STEP_SALT,
4137 salt->x, salt->len ) );
4138 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4139 PSA_KDF_STEP_SECRET,
4140 handle ) );
4141 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4142 PSA_KDF_STEP_INFO,
4143 label->x, label->len ) );
4144 }
4145 else
4146 {
4147 // legacy
4148 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4149 salt->x, salt->len,
4150 label->x, label->len,
4151 requested_capacity ) );
4152 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004153 PSA_ASSERT( psa_get_generator_capacity( &generator,
4154 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004155 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004156 expected_capacity = requested_capacity;
4157
4158 /* Expansion phase. */
4159 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4160 {
4161 /* Read some bytes. */
4162 status = psa_generator_read( &generator,
4163 output_buffer, output_sizes[i] );
4164 if( expected_capacity == 0 && output_sizes[i] == 0 )
4165 {
4166 /* Reading 0 bytes when 0 bytes are available can go either way. */
4167 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004168 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004169 continue;
4170 }
4171 else if( expected_capacity == 0 ||
4172 output_sizes[i] > expected_capacity )
4173 {
4174 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004175 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004176 expected_capacity = 0;
4177 continue;
4178 }
4179 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004180 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004181 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004182 ASSERT_COMPARE( output_buffer, output_sizes[i],
4183 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004184 /* Check the generator status. */
4185 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004186 PSA_ASSERT( psa_get_generator_capacity( &generator,
4187 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004188 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004189 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004190 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004191
4192exit:
4193 mbedtls_free( output_buffer );
4194 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004195 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004196 mbedtls_psa_crypto_free( );
4197}
4198/* END_CASE */
4199
4200/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004201void derive_full( int alg_arg,
4202 data_t *key_data,
4203 data_t *salt,
4204 data_t *label,
4205 int requested_capacity_arg )
4206{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004207 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004208 psa_algorithm_t alg = alg_arg;
4209 size_t requested_capacity = requested_capacity_arg;
4210 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4211 unsigned char output_buffer[16];
4212 size_t expected_capacity = requested_capacity;
4213 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004215
Gilles Peskine8817f612018-12-18 00:18:46 +01004216 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004217
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004218 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4219 psa_set_key_algorithm( &attributes, alg );
4220 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004221
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004222 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004223 key_data->x, key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004224
4225 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004226 if( PSA_ALG_IS_HKDF( alg ) )
4227 {
4228 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4229 PSA_ASSERT( psa_set_generator_capacity( &generator,
4230 requested_capacity ) );
4231 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4232 PSA_KDF_STEP_SALT,
4233 salt->x, salt->len ) );
4234 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4235 PSA_KDF_STEP_SECRET,
4236 handle ) );
4237 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4238 PSA_KDF_STEP_INFO,
4239 label->x, label->len ) );
4240 }
4241 else
4242 {
4243 // legacy
4244 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4245 salt->x, salt->len,
4246 label->x, label->len,
4247 requested_capacity ) );
4248 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004249 PSA_ASSERT( psa_get_generator_capacity( &generator,
4250 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004251 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004252
4253 /* Expansion phase. */
4254 while( current_capacity > 0 )
4255 {
4256 size_t read_size = sizeof( output_buffer );
4257 if( read_size > current_capacity )
4258 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004259 PSA_ASSERT( psa_generator_read( &generator,
4260 output_buffer,
4261 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004262 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004263 PSA_ASSERT( psa_get_generator_capacity( &generator,
4264 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004265 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004266 }
4267
4268 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004269 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004270 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004271
Gilles Peskine8817f612018-12-18 00:18:46 +01004272 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004273
4274exit:
4275 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004276 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004277 mbedtls_psa_crypto_free( );
4278}
4279/* END_CASE */
4280
4281/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004282void derive_key_exercise( int alg_arg,
4283 data_t *key_data,
4284 data_t *salt,
4285 data_t *label,
4286 int derived_type_arg,
4287 int derived_bits_arg,
4288 int derived_usage_arg,
4289 int derived_alg_arg )
4290{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004291 psa_key_handle_t base_handle = 0;
4292 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004293 psa_algorithm_t alg = alg_arg;
4294 psa_key_type_t derived_type = derived_type_arg;
4295 size_t derived_bits = derived_bits_arg;
4296 psa_key_usage_t derived_usage = derived_usage_arg;
4297 psa_algorithm_t derived_alg = derived_alg_arg;
4298 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4299 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004301 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004302
Gilles Peskine8817f612018-12-18 00:18:46 +01004303 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004304
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004305 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4306 psa_set_key_algorithm( &attributes, alg );
4307 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
4308 PSA_ASSERT( psa_import_key( &attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004309 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004310
4311 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004312 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4313 salt->x, salt->len,
4314 label->x, label->len,
4315 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004316 psa_set_key_usage_flags( &attributes, derived_usage );
4317 psa_set_key_algorithm( &attributes, derived_alg );
4318 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004319 psa_set_key_bits( &attributes, derived_bits );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004320 PSA_ASSERT( psa_generate_derived_key( &attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004321 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004322
4323 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004324 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4325 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4326 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004327
4328 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004329 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004330 goto exit;
4331
4332exit:
4333 psa_generator_abort( &generator );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004334 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004335 psa_destroy_key( base_handle );
4336 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004337 mbedtls_psa_crypto_free( );
4338}
4339/* END_CASE */
4340
4341/* BEGIN_CASE */
4342void derive_key_export( int alg_arg,
4343 data_t *key_data,
4344 data_t *salt,
4345 data_t *label,
4346 int bytes1_arg,
4347 int bytes2_arg )
4348{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004349 psa_key_handle_t base_handle = 0;
4350 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004351 psa_algorithm_t alg = alg_arg;
4352 size_t bytes1 = bytes1_arg;
4353 size_t bytes2 = bytes2_arg;
4354 size_t capacity = bytes1 + bytes2;
4355 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004356 uint8_t *output_buffer = NULL;
4357 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004358 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4359 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004360 size_t length;
4361
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004362 ASSERT_ALLOC( output_buffer, capacity );
4363 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004364 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004365
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004366 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4367 psa_set_key_algorithm( &base_attributes, alg );
4368 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4369 PSA_ASSERT( psa_import_key( &base_attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004370 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004371
4372 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004373 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4374 salt->x, salt->len,
4375 label->x, label->len,
4376 capacity ) );
4377 PSA_ASSERT( psa_generator_read( &generator,
4378 output_buffer,
4379 capacity ) );
4380 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004381
4382 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004383 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4384 salt->x, salt->len,
4385 label->x, label->len,
4386 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004387 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4388 psa_set_key_algorithm( &derived_attributes, 0 );
4389 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004390 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004391 PSA_ASSERT( psa_generate_derived_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004392 &generator ) );
4393 PSA_ASSERT( psa_export_key( derived_handle,
4394 export_buffer, bytes1,
4395 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004396 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004397 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004398 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004399 PSA_ASSERT( psa_generate_derived_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004400 &generator ) );
4401 PSA_ASSERT( psa_export_key( derived_handle,
4402 export_buffer + bytes1, bytes2,
4403 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004404 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004405
4406 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004407 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4408 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004409
4410exit:
4411 mbedtls_free( output_buffer );
4412 mbedtls_free( export_buffer );
4413 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004414 psa_destroy_key( base_handle );
4415 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004416 mbedtls_psa_crypto_free( );
4417}
4418/* END_CASE */
4419
4420/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004421void key_agreement_setup( int alg_arg,
4422 int our_key_type_arg, data_t *our_key_data,
4423 data_t *peer_key_data,
4424 int expected_status_arg )
4425{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004426 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004427 psa_algorithm_t alg = alg_arg;
4428 psa_key_type_t our_key_type = our_key_type_arg;
4429 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004430 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004431 psa_status_t expected_status = expected_status_arg;
4432 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004433
Gilles Peskine8817f612018-12-18 00:18:46 +01004434 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004435
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004436 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4437 psa_set_key_algorithm( &attributes, alg );
4438 psa_set_key_type( &attributes, our_key_type );
4439 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004440 our_key_data->x, our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004441
Gilles Peskine77f40d82019-04-11 21:27:06 +02004442 /* The tests currently include inputs that should fail at either step.
4443 * Test cases that fail at the setup step should be changed to call
4444 * key_derivation_setup instead, and this function should be renamed
4445 * to key_agreement_fail. */
4446 status = psa_key_derivation_setup( &generator, alg );
4447 if( status == PSA_SUCCESS )
4448 {
4449 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
4450 our_key,
4451 peer_key_data->x, peer_key_data->len ),
4452 expected_status );
4453 }
4454 else
4455 {
4456 TEST_ASSERT( status == expected_status );
4457 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004458
4459exit:
4460 psa_generator_abort( &generator );
4461 psa_destroy_key( our_key );
4462 mbedtls_psa_crypto_free( );
4463}
4464/* END_CASE */
4465
4466/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004467void raw_key_agreement( int alg_arg,
4468 int our_key_type_arg, data_t *our_key_data,
4469 data_t *peer_key_data,
4470 data_t *expected_output )
4471{
4472 psa_key_handle_t our_key = 0;
4473 psa_algorithm_t alg = alg_arg;
4474 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004475 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004476 unsigned char *output = NULL;
4477 size_t output_length = ~0;
4478
4479 ASSERT_ALLOC( output, expected_output->len );
4480 PSA_ASSERT( psa_crypto_init( ) );
4481
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004482 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4483 psa_set_key_algorithm( &attributes, alg );
4484 psa_set_key_type( &attributes, our_key_type );
4485 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004486 our_key_data->x, our_key_data->len ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004487
4488 PSA_ASSERT( psa_key_agreement_raw_shared_secret(
4489 alg, our_key,
4490 peer_key_data->x, peer_key_data->len,
4491 output, expected_output->len, &output_length ) );
4492 ASSERT_COMPARE( output, output_length,
4493 expected_output->x, expected_output->len );
4494
4495exit:
4496 mbedtls_free( output );
4497 psa_destroy_key( our_key );
4498 mbedtls_psa_crypto_free( );
4499}
4500/* END_CASE */
4501
4502/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004503void key_agreement_capacity( int alg_arg,
4504 int our_key_type_arg, data_t *our_key_data,
4505 data_t *peer_key_data,
4506 int expected_capacity_arg )
4507{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004508 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004509 psa_algorithm_t alg = alg_arg;
4510 psa_key_type_t our_key_type = our_key_type_arg;
4511 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004513 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004514 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004515
Gilles Peskine8817f612018-12-18 00:18:46 +01004516 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004517
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004518 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4519 psa_set_key_algorithm( &attributes, alg );
4520 psa_set_key_type( &attributes, our_key_type );
4521 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004522 our_key_data->x, our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004523
Gilles Peskine969c5d62019-01-16 15:53:06 +01004524 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4525 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004526 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004527 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004528 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4529 {
4530 /* The test data is for info="" */
4531 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4532 PSA_KDF_STEP_INFO,
4533 NULL, 0 ) );
4534 }
Gilles Peskine59685592018-09-18 12:11:34 +02004535
Gilles Peskinebf491972018-10-25 22:36:12 +02004536 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004537 PSA_ASSERT( psa_get_generator_capacity(
4538 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004539 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004540
Gilles Peskinebf491972018-10-25 22:36:12 +02004541 /* Test the actual capacity by reading the output. */
4542 while( actual_capacity > sizeof( output ) )
4543 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004544 PSA_ASSERT( psa_generator_read( &generator,
4545 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004546 actual_capacity -= sizeof( output );
4547 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004548 PSA_ASSERT( psa_generator_read( &generator,
4549 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004550 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004551 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004552
Gilles Peskine59685592018-09-18 12:11:34 +02004553exit:
4554 psa_generator_abort( &generator );
4555 psa_destroy_key( our_key );
4556 mbedtls_psa_crypto_free( );
4557}
4558/* END_CASE */
4559
4560/* BEGIN_CASE */
4561void key_agreement_output( int alg_arg,
4562 int our_key_type_arg, data_t *our_key_data,
4563 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004564 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004565{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004566 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004567 psa_algorithm_t alg = alg_arg;
4568 psa_key_type_t our_key_type = our_key_type_arg;
4569 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004571 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004572
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004573 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4574 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004575
Gilles Peskine8817f612018-12-18 00:18:46 +01004576 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004577
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004578 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4579 psa_set_key_algorithm( &attributes, alg );
4580 psa_set_key_type( &attributes, our_key_type );
4581 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004582 our_key_data->x, our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004583
Gilles Peskine969c5d62019-01-16 15:53:06 +01004584 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4585 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004586 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004587 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004588 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4589 {
4590 /* The test data is for info="" */
4591 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4592 PSA_KDF_STEP_INFO,
4593 NULL, 0 ) );
4594 }
Gilles Peskine59685592018-09-18 12:11:34 +02004595
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004596 PSA_ASSERT( psa_generator_read( &generator,
4597 actual_output,
4598 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004599 ASSERT_COMPARE( actual_output, expected_output1->len,
4600 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004601 if( expected_output2->len != 0 )
4602 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004603 PSA_ASSERT( psa_generator_read( &generator,
4604 actual_output,
4605 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004606 ASSERT_COMPARE( actual_output, expected_output2->len,
4607 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004608 }
Gilles Peskine59685592018-09-18 12:11:34 +02004609
4610exit:
4611 psa_generator_abort( &generator );
4612 psa_destroy_key( our_key );
4613 mbedtls_psa_crypto_free( );
4614 mbedtls_free( actual_output );
4615}
4616/* END_CASE */
4617
4618/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004619void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004620{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004621 size_t bytes = bytes_arg;
4622 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004623 unsigned char *output = NULL;
4624 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004625 size_t i;
4626 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004627
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004628 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4629 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004630 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004631
Gilles Peskine8817f612018-12-18 00:18:46 +01004632 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004633
Gilles Peskinea50d7392018-06-21 10:22:13 +02004634 /* Run several times, to ensure that every output byte will be
4635 * nonzero at least once with overwhelming probability
4636 * (2^(-8*number_of_runs)). */
4637 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004638 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004639 if( bytes != 0 )
4640 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004641 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004642
4643 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004644 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4645 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004646
4647 for( i = 0; i < bytes; i++ )
4648 {
4649 if( output[i] != 0 )
4650 ++changed[i];
4651 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004652 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004653
4654 /* Check that every byte was changed to nonzero at least once. This
4655 * validates that psa_generate_random is overwriting every byte of
4656 * the output buffer. */
4657 for( i = 0; i < bytes; i++ )
4658 {
4659 TEST_ASSERT( changed[i] != 0 );
4660 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004661
4662exit:
4663 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004664 mbedtls_free( output );
4665 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004666}
4667/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004668
4669/* BEGIN_CASE */
4670void generate_key( int type_arg,
4671 int bits_arg,
4672 int usage_arg,
4673 int alg_arg,
4674 int expected_status_arg )
4675{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004676 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004677 psa_key_type_t type = type_arg;
4678 psa_key_usage_t usage = usage_arg;
4679 size_t bits = bits_arg;
4680 psa_algorithm_t alg = alg_arg;
4681 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004683 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004684
Gilles Peskine8817f612018-12-18 00:18:46 +01004685 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004686
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004687 psa_set_key_usage_flags( &attributes, usage );
4688 psa_set_key_algorithm( &attributes, alg );
4689 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004690 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004691
4692 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004693 TEST_EQUAL( psa_generate_random_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004694 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004695 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004696
4697 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004698 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4699 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4700 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004701
Gilles Peskine818ca122018-06-20 18:16:48 +02004702 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004703 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004704 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004705
4706exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004707 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004708 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004709 mbedtls_psa_crypto_free( );
4710}
4711/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004712
Gilles Peskinee56e8782019-04-26 17:34:02 +02004713/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4714void generate_key_rsa( int bits_arg,
4715 data_t *e_arg,
4716 int expected_status_arg )
4717{
4718 psa_key_handle_t handle = 0;
4719 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEYPAIR;
4720 size_t bits = bits_arg;
4721 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4722 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4723 psa_status_t expected_status = expected_status_arg;
4724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4725 uint8_t *exported = NULL;
4726 size_t exported_size =
4727 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4728 size_t exported_length = SIZE_MAX;
4729 uint8_t *e_read_buffer = NULL;
4730 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004731 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004732 size_t e_read_length = SIZE_MAX;
4733
4734 if( e_arg->len == 0 ||
4735 ( e_arg->len == 3 &&
4736 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4737 {
4738 is_default_public_exponent = 1;
4739 e_read_size = 0;
4740 }
4741 ASSERT_ALLOC( e_read_buffer, e_read_size );
4742 ASSERT_ALLOC( exported, exported_size );
4743
4744 PSA_ASSERT( psa_crypto_init( ) );
4745
4746 psa_set_key_usage_flags( &attributes, usage );
4747 psa_set_key_algorithm( &attributes, alg );
4748 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4749 e_arg->x, e_arg->len ) );
4750 psa_set_key_bits( &attributes, bits );
4751
4752 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004753 TEST_EQUAL( psa_generate_random_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004754 if( expected_status != PSA_SUCCESS )
4755 goto exit;
4756
4757 /* Test the key information */
4758 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4759 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4760 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4761 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4762 e_read_buffer, e_read_size,
4763 &e_read_length ) );
4764 if( is_default_public_exponent )
4765 TEST_EQUAL( e_read_length, 0 );
4766 else
4767 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4768
4769 /* Do something with the key according to its type and permitted usage. */
4770 if( ! exercise_key( handle, usage, alg ) )
4771 goto exit;
4772
4773 /* Export the key and check the public exponent. */
4774 PSA_ASSERT( psa_export_public_key( handle,
4775 exported, exported_size,
4776 &exported_length ) );
4777 {
4778 uint8_t *p = exported;
4779 uint8_t *end = exported + exported_length;
4780 size_t len;
4781 /* RSAPublicKey ::= SEQUENCE {
4782 * modulus INTEGER, -- n
4783 * publicExponent INTEGER } -- e
4784 */
4785 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4786 MBEDTLS_ASN1_SEQUENCE |
4787 MBEDTLS_ASN1_CONSTRUCTED ) );
4788 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
4789 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4790 MBEDTLS_ASN1_INTEGER ) );
4791 if( len >= 1 && p[0] == 0 )
4792 {
4793 ++p;
4794 --len;
4795 }
4796 if( e_arg->len == 0 )
4797 {
4798 TEST_EQUAL( len, 3 );
4799 TEST_EQUAL( p[0], 1 );
4800 TEST_EQUAL( p[1], 0 );
4801 TEST_EQUAL( p[2], 1 );
4802 }
4803 else
4804 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4805 }
4806
4807exit:
4808 psa_reset_key_attributes( &attributes );
4809 psa_destroy_key( handle );
4810 mbedtls_psa_crypto_free( );
4811 mbedtls_free( e_read_buffer );
4812 mbedtls_free( exported );
4813}
4814/* END_CASE */
4815
Darryl Greend49a4992018-06-18 17:27:26 +01004816/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004817void persistent_key_load_key_from_storage( data_t *data,
4818 int type_arg, int bits_arg,
4819 int usage_flags_arg, int alg_arg,
4820 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004821{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004822 psa_key_id_t key_id = 1;
4823 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004824 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004825 psa_key_handle_t base_key = 0;
4826 psa_key_type_t type = type_arg;
4827 size_t bits = bits_arg;
4828 psa_key_usage_t usage_flags = usage_flags_arg;
4829 psa_algorithm_t alg = alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004830 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004831 unsigned char *first_export = NULL;
4832 unsigned char *second_export = NULL;
4833 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4834 size_t first_exported_length;
4835 size_t second_exported_length;
4836
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004837 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4838 {
4839 ASSERT_ALLOC( first_export, export_size );
4840 ASSERT_ALLOC( second_export, export_size );
4841 }
Darryl Greend49a4992018-06-18 17:27:26 +01004842
Gilles Peskine8817f612018-12-18 00:18:46 +01004843 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004844
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004845 psa_make_key_persistent( &attributes, key_id, PSA_KEY_LIFETIME_PERSISTENT );
4846 psa_set_key_usage_flags( &attributes, usage_flags );
4847 psa_set_key_algorithm( &attributes, alg );
4848 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004849 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004850
Darryl Green0c6575a2018-11-07 16:05:30 +00004851 switch( generation_method )
4852 {
4853 case IMPORT_KEY:
4854 /* Import the key */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004855 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004856 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004857 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004858
Darryl Green0c6575a2018-11-07 16:05:30 +00004859 case GENERATE_KEY:
4860 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004861 PSA_ASSERT( psa_generate_random_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004862 break;
4863
4864 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004865 {
4866 /* Create base key */
4867 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4868 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4869 psa_set_key_usage_flags( &base_attributes,
4870 PSA_KEY_USAGE_DERIVE );
4871 psa_set_key_algorithm( &base_attributes, derive_alg );
4872 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4873 PSA_ASSERT( psa_import_key( &base_attributes, &base_key,
4874 data->x, data->len ) );
4875 /* Derive a key. */
4876 PSA_ASSERT( psa_key_derivation_setup( &generator, derive_alg ) );
4877 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4878 PSA_KDF_STEP_SECRET,
4879 base_key ) );
4880 PSA_ASSERT( psa_key_derivation_input_bytes(
4881 &generator, PSA_KDF_STEP_INFO,
4882 NULL, 0 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004883 PSA_ASSERT( psa_generate_derived_key( &attributes, &handle,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004884 &generator ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004885 PSA_ASSERT( psa_generator_abort( &generator ) );
4886 PSA_ASSERT( psa_destroy_key( base_key ) );
4887 base_key = 0;
4888 }
Darryl Green0c6575a2018-11-07 16:05:30 +00004889 break;
4890 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004891 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004892
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004893 /* Export the key if permitted by the key policy. */
4894 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4895 {
4896 PSA_ASSERT( psa_export_key( handle,
4897 first_export, export_size,
4898 &first_exported_length ) );
4899 if( generation_method == IMPORT_KEY )
4900 ASSERT_COMPARE( data->x, data->len,
4901 first_export, first_exported_length );
4902 }
Darryl Greend49a4992018-06-18 17:27:26 +01004903
4904 /* Shutdown and restart */
4905 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004906 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004907
Darryl Greend49a4992018-06-18 17:27:26 +01004908 /* Check key slot still contains key data */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004909 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
Gilles Peskine8817f612018-12-18 00:18:46 +01004910 &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004911 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4912 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
4913 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4914 PSA_KEY_LIFETIME_PERSISTENT );
4915 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4916 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4917 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4918 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004919
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004920 /* Export the key again if permitted by the key policy. */
4921 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004922 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004923 PSA_ASSERT( psa_export_key( handle,
4924 second_export, export_size,
4925 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004926 ASSERT_COMPARE( first_export, first_exported_length,
4927 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004928 }
4929
4930 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004931 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004932 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004933
4934exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004935 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004936 mbedtls_free( first_export );
4937 mbedtls_free( second_export );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004938 psa_generator_abort( &generator );
4939 psa_destroy_key( base_key );
4940 if( handle == 0 )
4941 {
4942 /* In case there was a test failure after creating the persistent key
4943 * but while it was not open, try to re-open the persistent key
4944 * to delete it. */
4945 psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle );
4946 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004947 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004948 mbedtls_psa_crypto_free();
4949}
4950/* END_CASE */