blob: c48ee649f9ea0f1c32eb363545b9181f7bb7c1ba [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinef426e0f2019-02-25 17:42:03 +010017/* A hash algorithm that is known to be supported.
18 *
19 * This is used in some smoke tests.
20 */
21#if defined(MBEDTLS_MD2_C)
22#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
23#elif defined(MBEDTLS_MD4_C)
24#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
25#elif defined(MBEDTLS_MD5_C)
26#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
27/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
28 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
29 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
30 * implausible anyway. */
31#elif defined(MBEDTLS_SHA1_C)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
33#elif defined(MBEDTLS_SHA256_C)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
35#elif defined(MBEDTLS_SHA512_C)
36#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
37#elif defined(MBEDTLS_SHA3_C)
38#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
39#else
40#undef KNOWN_SUPPORTED_HASH_ALG
41#endif
42
43/* A block cipher that is known to be supported.
44 *
45 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
46 */
47#if defined(MBEDTLS_AES_C)
48#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
49#elif defined(MBEDTLS_ARIA_C)
50#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
51#elif defined(MBEDTLS_CAMELLIA_C)
52#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
53#undef KNOWN_SUPPORTED_BLOCK_CIPHER
54#endif
55
56/* A MAC mode that is known to be supported.
57 *
58 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
59 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
60 *
61 * This is used in some smoke tests.
62 */
63#if defined(KNOWN_SUPPORTED_HASH_ALG)
64#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
65#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
66#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
67#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
68#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
69#else
70#undef KNOWN_SUPPORTED_MAC_ALG
71#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
72#endif
73
74/* A cipher algorithm and key type that are known to be supported.
75 *
76 * This is used in some smoke tests.
77 */
78#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
79#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
80#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
81#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
82#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
83#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
84#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
85#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
86#else
87#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
88#endif
89#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
90#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
92#elif defined(MBEDTLS_RC4_C)
93#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
94#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
95#else
96#undef KNOWN_SUPPORTED_CIPHER_ALG
97#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
98#endif
99
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200100/** Test if a buffer contains a constant byte value.
101 *
102 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200103 *
104 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200105 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200106 * \param size Size of the buffer in bytes.
107 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200108 * \return 1 if the buffer is all-bits-zero.
109 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200110 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200111static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200112{
113 size_t i;
114 for( i = 0; i < size; i++ )
115 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200116 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200117 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200118 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200119 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200120}
Gilles Peskine818ca122018-06-20 18:16:48 +0200121
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200122/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
123static int asn1_write_10x( unsigned char **p,
124 unsigned char *start,
125 size_t bits,
126 unsigned char x )
127{
128 int ret;
129 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200130 if( bits == 0 )
131 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
132 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200133 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300134 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200135 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
136 *p -= len;
137 ( *p )[len-1] = x;
138 if( bits % 8 == 0 )
139 ( *p )[1] |= 1;
140 else
141 ( *p )[0] |= 1 << ( bits % 8 );
142 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
143 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
144 MBEDTLS_ASN1_INTEGER ) );
145 return( len );
146}
147
148static int construct_fake_rsa_key( unsigned char *buffer,
149 size_t buffer_size,
150 unsigned char **p,
151 size_t bits,
152 int keypair )
153{
154 size_t half_bits = ( bits + 1 ) / 2;
155 int ret;
156 int len = 0;
157 /* Construct something that looks like a DER encoding of
158 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
159 * RSAPrivateKey ::= SEQUENCE {
160 * version Version,
161 * modulus INTEGER, -- n
162 * publicExponent INTEGER, -- e
163 * privateExponent INTEGER, -- d
164 * prime1 INTEGER, -- p
165 * prime2 INTEGER, -- q
166 * exponent1 INTEGER, -- d mod (p-1)
167 * exponent2 INTEGER, -- d mod (q-1)
168 * coefficient INTEGER, -- (inverse of q) mod p
169 * otherPrimeInfos OtherPrimeInfos OPTIONAL
170 * }
171 * Or, for a public key, the same structure with only
172 * version, modulus and publicExponent.
173 */
174 *p = buffer + buffer_size;
175 if( keypair )
176 {
177 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
178 asn1_write_10x( p, buffer, half_bits, 1 ) );
179 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
180 asn1_write_10x( p, buffer, half_bits, 1 ) );
181 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
182 asn1_write_10x( p, buffer, half_bits, 1 ) );
183 MBEDTLS_ASN1_CHK_ADD( len, /* q */
184 asn1_write_10x( p, buffer, half_bits, 1 ) );
185 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
186 asn1_write_10x( p, buffer, half_bits, 3 ) );
187 MBEDTLS_ASN1_CHK_ADD( len, /* d */
188 asn1_write_10x( p, buffer, bits, 1 ) );
189 }
190 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
191 asn1_write_10x( p, buffer, 17, 1 ) );
192 MBEDTLS_ASN1_CHK_ADD( len, /* n */
193 asn1_write_10x( p, buffer, bits, 1 ) );
194 if( keypair )
195 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
196 mbedtls_asn1_write_int( p, buffer, 0 ) );
197 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
198 {
199 const unsigned char tag =
200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
201 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
202 }
203 return( len );
204}
205
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100206int exercise_mac_setup( psa_key_type_t key_type,
207 const unsigned char *key_bytes,
208 size_t key_length,
209 psa_algorithm_t alg,
210 psa_mac_operation_t *operation,
211 psa_status_t *status )
212{
213 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100215
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
217 psa_set_key_algorithm( &attributes, alg );
218 psa_set_key_type( &attributes, key_type );
219 PSA_ASSERT( psa_import_key( &attributes, &handle, key_bytes, key_length ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100220
221 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 /* Whether setup succeeded or failed, abort must succeed. */
223 PSA_ASSERT( psa_mac_abort( operation ) );
224 /* If setup failed, reproduce the failure, so that the caller can
225 * test the resulting state of the operation object. */
226 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100227 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100228 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
229 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230 }
231
232 psa_destroy_key( handle );
233 return( 1 );
234
235exit:
236 psa_destroy_key( handle );
237 return( 0 );
238}
239
240int exercise_cipher_setup( psa_key_type_t key_type,
241 const unsigned char *key_bytes,
242 size_t key_length,
243 psa_algorithm_t alg,
244 psa_cipher_operation_t *operation,
245 psa_status_t *status )
246{
247 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100249
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
251 psa_set_key_algorithm( &attributes, alg );
252 psa_set_key_type( &attributes, key_type );
253 PSA_ASSERT( psa_import_key( &attributes, &handle, key_bytes, key_length ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100254
255 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100256 /* Whether setup succeeded or failed, abort must succeed. */
257 PSA_ASSERT( psa_cipher_abort( operation ) );
258 /* If setup failed, reproduce the failure, so that the caller can
259 * test the resulting state of the operation object. */
260 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100261 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100262 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
263 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100264 }
265
266 psa_destroy_key( handle );
267 return( 1 );
268
269exit:
270 psa_destroy_key( handle );
271 return( 0 );
272}
273
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100274static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200275 psa_key_usage_t usage,
276 psa_algorithm_t alg )
277{
Jaeden Amero769ce272019-01-04 11:48:03 +0000278 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200279 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200280 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200281 size_t mac_length = sizeof( mac );
282
283 if( usage & PSA_KEY_USAGE_SIGN )
284 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100285 PSA_ASSERT( psa_mac_sign_setup( &operation,
286 handle, alg ) );
287 PSA_ASSERT( psa_mac_update( &operation,
288 input, sizeof( input ) ) );
289 PSA_ASSERT( psa_mac_sign_finish( &operation,
290 mac, sizeof( mac ),
291 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 }
293
294 if( usage & PSA_KEY_USAGE_VERIFY )
295 {
296 psa_status_t verify_status =
297 ( usage & PSA_KEY_USAGE_SIGN ?
298 PSA_SUCCESS :
299 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100300 PSA_ASSERT( psa_mac_verify_setup( &operation,
301 handle, alg ) );
302 PSA_ASSERT( psa_mac_update( &operation,
303 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100304 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
305 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 }
307
308 return( 1 );
309
310exit:
311 psa_mac_abort( &operation );
312 return( 0 );
313}
314
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100315static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200316 psa_key_usage_t usage,
317 psa_algorithm_t alg )
318{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000319 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200320 unsigned char iv[16] = {0};
321 size_t iv_length = sizeof( iv );
322 const unsigned char plaintext[16] = "Hello, world...";
323 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 unsigned char decrypted[sizeof( ciphertext )];
326 size_t part_length;
327
328 if( usage & PSA_KEY_USAGE_ENCRYPT )
329 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100330 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
331 handle, alg ) );
332 PSA_ASSERT( psa_cipher_generate_iv( &operation,
333 iv, sizeof( iv ),
334 &iv_length ) );
335 PSA_ASSERT( psa_cipher_update( &operation,
336 plaintext, sizeof( plaintext ),
337 ciphertext, sizeof( ciphertext ),
338 &ciphertext_length ) );
339 PSA_ASSERT( psa_cipher_finish( &operation,
340 ciphertext + ciphertext_length,
341 sizeof( ciphertext ) - ciphertext_length,
342 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200343 ciphertext_length += part_length;
344 }
345
346 if( usage & PSA_KEY_USAGE_DECRYPT )
347 {
348 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200350 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
351 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
354 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
355 * have this macro yet. */
356 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
357 psa_get_key_type( &attributes ) );
358 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200359 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100360 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
361 handle, alg ) );
362 PSA_ASSERT( psa_cipher_set_iv( &operation,
363 iv, iv_length ) );
364 PSA_ASSERT( psa_cipher_update( &operation,
365 ciphertext, ciphertext_length,
366 decrypted, sizeof( decrypted ),
367 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200368 status = psa_cipher_finish( &operation,
369 decrypted + part_length,
370 sizeof( decrypted ) - part_length,
371 &part_length );
372 /* For a stream cipher, all inputs are valid. For a block cipher,
373 * if the input is some aribtrary data rather than an actual
374 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200375 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200376 TEST_ASSERT( status == PSA_SUCCESS ||
377 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200378 else
379 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200380 }
381
382 return( 1 );
383
384exit:
385 psa_cipher_abort( &operation );
386 return( 0 );
387}
388
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200390 psa_key_usage_t usage,
391 psa_algorithm_t alg )
392{
393 unsigned char nonce[16] = {0};
394 size_t nonce_length = sizeof( nonce );
395 unsigned char plaintext[16] = "Hello, world...";
396 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
397 size_t ciphertext_length = sizeof( ciphertext );
398 size_t plaintext_length = sizeof( ciphertext );
399
400 if( usage & PSA_KEY_USAGE_ENCRYPT )
401 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_aead_encrypt( handle, alg,
403 nonce, nonce_length,
404 NULL, 0,
405 plaintext, sizeof( plaintext ),
406 ciphertext, sizeof( ciphertext ),
407 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200408 }
409
410 if( usage & PSA_KEY_USAGE_DECRYPT )
411 {
412 psa_status_t verify_status =
413 ( usage & PSA_KEY_USAGE_ENCRYPT ?
414 PSA_SUCCESS :
415 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100416 TEST_EQUAL( psa_aead_decrypt( handle, alg,
417 nonce, nonce_length,
418 NULL, 0,
419 ciphertext, ciphertext_length,
420 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100421 &plaintext_length ),
422 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200423 }
424
425 return( 1 );
426
427exit:
428 return( 0 );
429}
430
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100431static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200432 psa_key_usage_t usage,
433 psa_algorithm_t alg )
434{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200435 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
436 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200437 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200438 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100439 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
440
441 /* If the policy allows signing with any hash, just pick one. */
442 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
443 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100444#if defined(KNOWN_SUPPORTED_HASH_ALG)
445 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
446 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100447#else
448 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100449 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100450#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100451 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200452
453 if( usage & PSA_KEY_USAGE_SIGN )
454 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200455 /* Some algorithms require the payload to have the size of
456 * the hash encoded in the algorithm. Use this input size
457 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200458 if( hash_alg != 0 )
459 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100460 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
461 payload, payload_length,
462 signature, sizeof( signature ),
463 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200464 }
465
466 if( usage & PSA_KEY_USAGE_VERIFY )
467 {
468 psa_status_t verify_status =
469 ( usage & PSA_KEY_USAGE_SIGN ?
470 PSA_SUCCESS :
471 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
473 payload, payload_length,
474 signature, signature_length ),
475 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200476 }
477
478 return( 1 );
479
480exit:
481 return( 0 );
482}
483
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100484static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200485 psa_key_usage_t usage,
486 psa_algorithm_t alg )
487{
488 unsigned char plaintext[256] = "Hello, world...";
489 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
490 size_t ciphertext_length = sizeof( ciphertext );
491 size_t plaintext_length = 16;
492
493 if( usage & PSA_KEY_USAGE_ENCRYPT )
494 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100495 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
496 plaintext, plaintext_length,
497 NULL, 0,
498 ciphertext, sizeof( ciphertext ),
499 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200500 }
501
502 if( usage & PSA_KEY_USAGE_DECRYPT )
503 {
504 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100505 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200506 ciphertext, ciphertext_length,
507 NULL, 0,
508 plaintext, sizeof( plaintext ),
509 &plaintext_length );
510 TEST_ASSERT( status == PSA_SUCCESS ||
511 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
512 ( status == PSA_ERROR_INVALID_ARGUMENT ||
513 status == PSA_ERROR_INVALID_PADDING ) ) );
514 }
515
516 return( 1 );
517
518exit:
519 return( 0 );
520}
Gilles Peskine02b75072018-07-01 22:31:34 +0200521
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100522static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200523 psa_key_usage_t usage,
524 psa_algorithm_t alg )
525{
526 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
527 unsigned char label[16] = "This is a label.";
528 size_t label_length = sizeof( label );
529 unsigned char seed[16] = "abcdefghijklmnop";
530 size_t seed_length = sizeof( seed );
531 unsigned char output[1];
532
533 if( usage & PSA_KEY_USAGE_DERIVE )
534 {
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100535 if( PSA_ALG_IS_HKDF( alg ) )
536 {
537 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
538 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
539 PSA_KDF_STEP_SALT,
540 label,
541 label_length ) );
542 PSA_ASSERT( psa_key_derivation_input_key( &generator,
543 PSA_KDF_STEP_SECRET,
544 handle ) );
545 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
546 PSA_KDF_STEP_INFO,
547 seed,
548 seed_length ) );
549 }
550 else
551 {
552 // legacy
553 PSA_ASSERT( psa_key_derivation( &generator,
554 handle, alg,
555 label, label_length,
556 seed, seed_length,
557 sizeof( output ) ) );
558 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100559 PSA_ASSERT( psa_generator_read( &generator,
560 output,
561 sizeof( output ) ) );
562 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200563 }
564
565 return( 1 );
566
567exit:
568 return( 0 );
569}
570
Gilles Peskinec7998b72018-11-07 18:45:02 +0100571/* We need two keys to exercise key agreement. Exercise the
572 * private key against its own public key. */
573static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +0100574 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100575{
576 psa_key_type_t private_key_type;
577 psa_key_type_t public_key_type;
578 size_t key_bits;
579 uint8_t *public_key = NULL;
580 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200581 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinec7998b72018-11-07 18:45:02 +0100582 * psa_key_agreement fails. This isn't fully satisfactory, but it's
583 * good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200584 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100586
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200587 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
588 private_key_type = psa_get_key_type( &attributes );
589 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100590 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
591 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
592 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100593 PSA_ASSERT( psa_export_public_key( handle,
594 public_key, public_key_length,
595 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100596
Gilles Peskine969c5d62019-01-16 15:53:06 +0100597 status = psa_key_agreement( generator, PSA_KDF_STEP_SECRET, handle,
598 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100599exit:
600 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200601 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100602 return( status );
603}
604
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200605/* We need two keys to exercise key agreement. Exercise the
606 * private key against its own public key. */
607static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
608 psa_key_handle_t handle )
609{
610 psa_key_type_t private_key_type;
611 psa_key_type_t public_key_type;
612 size_t key_bits;
613 uint8_t *public_key = NULL;
614 size_t public_key_length;
615 uint8_t output[1024];
616 size_t output_length;
617 /* Return GENERIC_ERROR if something other than the final call to
618 * psa_key_agreement fails. This isn't fully satisfactory, but it's
619 * good enough: callers will report it as a failed test anyway. */
620 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200622
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200623 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
624 private_key_type = psa_get_key_type( &attributes );
625 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200626 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
627 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
628 ASSERT_ALLOC( public_key, public_key_length );
629 PSA_ASSERT( psa_export_public_key( handle,
630 public_key, public_key_length,
631 &public_key_length ) );
632
633 status = psa_key_agreement_raw_shared_secret(
634 alg, handle,
635 public_key, public_key_length,
636 output, sizeof( output ), &output_length );
637exit:
638 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200639 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200640 return( status );
641}
642
643static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
644 psa_key_usage_t usage,
645 psa_algorithm_t alg )
646{
647 int ok = 0;
648
649 if( usage & PSA_KEY_USAGE_DERIVE )
650 {
651 /* We need two keys to exercise key agreement. Exercise the
652 * private key against its own public key. */
653 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
654 }
655 ok = 1;
656
657exit:
658 return( ok );
659}
660
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100661static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200662 psa_key_usage_t usage,
663 psa_algorithm_t alg )
664{
665 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200666 unsigned char output[1];
667 int ok = 0;
668
669 if( usage & PSA_KEY_USAGE_DERIVE )
670 {
671 /* We need two keys to exercise key agreement. Exercise the
672 * private key against its own public key. */
Gilles Peskine969c5d62019-01-16 15:53:06 +0100673 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
674 PSA_ASSERT( key_agreement_with_self( &generator, handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_generator_read( &generator,
676 output,
677 sizeof( output ) ) );
678 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200679 }
680 ok = 1;
681
682exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200683 return( ok );
684}
685
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200686static int is_oid_of_key_type( psa_key_type_t type,
687 const uint8_t *oid, size_t oid_length )
688{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200689 const uint8_t *expected_oid = NULL;
690 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200691#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200692 if( PSA_KEY_TYPE_IS_RSA( type ) )
693 {
694 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
695 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
696 }
697 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200698#endif /* MBEDTLS_RSA_C */
699#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200700 if( PSA_KEY_TYPE_IS_ECC( type ) )
701 {
702 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
703 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
704 }
705 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200706#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200707 {
708 char message[40];
709 mbedtls_snprintf( message, sizeof( message ),
710 "OID not known for key type=0x%08lx",
711 (unsigned long) type );
712 test_fail( message, __LINE__, __FILE__ );
713 return( 0 );
714 }
715
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200716 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200717 return( 1 );
718
719exit:
720 return( 0 );
721}
722
723static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
724 size_t min_bits, size_t max_bits,
725 int must_be_odd )
726{
727 size_t len;
728 size_t actual_bits;
729 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100730 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100731 MBEDTLS_ASN1_INTEGER ),
732 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200733 /* Tolerate a slight departure from DER encoding:
734 * - 0 may be represented by an empty string or a 1-byte string.
735 * - The sign bit may be used as a value bit. */
736 if( ( len == 1 && ( *p )[0] == 0 ) ||
737 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
738 {
739 ++( *p );
740 --len;
741 }
742 if( min_bits == 0 && len == 0 )
743 return( 1 );
744 msb = ( *p )[0];
745 TEST_ASSERT( msb != 0 );
746 actual_bits = 8 * ( len - 1 );
747 while( msb != 0 )
748 {
749 msb >>= 1;
750 ++actual_bits;
751 }
752 TEST_ASSERT( actual_bits >= min_bits );
753 TEST_ASSERT( actual_bits <= max_bits );
754 if( must_be_odd )
755 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
756 *p += len;
757 return( 1 );
758exit:
759 return( 0 );
760}
761
762static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
763 size_t *len,
764 unsigned char n, unsigned char tag )
765{
766 int ret;
767 ret = mbedtls_asn1_get_tag( p, end, len,
768 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
769 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
770 if( ret != 0 )
771 return( ret );
772 end = *p + *len;
773 ret = mbedtls_asn1_get_tag( p, end, len, tag );
774 if( ret != 0 )
775 return( ret );
776 if( *p + *len != end )
777 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
778 return( 0 );
779}
780
781static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
782 uint8_t *exported, size_t exported_length )
783{
784 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100785 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200786 else
787 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200788
789#if defined(MBEDTLS_DES_C)
790 if( type == PSA_KEY_TYPE_DES )
791 {
792 /* Check the parity bits. */
793 unsigned i;
794 for( i = 0; i < bits / 8; i++ )
795 {
796 unsigned bit_count = 0;
797 unsigned m;
798 for( m = 1; m <= 0x100; m <<= 1 )
799 {
800 if( exported[i] & m )
801 ++bit_count;
802 }
803 TEST_ASSERT( bit_count % 2 != 0 );
804 }
805 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200806 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200807#endif
808
809#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
810 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
811 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200812 uint8_t *p = exported;
813 uint8_t *end = exported + exported_length;
814 size_t len;
815 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200816 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200817 * modulus INTEGER, -- n
818 * publicExponent INTEGER, -- e
819 * privateExponent INTEGER, -- d
820 * prime1 INTEGER, -- p
821 * prime2 INTEGER, -- q
822 * exponent1 INTEGER, -- d mod (p-1)
823 * exponent2 INTEGER, -- d mod (q-1)
824 * coefficient INTEGER, -- (inverse of q) mod p
825 * }
826 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100827 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
828 MBEDTLS_ASN1_SEQUENCE |
829 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
830 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200831 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
832 goto exit;
833 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
834 goto exit;
835 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
836 goto exit;
837 /* Require d to be at least half the size of n. */
838 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
839 goto exit;
840 /* Require p and q to be at most half the size of n, rounded up. */
841 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
842 goto exit;
843 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
844 goto exit;
845 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
846 goto exit;
847 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
848 goto exit;
849 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
850 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100851 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100852 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200853 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200854#endif /* MBEDTLS_RSA_C */
855
856#if defined(MBEDTLS_ECP_C)
857 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
858 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100859 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100860 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100861 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200862 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200863#endif /* MBEDTLS_ECP_C */
864
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200865 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
866 {
867 uint8_t *p = exported;
868 uint8_t *end = exported + exported_length;
869 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200870#if defined(MBEDTLS_RSA_C)
871 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
872 {
873 /* RSAPublicKey ::= SEQUENCE {
874 * modulus INTEGER, -- n
875 * publicExponent INTEGER } -- e
876 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100877 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
878 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100879 MBEDTLS_ASN1_CONSTRUCTED ),
880 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100881 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200882 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
883 goto exit;
884 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
885 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100886 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200887 }
888 else
889#endif /* MBEDTLS_RSA_C */
890#if defined(MBEDTLS_ECP_C)
891 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
892 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000893 /* The representation of an ECC public key is:
894 * - The byte 0x04;
895 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
896 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
897 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000898 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100899 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
900 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200901 }
902 else
903#endif /* MBEDTLS_ECP_C */
904 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100905 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200906 mbedtls_snprintf( message, sizeof( message ),
907 "No sanity check for public key type=0x%08lx",
908 (unsigned long) type );
909 test_fail( message, __LINE__, __FILE__ );
910 return( 0 );
911 }
912 }
913 else
914
915 {
916 /* No sanity checks for other types */
917 }
918
919 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200920
921exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200922 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200923}
924
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100925static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200926 psa_key_usage_t usage )
927{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200929 uint8_t *exported = NULL;
930 size_t exported_size = 0;
931 size_t exported_length = 0;
932 int ok = 0;
933
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200934 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200935
936 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200937 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200938 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100939 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
940 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200941 ok = 1;
942 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +0200943 }
944
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200945 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
946 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200947 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200948
Gilles Peskine8817f612018-12-18 00:18:46 +0100949 PSA_ASSERT( psa_export_key( handle,
950 exported, exported_size,
951 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200952 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
953 psa_get_key_bits( &attributes ),
954 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +0200955
956exit:
957 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200958 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200959 return( ok );
960}
961
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100962static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200963{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200965 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +0200966 uint8_t *exported = NULL;
967 size_t exported_size = 0;
968 size_t exported_length = 0;
969 int ok = 0;
970
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200971 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
972 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200973 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100974 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100975 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200976 return( 1 );
977 }
978
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200979 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(
980 psa_get_key_type( &attributes ) );
981 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
982 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200983 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200984
Gilles Peskine8817f612018-12-18 00:18:46 +0100985 PSA_ASSERT( psa_export_public_key( handle,
986 exported, exported_size,
987 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200988 ok = exported_key_sanity_check( public_type,
989 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +0200990 exported, exported_length );
991
992exit:
993 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200994 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200995 return( ok );
996}
997
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100998/** Do smoke tests on a key.
999 *
1000 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1001 * sign/verify, or derivation) that is permitted according to \p usage.
1002 * \p usage and \p alg should correspond to the expected policy on the
1003 * key.
1004 *
1005 * Export the key if permitted by \p usage, and check that the output
1006 * looks sensible. If \p usage forbids export, check that
1007 * \p psa_export_key correctly rejects the attempt. If the key is
1008 * asymmetric, also check \p psa_export_public_key.
1009 *
1010 * If the key fails the tests, this function calls the test framework's
1011 * `test_fail` function and returns false. Otherwise this function returns
1012 * true. Therefore it should be used as follows:
1013 * ```
1014 * if( ! exercise_key( ... ) ) goto exit;
1015 * ```
1016 *
1017 * \param handle The key to exercise. It should be capable of performing
1018 * \p alg.
1019 * \param usage The usage flags to assume.
1020 * \param alg The algorithm to exercise.
1021 *
1022 * \retval 0 The key failed the smoke tests.
1023 * \retval 1 The key passed the smoke tests.
1024 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001025static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001026 psa_key_usage_t usage,
1027 psa_algorithm_t alg )
1028{
1029 int ok;
1030 if( alg == 0 )
1031 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1032 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001033 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001034 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001035 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001036 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001038 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001039 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001040 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001041 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001042 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001043 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001044 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1045 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001046 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001047 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001048 else
1049 {
1050 char message[40];
1051 mbedtls_snprintf( message, sizeof( message ),
1052 "No code to exercise alg=0x%08lx",
1053 (unsigned long) alg );
1054 test_fail( message, __LINE__, __FILE__ );
1055 ok = 0;
1056 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001057
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001058 ok = ok && exercise_export_key( handle, usage );
1059 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001060
Gilles Peskine02b75072018-07-01 22:31:34 +02001061 return( ok );
1062}
1063
Gilles Peskine10df3412018-10-25 22:35:43 +02001064static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1065 psa_algorithm_t alg )
1066{
1067 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1068 {
1069 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1070 PSA_KEY_USAGE_VERIFY :
1071 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1072 }
1073 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1074 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1075 {
1076 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1077 PSA_KEY_USAGE_ENCRYPT :
1078 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1079 }
1080 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1081 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1082 {
1083 return( PSA_KEY_USAGE_DERIVE );
1084 }
1085 else
1086 {
1087 return( 0 );
1088 }
1089
1090}
Darryl Green0c6575a2018-11-07 16:05:30 +00001091
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001092static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1093{
1094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1095 uint8_t buffer[1];
1096 size_t length;
1097 int ok = 0;
1098
1099 psa_make_key_persistent( &attributes, 0x6964, PSA_KEY_LIFETIME_PERSISTENT );
1100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1101 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1102 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1103 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1104 PSA_ERROR_INVALID_HANDLE );
1105 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001106 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001107 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1108 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1109 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1110 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1111
1112 TEST_EQUAL( psa_export_key( handle,
1113 buffer, sizeof( buffer ), &length ),
1114 PSA_ERROR_INVALID_HANDLE );
1115 TEST_EQUAL( psa_export_public_key( handle,
1116 buffer, sizeof( buffer ), &length ),
1117 PSA_ERROR_INVALID_HANDLE );
1118
1119 TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
1120 TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE );
1121
1122 ok = 1;
1123
1124exit:
1125 psa_reset_key_attributes( &attributes );
1126 return( ok );
1127}
1128
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001129/* An overapproximation of the amount of storage needed for a key of the
1130 * given type and with the given content. The API doesn't make it easy
1131 * to find a good value for the size. The current implementation doesn't
1132 * care about the value anyway. */
1133#define KEY_BITS_FROM_DATA( type, data ) \
1134 ( data )->len
1135
Darryl Green0c6575a2018-11-07 16:05:30 +00001136typedef enum {
1137 IMPORT_KEY = 0,
1138 GENERATE_KEY = 1,
1139 DERIVE_KEY = 2
1140} generate_method;
1141
Gilles Peskinee59236f2018-01-27 23:32:46 +01001142/* END_HEADER */
1143
1144/* BEGIN_DEPENDENCIES
1145 * depends_on:MBEDTLS_PSA_CRYPTO_C
1146 * END_DEPENDENCIES
1147 */
1148
1149/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001150void static_checks( )
1151{
1152 size_t max_truncated_mac_size =
1153 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1154
1155 /* Check that the length for a truncated MAC always fits in the algorithm
1156 * encoding. The shifted mask is the maximum truncated value. The
1157 * untruncated algorithm may be one byte larger. */
1158 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1159}
1160/* END_CASE */
1161
1162/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001163void attributes_set_get( int id_arg, int lifetime_arg,
1164 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001165 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001166{
Gilles Peskine4747d192019-04-17 15:05:45 +02001167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001168 psa_key_id_t id = id_arg;
1169 psa_key_lifetime_t lifetime = lifetime_arg;
1170 psa_key_usage_t usage_flags = usage_flags_arg;
1171 psa_algorithm_t alg = alg_arg;
1172 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001173 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001174
1175 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1176 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1177 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1178 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1179 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001180 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001181
1182 psa_make_key_persistent( &attributes, id, lifetime );
1183 psa_set_key_usage_flags( &attributes, usage_flags );
1184 psa_set_key_algorithm( &attributes, alg );
1185 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001186 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001187
1188 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1189 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1190 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1191 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1192 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001193 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001194
1195 psa_reset_key_attributes( &attributes );
1196
1197 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1198 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1199 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1200 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1201 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001202 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001203}
1204/* END_CASE */
1205
1206/* BEGIN_CASE */
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001207void import( data_t *data, int type_arg,
1208 int attr_bits_arg,
1209 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001210{
1211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1212 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001213 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001214 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001215 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001216 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001217 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001218
Gilles Peskine8817f612018-12-18 00:18:46 +01001219 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001220
Gilles Peskine4747d192019-04-17 15:05:45 +02001221 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001222 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine4747d192019-04-17 15:05:45 +02001223 status = psa_import_key( &attributes, &handle, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001224 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001225 if( status != PSA_SUCCESS )
1226 goto exit;
1227
1228 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1229 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001230 if( attr_bits != 0 )
1231 TEST_EQUAL( attr_bits, got_attributes.bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001232
1233 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001234 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001235
1236exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001237 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001238 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001239 mbedtls_psa_crypto_free( );
1240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001244void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1245{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001246 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001247 size_t bits = bits_arg;
1248 psa_status_t expected_status = expected_status_arg;
1249 psa_status_t status;
1250 psa_key_type_t type =
1251 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1252 size_t buffer_size = /* Slight overapproximations */
1253 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001254 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001255 unsigned char *p;
1256 int ret;
1257 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001259
Gilles Peskine8817f612018-12-18 00:18:46 +01001260 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001261 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001262
1263 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1264 bits, keypair ) ) >= 0 );
1265 length = ret;
1266
1267 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001268 psa_set_key_type( &attributes, type );
1269 status = psa_import_key( &attributes, &handle, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001270 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001271 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001272 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001273
1274exit:
1275 mbedtls_free( buffer );
1276 mbedtls_psa_crypto_free( );
1277}
1278/* END_CASE */
1279
1280/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001281void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001282 int type_arg,
1283 int alg_arg,
1284 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285 int expected_bits,
1286 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001287 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001288 int canonical_input )
1289{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001290 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001291 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001292 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001293 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001294 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001295 unsigned char *exported = NULL;
1296 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001297 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001298 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001301 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302
Moran Pekercb088e72018-07-17 17:36:59 +03001303 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001304 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001305 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001306 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001307 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308
Gilles Peskine4747d192019-04-17 15:05:45 +02001309 psa_set_key_usage_flags( &attributes, usage_arg );
1310 psa_set_key_algorithm( &attributes, alg );
1311 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001312
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001313 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001314 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315
1316 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001317 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1318 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1319 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001320
1321 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001322 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001323 exported, export_size,
1324 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001325 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001326
1327 /* The exported length must be set by psa_export_key() to a value between 0
1328 * and export_size. On errors, the exported length must be 0. */
1329 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1330 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1331 TEST_ASSERT( exported_length <= export_size );
1332
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001333 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001334 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001335 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001336 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001337 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001338 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001339 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001341 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001342 goto exit;
1343
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001344 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001345 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001346 else
1347 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001348 psa_key_handle_t handle2;
Gilles Peskine4747d192019-04-17 15:05:45 +02001349 PSA_ASSERT( psa_import_key( &attributes, &handle2,
1350 exported, exported_length ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001351 PSA_ASSERT( psa_export_key( handle2,
1352 reexported,
1353 export_size,
1354 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001355 ASSERT_COMPARE( exported, exported_length,
1356 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001357 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001358 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001359 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001360
1361destroy:
1362 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001363 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001364 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001365
1366exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001367 mbedtls_free( exported );
1368 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001369 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370 mbedtls_psa_crypto_free( );
1371}
1372/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001373
Moran Pekerf709f4a2018-06-06 17:26:04 +03001374/* BEGIN_CASE */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001375void invalid_handle( int handle )
Moran Peker28a38e62018-11-07 16:18:24 +02001376{
Gilles Peskine8817f612018-12-18 00:18:46 +01001377 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001378 test_operations_on_invalid_handle( handle );
Moran Peker28a38e62018-11-07 16:18:24 +02001379
1380exit:
1381 mbedtls_psa_crypto_free( );
1382}
1383/* END_CASE */
1384
1385/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001386void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001387 int type_arg,
1388 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001389 int export_size_delta,
1390 int expected_export_status_arg,
1391 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001392{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001393 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001394 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001395 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001396 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001397 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001398 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001399 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001400 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001402
Gilles Peskine8817f612018-12-18 00:18:46 +01001403 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001404
Gilles Peskine4747d192019-04-17 15:05:45 +02001405 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1406 psa_set_key_algorithm( &attributes, alg );
1407 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001408
1409 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001410 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001411
Gilles Peskine49c25912018-10-29 15:15:31 +01001412 /* Export the public key */
1413 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001414 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001415 exported, export_size,
1416 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001417 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001418 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001419 {
1420 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1421 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001422 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1423 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001424 TEST_ASSERT( expected_public_key->len <=
1425 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001426 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1427 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001428 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001429
1430exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001431 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001432 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001433 psa_reset_key_attributes( &attributes );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001434 mbedtls_psa_crypto_free( );
1435}
1436/* END_CASE */
1437
Gilles Peskine20035e32018-02-03 22:44:14 +01001438/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001439void import_and_exercise_key( data_t *data,
1440 int type_arg,
1441 int bits_arg,
1442 int alg_arg )
1443{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001444 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001445 psa_key_type_t type = type_arg;
1446 size_t bits = bits_arg;
1447 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001448 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001450 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001451
Gilles Peskine8817f612018-12-18 00:18:46 +01001452 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001453
Gilles Peskine4747d192019-04-17 15:05:45 +02001454 psa_set_key_usage_flags( &attributes, usage );
1455 psa_set_key_algorithm( &attributes, alg );
1456 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001457
1458 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001459 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001460
1461 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001462 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1463 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1464 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001465
1466 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001467 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001468 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001469
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001470 PSA_ASSERT( psa_destroy_key( handle ) );
1471 test_operations_on_invalid_handle( handle );
1472
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001473exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001474 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001475 psa_reset_key_attributes( &got_attributes );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001476 mbedtls_psa_crypto_free( );
1477}
1478/* END_CASE */
1479
1480/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001481void key_policy( int usage_arg, int alg_arg )
1482{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001483 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001484 psa_algorithm_t alg = alg_arg;
1485 psa_key_usage_t usage = usage_arg;
1486 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1487 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001489
1490 memset( key, 0x2a, sizeof( key ) );
1491
Gilles Peskine8817f612018-12-18 00:18:46 +01001492 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001493
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001494 psa_set_key_usage_flags( &attributes, usage );
1495 psa_set_key_algorithm( &attributes, alg );
1496 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001497
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001498 PSA_ASSERT( psa_import_key( &attributes, &handle, key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001499
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001500 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1501 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1502 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1503 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001504
1505exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001506 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001507 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001508 mbedtls_psa_crypto_free( );
1509}
1510/* END_CASE */
1511
1512/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001513void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001514{
1515 /* Test each valid way of initializing the object, except for `= {0}`, as
1516 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1517 * though it's OK by the C standard. We could test for this, but we'd need
1518 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001519 psa_key_attributes_t func = psa_key_attributes_init( );
1520 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1521 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001522
1523 memset( &zero, 0, sizeof( zero ) );
1524
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001525 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1526 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1527 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001528
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001529 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1530 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1531 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1532
1533 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1534 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1535 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1536
1537 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1538 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1539 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1540
1541 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1542 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1543 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001544}
1545/* END_CASE */
1546
1547/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001548void mac_key_policy( int policy_usage,
1549 int policy_alg,
1550 int key_type,
1551 data_t *key_data,
1552 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001553{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001554 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001556 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001557 psa_status_t status;
1558 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001559
Gilles Peskine8817f612018-12-18 00:18:46 +01001560 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001561
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001562 psa_set_key_usage_flags( &attributes, policy_usage );
1563 psa_set_key_algorithm( &attributes, policy_alg );
1564 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001565
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001566 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001567 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001568
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001569 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001570 if( policy_alg == exercise_alg &&
1571 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001572 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001573 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001574 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001575 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001576
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001578 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001579 if( policy_alg == exercise_alg &&
1580 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001581 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001582 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001583 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001584
1585exit:
1586 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001587 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001588 mbedtls_psa_crypto_free( );
1589}
1590/* END_CASE */
1591
1592/* BEGIN_CASE */
1593void cipher_key_policy( int policy_usage,
1594 int policy_alg,
1595 int key_type,
1596 data_t *key_data,
1597 int exercise_alg )
1598{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001599 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001600 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001601 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001602 psa_status_t status;
1603
Gilles Peskine8817f612018-12-18 00:18:46 +01001604 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001605
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001606 psa_set_key_usage_flags( &attributes, policy_usage );
1607 psa_set_key_algorithm( &attributes, policy_alg );
1608 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001609
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001610 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001611 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001612
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001613 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614 if( policy_alg == exercise_alg &&
1615 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001616 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001617 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001618 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619 psa_cipher_abort( &operation );
1620
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001621 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001622 if( policy_alg == exercise_alg &&
1623 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001624 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001625 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001626 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001627
1628exit:
1629 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001630 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001631 mbedtls_psa_crypto_free( );
1632}
1633/* END_CASE */
1634
1635/* BEGIN_CASE */
1636void aead_key_policy( int policy_usage,
1637 int policy_alg,
1638 int key_type,
1639 data_t *key_data,
1640 int nonce_length_arg,
1641 int tag_length_arg,
1642 int exercise_alg )
1643{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001644 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001646 psa_status_t status;
1647 unsigned char nonce[16] = {0};
1648 size_t nonce_length = nonce_length_arg;
1649 unsigned char tag[16];
1650 size_t tag_length = tag_length_arg;
1651 size_t output_length;
1652
1653 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1654 TEST_ASSERT( tag_length <= sizeof( tag ) );
1655
Gilles Peskine8817f612018-12-18 00:18:46 +01001656 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001657
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001658 psa_set_key_usage_flags( &attributes, policy_usage );
1659 psa_set_key_algorithm( &attributes, policy_alg );
1660 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001661
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001662 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001663 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001664
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001665 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001666 nonce, nonce_length,
1667 NULL, 0,
1668 NULL, 0,
1669 tag, tag_length,
1670 &output_length );
1671 if( policy_alg == exercise_alg &&
1672 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001673 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001675 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001676
1677 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001678 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001679 nonce, nonce_length,
1680 NULL, 0,
1681 tag, tag_length,
1682 NULL, 0,
1683 &output_length );
1684 if( policy_alg == exercise_alg &&
1685 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001686 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001687 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001688 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001689
1690exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001691 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001692 mbedtls_psa_crypto_free( );
1693}
1694/* END_CASE */
1695
1696/* BEGIN_CASE */
1697void asymmetric_encryption_key_policy( int policy_usage,
1698 int policy_alg,
1699 int key_type,
1700 data_t *key_data,
1701 int exercise_alg )
1702{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001703 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001705 psa_status_t status;
1706 size_t key_bits;
1707 size_t buffer_length;
1708 unsigned char *buffer = NULL;
1709 size_t output_length;
1710
Gilles Peskine8817f612018-12-18 00:18:46 +01001711 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001712
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001713 psa_set_key_usage_flags( &attributes, policy_usage );
1714 psa_set_key_algorithm( &attributes, policy_alg );
1715 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001716
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001717 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001719
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001720 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1721 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001722 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1723 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001724 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001725
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001726 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001727 NULL, 0,
1728 NULL, 0,
1729 buffer, buffer_length,
1730 &output_length );
1731 if( policy_alg == exercise_alg &&
1732 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001733 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001734 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001735 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001736
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001737 if( buffer_length != 0 )
1738 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001739 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001740 buffer, buffer_length,
1741 NULL, 0,
1742 buffer, buffer_length,
1743 &output_length );
1744 if( policy_alg == exercise_alg &&
1745 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001746 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001747 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001748 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001749
1750exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001751 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001752 psa_reset_key_attributes( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001753 mbedtls_psa_crypto_free( );
1754 mbedtls_free( buffer );
1755}
1756/* END_CASE */
1757
1758/* BEGIN_CASE */
1759void asymmetric_signature_key_policy( int policy_usage,
1760 int policy_alg,
1761 int key_type,
1762 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001763 int exercise_alg,
1764 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001765{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001766 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001768 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001769 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1770 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1771 * compatible with the policy and `payload_length_arg` is supposed to be
1772 * a valid input length to sign. If `payload_length_arg <= 0`,
1773 * `exercise_alg` is supposed to be forbidden by the policy. */
1774 int compatible_alg = payload_length_arg > 0;
1775 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001776 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1777 size_t signature_length;
1778
Gilles Peskine8817f612018-12-18 00:18:46 +01001779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001780
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001781 psa_set_key_usage_flags( &attributes, policy_usage );
1782 psa_set_key_algorithm( &attributes, policy_alg );
1783 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001784
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001785 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001786 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001787
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001788 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001789 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001790 signature, sizeof( signature ),
1791 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001792 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001793 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001794 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001795 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001796
1797 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001798 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001799 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001800 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001801 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001802 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001803 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001804 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001805
1806exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001807 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001808 mbedtls_psa_crypto_free( );
1809}
1810/* END_CASE */
1811
1812/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001813void derive_key_policy( int policy_usage,
1814 int policy_alg,
1815 int key_type,
1816 data_t *key_data,
1817 int exercise_alg )
1818{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001819 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001821 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1822 psa_status_t status;
1823
Gilles Peskine8817f612018-12-18 00:18:46 +01001824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001826 psa_set_key_usage_flags( &attributes, policy_usage );
1827 psa_set_key_algorithm( &attributes, policy_alg );
1828 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001829
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001830 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001831 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001832
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001833 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001834 exercise_alg,
1835 NULL, 0,
1836 NULL, 0,
1837 1 );
1838 if( policy_alg == exercise_alg &&
1839 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001840 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001841 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001842 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001843
1844exit:
1845 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001846 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001847 mbedtls_psa_crypto_free( );
1848}
1849/* END_CASE */
1850
1851/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001852void agreement_key_policy( int policy_usage,
1853 int policy_alg,
1854 int key_type_arg,
1855 data_t *key_data,
1856 int exercise_alg )
1857{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001858 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001860 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001861 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1862 psa_status_t status;
1863
Gilles Peskine8817f612018-12-18 00:18:46 +01001864 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001865
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001866 psa_set_key_usage_flags( &attributes, policy_usage );
1867 psa_set_key_algorithm( &attributes, policy_alg );
1868 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001869
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001870 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001871 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001872
Gilles Peskine969c5d62019-01-16 15:53:06 +01001873 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
1874 status = key_agreement_with_self( &generator, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001875
Gilles Peskine01d718c2018-09-18 12:01:02 +02001876 if( policy_alg == exercise_alg &&
1877 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001878 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001879 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001880 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001881
1882exit:
1883 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001884 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001885 mbedtls_psa_crypto_free( );
1886}
1887/* END_CASE */
1888
1889/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001890void raw_agreement_key_policy( int policy_usage,
1891 int policy_alg,
1892 int key_type_arg,
1893 data_t *key_data,
1894 int exercise_alg )
1895{
1896 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001898 psa_key_type_t key_type = key_type_arg;
1899 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1900 psa_status_t status;
1901
1902 PSA_ASSERT( psa_crypto_init( ) );
1903
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001904 psa_set_key_usage_flags( &attributes, policy_usage );
1905 psa_set_key_algorithm( &attributes, policy_alg );
1906 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001907
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001908 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001909 key_data->x, key_data->len ) );
1910
1911 status = raw_key_agreement_with_self( exercise_alg, handle );
1912
1913 if( policy_alg == exercise_alg &&
1914 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1915 PSA_ASSERT( status );
1916 else
1917 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1918
1919exit:
1920 psa_generator_abort( &generator );
1921 psa_destroy_key( handle );
1922 mbedtls_psa_crypto_free( );
1923}
1924/* END_CASE */
1925
1926/* BEGIN_CASE */
Gilles Peskine4a644642019-05-03 17:14:08 +02001927void copy_success( int source_usage_arg, int source_alg_arg,
1928 int type_arg, data_t *material,
1929 int copy_attributes,
1930 int target_usage_arg, int target_alg_arg,
1931 int expected_usage_arg, int expected_alg_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001932{
Gilles Peskineca25db92019-04-19 11:43:08 +02001933 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1934 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001935 psa_key_usage_t expected_usage = expected_usage_arg;
1936 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02001937 psa_key_handle_t source_handle = 0;
1938 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001939 uint8_t *export_buffer = NULL;
1940
Gilles Peskine57ab7212019-01-28 13:03:09 +01001941 PSA_ASSERT( psa_crypto_init( ) );
1942
Gilles Peskineca25db92019-04-19 11:43:08 +02001943 /* Prepare the source key. */
1944 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1945 psa_set_key_algorithm( &source_attributes, source_alg_arg );
1946 psa_set_key_type( &source_attributes, type_arg );
1947 PSA_ASSERT( psa_import_key( &source_attributes, &source_handle,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001948 material->x, material->len ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001949 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001950
Gilles Peskineca25db92019-04-19 11:43:08 +02001951 /* Prepare the target attributes. */
1952 if( copy_attributes )
1953 target_attributes = source_attributes;
1954 if( target_usage_arg != -1 )
1955 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1956 if( target_alg_arg != -1 )
1957 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001958
1959 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02001960 PSA_ASSERT( psa_copy_key( source_handle,
1961 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001962
1963 /* Destroy the source to ensure that this doesn't affect the target. */
1964 PSA_ASSERT( psa_destroy_key( source_handle ) );
1965
1966 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02001967 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
1968 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1969 psa_get_key_type( &target_attributes ) );
1970 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1971 psa_get_key_bits( &target_attributes ) );
1972 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1973 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001974 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1975 {
1976 size_t length;
1977 ASSERT_ALLOC( export_buffer, material->len );
1978 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
1979 material->len, &length ) );
1980 ASSERT_COMPARE( material->x, material->len,
1981 export_buffer, length );
1982 }
1983 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
1984 goto exit;
1985
1986 PSA_ASSERT( psa_close_key( target_handle ) );
1987
1988exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001989 psa_reset_key_attributes( &source_attributes );
1990 psa_reset_key_attributes( &target_attributes );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001991 mbedtls_psa_crypto_free( );
1992 mbedtls_free( export_buffer );
1993}
1994/* END_CASE */
1995
1996/* BEGIN_CASE */
Gilles Peskine4a644642019-05-03 17:14:08 +02001997void copy_fail( int source_usage_arg, int source_alg_arg,
1998 int type_arg, data_t *material,
1999 int target_type_arg, int target_bits_arg,
2000 int target_usage_arg, int target_alg_arg,
2001 int expected_status_arg )
2002{
2003 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2004 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2005 psa_key_handle_t source_handle = 0;
2006 psa_key_handle_t target_handle = 0;
2007
2008 PSA_ASSERT( psa_crypto_init( ) );
2009
2010 /* Prepare the source key. */
2011 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2012 psa_set_key_algorithm( &source_attributes, source_alg_arg );
2013 psa_set_key_type( &source_attributes, type_arg );
2014 PSA_ASSERT( psa_import_key( &source_attributes, &source_handle,
2015 material->x, material->len ) );
2016
2017 /* Prepare the target attributes. */
2018 psa_set_key_type( &target_attributes, target_type_arg );
2019 psa_set_key_bits( &target_attributes, target_bits_arg );
2020 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2021 psa_set_key_algorithm( &target_attributes, target_alg_arg );
2022
2023 /* Try to copy the key. */
2024 TEST_EQUAL( psa_copy_key( source_handle,
2025 &target_attributes, &target_handle ),
2026 expected_status_arg );
2027exit:
2028 psa_reset_key_attributes( &source_attributes );
2029 psa_reset_key_attributes( &target_attributes );
2030 mbedtls_psa_crypto_free( );
2031}
2032/* END_CASE */
2033
2034/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002035void hash_operation_init( )
2036{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002037 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002038 /* Test each valid way of initializing the object, except for `= {0}`, as
2039 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2040 * though it's OK by the C standard. We could test for this, but we'd need
2041 * to supress the Clang warning for the test. */
2042 psa_hash_operation_t func = psa_hash_operation_init( );
2043 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2044 psa_hash_operation_t zero;
2045
2046 memset( &zero, 0, sizeof( zero ) );
2047
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002048 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002049 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2050 PSA_ERROR_BAD_STATE );
2051 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2052 PSA_ERROR_BAD_STATE );
2053 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2054 PSA_ERROR_BAD_STATE );
2055
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002056 /* A default hash operation should be abortable without error. */
2057 PSA_ASSERT( psa_hash_abort( &func ) );
2058 PSA_ASSERT( psa_hash_abort( &init ) );
2059 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002060}
2061/* END_CASE */
2062
2063/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002064void hash_setup( int alg_arg,
2065 int expected_status_arg )
2066{
2067 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002068 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002069 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002070 psa_status_t status;
2071
Gilles Peskine8817f612018-12-18 00:18:46 +01002072 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002073
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002074 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002075 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002076
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002077 /* Whether setup succeeded or failed, abort must succeed. */
2078 PSA_ASSERT( psa_hash_abort( &operation ) );
2079
2080 /* If setup failed, reproduce the failure, so as to
2081 * test the resulting state of the operation object. */
2082 if( status != PSA_SUCCESS )
2083 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2084
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002085 /* Now the operation object should be reusable. */
2086#if defined(KNOWN_SUPPORTED_HASH_ALG)
2087 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2088 PSA_ASSERT( psa_hash_abort( &operation ) );
2089#endif
2090
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002091exit:
2092 mbedtls_psa_crypto_free( );
2093}
2094/* END_CASE */
2095
2096/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002097void hash_bad_order( )
2098{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002099 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002100 unsigned char input[] = "";
2101 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002102 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002103 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2104 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2105 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002106 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002107 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002108 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002109
Gilles Peskine8817f612018-12-18 00:18:46 +01002110 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002111
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002112 /* Call setup twice in a row. */
2113 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2114 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2115 PSA_ERROR_BAD_STATE );
2116 PSA_ASSERT( psa_hash_abort( &operation ) );
2117
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002118 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002119 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002120 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002121 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002122
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002123 /* Call update after finish. */
2124 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2125 PSA_ASSERT( psa_hash_finish( &operation,
2126 hash, sizeof( hash ), &hash_len ) );
2127 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002128 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002129 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002130
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002131 /* Call verify without calling setup beforehand. */
2132 TEST_EQUAL( psa_hash_verify( &operation,
2133 valid_hash, sizeof( valid_hash ) ),
2134 PSA_ERROR_BAD_STATE );
2135 PSA_ASSERT( psa_hash_abort( &operation ) );
2136
2137 /* Call verify after finish. */
2138 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2139 PSA_ASSERT( psa_hash_finish( &operation,
2140 hash, sizeof( hash ), &hash_len ) );
2141 TEST_EQUAL( psa_hash_verify( &operation,
2142 valid_hash, sizeof( valid_hash ) ),
2143 PSA_ERROR_BAD_STATE );
2144 PSA_ASSERT( psa_hash_abort( &operation ) );
2145
2146 /* Call verify twice in a row. */
2147 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2148 PSA_ASSERT( psa_hash_verify( &operation,
2149 valid_hash, sizeof( valid_hash ) ) );
2150 TEST_EQUAL( psa_hash_verify( &operation,
2151 valid_hash, sizeof( valid_hash ) ),
2152 PSA_ERROR_BAD_STATE );
2153 PSA_ASSERT( psa_hash_abort( &operation ) );
2154
2155 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002156 TEST_EQUAL( psa_hash_finish( &operation,
2157 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002158 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002159 PSA_ASSERT( psa_hash_abort( &operation ) );
2160
2161 /* Call finish twice in a row. */
2162 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2163 PSA_ASSERT( psa_hash_finish( &operation,
2164 hash, sizeof( hash ), &hash_len ) );
2165 TEST_EQUAL( psa_hash_finish( &operation,
2166 hash, sizeof( hash ), &hash_len ),
2167 PSA_ERROR_BAD_STATE );
2168 PSA_ASSERT( psa_hash_abort( &operation ) );
2169
2170 /* Call finish after calling verify. */
2171 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2172 PSA_ASSERT( psa_hash_verify( &operation,
2173 valid_hash, sizeof( valid_hash ) ) );
2174 TEST_EQUAL( psa_hash_finish( &operation,
2175 hash, sizeof( hash ), &hash_len ),
2176 PSA_ERROR_BAD_STATE );
2177 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002178
2179exit:
2180 mbedtls_psa_crypto_free( );
2181}
2182/* END_CASE */
2183
itayzafrir27e69452018-11-01 14:26:34 +02002184/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2185void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002186{
2187 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002188 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2189 * appended to it */
2190 unsigned char hash[] = {
2191 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2192 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2193 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002194 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002195 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002196
Gilles Peskine8817f612018-12-18 00:18:46 +01002197 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002198
itayzafrir27e69452018-11-01 14:26:34 +02002199 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002200 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002201 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002202 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002203
itayzafrir27e69452018-11-01 14:26:34 +02002204 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002205 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002206 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002207 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002208
itayzafrir27e69452018-11-01 14:26:34 +02002209 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002210 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002211 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002212 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002213
itayzafrirec93d302018-10-18 18:01:10 +03002214exit:
2215 mbedtls_psa_crypto_free( );
2216}
2217/* END_CASE */
2218
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002219/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2220void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002221{
2222 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002223 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002224 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002225 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002226 size_t hash_len;
2227
Gilles Peskine8817f612018-12-18 00:18:46 +01002228 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002229
itayzafrir58028322018-10-25 10:22:01 +03002230 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002231 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002232 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002233 hash, expected_size - 1, &hash_len ),
2234 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002235
2236exit:
2237 mbedtls_psa_crypto_free( );
2238}
2239/* END_CASE */
2240
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002241/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2242void hash_clone_source_state( )
2243{
2244 psa_algorithm_t alg = PSA_ALG_SHA_256;
2245 unsigned char hash[PSA_HASH_MAX_SIZE];
2246 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2247 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2248 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2249 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2250 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2251 size_t hash_len;
2252
2253 PSA_ASSERT( psa_crypto_init( ) );
2254 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2255
2256 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2257 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2258 PSA_ASSERT( psa_hash_finish( &op_finished,
2259 hash, sizeof( hash ), &hash_len ) );
2260 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2261 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2262
2263 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2264 PSA_ERROR_BAD_STATE );
2265
2266 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2267 PSA_ASSERT( psa_hash_finish( &op_init,
2268 hash, sizeof( hash ), &hash_len ) );
2269 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2270 PSA_ASSERT( psa_hash_finish( &op_finished,
2271 hash, sizeof( hash ), &hash_len ) );
2272 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2273 PSA_ASSERT( psa_hash_finish( &op_aborted,
2274 hash, sizeof( hash ), &hash_len ) );
2275
2276exit:
2277 psa_hash_abort( &op_source );
2278 psa_hash_abort( &op_init );
2279 psa_hash_abort( &op_setup );
2280 psa_hash_abort( &op_finished );
2281 psa_hash_abort( &op_aborted );
2282 mbedtls_psa_crypto_free( );
2283}
2284/* END_CASE */
2285
2286/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2287void hash_clone_target_state( )
2288{
2289 psa_algorithm_t alg = PSA_ALG_SHA_256;
2290 unsigned char hash[PSA_HASH_MAX_SIZE];
2291 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2292 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2293 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2294 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2295 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2296 size_t hash_len;
2297
2298 PSA_ASSERT( psa_crypto_init( ) );
2299
2300 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2301 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2302 PSA_ASSERT( psa_hash_finish( &op_finished,
2303 hash, sizeof( hash ), &hash_len ) );
2304 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2305 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2306
2307 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2308 PSA_ASSERT( psa_hash_finish( &op_target,
2309 hash, sizeof( hash ), &hash_len ) );
2310
2311 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2312 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2313 PSA_ERROR_BAD_STATE );
2314 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2315 PSA_ERROR_BAD_STATE );
2316
2317exit:
2318 psa_hash_abort( &op_target );
2319 psa_hash_abort( &op_init );
2320 psa_hash_abort( &op_setup );
2321 psa_hash_abort( &op_finished );
2322 psa_hash_abort( &op_aborted );
2323 mbedtls_psa_crypto_free( );
2324}
2325/* END_CASE */
2326
itayzafrir58028322018-10-25 10:22:01 +03002327/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002328void mac_operation_init( )
2329{
Jaeden Amero252ef282019-02-15 14:05:35 +00002330 const uint8_t input[1] = { 0 };
2331
Jaeden Amero769ce272019-01-04 11:48:03 +00002332 /* Test each valid way of initializing the object, except for `= {0}`, as
2333 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2334 * though it's OK by the C standard. We could test for this, but we'd need
2335 * to supress the Clang warning for the test. */
2336 psa_mac_operation_t func = psa_mac_operation_init( );
2337 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2338 psa_mac_operation_t zero;
2339
2340 memset( &zero, 0, sizeof( zero ) );
2341
Jaeden Amero252ef282019-02-15 14:05:35 +00002342 /* A freshly-initialized MAC operation should not be usable. */
2343 TEST_EQUAL( psa_mac_update( &func,
2344 input, sizeof( input ) ),
2345 PSA_ERROR_BAD_STATE );
2346 TEST_EQUAL( psa_mac_update( &init,
2347 input, sizeof( input ) ),
2348 PSA_ERROR_BAD_STATE );
2349 TEST_EQUAL( psa_mac_update( &zero,
2350 input, sizeof( input ) ),
2351 PSA_ERROR_BAD_STATE );
2352
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002353 /* A default MAC operation should be abortable without error. */
2354 PSA_ASSERT( psa_mac_abort( &func ) );
2355 PSA_ASSERT( psa_mac_abort( &init ) );
2356 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002357}
2358/* END_CASE */
2359
2360/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002361void mac_setup( int key_type_arg,
2362 data_t *key,
2363 int alg_arg,
2364 int expected_status_arg )
2365{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002366 psa_key_type_t key_type = key_type_arg;
2367 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002368 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002369 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002370 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2371#if defined(KNOWN_SUPPORTED_MAC_ALG)
2372 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2373#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002374
Gilles Peskine8817f612018-12-18 00:18:46 +01002375 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002376
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002377 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2378 &operation, &status ) )
2379 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002380 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002381
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002382 /* The operation object should be reusable. */
2383#if defined(KNOWN_SUPPORTED_MAC_ALG)
2384 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2385 smoke_test_key_data,
2386 sizeof( smoke_test_key_data ),
2387 KNOWN_SUPPORTED_MAC_ALG,
2388 &operation, &status ) )
2389 goto exit;
2390 TEST_EQUAL( status, PSA_SUCCESS );
2391#endif
2392
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002393exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002394 mbedtls_psa_crypto_free( );
2395}
2396/* END_CASE */
2397
2398/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002399void mac_bad_order( )
2400{
2401 psa_key_handle_t handle = 0;
2402 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2403 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2404 const uint8_t key[] = {
2405 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2406 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2407 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002409 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2410 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2411 size_t sign_mac_length = 0;
2412 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2413 const uint8_t verify_mac[] = {
2414 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2415 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2416 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2417
2418 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002419 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2420 psa_set_key_algorithm( &attributes, alg );
2421 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002422
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002423 PSA_ASSERT( psa_import_key( &attributes, &handle,
Jaeden Amero252ef282019-02-15 14:05:35 +00002424 key, sizeof(key) ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002425
Jaeden Amero252ef282019-02-15 14:05:35 +00002426 /* Call update without calling setup beforehand. */
2427 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2428 PSA_ERROR_BAD_STATE );
2429 PSA_ASSERT( psa_mac_abort( &operation ) );
2430
2431 /* Call sign finish without calling setup beforehand. */
2432 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2433 &sign_mac_length),
2434 PSA_ERROR_BAD_STATE );
2435 PSA_ASSERT( psa_mac_abort( &operation ) );
2436
2437 /* Call verify finish without calling setup beforehand. */
2438 TEST_EQUAL( psa_mac_verify_finish( &operation,
2439 verify_mac, sizeof( verify_mac ) ),
2440 PSA_ERROR_BAD_STATE );
2441 PSA_ASSERT( psa_mac_abort( &operation ) );
2442
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002443 /* Call setup twice in a row. */
2444 PSA_ASSERT( psa_mac_sign_setup( &operation,
2445 handle, alg ) );
2446 TEST_EQUAL( psa_mac_sign_setup( &operation,
2447 handle, alg ),
2448 PSA_ERROR_BAD_STATE );
2449 PSA_ASSERT( psa_mac_abort( &operation ) );
2450
Jaeden Amero252ef282019-02-15 14:05:35 +00002451 /* Call update after sign finish. */
2452 PSA_ASSERT( psa_mac_sign_setup( &operation,
2453 handle, alg ) );
2454 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2455 PSA_ASSERT( psa_mac_sign_finish( &operation,
2456 sign_mac, sizeof( sign_mac ),
2457 &sign_mac_length ) );
2458 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2459 PSA_ERROR_BAD_STATE );
2460 PSA_ASSERT( psa_mac_abort( &operation ) );
2461
2462 /* Call update after verify finish. */
2463 PSA_ASSERT( psa_mac_verify_setup( &operation,
2464 handle, alg ) );
2465 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2466 PSA_ASSERT( psa_mac_verify_finish( &operation,
2467 verify_mac, sizeof( verify_mac ) ) );
2468 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2469 PSA_ERROR_BAD_STATE );
2470 PSA_ASSERT( psa_mac_abort( &operation ) );
2471
2472 /* Call sign finish twice in a row. */
2473 PSA_ASSERT( psa_mac_sign_setup( &operation,
2474 handle, alg ) );
2475 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2476 PSA_ASSERT( psa_mac_sign_finish( &operation,
2477 sign_mac, sizeof( sign_mac ),
2478 &sign_mac_length ) );
2479 TEST_EQUAL( psa_mac_sign_finish( &operation,
2480 sign_mac, sizeof( sign_mac ),
2481 &sign_mac_length ),
2482 PSA_ERROR_BAD_STATE );
2483 PSA_ASSERT( psa_mac_abort( &operation ) );
2484
2485 /* Call verify finish twice in a row. */
2486 PSA_ASSERT( psa_mac_verify_setup( &operation,
2487 handle, alg ) );
2488 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2489 PSA_ASSERT( psa_mac_verify_finish( &operation,
2490 verify_mac, sizeof( verify_mac ) ) );
2491 TEST_EQUAL( psa_mac_verify_finish( &operation,
2492 verify_mac, sizeof( verify_mac ) ),
2493 PSA_ERROR_BAD_STATE );
2494 PSA_ASSERT( psa_mac_abort( &operation ) );
2495
2496 /* Setup sign but try verify. */
2497 PSA_ASSERT( psa_mac_sign_setup( &operation,
2498 handle, alg ) );
2499 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2500 TEST_EQUAL( psa_mac_verify_finish( &operation,
2501 verify_mac, sizeof( verify_mac ) ),
2502 PSA_ERROR_BAD_STATE );
2503 PSA_ASSERT( psa_mac_abort( &operation ) );
2504
2505 /* Setup verify but try sign. */
2506 PSA_ASSERT( psa_mac_verify_setup( &operation,
2507 handle, alg ) );
2508 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2509 TEST_EQUAL( psa_mac_sign_finish( &operation,
2510 sign_mac, sizeof( sign_mac ),
2511 &sign_mac_length ),
2512 PSA_ERROR_BAD_STATE );
2513 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002514
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002515exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002516 mbedtls_psa_crypto_free( );
2517}
2518/* END_CASE */
2519
2520/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002521void mac_sign( int key_type_arg,
2522 data_t *key,
2523 int alg_arg,
2524 data_t *input,
2525 data_t *expected_mac )
2526{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002527 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002528 psa_key_type_t key_type = key_type_arg;
2529 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002530 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002531 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002532 /* Leave a little extra room in the output buffer. At the end of the
2533 * test, we'll check that the implementation didn't overwrite onto
2534 * this extra room. */
2535 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2536 size_t mac_buffer_size =
2537 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2538 size_t mac_length = 0;
2539
2540 memset( actual_mac, '+', sizeof( actual_mac ) );
2541 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2542 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2543
Gilles Peskine8817f612018-12-18 00:18:46 +01002544 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002545
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002546 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2547 psa_set_key_algorithm( &attributes, alg );
2548 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002549
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002550 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002551 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002552
2553 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002554 PSA_ASSERT( psa_mac_sign_setup( &operation,
2555 handle, alg ) );
2556 PSA_ASSERT( psa_mac_update( &operation,
2557 input->x, input->len ) );
2558 PSA_ASSERT( psa_mac_sign_finish( &operation,
2559 actual_mac, mac_buffer_size,
2560 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002561
2562 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002563 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2564 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002565
2566 /* Verify that the end of the buffer is untouched. */
2567 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2568 sizeof( actual_mac ) - mac_length ) );
2569
2570exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002571 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002572 mbedtls_psa_crypto_free( );
2573}
2574/* END_CASE */
2575
2576/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002577void mac_verify( int key_type_arg,
2578 data_t *key,
2579 int alg_arg,
2580 data_t *input,
2581 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002583 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002584 psa_key_type_t key_type = key_type_arg;
2585 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002586 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002587 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588
Gilles Peskine69c12672018-06-28 00:07:19 +02002589 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2590
Gilles Peskine8817f612018-12-18 00:18:46 +01002591 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002593 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2594 psa_set_key_algorithm( &attributes, alg );
2595 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002596
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002597 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002598 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002599
Gilles Peskine8817f612018-12-18 00:18:46 +01002600 PSA_ASSERT( psa_mac_verify_setup( &operation,
2601 handle, alg ) );
2602 PSA_ASSERT( psa_destroy_key( handle ) );
2603 PSA_ASSERT( psa_mac_update( &operation,
2604 input->x, input->len ) );
2605 PSA_ASSERT( psa_mac_verify_finish( &operation,
2606 expected_mac->x,
2607 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608
2609exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002610 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611 mbedtls_psa_crypto_free( );
2612}
2613/* END_CASE */
2614
2615/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002616void cipher_operation_init( )
2617{
Jaeden Ameroab439972019-02-15 14:12:05 +00002618 const uint8_t input[1] = { 0 };
2619 unsigned char output[1] = { 0 };
2620 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002621 /* Test each valid way of initializing the object, except for `= {0}`, as
2622 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2623 * though it's OK by the C standard. We could test for this, but we'd need
2624 * to supress the Clang warning for the test. */
2625 psa_cipher_operation_t func = psa_cipher_operation_init( );
2626 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2627 psa_cipher_operation_t zero;
2628
2629 memset( &zero, 0, sizeof( zero ) );
2630
Jaeden Ameroab439972019-02-15 14:12:05 +00002631 /* A freshly-initialized cipher operation should not be usable. */
2632 TEST_EQUAL( psa_cipher_update( &func,
2633 input, sizeof( input ),
2634 output, sizeof( output ),
2635 &output_length ),
2636 PSA_ERROR_BAD_STATE );
2637 TEST_EQUAL( psa_cipher_update( &init,
2638 input, sizeof( input ),
2639 output, sizeof( output ),
2640 &output_length ),
2641 PSA_ERROR_BAD_STATE );
2642 TEST_EQUAL( psa_cipher_update( &zero,
2643 input, sizeof( input ),
2644 output, sizeof( output ),
2645 &output_length ),
2646 PSA_ERROR_BAD_STATE );
2647
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002648 /* A default cipher operation should be abortable without error. */
2649 PSA_ASSERT( psa_cipher_abort( &func ) );
2650 PSA_ASSERT( psa_cipher_abort( &init ) );
2651 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002652}
2653/* END_CASE */
2654
2655/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002656void cipher_setup( int key_type_arg,
2657 data_t *key,
2658 int alg_arg,
2659 int expected_status_arg )
2660{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002661 psa_key_type_t key_type = key_type_arg;
2662 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002663 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002664 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002665 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002666#if defined(KNOWN_SUPPORTED_MAC_ALG)
2667 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2668#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002669
Gilles Peskine8817f612018-12-18 00:18:46 +01002670 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002671
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002672 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2673 &operation, &status ) )
2674 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002675 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002676
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002677 /* The operation object should be reusable. */
2678#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2679 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2680 smoke_test_key_data,
2681 sizeof( smoke_test_key_data ),
2682 KNOWN_SUPPORTED_CIPHER_ALG,
2683 &operation, &status ) )
2684 goto exit;
2685 TEST_EQUAL( status, PSA_SUCCESS );
2686#endif
2687
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002688exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002689 mbedtls_psa_crypto_free( );
2690}
2691/* END_CASE */
2692
2693/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002694void cipher_bad_order( )
2695{
2696 psa_key_handle_t handle = 0;
2697 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2698 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002700 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2701 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2702 const uint8_t key[] = {
2703 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2704 0xaa, 0xaa, 0xaa, 0xaa };
2705 const uint8_t text[] = {
2706 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2707 0xbb, 0xbb, 0xbb, 0xbb };
2708 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2709 size_t length = 0;
2710
2711 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002712 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2713 psa_set_key_algorithm( &attributes, alg );
2714 psa_set_key_type( &attributes, key_type );
2715 PSA_ASSERT( psa_import_key( &attributes, &handle,
Jaeden Ameroab439972019-02-15 14:12:05 +00002716 key, sizeof(key) ) );
2717
2718
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002719 /* Call encrypt setup twice in a row. */
2720 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2721 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2722 PSA_ERROR_BAD_STATE );
2723 PSA_ASSERT( psa_cipher_abort( &operation ) );
2724
2725 /* Call decrypt setup twice in a row. */
2726 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2727 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2728 PSA_ERROR_BAD_STATE );
2729 PSA_ASSERT( psa_cipher_abort( &operation ) );
2730
Jaeden Ameroab439972019-02-15 14:12:05 +00002731 /* Generate an IV without calling setup beforehand. */
2732 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2733 buffer, sizeof( buffer ),
2734 &length ),
2735 PSA_ERROR_BAD_STATE );
2736 PSA_ASSERT( psa_cipher_abort( &operation ) );
2737
2738 /* Generate an IV twice in a row. */
2739 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2740 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2741 buffer, sizeof( buffer ),
2742 &length ) );
2743 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2744 buffer, sizeof( buffer ),
2745 &length ),
2746 PSA_ERROR_BAD_STATE );
2747 PSA_ASSERT( psa_cipher_abort( &operation ) );
2748
2749 /* Generate an IV after it's already set. */
2750 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2751 PSA_ASSERT( psa_cipher_set_iv( &operation,
2752 iv, sizeof( iv ) ) );
2753 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2754 buffer, sizeof( buffer ),
2755 &length ),
2756 PSA_ERROR_BAD_STATE );
2757 PSA_ASSERT( psa_cipher_abort( &operation ) );
2758
2759 /* Set an IV without calling setup beforehand. */
2760 TEST_EQUAL( psa_cipher_set_iv( &operation,
2761 iv, sizeof( iv ) ),
2762 PSA_ERROR_BAD_STATE );
2763 PSA_ASSERT( psa_cipher_abort( &operation ) );
2764
2765 /* Set an IV after it's already set. */
2766 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2767 PSA_ASSERT( psa_cipher_set_iv( &operation,
2768 iv, sizeof( iv ) ) );
2769 TEST_EQUAL( psa_cipher_set_iv( &operation,
2770 iv, sizeof( iv ) ),
2771 PSA_ERROR_BAD_STATE );
2772 PSA_ASSERT( psa_cipher_abort( &operation ) );
2773
2774 /* Set an IV after it's already generated. */
2775 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2776 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2777 buffer, sizeof( buffer ),
2778 &length ) );
2779 TEST_EQUAL( psa_cipher_set_iv( &operation,
2780 iv, sizeof( iv ) ),
2781 PSA_ERROR_BAD_STATE );
2782 PSA_ASSERT( psa_cipher_abort( &operation ) );
2783
2784 /* Call update without calling setup beforehand. */
2785 TEST_EQUAL( psa_cipher_update( &operation,
2786 text, sizeof( text ),
2787 buffer, sizeof( buffer ),
2788 &length ),
2789 PSA_ERROR_BAD_STATE );
2790 PSA_ASSERT( psa_cipher_abort( &operation ) );
2791
2792 /* Call update without an IV where an IV is required. */
2793 TEST_EQUAL( psa_cipher_update( &operation,
2794 text, sizeof( text ),
2795 buffer, sizeof( buffer ),
2796 &length ),
2797 PSA_ERROR_BAD_STATE );
2798 PSA_ASSERT( psa_cipher_abort( &operation ) );
2799
2800 /* Call update after finish. */
2801 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2802 PSA_ASSERT( psa_cipher_set_iv( &operation,
2803 iv, sizeof( iv ) ) );
2804 PSA_ASSERT( psa_cipher_finish( &operation,
2805 buffer, sizeof( buffer ), &length ) );
2806 TEST_EQUAL( psa_cipher_update( &operation,
2807 text, sizeof( text ),
2808 buffer, sizeof( buffer ),
2809 &length ),
2810 PSA_ERROR_BAD_STATE );
2811 PSA_ASSERT( psa_cipher_abort( &operation ) );
2812
2813 /* Call finish without calling setup beforehand. */
2814 TEST_EQUAL( psa_cipher_finish( &operation,
2815 buffer, sizeof( buffer ), &length ),
2816 PSA_ERROR_BAD_STATE );
2817 PSA_ASSERT( psa_cipher_abort( &operation ) );
2818
2819 /* Call finish without an IV where an IV is required. */
2820 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2821 /* Not calling update means we are encrypting an empty buffer, which is OK
2822 * for cipher modes with padding. */
2823 TEST_EQUAL( psa_cipher_finish( &operation,
2824 buffer, sizeof( buffer ), &length ),
2825 PSA_ERROR_BAD_STATE );
2826 PSA_ASSERT( psa_cipher_abort( &operation ) );
2827
2828 /* Call finish twice in a row. */
2829 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2830 PSA_ASSERT( psa_cipher_set_iv( &operation,
2831 iv, sizeof( iv ) ) );
2832 PSA_ASSERT( psa_cipher_finish( &operation,
2833 buffer, sizeof( buffer ), &length ) );
2834 TEST_EQUAL( psa_cipher_finish( &operation,
2835 buffer, sizeof( buffer ), &length ),
2836 PSA_ERROR_BAD_STATE );
2837 PSA_ASSERT( psa_cipher_abort( &operation ) );
2838
2839exit:
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002840 mbedtls_psa_crypto_free( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002841}
2842/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002843
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002845void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002846 data_t *key,
2847 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002848 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002849{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002850 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002851 psa_status_t status;
2852 psa_key_type_t key_type = key_type_arg;
2853 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002854 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002855 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002856 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002857 unsigned char *output = NULL;
2858 size_t output_buffer_size = 0;
2859 size_t function_output_length = 0;
2860 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002861 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002862 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002863
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002864 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2865 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002866
Gilles Peskine8817f612018-12-18 00:18:46 +01002867 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002868
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002869 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2870 psa_set_key_algorithm( &attributes, alg );
2871 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002872
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002873 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002874 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875
Gilles Peskine8817f612018-12-18 00:18:46 +01002876 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2877 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002878
Gilles Peskine8817f612018-12-18 00:18:46 +01002879 PSA_ASSERT( psa_cipher_set_iv( &operation,
2880 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002881 output_buffer_size = ( (size_t) input->len +
2882 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002883 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884
Gilles Peskine8817f612018-12-18 00:18:46 +01002885 PSA_ASSERT( psa_cipher_update( &operation,
2886 input->x, input->len,
2887 output, output_buffer_size,
2888 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889 total_output_length += function_output_length;
2890 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002891 output + total_output_length,
2892 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002893 &function_output_length );
2894 total_output_length += function_output_length;
2895
Gilles Peskinefe11b722018-12-18 00:24:04 +01002896 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002897 if( expected_status == PSA_SUCCESS )
2898 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002899 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002900 ASSERT_COMPARE( expected_output->x, expected_output->len,
2901 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002902 }
2903
2904exit:
2905 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002906 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002907 mbedtls_psa_crypto_free( );
2908}
2909/* END_CASE */
2910
2911/* BEGIN_CASE */
2912void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
2913 data_t *key,
2914 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002915 int first_part_size_arg,
2916 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002917 data_t *expected_output )
2918{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002919 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920 psa_key_type_t key_type = key_type_arg;
2921 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002922 size_t first_part_size = first_part_size_arg;
2923 size_t output1_length = output1_length_arg;
2924 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002925 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002926 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927 unsigned char *output = NULL;
2928 size_t output_buffer_size = 0;
2929 size_t function_output_length = 0;
2930 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002931 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002933
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002934 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2935 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002936
Gilles Peskine8817f612018-12-18 00:18:46 +01002937 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002938
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002939 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2940 psa_set_key_algorithm( &attributes, alg );
2941 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002942
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002943 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002944 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002945
Gilles Peskine8817f612018-12-18 00:18:46 +01002946 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2947 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002948
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 PSA_ASSERT( psa_cipher_set_iv( &operation,
2950 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002951 output_buffer_size = ( (size_t) input->len +
2952 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002953 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002954
Gilles Peskinee0866522019-02-19 19:44:00 +01002955 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002956 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2957 output, output_buffer_size,
2958 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002959 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002960 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002961 PSA_ASSERT( psa_cipher_update( &operation,
2962 input->x + first_part_size,
2963 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002964 output + total_output_length,
2965 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002966 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002967 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002968 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002969 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002970 output + total_output_length,
2971 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002972 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002973 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002974 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002975
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002976 ASSERT_COMPARE( expected_output->x, expected_output->len,
2977 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002978
2979exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002980 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002981 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002982 mbedtls_psa_crypto_free( );
2983}
2984/* END_CASE */
2985
2986/* BEGIN_CASE */
2987void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002988 data_t *key,
2989 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002990 int first_part_size_arg,
2991 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002992 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002993{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002994 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002995
2996 psa_key_type_t key_type = key_type_arg;
2997 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002998 size_t first_part_size = first_part_size_arg;
2999 size_t output1_length = output1_length_arg;
3000 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003001 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003002 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003003 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003004 size_t output_buffer_size = 0;
3005 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003006 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003007 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003008 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003009
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003010 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3011 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003012
Gilles Peskine8817f612018-12-18 00:18:46 +01003013 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003014
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003015 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3016 psa_set_key_algorithm( &attributes, alg );
3017 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003018
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003019 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003020 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003021
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3023 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003024
Gilles Peskine8817f612018-12-18 00:18:46 +01003025 PSA_ASSERT( psa_cipher_set_iv( &operation,
3026 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003027
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003028 output_buffer_size = ( (size_t) input->len +
3029 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003030 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003031
Gilles Peskinee0866522019-02-19 19:44:00 +01003032 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003033 PSA_ASSERT( psa_cipher_update( &operation,
3034 input->x, first_part_size,
3035 output, output_buffer_size,
3036 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003037 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003038 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003039 PSA_ASSERT( psa_cipher_update( &operation,
3040 input->x + first_part_size,
3041 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003042 output + total_output_length,
3043 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003044 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003045 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003046 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003047 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003048 output + total_output_length,
3049 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003050 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003051 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003052 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003053
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003054 ASSERT_COMPARE( expected_output->x, expected_output->len,
3055 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003056
3057exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003058 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003059 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003060 mbedtls_psa_crypto_free( );
3061}
3062/* END_CASE */
3063
Gilles Peskine50e586b2018-06-08 14:28:46 +02003064/* BEGIN_CASE */
3065void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003066 data_t *key,
3067 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003068 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003069{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003070 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003071 psa_status_t status;
3072 psa_key_type_t key_type = key_type_arg;
3073 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003074 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003075 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003076 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003077 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003078 size_t output_buffer_size = 0;
3079 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003080 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003081 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003083
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003084 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3085 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003086
Gilles Peskine8817f612018-12-18 00:18:46 +01003087 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003088
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003089 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3090 psa_set_key_algorithm( &attributes, alg );
3091 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003092
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003093 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003094 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003095
Gilles Peskine8817f612018-12-18 00:18:46 +01003096 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3097 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003098
Gilles Peskine8817f612018-12-18 00:18:46 +01003099 PSA_ASSERT( psa_cipher_set_iv( &operation,
3100 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003101
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003102 output_buffer_size = ( (size_t) input->len +
3103 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003104 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003105
Gilles Peskine8817f612018-12-18 00:18:46 +01003106 PSA_ASSERT( psa_cipher_update( &operation,
3107 input->x, input->len,
3108 output, output_buffer_size,
3109 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003110 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003111 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003112 output + total_output_length,
3113 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003114 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003115 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003116 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003117
3118 if( expected_status == PSA_SUCCESS )
3119 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003120 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003121 ASSERT_COMPARE( expected_output->x, expected_output->len,
3122 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003123 }
3124
Gilles Peskine50e586b2018-06-08 14:28:46 +02003125exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003126 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003127 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003128 mbedtls_psa_crypto_free( );
3129}
3130/* END_CASE */
3131
Gilles Peskine50e586b2018-06-08 14:28:46 +02003132/* BEGIN_CASE */
3133void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003134 data_t *key,
3135 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003136{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003137 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003138 psa_key_type_t key_type = key_type_arg;
3139 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003140 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003141 size_t iv_size = 16;
3142 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003143 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003144 size_t output1_size = 0;
3145 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003146 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003147 size_t output2_size = 0;
3148 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003149 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003150 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3151 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003153
Gilles Peskine8817f612018-12-18 00:18:46 +01003154 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003155
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003156 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3157 psa_set_key_algorithm( &attributes, alg );
3158 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003159
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003160 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003161 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003162
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3164 handle, alg ) );
3165 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3166 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003167
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3169 iv, iv_size,
3170 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003171 output1_size = ( (size_t) input->len +
3172 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003173 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003174
Gilles Peskine8817f612018-12-18 00:18:46 +01003175 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3176 output1, output1_size,
3177 &output1_length ) );
3178 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003179 output1 + output1_length,
3180 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003181 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003182
Gilles Peskine048b7f02018-06-08 14:20:49 +02003183 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003184
Gilles Peskine8817f612018-12-18 00:18:46 +01003185 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003186
3187 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003188 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003189
Gilles Peskine8817f612018-12-18 00:18:46 +01003190 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3191 iv, iv_length ) );
3192 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3193 output2, output2_size,
3194 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003195 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003196 PSA_ASSERT( psa_cipher_finish( &operation2,
3197 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003198 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003199 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003200
Gilles Peskine048b7f02018-06-08 14:20:49 +02003201 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003202
Gilles Peskine8817f612018-12-18 00:18:46 +01003203 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003204
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003205 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003206
3207exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003208 mbedtls_free( output1 );
3209 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003210 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003211 mbedtls_psa_crypto_free( );
3212}
3213/* END_CASE */
3214
3215/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003216void cipher_verify_output_multipart( int alg_arg,
3217 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003218 data_t *key,
3219 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003220 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003221{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003222 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003223 psa_key_type_t key_type = key_type_arg;
3224 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003225 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003226 unsigned char iv[16] = {0};
3227 size_t iv_size = 16;
3228 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003229 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003230 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003231 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003232 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003233 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003234 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003235 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003236 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3237 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003239
Gilles Peskine8817f612018-12-18 00:18:46 +01003240 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003241
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003242 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3243 psa_set_key_algorithm( &attributes, alg );
3244 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003245
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003246 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003247 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003248
Gilles Peskine8817f612018-12-18 00:18:46 +01003249 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3250 handle, alg ) );
3251 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3252 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003253
Gilles Peskine8817f612018-12-18 00:18:46 +01003254 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3255 iv, iv_size,
3256 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003257 output1_buffer_size = ( (size_t) input->len +
3258 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003259 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003260
Gilles Peskinee0866522019-02-19 19:44:00 +01003261 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003262
Gilles Peskine8817f612018-12-18 00:18:46 +01003263 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3264 output1, output1_buffer_size,
3265 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003266 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003267
Gilles Peskine8817f612018-12-18 00:18:46 +01003268 PSA_ASSERT( psa_cipher_update( &operation1,
3269 input->x + first_part_size,
3270 input->len - first_part_size,
3271 output1, output1_buffer_size,
3272 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003273 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003274
Gilles Peskine8817f612018-12-18 00:18:46 +01003275 PSA_ASSERT( psa_cipher_finish( &operation1,
3276 output1 + output1_length,
3277 output1_buffer_size - output1_length,
3278 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003279 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003280
Gilles Peskine8817f612018-12-18 00:18:46 +01003281 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003282
Gilles Peskine048b7f02018-06-08 14:20:49 +02003283 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003284 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003285
Gilles Peskine8817f612018-12-18 00:18:46 +01003286 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3287 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003288
Gilles Peskine8817f612018-12-18 00:18:46 +01003289 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3290 output2, output2_buffer_size,
3291 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003292 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003293
Gilles Peskine8817f612018-12-18 00:18:46 +01003294 PSA_ASSERT( psa_cipher_update( &operation2,
3295 output1 + first_part_size,
3296 output1_length - first_part_size,
3297 output2, output2_buffer_size,
3298 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003299 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003300
Gilles Peskine8817f612018-12-18 00:18:46 +01003301 PSA_ASSERT( psa_cipher_finish( &operation2,
3302 output2 + output2_length,
3303 output2_buffer_size - output2_length,
3304 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003305 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003306
Gilles Peskine8817f612018-12-18 00:18:46 +01003307 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003308
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003309 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003310
3311exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003312 mbedtls_free( output1 );
3313 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003314 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003315 mbedtls_psa_crypto_free( );
3316}
3317/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003318
Gilles Peskine20035e32018-02-03 22:44:14 +01003319/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003320void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003321 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003322 data_t *nonce,
3323 data_t *additional_data,
3324 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003325 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003326{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003327 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003328 psa_key_type_t key_type = key_type_arg;
3329 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003330 unsigned char *output_data = NULL;
3331 size_t output_size = 0;
3332 size_t output_length = 0;
3333 unsigned char *output_data2 = NULL;
3334 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003335 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003336 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003338
Gilles Peskine4abf7412018-06-18 16:35:34 +02003339 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003340 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341
Gilles Peskine8817f612018-12-18 00:18:46 +01003342 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003344 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3345 psa_set_key_algorithm( &attributes, alg );
3346 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003347
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003348 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003349 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003350
Gilles Peskinefe11b722018-12-18 00:24:04 +01003351 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3352 nonce->x, nonce->len,
3353 additional_data->x,
3354 additional_data->len,
3355 input_data->x, input_data->len,
3356 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003357 &output_length ),
3358 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003359
3360 if( PSA_SUCCESS == expected_result )
3361 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003362 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003363
Gilles Peskinefe11b722018-12-18 00:24:04 +01003364 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3365 nonce->x, nonce->len,
3366 additional_data->x,
3367 additional_data->len,
3368 output_data, output_length,
3369 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003370 &output_length2 ),
3371 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003372
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003373 ASSERT_COMPARE( input_data->x, input_data->len,
3374 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003375 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003376
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003378 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003379 mbedtls_free( output_data );
3380 mbedtls_free( output_data2 );
3381 mbedtls_psa_crypto_free( );
3382}
3383/* END_CASE */
3384
3385/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003386void aead_encrypt( int key_type_arg, data_t *key_data,
3387 int alg_arg,
3388 data_t *nonce,
3389 data_t *additional_data,
3390 data_t *input_data,
3391 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003392{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003393 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003394 psa_key_type_t key_type = key_type_arg;
3395 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003396 unsigned char *output_data = NULL;
3397 size_t output_size = 0;
3398 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003399 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003400 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003401
Gilles Peskine4abf7412018-06-18 16:35:34 +02003402 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003403 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003404
Gilles Peskine8817f612018-12-18 00:18:46 +01003405 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003407 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3408 psa_set_key_algorithm( &attributes, alg );
3409 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003410
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003411 PSA_ASSERT( psa_import_key( &attributes, &handle,
3412 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003413
Gilles Peskine8817f612018-12-18 00:18:46 +01003414 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3415 nonce->x, nonce->len,
3416 additional_data->x, additional_data->len,
3417 input_data->x, input_data->len,
3418 output_data, output_size,
3419 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003421 ASSERT_COMPARE( expected_result->x, expected_result->len,
3422 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003423
Gilles Peskinea1cac842018-06-11 19:33:02 +02003424exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003425 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003426 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003427 mbedtls_psa_crypto_free( );
3428}
3429/* END_CASE */
3430
3431/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003432void aead_decrypt( int key_type_arg, data_t *key_data,
3433 int alg_arg,
3434 data_t *nonce,
3435 data_t *additional_data,
3436 data_t *input_data,
3437 data_t *expected_data,
3438 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003439{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003440 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003441 psa_key_type_t key_type = key_type_arg;
3442 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003443 unsigned char *output_data = NULL;
3444 size_t output_size = 0;
3445 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003446 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003448 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003449
Gilles Peskine4abf7412018-06-18 16:35:34 +02003450 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003451 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003452
Gilles Peskine8817f612018-12-18 00:18:46 +01003453 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003454
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003455 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3456 psa_set_key_algorithm( &attributes, alg );
3457 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003458
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003459 PSA_ASSERT( psa_import_key( &attributes, &handle,
3460 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461
Gilles Peskinefe11b722018-12-18 00:24:04 +01003462 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3463 nonce->x, nonce->len,
3464 additional_data->x,
3465 additional_data->len,
3466 input_data->x, input_data->len,
3467 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003468 &output_length ),
3469 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470
Gilles Peskine2d277862018-06-18 15:41:12 +02003471 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003472 ASSERT_COMPARE( expected_data->x, expected_data->len,
3473 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003476 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003477 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478 mbedtls_psa_crypto_free( );
3479}
3480/* END_CASE */
3481
3482/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003483void signature_size( int type_arg,
3484 int bits,
3485 int alg_arg,
3486 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003487{
3488 psa_key_type_t type = type_arg;
3489 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003490 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003491 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003492exit:
3493 ;
3494}
3495/* END_CASE */
3496
3497/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003498void sign_deterministic( int key_type_arg, data_t *key_data,
3499 int alg_arg, data_t *input_data,
3500 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003501{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003502 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003503 psa_key_type_t key_type = key_type_arg;
3504 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003505 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003506 unsigned char *signature = NULL;
3507 size_t signature_size;
3508 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003509 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003510
Gilles Peskine8817f612018-12-18 00:18:46 +01003511 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003512
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003513 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3514 psa_set_key_algorithm( &attributes, alg );
3515 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003516
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003517 PSA_ASSERT( psa_import_key( &attributes, &handle,
3518 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003519 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3520 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003521
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003522 /* Allocate a buffer which has the size advertized by the
3523 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003524 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3525 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003526 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003527 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003528 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003529
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003530 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003531 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3532 input_data->x, input_data->len,
3533 signature, signature_size,
3534 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003535 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003536 ASSERT_COMPARE( output_data->x, output_data->len,
3537 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003538
3539exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003540 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003541 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003542 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003543 mbedtls_psa_crypto_free( );
3544}
3545/* END_CASE */
3546
3547/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003548void sign_fail( int key_type_arg, data_t *key_data,
3549 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003550 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003551{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003552 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003553 psa_key_type_t key_type = key_type_arg;
3554 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003555 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003556 psa_status_t actual_status;
3557 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003558 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003559 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003561
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003562 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003563
Gilles Peskine8817f612018-12-18 00:18:46 +01003564 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003565
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003566 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3567 psa_set_key_algorithm( &attributes, alg );
3568 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003569
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003570 PSA_ASSERT( psa_import_key( &attributes, &handle,
3571 key_data->x, key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003572
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003573 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003574 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003575 signature, signature_size,
3576 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003577 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003578 /* The value of *signature_length is unspecified on error, but
3579 * whatever it is, it should be less than signature_size, so that
3580 * if the caller tries to read *signature_length bytes without
3581 * checking the error code then they don't overflow a buffer. */
3582 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003583
3584exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003585 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003586 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003587 mbedtls_free( signature );
3588 mbedtls_psa_crypto_free( );
3589}
3590/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003591
3592/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003593void sign_verify( int key_type_arg, data_t *key_data,
3594 int alg_arg, data_t *input_data )
3595{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003596 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003597 psa_key_type_t key_type = key_type_arg;
3598 psa_algorithm_t alg = alg_arg;
3599 size_t key_bits;
3600 unsigned char *signature = NULL;
3601 size_t signature_size;
3602 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003604
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003606
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3608 psa_set_key_algorithm( &attributes, alg );
3609 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003610
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003611 PSA_ASSERT( psa_import_key( &attributes, &handle,
3612 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003613 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3614 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003615
3616 /* Allocate a buffer which has the size advertized by the
3617 * library. */
3618 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3619 key_bits, alg );
3620 TEST_ASSERT( signature_size != 0 );
3621 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003622 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003623
3624 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003625 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3626 input_data->x, input_data->len,
3627 signature, signature_size,
3628 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003629 /* Check that the signature length looks sensible. */
3630 TEST_ASSERT( signature_length <= signature_size );
3631 TEST_ASSERT( signature_length > 0 );
3632
3633 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003634 PSA_ASSERT( psa_asymmetric_verify(
3635 handle, alg,
3636 input_data->x, input_data->len,
3637 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003638
3639 if( input_data->len != 0 )
3640 {
3641 /* Flip a bit in the input and verify that the signature is now
3642 * detected as invalid. Flip a bit at the beginning, not at the end,
3643 * because ECDSA may ignore the last few bits of the input. */
3644 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003645 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3646 input_data->x, input_data->len,
3647 signature, signature_length ),
3648 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003649 }
3650
3651exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003652 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003653 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003654 mbedtls_free( signature );
3655 mbedtls_psa_crypto_free( );
3656}
3657/* END_CASE */
3658
3659/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003660void asymmetric_verify( int key_type_arg, data_t *key_data,
3661 int alg_arg, data_t *hash_data,
3662 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003663{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003664 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003665 psa_key_type_t key_type = key_type_arg;
3666 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003668
Gilles Peskine69c12672018-06-28 00:07:19 +02003669 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3670
Gilles Peskine8817f612018-12-18 00:18:46 +01003671 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003672
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003673 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3674 psa_set_key_algorithm( &attributes, alg );
3675 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003676
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003677 PSA_ASSERT( psa_import_key( &attributes, &handle,
3678 key_data->x, key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003679
Gilles Peskine8817f612018-12-18 00:18:46 +01003680 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3681 hash_data->x, hash_data->len,
3682 signature_data->x,
3683 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003684exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003685 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003686 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003687 mbedtls_psa_crypto_free( );
3688}
3689/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003690
3691/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003692void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3693 int alg_arg, data_t *hash_data,
3694 data_t *signature_data,
3695 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003696{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003697 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003698 psa_key_type_t key_type = key_type_arg;
3699 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003700 psa_status_t actual_status;
3701 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003703
Gilles Peskine8817f612018-12-18 00:18:46 +01003704 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003705
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003706 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3707 psa_set_key_algorithm( &attributes, alg );
3708 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003709
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003710 PSA_ASSERT( psa_import_key( &attributes, &handle,
3711 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003712
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003713 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003714 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003715 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003716 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003717
Gilles Peskinefe11b722018-12-18 00:24:04 +01003718 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003719
3720exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003721 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003722 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003723 mbedtls_psa_crypto_free( );
3724}
3725/* END_CASE */
3726
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003728void asymmetric_encrypt( int key_type_arg,
3729 data_t *key_data,
3730 int alg_arg,
3731 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003732 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003733 int expected_output_length_arg,
3734 int expected_status_arg )
3735{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003736 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003737 psa_key_type_t key_type = key_type_arg;
3738 psa_algorithm_t alg = alg_arg;
3739 size_t expected_output_length = expected_output_length_arg;
3740 size_t key_bits;
3741 unsigned char *output = NULL;
3742 size_t output_size;
3743 size_t output_length = ~0;
3744 psa_status_t actual_status;
3745 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003747
Gilles Peskine8817f612018-12-18 00:18:46 +01003748 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003749
Gilles Peskine656896e2018-06-29 19:12:28 +02003750 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003751 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3752 psa_set_key_algorithm( &attributes, alg );
3753 psa_set_key_type( &attributes, key_type );
3754 PSA_ASSERT( psa_import_key( &attributes, &handle,
3755 key_data->x, key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003756
3757 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003758 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3759 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003760 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003761 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003762
3763 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003764 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003765 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003766 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003767 output, output_size,
3768 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003769 TEST_EQUAL( actual_status, expected_status );
3770 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003771
Gilles Peskine68428122018-06-30 18:42:41 +02003772 /* If the label is empty, the test framework puts a non-null pointer
3773 * in label->x. Test that a null pointer works as well. */
3774 if( label->len == 0 )
3775 {
3776 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003777 if( output_size != 0 )
3778 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003779 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003780 input_data->x, input_data->len,
3781 NULL, label->len,
3782 output, output_size,
3783 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003784 TEST_EQUAL( actual_status, expected_status );
3785 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003786 }
3787
Gilles Peskine656896e2018-06-29 19:12:28 +02003788exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003789 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003790 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003791 mbedtls_free( output );
3792 mbedtls_psa_crypto_free( );
3793}
3794/* END_CASE */
3795
3796/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003797void asymmetric_encrypt_decrypt( int key_type_arg,
3798 data_t *key_data,
3799 int alg_arg,
3800 data_t *input_data,
3801 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003802{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003803 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003804 psa_key_type_t key_type = key_type_arg;
3805 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003806 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003807 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003808 size_t output_size;
3809 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003810 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003811 size_t output2_size;
3812 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003813 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003814
Gilles Peskine8817f612018-12-18 00:18:46 +01003815 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003816
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003817 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3818 psa_set_key_algorithm( &attributes, alg );
3819 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003820
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003821 PSA_ASSERT( psa_import_key( &attributes, &handle,
3822 key_data->x, key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003823
3824 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003825 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3826 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003827 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003828 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003829 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003830 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003831
Gilles Peskineeebd7382018-06-08 18:11:54 +02003832 /* We test encryption by checking that encrypt-then-decrypt gives back
3833 * the original plaintext because of the non-optional random
3834 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003835 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3836 input_data->x, input_data->len,
3837 label->x, label->len,
3838 output, output_size,
3839 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003840 /* We don't know what ciphertext length to expect, but check that
3841 * it looks sensible. */
3842 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003843
Gilles Peskine8817f612018-12-18 00:18:46 +01003844 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3845 output, output_length,
3846 label->x, label->len,
3847 output2, output2_size,
3848 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003849 ASSERT_COMPARE( input_data->x, input_data->len,
3850 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003851
3852exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003853 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003854 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003855 mbedtls_free( output );
3856 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003857 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003858}
3859/* END_CASE */
3860
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003861/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003862void asymmetric_decrypt( int key_type_arg,
3863 data_t *key_data,
3864 int alg_arg,
3865 data_t *input_data,
3866 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003867 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003868{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003869 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003870 psa_key_type_t key_type = key_type_arg;
3871 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003873 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003874 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003875 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876
Jaeden Amero412654a2019-02-06 12:57:46 +00003877 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003878 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003879
Gilles Peskine8817f612018-12-18 00:18:46 +01003880 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003882 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3883 psa_set_key_algorithm( &attributes, alg );
3884 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003885
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003886 PSA_ASSERT( psa_import_key( &attributes, &handle,
3887 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003888
Gilles Peskine8817f612018-12-18 00:18:46 +01003889 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3890 input_data->x, input_data->len,
3891 label->x, label->len,
3892 output,
3893 output_size,
3894 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003895 ASSERT_COMPARE( expected_data->x, expected_data->len,
3896 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003897
Gilles Peskine68428122018-06-30 18:42:41 +02003898 /* If the label is empty, the test framework puts a non-null pointer
3899 * in label->x. Test that a null pointer works as well. */
3900 if( label->len == 0 )
3901 {
3902 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003903 if( output_size != 0 )
3904 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003905 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3906 input_data->x, input_data->len,
3907 NULL, label->len,
3908 output,
3909 output_size,
3910 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003911 ASSERT_COMPARE( expected_data->x, expected_data->len,
3912 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003913 }
3914
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003915exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003916 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003917 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003918 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003919 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003920}
3921/* END_CASE */
3922
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003923/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003924void asymmetric_decrypt_fail( int key_type_arg,
3925 data_t *key_data,
3926 int alg_arg,
3927 data_t *input_data,
3928 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003929 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003930 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003932 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003933 psa_key_type_t key_type = key_type_arg;
3934 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003936 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003937 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003938 psa_status_t actual_status;
3939 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003942 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003943
Gilles Peskine8817f612018-12-18 00:18:46 +01003944 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003946 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3947 psa_set_key_algorithm( &attributes, alg );
3948 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003949
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003950 PSA_ASSERT( psa_import_key( &attributes, &handle,
3951 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003952
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003953 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003954 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003955 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003956 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003957 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003958 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003959 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003960
Gilles Peskine68428122018-06-30 18:42:41 +02003961 /* If the label is empty, the test framework puts a non-null pointer
3962 * in label->x. Test that a null pointer works as well. */
3963 if( label->len == 0 )
3964 {
3965 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003966 if( output_size != 0 )
3967 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003968 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003969 input_data->x, input_data->len,
3970 NULL, label->len,
3971 output, output_size,
3972 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003973 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003974 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003975 }
3976
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003978 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003979 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003980 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003981 mbedtls_psa_crypto_free( );
3982}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003983/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003984
3985/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003986void crypto_generator_init( )
3987{
3988 /* Test each valid way of initializing the object, except for `= {0}`, as
3989 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3990 * though it's OK by the C standard. We could test for this, but we'd need
3991 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003992 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003993 psa_crypto_generator_t func = psa_crypto_generator_init( );
3994 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3995 psa_crypto_generator_t zero;
3996
3997 memset( &zero, 0, sizeof( zero ) );
3998
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003999 /* A default generator should not be able to report its capacity. */
4000 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
4001 PSA_ERROR_BAD_STATE );
4002 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
4003 PSA_ERROR_BAD_STATE );
4004 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
4005 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004006
4007 /* A default generator should be abortable without error. */
4008 PSA_ASSERT( psa_generator_abort(&func) );
4009 PSA_ASSERT( psa_generator_abort(&init) );
4010 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004011}
4012/* END_CASE */
4013
4014/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004015void derive_setup( int key_type_arg,
4016 data_t *key_data,
4017 int alg_arg,
4018 data_t *salt,
4019 data_t *label,
4020 int requested_capacity_arg,
4021 int expected_status_arg )
4022{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004023 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004024 size_t key_type = key_type_arg;
4025 psa_algorithm_t alg = alg_arg;
4026 size_t requested_capacity = requested_capacity_arg;
4027 psa_status_t expected_status = expected_status_arg;
4028 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004030
Gilles Peskine8817f612018-12-18 00:18:46 +01004031 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004032
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4034 psa_set_key_algorithm( &attributes, alg );
4035 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004036
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004037 PSA_ASSERT( psa_import_key( &attributes, &handle,
4038 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004039
Gilles Peskinefe11b722018-12-18 00:24:04 +01004040 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4041 salt->x, salt->len,
4042 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004043 requested_capacity ),
4044 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004045
4046exit:
4047 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004048 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004049 mbedtls_psa_crypto_free( );
4050}
4051/* END_CASE */
4052
4053/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004054void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004055{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004056 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004057 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004058 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004059 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004060 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004061 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004062 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4063 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4064 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004065 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004066
Gilles Peskine8817f612018-12-18 00:18:46 +01004067 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004068
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004069 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4070 psa_set_key_algorithm( &attributes, alg );
4071 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004072
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004073 PSA_ASSERT( psa_import_key( &attributes, &handle,
4074 key_data, sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004075
4076 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004077 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4078 NULL, 0,
4079 NULL, 0,
4080 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004081
4082 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004083 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4084 NULL, 0,
4085 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004086 capacity ),
4087 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004088
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004089 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004090
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004091 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004092 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004093
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004094exit:
4095 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004096 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004097 mbedtls_psa_crypto_free( );
4098}
4099/* END_CASE */
4100
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004101/* BEGIN_CASE */
4102void test_derive_invalid_generator_tests( )
4103{
4104 uint8_t output_buffer[16];
4105 size_t buffer_size = 16;
4106 size_t capacity = 0;
4107 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4108
Nir Sonnenschein50789302018-10-31 12:16:38 +02004109 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004110 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004111
4112 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004113 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004114
Gilles Peskine8817f612018-12-18 00:18:46 +01004115 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004116
Nir Sonnenschein50789302018-10-31 12:16:38 +02004117 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004118 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004119
Nir Sonnenschein50789302018-10-31 12:16:38 +02004120 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004121 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004122
4123exit:
4124 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004125}
4126/* END_CASE */
4127
4128/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004129void derive_output( int alg_arg,
4130 data_t *key_data,
4131 data_t *salt,
4132 data_t *label,
4133 int requested_capacity_arg,
4134 data_t *expected_output1,
4135 data_t *expected_output2 )
4136{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004137 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004138 psa_algorithm_t alg = alg_arg;
4139 size_t requested_capacity = requested_capacity_arg;
4140 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4141 uint8_t *expected_outputs[2] =
4142 {expected_output1->x, expected_output2->x};
4143 size_t output_sizes[2] =
4144 {expected_output1->len, expected_output2->len};
4145 size_t output_buffer_size = 0;
4146 uint8_t *output_buffer = NULL;
4147 size_t expected_capacity;
4148 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004150 psa_status_t status;
4151 unsigned i;
4152
4153 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4154 {
4155 if( output_sizes[i] > output_buffer_size )
4156 output_buffer_size = output_sizes[i];
4157 if( output_sizes[i] == 0 )
4158 expected_outputs[i] = NULL;
4159 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004160 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004161 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004162
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004163 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4164 psa_set_key_algorithm( &attributes, alg );
4165 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004166
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004167 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004168 key_data->x, key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004169
4170 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004171 if( PSA_ALG_IS_HKDF( alg ) )
4172 {
4173 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4174 PSA_ASSERT( psa_set_generator_capacity( &generator,
4175 requested_capacity ) );
4176 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4177 PSA_KDF_STEP_SALT,
4178 salt->x, salt->len ) );
4179 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4180 PSA_KDF_STEP_SECRET,
4181 handle ) );
4182 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4183 PSA_KDF_STEP_INFO,
4184 label->x, label->len ) );
4185 }
4186 else
4187 {
4188 // legacy
4189 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4190 salt->x, salt->len,
4191 label->x, label->len,
4192 requested_capacity ) );
4193 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004194 PSA_ASSERT( psa_get_generator_capacity( &generator,
4195 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004196 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004197 expected_capacity = requested_capacity;
4198
4199 /* Expansion phase. */
4200 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4201 {
4202 /* Read some bytes. */
4203 status = psa_generator_read( &generator,
4204 output_buffer, output_sizes[i] );
4205 if( expected_capacity == 0 && output_sizes[i] == 0 )
4206 {
4207 /* Reading 0 bytes when 0 bytes are available can go either way. */
4208 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004209 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004210 continue;
4211 }
4212 else if( expected_capacity == 0 ||
4213 output_sizes[i] > expected_capacity )
4214 {
4215 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004216 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004217 expected_capacity = 0;
4218 continue;
4219 }
4220 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004221 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004222 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004223 ASSERT_COMPARE( output_buffer, output_sizes[i],
4224 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004225 /* Check the generator status. */
4226 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004227 PSA_ASSERT( psa_get_generator_capacity( &generator,
4228 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004229 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004230 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004231 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004232
4233exit:
4234 mbedtls_free( output_buffer );
4235 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004236 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004237 mbedtls_psa_crypto_free( );
4238}
4239/* END_CASE */
4240
4241/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004242void derive_full( int alg_arg,
4243 data_t *key_data,
4244 data_t *salt,
4245 data_t *label,
4246 int requested_capacity_arg )
4247{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004248 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004249 psa_algorithm_t alg = alg_arg;
4250 size_t requested_capacity = requested_capacity_arg;
4251 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4252 unsigned char output_buffer[16];
4253 size_t expected_capacity = requested_capacity;
4254 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004256
Gilles Peskine8817f612018-12-18 00:18:46 +01004257 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004258
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004259 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4260 psa_set_key_algorithm( &attributes, alg );
4261 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004262
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004263 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004264 key_data->x, key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004265
4266 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004267 if( PSA_ALG_IS_HKDF( alg ) )
4268 {
4269 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4270 PSA_ASSERT( psa_set_generator_capacity( &generator,
4271 requested_capacity ) );
4272 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4273 PSA_KDF_STEP_SALT,
4274 salt->x, salt->len ) );
4275 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4276 PSA_KDF_STEP_SECRET,
4277 handle ) );
4278 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4279 PSA_KDF_STEP_INFO,
4280 label->x, label->len ) );
4281 }
4282 else
4283 {
4284 // legacy
4285 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4286 salt->x, salt->len,
4287 label->x, label->len,
4288 requested_capacity ) );
4289 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004290 PSA_ASSERT( psa_get_generator_capacity( &generator,
4291 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004292 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004293
4294 /* Expansion phase. */
4295 while( current_capacity > 0 )
4296 {
4297 size_t read_size = sizeof( output_buffer );
4298 if( read_size > current_capacity )
4299 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004300 PSA_ASSERT( psa_generator_read( &generator,
4301 output_buffer,
4302 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004303 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004304 PSA_ASSERT( psa_get_generator_capacity( &generator,
4305 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004306 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004307 }
4308
4309 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004310 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004311 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004312
Gilles Peskine8817f612018-12-18 00:18:46 +01004313 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004314
4315exit:
4316 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004317 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004318 mbedtls_psa_crypto_free( );
4319}
4320/* END_CASE */
4321
4322/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004323void derive_key_exercise( int alg_arg,
4324 data_t *key_data,
4325 data_t *salt,
4326 data_t *label,
4327 int derived_type_arg,
4328 int derived_bits_arg,
4329 int derived_usage_arg,
4330 int derived_alg_arg )
4331{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004332 psa_key_handle_t base_handle = 0;
4333 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004334 psa_algorithm_t alg = alg_arg;
4335 psa_key_type_t derived_type = derived_type_arg;
4336 size_t derived_bits = derived_bits_arg;
4337 psa_key_usage_t derived_usage = derived_usage_arg;
4338 psa_algorithm_t derived_alg = derived_alg_arg;
4339 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4340 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004342 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004343
Gilles Peskine8817f612018-12-18 00:18:46 +01004344 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004345
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4347 psa_set_key_algorithm( &attributes, alg );
4348 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
4349 PSA_ASSERT( psa_import_key( &attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004350 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004351
4352 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004353 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4354 salt->x, salt->len,
4355 label->x, label->len,
4356 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004357 psa_set_key_usage_flags( &attributes, derived_usage );
4358 psa_set_key_algorithm( &attributes, derived_alg );
4359 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004360 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004361 PSA_ASSERT( psa_generator_import_key( &attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004362 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004363
4364 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004365 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4366 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4367 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004368
4369 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004370 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004371 goto exit;
4372
4373exit:
4374 psa_generator_abort( &generator );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004375 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004376 psa_destroy_key( base_handle );
4377 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004378 mbedtls_psa_crypto_free( );
4379}
4380/* END_CASE */
4381
4382/* BEGIN_CASE */
4383void derive_key_export( int alg_arg,
4384 data_t *key_data,
4385 data_t *salt,
4386 data_t *label,
4387 int bytes1_arg,
4388 int bytes2_arg )
4389{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004390 psa_key_handle_t base_handle = 0;
4391 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004392 psa_algorithm_t alg = alg_arg;
4393 size_t bytes1 = bytes1_arg;
4394 size_t bytes2 = bytes2_arg;
4395 size_t capacity = bytes1 + bytes2;
4396 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004397 uint8_t *output_buffer = NULL;
4398 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004399 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4400 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004401 size_t length;
4402
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004403 ASSERT_ALLOC( output_buffer, capacity );
4404 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004405 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004406
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004407 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4408 psa_set_key_algorithm( &base_attributes, alg );
4409 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4410 PSA_ASSERT( psa_import_key( &base_attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004411 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004412
4413 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004414 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4415 salt->x, salt->len,
4416 label->x, label->len,
4417 capacity ) );
4418 PSA_ASSERT( psa_generator_read( &generator,
4419 output_buffer,
4420 capacity ) );
4421 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004422
4423 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004424 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4425 salt->x, salt->len,
4426 label->x, label->len,
4427 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004428 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4429 psa_set_key_algorithm( &derived_attributes, 0 );
4430 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004431 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004432 PSA_ASSERT( psa_generator_import_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004433 &generator ) );
4434 PSA_ASSERT( psa_export_key( derived_handle,
4435 export_buffer, bytes1,
4436 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004437 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004438 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004439 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004440 PSA_ASSERT( psa_generator_import_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004441 &generator ) );
4442 PSA_ASSERT( psa_export_key( derived_handle,
4443 export_buffer + bytes1, bytes2,
4444 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004445 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004446
4447 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004448 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4449 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004450
4451exit:
4452 mbedtls_free( output_buffer );
4453 mbedtls_free( export_buffer );
4454 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004455 psa_destroy_key( base_handle );
4456 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004457 mbedtls_psa_crypto_free( );
4458}
4459/* END_CASE */
4460
4461/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004462void key_agreement_setup( int alg_arg,
4463 int our_key_type_arg, data_t *our_key_data,
4464 data_t *peer_key_data,
4465 int expected_status_arg )
4466{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004467 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004468 psa_algorithm_t alg = alg_arg;
4469 psa_key_type_t our_key_type = our_key_type_arg;
4470 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004472 psa_status_t expected_status = expected_status_arg;
4473 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004474
Gilles Peskine8817f612018-12-18 00:18:46 +01004475 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004476
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004477 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4478 psa_set_key_algorithm( &attributes, alg );
4479 psa_set_key_type( &attributes, our_key_type );
4480 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004481 our_key_data->x, our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004482
Gilles Peskine77f40d82019-04-11 21:27:06 +02004483 /* The tests currently include inputs that should fail at either step.
4484 * Test cases that fail at the setup step should be changed to call
4485 * key_derivation_setup instead, and this function should be renamed
4486 * to key_agreement_fail. */
4487 status = psa_key_derivation_setup( &generator, alg );
4488 if( status == PSA_SUCCESS )
4489 {
4490 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
4491 our_key,
4492 peer_key_data->x, peer_key_data->len ),
4493 expected_status );
4494 }
4495 else
4496 {
4497 TEST_ASSERT( status == expected_status );
4498 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004499
4500exit:
4501 psa_generator_abort( &generator );
4502 psa_destroy_key( our_key );
4503 mbedtls_psa_crypto_free( );
4504}
4505/* END_CASE */
4506
4507/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004508void raw_key_agreement( int alg_arg,
4509 int our_key_type_arg, data_t *our_key_data,
4510 data_t *peer_key_data,
4511 data_t *expected_output )
4512{
4513 psa_key_handle_t our_key = 0;
4514 psa_algorithm_t alg = alg_arg;
4515 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004517 unsigned char *output = NULL;
4518 size_t output_length = ~0;
4519
4520 ASSERT_ALLOC( output, expected_output->len );
4521 PSA_ASSERT( psa_crypto_init( ) );
4522
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004523 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4524 psa_set_key_algorithm( &attributes, alg );
4525 psa_set_key_type( &attributes, our_key_type );
4526 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004527 our_key_data->x, our_key_data->len ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004528
4529 PSA_ASSERT( psa_key_agreement_raw_shared_secret(
4530 alg, our_key,
4531 peer_key_data->x, peer_key_data->len,
4532 output, expected_output->len, &output_length ) );
4533 ASSERT_COMPARE( output, output_length,
4534 expected_output->x, expected_output->len );
4535
4536exit:
4537 mbedtls_free( output );
4538 psa_destroy_key( our_key );
4539 mbedtls_psa_crypto_free( );
4540}
4541/* END_CASE */
4542
4543/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004544void key_agreement_capacity( int alg_arg,
4545 int our_key_type_arg, data_t *our_key_data,
4546 data_t *peer_key_data,
4547 int expected_capacity_arg )
4548{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004549 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004550 psa_algorithm_t alg = alg_arg;
4551 psa_key_type_t our_key_type = our_key_type_arg;
4552 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004553 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004554 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004555 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004556
Gilles Peskine8817f612018-12-18 00:18:46 +01004557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004558
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004559 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4560 psa_set_key_algorithm( &attributes, alg );
4561 psa_set_key_type( &attributes, our_key_type );
4562 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004563 our_key_data->x, our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004564
Gilles Peskine969c5d62019-01-16 15:53:06 +01004565 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4566 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004567 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004568 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004569 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4570 {
4571 /* The test data is for info="" */
4572 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4573 PSA_KDF_STEP_INFO,
4574 NULL, 0 ) );
4575 }
Gilles Peskine59685592018-09-18 12:11:34 +02004576
Gilles Peskinebf491972018-10-25 22:36:12 +02004577 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004578 PSA_ASSERT( psa_get_generator_capacity(
4579 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004580 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004581
Gilles Peskinebf491972018-10-25 22:36:12 +02004582 /* Test the actual capacity by reading the output. */
4583 while( actual_capacity > sizeof( output ) )
4584 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004585 PSA_ASSERT( psa_generator_read( &generator,
4586 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004587 actual_capacity -= sizeof( output );
4588 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004589 PSA_ASSERT( psa_generator_read( &generator,
4590 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004591 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004592 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004593
Gilles Peskine59685592018-09-18 12:11:34 +02004594exit:
4595 psa_generator_abort( &generator );
4596 psa_destroy_key( our_key );
4597 mbedtls_psa_crypto_free( );
4598}
4599/* END_CASE */
4600
4601/* BEGIN_CASE */
4602void key_agreement_output( int alg_arg,
4603 int our_key_type_arg, data_t *our_key_data,
4604 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004605 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004606{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004607 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004608 psa_algorithm_t alg = alg_arg;
4609 psa_key_type_t our_key_type = our_key_type_arg;
4610 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004612 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004613
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004614 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4615 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004616
Gilles Peskine8817f612018-12-18 00:18:46 +01004617 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004618
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004619 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4620 psa_set_key_algorithm( &attributes, alg );
4621 psa_set_key_type( &attributes, our_key_type );
4622 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004623 our_key_data->x, our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004624
Gilles Peskine969c5d62019-01-16 15:53:06 +01004625 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4626 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004627 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004628 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004629 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4630 {
4631 /* The test data is for info="" */
4632 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4633 PSA_KDF_STEP_INFO,
4634 NULL, 0 ) );
4635 }
Gilles Peskine59685592018-09-18 12:11:34 +02004636
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004637 PSA_ASSERT( psa_generator_read( &generator,
4638 actual_output,
4639 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004640 ASSERT_COMPARE( actual_output, expected_output1->len,
4641 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004642 if( expected_output2->len != 0 )
4643 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004644 PSA_ASSERT( psa_generator_read( &generator,
4645 actual_output,
4646 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004647 ASSERT_COMPARE( actual_output, expected_output2->len,
4648 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004649 }
Gilles Peskine59685592018-09-18 12:11:34 +02004650
4651exit:
4652 psa_generator_abort( &generator );
4653 psa_destroy_key( our_key );
4654 mbedtls_psa_crypto_free( );
4655 mbedtls_free( actual_output );
4656}
4657/* END_CASE */
4658
4659/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004660void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004661{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004662 size_t bytes = bytes_arg;
4663 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004664 unsigned char *output = NULL;
4665 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004666 size_t i;
4667 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004668
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004669 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4670 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004671 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004672
Gilles Peskine8817f612018-12-18 00:18:46 +01004673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004674
Gilles Peskinea50d7392018-06-21 10:22:13 +02004675 /* Run several times, to ensure that every output byte will be
4676 * nonzero at least once with overwhelming probability
4677 * (2^(-8*number_of_runs)). */
4678 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004679 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004680 if( bytes != 0 )
4681 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004682 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004683
4684 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004685 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4686 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004687
4688 for( i = 0; i < bytes; i++ )
4689 {
4690 if( output[i] != 0 )
4691 ++changed[i];
4692 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004693 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004694
4695 /* Check that every byte was changed to nonzero at least once. This
4696 * validates that psa_generate_random is overwriting every byte of
4697 * the output buffer. */
4698 for( i = 0; i < bytes; i++ )
4699 {
4700 TEST_ASSERT( changed[i] != 0 );
4701 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004702
4703exit:
4704 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004705 mbedtls_free( output );
4706 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004707}
4708/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004709
4710/* BEGIN_CASE */
4711void generate_key( int type_arg,
4712 int bits_arg,
4713 int usage_arg,
4714 int alg_arg,
4715 int expected_status_arg )
4716{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004717 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004718 psa_key_type_t type = type_arg;
4719 psa_key_usage_t usage = usage_arg;
4720 size_t bits = bits_arg;
4721 psa_algorithm_t alg = alg_arg;
4722 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004723 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004724 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004725
Gilles Peskine8817f612018-12-18 00:18:46 +01004726 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004727
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004728 psa_set_key_usage_flags( &attributes, usage );
4729 psa_set_key_algorithm( &attributes, alg );
4730 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004731 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004732
4733 /* Generate a key */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004734 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
4735 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004736 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004737
4738 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004739 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4740 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4741 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004742
Gilles Peskine818ca122018-06-20 18:16:48 +02004743 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004744 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004745 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004746
4747exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004748 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004749 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004750 mbedtls_psa_crypto_free( );
4751}
4752/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004753
Gilles Peskinee56e8782019-04-26 17:34:02 +02004754/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4755void generate_key_rsa( int bits_arg,
4756 data_t *e_arg,
4757 int expected_status_arg )
4758{
4759 psa_key_handle_t handle = 0;
4760 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEYPAIR;
4761 size_t bits = bits_arg;
4762 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4763 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4764 psa_status_t expected_status = expected_status_arg;
4765 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4766 uint8_t *exported = NULL;
4767 size_t exported_size =
4768 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4769 size_t exported_length = SIZE_MAX;
4770 uint8_t *e_read_buffer = NULL;
4771 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004772 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004773 size_t e_read_length = SIZE_MAX;
4774
4775 if( e_arg->len == 0 ||
4776 ( e_arg->len == 3 &&
4777 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4778 {
4779 is_default_public_exponent = 1;
4780 e_read_size = 0;
4781 }
4782 ASSERT_ALLOC( e_read_buffer, e_read_size );
4783 ASSERT_ALLOC( exported, exported_size );
4784
4785 PSA_ASSERT( psa_crypto_init( ) );
4786
4787 psa_set_key_usage_flags( &attributes, usage );
4788 psa_set_key_algorithm( &attributes, alg );
4789 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4790 e_arg->x, e_arg->len ) );
4791 psa_set_key_bits( &attributes, bits );
4792
4793 /* Generate a key */
4794 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
4795 if( expected_status != PSA_SUCCESS )
4796 goto exit;
4797
4798 /* Test the key information */
4799 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4800 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4801 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4802 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4803 e_read_buffer, e_read_size,
4804 &e_read_length ) );
4805 if( is_default_public_exponent )
4806 TEST_EQUAL( e_read_length, 0 );
4807 else
4808 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4809
4810 /* Do something with the key according to its type and permitted usage. */
4811 if( ! exercise_key( handle, usage, alg ) )
4812 goto exit;
4813
4814 /* Export the key and check the public exponent. */
4815 PSA_ASSERT( psa_export_public_key( handle,
4816 exported, exported_size,
4817 &exported_length ) );
4818 {
4819 uint8_t *p = exported;
4820 uint8_t *end = exported + exported_length;
4821 size_t len;
4822 /* RSAPublicKey ::= SEQUENCE {
4823 * modulus INTEGER, -- n
4824 * publicExponent INTEGER } -- e
4825 */
4826 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4827 MBEDTLS_ASN1_SEQUENCE |
4828 MBEDTLS_ASN1_CONSTRUCTED ) );
4829 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
4830 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4831 MBEDTLS_ASN1_INTEGER ) );
4832 if( len >= 1 && p[0] == 0 )
4833 {
4834 ++p;
4835 --len;
4836 }
4837 if( e_arg->len == 0 )
4838 {
4839 TEST_EQUAL( len, 3 );
4840 TEST_EQUAL( p[0], 1 );
4841 TEST_EQUAL( p[1], 0 );
4842 TEST_EQUAL( p[2], 1 );
4843 }
4844 else
4845 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4846 }
4847
4848exit:
4849 psa_reset_key_attributes( &attributes );
4850 psa_destroy_key( handle );
4851 mbedtls_psa_crypto_free( );
4852 mbedtls_free( e_read_buffer );
4853 mbedtls_free( exported );
4854}
4855/* END_CASE */
4856
Darryl Greend49a4992018-06-18 17:27:26 +01004857/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004858void persistent_key_load_key_from_storage( data_t *data,
4859 int type_arg, int bits_arg,
4860 int usage_flags_arg, int alg_arg,
4861 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004862{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004863 psa_key_id_t key_id = 1;
4864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004865 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004866 psa_key_handle_t base_key = 0;
4867 psa_key_type_t type = type_arg;
4868 size_t bits = bits_arg;
4869 psa_key_usage_t usage_flags = usage_flags_arg;
4870 psa_algorithm_t alg = alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004871 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004872 unsigned char *first_export = NULL;
4873 unsigned char *second_export = NULL;
4874 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4875 size_t first_exported_length;
4876 size_t second_exported_length;
4877
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004878 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4879 {
4880 ASSERT_ALLOC( first_export, export_size );
4881 ASSERT_ALLOC( second_export, export_size );
4882 }
Darryl Greend49a4992018-06-18 17:27:26 +01004883
Gilles Peskine8817f612018-12-18 00:18:46 +01004884 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004885
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004886 psa_make_key_persistent( &attributes, key_id, PSA_KEY_LIFETIME_PERSISTENT );
4887 psa_set_key_usage_flags( &attributes, usage_flags );
4888 psa_set_key_algorithm( &attributes, alg );
4889 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004890 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004891
Darryl Green0c6575a2018-11-07 16:05:30 +00004892 switch( generation_method )
4893 {
4894 case IMPORT_KEY:
4895 /* Import the key */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004896 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004897 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004898 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004899
Darryl Green0c6575a2018-11-07 16:05:30 +00004900 case GENERATE_KEY:
4901 /* Generate a key */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004902 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004903 break;
4904
4905 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004906 {
4907 /* Create base key */
4908 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4909 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4910 psa_set_key_usage_flags( &base_attributes,
4911 PSA_KEY_USAGE_DERIVE );
4912 psa_set_key_algorithm( &base_attributes, derive_alg );
4913 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4914 PSA_ASSERT( psa_import_key( &base_attributes, &base_key,
4915 data->x, data->len ) );
4916 /* Derive a key. */
4917 PSA_ASSERT( psa_key_derivation_setup( &generator, derive_alg ) );
4918 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4919 PSA_KDF_STEP_SECRET,
4920 base_key ) );
4921 PSA_ASSERT( psa_key_derivation_input_bytes(
4922 &generator, PSA_KDF_STEP_INFO,
4923 NULL, 0 ) );
4924 PSA_ASSERT( psa_generator_import_key( &attributes, &handle,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004925 &generator ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004926 PSA_ASSERT( psa_generator_abort( &generator ) );
4927 PSA_ASSERT( psa_destroy_key( base_key ) );
4928 base_key = 0;
4929 }
Darryl Green0c6575a2018-11-07 16:05:30 +00004930 break;
4931 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004932 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004933
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004934 /* Export the key if permitted by the key policy. */
4935 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4936 {
4937 PSA_ASSERT( psa_export_key( handle,
4938 first_export, export_size,
4939 &first_exported_length ) );
4940 if( generation_method == IMPORT_KEY )
4941 ASSERT_COMPARE( data->x, data->len,
4942 first_export, first_exported_length );
4943 }
Darryl Greend49a4992018-06-18 17:27:26 +01004944
4945 /* Shutdown and restart */
4946 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004947 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004948
Darryl Greend49a4992018-06-18 17:27:26 +01004949 /* Check key slot still contains key data */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004950 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
Gilles Peskine8817f612018-12-18 00:18:46 +01004951 &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004952 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4953 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
4954 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4955 PSA_KEY_LIFETIME_PERSISTENT );
4956 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4957 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4958 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4959 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004960
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004961 /* Export the key again if permitted by the key policy. */
4962 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004963 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004964 PSA_ASSERT( psa_export_key( handle,
4965 second_export, export_size,
4966 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004967 ASSERT_COMPARE( first_export, first_exported_length,
4968 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004969 }
4970
4971 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004972 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004973 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004974
4975exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004976 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004977 mbedtls_free( first_export );
4978 mbedtls_free( second_export );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004979 psa_generator_abort( &generator );
4980 psa_destroy_key( base_key );
4981 if( handle == 0 )
4982 {
4983 /* In case there was a test failure after creating the persistent key
4984 * but while it was not open, try to re-open the persistent key
4985 * to delete it. */
4986 psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle );
4987 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004988 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004989 mbedtls_psa_crypto_free();
4990}
4991/* END_CASE */