blob: 4ae9deb0947043ba9b7780b02ab6fac90e752359 [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
Gilles Peskinec87af662019-05-15 16:12:22 +02001099 psa_set_key_id( &attributes, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001100 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
Gilles Peskinec87af662019-05-15 16:12:22 +02001182 psa_set_key_id( &attributes, id );
1183 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001184 psa_set_key_usage_flags( &attributes, usage_flags );
1185 psa_set_key_algorithm( &attributes, alg );
1186 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001187 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001188
1189 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1190 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1191 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1192 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1193 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001194 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001195
1196 psa_reset_key_attributes( &attributes );
1197
1198 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1199 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1200 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1201 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1202 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001203 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001204}
1205/* END_CASE */
1206
1207/* BEGIN_CASE */
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001208void import( data_t *data, int type_arg,
1209 int attr_bits_arg,
1210 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001211{
1212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1213 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001214 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001215 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001216 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001217 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001218 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001219
Gilles Peskine8817f612018-12-18 00:18:46 +01001220 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001221
Gilles Peskine4747d192019-04-17 15:05:45 +02001222 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001223 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine4747d192019-04-17 15:05:45 +02001224 status = psa_import_key( &attributes, &handle, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001225 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001226 if( status != PSA_SUCCESS )
1227 goto exit;
1228
1229 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1230 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001231 if( attr_bits != 0 )
1232 TEST_EQUAL( attr_bits, got_attributes.bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001233
1234 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001235 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001236
1237exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001238 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001239 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001240 mbedtls_psa_crypto_free( );
1241}
1242/* END_CASE */
1243
1244/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001245void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1246{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001247 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001248 size_t bits = bits_arg;
1249 psa_status_t expected_status = expected_status_arg;
1250 psa_status_t status;
1251 psa_key_type_t type =
1252 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1253 size_t buffer_size = /* Slight overapproximations */
1254 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001255 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001256 unsigned char *p;
1257 int ret;
1258 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001259 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001260
Gilles Peskine8817f612018-12-18 00:18:46 +01001261 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001262 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001263
1264 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1265 bits, keypair ) ) >= 0 );
1266 length = ret;
1267
1268 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001269 psa_set_key_type( &attributes, type );
1270 status = psa_import_key( &attributes, &handle, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001271 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001272 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001273 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001274
1275exit:
1276 mbedtls_free( buffer );
1277 mbedtls_psa_crypto_free( );
1278}
1279/* END_CASE */
1280
1281/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001282void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001283 int type_arg,
1284 int alg_arg,
1285 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001286 int expected_bits,
1287 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001288 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001289 int canonical_input )
1290{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001291 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001292 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001293 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001294 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001295 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001296 unsigned char *exported = NULL;
1297 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001298 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001299 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001300 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001301 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001302 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001303
Moran Pekercb088e72018-07-17 17:36:59 +03001304 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001305 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001306 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001307 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001308 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001309
Gilles Peskine4747d192019-04-17 15:05:45 +02001310 psa_set_key_usage_flags( &attributes, usage_arg );
1311 psa_set_key_algorithm( &attributes, alg );
1312 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001313
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001314 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001315 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001316
1317 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001318 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1319 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1320 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001321
1322 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001323 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001324 exported, export_size,
1325 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001326 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001327
1328 /* The exported length must be set by psa_export_key() to a value between 0
1329 * and export_size. On errors, the exported length must be 0. */
1330 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1331 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1332 TEST_ASSERT( exported_length <= export_size );
1333
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001334 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001335 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001336 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001337 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001338 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001339 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001340 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001341
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001342 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001343 goto exit;
1344
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001345 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001346 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001347 else
1348 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001349 psa_key_handle_t handle2;
Gilles Peskine4747d192019-04-17 15:05:45 +02001350 PSA_ASSERT( psa_import_key( &attributes, &handle2,
1351 exported, exported_length ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001352 PSA_ASSERT( psa_export_key( handle2,
1353 reexported,
1354 export_size,
1355 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001356 ASSERT_COMPARE( exported, exported_length,
1357 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001358 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001359 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001360 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001361
1362destroy:
1363 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001364 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001365 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001366
1367exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001368 mbedtls_free( exported );
1369 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001370 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001371 mbedtls_psa_crypto_free( );
1372}
1373/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001374
Moran Pekerf709f4a2018-06-06 17:26:04 +03001375/* BEGIN_CASE */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001376void invalid_handle( int handle )
Moran Peker28a38e62018-11-07 16:18:24 +02001377{
Gilles Peskine8817f612018-12-18 00:18:46 +01001378 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001379 test_operations_on_invalid_handle( handle );
Moran Peker28a38e62018-11-07 16:18:24 +02001380
1381exit:
1382 mbedtls_psa_crypto_free( );
1383}
1384/* END_CASE */
1385
1386/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001387void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001388 int type_arg,
1389 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001390 int export_size_delta,
1391 int expected_export_status_arg,
1392 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001393{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001394 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001395 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001396 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001397 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001398 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001399 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001400 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001401 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001403
Gilles Peskine8817f612018-12-18 00:18:46 +01001404 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001405
Gilles Peskine4747d192019-04-17 15:05:45 +02001406 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1407 psa_set_key_algorithm( &attributes, alg );
1408 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001409
1410 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001411 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001412
Gilles Peskine49c25912018-10-29 15:15:31 +01001413 /* Export the public key */
1414 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001415 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001416 exported, export_size,
1417 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001418 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001419 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001420 {
1421 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1422 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001423 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1424 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001425 TEST_ASSERT( expected_public_key->len <=
1426 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001427 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1428 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001429 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001430
1431exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001432 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001433 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001434 psa_reset_key_attributes( &attributes );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001435 mbedtls_psa_crypto_free( );
1436}
1437/* END_CASE */
1438
Gilles Peskine20035e32018-02-03 22:44:14 +01001439/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001440void import_and_exercise_key( data_t *data,
1441 int type_arg,
1442 int bits_arg,
1443 int alg_arg )
1444{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001445 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001446 psa_key_type_t type = type_arg;
1447 size_t bits = bits_arg;
1448 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001449 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001451 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001452
Gilles Peskine8817f612018-12-18 00:18:46 +01001453 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001454
Gilles Peskine4747d192019-04-17 15:05:45 +02001455 psa_set_key_usage_flags( &attributes, usage );
1456 psa_set_key_algorithm( &attributes, alg );
1457 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001458
1459 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001460 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001461
1462 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001463 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1464 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1465 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001466
1467 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001468 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001469 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001470
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001471 PSA_ASSERT( psa_destroy_key( handle ) );
1472 test_operations_on_invalid_handle( handle );
1473
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001474exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001475 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001476 psa_reset_key_attributes( &got_attributes );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001477 mbedtls_psa_crypto_free( );
1478}
1479/* END_CASE */
1480
1481/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001482void key_policy( int usage_arg, int alg_arg )
1483{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001484 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001485 psa_algorithm_t alg = alg_arg;
1486 psa_key_usage_t usage = usage_arg;
1487 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1488 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001490
1491 memset( key, 0x2a, sizeof( key ) );
1492
Gilles Peskine8817f612018-12-18 00:18:46 +01001493 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001494
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001495 psa_set_key_usage_flags( &attributes, usage );
1496 psa_set_key_algorithm( &attributes, alg );
1497 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001498
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001499 PSA_ASSERT( psa_import_key( &attributes, &handle, key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001500
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001501 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1502 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1503 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1504 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001505
1506exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001507 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001508 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001509 mbedtls_psa_crypto_free( );
1510}
1511/* END_CASE */
1512
1513/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001514void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001515{
1516 /* Test each valid way of initializing the object, except for `= {0}`, as
1517 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1518 * though it's OK by the C standard. We could test for this, but we'd need
1519 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001520 psa_key_attributes_t func = psa_key_attributes_init( );
1521 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1522 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001523
1524 memset( &zero, 0, sizeof( zero ) );
1525
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001526 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1527 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1528 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001529
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001530 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1531 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1532 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1533
1534 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1535 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1536 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1537
1538 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1539 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1540 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1541
1542 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1543 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1544 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001545}
1546/* END_CASE */
1547
1548/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001549void mac_key_policy( int policy_usage,
1550 int policy_alg,
1551 int key_type,
1552 data_t *key_data,
1553 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001554{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001555 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001556 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001557 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001558 psa_status_t status;
1559 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001560
Gilles Peskine8817f612018-12-18 00:18:46 +01001561 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001562
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001563 psa_set_key_usage_flags( &attributes, policy_usage );
1564 psa_set_key_algorithm( &attributes, policy_alg );
1565 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001566
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001567 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001568 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001569
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001570 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001571 if( policy_alg == exercise_alg &&
1572 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001573 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001574 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001575 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001576 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001577
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001578 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001579 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001580 if( policy_alg == exercise_alg &&
1581 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001582 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001583 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001584 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001585
1586exit:
1587 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001588 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001589 mbedtls_psa_crypto_free( );
1590}
1591/* END_CASE */
1592
1593/* BEGIN_CASE */
1594void cipher_key_policy( int policy_usage,
1595 int policy_alg,
1596 int key_type,
1597 data_t *key_data,
1598 int exercise_alg )
1599{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001600 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001601 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001602 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 psa_status_t status;
1604
Gilles Peskine8817f612018-12-18 00:18:46 +01001605 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001606
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001607 psa_set_key_usage_flags( &attributes, policy_usage );
1608 psa_set_key_algorithm( &attributes, policy_alg );
1609 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001610
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001611 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001612 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001613
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001614 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001615 if( policy_alg == exercise_alg &&
1616 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001617 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001618 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001619 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001620 psa_cipher_abort( &operation );
1621
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001622 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001623 if( policy_alg == exercise_alg &&
1624 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001625 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001627 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628
1629exit:
1630 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001631 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001632 mbedtls_psa_crypto_free( );
1633}
1634/* END_CASE */
1635
1636/* BEGIN_CASE */
1637void aead_key_policy( int policy_usage,
1638 int policy_alg,
1639 int key_type,
1640 data_t *key_data,
1641 int nonce_length_arg,
1642 int tag_length_arg,
1643 int exercise_alg )
1644{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001645 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001647 psa_status_t status;
1648 unsigned char nonce[16] = {0};
1649 size_t nonce_length = nonce_length_arg;
1650 unsigned char tag[16];
1651 size_t tag_length = tag_length_arg;
1652 size_t output_length;
1653
1654 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1655 TEST_ASSERT( tag_length <= sizeof( tag ) );
1656
Gilles Peskine8817f612018-12-18 00:18:46 +01001657 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001658
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001659 psa_set_key_usage_flags( &attributes, policy_usage );
1660 psa_set_key_algorithm( &attributes, policy_alg );
1661 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001662
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001663 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001664 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001665
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001666 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001667 nonce, nonce_length,
1668 NULL, 0,
1669 NULL, 0,
1670 tag, tag_length,
1671 &output_length );
1672 if( policy_alg == exercise_alg &&
1673 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001674 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001675 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001676 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001677
1678 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001679 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001680 nonce, nonce_length,
1681 NULL, 0,
1682 tag, tag_length,
1683 NULL, 0,
1684 &output_length );
1685 if( policy_alg == exercise_alg &&
1686 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001687 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001688 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001689 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001690
1691exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001692 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001693 mbedtls_psa_crypto_free( );
1694}
1695/* END_CASE */
1696
1697/* BEGIN_CASE */
1698void asymmetric_encryption_key_policy( int policy_usage,
1699 int policy_alg,
1700 int key_type,
1701 data_t *key_data,
1702 int exercise_alg )
1703{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001704 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001706 psa_status_t status;
1707 size_t key_bits;
1708 size_t buffer_length;
1709 unsigned char *buffer = NULL;
1710 size_t output_length;
1711
Gilles Peskine8817f612018-12-18 00:18:46 +01001712 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001713
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001714 psa_set_key_usage_flags( &attributes, policy_usage );
1715 psa_set_key_algorithm( &attributes, policy_alg );
1716 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001717
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001718 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001719 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001720
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001721 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1722 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001723 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1724 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001725 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001726
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001727 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001728 NULL, 0,
1729 NULL, 0,
1730 buffer, buffer_length,
1731 &output_length );
1732 if( policy_alg == exercise_alg &&
1733 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001734 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001735 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001736 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001737
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001738 if( buffer_length != 0 )
1739 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001740 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001741 buffer, buffer_length,
1742 NULL, 0,
1743 buffer, buffer_length,
1744 &output_length );
1745 if( policy_alg == exercise_alg &&
1746 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001747 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001748 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001749 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001750
1751exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001752 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001753 psa_reset_key_attributes( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001754 mbedtls_psa_crypto_free( );
1755 mbedtls_free( buffer );
1756}
1757/* END_CASE */
1758
1759/* BEGIN_CASE */
1760void asymmetric_signature_key_policy( int policy_usage,
1761 int policy_alg,
1762 int key_type,
1763 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001764 int exercise_alg,
1765 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001766{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001767 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001769 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001770 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1771 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1772 * compatible with the policy and `payload_length_arg` is supposed to be
1773 * a valid input length to sign. If `payload_length_arg <= 0`,
1774 * `exercise_alg` is supposed to be forbidden by the policy. */
1775 int compatible_alg = payload_length_arg > 0;
1776 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001777 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1778 size_t signature_length;
1779
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001781
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001782 psa_set_key_usage_flags( &attributes, policy_usage );
1783 psa_set_key_algorithm( &attributes, policy_alg );
1784 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001785
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001786 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001787 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001788
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001789 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001790 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001791 signature, sizeof( signature ),
1792 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001793 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001794 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001795 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001796 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001797
1798 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001799 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001800 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001801 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001802 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001803 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001804 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001805 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001806
1807exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001808 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001809 mbedtls_psa_crypto_free( );
1810}
1811/* END_CASE */
1812
1813/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001814void derive_key_policy( int policy_usage,
1815 int policy_alg,
1816 int key_type,
1817 data_t *key_data,
1818 int exercise_alg )
1819{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001820 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001822 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1823 psa_status_t status;
1824
Gilles Peskine8817f612018-12-18 00:18:46 +01001825 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001826
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001827 psa_set_key_usage_flags( &attributes, policy_usage );
1828 psa_set_key_algorithm( &attributes, policy_alg );
1829 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001830
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001831 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001832 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001833
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001834 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001835 exercise_alg,
1836 NULL, 0,
1837 NULL, 0,
1838 1 );
1839 if( policy_alg == exercise_alg &&
1840 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001841 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001842 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001843 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001844
1845exit:
1846 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001847 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001848 mbedtls_psa_crypto_free( );
1849}
1850/* END_CASE */
1851
1852/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001853void agreement_key_policy( int policy_usage,
1854 int policy_alg,
1855 int key_type_arg,
1856 data_t *key_data,
1857 int exercise_alg )
1858{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001859 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001861 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001862 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1863 psa_status_t status;
1864
Gilles Peskine8817f612018-12-18 00:18:46 +01001865 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001866
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001867 psa_set_key_usage_flags( &attributes, policy_usage );
1868 psa_set_key_algorithm( &attributes, policy_alg );
1869 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001870
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001871 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001872 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001873
Gilles Peskine969c5d62019-01-16 15:53:06 +01001874 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
1875 status = key_agreement_with_self( &generator, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001876
Gilles Peskine01d718c2018-09-18 12:01:02 +02001877 if( policy_alg == exercise_alg &&
1878 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001879 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001880 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001881 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001882
1883exit:
1884 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001885 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001886 mbedtls_psa_crypto_free( );
1887}
1888/* END_CASE */
1889
1890/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001891void raw_agreement_key_policy( int policy_usage,
1892 int policy_alg,
1893 int key_type_arg,
1894 data_t *key_data,
1895 int exercise_alg )
1896{
1897 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001899 psa_key_type_t key_type = key_type_arg;
1900 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1901 psa_status_t status;
1902
1903 PSA_ASSERT( psa_crypto_init( ) );
1904
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001905 psa_set_key_usage_flags( &attributes, policy_usage );
1906 psa_set_key_algorithm( &attributes, policy_alg );
1907 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001908
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001909 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001910 key_data->x, key_data->len ) );
1911
1912 status = raw_key_agreement_with_self( exercise_alg, handle );
1913
1914 if( policy_alg == exercise_alg &&
1915 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1916 PSA_ASSERT( status );
1917 else
1918 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1919
1920exit:
1921 psa_generator_abort( &generator );
1922 psa_destroy_key( handle );
1923 mbedtls_psa_crypto_free( );
1924}
1925/* END_CASE */
1926
1927/* BEGIN_CASE */
Gilles Peskine4a644642019-05-03 17:14:08 +02001928void copy_success( int source_usage_arg, int source_alg_arg,
1929 int type_arg, data_t *material,
1930 int copy_attributes,
1931 int target_usage_arg, int target_alg_arg,
1932 int expected_usage_arg, int expected_alg_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001933{
Gilles Peskineca25db92019-04-19 11:43:08 +02001934 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1935 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001936 psa_key_usage_t expected_usage = expected_usage_arg;
1937 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02001938 psa_key_handle_t source_handle = 0;
1939 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001940 uint8_t *export_buffer = NULL;
1941
Gilles Peskine57ab7212019-01-28 13:03:09 +01001942 PSA_ASSERT( psa_crypto_init( ) );
1943
Gilles Peskineca25db92019-04-19 11:43:08 +02001944 /* Prepare the source key. */
1945 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1946 psa_set_key_algorithm( &source_attributes, source_alg_arg );
1947 psa_set_key_type( &source_attributes, type_arg );
1948 PSA_ASSERT( psa_import_key( &source_attributes, &source_handle,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001949 material->x, material->len ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001950 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001951
Gilles Peskineca25db92019-04-19 11:43:08 +02001952 /* Prepare the target attributes. */
1953 if( copy_attributes )
1954 target_attributes = source_attributes;
1955 if( target_usage_arg != -1 )
1956 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1957 if( target_alg_arg != -1 )
1958 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001959
1960 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02001961 PSA_ASSERT( psa_copy_key( source_handle,
1962 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001963
1964 /* Destroy the source to ensure that this doesn't affect the target. */
1965 PSA_ASSERT( psa_destroy_key( source_handle ) );
1966
1967 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02001968 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
1969 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1970 psa_get_key_type( &target_attributes ) );
1971 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1972 psa_get_key_bits( &target_attributes ) );
1973 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1974 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001975 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1976 {
1977 size_t length;
1978 ASSERT_ALLOC( export_buffer, material->len );
1979 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
1980 material->len, &length ) );
1981 ASSERT_COMPARE( material->x, material->len,
1982 export_buffer, length );
1983 }
1984 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
1985 goto exit;
1986
1987 PSA_ASSERT( psa_close_key( target_handle ) );
1988
1989exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001990 psa_reset_key_attributes( &source_attributes );
1991 psa_reset_key_attributes( &target_attributes );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001992 mbedtls_psa_crypto_free( );
1993 mbedtls_free( export_buffer );
1994}
1995/* END_CASE */
1996
1997/* BEGIN_CASE */
Gilles Peskine4a644642019-05-03 17:14:08 +02001998void copy_fail( int source_usage_arg, int source_alg_arg,
1999 int type_arg, data_t *material,
2000 int target_type_arg, int target_bits_arg,
2001 int target_usage_arg, int target_alg_arg,
2002 int expected_status_arg )
2003{
2004 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2005 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2006 psa_key_handle_t source_handle = 0;
2007 psa_key_handle_t target_handle = 0;
2008
2009 PSA_ASSERT( psa_crypto_init( ) );
2010
2011 /* Prepare the source key. */
2012 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2013 psa_set_key_algorithm( &source_attributes, source_alg_arg );
2014 psa_set_key_type( &source_attributes, type_arg );
2015 PSA_ASSERT( psa_import_key( &source_attributes, &source_handle,
2016 material->x, material->len ) );
2017
2018 /* Prepare the target attributes. */
2019 psa_set_key_type( &target_attributes, target_type_arg );
2020 psa_set_key_bits( &target_attributes, target_bits_arg );
2021 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2022 psa_set_key_algorithm( &target_attributes, target_alg_arg );
2023
2024 /* Try to copy the key. */
2025 TEST_EQUAL( psa_copy_key( source_handle,
2026 &target_attributes, &target_handle ),
2027 expected_status_arg );
2028exit:
2029 psa_reset_key_attributes( &source_attributes );
2030 psa_reset_key_attributes( &target_attributes );
2031 mbedtls_psa_crypto_free( );
2032}
2033/* END_CASE */
2034
2035/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002036void hash_operation_init( )
2037{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002038 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002039 /* Test each valid way of initializing the object, except for `= {0}`, as
2040 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2041 * though it's OK by the C standard. We could test for this, but we'd need
2042 * to supress the Clang warning for the test. */
2043 psa_hash_operation_t func = psa_hash_operation_init( );
2044 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2045 psa_hash_operation_t zero;
2046
2047 memset( &zero, 0, sizeof( zero ) );
2048
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002049 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002050 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2051 PSA_ERROR_BAD_STATE );
2052 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2053 PSA_ERROR_BAD_STATE );
2054 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2055 PSA_ERROR_BAD_STATE );
2056
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002057 /* A default hash operation should be abortable without error. */
2058 PSA_ASSERT( psa_hash_abort( &func ) );
2059 PSA_ASSERT( psa_hash_abort( &init ) );
2060 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002061}
2062/* END_CASE */
2063
2064/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002065void hash_setup( int alg_arg,
2066 int expected_status_arg )
2067{
2068 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002069 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002070 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002071 psa_status_t status;
2072
Gilles Peskine8817f612018-12-18 00:18:46 +01002073 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002074
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002075 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002076 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002077
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002078 /* Whether setup succeeded or failed, abort must succeed. */
2079 PSA_ASSERT( psa_hash_abort( &operation ) );
2080
2081 /* If setup failed, reproduce the failure, so as to
2082 * test the resulting state of the operation object. */
2083 if( status != PSA_SUCCESS )
2084 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2085
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002086 /* Now the operation object should be reusable. */
2087#if defined(KNOWN_SUPPORTED_HASH_ALG)
2088 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2089 PSA_ASSERT( psa_hash_abort( &operation ) );
2090#endif
2091
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002092exit:
2093 mbedtls_psa_crypto_free( );
2094}
2095/* END_CASE */
2096
2097/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002098void hash_bad_order( )
2099{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002100 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002101 unsigned char input[] = "";
2102 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002103 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002104 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2105 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2106 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002107 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002108 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002109 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002110
Gilles Peskine8817f612018-12-18 00:18:46 +01002111 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002112
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002113 /* Call setup twice in a row. */
2114 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2115 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2116 PSA_ERROR_BAD_STATE );
2117 PSA_ASSERT( psa_hash_abort( &operation ) );
2118
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002119 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002120 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002121 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002122 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002123
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002124 /* Call update after finish. */
2125 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2126 PSA_ASSERT( psa_hash_finish( &operation,
2127 hash, sizeof( hash ), &hash_len ) );
2128 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002129 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002130 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002131
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002132 /* Call verify without calling setup beforehand. */
2133 TEST_EQUAL( psa_hash_verify( &operation,
2134 valid_hash, sizeof( valid_hash ) ),
2135 PSA_ERROR_BAD_STATE );
2136 PSA_ASSERT( psa_hash_abort( &operation ) );
2137
2138 /* Call verify after finish. */
2139 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2140 PSA_ASSERT( psa_hash_finish( &operation,
2141 hash, sizeof( hash ), &hash_len ) );
2142 TEST_EQUAL( psa_hash_verify( &operation,
2143 valid_hash, sizeof( valid_hash ) ),
2144 PSA_ERROR_BAD_STATE );
2145 PSA_ASSERT( psa_hash_abort( &operation ) );
2146
2147 /* Call verify twice in a row. */
2148 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2149 PSA_ASSERT( psa_hash_verify( &operation,
2150 valid_hash, sizeof( valid_hash ) ) );
2151 TEST_EQUAL( psa_hash_verify( &operation,
2152 valid_hash, sizeof( valid_hash ) ),
2153 PSA_ERROR_BAD_STATE );
2154 PSA_ASSERT( psa_hash_abort( &operation ) );
2155
2156 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002157 TEST_EQUAL( psa_hash_finish( &operation,
2158 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002159 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002160 PSA_ASSERT( psa_hash_abort( &operation ) );
2161
2162 /* Call finish twice in a row. */
2163 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2164 PSA_ASSERT( psa_hash_finish( &operation,
2165 hash, sizeof( hash ), &hash_len ) );
2166 TEST_EQUAL( psa_hash_finish( &operation,
2167 hash, sizeof( hash ), &hash_len ),
2168 PSA_ERROR_BAD_STATE );
2169 PSA_ASSERT( psa_hash_abort( &operation ) );
2170
2171 /* Call finish after calling verify. */
2172 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2173 PSA_ASSERT( psa_hash_verify( &operation,
2174 valid_hash, sizeof( valid_hash ) ) );
2175 TEST_EQUAL( psa_hash_finish( &operation,
2176 hash, sizeof( hash ), &hash_len ),
2177 PSA_ERROR_BAD_STATE );
2178 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002179
2180exit:
2181 mbedtls_psa_crypto_free( );
2182}
2183/* END_CASE */
2184
itayzafrir27e69452018-11-01 14:26:34 +02002185/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2186void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002187{
2188 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002189 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2190 * appended to it */
2191 unsigned char hash[] = {
2192 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2193 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2194 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002195 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002196 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002197
Gilles Peskine8817f612018-12-18 00:18:46 +01002198 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002199
itayzafrir27e69452018-11-01 14:26:34 +02002200 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002201 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002202 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002203 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002204
itayzafrir27e69452018-11-01 14:26:34 +02002205 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002206 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002207 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002208 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002209
itayzafrir27e69452018-11-01 14:26:34 +02002210 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002211 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002212 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002213 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002214
itayzafrirec93d302018-10-18 18:01:10 +03002215exit:
2216 mbedtls_psa_crypto_free( );
2217}
2218/* END_CASE */
2219
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002220/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2221void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002222{
2223 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002224 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002225 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002226 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002227 size_t hash_len;
2228
Gilles Peskine8817f612018-12-18 00:18:46 +01002229 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002230
itayzafrir58028322018-10-25 10:22:01 +03002231 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002232 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002233 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002234 hash, expected_size - 1, &hash_len ),
2235 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002236
2237exit:
2238 mbedtls_psa_crypto_free( );
2239}
2240/* END_CASE */
2241
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002242/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2243void hash_clone_source_state( )
2244{
2245 psa_algorithm_t alg = PSA_ALG_SHA_256;
2246 unsigned char hash[PSA_HASH_MAX_SIZE];
2247 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2248 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2249 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2250 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2251 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2252 size_t hash_len;
2253
2254 PSA_ASSERT( psa_crypto_init( ) );
2255 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2256
2257 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2258 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2259 PSA_ASSERT( psa_hash_finish( &op_finished,
2260 hash, sizeof( hash ), &hash_len ) );
2261 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2262 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2263
2264 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2265 PSA_ERROR_BAD_STATE );
2266
2267 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2268 PSA_ASSERT( psa_hash_finish( &op_init,
2269 hash, sizeof( hash ), &hash_len ) );
2270 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2271 PSA_ASSERT( psa_hash_finish( &op_finished,
2272 hash, sizeof( hash ), &hash_len ) );
2273 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2274 PSA_ASSERT( psa_hash_finish( &op_aborted,
2275 hash, sizeof( hash ), &hash_len ) );
2276
2277exit:
2278 psa_hash_abort( &op_source );
2279 psa_hash_abort( &op_init );
2280 psa_hash_abort( &op_setup );
2281 psa_hash_abort( &op_finished );
2282 psa_hash_abort( &op_aborted );
2283 mbedtls_psa_crypto_free( );
2284}
2285/* END_CASE */
2286
2287/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2288void hash_clone_target_state( )
2289{
2290 psa_algorithm_t alg = PSA_ALG_SHA_256;
2291 unsigned char hash[PSA_HASH_MAX_SIZE];
2292 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2293 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2294 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2295 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2296 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2297 size_t hash_len;
2298
2299 PSA_ASSERT( psa_crypto_init( ) );
2300
2301 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2302 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2303 PSA_ASSERT( psa_hash_finish( &op_finished,
2304 hash, sizeof( hash ), &hash_len ) );
2305 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2306 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2307
2308 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2309 PSA_ASSERT( psa_hash_finish( &op_target,
2310 hash, sizeof( hash ), &hash_len ) );
2311
2312 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2313 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2314 PSA_ERROR_BAD_STATE );
2315 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2316 PSA_ERROR_BAD_STATE );
2317
2318exit:
2319 psa_hash_abort( &op_target );
2320 psa_hash_abort( &op_init );
2321 psa_hash_abort( &op_setup );
2322 psa_hash_abort( &op_finished );
2323 psa_hash_abort( &op_aborted );
2324 mbedtls_psa_crypto_free( );
2325}
2326/* END_CASE */
2327
itayzafrir58028322018-10-25 10:22:01 +03002328/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002329void mac_operation_init( )
2330{
Jaeden Amero252ef282019-02-15 14:05:35 +00002331 const uint8_t input[1] = { 0 };
2332
Jaeden Amero769ce272019-01-04 11:48:03 +00002333 /* Test each valid way of initializing the object, except for `= {0}`, as
2334 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2335 * though it's OK by the C standard. We could test for this, but we'd need
2336 * to supress the Clang warning for the test. */
2337 psa_mac_operation_t func = psa_mac_operation_init( );
2338 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2339 psa_mac_operation_t zero;
2340
2341 memset( &zero, 0, sizeof( zero ) );
2342
Jaeden Amero252ef282019-02-15 14:05:35 +00002343 /* A freshly-initialized MAC operation should not be usable. */
2344 TEST_EQUAL( psa_mac_update( &func,
2345 input, sizeof( input ) ),
2346 PSA_ERROR_BAD_STATE );
2347 TEST_EQUAL( psa_mac_update( &init,
2348 input, sizeof( input ) ),
2349 PSA_ERROR_BAD_STATE );
2350 TEST_EQUAL( psa_mac_update( &zero,
2351 input, sizeof( input ) ),
2352 PSA_ERROR_BAD_STATE );
2353
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002354 /* A default MAC operation should be abortable without error. */
2355 PSA_ASSERT( psa_mac_abort( &func ) );
2356 PSA_ASSERT( psa_mac_abort( &init ) );
2357 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002358}
2359/* END_CASE */
2360
2361/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002362void mac_setup( int key_type_arg,
2363 data_t *key,
2364 int alg_arg,
2365 int expected_status_arg )
2366{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002367 psa_key_type_t key_type = key_type_arg;
2368 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002369 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002370 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002371 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2372#if defined(KNOWN_SUPPORTED_MAC_ALG)
2373 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2374#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002375
Gilles Peskine8817f612018-12-18 00:18:46 +01002376 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002377
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002378 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2379 &operation, &status ) )
2380 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002381 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002382
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002383 /* The operation object should be reusable. */
2384#if defined(KNOWN_SUPPORTED_MAC_ALG)
2385 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2386 smoke_test_key_data,
2387 sizeof( smoke_test_key_data ),
2388 KNOWN_SUPPORTED_MAC_ALG,
2389 &operation, &status ) )
2390 goto exit;
2391 TEST_EQUAL( status, PSA_SUCCESS );
2392#endif
2393
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002394exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002395 mbedtls_psa_crypto_free( );
2396}
2397/* END_CASE */
2398
2399/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002400void mac_bad_order( )
2401{
2402 psa_key_handle_t handle = 0;
2403 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2404 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2405 const uint8_t key[] = {
2406 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2407 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2408 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002410 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2411 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2412 size_t sign_mac_length = 0;
2413 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2414 const uint8_t verify_mac[] = {
2415 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2416 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2417 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2418
2419 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002420 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2421 psa_set_key_algorithm( &attributes, alg );
2422 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002423
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002424 PSA_ASSERT( psa_import_key( &attributes, &handle,
Jaeden Amero252ef282019-02-15 14:05:35 +00002425 key, sizeof(key) ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002426
Jaeden Amero252ef282019-02-15 14:05:35 +00002427 /* Call update without calling setup beforehand. */
2428 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2429 PSA_ERROR_BAD_STATE );
2430 PSA_ASSERT( psa_mac_abort( &operation ) );
2431
2432 /* Call sign finish without calling setup beforehand. */
2433 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2434 &sign_mac_length),
2435 PSA_ERROR_BAD_STATE );
2436 PSA_ASSERT( psa_mac_abort( &operation ) );
2437
2438 /* Call verify finish without calling setup beforehand. */
2439 TEST_EQUAL( psa_mac_verify_finish( &operation,
2440 verify_mac, sizeof( verify_mac ) ),
2441 PSA_ERROR_BAD_STATE );
2442 PSA_ASSERT( psa_mac_abort( &operation ) );
2443
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002444 /* Call setup twice in a row. */
2445 PSA_ASSERT( psa_mac_sign_setup( &operation,
2446 handle, alg ) );
2447 TEST_EQUAL( psa_mac_sign_setup( &operation,
2448 handle, alg ),
2449 PSA_ERROR_BAD_STATE );
2450 PSA_ASSERT( psa_mac_abort( &operation ) );
2451
Jaeden Amero252ef282019-02-15 14:05:35 +00002452 /* Call update after sign finish. */
2453 PSA_ASSERT( psa_mac_sign_setup( &operation,
2454 handle, alg ) );
2455 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2456 PSA_ASSERT( psa_mac_sign_finish( &operation,
2457 sign_mac, sizeof( sign_mac ),
2458 &sign_mac_length ) );
2459 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2460 PSA_ERROR_BAD_STATE );
2461 PSA_ASSERT( psa_mac_abort( &operation ) );
2462
2463 /* Call update after verify finish. */
2464 PSA_ASSERT( psa_mac_verify_setup( &operation,
2465 handle, alg ) );
2466 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2467 PSA_ASSERT( psa_mac_verify_finish( &operation,
2468 verify_mac, sizeof( verify_mac ) ) );
2469 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2470 PSA_ERROR_BAD_STATE );
2471 PSA_ASSERT( psa_mac_abort( &operation ) );
2472
2473 /* Call sign finish twice in a row. */
2474 PSA_ASSERT( psa_mac_sign_setup( &operation,
2475 handle, alg ) );
2476 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2477 PSA_ASSERT( psa_mac_sign_finish( &operation,
2478 sign_mac, sizeof( sign_mac ),
2479 &sign_mac_length ) );
2480 TEST_EQUAL( psa_mac_sign_finish( &operation,
2481 sign_mac, sizeof( sign_mac ),
2482 &sign_mac_length ),
2483 PSA_ERROR_BAD_STATE );
2484 PSA_ASSERT( psa_mac_abort( &operation ) );
2485
2486 /* Call verify finish twice in a row. */
2487 PSA_ASSERT( psa_mac_verify_setup( &operation,
2488 handle, alg ) );
2489 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2490 PSA_ASSERT( psa_mac_verify_finish( &operation,
2491 verify_mac, sizeof( verify_mac ) ) );
2492 TEST_EQUAL( psa_mac_verify_finish( &operation,
2493 verify_mac, sizeof( verify_mac ) ),
2494 PSA_ERROR_BAD_STATE );
2495 PSA_ASSERT( psa_mac_abort( &operation ) );
2496
2497 /* Setup sign but try verify. */
2498 PSA_ASSERT( psa_mac_sign_setup( &operation,
2499 handle, alg ) );
2500 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2501 TEST_EQUAL( psa_mac_verify_finish( &operation,
2502 verify_mac, sizeof( verify_mac ) ),
2503 PSA_ERROR_BAD_STATE );
2504 PSA_ASSERT( psa_mac_abort( &operation ) );
2505
2506 /* Setup verify but try sign. */
2507 PSA_ASSERT( psa_mac_verify_setup( &operation,
2508 handle, alg ) );
2509 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2510 TEST_EQUAL( psa_mac_sign_finish( &operation,
2511 sign_mac, sizeof( sign_mac ),
2512 &sign_mac_length ),
2513 PSA_ERROR_BAD_STATE );
2514 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002515
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002516exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002517 mbedtls_psa_crypto_free( );
2518}
2519/* END_CASE */
2520
2521/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002522void mac_sign( int key_type_arg,
2523 data_t *key,
2524 int alg_arg,
2525 data_t *input,
2526 data_t *expected_mac )
2527{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002528 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002529 psa_key_type_t key_type = key_type_arg;
2530 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002531 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002533 /* Leave a little extra room in the output buffer. At the end of the
2534 * test, we'll check that the implementation didn't overwrite onto
2535 * this extra room. */
2536 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2537 size_t mac_buffer_size =
2538 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2539 size_t mac_length = 0;
2540
2541 memset( actual_mac, '+', sizeof( actual_mac ) );
2542 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2543 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2544
Gilles Peskine8817f612018-12-18 00:18:46 +01002545 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002546
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002547 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2548 psa_set_key_algorithm( &attributes, alg );
2549 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002550
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002551 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002552 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002553
2554 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002555 PSA_ASSERT( psa_mac_sign_setup( &operation,
2556 handle, alg ) );
2557 PSA_ASSERT( psa_mac_update( &operation,
2558 input->x, input->len ) );
2559 PSA_ASSERT( psa_mac_sign_finish( &operation,
2560 actual_mac, mac_buffer_size,
2561 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002562
2563 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002564 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2565 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002566
2567 /* Verify that the end of the buffer is untouched. */
2568 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2569 sizeof( actual_mac ) - mac_length ) );
2570
2571exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002572 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002573 mbedtls_psa_crypto_free( );
2574}
2575/* END_CASE */
2576
2577/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002578void mac_verify( int key_type_arg,
2579 data_t *key,
2580 int alg_arg,
2581 data_t *input,
2582 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002584 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585 psa_key_type_t key_type = key_type_arg;
2586 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002587 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002588 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589
Gilles Peskine69c12672018-06-28 00:07:19 +02002590 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2591
Gilles Peskine8817f612018-12-18 00:18:46 +01002592 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002594 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2595 psa_set_key_algorithm( &attributes, alg );
2596 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002597
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002598 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002599 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002600
Gilles Peskine8817f612018-12-18 00:18:46 +01002601 PSA_ASSERT( psa_mac_verify_setup( &operation,
2602 handle, alg ) );
2603 PSA_ASSERT( psa_destroy_key( handle ) );
2604 PSA_ASSERT( psa_mac_update( &operation,
2605 input->x, input->len ) );
2606 PSA_ASSERT( psa_mac_verify_finish( &operation,
2607 expected_mac->x,
2608 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609
2610exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002611 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612 mbedtls_psa_crypto_free( );
2613}
2614/* END_CASE */
2615
2616/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002617void cipher_operation_init( )
2618{
Jaeden Ameroab439972019-02-15 14:12:05 +00002619 const uint8_t input[1] = { 0 };
2620 unsigned char output[1] = { 0 };
2621 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002622 /* Test each valid way of initializing the object, except for `= {0}`, as
2623 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2624 * though it's OK by the C standard. We could test for this, but we'd need
2625 * to supress the Clang warning for the test. */
2626 psa_cipher_operation_t func = psa_cipher_operation_init( );
2627 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2628 psa_cipher_operation_t zero;
2629
2630 memset( &zero, 0, sizeof( zero ) );
2631
Jaeden Ameroab439972019-02-15 14:12:05 +00002632 /* A freshly-initialized cipher operation should not be usable. */
2633 TEST_EQUAL( psa_cipher_update( &func,
2634 input, sizeof( input ),
2635 output, sizeof( output ),
2636 &output_length ),
2637 PSA_ERROR_BAD_STATE );
2638 TEST_EQUAL( psa_cipher_update( &init,
2639 input, sizeof( input ),
2640 output, sizeof( output ),
2641 &output_length ),
2642 PSA_ERROR_BAD_STATE );
2643 TEST_EQUAL( psa_cipher_update( &zero,
2644 input, sizeof( input ),
2645 output, sizeof( output ),
2646 &output_length ),
2647 PSA_ERROR_BAD_STATE );
2648
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002649 /* A default cipher operation should be abortable without error. */
2650 PSA_ASSERT( psa_cipher_abort( &func ) );
2651 PSA_ASSERT( psa_cipher_abort( &init ) );
2652 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002653}
2654/* END_CASE */
2655
2656/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002657void cipher_setup( int key_type_arg,
2658 data_t *key,
2659 int alg_arg,
2660 int expected_status_arg )
2661{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002662 psa_key_type_t key_type = key_type_arg;
2663 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002664 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002665 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002666 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002667#if defined(KNOWN_SUPPORTED_MAC_ALG)
2668 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2669#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002670
Gilles Peskine8817f612018-12-18 00:18:46 +01002671 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002672
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002673 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2674 &operation, &status ) )
2675 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002676 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002677
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002678 /* The operation object should be reusable. */
2679#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2680 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2681 smoke_test_key_data,
2682 sizeof( smoke_test_key_data ),
2683 KNOWN_SUPPORTED_CIPHER_ALG,
2684 &operation, &status ) )
2685 goto exit;
2686 TEST_EQUAL( status, PSA_SUCCESS );
2687#endif
2688
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002689exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002690 mbedtls_psa_crypto_free( );
2691}
2692/* END_CASE */
2693
2694/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002695void cipher_bad_order( )
2696{
2697 psa_key_handle_t handle = 0;
2698 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2699 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002701 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2702 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2703 const uint8_t key[] = {
2704 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2705 0xaa, 0xaa, 0xaa, 0xaa };
2706 const uint8_t text[] = {
2707 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2708 0xbb, 0xbb, 0xbb, 0xbb };
2709 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2710 size_t length = 0;
2711
2712 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002713 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2714 psa_set_key_algorithm( &attributes, alg );
2715 psa_set_key_type( &attributes, key_type );
2716 PSA_ASSERT( psa_import_key( &attributes, &handle,
Jaeden Ameroab439972019-02-15 14:12:05 +00002717 key, sizeof(key) ) );
2718
2719
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002720 /* Call encrypt setup twice in a row. */
2721 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2722 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2723 PSA_ERROR_BAD_STATE );
2724 PSA_ASSERT( psa_cipher_abort( &operation ) );
2725
2726 /* Call decrypt setup twice in a row. */
2727 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2728 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2729 PSA_ERROR_BAD_STATE );
2730 PSA_ASSERT( psa_cipher_abort( &operation ) );
2731
Jaeden Ameroab439972019-02-15 14:12:05 +00002732 /* Generate an IV without calling setup beforehand. */
2733 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2734 buffer, sizeof( buffer ),
2735 &length ),
2736 PSA_ERROR_BAD_STATE );
2737 PSA_ASSERT( psa_cipher_abort( &operation ) );
2738
2739 /* Generate an IV twice in a row. */
2740 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2741 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2742 buffer, sizeof( buffer ),
2743 &length ) );
2744 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2745 buffer, sizeof( buffer ),
2746 &length ),
2747 PSA_ERROR_BAD_STATE );
2748 PSA_ASSERT( psa_cipher_abort( &operation ) );
2749
2750 /* Generate an IV after it's already set. */
2751 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2752 PSA_ASSERT( psa_cipher_set_iv( &operation,
2753 iv, sizeof( iv ) ) );
2754 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2755 buffer, sizeof( buffer ),
2756 &length ),
2757 PSA_ERROR_BAD_STATE );
2758 PSA_ASSERT( psa_cipher_abort( &operation ) );
2759
2760 /* Set an IV without calling setup beforehand. */
2761 TEST_EQUAL( psa_cipher_set_iv( &operation,
2762 iv, sizeof( iv ) ),
2763 PSA_ERROR_BAD_STATE );
2764 PSA_ASSERT( psa_cipher_abort( &operation ) );
2765
2766 /* Set an IV after it's already set. */
2767 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2768 PSA_ASSERT( psa_cipher_set_iv( &operation,
2769 iv, sizeof( iv ) ) );
2770 TEST_EQUAL( psa_cipher_set_iv( &operation,
2771 iv, sizeof( iv ) ),
2772 PSA_ERROR_BAD_STATE );
2773 PSA_ASSERT( psa_cipher_abort( &operation ) );
2774
2775 /* Set an IV after it's already generated. */
2776 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2777 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2778 buffer, sizeof( buffer ),
2779 &length ) );
2780 TEST_EQUAL( psa_cipher_set_iv( &operation,
2781 iv, sizeof( iv ) ),
2782 PSA_ERROR_BAD_STATE );
2783 PSA_ASSERT( psa_cipher_abort( &operation ) );
2784
2785 /* Call update without calling setup beforehand. */
2786 TEST_EQUAL( psa_cipher_update( &operation,
2787 text, sizeof( text ),
2788 buffer, sizeof( buffer ),
2789 &length ),
2790 PSA_ERROR_BAD_STATE );
2791 PSA_ASSERT( psa_cipher_abort( &operation ) );
2792
2793 /* Call update without an IV where an IV is required. */
2794 TEST_EQUAL( psa_cipher_update( &operation,
2795 text, sizeof( text ),
2796 buffer, sizeof( buffer ),
2797 &length ),
2798 PSA_ERROR_BAD_STATE );
2799 PSA_ASSERT( psa_cipher_abort( &operation ) );
2800
2801 /* Call update after finish. */
2802 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2803 PSA_ASSERT( psa_cipher_set_iv( &operation,
2804 iv, sizeof( iv ) ) );
2805 PSA_ASSERT( psa_cipher_finish( &operation,
2806 buffer, sizeof( buffer ), &length ) );
2807 TEST_EQUAL( psa_cipher_update( &operation,
2808 text, sizeof( text ),
2809 buffer, sizeof( buffer ),
2810 &length ),
2811 PSA_ERROR_BAD_STATE );
2812 PSA_ASSERT( psa_cipher_abort( &operation ) );
2813
2814 /* Call finish without calling setup beforehand. */
2815 TEST_EQUAL( psa_cipher_finish( &operation,
2816 buffer, sizeof( buffer ), &length ),
2817 PSA_ERROR_BAD_STATE );
2818 PSA_ASSERT( psa_cipher_abort( &operation ) );
2819
2820 /* Call finish without an IV where an IV is required. */
2821 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2822 /* Not calling update means we are encrypting an empty buffer, which is OK
2823 * for cipher modes with padding. */
2824 TEST_EQUAL( psa_cipher_finish( &operation,
2825 buffer, sizeof( buffer ), &length ),
2826 PSA_ERROR_BAD_STATE );
2827 PSA_ASSERT( psa_cipher_abort( &operation ) );
2828
2829 /* Call finish twice in a row. */
2830 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2831 PSA_ASSERT( psa_cipher_set_iv( &operation,
2832 iv, sizeof( iv ) ) );
2833 PSA_ASSERT( psa_cipher_finish( &operation,
2834 buffer, sizeof( buffer ), &length ) );
2835 TEST_EQUAL( psa_cipher_finish( &operation,
2836 buffer, sizeof( buffer ), &length ),
2837 PSA_ERROR_BAD_STATE );
2838 PSA_ASSERT( psa_cipher_abort( &operation ) );
2839
2840exit:
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002841 mbedtls_psa_crypto_free( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002842}
2843/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
Gilles Peskine50e586b2018-06-08 14:28:46 +02002845/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002846void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002847 data_t *key,
2848 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002849 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002850{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002851 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002852 psa_status_t status;
2853 psa_key_type_t key_type = key_type_arg;
2854 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002855 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002856 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002857 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002858 unsigned char *output = NULL;
2859 size_t output_buffer_size = 0;
2860 size_t function_output_length = 0;
2861 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002862 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002863 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002864
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002865 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2866 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002867
Gilles Peskine8817f612018-12-18 00:18:46 +01002868 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002869
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002870 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2871 psa_set_key_algorithm( &attributes, alg );
2872 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002873
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002874 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002875 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002876
Gilles Peskine8817f612018-12-18 00:18:46 +01002877 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2878 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002879
Gilles Peskine8817f612018-12-18 00:18:46 +01002880 PSA_ASSERT( psa_cipher_set_iv( &operation,
2881 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002882 output_buffer_size = ( (size_t) input->len +
2883 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002884 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002885
Gilles Peskine8817f612018-12-18 00:18:46 +01002886 PSA_ASSERT( psa_cipher_update( &operation,
2887 input->x, input->len,
2888 output, output_buffer_size,
2889 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002890 total_output_length += function_output_length;
2891 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002892 output + total_output_length,
2893 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002894 &function_output_length );
2895 total_output_length += function_output_length;
2896
Gilles Peskinefe11b722018-12-18 00:24:04 +01002897 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898 if( expected_status == PSA_SUCCESS )
2899 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002900 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002901 ASSERT_COMPARE( expected_output->x, expected_output->len,
2902 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903 }
2904
2905exit:
2906 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002907 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908 mbedtls_psa_crypto_free( );
2909}
2910/* END_CASE */
2911
2912/* BEGIN_CASE */
2913void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
2914 data_t *key,
2915 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002916 int first_part_size_arg,
2917 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002918 data_t *expected_output )
2919{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002920 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921 psa_key_type_t key_type = key_type_arg;
2922 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002923 size_t first_part_size = first_part_size_arg;
2924 size_t output1_length = output1_length_arg;
2925 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002927 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002928 unsigned char *output = NULL;
2929 size_t output_buffer_size = 0;
2930 size_t function_output_length = 0;
2931 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002932 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002935 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2936 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002937
Gilles Peskine8817f612018-12-18 00:18:46 +01002938 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002939
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002940 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2941 psa_set_key_algorithm( &attributes, alg );
2942 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002943
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002944 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002945 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002946
Gilles Peskine8817f612018-12-18 00:18:46 +01002947 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2948 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002949
Gilles Peskine8817f612018-12-18 00:18:46 +01002950 PSA_ASSERT( psa_cipher_set_iv( &operation,
2951 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002952 output_buffer_size = ( (size_t) input->len +
2953 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002954 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002955
Gilles Peskinee0866522019-02-19 19:44:00 +01002956 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002957 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2958 output, output_buffer_size,
2959 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002960 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002961 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002962 PSA_ASSERT( psa_cipher_update( &operation,
2963 input->x + first_part_size,
2964 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002965 output + total_output_length,
2966 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002967 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002968 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002969 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002970 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002971 output + total_output_length,
2972 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002974 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002975 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002977 ASSERT_COMPARE( expected_output->x, expected_output->len,
2978 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002979
2980exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002981 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002982 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002983 mbedtls_psa_crypto_free( );
2984}
2985/* END_CASE */
2986
2987/* BEGIN_CASE */
2988void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002989 data_t *key,
2990 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002991 int first_part_size_arg,
2992 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002993 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002994{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002995 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002996
2997 psa_key_type_t key_type = key_type_arg;
2998 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002999 size_t first_part_size = first_part_size_arg;
3000 size_t output1_length = output1_length_arg;
3001 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003002 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003003 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003004 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003005 size_t output_buffer_size = 0;
3006 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003007 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003008 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003009 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003010
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003011 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3012 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003013
Gilles Peskine8817f612018-12-18 00:18:46 +01003014 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003015
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003016 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3017 psa_set_key_algorithm( &attributes, alg );
3018 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003019
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003020 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003021 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003022
Gilles Peskine8817f612018-12-18 00:18:46 +01003023 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3024 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003025
Gilles Peskine8817f612018-12-18 00:18:46 +01003026 PSA_ASSERT( psa_cipher_set_iv( &operation,
3027 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003028
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003029 output_buffer_size = ( (size_t) input->len +
3030 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003031 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003032
Gilles Peskinee0866522019-02-19 19:44:00 +01003033 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003034 PSA_ASSERT( psa_cipher_update( &operation,
3035 input->x, first_part_size,
3036 output, output_buffer_size,
3037 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003038 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003039 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003040 PSA_ASSERT( psa_cipher_update( &operation,
3041 input->x + first_part_size,
3042 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003043 output + total_output_length,
3044 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003045 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003046 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003047 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003048 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003049 output + total_output_length,
3050 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003051 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003052 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003053 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003054
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003055 ASSERT_COMPARE( expected_output->x, expected_output->len,
3056 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003057
3058exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003059 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003060 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003061 mbedtls_psa_crypto_free( );
3062}
3063/* END_CASE */
3064
Gilles Peskine50e586b2018-06-08 14:28:46 +02003065/* BEGIN_CASE */
3066void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003067 data_t *key,
3068 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003069 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003070{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003071 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003072 psa_status_t status;
3073 psa_key_type_t key_type = key_type_arg;
3074 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003075 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003076 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003077 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003078 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003079 size_t output_buffer_size = 0;
3080 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003081 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003082 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003084
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003085 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3086 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003087
Gilles Peskine8817f612018-12-18 00:18:46 +01003088 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003089
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003090 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3091 psa_set_key_algorithm( &attributes, alg );
3092 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003093
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003094 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003095 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003096
Gilles Peskine8817f612018-12-18 00:18:46 +01003097 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3098 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003099
Gilles Peskine8817f612018-12-18 00:18:46 +01003100 PSA_ASSERT( psa_cipher_set_iv( &operation,
3101 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003102
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003103 output_buffer_size = ( (size_t) input->len +
3104 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003105 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003106
Gilles Peskine8817f612018-12-18 00:18:46 +01003107 PSA_ASSERT( psa_cipher_update( &operation,
3108 input->x, input->len,
3109 output, output_buffer_size,
3110 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003111 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003112 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003113 output + total_output_length,
3114 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003115 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003116 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003117 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003118
3119 if( expected_status == PSA_SUCCESS )
3120 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003121 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003122 ASSERT_COMPARE( expected_output->x, expected_output->len,
3123 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003124 }
3125
Gilles Peskine50e586b2018-06-08 14:28:46 +02003126exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003127 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003128 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003129 mbedtls_psa_crypto_free( );
3130}
3131/* END_CASE */
3132
Gilles Peskine50e586b2018-06-08 14:28:46 +02003133/* BEGIN_CASE */
3134void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003135 data_t *key,
3136 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003138 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003139 psa_key_type_t key_type = key_type_arg;
3140 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003141 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003142 size_t iv_size = 16;
3143 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003144 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003145 size_t output1_size = 0;
3146 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003147 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003148 size_t output2_size = 0;
3149 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003150 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003151 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3152 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003156
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3158 psa_set_key_algorithm( &attributes, alg );
3159 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003160
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003161 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003162 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003163
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3165 handle, alg ) );
3166 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3167 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003168
Gilles Peskine8817f612018-12-18 00:18:46 +01003169 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3170 iv, iv_size,
3171 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003172 output1_size = ( (size_t) input->len +
3173 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003174 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003175
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3177 output1, output1_size,
3178 &output1_length ) );
3179 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003180 output1 + output1_length,
3181 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003183
Gilles Peskine048b7f02018-06-08 14:20:49 +02003184 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003185
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003187
3188 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003189 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003190
Gilles Peskine8817f612018-12-18 00:18:46 +01003191 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3192 iv, iv_length ) );
3193 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3194 output2, output2_size,
3195 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003196 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_cipher_finish( &operation2,
3198 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003199 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003200 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003201
Gilles Peskine048b7f02018-06-08 14:20:49 +02003202 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003203
Gilles Peskine8817f612018-12-18 00:18:46 +01003204 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003205
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003206 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003207
3208exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003209 mbedtls_free( output1 );
3210 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003211 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003212 mbedtls_psa_crypto_free( );
3213}
3214/* END_CASE */
3215
3216/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003217void cipher_verify_output_multipart( int alg_arg,
3218 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003219 data_t *key,
3220 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003221 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003222{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003223 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003224 psa_key_type_t key_type = key_type_arg;
3225 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003226 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003227 unsigned char iv[16] = {0};
3228 size_t iv_size = 16;
3229 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003230 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003231 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003232 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003233 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003234 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003235 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003236 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003237 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3238 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003240
Gilles Peskine8817f612018-12-18 00:18:46 +01003241 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003242
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003243 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3244 psa_set_key_algorithm( &attributes, alg );
3245 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003246
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003247 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003248 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3251 handle, alg ) );
3252 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3253 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003254
Gilles Peskine8817f612018-12-18 00:18:46 +01003255 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3256 iv, iv_size,
3257 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003258 output1_buffer_size = ( (size_t) input->len +
3259 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003260 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003261
Gilles Peskinee0866522019-02-19 19:44:00 +01003262 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003263
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3265 output1, output1_buffer_size,
3266 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003267 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003268
Gilles Peskine8817f612018-12-18 00:18:46 +01003269 PSA_ASSERT( psa_cipher_update( &operation1,
3270 input->x + first_part_size,
3271 input->len - first_part_size,
3272 output1, output1_buffer_size,
3273 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003274 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003275
Gilles Peskine8817f612018-12-18 00:18:46 +01003276 PSA_ASSERT( psa_cipher_finish( &operation1,
3277 output1 + output1_length,
3278 output1_buffer_size - output1_length,
3279 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003280 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003281
Gilles Peskine8817f612018-12-18 00:18:46 +01003282 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003283
Gilles Peskine048b7f02018-06-08 14:20:49 +02003284 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003285 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003286
Gilles Peskine8817f612018-12-18 00:18:46 +01003287 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3288 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003289
Gilles Peskine8817f612018-12-18 00:18:46 +01003290 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3291 output2, output2_buffer_size,
3292 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003293 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003294
Gilles Peskine8817f612018-12-18 00:18:46 +01003295 PSA_ASSERT( psa_cipher_update( &operation2,
3296 output1 + first_part_size,
3297 output1_length - first_part_size,
3298 output2, output2_buffer_size,
3299 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003300 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003301
Gilles Peskine8817f612018-12-18 00:18:46 +01003302 PSA_ASSERT( psa_cipher_finish( &operation2,
3303 output2 + output2_length,
3304 output2_buffer_size - output2_length,
3305 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003306 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003307
Gilles Peskine8817f612018-12-18 00:18:46 +01003308 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003309
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003310 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003311
3312exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003313 mbedtls_free( output1 );
3314 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003315 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003316 mbedtls_psa_crypto_free( );
3317}
3318/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003319
Gilles Peskine20035e32018-02-03 22:44:14 +01003320/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003321void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003322 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003323 data_t *nonce,
3324 data_t *additional_data,
3325 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003326 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003327{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003328 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329 psa_key_type_t key_type = key_type_arg;
3330 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003331 unsigned char *output_data = NULL;
3332 size_t output_size = 0;
3333 size_t output_length = 0;
3334 unsigned char *output_data2 = NULL;
3335 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003337 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339
Gilles Peskine4abf7412018-06-18 16:35:34 +02003340 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003341 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342
Gilles Peskine8817f612018-12-18 00:18:46 +01003343 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003345 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3346 psa_set_key_algorithm( &attributes, alg );
3347 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003348
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003349 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003350 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351
Gilles Peskinefe11b722018-12-18 00:24:04 +01003352 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3353 nonce->x, nonce->len,
3354 additional_data->x,
3355 additional_data->len,
3356 input_data->x, input_data->len,
3357 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003358 &output_length ),
3359 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003360
3361 if( PSA_SUCCESS == expected_result )
3362 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003363 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Gilles Peskinefe11b722018-12-18 00:24:04 +01003365 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3366 nonce->x, nonce->len,
3367 additional_data->x,
3368 additional_data->len,
3369 output_data, output_length,
3370 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003371 &output_length2 ),
3372 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003373
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003374 ASSERT_COMPARE( input_data->x, input_data->len,
3375 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003376 }
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 );
3381 mbedtls_free( output_data2 );
3382 mbedtls_psa_crypto_free( );
3383}
3384/* END_CASE */
3385
3386/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003387void aead_encrypt( int key_type_arg, data_t *key_data,
3388 int alg_arg,
3389 data_t *nonce,
3390 data_t *additional_data,
3391 data_t *input_data,
3392 data_t *expected_result )
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 Peskinea1cac842018-06-11 19:33:02 +02003400 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402
Gilles Peskine4abf7412018-06-18 16:35:34 +02003403 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003404 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003405
Gilles Peskine8817f612018-12-18 00:18:46 +01003406 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003407
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003408 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3409 psa_set_key_algorithm( &attributes, alg );
3410 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003411
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003412 PSA_ASSERT( psa_import_key( &attributes, &handle,
3413 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414
Gilles Peskine8817f612018-12-18 00:18:46 +01003415 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3416 nonce->x, nonce->len,
3417 additional_data->x, additional_data->len,
3418 input_data->x, input_data->len,
3419 output_data, output_size,
3420 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003421
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003422 ASSERT_COMPARE( expected_result->x, expected_result->len,
3423 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003424
Gilles Peskinea1cac842018-06-11 19:33:02 +02003425exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003426 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003427 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003428 mbedtls_psa_crypto_free( );
3429}
3430/* END_CASE */
3431
3432/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003433void aead_decrypt( int key_type_arg, data_t *key_data,
3434 int alg_arg,
3435 data_t *nonce,
3436 data_t *additional_data,
3437 data_t *input_data,
3438 data_t *expected_data,
3439 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003440{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003441 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442 psa_key_type_t key_type = key_type_arg;
3443 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444 unsigned char *output_data = NULL;
3445 size_t output_size = 0;
3446 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003447 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003449 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003450
Gilles Peskine4abf7412018-06-18 16:35:34 +02003451 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003452 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003453
Gilles Peskine8817f612018-12-18 00:18:46 +01003454 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003455
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003456 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3457 psa_set_key_algorithm( &attributes, alg );
3458 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003460 PSA_ASSERT( psa_import_key( &attributes, &handle,
3461 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003462
Gilles Peskinefe11b722018-12-18 00:24:04 +01003463 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3464 nonce->x, nonce->len,
3465 additional_data->x,
3466 additional_data->len,
3467 input_data->x, input_data->len,
3468 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003469 &output_length ),
3470 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003471
Gilles Peskine2d277862018-06-18 15:41:12 +02003472 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003473 ASSERT_COMPARE( expected_data->x, expected_data->len,
3474 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475
Gilles Peskinea1cac842018-06-11 19:33:02 +02003476exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003477 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479 mbedtls_psa_crypto_free( );
3480}
3481/* END_CASE */
3482
3483/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003484void signature_size( int type_arg,
3485 int bits,
3486 int alg_arg,
3487 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003488{
3489 psa_key_type_t type = type_arg;
3490 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003491 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003492 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003493exit:
3494 ;
3495}
3496/* END_CASE */
3497
3498/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003499void sign_deterministic( int key_type_arg, data_t *key_data,
3500 int alg_arg, data_t *input_data,
3501 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003502{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003503 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003504 psa_key_type_t key_type = key_type_arg;
3505 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003506 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003507 unsigned char *signature = NULL;
3508 size_t signature_size;
3509 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003511
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003513
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003514 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3515 psa_set_key_algorithm( &attributes, alg );
3516 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003517
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003518 PSA_ASSERT( psa_import_key( &attributes, &handle,
3519 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003520 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3521 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003522
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003523 /* Allocate a buffer which has the size advertized by the
3524 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003525 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3526 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003527 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003528 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003529 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003530
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003531 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003532 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3533 input_data->x, input_data->len,
3534 signature, signature_size,
3535 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003536 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003537 ASSERT_COMPARE( output_data->x, output_data->len,
3538 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003539
3540exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003541 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003542 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003543 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003544 mbedtls_psa_crypto_free( );
3545}
3546/* END_CASE */
3547
3548/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003549void sign_fail( int key_type_arg, data_t *key_data,
3550 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003551 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003552{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003553 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003554 psa_key_type_t key_type = key_type_arg;
3555 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003556 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003557 psa_status_t actual_status;
3558 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003559 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003560 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003561 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003562
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003563 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003564
Gilles Peskine8817f612018-12-18 00:18:46 +01003565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003566
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003567 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3568 psa_set_key_algorithm( &attributes, alg );
3569 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003570
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003571 PSA_ASSERT( psa_import_key( &attributes, &handle,
3572 key_data->x, key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003573
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003574 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003575 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003576 signature, signature_size,
3577 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003578 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003579 /* The value of *signature_length is unspecified on error, but
3580 * whatever it is, it should be less than signature_size, so that
3581 * if the caller tries to read *signature_length bytes without
3582 * checking the error code then they don't overflow a buffer. */
3583 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003584
3585exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003586 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003587 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003588 mbedtls_free( signature );
3589 mbedtls_psa_crypto_free( );
3590}
3591/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003592
3593/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003594void sign_verify( int key_type_arg, data_t *key_data,
3595 int alg_arg, data_t *input_data )
3596{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003597 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003598 psa_key_type_t key_type = key_type_arg;
3599 psa_algorithm_t alg = alg_arg;
3600 size_t key_bits;
3601 unsigned char *signature = NULL;
3602 size_t signature_size;
3603 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003605
Gilles Peskine8817f612018-12-18 00:18:46 +01003606 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003607
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003608 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3609 psa_set_key_algorithm( &attributes, alg );
3610 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003611
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003612 PSA_ASSERT( psa_import_key( &attributes, &handle,
3613 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003614 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3615 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003616
3617 /* Allocate a buffer which has the size advertized by the
3618 * library. */
3619 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3620 key_bits, alg );
3621 TEST_ASSERT( signature_size != 0 );
3622 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003623 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003624
3625 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003626 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3627 input_data->x, input_data->len,
3628 signature, signature_size,
3629 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003630 /* Check that the signature length looks sensible. */
3631 TEST_ASSERT( signature_length <= signature_size );
3632 TEST_ASSERT( signature_length > 0 );
3633
3634 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003635 PSA_ASSERT( psa_asymmetric_verify(
3636 handle, alg,
3637 input_data->x, input_data->len,
3638 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003639
3640 if( input_data->len != 0 )
3641 {
3642 /* Flip a bit in the input and verify that the signature is now
3643 * detected as invalid. Flip a bit at the beginning, not at the end,
3644 * because ECDSA may ignore the last few bits of the input. */
3645 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003646 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3647 input_data->x, input_data->len,
3648 signature, signature_length ),
3649 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003650 }
3651
3652exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003653 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003654 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003655 mbedtls_free( signature );
3656 mbedtls_psa_crypto_free( );
3657}
3658/* END_CASE */
3659
3660/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003661void asymmetric_verify( int key_type_arg, data_t *key_data,
3662 int alg_arg, data_t *hash_data,
3663 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003664{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003665 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003666 psa_key_type_t key_type = key_type_arg;
3667 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003669
Gilles Peskine69c12672018-06-28 00:07:19 +02003670 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3671
Gilles Peskine8817f612018-12-18 00:18:46 +01003672 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003673
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003674 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3675 psa_set_key_algorithm( &attributes, alg );
3676 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003677
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003678 PSA_ASSERT( psa_import_key( &attributes, &handle,
3679 key_data->x, key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003680
Gilles Peskine8817f612018-12-18 00:18:46 +01003681 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3682 hash_data->x, hash_data->len,
3683 signature_data->x,
3684 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003685exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003686 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003687 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003688 mbedtls_psa_crypto_free( );
3689}
3690/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003691
3692/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003693void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3694 int alg_arg, data_t *hash_data,
3695 data_t *signature_data,
3696 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003697{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003698 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003699 psa_key_type_t key_type = key_type_arg;
3700 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003701 psa_status_t actual_status;
3702 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003704
Gilles Peskine8817f612018-12-18 00:18:46 +01003705 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003706
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003707 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3708 psa_set_key_algorithm( &attributes, alg );
3709 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003710
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003711 PSA_ASSERT( psa_import_key( &attributes, &handle,
3712 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003713
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003714 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003715 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003716 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003717 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003718
Gilles Peskinefe11b722018-12-18 00:24:04 +01003719 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003720
3721exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003722 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003723 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003724 mbedtls_psa_crypto_free( );
3725}
3726/* END_CASE */
3727
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003728/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003729void asymmetric_encrypt( int key_type_arg,
3730 data_t *key_data,
3731 int alg_arg,
3732 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003733 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003734 int expected_output_length_arg,
3735 int expected_status_arg )
3736{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003737 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003738 psa_key_type_t key_type = key_type_arg;
3739 psa_algorithm_t alg = alg_arg;
3740 size_t expected_output_length = expected_output_length_arg;
3741 size_t key_bits;
3742 unsigned char *output = NULL;
3743 size_t output_size;
3744 size_t output_length = ~0;
3745 psa_status_t actual_status;
3746 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003748
Gilles Peskine8817f612018-12-18 00:18:46 +01003749 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003750
Gilles Peskine656896e2018-06-29 19:12:28 +02003751 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003752 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3753 psa_set_key_algorithm( &attributes, alg );
3754 psa_set_key_type( &attributes, key_type );
3755 PSA_ASSERT( psa_import_key( &attributes, &handle,
3756 key_data->x, key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003757
3758 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003759 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3760 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003761 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003762 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003763
3764 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003765 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003766 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003767 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003768 output, output_size,
3769 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003770 TEST_EQUAL( actual_status, expected_status );
3771 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003772
Gilles Peskine68428122018-06-30 18:42:41 +02003773 /* If the label is empty, the test framework puts a non-null pointer
3774 * in label->x. Test that a null pointer works as well. */
3775 if( label->len == 0 )
3776 {
3777 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003778 if( output_size != 0 )
3779 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003780 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003781 input_data->x, input_data->len,
3782 NULL, label->len,
3783 output, output_size,
3784 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003785 TEST_EQUAL( actual_status, expected_status );
3786 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003787 }
3788
Gilles Peskine656896e2018-06-29 19:12:28 +02003789exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003790 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003791 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003792 mbedtls_free( output );
3793 mbedtls_psa_crypto_free( );
3794}
3795/* END_CASE */
3796
3797/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003798void asymmetric_encrypt_decrypt( int key_type_arg,
3799 data_t *key_data,
3800 int alg_arg,
3801 data_t *input_data,
3802 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003803{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003804 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003805 psa_key_type_t key_type = key_type_arg;
3806 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003807 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003808 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003809 size_t output_size;
3810 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003811 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003812 size_t output2_size;
3813 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003815
Gilles Peskine8817f612018-12-18 00:18:46 +01003816 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003817
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003818 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3819 psa_set_key_algorithm( &attributes, alg );
3820 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003821
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003822 PSA_ASSERT( psa_import_key( &attributes, &handle,
3823 key_data->x, key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003824
3825 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003826 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3827 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003828 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003829 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003830 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003831 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003832
Gilles Peskineeebd7382018-06-08 18:11:54 +02003833 /* We test encryption by checking that encrypt-then-decrypt gives back
3834 * the original plaintext because of the non-optional random
3835 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003836 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3837 input_data->x, input_data->len,
3838 label->x, label->len,
3839 output, output_size,
3840 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003841 /* We don't know what ciphertext length to expect, but check that
3842 * it looks sensible. */
3843 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003844
Gilles Peskine8817f612018-12-18 00:18:46 +01003845 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3846 output, output_length,
3847 label->x, label->len,
3848 output2, output2_size,
3849 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003850 ASSERT_COMPARE( input_data->x, input_data->len,
3851 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003852
3853exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003854 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003855 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003856 mbedtls_free( output );
3857 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003858 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003859}
3860/* END_CASE */
3861
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003862/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003863void asymmetric_decrypt( int key_type_arg,
3864 data_t *key_data,
3865 int alg_arg,
3866 data_t *input_data,
3867 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003868 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003869{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003870 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871 psa_key_type_t key_type = key_type_arg;
3872 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003873 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003874 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003875 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003877
Jaeden Amero412654a2019-02-06 12:57:46 +00003878 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003879 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003880
Gilles Peskine8817f612018-12-18 00:18:46 +01003881 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003882
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3884 psa_set_key_algorithm( &attributes, alg );
3885 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003886
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003887 PSA_ASSERT( psa_import_key( &attributes, &handle,
3888 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003889
Gilles Peskine8817f612018-12-18 00:18:46 +01003890 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3891 input_data->x, input_data->len,
3892 label->x, label->len,
3893 output,
3894 output_size,
3895 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003896 ASSERT_COMPARE( expected_data->x, expected_data->len,
3897 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003898
Gilles Peskine68428122018-06-30 18:42:41 +02003899 /* If the label is empty, the test framework puts a non-null pointer
3900 * in label->x. Test that a null pointer works as well. */
3901 if( label->len == 0 )
3902 {
3903 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003904 if( output_size != 0 )
3905 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003906 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3907 input_data->x, input_data->len,
3908 NULL, label->len,
3909 output,
3910 output_size,
3911 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003912 ASSERT_COMPARE( expected_data->x, expected_data->len,
3913 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003914 }
3915
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003916exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003917 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003918 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003919 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003920 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003921}
3922/* END_CASE */
3923
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003924/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003925void asymmetric_decrypt_fail( int key_type_arg,
3926 data_t *key_data,
3927 int alg_arg,
3928 data_t *input_data,
3929 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003930 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003931 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003933 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003934 psa_key_type_t key_type = key_type_arg;
3935 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003936 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003937 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003938 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003939 psa_status_t actual_status;
3940 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003942
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003943 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003944
Gilles Peskine8817f612018-12-18 00:18:46 +01003945 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003947 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3948 psa_set_key_algorithm( &attributes, alg );
3949 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003950
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003951 PSA_ASSERT( psa_import_key( &attributes, &handle,
3952 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003953
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003954 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003955 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003956 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003957 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003958 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003959 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003960 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003961
Gilles Peskine68428122018-06-30 18:42:41 +02003962 /* If the label is empty, the test framework puts a non-null pointer
3963 * in label->x. Test that a null pointer works as well. */
3964 if( label->len == 0 )
3965 {
3966 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003967 if( output_size != 0 )
3968 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003969 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003970 input_data->x, input_data->len,
3971 NULL, label->len,
3972 output, output_size,
3973 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003974 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003975 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003976 }
3977
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003978exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003979 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003980 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003981 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003982 mbedtls_psa_crypto_free( );
3983}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003984/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003985
3986/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003987void crypto_generator_init( )
3988{
3989 /* Test each valid way of initializing the object, except for `= {0}`, as
3990 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3991 * though it's OK by the C standard. We could test for this, but we'd need
3992 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003993 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003994 psa_crypto_generator_t func = psa_crypto_generator_init( );
3995 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3996 psa_crypto_generator_t zero;
3997
3998 memset( &zero, 0, sizeof( zero ) );
3999
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004000 /* A default generator should not be able to report its capacity. */
4001 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
4002 PSA_ERROR_BAD_STATE );
4003 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
4004 PSA_ERROR_BAD_STATE );
4005 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
4006 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004007
4008 /* A default generator should be abortable without error. */
4009 PSA_ASSERT( psa_generator_abort(&func) );
4010 PSA_ASSERT( psa_generator_abort(&init) );
4011 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004012}
4013/* END_CASE */
4014
4015/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004016void derive_setup( int key_type_arg,
4017 data_t *key_data,
4018 int alg_arg,
4019 data_t *salt,
4020 data_t *label,
4021 int requested_capacity_arg,
4022 int expected_status_arg )
4023{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004024 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004025 size_t key_type = key_type_arg;
4026 psa_algorithm_t alg = alg_arg;
4027 size_t requested_capacity = requested_capacity_arg;
4028 psa_status_t expected_status = expected_status_arg;
4029 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004030 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004031
Gilles Peskine8817f612018-12-18 00:18:46 +01004032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004033
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004034 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4035 psa_set_key_algorithm( &attributes, alg );
4036 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004037
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004038 PSA_ASSERT( psa_import_key( &attributes, &handle,
4039 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004040
Gilles Peskinefe11b722018-12-18 00:24:04 +01004041 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4042 salt->x, salt->len,
4043 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004044 requested_capacity ),
4045 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004046
4047exit:
4048 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004049 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004050 mbedtls_psa_crypto_free( );
4051}
4052/* END_CASE */
4053
4054/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004055void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004056{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004057 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004058 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004059 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004060 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004061 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004062 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004063 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4064 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4065 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004066 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004067
Gilles Peskine8817f612018-12-18 00:18:46 +01004068 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004069
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004070 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4071 psa_set_key_algorithm( &attributes, alg );
4072 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004073
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004074 PSA_ASSERT( psa_import_key( &attributes, &handle,
4075 key_data, sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004076
4077 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004078 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4079 NULL, 0,
4080 NULL, 0,
4081 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004082
4083 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004084 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4085 NULL, 0,
4086 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004087 capacity ),
4088 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004089
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004090 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004091
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004092 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004093 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004094
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004095exit:
4096 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004097 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004098 mbedtls_psa_crypto_free( );
4099}
4100/* END_CASE */
4101
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004102/* BEGIN_CASE */
4103void test_derive_invalid_generator_tests( )
4104{
4105 uint8_t output_buffer[16];
4106 size_t buffer_size = 16;
4107 size_t capacity = 0;
4108 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4109
Nir Sonnenschein50789302018-10-31 12:16:38 +02004110 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004111 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004112
4113 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004114 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004115
Gilles Peskine8817f612018-12-18 00:18:46 +01004116 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004117
Nir Sonnenschein50789302018-10-31 12:16:38 +02004118 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004119 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004120
Nir Sonnenschein50789302018-10-31 12:16:38 +02004121 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004122 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004123
4124exit:
4125 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004126}
4127/* END_CASE */
4128
4129/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004130void derive_output( int alg_arg,
4131 data_t *key_data,
4132 data_t *salt,
4133 data_t *label,
4134 int requested_capacity_arg,
4135 data_t *expected_output1,
4136 data_t *expected_output2 )
4137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004138 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004139 psa_algorithm_t alg = alg_arg;
4140 size_t requested_capacity = requested_capacity_arg;
4141 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4142 uint8_t *expected_outputs[2] =
4143 {expected_output1->x, expected_output2->x};
4144 size_t output_sizes[2] =
4145 {expected_output1->len, expected_output2->len};
4146 size_t output_buffer_size = 0;
4147 uint8_t *output_buffer = NULL;
4148 size_t expected_capacity;
4149 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004151 psa_status_t status;
4152 unsigned i;
4153
4154 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4155 {
4156 if( output_sizes[i] > output_buffer_size )
4157 output_buffer_size = output_sizes[i];
4158 if( output_sizes[i] == 0 )
4159 expected_outputs[i] = NULL;
4160 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004161 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004162 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004163
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004164 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4165 psa_set_key_algorithm( &attributes, alg );
4166 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004167
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004168 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004169 key_data->x, key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004170
4171 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004172 if( PSA_ALG_IS_HKDF( alg ) )
4173 {
4174 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4175 PSA_ASSERT( psa_set_generator_capacity( &generator,
4176 requested_capacity ) );
4177 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4178 PSA_KDF_STEP_SALT,
4179 salt->x, salt->len ) );
4180 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4181 PSA_KDF_STEP_SECRET,
4182 handle ) );
4183 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4184 PSA_KDF_STEP_INFO,
4185 label->x, label->len ) );
4186 }
4187 else
4188 {
4189 // legacy
4190 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4191 salt->x, salt->len,
4192 label->x, label->len,
4193 requested_capacity ) );
4194 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004195 PSA_ASSERT( psa_get_generator_capacity( &generator,
4196 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004197 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004198 expected_capacity = requested_capacity;
4199
4200 /* Expansion phase. */
4201 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4202 {
4203 /* Read some bytes. */
4204 status = psa_generator_read( &generator,
4205 output_buffer, output_sizes[i] );
4206 if( expected_capacity == 0 && output_sizes[i] == 0 )
4207 {
4208 /* Reading 0 bytes when 0 bytes are available can go either way. */
4209 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004210 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004211 continue;
4212 }
4213 else if( expected_capacity == 0 ||
4214 output_sizes[i] > expected_capacity )
4215 {
4216 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004217 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004218 expected_capacity = 0;
4219 continue;
4220 }
4221 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004222 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004223 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004224 ASSERT_COMPARE( output_buffer, output_sizes[i],
4225 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004226 /* Check the generator status. */
4227 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004228 PSA_ASSERT( psa_get_generator_capacity( &generator,
4229 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004230 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004231 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004232 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004233
4234exit:
4235 mbedtls_free( output_buffer );
4236 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004237 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004238 mbedtls_psa_crypto_free( );
4239}
4240/* END_CASE */
4241
4242/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004243void derive_full( int alg_arg,
4244 data_t *key_data,
4245 data_t *salt,
4246 data_t *label,
4247 int requested_capacity_arg )
4248{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004249 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004250 psa_algorithm_t alg = alg_arg;
4251 size_t requested_capacity = requested_capacity_arg;
4252 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4253 unsigned char output_buffer[16];
4254 size_t expected_capacity = requested_capacity;
4255 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004257
Gilles Peskine8817f612018-12-18 00:18:46 +01004258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004259
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4261 psa_set_key_algorithm( &attributes, alg );
4262 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004263
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004264 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004265 key_data->x, key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004266
4267 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004268 if( PSA_ALG_IS_HKDF( alg ) )
4269 {
4270 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4271 PSA_ASSERT( psa_set_generator_capacity( &generator,
4272 requested_capacity ) );
4273 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4274 PSA_KDF_STEP_SALT,
4275 salt->x, salt->len ) );
4276 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4277 PSA_KDF_STEP_SECRET,
4278 handle ) );
4279 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4280 PSA_KDF_STEP_INFO,
4281 label->x, label->len ) );
4282 }
4283 else
4284 {
4285 // legacy
4286 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4287 salt->x, salt->len,
4288 label->x, label->len,
4289 requested_capacity ) );
4290 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004291 PSA_ASSERT( psa_get_generator_capacity( &generator,
4292 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004293 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004294
4295 /* Expansion phase. */
4296 while( current_capacity > 0 )
4297 {
4298 size_t read_size = sizeof( output_buffer );
4299 if( read_size > current_capacity )
4300 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004301 PSA_ASSERT( psa_generator_read( &generator,
4302 output_buffer,
4303 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004304 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004305 PSA_ASSERT( psa_get_generator_capacity( &generator,
4306 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004307 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004308 }
4309
4310 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004311 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004312 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004313
Gilles Peskine8817f612018-12-18 00:18:46 +01004314 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004315
4316exit:
4317 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004318 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004319 mbedtls_psa_crypto_free( );
4320}
4321/* END_CASE */
4322
4323/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004324void derive_key_exercise( int alg_arg,
4325 data_t *key_data,
4326 data_t *salt,
4327 data_t *label,
4328 int derived_type_arg,
4329 int derived_bits_arg,
4330 int derived_usage_arg,
4331 int derived_alg_arg )
4332{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004333 psa_key_handle_t base_handle = 0;
4334 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004335 psa_algorithm_t alg = alg_arg;
4336 psa_key_type_t derived_type = derived_type_arg;
4337 size_t derived_bits = derived_bits_arg;
4338 psa_key_usage_t derived_usage = derived_usage_arg;
4339 psa_algorithm_t derived_alg = derived_alg_arg;
4340 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4341 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004343 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004344
Gilles Peskine8817f612018-12-18 00:18:46 +01004345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004346
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4348 psa_set_key_algorithm( &attributes, alg );
4349 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
4350 PSA_ASSERT( psa_import_key( &attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004351 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004352
4353 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004354 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4355 salt->x, salt->len,
4356 label->x, label->len,
4357 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004358 psa_set_key_usage_flags( &attributes, derived_usage );
4359 psa_set_key_algorithm( &attributes, derived_alg );
4360 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004361 psa_set_key_bits( &attributes, derived_bits );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004362 PSA_ASSERT( psa_generate_derived_key( &attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004363 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004364
4365 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004366 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4367 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4368 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004369
4370 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004371 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004372 goto exit;
4373
4374exit:
4375 psa_generator_abort( &generator );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004376 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004377 psa_destroy_key( base_handle );
4378 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004379 mbedtls_psa_crypto_free( );
4380}
4381/* END_CASE */
4382
4383/* BEGIN_CASE */
4384void derive_key_export( int alg_arg,
4385 data_t *key_data,
4386 data_t *salt,
4387 data_t *label,
4388 int bytes1_arg,
4389 int bytes2_arg )
4390{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004391 psa_key_handle_t base_handle = 0;
4392 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004393 psa_algorithm_t alg = alg_arg;
4394 size_t bytes1 = bytes1_arg;
4395 size_t bytes2 = bytes2_arg;
4396 size_t capacity = bytes1 + bytes2;
4397 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004398 uint8_t *output_buffer = NULL;
4399 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004400 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4401 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004402 size_t length;
4403
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004404 ASSERT_ALLOC( output_buffer, capacity );
4405 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004406 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004407
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004408 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4409 psa_set_key_algorithm( &base_attributes, alg );
4410 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4411 PSA_ASSERT( psa_import_key( &base_attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004412 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004413
4414 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004415 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4416 salt->x, salt->len,
4417 label->x, label->len,
4418 capacity ) );
4419 PSA_ASSERT( psa_generator_read( &generator,
4420 output_buffer,
4421 capacity ) );
4422 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004423
4424 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004425 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4426 salt->x, salt->len,
4427 label->x, label->len,
4428 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004429 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4430 psa_set_key_algorithm( &derived_attributes, 0 );
4431 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004432 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004433 PSA_ASSERT( psa_generate_derived_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004434 &generator ) );
4435 PSA_ASSERT( psa_export_key( derived_handle,
4436 export_buffer, bytes1,
4437 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004438 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004439 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004440 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004441 PSA_ASSERT( psa_generate_derived_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004442 &generator ) );
4443 PSA_ASSERT( psa_export_key( derived_handle,
4444 export_buffer + bytes1, bytes2,
4445 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004446 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004447
4448 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004449 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4450 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004451
4452exit:
4453 mbedtls_free( output_buffer );
4454 mbedtls_free( export_buffer );
4455 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004456 psa_destroy_key( base_handle );
4457 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004458 mbedtls_psa_crypto_free( );
4459}
4460/* END_CASE */
4461
4462/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004463void key_agreement_setup( int alg_arg,
4464 int our_key_type_arg, data_t *our_key_data,
4465 data_t *peer_key_data,
4466 int expected_status_arg )
4467{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004468 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004469 psa_algorithm_t alg = alg_arg;
4470 psa_key_type_t our_key_type = our_key_type_arg;
4471 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004472 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004473 psa_status_t expected_status = expected_status_arg;
4474 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004475
Gilles Peskine8817f612018-12-18 00:18:46 +01004476 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004477
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004478 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4479 psa_set_key_algorithm( &attributes, alg );
4480 psa_set_key_type( &attributes, our_key_type );
4481 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004482 our_key_data->x, our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004483
Gilles Peskine77f40d82019-04-11 21:27:06 +02004484 /* The tests currently include inputs that should fail at either step.
4485 * Test cases that fail at the setup step should be changed to call
4486 * key_derivation_setup instead, and this function should be renamed
4487 * to key_agreement_fail. */
4488 status = psa_key_derivation_setup( &generator, alg );
4489 if( status == PSA_SUCCESS )
4490 {
4491 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
4492 our_key,
4493 peer_key_data->x, peer_key_data->len ),
4494 expected_status );
4495 }
4496 else
4497 {
4498 TEST_ASSERT( status == expected_status );
4499 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004500
4501exit:
4502 psa_generator_abort( &generator );
4503 psa_destroy_key( our_key );
4504 mbedtls_psa_crypto_free( );
4505}
4506/* END_CASE */
4507
4508/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004509void raw_key_agreement( int alg_arg,
4510 int our_key_type_arg, data_t *our_key_data,
4511 data_t *peer_key_data,
4512 data_t *expected_output )
4513{
4514 psa_key_handle_t our_key = 0;
4515 psa_algorithm_t alg = alg_arg;
4516 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004517 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004518 unsigned char *output = NULL;
4519 size_t output_length = ~0;
4520
4521 ASSERT_ALLOC( output, expected_output->len );
4522 PSA_ASSERT( psa_crypto_init( ) );
4523
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004524 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4525 psa_set_key_algorithm( &attributes, alg );
4526 psa_set_key_type( &attributes, our_key_type );
4527 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004528 our_key_data->x, our_key_data->len ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004529
4530 PSA_ASSERT( psa_key_agreement_raw_shared_secret(
4531 alg, our_key,
4532 peer_key_data->x, peer_key_data->len,
4533 output, expected_output->len, &output_length ) );
4534 ASSERT_COMPARE( output, output_length,
4535 expected_output->x, expected_output->len );
4536
4537exit:
4538 mbedtls_free( output );
4539 psa_destroy_key( our_key );
4540 mbedtls_psa_crypto_free( );
4541}
4542/* END_CASE */
4543
4544/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004545void key_agreement_capacity( int alg_arg,
4546 int our_key_type_arg, data_t *our_key_data,
4547 data_t *peer_key_data,
4548 int expected_capacity_arg )
4549{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004550 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004551 psa_algorithm_t alg = alg_arg;
4552 psa_key_type_t our_key_type = our_key_type_arg;
4553 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004555 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004556 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004557
Gilles Peskine8817f612018-12-18 00:18:46 +01004558 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004559
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004560 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4561 psa_set_key_algorithm( &attributes, alg );
4562 psa_set_key_type( &attributes, our_key_type );
4563 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004564 our_key_data->x, our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004565
Gilles Peskine969c5d62019-01-16 15:53:06 +01004566 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4567 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004568 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004569 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004570 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4571 {
4572 /* The test data is for info="" */
4573 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4574 PSA_KDF_STEP_INFO,
4575 NULL, 0 ) );
4576 }
Gilles Peskine59685592018-09-18 12:11:34 +02004577
Gilles Peskinebf491972018-10-25 22:36:12 +02004578 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004579 PSA_ASSERT( psa_get_generator_capacity(
4580 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004581 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004582
Gilles Peskinebf491972018-10-25 22:36:12 +02004583 /* Test the actual capacity by reading the output. */
4584 while( actual_capacity > sizeof( output ) )
4585 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004586 PSA_ASSERT( psa_generator_read( &generator,
4587 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004588 actual_capacity -= sizeof( output );
4589 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004590 PSA_ASSERT( psa_generator_read( &generator,
4591 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004592 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004593 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004594
Gilles Peskine59685592018-09-18 12:11:34 +02004595exit:
4596 psa_generator_abort( &generator );
4597 psa_destroy_key( our_key );
4598 mbedtls_psa_crypto_free( );
4599}
4600/* END_CASE */
4601
4602/* BEGIN_CASE */
4603void key_agreement_output( int alg_arg,
4604 int our_key_type_arg, data_t *our_key_data,
4605 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004606 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004607{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004608 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004609 psa_algorithm_t alg = alg_arg;
4610 psa_key_type_t our_key_type = our_key_type_arg;
4611 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004613 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004614
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004615 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4616 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004617
Gilles Peskine8817f612018-12-18 00:18:46 +01004618 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004619
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004620 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4621 psa_set_key_algorithm( &attributes, alg );
4622 psa_set_key_type( &attributes, our_key_type );
4623 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004624 our_key_data->x, our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004625
Gilles Peskine969c5d62019-01-16 15:53:06 +01004626 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4627 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004628 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004629 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004630 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4631 {
4632 /* The test data is for info="" */
4633 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4634 PSA_KDF_STEP_INFO,
4635 NULL, 0 ) );
4636 }
Gilles Peskine59685592018-09-18 12:11:34 +02004637
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004638 PSA_ASSERT( psa_generator_read( &generator,
4639 actual_output,
4640 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004641 ASSERT_COMPARE( actual_output, expected_output1->len,
4642 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004643 if( expected_output2->len != 0 )
4644 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004645 PSA_ASSERT( psa_generator_read( &generator,
4646 actual_output,
4647 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004648 ASSERT_COMPARE( actual_output, expected_output2->len,
4649 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004650 }
Gilles Peskine59685592018-09-18 12:11:34 +02004651
4652exit:
4653 psa_generator_abort( &generator );
4654 psa_destroy_key( our_key );
4655 mbedtls_psa_crypto_free( );
4656 mbedtls_free( actual_output );
4657}
4658/* END_CASE */
4659
4660/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004661void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004662{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004663 size_t bytes = bytes_arg;
4664 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004665 unsigned char *output = NULL;
4666 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004667 size_t i;
4668 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004669
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004670 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4671 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004672 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004673
Gilles Peskine8817f612018-12-18 00:18:46 +01004674 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004675
Gilles Peskinea50d7392018-06-21 10:22:13 +02004676 /* Run several times, to ensure that every output byte will be
4677 * nonzero at least once with overwhelming probability
4678 * (2^(-8*number_of_runs)). */
4679 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004680 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004681 if( bytes != 0 )
4682 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004683 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004684
4685 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004686 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4687 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004688
4689 for( i = 0; i < bytes; i++ )
4690 {
4691 if( output[i] != 0 )
4692 ++changed[i];
4693 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004694 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004695
4696 /* Check that every byte was changed to nonzero at least once. This
4697 * validates that psa_generate_random is overwriting every byte of
4698 * the output buffer. */
4699 for( i = 0; i < bytes; i++ )
4700 {
4701 TEST_ASSERT( changed[i] != 0 );
4702 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004703
4704exit:
4705 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004706 mbedtls_free( output );
4707 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004708}
4709/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004710
4711/* BEGIN_CASE */
4712void generate_key( int type_arg,
4713 int bits_arg,
4714 int usage_arg,
4715 int alg_arg,
4716 int expected_status_arg )
4717{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004718 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004719 psa_key_type_t type = type_arg;
4720 psa_key_usage_t usage = usage_arg;
4721 size_t bits = bits_arg;
4722 psa_algorithm_t alg = alg_arg;
4723 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004725 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004726
Gilles Peskine8817f612018-12-18 00:18:46 +01004727 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004728
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004729 psa_set_key_usage_flags( &attributes, usage );
4730 psa_set_key_algorithm( &attributes, alg );
4731 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004732 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004733
4734 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004735 TEST_EQUAL( psa_generate_random_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004736 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004737 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004738
4739 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004740 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4741 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4742 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004743
Gilles Peskine818ca122018-06-20 18:16:48 +02004744 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004745 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004746 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004747
4748exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004749 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004750 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004751 mbedtls_psa_crypto_free( );
4752}
4753/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004754
Gilles Peskinee56e8782019-04-26 17:34:02 +02004755/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4756void generate_key_rsa( int bits_arg,
4757 data_t *e_arg,
4758 int expected_status_arg )
4759{
4760 psa_key_handle_t handle = 0;
4761 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEYPAIR;
4762 size_t bits = bits_arg;
4763 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4764 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4765 psa_status_t expected_status = expected_status_arg;
4766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4767 uint8_t *exported = NULL;
4768 size_t exported_size =
4769 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4770 size_t exported_length = SIZE_MAX;
4771 uint8_t *e_read_buffer = NULL;
4772 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004773 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004774 size_t e_read_length = SIZE_MAX;
4775
4776 if( e_arg->len == 0 ||
4777 ( e_arg->len == 3 &&
4778 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4779 {
4780 is_default_public_exponent = 1;
4781 e_read_size = 0;
4782 }
4783 ASSERT_ALLOC( e_read_buffer, e_read_size );
4784 ASSERT_ALLOC( exported, exported_size );
4785
4786 PSA_ASSERT( psa_crypto_init( ) );
4787
4788 psa_set_key_usage_flags( &attributes, usage );
4789 psa_set_key_algorithm( &attributes, alg );
4790 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4791 e_arg->x, e_arg->len ) );
4792 psa_set_key_bits( &attributes, bits );
4793
4794 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004795 TEST_EQUAL( psa_generate_random_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004796 if( expected_status != PSA_SUCCESS )
4797 goto exit;
4798
4799 /* Test the key information */
4800 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4801 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4802 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4803 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4804 e_read_buffer, e_read_size,
4805 &e_read_length ) );
4806 if( is_default_public_exponent )
4807 TEST_EQUAL( e_read_length, 0 );
4808 else
4809 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4810
4811 /* Do something with the key according to its type and permitted usage. */
4812 if( ! exercise_key( handle, usage, alg ) )
4813 goto exit;
4814
4815 /* Export the key and check the public exponent. */
4816 PSA_ASSERT( psa_export_public_key( handle,
4817 exported, exported_size,
4818 &exported_length ) );
4819 {
4820 uint8_t *p = exported;
4821 uint8_t *end = exported + exported_length;
4822 size_t len;
4823 /* RSAPublicKey ::= SEQUENCE {
4824 * modulus INTEGER, -- n
4825 * publicExponent INTEGER } -- e
4826 */
4827 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4828 MBEDTLS_ASN1_SEQUENCE |
4829 MBEDTLS_ASN1_CONSTRUCTED ) );
4830 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
4831 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4832 MBEDTLS_ASN1_INTEGER ) );
4833 if( len >= 1 && p[0] == 0 )
4834 {
4835 ++p;
4836 --len;
4837 }
4838 if( e_arg->len == 0 )
4839 {
4840 TEST_EQUAL( len, 3 );
4841 TEST_EQUAL( p[0], 1 );
4842 TEST_EQUAL( p[1], 0 );
4843 TEST_EQUAL( p[2], 1 );
4844 }
4845 else
4846 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4847 }
4848
4849exit:
4850 psa_reset_key_attributes( &attributes );
4851 psa_destroy_key( handle );
4852 mbedtls_psa_crypto_free( );
4853 mbedtls_free( e_read_buffer );
4854 mbedtls_free( exported );
4855}
4856/* END_CASE */
4857
Darryl Greend49a4992018-06-18 17:27:26 +01004858/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004859void persistent_key_load_key_from_storage( data_t *data,
4860 int type_arg, int bits_arg,
4861 int usage_flags_arg, int alg_arg,
4862 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004863{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004864 psa_key_id_t key_id = 1;
4865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004866 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004867 psa_key_handle_t base_key = 0;
4868 psa_key_type_t type = type_arg;
4869 size_t bits = bits_arg;
4870 psa_key_usage_t usage_flags = usage_flags_arg;
4871 psa_algorithm_t alg = alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004872 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004873 unsigned char *first_export = NULL;
4874 unsigned char *second_export = NULL;
4875 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4876 size_t first_exported_length;
4877 size_t second_exported_length;
4878
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004879 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4880 {
4881 ASSERT_ALLOC( first_export, export_size );
4882 ASSERT_ALLOC( second_export, export_size );
4883 }
Darryl Greend49a4992018-06-18 17:27:26 +01004884
Gilles Peskine8817f612018-12-18 00:18:46 +01004885 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004886
Gilles Peskinec87af662019-05-15 16:12:22 +02004887 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004888 psa_set_key_usage_flags( &attributes, usage_flags );
4889 psa_set_key_algorithm( &attributes, alg );
4890 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004891 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004892
Darryl Green0c6575a2018-11-07 16:05:30 +00004893 switch( generation_method )
4894 {
4895 case IMPORT_KEY:
4896 /* Import the key */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004897 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004898 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004899 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004900
Darryl Green0c6575a2018-11-07 16:05:30 +00004901 case GENERATE_KEY:
4902 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004903 PSA_ASSERT( psa_generate_random_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004904 break;
4905
4906 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004907 {
4908 /* Create base key */
4909 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4910 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4911 psa_set_key_usage_flags( &base_attributes,
4912 PSA_KEY_USAGE_DERIVE );
4913 psa_set_key_algorithm( &base_attributes, derive_alg );
4914 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4915 PSA_ASSERT( psa_import_key( &base_attributes, &base_key,
4916 data->x, data->len ) );
4917 /* Derive a key. */
4918 PSA_ASSERT( psa_key_derivation_setup( &generator, derive_alg ) );
4919 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4920 PSA_KDF_STEP_SECRET,
4921 base_key ) );
4922 PSA_ASSERT( psa_key_derivation_input_bytes(
4923 &generator, PSA_KDF_STEP_INFO,
4924 NULL, 0 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004925 PSA_ASSERT( psa_generate_derived_key( &attributes, &handle,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004926 &generator ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004927 PSA_ASSERT( psa_generator_abort( &generator ) );
4928 PSA_ASSERT( psa_destroy_key( base_key ) );
4929 base_key = 0;
4930 }
Darryl Green0c6575a2018-11-07 16:05:30 +00004931 break;
4932 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004933 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004934
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004935 /* Export the key if permitted by the key policy. */
4936 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4937 {
4938 PSA_ASSERT( psa_export_key( handle,
4939 first_export, export_size,
4940 &first_exported_length ) );
4941 if( generation_method == IMPORT_KEY )
4942 ASSERT_COMPARE( data->x, data->len,
4943 first_export, first_exported_length );
4944 }
Darryl Greend49a4992018-06-18 17:27:26 +01004945
4946 /* Shutdown and restart */
4947 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004948 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004949
Darryl Greend49a4992018-06-18 17:27:26 +01004950 /* Check key slot still contains key data */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004951 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
Gilles Peskine8817f612018-12-18 00:18:46 +01004952 &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004953 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4954 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
4955 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4956 PSA_KEY_LIFETIME_PERSISTENT );
4957 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4958 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4959 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4960 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004961
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004962 /* Export the key again if permitted by the key policy. */
4963 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004964 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004965 PSA_ASSERT( psa_export_key( handle,
4966 second_export, export_size,
4967 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004968 ASSERT_COMPARE( first_export, first_exported_length,
4969 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004970 }
4971
4972 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004973 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004974 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004975
4976exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004977 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004978 mbedtls_free( first_export );
4979 mbedtls_free( second_export );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004980 psa_generator_abort( &generator );
4981 psa_destroy_key( base_key );
4982 if( handle == 0 )
4983 {
4984 /* In case there was a test failure after creating the persistent key
4985 * but while it was not open, try to re-open the persistent key
4986 * to delete it. */
4987 psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle );
4988 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004989 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004990 mbedtls_psa_crypto_free();
4991}
4992/* END_CASE */