blob: c194396965d4f07d666bbe7537da20ae94df1384 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinef426e0f2019-02-25 17:42:03 +010017/* A hash algorithm that is known to be supported.
18 *
19 * This is used in some smoke tests.
20 */
21#if defined(MBEDTLS_MD2_C)
22#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
23#elif defined(MBEDTLS_MD4_C)
24#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
25#elif defined(MBEDTLS_MD5_C)
26#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
27/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
28 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
29 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
30 * implausible anyway. */
31#elif defined(MBEDTLS_SHA1_C)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
33#elif defined(MBEDTLS_SHA256_C)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
35#elif defined(MBEDTLS_SHA512_C)
36#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
37#elif defined(MBEDTLS_SHA3_C)
38#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
39#else
40#undef KNOWN_SUPPORTED_HASH_ALG
41#endif
42
43/* A block cipher that is known to be supported.
44 *
45 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
46 */
47#if defined(MBEDTLS_AES_C)
48#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
49#elif defined(MBEDTLS_ARIA_C)
50#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
51#elif defined(MBEDTLS_CAMELLIA_C)
52#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
53#undef KNOWN_SUPPORTED_BLOCK_CIPHER
54#endif
55
56/* A MAC mode that is known to be supported.
57 *
58 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
59 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
60 *
61 * This is used in some smoke tests.
62 */
63#if defined(KNOWN_SUPPORTED_HASH_ALG)
64#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
65#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
66#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
67#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
68#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
69#else
70#undef KNOWN_SUPPORTED_MAC_ALG
71#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
72#endif
73
74/* A cipher algorithm and key type that are known to be supported.
75 *
76 * This is used in some smoke tests.
77 */
78#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
79#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
80#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
81#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
82#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
83#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
84#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
85#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
86#else
87#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
88#endif
89#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
90#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
92#elif defined(MBEDTLS_RC4_C)
93#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
94#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
95#else
96#undef KNOWN_SUPPORTED_CIPHER_ALG
97#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
98#endif
99
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200100/** Test if a buffer contains a constant byte value.
101 *
102 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200103 *
104 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200105 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200106 * \param size Size of the buffer in bytes.
107 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200108 * \return 1 if the buffer is all-bits-zero.
109 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200110 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200111static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200112{
113 size_t i;
114 for( i = 0; i < size; i++ )
115 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200116 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200117 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200118 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200119 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200120}
Gilles Peskine818ca122018-06-20 18:16:48 +0200121
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200122/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
123static int asn1_write_10x( unsigned char **p,
124 unsigned char *start,
125 size_t bits,
126 unsigned char x )
127{
128 int ret;
129 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200130 if( bits == 0 )
131 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
132 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200133 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300134 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200135 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
136 *p -= len;
137 ( *p )[len-1] = x;
138 if( bits % 8 == 0 )
139 ( *p )[1] |= 1;
140 else
141 ( *p )[0] |= 1 << ( bits % 8 );
142 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
143 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
144 MBEDTLS_ASN1_INTEGER ) );
145 return( len );
146}
147
148static int construct_fake_rsa_key( unsigned char *buffer,
149 size_t buffer_size,
150 unsigned char **p,
151 size_t bits,
152 int keypair )
153{
154 size_t half_bits = ( bits + 1 ) / 2;
155 int ret;
156 int len = 0;
157 /* Construct something that looks like a DER encoding of
158 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
159 * RSAPrivateKey ::= SEQUENCE {
160 * version Version,
161 * modulus INTEGER, -- n
162 * publicExponent INTEGER, -- e
163 * privateExponent INTEGER, -- d
164 * prime1 INTEGER, -- p
165 * prime2 INTEGER, -- q
166 * exponent1 INTEGER, -- d mod (p-1)
167 * exponent2 INTEGER, -- d mod (q-1)
168 * coefficient INTEGER, -- (inverse of q) mod p
169 * otherPrimeInfos OtherPrimeInfos OPTIONAL
170 * }
171 * Or, for a public key, the same structure with only
172 * version, modulus and publicExponent.
173 */
174 *p = buffer + buffer_size;
175 if( keypair )
176 {
177 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
178 asn1_write_10x( p, buffer, half_bits, 1 ) );
179 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
180 asn1_write_10x( p, buffer, half_bits, 1 ) );
181 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
182 asn1_write_10x( p, buffer, half_bits, 1 ) );
183 MBEDTLS_ASN1_CHK_ADD( len, /* q */
184 asn1_write_10x( p, buffer, half_bits, 1 ) );
185 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
186 asn1_write_10x( p, buffer, half_bits, 3 ) );
187 MBEDTLS_ASN1_CHK_ADD( len, /* d */
188 asn1_write_10x( p, buffer, bits, 1 ) );
189 }
190 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
191 asn1_write_10x( p, buffer, 17, 1 ) );
192 MBEDTLS_ASN1_CHK_ADD( len, /* n */
193 asn1_write_10x( p, buffer, bits, 1 ) );
194 if( keypair )
195 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
196 mbedtls_asn1_write_int( p, buffer, 0 ) );
197 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
198 {
199 const unsigned char tag =
200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
201 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
202 }
203 return( len );
204}
205
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100206int exercise_mac_setup( psa_key_type_t key_type,
207 const unsigned char *key_bytes,
208 size_t key_length,
209 psa_algorithm_t alg,
210 psa_mac_operation_t *operation,
211 psa_status_t *status )
212{
213 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100215
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
217 psa_set_key_algorithm( &attributes, alg );
218 psa_set_key_type( &attributes, key_type );
219 PSA_ASSERT( psa_import_key( &attributes, &handle, key_bytes, key_length ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100220
221 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 /* Whether setup succeeded or failed, abort must succeed. */
223 PSA_ASSERT( psa_mac_abort( operation ) );
224 /* If setup failed, reproduce the failure, so that the caller can
225 * test the resulting state of the operation object. */
226 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100227 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100228 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
229 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230 }
231
232 psa_destroy_key( handle );
233 return( 1 );
234
235exit:
236 psa_destroy_key( handle );
237 return( 0 );
238}
239
240int exercise_cipher_setup( psa_key_type_t key_type,
241 const unsigned char *key_bytes,
242 size_t key_length,
243 psa_algorithm_t alg,
244 psa_cipher_operation_t *operation,
245 psa_status_t *status )
246{
247 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100249
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
251 psa_set_key_algorithm( &attributes, alg );
252 psa_set_key_type( &attributes, key_type );
253 PSA_ASSERT( psa_import_key( &attributes, &handle, key_bytes, key_length ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100254
255 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100256 /* Whether setup succeeded or failed, abort must succeed. */
257 PSA_ASSERT( psa_cipher_abort( operation ) );
258 /* If setup failed, reproduce the failure, so that the caller can
259 * test the resulting state of the operation object. */
260 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100261 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100262 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
263 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100264 }
265
266 psa_destroy_key( handle );
267 return( 1 );
268
269exit:
270 psa_destroy_key( handle );
271 return( 0 );
272}
273
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100274static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200275 psa_key_usage_t usage,
276 psa_algorithm_t alg )
277{
Jaeden Amero769ce272019-01-04 11:48:03 +0000278 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200279 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200280 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200281 size_t mac_length = sizeof( mac );
282
283 if( usage & PSA_KEY_USAGE_SIGN )
284 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100285 PSA_ASSERT( psa_mac_sign_setup( &operation,
286 handle, alg ) );
287 PSA_ASSERT( psa_mac_update( &operation,
288 input, sizeof( input ) ) );
289 PSA_ASSERT( psa_mac_sign_finish( &operation,
290 mac, sizeof( mac ),
291 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 }
293
294 if( usage & PSA_KEY_USAGE_VERIFY )
295 {
296 psa_status_t verify_status =
297 ( usage & PSA_KEY_USAGE_SIGN ?
298 PSA_SUCCESS :
299 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100300 PSA_ASSERT( psa_mac_verify_setup( &operation,
301 handle, alg ) );
302 PSA_ASSERT( psa_mac_update( &operation,
303 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100304 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
305 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 }
307
308 return( 1 );
309
310exit:
311 psa_mac_abort( &operation );
312 return( 0 );
313}
314
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100315static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200316 psa_key_usage_t usage,
317 psa_algorithm_t alg )
318{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000319 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200320 unsigned char iv[16] = {0};
321 size_t iv_length = sizeof( iv );
322 const unsigned char plaintext[16] = "Hello, world...";
323 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 unsigned char decrypted[sizeof( ciphertext )];
326 size_t part_length;
327
328 if( usage & PSA_KEY_USAGE_ENCRYPT )
329 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100330 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
331 handle, alg ) );
332 PSA_ASSERT( psa_cipher_generate_iv( &operation,
333 iv, sizeof( iv ),
334 &iv_length ) );
335 PSA_ASSERT( psa_cipher_update( &operation,
336 plaintext, sizeof( plaintext ),
337 ciphertext, sizeof( ciphertext ),
338 &ciphertext_length ) );
339 PSA_ASSERT( psa_cipher_finish( &operation,
340 ciphertext + ciphertext_length,
341 sizeof( ciphertext ) - ciphertext_length,
342 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200343 ciphertext_length += part_length;
344 }
345
346 if( usage & PSA_KEY_USAGE_DECRYPT )
347 {
348 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200350 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
351 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
354 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
355 * have this macro yet. */
356 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
357 psa_get_key_type( &attributes ) );
358 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200359 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100360 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
361 handle, alg ) );
362 PSA_ASSERT( psa_cipher_set_iv( &operation,
363 iv, iv_length ) );
364 PSA_ASSERT( psa_cipher_update( &operation,
365 ciphertext, ciphertext_length,
366 decrypted, sizeof( decrypted ),
367 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200368 status = psa_cipher_finish( &operation,
369 decrypted + part_length,
370 sizeof( decrypted ) - part_length,
371 &part_length );
372 /* For a stream cipher, all inputs are valid. For a block cipher,
373 * if the input is some aribtrary data rather than an actual
374 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200375 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200376 TEST_ASSERT( status == PSA_SUCCESS ||
377 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200378 else
379 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200380 }
381
382 return( 1 );
383
384exit:
385 psa_cipher_abort( &operation );
386 return( 0 );
387}
388
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200390 psa_key_usage_t usage,
391 psa_algorithm_t alg )
392{
393 unsigned char nonce[16] = {0};
394 size_t nonce_length = sizeof( nonce );
395 unsigned char plaintext[16] = "Hello, world...";
396 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
397 size_t ciphertext_length = sizeof( ciphertext );
398 size_t plaintext_length = sizeof( ciphertext );
399
400 if( usage & PSA_KEY_USAGE_ENCRYPT )
401 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_aead_encrypt( handle, alg,
403 nonce, nonce_length,
404 NULL, 0,
405 plaintext, sizeof( plaintext ),
406 ciphertext, sizeof( ciphertext ),
407 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200408 }
409
410 if( usage & PSA_KEY_USAGE_DECRYPT )
411 {
412 psa_status_t verify_status =
413 ( usage & PSA_KEY_USAGE_ENCRYPT ?
414 PSA_SUCCESS :
415 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100416 TEST_EQUAL( psa_aead_decrypt( handle, alg,
417 nonce, nonce_length,
418 NULL, 0,
419 ciphertext, ciphertext_length,
420 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100421 &plaintext_length ),
422 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200423 }
424
425 return( 1 );
426
427exit:
428 return( 0 );
429}
430
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100431static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200432 psa_key_usage_t usage,
433 psa_algorithm_t alg )
434{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200435 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
436 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200437 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200438 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100439 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
440
441 /* If the policy allows signing with any hash, just pick one. */
442 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
443 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100444#if defined(KNOWN_SUPPORTED_HASH_ALG)
445 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
446 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100447#else
448 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100449 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100450#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100451 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200452
453 if( usage & PSA_KEY_USAGE_SIGN )
454 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200455 /* Some algorithms require the payload to have the size of
456 * the hash encoded in the algorithm. Use this input size
457 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200458 if( hash_alg != 0 )
459 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100460 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
461 payload, payload_length,
462 signature, sizeof( signature ),
463 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200464 }
465
466 if( usage & PSA_KEY_USAGE_VERIFY )
467 {
468 psa_status_t verify_status =
469 ( usage & PSA_KEY_USAGE_SIGN ?
470 PSA_SUCCESS :
471 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
473 payload, payload_length,
474 signature, signature_length ),
475 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200476 }
477
478 return( 1 );
479
480exit:
481 return( 0 );
482}
483
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100484static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200485 psa_key_usage_t usage,
486 psa_algorithm_t alg )
487{
488 unsigned char plaintext[256] = "Hello, world...";
489 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
490 size_t ciphertext_length = sizeof( ciphertext );
491 size_t plaintext_length = 16;
492
493 if( usage & PSA_KEY_USAGE_ENCRYPT )
494 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100495 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
496 plaintext, plaintext_length,
497 NULL, 0,
498 ciphertext, sizeof( ciphertext ),
499 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200500 }
501
502 if( usage & PSA_KEY_USAGE_DECRYPT )
503 {
504 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100505 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200506 ciphertext, ciphertext_length,
507 NULL, 0,
508 plaintext, sizeof( plaintext ),
509 &plaintext_length );
510 TEST_ASSERT( status == PSA_SUCCESS ||
511 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
512 ( status == PSA_ERROR_INVALID_ARGUMENT ||
513 status == PSA_ERROR_INVALID_PADDING ) ) );
514 }
515
516 return( 1 );
517
518exit:
519 return( 0 );
520}
Gilles Peskine02b75072018-07-01 22:31:34 +0200521
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100522static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200523 psa_key_usage_t usage,
524 psa_algorithm_t alg )
525{
526 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
527 unsigned char label[16] = "This is a label.";
528 size_t label_length = sizeof( label );
529 unsigned char seed[16] = "abcdefghijklmnop";
530 size_t seed_length = sizeof( seed );
531 unsigned char output[1];
532
533 if( usage & PSA_KEY_USAGE_DERIVE )
534 {
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100535 if( PSA_ALG_IS_HKDF( alg ) )
536 {
537 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
538 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
539 PSA_KDF_STEP_SALT,
540 label,
541 label_length ) );
542 PSA_ASSERT( psa_key_derivation_input_key( &generator,
543 PSA_KDF_STEP_SECRET,
544 handle ) );
545 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
546 PSA_KDF_STEP_INFO,
547 seed,
548 seed_length ) );
549 }
550 else
551 {
552 // legacy
553 PSA_ASSERT( psa_key_derivation( &generator,
554 handle, alg,
555 label, label_length,
556 seed, seed_length,
557 sizeof( output ) ) );
558 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100559 PSA_ASSERT( psa_generator_read( &generator,
560 output,
561 sizeof( output ) ) );
562 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200563 }
564
565 return( 1 );
566
567exit:
568 return( 0 );
569}
570
Gilles Peskinec7998b72018-11-07 18:45:02 +0100571/* We need two keys to exercise key agreement. Exercise the
572 * private key against its own public key. */
573static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +0100574 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100575{
576 psa_key_type_t private_key_type;
577 psa_key_type_t public_key_type;
578 size_t key_bits;
579 uint8_t *public_key = NULL;
580 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200581 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinec7998b72018-11-07 18:45:02 +0100582 * psa_key_agreement fails. This isn't fully satisfactory, but it's
583 * good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200584 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100586
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200587 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
588 private_key_type = psa_get_key_type( &attributes );
589 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100590 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
591 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
592 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100593 PSA_ASSERT( psa_export_public_key( handle,
594 public_key, public_key_length,
595 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100596
Gilles Peskine969c5d62019-01-16 15:53:06 +0100597 status = psa_key_agreement( generator, PSA_KDF_STEP_SECRET, handle,
598 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100599exit:
600 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200601 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100602 return( status );
603}
604
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200605/* We need two keys to exercise key agreement. Exercise the
606 * private key against its own public key. */
607static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
608 psa_key_handle_t handle )
609{
610 psa_key_type_t private_key_type;
611 psa_key_type_t public_key_type;
612 size_t key_bits;
613 uint8_t *public_key = NULL;
614 size_t public_key_length;
615 uint8_t output[1024];
616 size_t output_length;
617 /* Return GENERIC_ERROR if something other than the final call to
618 * psa_key_agreement fails. This isn't fully satisfactory, but it's
619 * good enough: callers will report it as a failed test anyway. */
620 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200622
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200623 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
624 private_key_type = psa_get_key_type( &attributes );
625 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200626 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
627 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
628 ASSERT_ALLOC( public_key, public_key_length );
629 PSA_ASSERT( psa_export_public_key( handle,
630 public_key, public_key_length,
631 &public_key_length ) );
632
633 status = psa_key_agreement_raw_shared_secret(
634 alg, handle,
635 public_key, public_key_length,
636 output, sizeof( output ), &output_length );
637exit:
638 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200639 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200640 return( status );
641}
642
643static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
644 psa_key_usage_t usage,
645 psa_algorithm_t alg )
646{
647 int ok = 0;
648
649 if( usage & PSA_KEY_USAGE_DERIVE )
650 {
651 /* We need two keys to exercise key agreement. Exercise the
652 * private key against its own public key. */
653 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
654 }
655 ok = 1;
656
657exit:
658 return( ok );
659}
660
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100661static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200662 psa_key_usage_t usage,
663 psa_algorithm_t alg )
664{
665 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200666 unsigned char output[1];
667 int ok = 0;
668
669 if( usage & PSA_KEY_USAGE_DERIVE )
670 {
671 /* We need two keys to exercise key agreement. Exercise the
672 * private key against its own public key. */
Gilles Peskine969c5d62019-01-16 15:53:06 +0100673 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
674 PSA_ASSERT( key_agreement_with_self( &generator, handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_generator_read( &generator,
676 output,
677 sizeof( output ) ) );
678 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200679 }
680 ok = 1;
681
682exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200683 return( ok );
684}
685
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200686static int is_oid_of_key_type( psa_key_type_t type,
687 const uint8_t *oid, size_t oid_length )
688{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200689 const uint8_t *expected_oid = NULL;
690 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200691#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200692 if( PSA_KEY_TYPE_IS_RSA( type ) )
693 {
694 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
695 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
696 }
697 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200698#endif /* MBEDTLS_RSA_C */
699#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200700 if( PSA_KEY_TYPE_IS_ECC( type ) )
701 {
702 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
703 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
704 }
705 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200706#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200707 {
708 char message[40];
709 mbedtls_snprintf( message, sizeof( message ),
710 "OID not known for key type=0x%08lx",
711 (unsigned long) type );
712 test_fail( message, __LINE__, __FILE__ );
713 return( 0 );
714 }
715
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200716 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200717 return( 1 );
718
719exit:
720 return( 0 );
721}
722
723static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
724 size_t min_bits, size_t max_bits,
725 int must_be_odd )
726{
727 size_t len;
728 size_t actual_bits;
729 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100730 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100731 MBEDTLS_ASN1_INTEGER ),
732 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200733 /* Tolerate a slight departure from DER encoding:
734 * - 0 may be represented by an empty string or a 1-byte string.
735 * - The sign bit may be used as a value bit. */
736 if( ( len == 1 && ( *p )[0] == 0 ) ||
737 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
738 {
739 ++( *p );
740 --len;
741 }
742 if( min_bits == 0 && len == 0 )
743 return( 1 );
744 msb = ( *p )[0];
745 TEST_ASSERT( msb != 0 );
746 actual_bits = 8 * ( len - 1 );
747 while( msb != 0 )
748 {
749 msb >>= 1;
750 ++actual_bits;
751 }
752 TEST_ASSERT( actual_bits >= min_bits );
753 TEST_ASSERT( actual_bits <= max_bits );
754 if( must_be_odd )
755 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
756 *p += len;
757 return( 1 );
758exit:
759 return( 0 );
760}
761
762static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
763 size_t *len,
764 unsigned char n, unsigned char tag )
765{
766 int ret;
767 ret = mbedtls_asn1_get_tag( p, end, len,
768 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
769 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
770 if( ret != 0 )
771 return( ret );
772 end = *p + *len;
773 ret = mbedtls_asn1_get_tag( p, end, len, tag );
774 if( ret != 0 )
775 return( ret );
776 if( *p + *len != end )
777 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
778 return( 0 );
779}
780
781static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
782 uint8_t *exported, size_t exported_length )
783{
784 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100785 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200786 else
787 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200788
789#if defined(MBEDTLS_DES_C)
790 if( type == PSA_KEY_TYPE_DES )
791 {
792 /* Check the parity bits. */
793 unsigned i;
794 for( i = 0; i < bits / 8; i++ )
795 {
796 unsigned bit_count = 0;
797 unsigned m;
798 for( m = 1; m <= 0x100; m <<= 1 )
799 {
800 if( exported[i] & m )
801 ++bit_count;
802 }
803 TEST_ASSERT( bit_count % 2 != 0 );
804 }
805 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200806 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200807#endif
808
809#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
810 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
811 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200812 uint8_t *p = exported;
813 uint8_t *end = exported + exported_length;
814 size_t len;
815 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200816 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200817 * modulus INTEGER, -- n
818 * publicExponent INTEGER, -- e
819 * privateExponent INTEGER, -- d
820 * prime1 INTEGER, -- p
821 * prime2 INTEGER, -- q
822 * exponent1 INTEGER, -- d mod (p-1)
823 * exponent2 INTEGER, -- d mod (q-1)
824 * coefficient INTEGER, -- (inverse of q) mod p
825 * }
826 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100827 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
828 MBEDTLS_ASN1_SEQUENCE |
829 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
830 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200831 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
832 goto exit;
833 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
834 goto exit;
835 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
836 goto exit;
837 /* Require d to be at least half the size of n. */
838 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
839 goto exit;
840 /* Require p and q to be at most half the size of n, rounded up. */
841 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
842 goto exit;
843 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
844 goto exit;
845 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
846 goto exit;
847 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
848 goto exit;
849 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
850 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100851 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100852 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200853 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200854#endif /* MBEDTLS_RSA_C */
855
856#if defined(MBEDTLS_ECP_C)
857 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
858 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100859 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100860 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100861 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200862 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200863#endif /* MBEDTLS_ECP_C */
864
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200865 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
866 {
867 uint8_t *p = exported;
868 uint8_t *end = exported + exported_length;
869 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200870#if defined(MBEDTLS_RSA_C)
871 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
872 {
873 /* RSAPublicKey ::= SEQUENCE {
874 * modulus INTEGER, -- n
875 * publicExponent INTEGER } -- e
876 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100877 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
878 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100879 MBEDTLS_ASN1_CONSTRUCTED ),
880 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100881 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200882 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
883 goto exit;
884 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
885 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100886 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200887 }
888 else
889#endif /* MBEDTLS_RSA_C */
890#if defined(MBEDTLS_ECP_C)
891 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
892 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000893 /* The representation of an ECC public key is:
894 * - The byte 0x04;
895 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
896 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
897 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000898 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100899 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
900 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200901 }
902 else
903#endif /* MBEDTLS_ECP_C */
904 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100905 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200906 mbedtls_snprintf( message, sizeof( message ),
907 "No sanity check for public key type=0x%08lx",
908 (unsigned long) type );
909 test_fail( message, __LINE__, __FILE__ );
910 return( 0 );
911 }
912 }
913 else
914
915 {
916 /* No sanity checks for other types */
917 }
918
919 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200920
921exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200922 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200923}
924
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100925static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200926 psa_key_usage_t usage )
927{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200929 uint8_t *exported = NULL;
930 size_t exported_size = 0;
931 size_t exported_length = 0;
932 int ok = 0;
933
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200934 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200935
936 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200937 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200938 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100939 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
940 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200941 ok = 1;
942 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +0200943 }
944
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200945 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
946 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200947 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200948
Gilles Peskine8817f612018-12-18 00:18:46 +0100949 PSA_ASSERT( psa_export_key( handle,
950 exported, exported_size,
951 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200952 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
953 psa_get_key_bits( &attributes ),
954 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +0200955
956exit:
957 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200958 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200959 return( ok );
960}
961
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100962static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200963{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200965 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +0200966 uint8_t *exported = NULL;
967 size_t exported_size = 0;
968 size_t exported_length = 0;
969 int ok = 0;
970
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200971 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
972 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200973 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100974 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100975 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200976 return( 1 );
977 }
978
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200979 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(
980 psa_get_key_type( &attributes ) );
981 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
982 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200983 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200984
Gilles Peskine8817f612018-12-18 00:18:46 +0100985 PSA_ASSERT( psa_export_public_key( handle,
986 exported, exported_size,
987 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200988 ok = exported_key_sanity_check( public_type,
989 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +0200990 exported, exported_length );
991
992exit:
993 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200994 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200995 return( ok );
996}
997
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100998/** Do smoke tests on a key.
999 *
1000 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1001 * sign/verify, or derivation) that is permitted according to \p usage.
1002 * \p usage and \p alg should correspond to the expected policy on the
1003 * key.
1004 *
1005 * Export the key if permitted by \p usage, and check that the output
1006 * looks sensible. If \p usage forbids export, check that
1007 * \p psa_export_key correctly rejects the attempt. If the key is
1008 * asymmetric, also check \p psa_export_public_key.
1009 *
1010 * If the key fails the tests, this function calls the test framework's
1011 * `test_fail` function and returns false. Otherwise this function returns
1012 * true. Therefore it should be used as follows:
1013 * ```
1014 * if( ! exercise_key( ... ) ) goto exit;
1015 * ```
1016 *
1017 * \param handle The key to exercise. It should be capable of performing
1018 * \p alg.
1019 * \param usage The usage flags to assume.
1020 * \param alg The algorithm to exercise.
1021 *
1022 * \retval 0 The key failed the smoke tests.
1023 * \retval 1 The key passed the smoke tests.
1024 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001025static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001026 psa_key_usage_t usage,
1027 psa_algorithm_t alg )
1028{
1029 int ok;
1030 if( alg == 0 )
1031 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1032 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001033 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001034 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001035 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001036 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001038 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001039 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001040 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001041 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001042 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001043 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001044 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1045 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001046 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001047 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001048 else
1049 {
1050 char message[40];
1051 mbedtls_snprintf( message, sizeof( message ),
1052 "No code to exercise alg=0x%08lx",
1053 (unsigned long) alg );
1054 test_fail( message, __LINE__, __FILE__ );
1055 ok = 0;
1056 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001057
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001058 ok = ok && exercise_export_key( handle, usage );
1059 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001060
Gilles Peskine02b75072018-07-01 22:31:34 +02001061 return( ok );
1062}
1063
Gilles Peskine10df3412018-10-25 22:35:43 +02001064static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1065 psa_algorithm_t alg )
1066{
1067 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1068 {
1069 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1070 PSA_KEY_USAGE_VERIFY :
1071 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1072 }
1073 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1074 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1075 {
1076 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1077 PSA_KEY_USAGE_ENCRYPT :
1078 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1079 }
1080 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1081 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1082 {
1083 return( PSA_KEY_USAGE_DERIVE );
1084 }
1085 else
1086 {
1087 return( 0 );
1088 }
1089
1090}
Darryl Green0c6575a2018-11-07 16:05:30 +00001091
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001092static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1093{
1094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1095 uint8_t buffer[1];
1096 size_t length;
1097 int ok = 0;
1098
1099 psa_make_key_persistent( &attributes, 0x6964, PSA_KEY_LIFETIME_PERSISTENT );
1100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1101 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1102 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1103 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1104 PSA_ERROR_INVALID_HANDLE );
1105 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001106 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001107 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1108 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1109 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1110 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1111
1112 TEST_EQUAL( psa_export_key( handle,
1113 buffer, sizeof( buffer ), &length ),
1114 PSA_ERROR_INVALID_HANDLE );
1115 TEST_EQUAL( psa_export_public_key( handle,
1116 buffer, sizeof( buffer ), &length ),
1117 PSA_ERROR_INVALID_HANDLE );
1118
1119 TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
1120 TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE );
1121
1122 ok = 1;
1123
1124exit:
1125 psa_reset_key_attributes( &attributes );
1126 return( ok );
1127}
1128
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001129/* An overapproximation of the amount of storage needed for a key of the
1130 * given type and with the given content. The API doesn't make it easy
1131 * to find a good value for the size. The current implementation doesn't
1132 * care about the value anyway. */
1133#define KEY_BITS_FROM_DATA( type, data ) \
1134 ( data )->len
1135
Darryl Green0c6575a2018-11-07 16:05:30 +00001136typedef enum {
1137 IMPORT_KEY = 0,
1138 GENERATE_KEY = 1,
1139 DERIVE_KEY = 2
1140} generate_method;
1141
Gilles Peskinee59236f2018-01-27 23:32:46 +01001142/* END_HEADER */
1143
1144/* BEGIN_DEPENDENCIES
1145 * depends_on:MBEDTLS_PSA_CRYPTO_C
1146 * END_DEPENDENCIES
1147 */
1148
1149/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001150void static_checks( )
1151{
1152 size_t max_truncated_mac_size =
1153 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1154
1155 /* Check that the length for a truncated MAC always fits in the algorithm
1156 * encoding. The shifted mask is the maximum truncated value. The
1157 * untruncated algorithm may be one byte larger. */
1158 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1159}
1160/* END_CASE */
1161
1162/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001163void attributes_set_get( int id_arg, int lifetime_arg,
1164 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001165 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001166{
Gilles Peskine4747d192019-04-17 15:05:45 +02001167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001168 psa_key_id_t id = id_arg;
1169 psa_key_lifetime_t lifetime = lifetime_arg;
1170 psa_key_usage_t usage_flags = usage_flags_arg;
1171 psa_algorithm_t alg = alg_arg;
1172 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001173 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001174
1175 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1176 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1177 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1178 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1179 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001180 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001181
1182 psa_make_key_persistent( &attributes, id, lifetime );
1183 psa_set_key_usage_flags( &attributes, usage_flags );
1184 psa_set_key_algorithm( &attributes, alg );
1185 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001186 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001187
1188 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1189 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1190 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1191 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1192 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001193 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001194
1195 psa_reset_key_attributes( &attributes );
1196
1197 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1198 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1199 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1200 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1201 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001202 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001203}
1204/* END_CASE */
1205
1206/* BEGIN_CASE */
1207void import( data_t *data, int type_arg, int expected_status_arg )
1208{
1209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1210 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001211 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001212 psa_key_type_t type = type_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001213 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001214 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001215
Gilles Peskine8817f612018-12-18 00:18:46 +01001216 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001217
Gilles Peskine4747d192019-04-17 15:05:45 +02001218 psa_set_key_type( &attributes, type );
1219 status = psa_import_key( &attributes, &handle, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001220 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001221 if( status != PSA_SUCCESS )
1222 goto exit;
1223
1224 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1225 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1226
1227 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001228 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001229
1230exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001231 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001232 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233 mbedtls_psa_crypto_free( );
1234}
1235/* END_CASE */
1236
1237/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001238void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1239{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001240 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001241 size_t bits = bits_arg;
1242 psa_status_t expected_status = expected_status_arg;
1243 psa_status_t status;
1244 psa_key_type_t type =
1245 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1246 size_t buffer_size = /* Slight overapproximations */
1247 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001248 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001249 unsigned char *p;
1250 int ret;
1251 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001253
Gilles Peskine8817f612018-12-18 00:18:46 +01001254 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001255 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001256
1257 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1258 bits, keypair ) ) >= 0 );
1259 length = ret;
1260
1261 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001262 psa_set_key_type( &attributes, type );
1263 status = psa_import_key( &attributes, &handle, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001264 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001265 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001266 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001267
1268exit:
1269 mbedtls_free( buffer );
1270 mbedtls_psa_crypto_free( );
1271}
1272/* END_CASE */
1273
1274/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001275void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001276 int type_arg,
1277 int alg_arg,
1278 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001279 int expected_bits,
1280 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001281 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001282 int canonical_input )
1283{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001284 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001286 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001287 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001288 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001289 unsigned char *exported = NULL;
1290 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001291 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001292 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001293 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001294 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001295 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001296
Moran Pekercb088e72018-07-17 17:36:59 +03001297 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001298 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001300 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001301 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302
Gilles Peskine4747d192019-04-17 15:05:45 +02001303 psa_set_key_usage_flags( &attributes, usage_arg );
1304 psa_set_key_algorithm( &attributes, alg );
1305 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001306
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001308 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001309
1310 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001311 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1312 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1313 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001314
1315 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001316 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001317 exported, export_size,
1318 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001319 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001320
1321 /* The exported length must be set by psa_export_key() to a value between 0
1322 * and export_size. On errors, the exported length must be 0. */
1323 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1324 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1325 TEST_ASSERT( exported_length <= export_size );
1326
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001327 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001328 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001330 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001331 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001332 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001333 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001334
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001335 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001336 goto exit;
1337
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001338 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001339 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340 else
1341 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001342 psa_key_handle_t handle2;
Gilles Peskine4747d192019-04-17 15:05:45 +02001343 PSA_ASSERT( psa_import_key( &attributes, &handle2,
1344 exported, exported_length ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001345 PSA_ASSERT( psa_export_key( handle2,
1346 reexported,
1347 export_size,
1348 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001349 ASSERT_COMPARE( exported, exported_length,
1350 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001351 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001353 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001354
1355destroy:
1356 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001357 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001358 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001359
1360exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001361 mbedtls_free( exported );
1362 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001363 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364 mbedtls_psa_crypto_free( );
1365}
1366/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001367
Moran Pekerf709f4a2018-06-06 17:26:04 +03001368/* BEGIN_CASE */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001369void invalid_handle( int handle )
Moran Peker28a38e62018-11-07 16:18:24 +02001370{
Gilles Peskine8817f612018-12-18 00:18:46 +01001371 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001372 test_operations_on_invalid_handle( handle );
Moran Peker28a38e62018-11-07 16:18:24 +02001373
1374exit:
1375 mbedtls_psa_crypto_free( );
1376}
1377/* END_CASE */
1378
1379/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001380void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001381 int type_arg,
1382 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001383 int export_size_delta,
1384 int expected_export_status_arg,
1385 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001386{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001387 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001388 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001389 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001390 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001391 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001392 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001393 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001394 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001396
Gilles Peskine8817f612018-12-18 00:18:46 +01001397 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001398
Gilles Peskine4747d192019-04-17 15:05:45 +02001399 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1400 psa_set_key_algorithm( &attributes, alg );
1401 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001402
1403 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001404 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001405
Gilles Peskine49c25912018-10-29 15:15:31 +01001406 /* Export the public key */
1407 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001408 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001409 exported, export_size,
1410 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001411 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001412 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001413 {
1414 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1415 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001416 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1417 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001418 TEST_ASSERT( expected_public_key->len <=
1419 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001420 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1421 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001422 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001423
1424exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001425 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001426 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001427 psa_reset_key_attributes( &attributes );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001428 mbedtls_psa_crypto_free( );
1429}
1430/* END_CASE */
1431
Gilles Peskine20035e32018-02-03 22:44:14 +01001432/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001433void import_and_exercise_key( data_t *data,
1434 int type_arg,
1435 int bits_arg,
1436 int alg_arg )
1437{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001438 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001439 psa_key_type_t type = type_arg;
1440 size_t bits = bits_arg;
1441 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001442 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001444 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001445
Gilles Peskine8817f612018-12-18 00:18:46 +01001446 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001447
Gilles Peskine4747d192019-04-17 15:05:45 +02001448 psa_set_key_usage_flags( &attributes, usage );
1449 psa_set_key_algorithm( &attributes, alg );
1450 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001451
1452 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001453 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001454
1455 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001456 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1457 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1458 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001459
1460 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001461 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001462 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001463
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001464 PSA_ASSERT( psa_destroy_key( handle ) );
1465 test_operations_on_invalid_handle( handle );
1466
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001467exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001468 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001469 psa_reset_key_attributes( &got_attributes );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001470 mbedtls_psa_crypto_free( );
1471}
1472/* END_CASE */
1473
1474/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001475void key_policy( int usage_arg, int alg_arg )
1476{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001477 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001478 psa_algorithm_t alg = alg_arg;
1479 psa_key_usage_t usage = usage_arg;
1480 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1481 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001483
1484 memset( key, 0x2a, sizeof( key ) );
1485
Gilles Peskine8817f612018-12-18 00:18:46 +01001486 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001487
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001488 psa_set_key_usage_flags( &attributes, usage );
1489 psa_set_key_algorithm( &attributes, alg );
1490 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001491
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001492 PSA_ASSERT( psa_import_key( &attributes, &handle, key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001493
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001494 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1495 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1496 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1497 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001498
1499exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001500 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001501 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001502 mbedtls_psa_crypto_free( );
1503}
1504/* END_CASE */
1505
1506/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001507void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001508{
1509 /* Test each valid way of initializing the object, except for `= {0}`, as
1510 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1511 * though it's OK by the C standard. We could test for this, but we'd need
1512 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001513 psa_key_attributes_t func = psa_key_attributes_init( );
1514 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1515 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001516
1517 memset( &zero, 0, sizeof( zero ) );
1518
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001519 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1520 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1521 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001522
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001523 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1524 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1525 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1526
1527 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1528 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1529 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1530
1531 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1532 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1533 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1534
1535 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1536 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1537 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001538}
1539/* END_CASE */
1540
1541/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001542void mac_key_policy( int policy_usage,
1543 int policy_alg,
1544 int key_type,
1545 data_t *key_data,
1546 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001547{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001548 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001549 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001550 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001551 psa_status_t status;
1552 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001553
Gilles Peskine8817f612018-12-18 00:18:46 +01001554 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001555
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001556 psa_set_key_usage_flags( &attributes, policy_usage );
1557 psa_set_key_algorithm( &attributes, policy_alg );
1558 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001559
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001560 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001561 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001562
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564 if( policy_alg == exercise_alg &&
1565 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001566 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001567 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001568 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001569 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001570
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001571 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001572 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001573 if( policy_alg == exercise_alg &&
1574 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001575 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001576 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001577 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001578
1579exit:
1580 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001581 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001582 mbedtls_psa_crypto_free( );
1583}
1584/* END_CASE */
1585
1586/* BEGIN_CASE */
1587void cipher_key_policy( int policy_usage,
1588 int policy_alg,
1589 int key_type,
1590 data_t *key_data,
1591 int exercise_alg )
1592{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001593 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001594 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001595 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001596 psa_status_t status;
1597
Gilles Peskine8817f612018-12-18 00:18:46 +01001598 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001599
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001600 psa_set_key_usage_flags( &attributes, policy_usage );
1601 psa_set_key_algorithm( &attributes, policy_alg );
1602 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001604 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001605 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001606
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001607 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001608 if( policy_alg == exercise_alg &&
1609 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001611 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001612 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001613 psa_cipher_abort( &operation );
1614
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001615 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616 if( policy_alg == exercise_alg &&
1617 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001618 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001620 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001621
1622exit:
1623 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001624 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001625 mbedtls_psa_crypto_free( );
1626}
1627/* END_CASE */
1628
1629/* BEGIN_CASE */
1630void aead_key_policy( int policy_usage,
1631 int policy_alg,
1632 int key_type,
1633 data_t *key_data,
1634 int nonce_length_arg,
1635 int tag_length_arg,
1636 int exercise_alg )
1637{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001638 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001639 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001640 psa_status_t status;
1641 unsigned char nonce[16] = {0};
1642 size_t nonce_length = nonce_length_arg;
1643 unsigned char tag[16];
1644 size_t tag_length = tag_length_arg;
1645 size_t output_length;
1646
1647 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1648 TEST_ASSERT( tag_length <= sizeof( tag ) );
1649
Gilles Peskine8817f612018-12-18 00:18:46 +01001650 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001651
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001652 psa_set_key_usage_flags( &attributes, policy_usage );
1653 psa_set_key_algorithm( &attributes, policy_alg );
1654 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001655
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001656 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001657 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001658
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001659 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001660 nonce, nonce_length,
1661 NULL, 0,
1662 NULL, 0,
1663 tag, tag_length,
1664 &output_length );
1665 if( policy_alg == exercise_alg &&
1666 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001667 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001668 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001669 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001670
1671 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001672 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001673 nonce, nonce_length,
1674 NULL, 0,
1675 tag, tag_length,
1676 NULL, 0,
1677 &output_length );
1678 if( policy_alg == exercise_alg &&
1679 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001680 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001681 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001682 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001683
1684exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001685 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001686 mbedtls_psa_crypto_free( );
1687}
1688/* END_CASE */
1689
1690/* BEGIN_CASE */
1691void asymmetric_encryption_key_policy( int policy_usage,
1692 int policy_alg,
1693 int key_type,
1694 data_t *key_data,
1695 int exercise_alg )
1696{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001697 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001699 psa_status_t status;
1700 size_t key_bits;
1701 size_t buffer_length;
1702 unsigned char *buffer = NULL;
1703 size_t output_length;
1704
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001706
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001707 psa_set_key_usage_flags( &attributes, policy_usage );
1708 psa_set_key_algorithm( &attributes, policy_alg );
1709 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001710
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001711 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001712 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001713
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001714 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1715 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001716 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1717 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001718 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001719
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001720 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001721 NULL, 0,
1722 NULL, 0,
1723 buffer, buffer_length,
1724 &output_length );
1725 if( policy_alg == exercise_alg &&
1726 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001727 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001728 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001729 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001730
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001731 if( buffer_length != 0 )
1732 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001733 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001734 buffer, buffer_length,
1735 NULL, 0,
1736 buffer, buffer_length,
1737 &output_length );
1738 if( policy_alg == exercise_alg &&
1739 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001740 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001741 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001742 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001743
1744exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001745 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001746 psa_reset_key_attributes( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001747 mbedtls_psa_crypto_free( );
1748 mbedtls_free( buffer );
1749}
1750/* END_CASE */
1751
1752/* BEGIN_CASE */
1753void asymmetric_signature_key_policy( int policy_usage,
1754 int policy_alg,
1755 int key_type,
1756 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001757 int exercise_alg,
1758 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001759{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001760 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001761 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001762 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001763 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1764 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1765 * compatible with the policy and `payload_length_arg` is supposed to be
1766 * a valid input length to sign. If `payload_length_arg <= 0`,
1767 * `exercise_alg` is supposed to be forbidden by the policy. */
1768 int compatible_alg = payload_length_arg > 0;
1769 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001770 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1771 size_t signature_length;
1772
Gilles Peskine8817f612018-12-18 00:18:46 +01001773 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001774
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001775 psa_set_key_usage_flags( &attributes, policy_usage );
1776 psa_set_key_algorithm( &attributes, policy_alg );
1777 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001778
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001779 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001781
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001782 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001783 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001784 signature, sizeof( signature ),
1785 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001786 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001787 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001788 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001789 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001790
1791 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001792 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001793 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001794 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001795 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001796 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001797 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001798 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001799
1800exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001801 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001802 mbedtls_psa_crypto_free( );
1803}
1804/* END_CASE */
1805
1806/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001807void derive_key_policy( int policy_usage,
1808 int policy_alg,
1809 int key_type,
1810 data_t *key_data,
1811 int exercise_alg )
1812{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001813 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001815 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1816 psa_status_t status;
1817
Gilles Peskine8817f612018-12-18 00:18:46 +01001818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001819
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001820 psa_set_key_usage_flags( &attributes, policy_usage );
1821 psa_set_key_algorithm( &attributes, policy_alg );
1822 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001823
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001824 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001825 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001826
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001827 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001828 exercise_alg,
1829 NULL, 0,
1830 NULL, 0,
1831 1 );
1832 if( policy_alg == exercise_alg &&
1833 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001834 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001835 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001836 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001837
1838exit:
1839 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001840 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001841 mbedtls_psa_crypto_free( );
1842}
1843/* END_CASE */
1844
1845/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001846void agreement_key_policy( int policy_usage,
1847 int policy_alg,
1848 int key_type_arg,
1849 data_t *key_data,
1850 int exercise_alg )
1851{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001852 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001854 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001855 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1856 psa_status_t status;
1857
Gilles Peskine8817f612018-12-18 00:18:46 +01001858 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001859
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001860 psa_set_key_usage_flags( &attributes, policy_usage );
1861 psa_set_key_algorithm( &attributes, policy_alg );
1862 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001863
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001864 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001865 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001866
Gilles Peskine969c5d62019-01-16 15:53:06 +01001867 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
1868 status = key_agreement_with_self( &generator, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001869
Gilles Peskine01d718c2018-09-18 12:01:02 +02001870 if( policy_alg == exercise_alg &&
1871 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001872 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001873 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001874 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001875
1876exit:
1877 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001878 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001879 mbedtls_psa_crypto_free( );
1880}
1881/* END_CASE */
1882
1883/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001884void raw_agreement_key_policy( int policy_usage,
1885 int policy_alg,
1886 int key_type_arg,
1887 data_t *key_data,
1888 int exercise_alg )
1889{
1890 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001892 psa_key_type_t key_type = key_type_arg;
1893 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1894 psa_status_t status;
1895
1896 PSA_ASSERT( psa_crypto_init( ) );
1897
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001898 psa_set_key_usage_flags( &attributes, policy_usage );
1899 psa_set_key_algorithm( &attributes, policy_alg );
1900 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001901
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001902 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001903 key_data->x, key_data->len ) );
1904
1905 status = raw_key_agreement_with_self( exercise_alg, handle );
1906
1907 if( policy_alg == exercise_alg &&
1908 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1909 PSA_ASSERT( status );
1910 else
1911 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1912
1913exit:
1914 psa_generator_abort( &generator );
1915 psa_destroy_key( handle );
1916 mbedtls_psa_crypto_free( );
1917}
1918/* END_CASE */
1919
1920/* BEGIN_CASE */
Gilles Peskineca25db92019-04-19 11:43:08 +02001921void copy_key( int source_usage_arg, int source_alg_arg,
1922 int type_arg, data_t *material,
1923 int copy_attributes,
1924 int target_usage_arg, int target_alg_arg,
1925 int expected_status_arg,
1926 int expected_usage_arg, int expected_alg_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001927{
Gilles Peskineca25db92019-04-19 11:43:08 +02001928 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1929 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001930 psa_key_usage_t expected_usage = expected_usage_arg;
1931 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02001932 psa_key_handle_t source_handle = 0;
1933 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001934 uint8_t *export_buffer = NULL;
1935
Gilles Peskine57ab7212019-01-28 13:03:09 +01001936 PSA_ASSERT( psa_crypto_init( ) );
1937
Gilles Peskineca25db92019-04-19 11:43:08 +02001938 /* Prepare the source key. */
1939 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1940 psa_set_key_algorithm( &source_attributes, source_alg_arg );
1941 psa_set_key_type( &source_attributes, type_arg );
1942 PSA_ASSERT( psa_import_key( &source_attributes, &source_handle,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001943 material->x, material->len ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001944 /* Retrieve the key size. */
1945 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001946
Gilles Peskineca25db92019-04-19 11:43:08 +02001947 /* Prepare the target attributes. */
1948 if( copy_attributes )
1949 target_attributes = source_attributes;
1950 if( target_usage_arg != -1 )
1951 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1952 if( target_alg_arg != -1 )
1953 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001954
1955 /* Copy the key. */
Gilles Peskineca25db92019-04-19 11:43:08 +02001956 TEST_EQUAL( psa_copy_key( source_handle,
1957 &target_attributes, &target_handle ),
1958 expected_status_arg );
1959 if( expected_status_arg != PSA_SUCCESS )
1960 {
1961 TEST_EQUAL( target_handle, 0 );
1962 goto exit;
1963 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001964
1965 /* Destroy the source to ensure that this doesn't affect the target. */
1966 PSA_ASSERT( psa_destroy_key( source_handle ) );
1967
1968 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02001969 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
1970 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1971 psa_get_key_type( &target_attributes ) );
1972 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1973 psa_get_key_bits( &target_attributes ) );
1974 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1975 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001976 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1977 {
1978 size_t length;
1979 ASSERT_ALLOC( export_buffer, material->len );
1980 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
1981 material->len, &length ) );
1982 ASSERT_COMPARE( material->x, material->len,
1983 export_buffer, length );
1984 }
1985 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
1986 goto exit;
1987
1988 PSA_ASSERT( psa_close_key( target_handle ) );
1989
1990exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001991 psa_reset_key_attributes( &source_attributes );
1992 psa_reset_key_attributes( &target_attributes );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001993 mbedtls_psa_crypto_free( );
1994 mbedtls_free( export_buffer );
1995}
1996/* END_CASE */
1997
1998/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001999void hash_operation_init( )
2000{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002001 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002002 /* Test each valid way of initializing the object, except for `= {0}`, as
2003 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2004 * though it's OK by the C standard. We could test for this, but we'd need
2005 * to supress the Clang warning for the test. */
2006 psa_hash_operation_t func = psa_hash_operation_init( );
2007 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2008 psa_hash_operation_t zero;
2009
2010 memset( &zero, 0, sizeof( zero ) );
2011
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002012 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002013 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2014 PSA_ERROR_BAD_STATE );
2015 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2016 PSA_ERROR_BAD_STATE );
2017 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2018 PSA_ERROR_BAD_STATE );
2019
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002020 /* A default hash operation should be abortable without error. */
2021 PSA_ASSERT( psa_hash_abort( &func ) );
2022 PSA_ASSERT( psa_hash_abort( &init ) );
2023 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002024}
2025/* END_CASE */
2026
2027/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002028void hash_setup( int alg_arg,
2029 int expected_status_arg )
2030{
2031 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002032 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002033 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002034 psa_status_t status;
2035
Gilles Peskine8817f612018-12-18 00:18:46 +01002036 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002037
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002038 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002039 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002040
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002041 /* Whether setup succeeded or failed, abort must succeed. */
2042 PSA_ASSERT( psa_hash_abort( &operation ) );
2043
2044 /* If setup failed, reproduce the failure, so as to
2045 * test the resulting state of the operation object. */
2046 if( status != PSA_SUCCESS )
2047 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2048
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002049 /* Now the operation object should be reusable. */
2050#if defined(KNOWN_SUPPORTED_HASH_ALG)
2051 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2052 PSA_ASSERT( psa_hash_abort( &operation ) );
2053#endif
2054
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002055exit:
2056 mbedtls_psa_crypto_free( );
2057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002061void hash_bad_order( )
2062{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002063 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002064 unsigned char input[] = "";
2065 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002066 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002067 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2068 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2069 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002070 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002071 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002072 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002073
Gilles Peskine8817f612018-12-18 00:18:46 +01002074 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002075
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002076 /* Call setup twice in a row. */
2077 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2078 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2079 PSA_ERROR_BAD_STATE );
2080 PSA_ASSERT( psa_hash_abort( &operation ) );
2081
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002082 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002083 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002084 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002085 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002086
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002087 /* Call update after finish. */
2088 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2089 PSA_ASSERT( psa_hash_finish( &operation,
2090 hash, sizeof( hash ), &hash_len ) );
2091 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002092 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002093 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002094
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002095 /* Call verify without calling setup beforehand. */
2096 TEST_EQUAL( psa_hash_verify( &operation,
2097 valid_hash, sizeof( valid_hash ) ),
2098 PSA_ERROR_BAD_STATE );
2099 PSA_ASSERT( psa_hash_abort( &operation ) );
2100
2101 /* Call verify after finish. */
2102 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2103 PSA_ASSERT( psa_hash_finish( &operation,
2104 hash, sizeof( hash ), &hash_len ) );
2105 TEST_EQUAL( psa_hash_verify( &operation,
2106 valid_hash, sizeof( valid_hash ) ),
2107 PSA_ERROR_BAD_STATE );
2108 PSA_ASSERT( psa_hash_abort( &operation ) );
2109
2110 /* Call verify twice in a row. */
2111 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2112 PSA_ASSERT( psa_hash_verify( &operation,
2113 valid_hash, sizeof( valid_hash ) ) );
2114 TEST_EQUAL( psa_hash_verify( &operation,
2115 valid_hash, sizeof( valid_hash ) ),
2116 PSA_ERROR_BAD_STATE );
2117 PSA_ASSERT( psa_hash_abort( &operation ) );
2118
2119 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002120 TEST_EQUAL( psa_hash_finish( &operation,
2121 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002122 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002123 PSA_ASSERT( psa_hash_abort( &operation ) );
2124
2125 /* Call finish twice in a row. */
2126 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2127 PSA_ASSERT( psa_hash_finish( &operation,
2128 hash, sizeof( hash ), &hash_len ) );
2129 TEST_EQUAL( psa_hash_finish( &operation,
2130 hash, sizeof( hash ), &hash_len ),
2131 PSA_ERROR_BAD_STATE );
2132 PSA_ASSERT( psa_hash_abort( &operation ) );
2133
2134 /* Call finish after calling verify. */
2135 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2136 PSA_ASSERT( psa_hash_verify( &operation,
2137 valid_hash, sizeof( valid_hash ) ) );
2138 TEST_EQUAL( psa_hash_finish( &operation,
2139 hash, sizeof( hash ), &hash_len ),
2140 PSA_ERROR_BAD_STATE );
2141 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002142
2143exit:
2144 mbedtls_psa_crypto_free( );
2145}
2146/* END_CASE */
2147
itayzafrir27e69452018-11-01 14:26:34 +02002148/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2149void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002150{
2151 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002152 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2153 * appended to it */
2154 unsigned char hash[] = {
2155 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2156 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2157 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002158 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002159 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002160
Gilles Peskine8817f612018-12-18 00:18:46 +01002161 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002162
itayzafrir27e69452018-11-01 14:26:34 +02002163 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002164 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002165 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002166 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002167
itayzafrir27e69452018-11-01 14:26:34 +02002168 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002169 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002170 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002171 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002172
itayzafrir27e69452018-11-01 14:26:34 +02002173 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002174 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002175 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002176 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002177
itayzafrirec93d302018-10-18 18:01:10 +03002178exit:
2179 mbedtls_psa_crypto_free( );
2180}
2181/* END_CASE */
2182
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002183/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2184void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002185{
2186 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002187 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002188 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002189 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002190 size_t hash_len;
2191
Gilles Peskine8817f612018-12-18 00:18:46 +01002192 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002193
itayzafrir58028322018-10-25 10:22:01 +03002194 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002195 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002196 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002197 hash, expected_size - 1, &hash_len ),
2198 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002199
2200exit:
2201 mbedtls_psa_crypto_free( );
2202}
2203/* END_CASE */
2204
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002205/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2206void hash_clone_source_state( )
2207{
2208 psa_algorithm_t alg = PSA_ALG_SHA_256;
2209 unsigned char hash[PSA_HASH_MAX_SIZE];
2210 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2211 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2212 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2213 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2214 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2215 size_t hash_len;
2216
2217 PSA_ASSERT( psa_crypto_init( ) );
2218 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2219
2220 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2221 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2222 PSA_ASSERT( psa_hash_finish( &op_finished,
2223 hash, sizeof( hash ), &hash_len ) );
2224 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2225 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2226
2227 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2228 PSA_ERROR_BAD_STATE );
2229
2230 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2231 PSA_ASSERT( psa_hash_finish( &op_init,
2232 hash, sizeof( hash ), &hash_len ) );
2233 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2234 PSA_ASSERT( psa_hash_finish( &op_finished,
2235 hash, sizeof( hash ), &hash_len ) );
2236 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2237 PSA_ASSERT( psa_hash_finish( &op_aborted,
2238 hash, sizeof( hash ), &hash_len ) );
2239
2240exit:
2241 psa_hash_abort( &op_source );
2242 psa_hash_abort( &op_init );
2243 psa_hash_abort( &op_setup );
2244 psa_hash_abort( &op_finished );
2245 psa_hash_abort( &op_aborted );
2246 mbedtls_psa_crypto_free( );
2247}
2248/* END_CASE */
2249
2250/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2251void hash_clone_target_state( )
2252{
2253 psa_algorithm_t alg = PSA_ALG_SHA_256;
2254 unsigned char hash[PSA_HASH_MAX_SIZE];
2255 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2256 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2257 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2258 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2259 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2260 size_t hash_len;
2261
2262 PSA_ASSERT( psa_crypto_init( ) );
2263
2264 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2265 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2266 PSA_ASSERT( psa_hash_finish( &op_finished,
2267 hash, sizeof( hash ), &hash_len ) );
2268 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2269 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2270
2271 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2272 PSA_ASSERT( psa_hash_finish( &op_target,
2273 hash, sizeof( hash ), &hash_len ) );
2274
2275 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2276 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2277 PSA_ERROR_BAD_STATE );
2278 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2279 PSA_ERROR_BAD_STATE );
2280
2281exit:
2282 psa_hash_abort( &op_target );
2283 psa_hash_abort( &op_init );
2284 psa_hash_abort( &op_setup );
2285 psa_hash_abort( &op_finished );
2286 psa_hash_abort( &op_aborted );
2287 mbedtls_psa_crypto_free( );
2288}
2289/* END_CASE */
2290
itayzafrir58028322018-10-25 10:22:01 +03002291/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002292void mac_operation_init( )
2293{
Jaeden Amero252ef282019-02-15 14:05:35 +00002294 const uint8_t input[1] = { 0 };
2295
Jaeden Amero769ce272019-01-04 11:48:03 +00002296 /* Test each valid way of initializing the object, except for `= {0}`, as
2297 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2298 * though it's OK by the C standard. We could test for this, but we'd need
2299 * to supress the Clang warning for the test. */
2300 psa_mac_operation_t func = psa_mac_operation_init( );
2301 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2302 psa_mac_operation_t zero;
2303
2304 memset( &zero, 0, sizeof( zero ) );
2305
Jaeden Amero252ef282019-02-15 14:05:35 +00002306 /* A freshly-initialized MAC operation should not be usable. */
2307 TEST_EQUAL( psa_mac_update( &func,
2308 input, sizeof( input ) ),
2309 PSA_ERROR_BAD_STATE );
2310 TEST_EQUAL( psa_mac_update( &init,
2311 input, sizeof( input ) ),
2312 PSA_ERROR_BAD_STATE );
2313 TEST_EQUAL( psa_mac_update( &zero,
2314 input, sizeof( input ) ),
2315 PSA_ERROR_BAD_STATE );
2316
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002317 /* A default MAC operation should be abortable without error. */
2318 PSA_ASSERT( psa_mac_abort( &func ) );
2319 PSA_ASSERT( psa_mac_abort( &init ) );
2320 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002321}
2322/* END_CASE */
2323
2324/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002325void mac_setup( int key_type_arg,
2326 data_t *key,
2327 int alg_arg,
2328 int expected_status_arg )
2329{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002330 psa_key_type_t key_type = key_type_arg;
2331 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002332 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002333 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002334 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2335#if defined(KNOWN_SUPPORTED_MAC_ALG)
2336 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2337#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002338
Gilles Peskine8817f612018-12-18 00:18:46 +01002339 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002341 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2342 &operation, &status ) )
2343 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002344 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002346 /* The operation object should be reusable. */
2347#if defined(KNOWN_SUPPORTED_MAC_ALG)
2348 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2349 smoke_test_key_data,
2350 sizeof( smoke_test_key_data ),
2351 KNOWN_SUPPORTED_MAC_ALG,
2352 &operation, &status ) )
2353 goto exit;
2354 TEST_EQUAL( status, PSA_SUCCESS );
2355#endif
2356
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002357exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002358 mbedtls_psa_crypto_free( );
2359}
2360/* END_CASE */
2361
2362/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002363void mac_bad_order( )
2364{
2365 psa_key_handle_t handle = 0;
2366 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2367 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2368 const uint8_t key[] = {
2369 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2370 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2371 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002373 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2374 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2375 size_t sign_mac_length = 0;
2376 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2377 const uint8_t verify_mac[] = {
2378 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2379 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2380 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2381
2382 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002383 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2384 psa_set_key_algorithm( &attributes, alg );
2385 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002387 PSA_ASSERT( psa_import_key( &attributes, &handle,
Jaeden Amero252ef282019-02-15 14:05:35 +00002388 key, sizeof(key) ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002389
Jaeden Amero252ef282019-02-15 14:05:35 +00002390 /* Call update without calling setup beforehand. */
2391 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2392 PSA_ERROR_BAD_STATE );
2393 PSA_ASSERT( psa_mac_abort( &operation ) );
2394
2395 /* Call sign finish without calling setup beforehand. */
2396 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2397 &sign_mac_length),
2398 PSA_ERROR_BAD_STATE );
2399 PSA_ASSERT( psa_mac_abort( &operation ) );
2400
2401 /* Call verify finish without calling setup beforehand. */
2402 TEST_EQUAL( psa_mac_verify_finish( &operation,
2403 verify_mac, sizeof( verify_mac ) ),
2404 PSA_ERROR_BAD_STATE );
2405 PSA_ASSERT( psa_mac_abort( &operation ) );
2406
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002407 /* Call setup twice in a row. */
2408 PSA_ASSERT( psa_mac_sign_setup( &operation,
2409 handle, alg ) );
2410 TEST_EQUAL( psa_mac_sign_setup( &operation,
2411 handle, alg ),
2412 PSA_ERROR_BAD_STATE );
2413 PSA_ASSERT( psa_mac_abort( &operation ) );
2414
Jaeden Amero252ef282019-02-15 14:05:35 +00002415 /* Call update after sign finish. */
2416 PSA_ASSERT( psa_mac_sign_setup( &operation,
2417 handle, alg ) );
2418 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2419 PSA_ASSERT( psa_mac_sign_finish( &operation,
2420 sign_mac, sizeof( sign_mac ),
2421 &sign_mac_length ) );
2422 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2423 PSA_ERROR_BAD_STATE );
2424 PSA_ASSERT( psa_mac_abort( &operation ) );
2425
2426 /* Call update after verify finish. */
2427 PSA_ASSERT( psa_mac_verify_setup( &operation,
2428 handle, alg ) );
2429 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2430 PSA_ASSERT( psa_mac_verify_finish( &operation,
2431 verify_mac, sizeof( verify_mac ) ) );
2432 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2433 PSA_ERROR_BAD_STATE );
2434 PSA_ASSERT( psa_mac_abort( &operation ) );
2435
2436 /* Call sign finish twice in a row. */
2437 PSA_ASSERT( psa_mac_sign_setup( &operation,
2438 handle, alg ) );
2439 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2440 PSA_ASSERT( psa_mac_sign_finish( &operation,
2441 sign_mac, sizeof( sign_mac ),
2442 &sign_mac_length ) );
2443 TEST_EQUAL( psa_mac_sign_finish( &operation,
2444 sign_mac, sizeof( sign_mac ),
2445 &sign_mac_length ),
2446 PSA_ERROR_BAD_STATE );
2447 PSA_ASSERT( psa_mac_abort( &operation ) );
2448
2449 /* Call verify finish twice in a row. */
2450 PSA_ASSERT( psa_mac_verify_setup( &operation,
2451 handle, alg ) );
2452 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2453 PSA_ASSERT( psa_mac_verify_finish( &operation,
2454 verify_mac, sizeof( verify_mac ) ) );
2455 TEST_EQUAL( psa_mac_verify_finish( &operation,
2456 verify_mac, sizeof( verify_mac ) ),
2457 PSA_ERROR_BAD_STATE );
2458 PSA_ASSERT( psa_mac_abort( &operation ) );
2459
2460 /* Setup sign but try verify. */
2461 PSA_ASSERT( psa_mac_sign_setup( &operation,
2462 handle, alg ) );
2463 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2464 TEST_EQUAL( psa_mac_verify_finish( &operation,
2465 verify_mac, sizeof( verify_mac ) ),
2466 PSA_ERROR_BAD_STATE );
2467 PSA_ASSERT( psa_mac_abort( &operation ) );
2468
2469 /* Setup verify but try sign. */
2470 PSA_ASSERT( psa_mac_verify_setup( &operation,
2471 handle, alg ) );
2472 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2473 TEST_EQUAL( psa_mac_sign_finish( &operation,
2474 sign_mac, sizeof( sign_mac ),
2475 &sign_mac_length ),
2476 PSA_ERROR_BAD_STATE );
2477 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002478
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002479exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002480 mbedtls_psa_crypto_free( );
2481}
2482/* END_CASE */
2483
2484/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002485void mac_sign( int key_type_arg,
2486 data_t *key,
2487 int alg_arg,
2488 data_t *input,
2489 data_t *expected_mac )
2490{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002491 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002492 psa_key_type_t key_type = key_type_arg;
2493 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002494 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002496 /* Leave a little extra room in the output buffer. At the end of the
2497 * test, we'll check that the implementation didn't overwrite onto
2498 * this extra room. */
2499 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2500 size_t mac_buffer_size =
2501 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2502 size_t mac_length = 0;
2503
2504 memset( actual_mac, '+', sizeof( actual_mac ) );
2505 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2506 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2507
Gilles Peskine8817f612018-12-18 00:18:46 +01002508 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002509
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002510 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2511 psa_set_key_algorithm( &attributes, alg );
2512 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002513
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002514 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002515 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002516
2517 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002518 PSA_ASSERT( psa_mac_sign_setup( &operation,
2519 handle, alg ) );
2520 PSA_ASSERT( psa_mac_update( &operation,
2521 input->x, input->len ) );
2522 PSA_ASSERT( psa_mac_sign_finish( &operation,
2523 actual_mac, mac_buffer_size,
2524 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002525
2526 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002527 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2528 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002529
2530 /* Verify that the end of the buffer is untouched. */
2531 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2532 sizeof( actual_mac ) - mac_length ) );
2533
2534exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002535 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002536 mbedtls_psa_crypto_free( );
2537}
2538/* END_CASE */
2539
2540/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002541void mac_verify( int key_type_arg,
2542 data_t *key,
2543 int alg_arg,
2544 data_t *input,
2545 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002547 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 psa_key_type_t key_type = key_type_arg;
2549 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002550 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552
Gilles Peskine69c12672018-06-28 00:07:19 +02002553 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2554
Gilles Peskine8817f612018-12-18 00:18:46 +01002555 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002557 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2558 psa_set_key_algorithm( &attributes, alg );
2559 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002560
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002561 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002562 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002563
Gilles Peskine8817f612018-12-18 00:18:46 +01002564 PSA_ASSERT( psa_mac_verify_setup( &operation,
2565 handle, alg ) );
2566 PSA_ASSERT( psa_destroy_key( handle ) );
2567 PSA_ASSERT( psa_mac_update( &operation,
2568 input->x, input->len ) );
2569 PSA_ASSERT( psa_mac_verify_finish( &operation,
2570 expected_mac->x,
2571 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572
2573exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002574 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575 mbedtls_psa_crypto_free( );
2576}
2577/* END_CASE */
2578
2579/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002580void cipher_operation_init( )
2581{
Jaeden Ameroab439972019-02-15 14:12:05 +00002582 const uint8_t input[1] = { 0 };
2583 unsigned char output[1] = { 0 };
2584 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002585 /* Test each valid way of initializing the object, except for `= {0}`, as
2586 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2587 * though it's OK by the C standard. We could test for this, but we'd need
2588 * to supress the Clang warning for the test. */
2589 psa_cipher_operation_t func = psa_cipher_operation_init( );
2590 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2591 psa_cipher_operation_t zero;
2592
2593 memset( &zero, 0, sizeof( zero ) );
2594
Jaeden Ameroab439972019-02-15 14:12:05 +00002595 /* A freshly-initialized cipher operation should not be usable. */
2596 TEST_EQUAL( psa_cipher_update( &func,
2597 input, sizeof( input ),
2598 output, sizeof( output ),
2599 &output_length ),
2600 PSA_ERROR_BAD_STATE );
2601 TEST_EQUAL( psa_cipher_update( &init,
2602 input, sizeof( input ),
2603 output, sizeof( output ),
2604 &output_length ),
2605 PSA_ERROR_BAD_STATE );
2606 TEST_EQUAL( psa_cipher_update( &zero,
2607 input, sizeof( input ),
2608 output, sizeof( output ),
2609 &output_length ),
2610 PSA_ERROR_BAD_STATE );
2611
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002612 /* A default cipher operation should be abortable without error. */
2613 PSA_ASSERT( psa_cipher_abort( &func ) );
2614 PSA_ASSERT( psa_cipher_abort( &init ) );
2615 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002616}
2617/* END_CASE */
2618
2619/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002620void cipher_setup( int key_type_arg,
2621 data_t *key,
2622 int alg_arg,
2623 int expected_status_arg )
2624{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002625 psa_key_type_t key_type = key_type_arg;
2626 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002627 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002628 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002629 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002630#if defined(KNOWN_SUPPORTED_MAC_ALG)
2631 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2632#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002633
Gilles Peskine8817f612018-12-18 00:18:46 +01002634 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002635
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002636 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2637 &operation, &status ) )
2638 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002639 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002640
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002641 /* The operation object should be reusable. */
2642#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2643 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2644 smoke_test_key_data,
2645 sizeof( smoke_test_key_data ),
2646 KNOWN_SUPPORTED_CIPHER_ALG,
2647 &operation, &status ) )
2648 goto exit;
2649 TEST_EQUAL( status, PSA_SUCCESS );
2650#endif
2651
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002652exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002653 mbedtls_psa_crypto_free( );
2654}
2655/* END_CASE */
2656
2657/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002658void cipher_bad_order( )
2659{
2660 psa_key_handle_t handle = 0;
2661 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2662 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002664 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2665 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2666 const uint8_t key[] = {
2667 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2668 0xaa, 0xaa, 0xaa, 0xaa };
2669 const uint8_t text[] = {
2670 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2671 0xbb, 0xbb, 0xbb, 0xbb };
2672 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2673 size_t length = 0;
2674
2675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002676 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2677 psa_set_key_algorithm( &attributes, alg );
2678 psa_set_key_type( &attributes, key_type );
2679 PSA_ASSERT( psa_import_key( &attributes, &handle,
Jaeden Ameroab439972019-02-15 14:12:05 +00002680 key, sizeof(key) ) );
2681
2682
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002683 /* Call encrypt setup twice in a row. */
2684 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2685 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2686 PSA_ERROR_BAD_STATE );
2687 PSA_ASSERT( psa_cipher_abort( &operation ) );
2688
2689 /* Call decrypt setup twice in a row. */
2690 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2691 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2692 PSA_ERROR_BAD_STATE );
2693 PSA_ASSERT( psa_cipher_abort( &operation ) );
2694
Jaeden Ameroab439972019-02-15 14:12:05 +00002695 /* Generate an IV without calling setup beforehand. */
2696 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2697 buffer, sizeof( buffer ),
2698 &length ),
2699 PSA_ERROR_BAD_STATE );
2700 PSA_ASSERT( psa_cipher_abort( &operation ) );
2701
2702 /* Generate an IV twice in a row. */
2703 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2704 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2705 buffer, sizeof( buffer ),
2706 &length ) );
2707 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2708 buffer, sizeof( buffer ),
2709 &length ),
2710 PSA_ERROR_BAD_STATE );
2711 PSA_ASSERT( psa_cipher_abort( &operation ) );
2712
2713 /* Generate an IV after it's already set. */
2714 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2715 PSA_ASSERT( psa_cipher_set_iv( &operation,
2716 iv, sizeof( iv ) ) );
2717 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2718 buffer, sizeof( buffer ),
2719 &length ),
2720 PSA_ERROR_BAD_STATE );
2721 PSA_ASSERT( psa_cipher_abort( &operation ) );
2722
2723 /* Set an IV without calling setup beforehand. */
2724 TEST_EQUAL( psa_cipher_set_iv( &operation,
2725 iv, sizeof( iv ) ),
2726 PSA_ERROR_BAD_STATE );
2727 PSA_ASSERT( psa_cipher_abort( &operation ) );
2728
2729 /* Set an IV after it's already set. */
2730 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2731 PSA_ASSERT( psa_cipher_set_iv( &operation,
2732 iv, sizeof( iv ) ) );
2733 TEST_EQUAL( psa_cipher_set_iv( &operation,
2734 iv, sizeof( iv ) ),
2735 PSA_ERROR_BAD_STATE );
2736 PSA_ASSERT( psa_cipher_abort( &operation ) );
2737
2738 /* Set an IV after it's already generated. */
2739 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2740 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2741 buffer, sizeof( buffer ),
2742 &length ) );
2743 TEST_EQUAL( psa_cipher_set_iv( &operation,
2744 iv, sizeof( iv ) ),
2745 PSA_ERROR_BAD_STATE );
2746 PSA_ASSERT( psa_cipher_abort( &operation ) );
2747
2748 /* Call update without calling setup beforehand. */
2749 TEST_EQUAL( psa_cipher_update( &operation,
2750 text, sizeof( text ),
2751 buffer, sizeof( buffer ),
2752 &length ),
2753 PSA_ERROR_BAD_STATE );
2754 PSA_ASSERT( psa_cipher_abort( &operation ) );
2755
2756 /* Call update without an IV where an IV is required. */
2757 TEST_EQUAL( psa_cipher_update( &operation,
2758 text, sizeof( text ),
2759 buffer, sizeof( buffer ),
2760 &length ),
2761 PSA_ERROR_BAD_STATE );
2762 PSA_ASSERT( psa_cipher_abort( &operation ) );
2763
2764 /* Call update after finish. */
2765 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2766 PSA_ASSERT( psa_cipher_set_iv( &operation,
2767 iv, sizeof( iv ) ) );
2768 PSA_ASSERT( psa_cipher_finish( &operation,
2769 buffer, sizeof( buffer ), &length ) );
2770 TEST_EQUAL( psa_cipher_update( &operation,
2771 text, sizeof( text ),
2772 buffer, sizeof( buffer ),
2773 &length ),
2774 PSA_ERROR_BAD_STATE );
2775 PSA_ASSERT( psa_cipher_abort( &operation ) );
2776
2777 /* Call finish without calling setup beforehand. */
2778 TEST_EQUAL( psa_cipher_finish( &operation,
2779 buffer, sizeof( buffer ), &length ),
2780 PSA_ERROR_BAD_STATE );
2781 PSA_ASSERT( psa_cipher_abort( &operation ) );
2782
2783 /* Call finish without an IV where an IV is required. */
2784 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2785 /* Not calling update means we are encrypting an empty buffer, which is OK
2786 * for cipher modes with padding. */
2787 TEST_EQUAL( psa_cipher_finish( &operation,
2788 buffer, sizeof( buffer ), &length ),
2789 PSA_ERROR_BAD_STATE );
2790 PSA_ASSERT( psa_cipher_abort( &operation ) );
2791
2792 /* Call finish twice in a row. */
2793 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2794 PSA_ASSERT( psa_cipher_set_iv( &operation,
2795 iv, sizeof( iv ) ) );
2796 PSA_ASSERT( psa_cipher_finish( &operation,
2797 buffer, sizeof( buffer ), &length ) );
2798 TEST_EQUAL( psa_cipher_finish( &operation,
2799 buffer, sizeof( buffer ), &length ),
2800 PSA_ERROR_BAD_STATE );
2801 PSA_ASSERT( psa_cipher_abort( &operation ) );
2802
2803exit:
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002804 mbedtls_psa_crypto_free( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805}
2806/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807
Gilles Peskine50e586b2018-06-08 14:28:46 +02002808/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002810 data_t *key,
2811 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002812 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002813{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002814 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815 psa_status_t status;
2816 psa_key_type_t key_type = key_type_arg;
2817 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002818 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002819 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002820 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002821 unsigned char *output = NULL;
2822 size_t output_buffer_size = 0;
2823 size_t function_output_length = 0;
2824 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002825 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002826 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002827
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002828 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2829 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830
Gilles Peskine8817f612018-12-18 00:18:46 +01002831 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002832
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002833 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2834 psa_set_key_algorithm( &attributes, alg );
2835 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002836
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002837 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002838 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839
Gilles Peskine8817f612018-12-18 00:18:46 +01002840 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2841 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002842
Gilles Peskine8817f612018-12-18 00:18:46 +01002843 PSA_ASSERT( psa_cipher_set_iv( &operation,
2844 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002845 output_buffer_size = ( (size_t) input->len +
2846 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002847 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002848
Gilles Peskine8817f612018-12-18 00:18:46 +01002849 PSA_ASSERT( psa_cipher_update( &operation,
2850 input->x, input->len,
2851 output, output_buffer_size,
2852 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002853 total_output_length += function_output_length;
2854 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002855 output + total_output_length,
2856 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002857 &function_output_length );
2858 total_output_length += function_output_length;
2859
Gilles Peskinefe11b722018-12-18 00:24:04 +01002860 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002861 if( expected_status == PSA_SUCCESS )
2862 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002863 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002864 ASSERT_COMPARE( expected_output->x, expected_output->len,
2865 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002866 }
2867
2868exit:
2869 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002870 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002871 mbedtls_psa_crypto_free( );
2872}
2873/* END_CASE */
2874
2875/* BEGIN_CASE */
2876void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
2877 data_t *key,
2878 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002879 int first_part_size_arg,
2880 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881 data_t *expected_output )
2882{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002883 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884 psa_key_type_t key_type = key_type_arg;
2885 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002886 size_t first_part_size = first_part_size_arg;
2887 size_t output1_length = output1_length_arg;
2888 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002890 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002891 unsigned char *output = NULL;
2892 size_t output_buffer_size = 0;
2893 size_t function_output_length = 0;
2894 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002895 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002897
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002898 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2899 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002900
Gilles Peskine8817f612018-12-18 00:18:46 +01002901 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002902
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002903 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2904 psa_set_key_algorithm( &attributes, alg );
2905 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002906
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002907 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002908 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002909
Gilles Peskine8817f612018-12-18 00:18:46 +01002910 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2911 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002912
Gilles Peskine8817f612018-12-18 00:18:46 +01002913 PSA_ASSERT( psa_cipher_set_iv( &operation,
2914 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002915 output_buffer_size = ( (size_t) input->len +
2916 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002917 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002918
Gilles Peskinee0866522019-02-19 19:44:00 +01002919 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002920 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2921 output, output_buffer_size,
2922 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002923 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002924 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002925 PSA_ASSERT( psa_cipher_update( &operation,
2926 input->x + first_part_size,
2927 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002928 output + total_output_length,
2929 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002930 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002931 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002932 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002933 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002934 output + total_output_length,
2935 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002936 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002937 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002938 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002939
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002940 ASSERT_COMPARE( expected_output->x, expected_output->len,
2941 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002942
2943exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002944 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002945 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002946 mbedtls_psa_crypto_free( );
2947}
2948/* END_CASE */
2949
2950/* BEGIN_CASE */
2951void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002952 data_t *key,
2953 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002954 int first_part_size_arg,
2955 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002956 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002957{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002958 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002959
2960 psa_key_type_t key_type = key_type_arg;
2961 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002962 size_t first_part_size = first_part_size_arg;
2963 size_t output1_length = output1_length_arg;
2964 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002965 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002966 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002967 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002968 size_t output_buffer_size = 0;
2969 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002970 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002971 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002972 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002973
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002974 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2975 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976
Gilles Peskine8817f612018-12-18 00:18:46 +01002977 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002978
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002979 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2980 psa_set_key_algorithm( &attributes, alg );
2981 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002982
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002983 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002984 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002985
Gilles Peskine8817f612018-12-18 00:18:46 +01002986 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2987 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002988
Gilles Peskine8817f612018-12-18 00:18:46 +01002989 PSA_ASSERT( psa_cipher_set_iv( &operation,
2990 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002991
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002992 output_buffer_size = ( (size_t) input->len +
2993 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002994 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002995
Gilles Peskinee0866522019-02-19 19:44:00 +01002996 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002997 PSA_ASSERT( psa_cipher_update( &operation,
2998 input->x, first_part_size,
2999 output, output_buffer_size,
3000 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003001 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003002 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003003 PSA_ASSERT( psa_cipher_update( &operation,
3004 input->x + first_part_size,
3005 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003006 output + total_output_length,
3007 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003008 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003009 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003010 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003011 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003012 output + total_output_length,
3013 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003014 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003015 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003016 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003017
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003018 ASSERT_COMPARE( expected_output->x, expected_output->len,
3019 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003020
3021exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003022 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003023 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003024 mbedtls_psa_crypto_free( );
3025}
3026/* END_CASE */
3027
Gilles Peskine50e586b2018-06-08 14:28:46 +02003028/* BEGIN_CASE */
3029void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003030 data_t *key,
3031 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003032 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003033{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003034 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003035 psa_status_t status;
3036 psa_key_type_t key_type = key_type_arg;
3037 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003038 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003039 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003040 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003041 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003042 size_t output_buffer_size = 0;
3043 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003044 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003045 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003047
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003048 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3049 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003050
Gilles Peskine8817f612018-12-18 00:18:46 +01003051 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003052
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003053 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3054 psa_set_key_algorithm( &attributes, alg );
3055 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003056
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003057 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003058 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003059
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3061 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003062
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 PSA_ASSERT( psa_cipher_set_iv( &operation,
3064 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003065
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003066 output_buffer_size = ( (size_t) input->len +
3067 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003068 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003069
Gilles Peskine8817f612018-12-18 00:18:46 +01003070 PSA_ASSERT( psa_cipher_update( &operation,
3071 input->x, input->len,
3072 output, output_buffer_size,
3073 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003074 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003075 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003076 output + total_output_length,
3077 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003078 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003079 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003080 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003081
3082 if( expected_status == PSA_SUCCESS )
3083 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003084 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003085 ASSERT_COMPARE( expected_output->x, expected_output->len,
3086 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003087 }
3088
Gilles Peskine50e586b2018-06-08 14:28:46 +02003089exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003090 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003091 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003092 mbedtls_psa_crypto_free( );
3093}
3094/* END_CASE */
3095
Gilles Peskine50e586b2018-06-08 14:28:46 +02003096/* BEGIN_CASE */
3097void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003098 data_t *key,
3099 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003100{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003101 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003102 psa_key_type_t key_type = key_type_arg;
3103 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003104 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003105 size_t iv_size = 16;
3106 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003107 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003108 size_t output1_size = 0;
3109 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003110 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003111 size_t output2_size = 0;
3112 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003113 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003114 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3115 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003116 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003117
Gilles Peskine8817f612018-12-18 00:18:46 +01003118 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003119
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003120 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3121 psa_set_key_algorithm( &attributes, alg );
3122 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003123
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003124 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003125 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003126
Gilles Peskine8817f612018-12-18 00:18:46 +01003127 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3128 handle, alg ) );
3129 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3130 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003131
Gilles Peskine8817f612018-12-18 00:18:46 +01003132 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3133 iv, iv_size,
3134 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003135 output1_size = ( (size_t) input->len +
3136 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003137 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003138
Gilles Peskine8817f612018-12-18 00:18:46 +01003139 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3140 output1, output1_size,
3141 &output1_length ) );
3142 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003143 output1 + output1_length,
3144 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003146
Gilles Peskine048b7f02018-06-08 14:20:49 +02003147 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003148
Gilles Peskine8817f612018-12-18 00:18:46 +01003149 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003150
3151 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003152 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003153
Gilles Peskine8817f612018-12-18 00:18:46 +01003154 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3155 iv, iv_length ) );
3156 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3157 output2, output2_size,
3158 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003159 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003160 PSA_ASSERT( psa_cipher_finish( &operation2,
3161 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003162 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003164
Gilles Peskine048b7f02018-06-08 14:20:49 +02003165 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003166
Gilles Peskine8817f612018-12-18 00:18:46 +01003167 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003168
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003169 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003170
3171exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003172 mbedtls_free( output1 );
3173 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003174 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003175 mbedtls_psa_crypto_free( );
3176}
3177/* END_CASE */
3178
3179/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003180void cipher_verify_output_multipart( int alg_arg,
3181 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003182 data_t *key,
3183 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003184 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003185{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003186 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003187 psa_key_type_t key_type = key_type_arg;
3188 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003189 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003190 unsigned char iv[16] = {0};
3191 size_t iv_size = 16;
3192 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003193 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003194 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003195 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003196 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003197 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003198 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003199 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003200 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3201 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003202 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003203
Gilles Peskine8817f612018-12-18 00:18:46 +01003204 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003205
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003206 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3207 psa_set_key_algorithm( &attributes, alg );
3208 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003209
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003210 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003212
Gilles Peskine8817f612018-12-18 00:18:46 +01003213 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3214 handle, alg ) );
3215 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3216 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003217
Gilles Peskine8817f612018-12-18 00:18:46 +01003218 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3219 iv, iv_size,
3220 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003221 output1_buffer_size = ( (size_t) input->len +
3222 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003223 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003224
Gilles Peskinee0866522019-02-19 19:44:00 +01003225 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003226
Gilles Peskine8817f612018-12-18 00:18:46 +01003227 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3228 output1, output1_buffer_size,
3229 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003230 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003231
Gilles Peskine8817f612018-12-18 00:18:46 +01003232 PSA_ASSERT( psa_cipher_update( &operation1,
3233 input->x + first_part_size,
3234 input->len - first_part_size,
3235 output1, output1_buffer_size,
3236 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003237 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003238
Gilles Peskine8817f612018-12-18 00:18:46 +01003239 PSA_ASSERT( psa_cipher_finish( &operation1,
3240 output1 + output1_length,
3241 output1_buffer_size - output1_length,
3242 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003243 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003244
Gilles Peskine8817f612018-12-18 00:18:46 +01003245 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003246
Gilles Peskine048b7f02018-06-08 14:20:49 +02003247 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003248 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3251 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003252
Gilles Peskine8817f612018-12-18 00:18:46 +01003253 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3254 output2, output2_buffer_size,
3255 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003256 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003257
Gilles Peskine8817f612018-12-18 00:18:46 +01003258 PSA_ASSERT( psa_cipher_update( &operation2,
3259 output1 + first_part_size,
3260 output1_length - first_part_size,
3261 output2, output2_buffer_size,
3262 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003263 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003264
Gilles Peskine8817f612018-12-18 00:18:46 +01003265 PSA_ASSERT( psa_cipher_finish( &operation2,
3266 output2 + output2_length,
3267 output2_buffer_size - output2_length,
3268 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003269 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003270
Gilles Peskine8817f612018-12-18 00:18:46 +01003271 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003272
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003273 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003274
3275exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003276 mbedtls_free( output1 );
3277 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003278 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003279 mbedtls_psa_crypto_free( );
3280}
3281/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003282
Gilles Peskine20035e32018-02-03 22:44:14 +01003283/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003284void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003285 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003286 data_t *nonce,
3287 data_t *additional_data,
3288 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003289 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003290{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003291 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003292 psa_key_type_t key_type = key_type_arg;
3293 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003294 unsigned char *output_data = NULL;
3295 size_t output_size = 0;
3296 size_t output_length = 0;
3297 unsigned char *output_data2 = NULL;
3298 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003299 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003300 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003301 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003302
Gilles Peskine4abf7412018-06-18 16:35:34 +02003303 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003304 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003305
Gilles Peskine8817f612018-12-18 00:18:46 +01003306 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003308 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3309 psa_set_key_algorithm( &attributes, alg );
3310 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003312 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003313 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003314
Gilles Peskinefe11b722018-12-18 00:24:04 +01003315 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3316 nonce->x, nonce->len,
3317 additional_data->x,
3318 additional_data->len,
3319 input_data->x, input_data->len,
3320 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003321 &output_length ),
3322 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003323
3324 if( PSA_SUCCESS == expected_result )
3325 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003326 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003327
Gilles Peskinefe11b722018-12-18 00:24:04 +01003328 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3329 nonce->x, nonce->len,
3330 additional_data->x,
3331 additional_data->len,
3332 output_data, output_length,
3333 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003334 &output_length2 ),
3335 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003336
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003337 ASSERT_COMPARE( input_data->x, input_data->len,
3338 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003340
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003342 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343 mbedtls_free( output_data );
3344 mbedtls_free( output_data2 );
3345 mbedtls_psa_crypto_free( );
3346}
3347/* END_CASE */
3348
3349/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003350void aead_encrypt( int key_type_arg, data_t *key_data,
3351 int alg_arg,
3352 data_t *nonce,
3353 data_t *additional_data,
3354 data_t *input_data,
3355 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003356{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003357 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003358 psa_key_type_t key_type = key_type_arg;
3359 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003360 unsigned char *output_data = NULL;
3361 size_t output_size = 0;
3362 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003363 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365
Gilles Peskine4abf7412018-06-18 16:35:34 +02003366 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003367 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003368
Gilles Peskine8817f612018-12-18 00:18:46 +01003369 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003370
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003371 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3372 psa_set_key_algorithm( &attributes, alg );
3373 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003374
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003375 PSA_ASSERT( psa_import_key( &attributes, &handle,
3376 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377
Gilles Peskine8817f612018-12-18 00:18:46 +01003378 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3379 nonce->x, nonce->len,
3380 additional_data->x, additional_data->len,
3381 input_data->x, input_data->len,
3382 output_data, output_size,
3383 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003384
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003385 ASSERT_COMPARE( expected_result->x, expected_result->len,
3386 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003387
Gilles Peskinea1cac842018-06-11 19:33:02 +02003388exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003389 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003391 mbedtls_psa_crypto_free( );
3392}
3393/* END_CASE */
3394
3395/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003396void aead_decrypt( int key_type_arg, data_t *key_data,
3397 int alg_arg,
3398 data_t *nonce,
3399 data_t *additional_data,
3400 data_t *input_data,
3401 data_t *expected_data,
3402 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003404 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003405 psa_key_type_t key_type = key_type_arg;
3406 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003407 unsigned char *output_data = NULL;
3408 size_t output_size = 0;
3409 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003410 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003412 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003413
Gilles Peskine4abf7412018-06-18 16:35:34 +02003414 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003415 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003416
Gilles Peskine8817f612018-12-18 00:18:46 +01003417 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003419 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3420 psa_set_key_algorithm( &attributes, alg );
3421 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003422
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003423 PSA_ASSERT( psa_import_key( &attributes, &handle,
3424 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003425
Gilles Peskinefe11b722018-12-18 00:24:04 +01003426 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3427 nonce->x, nonce->len,
3428 additional_data->x,
3429 additional_data->len,
3430 input_data->x, input_data->len,
3431 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003432 &output_length ),
3433 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003434
Gilles Peskine2d277862018-06-18 15:41:12 +02003435 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003436 ASSERT_COMPARE( expected_data->x, expected_data->len,
3437 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003438
Gilles Peskinea1cac842018-06-11 19:33:02 +02003439exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003440 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003441 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442 mbedtls_psa_crypto_free( );
3443}
3444/* END_CASE */
3445
3446/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003447void signature_size( int type_arg,
3448 int bits,
3449 int alg_arg,
3450 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003451{
3452 psa_key_type_t type = type_arg;
3453 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003454 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003455 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003456exit:
3457 ;
3458}
3459/* END_CASE */
3460
3461/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003462void sign_deterministic( int key_type_arg, data_t *key_data,
3463 int alg_arg, data_t *input_data,
3464 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003465{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003466 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003467 psa_key_type_t key_type = key_type_arg;
3468 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003469 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003470 unsigned char *signature = NULL;
3471 size_t signature_size;
3472 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003473 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003474
Gilles Peskine8817f612018-12-18 00:18:46 +01003475 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003476
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003477 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3478 psa_set_key_algorithm( &attributes, alg );
3479 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003480
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003481 PSA_ASSERT( psa_import_key( &attributes, &handle,
3482 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003483 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3484 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003485
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003486 /* Allocate a buffer which has the size advertized by the
3487 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003488 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3489 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003490 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003491 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003492 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003493
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003494 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003495 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3496 input_data->x, input_data->len,
3497 signature, signature_size,
3498 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003499 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003500 ASSERT_COMPARE( output_data->x, output_data->len,
3501 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003502
3503exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003504 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003505 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003506 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003507 mbedtls_psa_crypto_free( );
3508}
3509/* END_CASE */
3510
3511/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003512void sign_fail( int key_type_arg, data_t *key_data,
3513 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003514 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003515{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003516 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003517 psa_key_type_t key_type = key_type_arg;
3518 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003519 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003520 psa_status_t actual_status;
3521 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003522 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003523 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003524 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003525
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003526 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003527
Gilles Peskine8817f612018-12-18 00:18:46 +01003528 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003529
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003530 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3531 psa_set_key_algorithm( &attributes, alg );
3532 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003533
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003534 PSA_ASSERT( psa_import_key( &attributes, &handle,
3535 key_data->x, key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003536
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003537 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003538 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003539 signature, signature_size,
3540 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003541 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003542 /* The value of *signature_length is unspecified on error, but
3543 * whatever it is, it should be less than signature_size, so that
3544 * if the caller tries to read *signature_length bytes without
3545 * checking the error code then they don't overflow a buffer. */
3546 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003547
3548exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003549 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003550 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003551 mbedtls_free( signature );
3552 mbedtls_psa_crypto_free( );
3553}
3554/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003555
3556/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003557void sign_verify( int key_type_arg, data_t *key_data,
3558 int alg_arg, data_t *input_data )
3559{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003560 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003561 psa_key_type_t key_type = key_type_arg;
3562 psa_algorithm_t alg = alg_arg;
3563 size_t key_bits;
3564 unsigned char *signature = NULL;
3565 size_t signature_size;
3566 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003568
Gilles Peskine8817f612018-12-18 00:18:46 +01003569 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003570
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3572 psa_set_key_algorithm( &attributes, alg );
3573 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003574
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003575 PSA_ASSERT( psa_import_key( &attributes, &handle,
3576 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003577 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3578 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003579
3580 /* Allocate a buffer which has the size advertized by the
3581 * library. */
3582 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3583 key_bits, alg );
3584 TEST_ASSERT( signature_size != 0 );
3585 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003586 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003587
3588 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003589 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3590 input_data->x, input_data->len,
3591 signature, signature_size,
3592 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003593 /* Check that the signature length looks sensible. */
3594 TEST_ASSERT( signature_length <= signature_size );
3595 TEST_ASSERT( signature_length > 0 );
3596
3597 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003598 PSA_ASSERT( psa_asymmetric_verify(
3599 handle, alg,
3600 input_data->x, input_data->len,
3601 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003602
3603 if( input_data->len != 0 )
3604 {
3605 /* Flip a bit in the input and verify that the signature is now
3606 * detected as invalid. Flip a bit at the beginning, not at the end,
3607 * because ECDSA may ignore the last few bits of the input. */
3608 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003609 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3610 input_data->x, input_data->len,
3611 signature, signature_length ),
3612 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003613 }
3614
3615exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003616 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003617 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003618 mbedtls_free( signature );
3619 mbedtls_psa_crypto_free( );
3620}
3621/* END_CASE */
3622
3623/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003624void asymmetric_verify( int key_type_arg, data_t *key_data,
3625 int alg_arg, data_t *hash_data,
3626 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003627{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003628 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003629 psa_key_type_t key_type = key_type_arg;
3630 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003632
Gilles Peskine69c12672018-06-28 00:07:19 +02003633 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3634
Gilles Peskine8817f612018-12-18 00:18:46 +01003635 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003636
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003637 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3638 psa_set_key_algorithm( &attributes, alg );
3639 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003640
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003641 PSA_ASSERT( psa_import_key( &attributes, &handle,
3642 key_data->x, key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003643
Gilles Peskine8817f612018-12-18 00:18:46 +01003644 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3645 hash_data->x, hash_data->len,
3646 signature_data->x,
3647 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003648exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003649 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003650 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003651 mbedtls_psa_crypto_free( );
3652}
3653/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003654
3655/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003656void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3657 int alg_arg, data_t *hash_data,
3658 data_t *signature_data,
3659 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003660{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003661 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662 psa_key_type_t key_type = key_type_arg;
3663 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664 psa_status_t actual_status;
3665 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003667
Gilles Peskine8817f612018-12-18 00:18:46 +01003668 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003669
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003670 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3671 psa_set_key_algorithm( &attributes, alg );
3672 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003673
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003674 PSA_ASSERT( psa_import_key( &attributes, &handle,
3675 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003676
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003677 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003678 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003679 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003680 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003681
Gilles Peskinefe11b722018-12-18 00:24:04 +01003682 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003683
3684exit:
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 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003687 mbedtls_psa_crypto_free( );
3688}
3689/* END_CASE */
3690
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003691/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003692void asymmetric_encrypt( int key_type_arg,
3693 data_t *key_data,
3694 int alg_arg,
3695 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003696 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003697 int expected_output_length_arg,
3698 int expected_status_arg )
3699{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003700 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003701 psa_key_type_t key_type = key_type_arg;
3702 psa_algorithm_t alg = alg_arg;
3703 size_t expected_output_length = expected_output_length_arg;
3704 size_t key_bits;
3705 unsigned char *output = NULL;
3706 size_t output_size;
3707 size_t output_length = ~0;
3708 psa_status_t actual_status;
3709 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003710 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003711
Gilles Peskine8817f612018-12-18 00:18:46 +01003712 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003713
Gilles Peskine656896e2018-06-29 19:12:28 +02003714 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003715 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3716 psa_set_key_algorithm( &attributes, alg );
3717 psa_set_key_type( &attributes, key_type );
3718 PSA_ASSERT( psa_import_key( &attributes, &handle,
3719 key_data->x, key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003720
3721 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003722 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3723 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003724 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003725 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003726
3727 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003728 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003729 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003730 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003731 output, output_size,
3732 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003733 TEST_EQUAL( actual_status, expected_status );
3734 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003735
Gilles Peskine68428122018-06-30 18:42:41 +02003736 /* If the label is empty, the test framework puts a non-null pointer
3737 * in label->x. Test that a null pointer works as well. */
3738 if( label->len == 0 )
3739 {
3740 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003741 if( output_size != 0 )
3742 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003743 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003744 input_data->x, input_data->len,
3745 NULL, label->len,
3746 output, output_size,
3747 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003748 TEST_EQUAL( actual_status, expected_status );
3749 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003750 }
3751
Gilles Peskine656896e2018-06-29 19:12:28 +02003752exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003753 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003754 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003755 mbedtls_free( output );
3756 mbedtls_psa_crypto_free( );
3757}
3758/* END_CASE */
3759
3760/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003761void asymmetric_encrypt_decrypt( int key_type_arg,
3762 data_t *key_data,
3763 int alg_arg,
3764 data_t *input_data,
3765 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003766{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003767 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003768 psa_key_type_t key_type = key_type_arg;
3769 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003770 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003771 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003772 size_t output_size;
3773 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003774 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003775 size_t output2_size;
3776 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003778
Gilles Peskine8817f612018-12-18 00:18:46 +01003779 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003780
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003781 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3782 psa_set_key_algorithm( &attributes, alg );
3783 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003784
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003785 PSA_ASSERT( psa_import_key( &attributes, &handle,
3786 key_data->x, key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003787
3788 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003789 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3790 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003791 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003792 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003793 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003794 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003795
Gilles Peskineeebd7382018-06-08 18:11:54 +02003796 /* We test encryption by checking that encrypt-then-decrypt gives back
3797 * the original plaintext because of the non-optional random
3798 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003799 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3800 input_data->x, input_data->len,
3801 label->x, label->len,
3802 output, output_size,
3803 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003804 /* We don't know what ciphertext length to expect, but check that
3805 * it looks sensible. */
3806 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003807
Gilles Peskine8817f612018-12-18 00:18:46 +01003808 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3809 output, output_length,
3810 label->x, label->len,
3811 output2, output2_size,
3812 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003813 ASSERT_COMPARE( input_data->x, input_data->len,
3814 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003815
3816exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003817 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003818 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003819 mbedtls_free( output );
3820 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003821 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003822}
3823/* END_CASE */
3824
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003825/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003826void asymmetric_decrypt( int key_type_arg,
3827 data_t *key_data,
3828 int alg_arg,
3829 data_t *input_data,
3830 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003831 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003832{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003833 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003834 psa_key_type_t key_type = key_type_arg;
3835 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003836 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003837 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003838 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003839 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003840
Jaeden Amero412654a2019-02-06 12:57:46 +00003841 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003842 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003843
Gilles Peskine8817f612018-12-18 00:18:46 +01003844 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003845
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003846 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3847 psa_set_key_algorithm( &attributes, alg );
3848 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003849
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003850 PSA_ASSERT( psa_import_key( &attributes, &handle,
3851 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003852
Gilles Peskine8817f612018-12-18 00:18:46 +01003853 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3854 input_data->x, input_data->len,
3855 label->x, label->len,
3856 output,
3857 output_size,
3858 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003859 ASSERT_COMPARE( expected_data->x, expected_data->len,
3860 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003861
Gilles Peskine68428122018-06-30 18:42:41 +02003862 /* If the label is empty, the test framework puts a non-null pointer
3863 * in label->x. Test that a null pointer works as well. */
3864 if( label->len == 0 )
3865 {
3866 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003867 if( output_size != 0 )
3868 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003869 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3870 input_data->x, input_data->len,
3871 NULL, label->len,
3872 output,
3873 output_size,
3874 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003875 ASSERT_COMPARE( expected_data->x, expected_data->len,
3876 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003877 }
3878
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003879exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003880 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003881 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003882 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884}
3885/* END_CASE */
3886
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003888void asymmetric_decrypt_fail( int key_type_arg,
3889 data_t *key_data,
3890 int alg_arg,
3891 data_t *input_data,
3892 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003893 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003894 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003895{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003896 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003897 psa_key_type_t key_type = key_type_arg;
3898 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003899 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003900 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003901 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003902 psa_status_t actual_status;
3903 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003904 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003905
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003906 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003907
Gilles Peskine8817f612018-12-18 00:18:46 +01003908 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003909
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003910 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3911 psa_set_key_algorithm( &attributes, alg );
3912 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003913
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003914 PSA_ASSERT( psa_import_key( &attributes, &handle,
3915 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003916
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003917 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003918 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003919 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003920 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003921 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003922 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003923 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003924
Gilles Peskine68428122018-06-30 18:42:41 +02003925 /* If the label is empty, the test framework puts a non-null pointer
3926 * in label->x. Test that a null pointer works as well. */
3927 if( label->len == 0 )
3928 {
3929 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003930 if( output_size != 0 )
3931 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003932 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003933 input_data->x, input_data->len,
3934 NULL, label->len,
3935 output, output_size,
3936 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003937 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003938 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003939 }
3940
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003942 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003943 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003944 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945 mbedtls_psa_crypto_free( );
3946}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003947/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003948
3949/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003950void crypto_generator_init( )
3951{
3952 /* Test each valid way of initializing the object, except for `= {0}`, as
3953 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3954 * though it's OK by the C standard. We could test for this, but we'd need
3955 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003956 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003957 psa_crypto_generator_t func = psa_crypto_generator_init( );
3958 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3959 psa_crypto_generator_t zero;
3960
3961 memset( &zero, 0, sizeof( zero ) );
3962
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003963 /* A default generator should not be able to report its capacity. */
3964 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
3965 PSA_ERROR_BAD_STATE );
3966 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
3967 PSA_ERROR_BAD_STATE );
3968 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
3969 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003970
3971 /* A default generator should be abortable without error. */
3972 PSA_ASSERT( psa_generator_abort(&func) );
3973 PSA_ASSERT( psa_generator_abort(&init) );
3974 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003975}
3976/* END_CASE */
3977
3978/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003979void derive_setup( int key_type_arg,
3980 data_t *key_data,
3981 int alg_arg,
3982 data_t *salt,
3983 data_t *label,
3984 int requested_capacity_arg,
3985 int expected_status_arg )
3986{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003987 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003988 size_t key_type = key_type_arg;
3989 psa_algorithm_t alg = alg_arg;
3990 size_t requested_capacity = requested_capacity_arg;
3991 psa_status_t expected_status = expected_status_arg;
3992 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003994
Gilles Peskine8817f612018-12-18 00:18:46 +01003995 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003996
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003997 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3998 psa_set_key_algorithm( &attributes, alg );
3999 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004000
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004001 PSA_ASSERT( psa_import_key( &attributes, &handle,
4002 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004003
Gilles Peskinefe11b722018-12-18 00:24:04 +01004004 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4005 salt->x, salt->len,
4006 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004007 requested_capacity ),
4008 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004009
4010exit:
4011 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004012 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004013 mbedtls_psa_crypto_free( );
4014}
4015/* END_CASE */
4016
4017/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004018void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004019{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004020 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004021 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004022 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004023 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004024 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004025 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004026 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4027 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4028 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004030
Gilles Peskine8817f612018-12-18 00:18:46 +01004031 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004032
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 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004036
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004037 PSA_ASSERT( psa_import_key( &attributes, &handle,
4038 key_data, sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004039
4040 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004041 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4042 NULL, 0,
4043 NULL, 0,
4044 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004045
4046 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004047 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4048 NULL, 0,
4049 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004050 capacity ),
4051 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004052
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004053 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004054
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004055 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004056 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004057
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004058exit:
4059 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004060 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004061 mbedtls_psa_crypto_free( );
4062}
4063/* END_CASE */
4064
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004065/* BEGIN_CASE */
4066void test_derive_invalid_generator_tests( )
4067{
4068 uint8_t output_buffer[16];
4069 size_t buffer_size = 16;
4070 size_t capacity = 0;
4071 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4072
Nir Sonnenschein50789302018-10-31 12:16:38 +02004073 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004074 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004075
4076 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004077 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004078
Gilles Peskine8817f612018-12-18 00:18:46 +01004079 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004080
Nir Sonnenschein50789302018-10-31 12:16:38 +02004081 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004082 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004083
Nir Sonnenschein50789302018-10-31 12:16:38 +02004084 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004085 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004086
4087exit:
4088 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004089}
4090/* END_CASE */
4091
4092/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004093void derive_output( int alg_arg,
4094 data_t *key_data,
4095 data_t *salt,
4096 data_t *label,
4097 int requested_capacity_arg,
4098 data_t *expected_output1,
4099 data_t *expected_output2 )
4100{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004101 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004102 psa_algorithm_t alg = alg_arg;
4103 size_t requested_capacity = requested_capacity_arg;
4104 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4105 uint8_t *expected_outputs[2] =
4106 {expected_output1->x, expected_output2->x};
4107 size_t output_sizes[2] =
4108 {expected_output1->len, expected_output2->len};
4109 size_t output_buffer_size = 0;
4110 uint8_t *output_buffer = NULL;
4111 size_t expected_capacity;
4112 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004114 psa_status_t status;
4115 unsigned i;
4116
4117 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4118 {
4119 if( output_sizes[i] > output_buffer_size )
4120 output_buffer_size = output_sizes[i];
4121 if( output_sizes[i] == 0 )
4122 expected_outputs[i] = NULL;
4123 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004124 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004126
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004127 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4128 psa_set_key_algorithm( &attributes, alg );
4129 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004130
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004131 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004132 key_data->x, key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004133
4134 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004135 if( PSA_ALG_IS_HKDF( alg ) )
4136 {
4137 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4138 PSA_ASSERT( psa_set_generator_capacity( &generator,
4139 requested_capacity ) );
4140 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4141 PSA_KDF_STEP_SALT,
4142 salt->x, salt->len ) );
4143 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4144 PSA_KDF_STEP_SECRET,
4145 handle ) );
4146 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4147 PSA_KDF_STEP_INFO,
4148 label->x, label->len ) );
4149 }
4150 else
4151 {
4152 // legacy
4153 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4154 salt->x, salt->len,
4155 label->x, label->len,
4156 requested_capacity ) );
4157 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004158 PSA_ASSERT( psa_get_generator_capacity( &generator,
4159 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004160 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004161 expected_capacity = requested_capacity;
4162
4163 /* Expansion phase. */
4164 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4165 {
4166 /* Read some bytes. */
4167 status = psa_generator_read( &generator,
4168 output_buffer, output_sizes[i] );
4169 if( expected_capacity == 0 && output_sizes[i] == 0 )
4170 {
4171 /* Reading 0 bytes when 0 bytes are available can go either way. */
4172 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004173 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004174 continue;
4175 }
4176 else if( expected_capacity == 0 ||
4177 output_sizes[i] > expected_capacity )
4178 {
4179 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004180 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004181 expected_capacity = 0;
4182 continue;
4183 }
4184 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004185 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004186 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004187 ASSERT_COMPARE( output_buffer, output_sizes[i],
4188 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004189 /* Check the generator status. */
4190 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004191 PSA_ASSERT( psa_get_generator_capacity( &generator,
4192 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004193 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004194 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004195 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004196
4197exit:
4198 mbedtls_free( output_buffer );
4199 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004200 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004201 mbedtls_psa_crypto_free( );
4202}
4203/* END_CASE */
4204
4205/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004206void derive_full( int alg_arg,
4207 data_t *key_data,
4208 data_t *salt,
4209 data_t *label,
4210 int requested_capacity_arg )
4211{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004212 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004213 psa_algorithm_t alg = alg_arg;
4214 size_t requested_capacity = requested_capacity_arg;
4215 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4216 unsigned char output_buffer[16];
4217 size_t expected_capacity = requested_capacity;
4218 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004220
Gilles Peskine8817f612018-12-18 00:18:46 +01004221 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004222
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004223 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4224 psa_set_key_algorithm( &attributes, alg );
4225 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004226
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004227 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004228 key_data->x, key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004229
4230 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004231 if( PSA_ALG_IS_HKDF( alg ) )
4232 {
4233 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4234 PSA_ASSERT( psa_set_generator_capacity( &generator,
4235 requested_capacity ) );
4236 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4237 PSA_KDF_STEP_SALT,
4238 salt->x, salt->len ) );
4239 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4240 PSA_KDF_STEP_SECRET,
4241 handle ) );
4242 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4243 PSA_KDF_STEP_INFO,
4244 label->x, label->len ) );
4245 }
4246 else
4247 {
4248 // legacy
4249 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4250 salt->x, salt->len,
4251 label->x, label->len,
4252 requested_capacity ) );
4253 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004254 PSA_ASSERT( psa_get_generator_capacity( &generator,
4255 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004256 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004257
4258 /* Expansion phase. */
4259 while( current_capacity > 0 )
4260 {
4261 size_t read_size = sizeof( output_buffer );
4262 if( read_size > current_capacity )
4263 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004264 PSA_ASSERT( psa_generator_read( &generator,
4265 output_buffer,
4266 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004267 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004268 PSA_ASSERT( psa_get_generator_capacity( &generator,
4269 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004270 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004271 }
4272
4273 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004274 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004275 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004276
Gilles Peskine8817f612018-12-18 00:18:46 +01004277 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004278
4279exit:
4280 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004281 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004282 mbedtls_psa_crypto_free( );
4283}
4284/* END_CASE */
4285
4286/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004287void derive_key_exercise( int alg_arg,
4288 data_t *key_data,
4289 data_t *salt,
4290 data_t *label,
4291 int derived_type_arg,
4292 int derived_bits_arg,
4293 int derived_usage_arg,
4294 int derived_alg_arg )
4295{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004296 psa_key_handle_t base_handle = 0;
4297 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004298 psa_algorithm_t alg = alg_arg;
4299 psa_key_type_t derived_type = derived_type_arg;
4300 size_t derived_bits = derived_bits_arg;
4301 psa_key_usage_t derived_usage = derived_usage_arg;
4302 psa_algorithm_t derived_alg = derived_alg_arg;
4303 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4304 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004305 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004306 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004307
Gilles Peskine8817f612018-12-18 00:18:46 +01004308 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004309
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004310 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4311 psa_set_key_algorithm( &attributes, alg );
4312 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
4313 PSA_ASSERT( psa_import_key( &attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004314 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004315
4316 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004317 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4318 salt->x, salt->len,
4319 label->x, label->len,
4320 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004321 psa_set_key_usage_flags( &attributes, derived_usage );
4322 psa_set_key_algorithm( &attributes, derived_alg );
4323 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004324 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004325 PSA_ASSERT( psa_generator_import_key( &attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004326 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004327
4328 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004329 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4330 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4331 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004332
4333 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004334 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004335 goto exit;
4336
4337exit:
4338 psa_generator_abort( &generator );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004339 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004340 psa_destroy_key( base_handle );
4341 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004342 mbedtls_psa_crypto_free( );
4343}
4344/* END_CASE */
4345
4346/* BEGIN_CASE */
4347void derive_key_export( int alg_arg,
4348 data_t *key_data,
4349 data_t *salt,
4350 data_t *label,
4351 int bytes1_arg,
4352 int bytes2_arg )
4353{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004354 psa_key_handle_t base_handle = 0;
4355 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004356 psa_algorithm_t alg = alg_arg;
4357 size_t bytes1 = bytes1_arg;
4358 size_t bytes2 = bytes2_arg;
4359 size_t capacity = bytes1 + bytes2;
4360 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004361 uint8_t *output_buffer = NULL;
4362 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004363 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4364 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004365 size_t length;
4366
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004367 ASSERT_ALLOC( output_buffer, capacity );
4368 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004369 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004370
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004371 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4372 psa_set_key_algorithm( &base_attributes, alg );
4373 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4374 PSA_ASSERT( psa_import_key( &base_attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004375 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004376
4377 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004378 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4379 salt->x, salt->len,
4380 label->x, label->len,
4381 capacity ) );
4382 PSA_ASSERT( psa_generator_read( &generator,
4383 output_buffer,
4384 capacity ) );
4385 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004386
4387 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004388 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4389 salt->x, salt->len,
4390 label->x, label->len,
4391 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004392 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4393 psa_set_key_algorithm( &derived_attributes, 0 );
4394 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004395 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004396 PSA_ASSERT( psa_generator_import_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004397 &generator ) );
4398 PSA_ASSERT( psa_export_key( derived_handle,
4399 export_buffer, bytes1,
4400 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004401 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004402 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004403 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004404 PSA_ASSERT( psa_generator_import_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004405 &generator ) );
4406 PSA_ASSERT( psa_export_key( derived_handle,
4407 export_buffer + bytes1, bytes2,
4408 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004409 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004410
4411 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004412 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4413 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004414
4415exit:
4416 mbedtls_free( output_buffer );
4417 mbedtls_free( export_buffer );
4418 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004419 psa_destroy_key( base_handle );
4420 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004421 mbedtls_psa_crypto_free( );
4422}
4423/* END_CASE */
4424
4425/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004426void key_agreement_setup( int alg_arg,
4427 int our_key_type_arg, data_t *our_key_data,
4428 data_t *peer_key_data,
4429 int expected_status_arg )
4430{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004431 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004432 psa_algorithm_t alg = alg_arg;
4433 psa_key_type_t our_key_type = our_key_type_arg;
4434 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004436 psa_status_t expected_status = expected_status_arg;
4437 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004438
Gilles Peskine8817f612018-12-18 00:18:46 +01004439 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004440
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004441 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4442 psa_set_key_algorithm( &attributes, alg );
4443 psa_set_key_type( &attributes, our_key_type );
4444 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004445 our_key_data->x, our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004446
Gilles Peskine77f40d82019-04-11 21:27:06 +02004447 /* The tests currently include inputs that should fail at either step.
4448 * Test cases that fail at the setup step should be changed to call
4449 * key_derivation_setup instead, and this function should be renamed
4450 * to key_agreement_fail. */
4451 status = psa_key_derivation_setup( &generator, alg );
4452 if( status == PSA_SUCCESS )
4453 {
4454 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
4455 our_key,
4456 peer_key_data->x, peer_key_data->len ),
4457 expected_status );
4458 }
4459 else
4460 {
4461 TEST_ASSERT( status == expected_status );
4462 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004463
4464exit:
4465 psa_generator_abort( &generator );
4466 psa_destroy_key( our_key );
4467 mbedtls_psa_crypto_free( );
4468}
4469/* END_CASE */
4470
4471/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004472void raw_key_agreement( int alg_arg,
4473 int our_key_type_arg, data_t *our_key_data,
4474 data_t *peer_key_data,
4475 data_t *expected_output )
4476{
4477 psa_key_handle_t our_key = 0;
4478 psa_algorithm_t alg = alg_arg;
4479 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004480 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004481 unsigned char *output = NULL;
4482 size_t output_length = ~0;
4483
4484 ASSERT_ALLOC( output, expected_output->len );
4485 PSA_ASSERT( psa_crypto_init( ) );
4486
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004487 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4488 psa_set_key_algorithm( &attributes, alg );
4489 psa_set_key_type( &attributes, our_key_type );
4490 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004491 our_key_data->x, our_key_data->len ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004492
4493 PSA_ASSERT( psa_key_agreement_raw_shared_secret(
4494 alg, our_key,
4495 peer_key_data->x, peer_key_data->len,
4496 output, expected_output->len, &output_length ) );
4497 ASSERT_COMPARE( output, output_length,
4498 expected_output->x, expected_output->len );
4499
4500exit:
4501 mbedtls_free( output );
4502 psa_destroy_key( our_key );
4503 mbedtls_psa_crypto_free( );
4504}
4505/* END_CASE */
4506
4507/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004508void key_agreement_capacity( int alg_arg,
4509 int our_key_type_arg, data_t *our_key_data,
4510 data_t *peer_key_data,
4511 int expected_capacity_arg )
4512{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004513 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004514 psa_algorithm_t alg = alg_arg;
4515 psa_key_type_t our_key_type = our_key_type_arg;
4516 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004517 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004518 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004519 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004520
Gilles Peskine8817f612018-12-18 00:18:46 +01004521 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004522
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 Peskine59685592018-09-18 12:11:34 +02004528
Gilles Peskine969c5d62019-01-16 15:53:06 +01004529 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4530 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004531 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004532 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004533 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4534 {
4535 /* The test data is for info="" */
4536 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4537 PSA_KDF_STEP_INFO,
4538 NULL, 0 ) );
4539 }
Gilles Peskine59685592018-09-18 12:11:34 +02004540
Gilles Peskinebf491972018-10-25 22:36:12 +02004541 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004542 PSA_ASSERT( psa_get_generator_capacity(
4543 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004544 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004545
Gilles Peskinebf491972018-10-25 22:36:12 +02004546 /* Test the actual capacity by reading the output. */
4547 while( actual_capacity > sizeof( output ) )
4548 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004549 PSA_ASSERT( psa_generator_read( &generator,
4550 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004551 actual_capacity -= sizeof( output );
4552 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004553 PSA_ASSERT( psa_generator_read( &generator,
4554 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004555 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004556 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004557
Gilles Peskine59685592018-09-18 12:11:34 +02004558exit:
4559 psa_generator_abort( &generator );
4560 psa_destroy_key( our_key );
4561 mbedtls_psa_crypto_free( );
4562}
4563/* END_CASE */
4564
4565/* BEGIN_CASE */
4566void key_agreement_output( int alg_arg,
4567 int our_key_type_arg, data_t *our_key_data,
4568 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004569 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004570{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004571 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004572 psa_algorithm_t alg = alg_arg;
4573 psa_key_type_t our_key_type = our_key_type_arg;
4574 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004575 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004576 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004577
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004578 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4579 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004580
Gilles Peskine8817f612018-12-18 00:18:46 +01004581 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004582
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004583 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4584 psa_set_key_algorithm( &attributes, alg );
4585 psa_set_key_type( &attributes, our_key_type );
4586 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004587 our_key_data->x, our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004588
Gilles Peskine969c5d62019-01-16 15:53:06 +01004589 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4590 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004591 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004592 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004593 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4594 {
4595 /* The test data is for info="" */
4596 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4597 PSA_KDF_STEP_INFO,
4598 NULL, 0 ) );
4599 }
Gilles Peskine59685592018-09-18 12:11:34 +02004600
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004601 PSA_ASSERT( psa_generator_read( &generator,
4602 actual_output,
4603 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004604 ASSERT_COMPARE( actual_output, expected_output1->len,
4605 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004606 if( expected_output2->len != 0 )
4607 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004608 PSA_ASSERT( psa_generator_read( &generator,
4609 actual_output,
4610 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004611 ASSERT_COMPARE( actual_output, expected_output2->len,
4612 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004613 }
Gilles Peskine59685592018-09-18 12:11:34 +02004614
4615exit:
4616 psa_generator_abort( &generator );
4617 psa_destroy_key( our_key );
4618 mbedtls_psa_crypto_free( );
4619 mbedtls_free( actual_output );
4620}
4621/* END_CASE */
4622
4623/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004624void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004625{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004626 size_t bytes = bytes_arg;
4627 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004628 unsigned char *output = NULL;
4629 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004630 size_t i;
4631 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004632
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004633 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4634 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004635 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004636
Gilles Peskine8817f612018-12-18 00:18:46 +01004637 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004638
Gilles Peskinea50d7392018-06-21 10:22:13 +02004639 /* Run several times, to ensure that every output byte will be
4640 * nonzero at least once with overwhelming probability
4641 * (2^(-8*number_of_runs)). */
4642 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004643 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004644 if( bytes != 0 )
4645 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004646 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004647
4648 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004649 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4650 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004651
4652 for( i = 0; i < bytes; i++ )
4653 {
4654 if( output[i] != 0 )
4655 ++changed[i];
4656 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004657 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004658
4659 /* Check that every byte was changed to nonzero at least once. This
4660 * validates that psa_generate_random is overwriting every byte of
4661 * the output buffer. */
4662 for( i = 0; i < bytes; i++ )
4663 {
4664 TEST_ASSERT( changed[i] != 0 );
4665 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004666
4667exit:
4668 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004669 mbedtls_free( output );
4670 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004671}
4672/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004673
4674/* BEGIN_CASE */
4675void generate_key( int type_arg,
4676 int bits_arg,
4677 int usage_arg,
4678 int alg_arg,
4679 int expected_status_arg )
4680{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004681 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004682 psa_key_type_t type = type_arg;
4683 psa_key_usage_t usage = usage_arg;
4684 size_t bits = bits_arg;
4685 psa_algorithm_t alg = alg_arg;
4686 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004687 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004688 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004689
Gilles Peskine8817f612018-12-18 00:18:46 +01004690 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004691
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004692 psa_set_key_usage_flags( &attributes, usage );
4693 psa_set_key_algorithm( &attributes, alg );
4694 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004695 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004696
4697 /* Generate a key */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004698 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
4699 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004700 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004701
4702 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004703 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4704 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4705 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004706
Gilles Peskine818ca122018-06-20 18:16:48 +02004707 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004708 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004709 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004710
4711exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004712 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004713 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004714 mbedtls_psa_crypto_free( );
4715}
4716/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004717
Gilles Peskinee56e8782019-04-26 17:34:02 +02004718/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4719void generate_key_rsa( int bits_arg,
4720 data_t *e_arg,
4721 int expected_status_arg )
4722{
4723 psa_key_handle_t handle = 0;
4724 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEYPAIR;
4725 size_t bits = bits_arg;
4726 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4727 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4728 psa_status_t expected_status = expected_status_arg;
4729 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4730 uint8_t *exported = NULL;
4731 size_t exported_size =
4732 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4733 size_t exported_length = SIZE_MAX;
4734 uint8_t *e_read_buffer = NULL;
4735 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004736 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004737 size_t e_read_length = SIZE_MAX;
4738
4739 if( e_arg->len == 0 ||
4740 ( e_arg->len == 3 &&
4741 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4742 {
4743 is_default_public_exponent = 1;
4744 e_read_size = 0;
4745 }
4746 ASSERT_ALLOC( e_read_buffer, e_read_size );
4747 ASSERT_ALLOC( exported, exported_size );
4748
4749 PSA_ASSERT( psa_crypto_init( ) );
4750
4751 psa_set_key_usage_flags( &attributes, usage );
4752 psa_set_key_algorithm( &attributes, alg );
4753 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4754 e_arg->x, e_arg->len ) );
4755 psa_set_key_bits( &attributes, bits );
4756
4757 /* Generate a key */
4758 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
4759 if( expected_status != PSA_SUCCESS )
4760 goto exit;
4761
4762 /* Test the key information */
4763 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4764 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4765 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4766 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4767 e_read_buffer, e_read_size,
4768 &e_read_length ) );
4769 if( is_default_public_exponent )
4770 TEST_EQUAL( e_read_length, 0 );
4771 else
4772 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4773
4774 /* Do something with the key according to its type and permitted usage. */
4775 if( ! exercise_key( handle, usage, alg ) )
4776 goto exit;
4777
4778 /* Export the key and check the public exponent. */
4779 PSA_ASSERT( psa_export_public_key( handle,
4780 exported, exported_size,
4781 &exported_length ) );
4782 {
4783 uint8_t *p = exported;
4784 uint8_t *end = exported + exported_length;
4785 size_t len;
4786 /* RSAPublicKey ::= SEQUENCE {
4787 * modulus INTEGER, -- n
4788 * publicExponent INTEGER } -- e
4789 */
4790 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4791 MBEDTLS_ASN1_SEQUENCE |
4792 MBEDTLS_ASN1_CONSTRUCTED ) );
4793 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
4794 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4795 MBEDTLS_ASN1_INTEGER ) );
4796 if( len >= 1 && p[0] == 0 )
4797 {
4798 ++p;
4799 --len;
4800 }
4801 if( e_arg->len == 0 )
4802 {
4803 TEST_EQUAL( len, 3 );
4804 TEST_EQUAL( p[0], 1 );
4805 TEST_EQUAL( p[1], 0 );
4806 TEST_EQUAL( p[2], 1 );
4807 }
4808 else
4809 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4810 }
4811
4812exit:
4813 psa_reset_key_attributes( &attributes );
4814 psa_destroy_key( handle );
4815 mbedtls_psa_crypto_free( );
4816 mbedtls_free( e_read_buffer );
4817 mbedtls_free( exported );
4818}
4819/* END_CASE */
4820
Darryl Greend49a4992018-06-18 17:27:26 +01004821/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004822void persistent_key_load_key_from_storage( data_t *data,
4823 int type_arg, int bits_arg,
4824 int usage_flags_arg, int alg_arg,
4825 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004826{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004827 psa_key_id_t key_id = 1;
4828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004829 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004830 psa_key_handle_t base_key = 0;
4831 psa_key_type_t type = type_arg;
4832 size_t bits = bits_arg;
4833 psa_key_usage_t usage_flags = usage_flags_arg;
4834 psa_algorithm_t alg = alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004835 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004836 unsigned char *first_export = NULL;
4837 unsigned char *second_export = NULL;
4838 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4839 size_t first_exported_length;
4840 size_t second_exported_length;
4841
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004842 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4843 {
4844 ASSERT_ALLOC( first_export, export_size );
4845 ASSERT_ALLOC( second_export, export_size );
4846 }
Darryl Greend49a4992018-06-18 17:27:26 +01004847
Gilles Peskine8817f612018-12-18 00:18:46 +01004848 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004849
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004850 psa_make_key_persistent( &attributes, key_id, PSA_KEY_LIFETIME_PERSISTENT );
4851 psa_set_key_usage_flags( &attributes, usage_flags );
4852 psa_set_key_algorithm( &attributes, alg );
4853 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004854 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004855
Darryl Green0c6575a2018-11-07 16:05:30 +00004856 switch( generation_method )
4857 {
4858 case IMPORT_KEY:
4859 /* Import the key */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004860 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004861 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004862 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004863
Darryl Green0c6575a2018-11-07 16:05:30 +00004864 case GENERATE_KEY:
4865 /* Generate a key */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004866 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004867 break;
4868
4869 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004870 {
4871 /* Create base key */
4872 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4873 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4874 psa_set_key_usage_flags( &base_attributes,
4875 PSA_KEY_USAGE_DERIVE );
4876 psa_set_key_algorithm( &base_attributes, derive_alg );
4877 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4878 PSA_ASSERT( psa_import_key( &base_attributes, &base_key,
4879 data->x, data->len ) );
4880 /* Derive a key. */
4881 PSA_ASSERT( psa_key_derivation_setup( &generator, derive_alg ) );
4882 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4883 PSA_KDF_STEP_SECRET,
4884 base_key ) );
4885 PSA_ASSERT( psa_key_derivation_input_bytes(
4886 &generator, PSA_KDF_STEP_INFO,
4887 NULL, 0 ) );
4888 PSA_ASSERT( psa_generator_import_key( &attributes, &handle,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004889 &generator ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004890 PSA_ASSERT( psa_generator_abort( &generator ) );
4891 PSA_ASSERT( psa_destroy_key( base_key ) );
4892 base_key = 0;
4893 }
Darryl Green0c6575a2018-11-07 16:05:30 +00004894 break;
4895 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004896 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004897
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004898 /* Export the key if permitted by the key policy. */
4899 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4900 {
4901 PSA_ASSERT( psa_export_key( handle,
4902 first_export, export_size,
4903 &first_exported_length ) );
4904 if( generation_method == IMPORT_KEY )
4905 ASSERT_COMPARE( data->x, data->len,
4906 first_export, first_exported_length );
4907 }
Darryl Greend49a4992018-06-18 17:27:26 +01004908
4909 /* Shutdown and restart */
4910 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004911 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004912
Darryl Greend49a4992018-06-18 17:27:26 +01004913 /* Check key slot still contains key data */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004914 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
Gilles Peskine8817f612018-12-18 00:18:46 +01004915 &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004916 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4917 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
4918 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4919 PSA_KEY_LIFETIME_PERSISTENT );
4920 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4921 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4922 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4923 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004924
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004925 /* Export the key again if permitted by the key policy. */
4926 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004927 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004928 PSA_ASSERT( psa_export_key( handle,
4929 second_export, export_size,
4930 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004931 ASSERT_COMPARE( first_export, first_exported_length,
4932 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004933 }
4934
4935 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004936 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004937 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004938
4939exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004940 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004941 mbedtls_free( first_export );
4942 mbedtls_free( second_export );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004943 psa_generator_abort( &generator );
4944 psa_destroy_key( base_key );
4945 if( handle == 0 )
4946 {
4947 /* In case there was a test failure after creating the persistent key
4948 * but while it was not open, try to re-open the persistent key
4949 * to delete it. */
4950 psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle );
4951 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004952 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004953 mbedtls_psa_crypto_free();
4954}
4955/* END_CASE */