blob: f3f79abcfea2698762f0c10ed73904dd2d4bc643 [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
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskine1838e822019-06-20 12:40:56 +020012#include "psa_crypto_helpers.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskinec744d992019-07-30 17:26:54 +020014/* Tests that require more than 128kB of RAM plus change have this symbol
15 * as a dependency. Currently we always define this symbol, so the tests
16 * are always executed. In the future we should make this conditional
17 * so that tests that require a lot of memory are skipped on constrained
18 * platforms. */
Gilles Peskine49232e82019-08-07 11:01:30 +020019#define HAVE_RAM_AVAILABLE_128K
Gilles Peskinec744d992019-07-30 17:26:54 +020020
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020021#include "psa/crypto.h"
22
Jaeden Amerof24c7f82018-06-27 17:20:43 +010023/** An invalid export length that will never be set by psa_export_key(). */
24static const size_t INVALID_EXPORT_LENGTH = ~0U;
25
Gilles Peskinef426e0f2019-02-25 17:42:03 +010026/* A hash algorithm that is known to be supported.
27 *
28 * This is used in some smoke tests.
29 */
30#if defined(MBEDTLS_MD2_C)
31#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
32#elif defined(MBEDTLS_MD4_C)
33#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
34#elif defined(MBEDTLS_MD5_C)
35#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
36/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
37 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
38 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
39 * implausible anyway. */
40#elif defined(MBEDTLS_SHA1_C)
41#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
42#elif defined(MBEDTLS_SHA256_C)
43#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
44#elif defined(MBEDTLS_SHA512_C)
45#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
46#elif defined(MBEDTLS_SHA3_C)
47#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
48#else
49#undef KNOWN_SUPPORTED_HASH_ALG
50#endif
51
52/* A block cipher that is known to be supported.
53 *
54 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
55 */
56#if defined(MBEDTLS_AES_C)
57#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
58#elif defined(MBEDTLS_ARIA_C)
59#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
60#elif defined(MBEDTLS_CAMELLIA_C)
61#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
62#undef KNOWN_SUPPORTED_BLOCK_CIPHER
63#endif
64
65/* A MAC mode that is known to be supported.
66 *
67 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
68 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
69 *
70 * This is used in some smoke tests.
71 */
72#if defined(KNOWN_SUPPORTED_HASH_ALG)
73#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
74#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
75#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
76#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
77#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
78#else
79#undef KNOWN_SUPPORTED_MAC_ALG
80#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
81#endif
82
83/* A cipher algorithm and key type that are known to be supported.
84 *
85 * This is used in some smoke tests.
86 */
87#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
89#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
90#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
91#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
92#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
93#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
94#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
95#else
96#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
97#endif
98#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
99#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
100#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
101#elif defined(MBEDTLS_RC4_C)
102#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
103#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
104#else
105#undef KNOWN_SUPPORTED_CIPHER_ALG
106#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
107#endif
108
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200109/** Test if a buffer contains a constant byte value.
110 *
111 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200112 *
113 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200114 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200115 * \param size Size of the buffer in bytes.
116 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200117 * \return 1 if the buffer is all-bits-zero.
118 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200119 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200120static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200121{
122 size_t i;
123 for( i = 0; i < size; i++ )
124 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200125 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200126 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200127 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200128 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200129}
Gilles Peskine818ca122018-06-20 18:16:48 +0200130
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200131/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
132static int asn1_write_10x( unsigned char **p,
133 unsigned char *start,
134 size_t bits,
135 unsigned char x )
136{
137 int ret;
138 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200139 if( bits == 0 )
140 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
141 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300143 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200144 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
145 *p -= len;
146 ( *p )[len-1] = x;
147 if( bits % 8 == 0 )
148 ( *p )[1] |= 1;
149 else
150 ( *p )[0] |= 1 << ( bits % 8 );
151 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
152 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
153 MBEDTLS_ASN1_INTEGER ) );
154 return( len );
155}
156
157static int construct_fake_rsa_key( unsigned char *buffer,
158 size_t buffer_size,
159 unsigned char **p,
160 size_t bits,
161 int keypair )
162{
163 size_t half_bits = ( bits + 1 ) / 2;
164 int ret;
165 int len = 0;
166 /* Construct something that looks like a DER encoding of
167 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
168 * RSAPrivateKey ::= SEQUENCE {
169 * version Version,
170 * modulus INTEGER, -- n
171 * publicExponent INTEGER, -- e
172 * privateExponent INTEGER, -- d
173 * prime1 INTEGER, -- p
174 * prime2 INTEGER, -- q
175 * exponent1 INTEGER, -- d mod (p-1)
176 * exponent2 INTEGER, -- d mod (q-1)
177 * coefficient INTEGER, -- (inverse of q) mod p
178 * otherPrimeInfos OtherPrimeInfos OPTIONAL
179 * }
180 * Or, for a public key, the same structure with only
181 * version, modulus and publicExponent.
182 */
183 *p = buffer + buffer_size;
184 if( keypair )
185 {
186 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
187 asn1_write_10x( p, buffer, half_bits, 1 ) );
188 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
189 asn1_write_10x( p, buffer, half_bits, 1 ) );
190 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
191 asn1_write_10x( p, buffer, half_bits, 1 ) );
192 MBEDTLS_ASN1_CHK_ADD( len, /* q */
193 asn1_write_10x( p, buffer, half_bits, 1 ) );
194 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
195 asn1_write_10x( p, buffer, half_bits, 3 ) );
196 MBEDTLS_ASN1_CHK_ADD( len, /* d */
197 asn1_write_10x( p, buffer, bits, 1 ) );
198 }
199 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
200 asn1_write_10x( p, buffer, 17, 1 ) );
201 MBEDTLS_ASN1_CHK_ADD( len, /* n */
202 asn1_write_10x( p, buffer, bits, 1 ) );
203 if( keypair )
204 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
205 mbedtls_asn1_write_int( p, buffer, 0 ) );
206 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
207 {
208 const unsigned char tag =
209 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
210 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
211 }
212 return( len );
213}
214
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100215int exercise_mac_setup( psa_key_type_t key_type,
216 const unsigned char *key_bytes,
217 size_t key_length,
218 psa_algorithm_t alg,
219 psa_mac_operation_t *operation,
220 psa_status_t *status )
221{
222 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200223 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100224
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200225 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
226 psa_set_key_algorithm( &attributes, alg );
227 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200228 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
229 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230
231 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100232 /* Whether setup succeeded or failed, abort must succeed. */
233 PSA_ASSERT( psa_mac_abort( operation ) );
234 /* If setup failed, reproduce the failure, so that the caller can
235 * test the resulting state of the operation object. */
236 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100237 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100238 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
239 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100240 }
241
242 psa_destroy_key( handle );
243 return( 1 );
244
245exit:
246 psa_destroy_key( handle );
247 return( 0 );
248}
249
250int exercise_cipher_setup( psa_key_type_t key_type,
251 const unsigned char *key_bytes,
252 size_t key_length,
253 psa_algorithm_t alg,
254 psa_cipher_operation_t *operation,
255 psa_status_t *status )
256{
257 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
261 psa_set_key_algorithm( &attributes, alg );
262 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200263 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
264 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100265
266 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100267 /* Whether setup succeeded or failed, abort must succeed. */
268 PSA_ASSERT( psa_cipher_abort( operation ) );
269 /* If setup failed, reproduce the failure, so that the caller can
270 * test the resulting state of the operation object. */
271 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100272 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100273 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
274 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100275 }
276
277 psa_destroy_key( handle );
278 return( 1 );
279
280exit:
281 psa_destroy_key( handle );
282 return( 0 );
283}
284
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100285static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200286 psa_key_usage_t usage,
287 psa_algorithm_t alg )
288{
Jaeden Amero769ce272019-01-04 11:48:03 +0000289 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200290 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200291 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 size_t mac_length = sizeof( mac );
293
294 if( usage & PSA_KEY_USAGE_SIGN )
295 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100296 PSA_ASSERT( psa_mac_sign_setup( &operation,
297 handle, alg ) );
298 PSA_ASSERT( psa_mac_update( &operation,
299 input, sizeof( input ) ) );
300 PSA_ASSERT( psa_mac_sign_finish( &operation,
301 mac, sizeof( mac ),
302 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200303 }
304
305 if( usage & PSA_KEY_USAGE_VERIFY )
306 {
307 psa_status_t verify_status =
308 ( usage & PSA_KEY_USAGE_SIGN ?
309 PSA_SUCCESS :
310 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100311 PSA_ASSERT( psa_mac_verify_setup( &operation,
312 handle, alg ) );
313 PSA_ASSERT( psa_mac_update( &operation,
314 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100315 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
316 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200317 }
318
319 return( 1 );
320
321exit:
322 psa_mac_abort( &operation );
323 return( 0 );
324}
325
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100326static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200327 psa_key_usage_t usage,
328 psa_algorithm_t alg )
329{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000330 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200331 unsigned char iv[16] = {0};
332 size_t iv_length = sizeof( iv );
333 const unsigned char plaintext[16] = "Hello, world...";
334 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
335 size_t ciphertext_length = sizeof( ciphertext );
336 unsigned char decrypted[sizeof( ciphertext )];
337 size_t part_length;
338
339 if( usage & PSA_KEY_USAGE_ENCRYPT )
340 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100341 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
342 handle, alg ) );
343 PSA_ASSERT( psa_cipher_generate_iv( &operation,
344 iv, sizeof( iv ),
345 &iv_length ) );
346 PSA_ASSERT( psa_cipher_update( &operation,
347 plaintext, sizeof( plaintext ),
348 ciphertext, sizeof( ciphertext ),
349 &ciphertext_length ) );
350 PSA_ASSERT( psa_cipher_finish( &operation,
351 ciphertext + ciphertext_length,
352 sizeof( ciphertext ) - ciphertext_length,
353 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200354 ciphertext_length += part_length;
355 }
356
357 if( usage & PSA_KEY_USAGE_DECRYPT )
358 {
359 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200360 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200361 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
362 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
364 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
365 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
366 * have this macro yet. */
367 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
368 psa_get_key_type( &attributes ) );
369 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200370 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100371 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
372 handle, alg ) );
373 PSA_ASSERT( psa_cipher_set_iv( &operation,
374 iv, iv_length ) );
375 PSA_ASSERT( psa_cipher_update( &operation,
376 ciphertext, ciphertext_length,
377 decrypted, sizeof( decrypted ),
378 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200379 status = psa_cipher_finish( &operation,
380 decrypted + part_length,
381 sizeof( decrypted ) - part_length,
382 &part_length );
383 /* For a stream cipher, all inputs are valid. For a block cipher,
384 * if the input is some aribtrary data rather than an actual
385 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200386 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200387 TEST_ASSERT( status == PSA_SUCCESS ||
388 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200389 else
390 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200391 }
392
393 return( 1 );
394
395exit:
396 psa_cipher_abort( &operation );
397 return( 0 );
398}
399
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100400static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200401 psa_key_usage_t usage,
402 psa_algorithm_t alg )
403{
404 unsigned char nonce[16] = {0};
405 size_t nonce_length = sizeof( nonce );
406 unsigned char plaintext[16] = "Hello, world...";
407 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
408 size_t ciphertext_length = sizeof( ciphertext );
409 size_t plaintext_length = sizeof( ciphertext );
410
411 if( usage & PSA_KEY_USAGE_ENCRYPT )
412 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100413 PSA_ASSERT( psa_aead_encrypt( handle, alg,
414 nonce, nonce_length,
415 NULL, 0,
416 plaintext, sizeof( plaintext ),
417 ciphertext, sizeof( ciphertext ),
418 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200419 }
420
421 if( usage & PSA_KEY_USAGE_DECRYPT )
422 {
423 psa_status_t verify_status =
424 ( usage & PSA_KEY_USAGE_ENCRYPT ?
425 PSA_SUCCESS :
426 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100427 TEST_EQUAL( psa_aead_decrypt( handle, alg,
428 nonce, nonce_length,
429 NULL, 0,
430 ciphertext, ciphertext_length,
431 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100432 &plaintext_length ),
433 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200434 }
435
436 return( 1 );
437
438exit:
439 return( 0 );
440}
441
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100442static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200443 psa_key_usage_t usage,
444 psa_algorithm_t alg )
445{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200446 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
447 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200448 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200449 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100450 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
451
452 /* If the policy allows signing with any hash, just pick one. */
453 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
454 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100455#if defined(KNOWN_SUPPORTED_HASH_ALG)
456 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
457 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100458#else
459 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100460 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100461#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100462 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200463
464 if( usage & PSA_KEY_USAGE_SIGN )
465 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200466 /* Some algorithms require the payload to have the size of
467 * the hash encoded in the algorithm. Use this input size
468 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200469 if( hash_alg != 0 )
470 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100471 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
472 payload, payload_length,
473 signature, sizeof( signature ),
474 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200475 }
476
477 if( usage & PSA_KEY_USAGE_VERIFY )
478 {
479 psa_status_t verify_status =
480 ( usage & PSA_KEY_USAGE_SIGN ?
481 PSA_SUCCESS :
482 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100483 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
484 payload, payload_length,
485 signature, signature_length ),
486 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200487 }
488
489 return( 1 );
490
491exit:
492 return( 0 );
493}
494
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100495static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200496 psa_key_usage_t usage,
497 psa_algorithm_t alg )
498{
499 unsigned char plaintext[256] = "Hello, world...";
500 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
501 size_t ciphertext_length = sizeof( ciphertext );
502 size_t plaintext_length = 16;
503
504 if( usage & PSA_KEY_USAGE_ENCRYPT )
505 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100506 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
507 plaintext, plaintext_length,
508 NULL, 0,
509 ciphertext, sizeof( ciphertext ),
510 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200511 }
512
513 if( usage & PSA_KEY_USAGE_DECRYPT )
514 {
515 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100516 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200517 ciphertext, ciphertext_length,
518 NULL, 0,
519 plaintext, sizeof( plaintext ),
520 &plaintext_length );
521 TEST_ASSERT( status == PSA_SUCCESS ||
522 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
523 ( status == PSA_ERROR_INVALID_ARGUMENT ||
524 status == PSA_ERROR_INVALID_PADDING ) ) );
525 }
526
527 return( 1 );
528
529exit:
530 return( 0 );
531}
Gilles Peskine02b75072018-07-01 22:31:34 +0200532
Janos Follathf2815ea2019-07-03 12:41:36 +0100533static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
534 psa_key_handle_t handle,
535 psa_algorithm_t alg,
536 unsigned char* input1, size_t input1_length,
537 unsigned char* input2, size_t input2_length,
538 size_t capacity )
539{
540 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
541 if( PSA_ALG_IS_HKDF( alg ) )
542 {
543 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
544 PSA_KEY_DERIVATION_INPUT_SALT,
545 input1, input1_length ) );
546 PSA_ASSERT( psa_key_derivation_input_key( operation,
547 PSA_KEY_DERIVATION_INPUT_SECRET,
548 handle ) );
549 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
550 PSA_KEY_DERIVATION_INPUT_INFO,
551 input2,
552 input2_length ) );
553 }
554 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
555 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
556 {
557 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
558 PSA_KEY_DERIVATION_INPUT_SEED,
559 input1, input1_length ) );
560 PSA_ASSERT( psa_key_derivation_input_key( operation,
561 PSA_KEY_DERIVATION_INPUT_SECRET,
562 handle ) );
563 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
564 PSA_KEY_DERIVATION_INPUT_LABEL,
565 input2, input2_length ) );
566 }
567 else
568 {
569 TEST_ASSERT( ! "Key derivation algorithm not supported" );
570 }
571
Gilles Peskinec744d992019-07-30 17:26:54 +0200572 if( capacity != SIZE_MAX )
573 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100574
575 return( 1 );
576
577exit:
578 return( 0 );
579}
580
581
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100582static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200583 psa_key_usage_t usage,
584 psa_algorithm_t alg )
585{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200586 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100587 unsigned char input1[] = "Input 1";
588 size_t input1_length = sizeof( input1 );
589 unsigned char input2[] = "Input 2";
590 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200591 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100592 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200593
594 if( usage & PSA_KEY_USAGE_DERIVE )
595 {
Janos Follathf2815ea2019-07-03 12:41:36 +0100596 if( !setup_key_derivation_wrap( &operation, handle, alg,
597 input1, input1_length,
598 input2, input2_length, capacity ) )
599 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200600
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200601 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200602 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100603 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200604 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200605 }
606
607 return( 1 );
608
609exit:
610 return( 0 );
611}
612
Gilles Peskinec7998b72018-11-07 18:45:02 +0100613/* We need two keys to exercise key agreement. Exercise the
614 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200615static psa_status_t key_agreement_with_self(
616 psa_key_derivation_operation_t *operation,
617 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100618{
619 psa_key_type_t private_key_type;
620 psa_key_type_t public_key_type;
621 size_t key_bits;
622 uint8_t *public_key = NULL;
623 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200624 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200625 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
626 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200627 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200628 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100629
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200630 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
631 private_key_type = psa_get_key_type( &attributes );
632 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200633 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100634 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
635 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100636 PSA_ASSERT( psa_export_public_key( handle,
637 public_key, public_key_length,
638 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100639
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200640 status = psa_key_derivation_key_agreement(
641 operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle,
642 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100643exit:
644 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200645 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100646 return( status );
647}
648
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200649/* We need two keys to exercise key agreement. Exercise the
650 * private key against its own public key. */
651static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
652 psa_key_handle_t handle )
653{
654 psa_key_type_t private_key_type;
655 psa_key_type_t public_key_type;
656 size_t key_bits;
657 uint8_t *public_key = NULL;
658 size_t public_key_length;
659 uint8_t output[1024];
660 size_t output_length;
661 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200662 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
663 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200664 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200666
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200667 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
668 private_key_type = psa_get_key_type( &attributes );
669 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200670 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200671 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
672 ASSERT_ALLOC( public_key, public_key_length );
673 PSA_ASSERT( psa_export_public_key( handle,
674 public_key, public_key_length,
675 &public_key_length ) );
676
Gilles Peskinebe697d82019-05-16 18:00:41 +0200677 status = psa_raw_key_agreement( alg, handle,
678 public_key, public_key_length,
679 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200680exit:
681 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200682 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200683 return( status );
684}
685
686static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
687 psa_key_usage_t usage,
688 psa_algorithm_t alg )
689{
690 int ok = 0;
691
692 if( usage & PSA_KEY_USAGE_DERIVE )
693 {
694 /* We need two keys to exercise key agreement. Exercise the
695 * private key against its own public key. */
696 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
697 }
698 ok = 1;
699
700exit:
701 return( ok );
702}
703
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100704static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200705 psa_key_usage_t usage,
706 psa_algorithm_t alg )
707{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200708 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200709 unsigned char output[1];
710 int ok = 0;
711
712 if( usage & PSA_KEY_USAGE_DERIVE )
713 {
714 /* We need two keys to exercise key agreement. Exercise the
715 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200716 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
717 PSA_ASSERT( key_agreement_with_self( &operation, handle ) );
718 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200719 output,
720 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200721 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200722 }
723 ok = 1;
724
725exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200726 return( ok );
727}
728
Jaeden Amerof7dca862019-06-27 17:31:33 +0100729int asn1_skip_integer( unsigned char **p, const unsigned char *end,
730 size_t min_bits, size_t max_bits,
731 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200732{
733 size_t len;
734 size_t actual_bits;
735 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100736 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100737 MBEDTLS_ASN1_INTEGER ),
738 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200739
740 /* Check if the retrieved length doesn't extend the actual buffer's size.
741 * It is assumed here, that end >= p, which validates casting to size_t. */
742 TEST_ASSERT( len <= (size_t)( end - *p) );
743
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200744 /* Tolerate a slight departure from DER encoding:
745 * - 0 may be represented by an empty string or a 1-byte string.
746 * - The sign bit may be used as a value bit. */
747 if( ( len == 1 && ( *p )[0] == 0 ) ||
748 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
749 {
750 ++( *p );
751 --len;
752 }
753 if( min_bits == 0 && len == 0 )
754 return( 1 );
755 msb = ( *p )[0];
756 TEST_ASSERT( msb != 0 );
757 actual_bits = 8 * ( len - 1 );
758 while( msb != 0 )
759 {
760 msb >>= 1;
761 ++actual_bits;
762 }
763 TEST_ASSERT( actual_bits >= min_bits );
764 TEST_ASSERT( actual_bits <= max_bits );
765 if( must_be_odd )
766 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
767 *p += len;
768 return( 1 );
769exit:
770 return( 0 );
771}
772
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200773static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
774 uint8_t *exported, size_t exported_length )
775{
776 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100777 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200778 else
779 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200780
781#if defined(MBEDTLS_DES_C)
782 if( type == PSA_KEY_TYPE_DES )
783 {
784 /* Check the parity bits. */
785 unsigned i;
786 for( i = 0; i < bits / 8; i++ )
787 {
788 unsigned bit_count = 0;
789 unsigned m;
790 for( m = 1; m <= 0x100; m <<= 1 )
791 {
792 if( exported[i] & m )
793 ++bit_count;
794 }
795 TEST_ASSERT( bit_count % 2 != 0 );
796 }
797 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200798 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200799#endif
800
801#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200802 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200803 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200804 uint8_t *p = exported;
805 uint8_t *end = exported + exported_length;
806 size_t len;
807 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200808 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200809 * modulus INTEGER, -- n
810 * publicExponent INTEGER, -- e
811 * privateExponent INTEGER, -- d
812 * prime1 INTEGER, -- p
813 * prime2 INTEGER, -- q
814 * exponent1 INTEGER, -- d mod (p-1)
815 * exponent2 INTEGER, -- d mod (q-1)
816 * coefficient INTEGER, -- (inverse of q) mod p
817 * }
818 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100819 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
820 MBEDTLS_ASN1_SEQUENCE |
821 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
822 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200823 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
824 goto exit;
825 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
826 goto exit;
827 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
828 goto exit;
829 /* Require d to be at least half the size of n. */
830 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
831 goto exit;
832 /* Require p and q to be at most half the size of n, rounded up. */
833 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
834 goto exit;
835 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
836 goto exit;
837 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
838 goto exit;
839 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
840 goto exit;
841 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
842 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100843 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100844 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200845 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200846#endif /* MBEDTLS_RSA_C */
847
848#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200849 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200850 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100851 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100852 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100853 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200854 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200855#endif /* MBEDTLS_ECP_C */
856
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200857 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
858 {
859 uint8_t *p = exported;
860 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200861#if defined(MBEDTLS_RSA_C)
862 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
863 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100864 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200865 /* RSAPublicKey ::= SEQUENCE {
866 * modulus INTEGER, -- n
867 * publicExponent INTEGER } -- e
868 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100869 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
870 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100871 MBEDTLS_ASN1_CONSTRUCTED ),
872 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100873 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200874 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
875 goto exit;
876 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
877 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100878 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200879 }
880 else
881#endif /* MBEDTLS_RSA_C */
882#if defined(MBEDTLS_ECP_C)
883 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
884 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000885 /* The representation of an ECC public key is:
886 * - The byte 0x04;
887 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
888 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
889 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000890 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100891 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
892 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200893 }
894 else
895#endif /* MBEDTLS_ECP_C */
896 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100897 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200898 mbedtls_snprintf( message, sizeof( message ),
899 "No sanity check for public key type=0x%08lx",
900 (unsigned long) type );
901 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200902 (void) p;
903 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200904 return( 0 );
905 }
906 }
907 else
908
909 {
910 /* No sanity checks for other types */
911 }
912
913 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200914
915exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200916 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200917}
918
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100919static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200920 psa_key_usage_t usage )
921{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200922 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200923 uint8_t *exported = NULL;
924 size_t exported_size = 0;
925 size_t exported_length = 0;
926 int ok = 0;
927
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200928 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200929
930 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200931 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200932 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100933 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
934 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200935 ok = 1;
936 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +0200937 }
938
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200939 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
940 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200941 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200942
Gilles Peskine8817f612018-12-18 00:18:46 +0100943 PSA_ASSERT( psa_export_key( handle,
944 exported, exported_size,
945 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200946 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
947 psa_get_key_bits( &attributes ),
948 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +0200949
950exit:
951 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200952 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200953 return( ok );
954}
955
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100956static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200957{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200958 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200959 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +0200960 uint8_t *exported = NULL;
961 size_t exported_size = 0;
962 size_t exported_length = 0;
963 int ok = 0;
964
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200965 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
966 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200967 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100968 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100969 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200970 return( 1 );
971 }
972
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200973 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200974 psa_get_key_type( &attributes ) );
975 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
976 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200977 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200978
Gilles Peskine8817f612018-12-18 00:18:46 +0100979 PSA_ASSERT( psa_export_public_key( handle,
980 exported, exported_size,
981 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200982 ok = exported_key_sanity_check( public_type,
983 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +0200984 exported, exported_length );
985
986exit:
987 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200988 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200989 return( ok );
990}
991
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100992/** Do smoke tests on a key.
993 *
994 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
995 * sign/verify, or derivation) that is permitted according to \p usage.
996 * \p usage and \p alg should correspond to the expected policy on the
997 * key.
998 *
999 * Export the key if permitted by \p usage, and check that the output
1000 * looks sensible. If \p usage forbids export, check that
1001 * \p psa_export_key correctly rejects the attempt. If the key is
1002 * asymmetric, also check \p psa_export_public_key.
1003 *
1004 * If the key fails the tests, this function calls the test framework's
1005 * `test_fail` function and returns false. Otherwise this function returns
1006 * true. Therefore it should be used as follows:
1007 * ```
1008 * if( ! exercise_key( ... ) ) goto exit;
1009 * ```
1010 *
1011 * \param handle The key to exercise. It should be capable of performing
1012 * \p alg.
1013 * \param usage The usage flags to assume.
1014 * \param alg The algorithm to exercise.
1015 *
1016 * \retval 0 The key failed the smoke tests.
1017 * \retval 1 The key passed the smoke tests.
1018 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001019static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001020 psa_key_usage_t usage,
1021 psa_algorithm_t alg )
1022{
1023 int ok;
1024 if( alg == 0 )
1025 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1026 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001027 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001028 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001029 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001030 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001031 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001032 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001033 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001034 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001035 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001036 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001038 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1039 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001040 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001041 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001042 else
1043 {
1044 char message[40];
1045 mbedtls_snprintf( message, sizeof( message ),
1046 "No code to exercise alg=0x%08lx",
1047 (unsigned long) alg );
1048 test_fail( message, __LINE__, __FILE__ );
1049 ok = 0;
1050 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001051
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001052 ok = ok && exercise_export_key( handle, usage );
1053 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001054
Gilles Peskine02b75072018-07-01 22:31:34 +02001055 return( ok );
1056}
1057
Gilles Peskine10df3412018-10-25 22:35:43 +02001058static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1059 psa_algorithm_t alg )
1060{
1061 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1062 {
1063 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1064 PSA_KEY_USAGE_VERIFY :
1065 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1066 }
1067 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1068 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1069 {
1070 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1071 PSA_KEY_USAGE_ENCRYPT :
1072 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1073 }
1074 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1075 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1076 {
1077 return( PSA_KEY_USAGE_DERIVE );
1078 }
1079 else
1080 {
1081 return( 0 );
1082 }
1083
1084}
Darryl Green0c6575a2018-11-07 16:05:30 +00001085
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001086static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1087{
1088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1089 uint8_t buffer[1];
1090 size_t length;
1091 int ok = 0;
1092
Gilles Peskinec87af662019-05-15 16:12:22 +02001093 psa_set_key_id( &attributes, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001094 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1095 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1096 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1097 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1098 PSA_ERROR_INVALID_HANDLE );
1099 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001100 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001101 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1102 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1103 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1104 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1105
1106 TEST_EQUAL( psa_export_key( handle,
1107 buffer, sizeof( buffer ), &length ),
1108 PSA_ERROR_INVALID_HANDLE );
1109 TEST_EQUAL( psa_export_public_key( handle,
1110 buffer, sizeof( buffer ), &length ),
1111 PSA_ERROR_INVALID_HANDLE );
1112
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001113 ok = 1;
1114
1115exit:
1116 psa_reset_key_attributes( &attributes );
1117 return( ok );
1118}
1119
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001120/* Assert that a key isn't reported as having a slot number. */
1121#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1122#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1123 do \
1124 { \
1125 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1126 TEST_EQUAL( psa_get_key_slot_number( \
1127 attributes, \
1128 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1129 PSA_ERROR_INVALID_ARGUMENT ); \
1130 } \
1131 while( 0 )
1132#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1133#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1134 ( (void) 0 )
1135#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1136
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001137/* An overapproximation of the amount of storage needed for a key of the
1138 * given type and with the given content. The API doesn't make it easy
1139 * to find a good value for the size. The current implementation doesn't
1140 * care about the value anyway. */
1141#define KEY_BITS_FROM_DATA( type, data ) \
1142 ( data )->len
1143
Darryl Green0c6575a2018-11-07 16:05:30 +00001144typedef enum {
1145 IMPORT_KEY = 0,
1146 GENERATE_KEY = 1,
1147 DERIVE_KEY = 2
1148} generate_method;
1149
Gilles Peskinee59236f2018-01-27 23:32:46 +01001150/* END_HEADER */
1151
1152/* BEGIN_DEPENDENCIES
1153 * depends_on:MBEDTLS_PSA_CRYPTO_C
1154 * END_DEPENDENCIES
1155 */
1156
1157/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001158void static_checks( )
1159{
1160 size_t max_truncated_mac_size =
1161 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1162
1163 /* Check that the length for a truncated MAC always fits in the algorithm
1164 * encoding. The shifted mask is the maximum truncated value. The
1165 * untruncated algorithm may be one byte larger. */
1166 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1167}
1168/* END_CASE */
1169
1170/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001171void attributes_set_get( int id_arg, int lifetime_arg,
1172 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001173 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001174{
Gilles Peskine4747d192019-04-17 15:05:45 +02001175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001176 psa_key_id_t id = id_arg;
1177 psa_key_lifetime_t lifetime = lifetime_arg;
1178 psa_key_usage_t usage_flags = usage_flags_arg;
1179 psa_algorithm_t alg = alg_arg;
1180 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001181 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001182
1183 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1184 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1185 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1186 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1187 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001188 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001189
Gilles Peskinec87af662019-05-15 16:12:22 +02001190 psa_set_key_id( &attributes, id );
1191 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001192 psa_set_key_usage_flags( &attributes, usage_flags );
1193 psa_set_key_algorithm( &attributes, alg );
1194 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001195 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001196
1197 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1198 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1199 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1200 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1201 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001202 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001203
1204 psa_reset_key_attributes( &attributes );
1205
1206 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1207 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1208 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1209 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1210 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001211 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001212}
1213/* END_CASE */
1214
1215/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001216void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1217 int expected_id_arg, int expected_lifetime_arg )
1218{
1219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1220 psa_key_id_t id1 = id1_arg;
1221 psa_key_lifetime_t lifetime = lifetime_arg;
1222 psa_key_id_t id2 = id2_arg;
1223 psa_key_id_t expected_id = expected_id_arg;
1224 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1225
1226 if( id1_arg != -1 )
1227 psa_set_key_id( &attributes, id1 );
1228 if( lifetime_arg != -1 )
1229 psa_set_key_lifetime( &attributes, lifetime );
1230 if( id2_arg != -1 )
1231 psa_set_key_id( &attributes, id2 );
1232
1233 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1234 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1235}
1236/* END_CASE */
1237
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001238/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1239void slot_number_attribute( )
1240{
1241 psa_key_slot_number_t slot_number = 0xdeadbeef;
1242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1243
1244 /* Initially, there is no slot number. */
1245 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1246 PSA_ERROR_INVALID_ARGUMENT );
1247
1248 /* Test setting a slot number. */
1249 psa_set_key_slot_number( &attributes, 0 );
1250 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1251 TEST_EQUAL( slot_number, 0 );
1252
1253 /* Test changing the slot number. */
1254 psa_set_key_slot_number( &attributes, 42 );
1255 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1256 TEST_EQUAL( slot_number, 42 );
1257
1258 /* Test clearing the slot number. */
1259 psa_clear_key_slot_number( &attributes );
1260 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1261 PSA_ERROR_INVALID_ARGUMENT );
1262
1263 /* Clearing again should have no effect. */
1264 psa_clear_key_slot_number( &attributes );
1265 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1266 PSA_ERROR_INVALID_ARGUMENT );
1267
1268 /* Test that reset clears the slot number. */
1269 psa_set_key_slot_number( &attributes, 42 );
1270 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1271 TEST_EQUAL( slot_number, 42 );
1272 psa_reset_key_attributes( &attributes );
1273 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1274 PSA_ERROR_INVALID_ARGUMENT );
1275}
1276/* END_CASE */
1277
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001278/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001279void import_with_policy( int type_arg,
1280 int usage_arg, int alg_arg,
1281 int expected_status_arg )
1282{
1283 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1284 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1285 psa_key_handle_t handle = 0;
1286 psa_key_type_t type = type_arg;
1287 psa_key_usage_t usage = usage_arg;
1288 psa_algorithm_t alg = alg_arg;
1289 psa_status_t expected_status = expected_status_arg;
1290 const uint8_t key_material[16] = {0};
1291 psa_status_t status;
1292
1293 PSA_ASSERT( psa_crypto_init( ) );
1294
1295 psa_set_key_type( &attributes, type );
1296 psa_set_key_usage_flags( &attributes, usage );
1297 psa_set_key_algorithm( &attributes, alg );
1298
1299 status = psa_import_key( &attributes,
1300 key_material, sizeof( key_material ),
1301 &handle );
1302 TEST_EQUAL( status, expected_status );
1303 if( status != PSA_SUCCESS )
1304 goto exit;
1305
1306 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1307 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1308 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1309 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001310 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001311
1312 PSA_ASSERT( psa_destroy_key( handle ) );
1313 test_operations_on_invalid_handle( handle );
1314
1315exit:
1316 psa_destroy_key( handle );
1317 psa_reset_key_attributes( &got_attributes );
1318 PSA_DONE( );
1319}
1320/* END_CASE */
1321
1322/* BEGIN_CASE */
1323void import_with_data( data_t *data, int type_arg,
1324 int attr_bits_arg,
1325 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001326{
1327 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1328 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001329 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001330 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001331 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001332 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001333 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001334
Gilles Peskine8817f612018-12-18 00:18:46 +01001335 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001336
Gilles Peskine4747d192019-04-17 15:05:45 +02001337 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001338 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001339
Gilles Peskine73676cb2019-05-15 20:15:10 +02001340 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001341 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001342 if( status != PSA_SUCCESS )
1343 goto exit;
1344
1345 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1346 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001347 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001348 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001349 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001350
1351 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001352 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001353
1354exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001355 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001356 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001357 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001358}
1359/* END_CASE */
1360
1361/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001362void import_large_key( int type_arg, int byte_size_arg,
1363 int expected_status_arg )
1364{
1365 psa_key_type_t type = type_arg;
1366 size_t byte_size = byte_size_arg;
1367 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1368 psa_status_t expected_status = expected_status_arg;
1369 psa_key_handle_t handle = 0;
1370 psa_status_t status;
1371 uint8_t *buffer = NULL;
1372 size_t buffer_size = byte_size + 1;
1373 size_t n;
1374
1375 /* It would be better to skip the test than fail it if the allocation
1376 * fails, but the test framework doesn't support this yet. */
1377 ASSERT_ALLOC( buffer, buffer_size );
1378 memset( buffer, 'K', byte_size );
1379
1380 PSA_ASSERT( psa_crypto_init( ) );
1381
1382 /* Try importing the key */
1383 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1384 psa_set_key_type( &attributes, type );
1385 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1386 TEST_EQUAL( status, expected_status );
1387
1388 if( status == PSA_SUCCESS )
1389 {
1390 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1391 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1392 TEST_EQUAL( psa_get_key_bits( &attributes ),
1393 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001394 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001395 memset( buffer, 0, byte_size + 1 );
1396 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1397 for( n = 0; n < byte_size; n++ )
1398 TEST_EQUAL( buffer[n], 'K' );
1399 for( n = byte_size; n < buffer_size; n++ )
1400 TEST_EQUAL( buffer[n], 0 );
1401 }
1402
1403exit:
1404 psa_destroy_key( handle );
1405 PSA_DONE( );
1406 mbedtls_free( buffer );
1407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001411void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1412{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001413 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001414 size_t bits = bits_arg;
1415 psa_status_t expected_status = expected_status_arg;
1416 psa_status_t status;
1417 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001418 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001419 size_t buffer_size = /* Slight overapproximations */
1420 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001421 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001422 unsigned char *p;
1423 int ret;
1424 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001425 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001426
Gilles Peskine8817f612018-12-18 00:18:46 +01001427 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001428 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001429
1430 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1431 bits, keypair ) ) >= 0 );
1432 length = ret;
1433
1434 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001435 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001436 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001437 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001438
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001439 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001440 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001441
1442exit:
1443 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001444 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001445}
1446/* END_CASE */
1447
1448/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001449void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001450 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001451 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001452 int expected_bits,
1453 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001454 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001455 int canonical_input )
1456{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001457 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001458 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001459 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001460 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001461 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001462 unsigned char *exported = NULL;
1463 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001464 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001465 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001466 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001468 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001469
Moran Pekercb088e72018-07-17 17:36:59 +03001470 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001471 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001472 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001473 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001474 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001475
Gilles Peskine4747d192019-04-17 15:05:45 +02001476 psa_set_key_usage_flags( &attributes, usage_arg );
1477 psa_set_key_algorithm( &attributes, alg );
1478 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001479
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001480 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001481 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001482
1483 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001484 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1485 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1486 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001487 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001488
1489 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001490 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001491 exported, export_size,
1492 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001493 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001494
1495 /* The exported length must be set by psa_export_key() to a value between 0
1496 * and export_size. On errors, the exported length must be 0. */
1497 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1498 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1499 TEST_ASSERT( exported_length <= export_size );
1500
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001501 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001502 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001504 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001505 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001506 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001507 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001508
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001509 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001510 goto exit;
1511
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001512 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001513 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001514 else
1515 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001516 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001517 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1518 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001519 PSA_ASSERT( psa_export_key( handle2,
1520 reexported,
1521 export_size,
1522 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001523 ASSERT_COMPARE( exported, exported_length,
1524 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001525 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001526 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001527 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001528
1529destroy:
1530 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001531 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001532 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001533
1534exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001535 mbedtls_free( exported );
1536 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001537 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001538 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001539}
1540/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001541
Moran Pekerf709f4a2018-06-06 17:26:04 +03001542/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001543void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001544 int type_arg,
1545 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001546 int export_size_delta,
1547 int expected_export_status_arg,
1548 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001549{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001550 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001551 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001552 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001553 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001554 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001555 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001556 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001557 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001559
Gilles Peskine8817f612018-12-18 00:18:46 +01001560 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001561
Gilles Peskine4747d192019-04-17 15:05:45 +02001562 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1563 psa_set_key_algorithm( &attributes, alg );
1564 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001565
1566 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001567 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001568
Gilles Peskine49c25912018-10-29 15:15:31 +01001569 /* Export the public key */
1570 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001571 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001572 exported, export_size,
1573 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001574 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001575 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001576 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001577 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001578 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001579 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1580 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001581 TEST_ASSERT( expected_public_key->len <=
1582 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001583 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1584 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001585 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001586
1587exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001588 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001589 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001590 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001591 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001592}
1593/* END_CASE */
1594
Gilles Peskine20035e32018-02-03 22:44:14 +01001595/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001596void import_and_exercise_key( data_t *data,
1597 int type_arg,
1598 int bits_arg,
1599 int alg_arg )
1600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001601 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001602 psa_key_type_t type = type_arg;
1603 size_t bits = bits_arg;
1604 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001605 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001606 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001607 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001608
Gilles Peskine8817f612018-12-18 00:18:46 +01001609 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001610
Gilles Peskine4747d192019-04-17 15:05:45 +02001611 psa_set_key_usage_flags( &attributes, usage );
1612 psa_set_key_algorithm( &attributes, alg );
1613 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001614
1615 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001616 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001617
1618 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001619 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1620 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1621 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001622
1623 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001624 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001625 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001626
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001627 PSA_ASSERT( psa_destroy_key( handle ) );
1628 test_operations_on_invalid_handle( handle );
1629
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001630exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001631 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001632 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001633 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001634}
1635/* END_CASE */
1636
1637/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001638void key_policy( int usage_arg, int alg_arg )
1639{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001640 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001641 psa_algorithm_t alg = alg_arg;
1642 psa_key_usage_t usage = usage_arg;
1643 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1644 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001646
1647 memset( key, 0x2a, sizeof( key ) );
1648
Gilles Peskine8817f612018-12-18 00:18:46 +01001649 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001650
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001651 psa_set_key_usage_flags( &attributes, usage );
1652 psa_set_key_algorithm( &attributes, alg );
1653 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001654
Gilles Peskine73676cb2019-05-15 20:15:10 +02001655 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001656
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001657 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1658 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1659 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1660 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001661
1662exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001663 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001664 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001665 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001666}
1667/* END_CASE */
1668
1669/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001670void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001671{
1672 /* Test each valid way of initializing the object, except for `= {0}`, as
1673 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1674 * though it's OK by the C standard. We could test for this, but we'd need
1675 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001676 psa_key_attributes_t func = psa_key_attributes_init( );
1677 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1678 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001679
1680 memset( &zero, 0, sizeof( zero ) );
1681
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001682 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1683 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1684 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001685
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001686 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1687 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1688 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1689
1690 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1691 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1692 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1693
1694 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1695 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1696 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1697
1698 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1699 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1700 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001701}
1702/* END_CASE */
1703
1704/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001705void mac_key_policy( int policy_usage,
1706 int policy_alg,
1707 int key_type,
1708 data_t *key_data,
1709 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001710{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001711 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001713 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001714 psa_status_t status;
1715 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001716
Gilles Peskine8817f612018-12-18 00:18:46 +01001717 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001718
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001719 psa_set_key_usage_flags( &attributes, policy_usage );
1720 psa_set_key_algorithm( &attributes, policy_alg );
1721 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001722
Gilles Peskine049c7532019-05-15 20:22:09 +02001723 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1724 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001725
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001726 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001727 if( policy_alg == exercise_alg &&
1728 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001729 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001730 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001731 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001732 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001733
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001734 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001735 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001736 if( policy_alg == exercise_alg &&
1737 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001738 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001739 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001740 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001741
1742exit:
1743 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001744 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001745 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001746}
1747/* END_CASE */
1748
1749/* BEGIN_CASE */
1750void cipher_key_policy( int policy_usage,
1751 int policy_alg,
1752 int key_type,
1753 data_t *key_data,
1754 int exercise_alg )
1755{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001756 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001758 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001759 psa_status_t status;
1760
Gilles Peskine8817f612018-12-18 00:18:46 +01001761 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001762
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001763 psa_set_key_usage_flags( &attributes, policy_usage );
1764 psa_set_key_algorithm( &attributes, policy_alg );
1765 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001766
Gilles Peskine049c7532019-05-15 20:22:09 +02001767 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1768 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001769
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001770 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001771 if( policy_alg == exercise_alg &&
1772 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001773 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001774 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001775 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001776 psa_cipher_abort( &operation );
1777
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001778 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001779 if( policy_alg == exercise_alg &&
1780 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001781 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001782 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001783 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001784
1785exit:
1786 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001787 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001788 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001789}
1790/* END_CASE */
1791
1792/* BEGIN_CASE */
1793void aead_key_policy( int policy_usage,
1794 int policy_alg,
1795 int key_type,
1796 data_t *key_data,
1797 int nonce_length_arg,
1798 int tag_length_arg,
1799 int exercise_alg )
1800{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001801 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001803 psa_status_t status;
1804 unsigned char nonce[16] = {0};
1805 size_t nonce_length = nonce_length_arg;
1806 unsigned char tag[16];
1807 size_t tag_length = tag_length_arg;
1808 size_t output_length;
1809
1810 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1811 TEST_ASSERT( tag_length <= sizeof( tag ) );
1812
Gilles Peskine8817f612018-12-18 00:18:46 +01001813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001814
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001815 psa_set_key_usage_flags( &attributes, policy_usage );
1816 psa_set_key_algorithm( &attributes, policy_alg );
1817 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001818
Gilles Peskine049c7532019-05-15 20:22:09 +02001819 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1820 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001821
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001822 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001823 nonce, nonce_length,
1824 NULL, 0,
1825 NULL, 0,
1826 tag, tag_length,
1827 &output_length );
1828 if( policy_alg == exercise_alg &&
1829 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001830 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001831 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001832 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833
1834 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001835 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001836 nonce, nonce_length,
1837 NULL, 0,
1838 tag, tag_length,
1839 NULL, 0,
1840 &output_length );
1841 if( policy_alg == exercise_alg &&
1842 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001843 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001844 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001845 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001846
1847exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001848 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001849 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001850}
1851/* END_CASE */
1852
1853/* BEGIN_CASE */
1854void asymmetric_encryption_key_policy( int policy_usage,
1855 int policy_alg,
1856 int key_type,
1857 data_t *key_data,
1858 int exercise_alg )
1859{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001860 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001861 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001862 psa_status_t status;
1863 size_t key_bits;
1864 size_t buffer_length;
1865 unsigned char *buffer = NULL;
1866 size_t output_length;
1867
Gilles Peskine8817f612018-12-18 00:18:46 +01001868 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001869
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001870 psa_set_key_usage_flags( &attributes, policy_usage );
1871 psa_set_key_algorithm( &attributes, policy_alg );
1872 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001873
Gilles Peskine049c7532019-05-15 20:22:09 +02001874 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1875 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001876
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001877 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1878 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001879 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1880 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001881 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001882
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001883 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001884 NULL, 0,
1885 NULL, 0,
1886 buffer, buffer_length,
1887 &output_length );
1888 if( policy_alg == exercise_alg &&
1889 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001890 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001891 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001892 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001893
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001894 if( buffer_length != 0 )
1895 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001896 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001897 buffer, buffer_length,
1898 NULL, 0,
1899 buffer, buffer_length,
1900 &output_length );
1901 if( policy_alg == exercise_alg &&
1902 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001903 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001904 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001905 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001906
1907exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001908 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001909 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001910 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911 mbedtls_free( buffer );
1912}
1913/* END_CASE */
1914
1915/* BEGIN_CASE */
1916void asymmetric_signature_key_policy( int policy_usage,
1917 int policy_alg,
1918 int key_type,
1919 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001920 int exercise_alg,
1921 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001923 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001925 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001926 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1927 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1928 * compatible with the policy and `payload_length_arg` is supposed to be
1929 * a valid input length to sign. If `payload_length_arg <= 0`,
1930 * `exercise_alg` is supposed to be forbidden by the policy. */
1931 int compatible_alg = payload_length_arg > 0;
1932 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001933 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1934 size_t signature_length;
1935
Gilles Peskine8817f612018-12-18 00:18:46 +01001936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001937
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001938 psa_set_key_usage_flags( &attributes, policy_usage );
1939 psa_set_key_algorithm( &attributes, policy_alg );
1940 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941
Gilles Peskine049c7532019-05-15 20:22:09 +02001942 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1943 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001944
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001945 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001947 signature, sizeof( signature ),
1948 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001949 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001950 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001951 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001952 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953
1954 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001955 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001957 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001958 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001959 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001960 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001961 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001962
1963exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001964 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001965 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001966}
1967/* END_CASE */
1968
Janos Follathba3fab92019-06-11 14:50:16 +01001969/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001970void derive_key_policy( int policy_usage,
1971 int policy_alg,
1972 int key_type,
1973 data_t *key_data,
1974 int exercise_alg )
1975{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001976 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001977 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001978 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001979 psa_status_t status;
1980
Gilles Peskine8817f612018-12-18 00:18:46 +01001981 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001982
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001983 psa_set_key_usage_flags( &attributes, policy_usage );
1984 psa_set_key_algorithm( &attributes, policy_alg );
1985 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001986
Gilles Peskine049c7532019-05-15 20:22:09 +02001987 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1988 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001989
Janos Follathba3fab92019-06-11 14:50:16 +01001990 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1991
1992 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1993 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001994 {
Janos Follathba3fab92019-06-11 14:50:16 +01001995 PSA_ASSERT( psa_key_derivation_input_bytes(
1996 &operation,
1997 PSA_KEY_DERIVATION_INPUT_SEED,
1998 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001999 }
Janos Follathba3fab92019-06-11 14:50:16 +01002000
2001 status = psa_key_derivation_input_key( &operation,
2002 PSA_KEY_DERIVATION_INPUT_SECRET,
2003 handle );
2004
Gilles Peskineea0fb492018-07-12 17:17:20 +02002005 if( policy_alg == exercise_alg &&
2006 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002007 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002008 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002009 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002010
2011exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002012 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002013 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002014 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002015}
2016/* END_CASE */
2017
2018/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002019void agreement_key_policy( int policy_usage,
2020 int policy_alg,
2021 int key_type_arg,
2022 data_t *key_data,
2023 int exercise_alg )
2024{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002025 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002027 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002028 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002029 psa_status_t status;
2030
Gilles Peskine8817f612018-12-18 00:18:46 +01002031 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002032
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002033 psa_set_key_usage_flags( &attributes, policy_usage );
2034 psa_set_key_algorithm( &attributes, policy_alg );
2035 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002036
Gilles Peskine049c7532019-05-15 20:22:09 +02002037 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2038 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002039
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002040 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2041 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002042
Gilles Peskine01d718c2018-09-18 12:01:02 +02002043 if( policy_alg == exercise_alg &&
2044 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002045 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002046 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002047 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002048
2049exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002050 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002051 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002052 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002053}
2054/* END_CASE */
2055
2056/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002057void key_policy_alg2( int key_type_arg, data_t *key_data,
2058 int usage_arg, int alg_arg, int alg2_arg )
2059{
2060 psa_key_handle_t handle = 0;
2061 psa_key_type_t key_type = key_type_arg;
2062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2063 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2064 psa_key_usage_t usage = usage_arg;
2065 psa_algorithm_t alg = alg_arg;
2066 psa_algorithm_t alg2 = alg2_arg;
2067
2068 PSA_ASSERT( psa_crypto_init( ) );
2069
2070 psa_set_key_usage_flags( &attributes, usage );
2071 psa_set_key_algorithm( &attributes, alg );
2072 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2073 psa_set_key_type( &attributes, key_type );
2074 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2075 &handle ) );
2076
2077 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2078 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2079 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2080 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2081
2082 if( ! exercise_key( handle, usage, alg ) )
2083 goto exit;
2084 if( ! exercise_key( handle, usage, alg2 ) )
2085 goto exit;
2086
2087exit:
2088 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002089 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002090}
2091/* END_CASE */
2092
2093/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002094void raw_agreement_key_policy( int policy_usage,
2095 int policy_alg,
2096 int key_type_arg,
2097 data_t *key_data,
2098 int exercise_alg )
2099{
2100 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002102 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002103 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002104 psa_status_t status;
2105
2106 PSA_ASSERT( psa_crypto_init( ) );
2107
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002108 psa_set_key_usage_flags( &attributes, policy_usage );
2109 psa_set_key_algorithm( &attributes, policy_alg );
2110 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002111
Gilles Peskine049c7532019-05-15 20:22:09 +02002112 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2113 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002114
2115 status = raw_key_agreement_with_self( exercise_alg, handle );
2116
2117 if( policy_alg == exercise_alg &&
2118 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2119 PSA_ASSERT( status );
2120 else
2121 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2122
2123exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002124 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002125 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002126 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002127}
2128/* END_CASE */
2129
2130/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002131void copy_success( int source_usage_arg,
2132 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002133 int type_arg, data_t *material,
2134 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002135 int target_usage_arg,
2136 int target_alg_arg, int target_alg2_arg,
2137 int expected_usage_arg,
2138 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002139{
Gilles Peskineca25db92019-04-19 11:43:08 +02002140 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2141 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002142 psa_key_usage_t expected_usage = expected_usage_arg;
2143 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002144 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002145 psa_key_handle_t source_handle = 0;
2146 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002147 uint8_t *export_buffer = NULL;
2148
Gilles Peskine57ab7212019-01-28 13:03:09 +01002149 PSA_ASSERT( psa_crypto_init( ) );
2150
Gilles Peskineca25db92019-04-19 11:43:08 +02002151 /* Prepare the source key. */
2152 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2153 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002154 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002155 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002156 PSA_ASSERT( psa_import_key( &source_attributes,
2157 material->x, material->len,
2158 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002159 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002160
Gilles Peskineca25db92019-04-19 11:43:08 +02002161 /* Prepare the target attributes. */
2162 if( copy_attributes )
2163 target_attributes = source_attributes;
2164 if( target_usage_arg != -1 )
2165 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2166 if( target_alg_arg != -1 )
2167 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002168 if( target_alg2_arg != -1 )
2169 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002170
2171 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002172 PSA_ASSERT( psa_copy_key( source_handle,
2173 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002174
2175 /* Destroy the source to ensure that this doesn't affect the target. */
2176 PSA_ASSERT( psa_destroy_key( source_handle ) );
2177
2178 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002179 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2180 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2181 psa_get_key_type( &target_attributes ) );
2182 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2183 psa_get_key_bits( &target_attributes ) );
2184 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2185 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002186 TEST_EQUAL( expected_alg2,
2187 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002188 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2189 {
2190 size_t length;
2191 ASSERT_ALLOC( export_buffer, material->len );
2192 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2193 material->len, &length ) );
2194 ASSERT_COMPARE( material->x, material->len,
2195 export_buffer, length );
2196 }
2197 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2198 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002199 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2200 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002201
2202 PSA_ASSERT( psa_close_key( target_handle ) );
2203
2204exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002205 psa_reset_key_attributes( &source_attributes );
2206 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002207 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002208 mbedtls_free( export_buffer );
2209}
2210/* END_CASE */
2211
2212/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002213void copy_fail( int source_usage_arg,
2214 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002215 int type_arg, data_t *material,
2216 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002217 int target_usage_arg,
2218 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002219 int expected_status_arg )
2220{
2221 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2222 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2223 psa_key_handle_t source_handle = 0;
2224 psa_key_handle_t target_handle = 0;
2225
2226 PSA_ASSERT( psa_crypto_init( ) );
2227
2228 /* Prepare the source key. */
2229 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2230 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002231 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002232 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002233 PSA_ASSERT( psa_import_key( &source_attributes,
2234 material->x, material->len,
2235 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002236
2237 /* Prepare the target attributes. */
2238 psa_set_key_type( &target_attributes, target_type_arg );
2239 psa_set_key_bits( &target_attributes, target_bits_arg );
2240 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2241 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002242 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002243
2244 /* Try to copy the key. */
2245 TEST_EQUAL( psa_copy_key( source_handle,
2246 &target_attributes, &target_handle ),
2247 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002248
2249 PSA_ASSERT( psa_destroy_key( source_handle ) );
2250
Gilles Peskine4a644642019-05-03 17:14:08 +02002251exit:
2252 psa_reset_key_attributes( &source_attributes );
2253 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002254 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002255}
2256/* END_CASE */
2257
2258/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002259void hash_operation_init( )
2260{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002261 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002262 /* Test each valid way of initializing the object, except for `= {0}`, as
2263 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2264 * though it's OK by the C standard. We could test for this, but we'd need
2265 * to supress the Clang warning for the test. */
2266 psa_hash_operation_t func = psa_hash_operation_init( );
2267 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2268 psa_hash_operation_t zero;
2269
2270 memset( &zero, 0, sizeof( zero ) );
2271
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002272 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002273 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2274 PSA_ERROR_BAD_STATE );
2275 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2276 PSA_ERROR_BAD_STATE );
2277 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2278 PSA_ERROR_BAD_STATE );
2279
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002280 /* A default hash operation should be abortable without error. */
2281 PSA_ASSERT( psa_hash_abort( &func ) );
2282 PSA_ASSERT( psa_hash_abort( &init ) );
2283 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002284}
2285/* END_CASE */
2286
2287/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002288void hash_setup( int alg_arg,
2289 int expected_status_arg )
2290{
2291 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002292 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002293 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002294 psa_status_t status;
2295
Gilles Peskine8817f612018-12-18 00:18:46 +01002296 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002297
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002298 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002299 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002300
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002301 /* Whether setup succeeded or failed, abort must succeed. */
2302 PSA_ASSERT( psa_hash_abort( &operation ) );
2303
2304 /* If setup failed, reproduce the failure, so as to
2305 * test the resulting state of the operation object. */
2306 if( status != PSA_SUCCESS )
2307 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2308
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002309 /* Now the operation object should be reusable. */
2310#if defined(KNOWN_SUPPORTED_HASH_ALG)
2311 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2312 PSA_ASSERT( psa_hash_abort( &operation ) );
2313#endif
2314
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002315exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002316 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002317}
2318/* END_CASE */
2319
2320/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002321void hash_bad_order( )
2322{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002323 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002324 unsigned char input[] = "";
2325 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002326 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002327 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2328 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2329 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002330 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002331 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002332 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002333
Gilles Peskine8817f612018-12-18 00:18:46 +01002334 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002335
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002336 /* Call setup twice in a row. */
2337 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2338 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2339 PSA_ERROR_BAD_STATE );
2340 PSA_ASSERT( psa_hash_abort( &operation ) );
2341
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002342 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002343 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002344 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002345 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002346
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002347 /* Call update after finish. */
2348 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2349 PSA_ASSERT( psa_hash_finish( &operation,
2350 hash, sizeof( hash ), &hash_len ) );
2351 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002352 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002353 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002354
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002355 /* Call verify without calling setup beforehand. */
2356 TEST_EQUAL( psa_hash_verify( &operation,
2357 valid_hash, sizeof( valid_hash ) ),
2358 PSA_ERROR_BAD_STATE );
2359 PSA_ASSERT( psa_hash_abort( &operation ) );
2360
2361 /* Call verify after finish. */
2362 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2363 PSA_ASSERT( psa_hash_finish( &operation,
2364 hash, sizeof( hash ), &hash_len ) );
2365 TEST_EQUAL( psa_hash_verify( &operation,
2366 valid_hash, sizeof( valid_hash ) ),
2367 PSA_ERROR_BAD_STATE );
2368 PSA_ASSERT( psa_hash_abort( &operation ) );
2369
2370 /* Call verify twice in a row. */
2371 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2372 PSA_ASSERT( psa_hash_verify( &operation,
2373 valid_hash, sizeof( valid_hash ) ) );
2374 TEST_EQUAL( psa_hash_verify( &operation,
2375 valid_hash, sizeof( valid_hash ) ),
2376 PSA_ERROR_BAD_STATE );
2377 PSA_ASSERT( psa_hash_abort( &operation ) );
2378
2379 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002380 TEST_EQUAL( psa_hash_finish( &operation,
2381 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002382 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002383 PSA_ASSERT( psa_hash_abort( &operation ) );
2384
2385 /* Call finish twice in a row. */
2386 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2387 PSA_ASSERT( psa_hash_finish( &operation,
2388 hash, sizeof( hash ), &hash_len ) );
2389 TEST_EQUAL( psa_hash_finish( &operation,
2390 hash, sizeof( hash ), &hash_len ),
2391 PSA_ERROR_BAD_STATE );
2392 PSA_ASSERT( psa_hash_abort( &operation ) );
2393
2394 /* Call finish after calling verify. */
2395 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2396 PSA_ASSERT( psa_hash_verify( &operation,
2397 valid_hash, sizeof( valid_hash ) ) );
2398 TEST_EQUAL( psa_hash_finish( &operation,
2399 hash, sizeof( hash ), &hash_len ),
2400 PSA_ERROR_BAD_STATE );
2401 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002402
2403exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002404 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002405}
2406/* END_CASE */
2407
itayzafrir27e69452018-11-01 14:26:34 +02002408/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2409void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002410{
2411 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002412 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2413 * appended to it */
2414 unsigned char hash[] = {
2415 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2416 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2417 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002418 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002419 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002420
Gilles Peskine8817f612018-12-18 00:18:46 +01002421 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002422
itayzafrir27e69452018-11-01 14:26:34 +02002423 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002424 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002425 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002426 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002427
itayzafrir27e69452018-11-01 14:26:34 +02002428 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002429 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002430 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002431 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002432
itayzafrir27e69452018-11-01 14:26:34 +02002433 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002434 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002435 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002436 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002437
itayzafrirec93d302018-10-18 18:01:10 +03002438exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002439 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002440}
2441/* END_CASE */
2442
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002443/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2444void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002445{
2446 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002447 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002448 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002449 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002450 size_t hash_len;
2451
Gilles Peskine8817f612018-12-18 00:18:46 +01002452 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002453
itayzafrir58028322018-10-25 10:22:01 +03002454 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002455 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002456 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002457 hash, expected_size - 1, &hash_len ),
2458 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002459
2460exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002461 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002462}
2463/* END_CASE */
2464
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002465/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2466void hash_clone_source_state( )
2467{
2468 psa_algorithm_t alg = PSA_ALG_SHA_256;
2469 unsigned char hash[PSA_HASH_MAX_SIZE];
2470 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2471 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2472 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2473 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2474 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2475 size_t hash_len;
2476
2477 PSA_ASSERT( psa_crypto_init( ) );
2478 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2479
2480 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2481 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2482 PSA_ASSERT( psa_hash_finish( &op_finished,
2483 hash, sizeof( hash ), &hash_len ) );
2484 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2485 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2486
2487 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2488 PSA_ERROR_BAD_STATE );
2489
2490 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2491 PSA_ASSERT( psa_hash_finish( &op_init,
2492 hash, sizeof( hash ), &hash_len ) );
2493 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2494 PSA_ASSERT( psa_hash_finish( &op_finished,
2495 hash, sizeof( hash ), &hash_len ) );
2496 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2497 PSA_ASSERT( psa_hash_finish( &op_aborted,
2498 hash, sizeof( hash ), &hash_len ) );
2499
2500exit:
2501 psa_hash_abort( &op_source );
2502 psa_hash_abort( &op_init );
2503 psa_hash_abort( &op_setup );
2504 psa_hash_abort( &op_finished );
2505 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002506 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002507}
2508/* END_CASE */
2509
2510/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2511void hash_clone_target_state( )
2512{
2513 psa_algorithm_t alg = PSA_ALG_SHA_256;
2514 unsigned char hash[PSA_HASH_MAX_SIZE];
2515 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2516 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2517 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2518 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2519 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2520 size_t hash_len;
2521
2522 PSA_ASSERT( psa_crypto_init( ) );
2523
2524 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2525 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2526 PSA_ASSERT( psa_hash_finish( &op_finished,
2527 hash, sizeof( hash ), &hash_len ) );
2528 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2529 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2530
2531 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2532 PSA_ASSERT( psa_hash_finish( &op_target,
2533 hash, sizeof( hash ), &hash_len ) );
2534
2535 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2536 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2537 PSA_ERROR_BAD_STATE );
2538 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2539 PSA_ERROR_BAD_STATE );
2540
2541exit:
2542 psa_hash_abort( &op_target );
2543 psa_hash_abort( &op_init );
2544 psa_hash_abort( &op_setup );
2545 psa_hash_abort( &op_finished );
2546 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002547 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002548}
2549/* END_CASE */
2550
itayzafrir58028322018-10-25 10:22:01 +03002551/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002552void mac_operation_init( )
2553{
Jaeden Amero252ef282019-02-15 14:05:35 +00002554 const uint8_t input[1] = { 0 };
2555
Jaeden Amero769ce272019-01-04 11:48:03 +00002556 /* Test each valid way of initializing the object, except for `= {0}`, as
2557 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2558 * though it's OK by the C standard. We could test for this, but we'd need
2559 * to supress the Clang warning for the test. */
2560 psa_mac_operation_t func = psa_mac_operation_init( );
2561 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2562 psa_mac_operation_t zero;
2563
2564 memset( &zero, 0, sizeof( zero ) );
2565
Jaeden Amero252ef282019-02-15 14:05:35 +00002566 /* A freshly-initialized MAC operation should not be usable. */
2567 TEST_EQUAL( psa_mac_update( &func,
2568 input, sizeof( input ) ),
2569 PSA_ERROR_BAD_STATE );
2570 TEST_EQUAL( psa_mac_update( &init,
2571 input, sizeof( input ) ),
2572 PSA_ERROR_BAD_STATE );
2573 TEST_EQUAL( psa_mac_update( &zero,
2574 input, sizeof( input ) ),
2575 PSA_ERROR_BAD_STATE );
2576
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002577 /* A default MAC operation should be abortable without error. */
2578 PSA_ASSERT( psa_mac_abort( &func ) );
2579 PSA_ASSERT( psa_mac_abort( &init ) );
2580 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002581}
2582/* END_CASE */
2583
2584/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002585void mac_setup( int key_type_arg,
2586 data_t *key,
2587 int alg_arg,
2588 int expected_status_arg )
2589{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002590 psa_key_type_t key_type = key_type_arg;
2591 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002592 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002593 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002594 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2595#if defined(KNOWN_SUPPORTED_MAC_ALG)
2596 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2597#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002598
Gilles Peskine8817f612018-12-18 00:18:46 +01002599 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002600
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002601 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2602 &operation, &status ) )
2603 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002604 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002605
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002606 /* The operation object should be reusable. */
2607#if defined(KNOWN_SUPPORTED_MAC_ALG)
2608 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2609 smoke_test_key_data,
2610 sizeof( smoke_test_key_data ),
2611 KNOWN_SUPPORTED_MAC_ALG,
2612 &operation, &status ) )
2613 goto exit;
2614 TEST_EQUAL( status, PSA_SUCCESS );
2615#endif
2616
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002617exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002618 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619}
2620/* END_CASE */
2621
2622/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002623void mac_bad_order( )
2624{
2625 psa_key_handle_t handle = 0;
2626 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2627 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2628 const uint8_t key[] = {
2629 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2630 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2631 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002632 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002633 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2634 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2635 size_t sign_mac_length = 0;
2636 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2637 const uint8_t verify_mac[] = {
2638 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2639 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2640 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2641
2642 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002643 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2644 psa_set_key_algorithm( &attributes, alg );
2645 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002646
Gilles Peskine73676cb2019-05-15 20:15:10 +02002647 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002648
Jaeden Amero252ef282019-02-15 14:05:35 +00002649 /* Call update without calling setup beforehand. */
2650 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2651 PSA_ERROR_BAD_STATE );
2652 PSA_ASSERT( psa_mac_abort( &operation ) );
2653
2654 /* Call sign finish without calling setup beforehand. */
2655 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2656 &sign_mac_length),
2657 PSA_ERROR_BAD_STATE );
2658 PSA_ASSERT( psa_mac_abort( &operation ) );
2659
2660 /* Call verify finish without calling setup beforehand. */
2661 TEST_EQUAL( psa_mac_verify_finish( &operation,
2662 verify_mac, sizeof( verify_mac ) ),
2663 PSA_ERROR_BAD_STATE );
2664 PSA_ASSERT( psa_mac_abort( &operation ) );
2665
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002666 /* Call setup twice in a row. */
2667 PSA_ASSERT( psa_mac_sign_setup( &operation,
2668 handle, alg ) );
2669 TEST_EQUAL( psa_mac_sign_setup( &operation,
2670 handle, alg ),
2671 PSA_ERROR_BAD_STATE );
2672 PSA_ASSERT( psa_mac_abort( &operation ) );
2673
Jaeden Amero252ef282019-02-15 14:05:35 +00002674 /* Call update after sign finish. */
2675 PSA_ASSERT( psa_mac_sign_setup( &operation,
2676 handle, alg ) );
2677 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2678 PSA_ASSERT( psa_mac_sign_finish( &operation,
2679 sign_mac, sizeof( sign_mac ),
2680 &sign_mac_length ) );
2681 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2682 PSA_ERROR_BAD_STATE );
2683 PSA_ASSERT( psa_mac_abort( &operation ) );
2684
2685 /* Call update after verify finish. */
2686 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002687 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002688 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2689 PSA_ASSERT( psa_mac_verify_finish( &operation,
2690 verify_mac, sizeof( verify_mac ) ) );
2691 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2692 PSA_ERROR_BAD_STATE );
2693 PSA_ASSERT( psa_mac_abort( &operation ) );
2694
2695 /* Call sign finish twice in a row. */
2696 PSA_ASSERT( psa_mac_sign_setup( &operation,
2697 handle, alg ) );
2698 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2699 PSA_ASSERT( psa_mac_sign_finish( &operation,
2700 sign_mac, sizeof( sign_mac ),
2701 &sign_mac_length ) );
2702 TEST_EQUAL( psa_mac_sign_finish( &operation,
2703 sign_mac, sizeof( sign_mac ),
2704 &sign_mac_length ),
2705 PSA_ERROR_BAD_STATE );
2706 PSA_ASSERT( psa_mac_abort( &operation ) );
2707
2708 /* Call verify finish twice in a row. */
2709 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002710 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002711 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2712 PSA_ASSERT( psa_mac_verify_finish( &operation,
2713 verify_mac, sizeof( verify_mac ) ) );
2714 TEST_EQUAL( psa_mac_verify_finish( &operation,
2715 verify_mac, sizeof( verify_mac ) ),
2716 PSA_ERROR_BAD_STATE );
2717 PSA_ASSERT( psa_mac_abort( &operation ) );
2718
2719 /* Setup sign but try verify. */
2720 PSA_ASSERT( psa_mac_sign_setup( &operation,
2721 handle, alg ) );
2722 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2723 TEST_EQUAL( psa_mac_verify_finish( &operation,
2724 verify_mac, sizeof( verify_mac ) ),
2725 PSA_ERROR_BAD_STATE );
2726 PSA_ASSERT( psa_mac_abort( &operation ) );
2727
2728 /* Setup verify but try sign. */
2729 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002730 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002731 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2732 TEST_EQUAL( psa_mac_sign_finish( &operation,
2733 sign_mac, sizeof( sign_mac ),
2734 &sign_mac_length ),
2735 PSA_ERROR_BAD_STATE );
2736 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002737
Gilles Peskine76b29a72019-05-28 14:08:50 +02002738 PSA_ASSERT( psa_destroy_key( handle ) );
2739
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002740exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002741 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002742}
2743/* END_CASE */
2744
2745/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002746void mac_sign( int key_type_arg,
2747 data_t *key,
2748 int alg_arg,
2749 data_t *input,
2750 data_t *expected_mac )
2751{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002752 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002753 psa_key_type_t key_type = key_type_arg;
2754 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002755 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002756 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002757 /* Leave a little extra room in the output buffer. At the end of the
2758 * test, we'll check that the implementation didn't overwrite onto
2759 * this extra room. */
2760 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2761 size_t mac_buffer_size =
2762 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2763 size_t mac_length = 0;
2764
2765 memset( actual_mac, '+', sizeof( actual_mac ) );
2766 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2767 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2768
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002770
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002771 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2772 psa_set_key_algorithm( &attributes, alg );
2773 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002774
Gilles Peskine73676cb2019-05-15 20:15:10 +02002775 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002776
2777 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002778 PSA_ASSERT( psa_mac_sign_setup( &operation,
2779 handle, alg ) );
2780 PSA_ASSERT( psa_mac_update( &operation,
2781 input->x, input->len ) );
2782 PSA_ASSERT( psa_mac_sign_finish( &operation,
2783 actual_mac, mac_buffer_size,
2784 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002785
2786 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002787 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2788 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002789
2790 /* Verify that the end of the buffer is untouched. */
2791 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2792 sizeof( actual_mac ) - mac_length ) );
2793
2794exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002795 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002796 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002797}
2798/* END_CASE */
2799
2800/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002801void mac_verify( int key_type_arg,
2802 data_t *key,
2803 int alg_arg,
2804 data_t *input,
2805 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002806{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002807 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002808 psa_key_type_t key_type = key_type_arg;
2809 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002810 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002812
Gilles Peskine69c12672018-06-28 00:07:19 +02002813 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2814
Gilles Peskine8817f612018-12-18 00:18:46 +01002815 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002816
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002817 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2818 psa_set_key_algorithm( &attributes, alg );
2819 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002820
Gilles Peskine73676cb2019-05-15 20:15:10 +02002821 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002822
Gilles Peskine8817f612018-12-18 00:18:46 +01002823 PSA_ASSERT( psa_mac_verify_setup( &operation,
2824 handle, alg ) );
2825 PSA_ASSERT( psa_destroy_key( handle ) );
2826 PSA_ASSERT( psa_mac_update( &operation,
2827 input->x, input->len ) );
2828 PSA_ASSERT( psa_mac_verify_finish( &operation,
2829 expected_mac->x,
2830 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831
2832exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002833 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002834 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835}
2836/* END_CASE */
2837
2838/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002839void cipher_operation_init( )
2840{
Jaeden Ameroab439972019-02-15 14:12:05 +00002841 const uint8_t input[1] = { 0 };
2842 unsigned char output[1] = { 0 };
2843 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002844 /* Test each valid way of initializing the object, except for `= {0}`, as
2845 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2846 * though it's OK by the C standard. We could test for this, but we'd need
2847 * to supress the Clang warning for the test. */
2848 psa_cipher_operation_t func = psa_cipher_operation_init( );
2849 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2850 psa_cipher_operation_t zero;
2851
2852 memset( &zero, 0, sizeof( zero ) );
2853
Jaeden Ameroab439972019-02-15 14:12:05 +00002854 /* A freshly-initialized cipher operation should not be usable. */
2855 TEST_EQUAL( psa_cipher_update( &func,
2856 input, sizeof( input ),
2857 output, sizeof( output ),
2858 &output_length ),
2859 PSA_ERROR_BAD_STATE );
2860 TEST_EQUAL( psa_cipher_update( &init,
2861 input, sizeof( input ),
2862 output, sizeof( output ),
2863 &output_length ),
2864 PSA_ERROR_BAD_STATE );
2865 TEST_EQUAL( psa_cipher_update( &zero,
2866 input, sizeof( input ),
2867 output, sizeof( output ),
2868 &output_length ),
2869 PSA_ERROR_BAD_STATE );
2870
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002871 /* A default cipher operation should be abortable without error. */
2872 PSA_ASSERT( psa_cipher_abort( &func ) );
2873 PSA_ASSERT( psa_cipher_abort( &init ) );
2874 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002875}
2876/* END_CASE */
2877
2878/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002879void cipher_setup( int key_type_arg,
2880 data_t *key,
2881 int alg_arg,
2882 int expected_status_arg )
2883{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002884 psa_key_type_t key_type = key_type_arg;
2885 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002886 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002887 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002888 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002889#if defined(KNOWN_SUPPORTED_MAC_ALG)
2890 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2891#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002892
Gilles Peskine8817f612018-12-18 00:18:46 +01002893 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002894
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002895 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2896 &operation, &status ) )
2897 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002898 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002899
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002900 /* The operation object should be reusable. */
2901#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2902 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2903 smoke_test_key_data,
2904 sizeof( smoke_test_key_data ),
2905 KNOWN_SUPPORTED_CIPHER_ALG,
2906 &operation, &status ) )
2907 goto exit;
2908 TEST_EQUAL( status, PSA_SUCCESS );
2909#endif
2910
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002911exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002912 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002913}
2914/* END_CASE */
2915
2916/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002917void cipher_bad_order( )
2918{
2919 psa_key_handle_t handle = 0;
2920 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2921 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002922 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002923 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2924 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2925 const uint8_t key[] = {
2926 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2927 0xaa, 0xaa, 0xaa, 0xaa };
2928 const uint8_t text[] = {
2929 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2930 0xbb, 0xbb, 0xbb, 0xbb };
2931 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2932 size_t length = 0;
2933
2934 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002935 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2936 psa_set_key_algorithm( &attributes, alg );
2937 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02002938 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002939
2940
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002941 /* Call encrypt setup twice in a row. */
2942 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2943 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2944 PSA_ERROR_BAD_STATE );
2945 PSA_ASSERT( psa_cipher_abort( &operation ) );
2946
2947 /* Call decrypt setup twice in a row. */
2948 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2949 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2950 PSA_ERROR_BAD_STATE );
2951 PSA_ASSERT( psa_cipher_abort( &operation ) );
2952
Jaeden Ameroab439972019-02-15 14:12:05 +00002953 /* Generate an IV without calling setup beforehand. */
2954 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2955 buffer, sizeof( buffer ),
2956 &length ),
2957 PSA_ERROR_BAD_STATE );
2958 PSA_ASSERT( psa_cipher_abort( &operation ) );
2959
2960 /* Generate an IV twice in a row. */
2961 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2962 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2963 buffer, sizeof( buffer ),
2964 &length ) );
2965 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2966 buffer, sizeof( buffer ),
2967 &length ),
2968 PSA_ERROR_BAD_STATE );
2969 PSA_ASSERT( psa_cipher_abort( &operation ) );
2970
2971 /* Generate an IV after it's already set. */
2972 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2973 PSA_ASSERT( psa_cipher_set_iv( &operation,
2974 iv, sizeof( iv ) ) );
2975 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2976 buffer, sizeof( buffer ),
2977 &length ),
2978 PSA_ERROR_BAD_STATE );
2979 PSA_ASSERT( psa_cipher_abort( &operation ) );
2980
2981 /* Set an IV without calling setup beforehand. */
2982 TEST_EQUAL( psa_cipher_set_iv( &operation,
2983 iv, sizeof( iv ) ),
2984 PSA_ERROR_BAD_STATE );
2985 PSA_ASSERT( psa_cipher_abort( &operation ) );
2986
2987 /* Set an IV after it's already set. */
2988 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2989 PSA_ASSERT( psa_cipher_set_iv( &operation,
2990 iv, sizeof( iv ) ) );
2991 TEST_EQUAL( psa_cipher_set_iv( &operation,
2992 iv, sizeof( iv ) ),
2993 PSA_ERROR_BAD_STATE );
2994 PSA_ASSERT( psa_cipher_abort( &operation ) );
2995
2996 /* Set an IV after it's already generated. */
2997 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2998 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2999 buffer, sizeof( buffer ),
3000 &length ) );
3001 TEST_EQUAL( psa_cipher_set_iv( &operation,
3002 iv, sizeof( iv ) ),
3003 PSA_ERROR_BAD_STATE );
3004 PSA_ASSERT( psa_cipher_abort( &operation ) );
3005
3006 /* Call update without calling setup beforehand. */
3007 TEST_EQUAL( psa_cipher_update( &operation,
3008 text, sizeof( text ),
3009 buffer, sizeof( buffer ),
3010 &length ),
3011 PSA_ERROR_BAD_STATE );
3012 PSA_ASSERT( psa_cipher_abort( &operation ) );
3013
3014 /* Call update without an IV where an IV is required. */
3015 TEST_EQUAL( psa_cipher_update( &operation,
3016 text, sizeof( text ),
3017 buffer, sizeof( buffer ),
3018 &length ),
3019 PSA_ERROR_BAD_STATE );
3020 PSA_ASSERT( psa_cipher_abort( &operation ) );
3021
3022 /* Call update after finish. */
3023 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3024 PSA_ASSERT( psa_cipher_set_iv( &operation,
3025 iv, sizeof( iv ) ) );
3026 PSA_ASSERT( psa_cipher_finish( &operation,
3027 buffer, sizeof( buffer ), &length ) );
3028 TEST_EQUAL( psa_cipher_update( &operation,
3029 text, sizeof( text ),
3030 buffer, sizeof( buffer ),
3031 &length ),
3032 PSA_ERROR_BAD_STATE );
3033 PSA_ASSERT( psa_cipher_abort( &operation ) );
3034
3035 /* Call finish without calling setup beforehand. */
3036 TEST_EQUAL( psa_cipher_finish( &operation,
3037 buffer, sizeof( buffer ), &length ),
3038 PSA_ERROR_BAD_STATE );
3039 PSA_ASSERT( psa_cipher_abort( &operation ) );
3040
3041 /* Call finish without an IV where an IV is required. */
3042 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3043 /* Not calling update means we are encrypting an empty buffer, which is OK
3044 * for cipher modes with padding. */
3045 TEST_EQUAL( psa_cipher_finish( &operation,
3046 buffer, sizeof( buffer ), &length ),
3047 PSA_ERROR_BAD_STATE );
3048 PSA_ASSERT( psa_cipher_abort( &operation ) );
3049
3050 /* Call finish twice in a row. */
3051 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3052 PSA_ASSERT( psa_cipher_set_iv( &operation,
3053 iv, sizeof( iv ) ) );
3054 PSA_ASSERT( psa_cipher_finish( &operation,
3055 buffer, sizeof( buffer ), &length ) );
3056 TEST_EQUAL( psa_cipher_finish( &operation,
3057 buffer, sizeof( buffer ), &length ),
3058 PSA_ERROR_BAD_STATE );
3059 PSA_ASSERT( psa_cipher_abort( &operation ) );
3060
Gilles Peskine76b29a72019-05-28 14:08:50 +02003061 PSA_ASSERT( psa_destroy_key( handle ) );
3062
Jaeden Ameroab439972019-02-15 14:12:05 +00003063exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003064 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003065}
3066/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003067
Gilles Peskine50e586b2018-06-08 14:28:46 +02003068/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003069void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003070 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003071 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003072 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003073{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003074 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003075 psa_status_t status;
3076 psa_key_type_t key_type = key_type_arg;
3077 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003078 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003079 unsigned char *output = NULL;
3080 size_t output_buffer_size = 0;
3081 size_t function_output_length = 0;
3082 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003083 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003084 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003085
Gilles Peskine8817f612018-12-18 00:18:46 +01003086 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003087
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003088 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3089 psa_set_key_algorithm( &attributes, alg );
3090 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003091
Gilles Peskine73676cb2019-05-15 20:15:10 +02003092 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003093
Gilles Peskine8817f612018-12-18 00:18:46 +01003094 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3095 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003096
Gilles Peskine423005e2019-05-06 15:22:57 +02003097 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003098 output_buffer_size = ( (size_t) input->len +
3099 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003100 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003101
Gilles Peskine8817f612018-12-18 00:18:46 +01003102 PSA_ASSERT( psa_cipher_update( &operation,
3103 input->x, input->len,
3104 output, output_buffer_size,
3105 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003106 total_output_length += function_output_length;
3107 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003108 output + total_output_length,
3109 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003110 &function_output_length );
3111 total_output_length += function_output_length;
3112
Gilles Peskinefe11b722018-12-18 00:24:04 +01003113 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003114 if( expected_status == PSA_SUCCESS )
3115 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003116 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003117 ASSERT_COMPARE( expected_output->x, expected_output->len,
3118 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003119 }
3120
3121exit:
3122 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003123 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003124 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003125}
3126/* END_CASE */
3127
3128/* BEGIN_CASE */
3129void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003130 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003131 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003132 int first_part_size_arg,
3133 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003134 data_t *expected_output )
3135{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003136 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003137 psa_key_type_t key_type = key_type_arg;
3138 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003139 size_t first_part_size = first_part_size_arg;
3140 size_t output1_length = output1_length_arg;
3141 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003142 unsigned char *output = NULL;
3143 size_t output_buffer_size = 0;
3144 size_t function_output_length = 0;
3145 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003146 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003148
Gilles Peskine8817f612018-12-18 00:18:46 +01003149 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003150
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003151 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3152 psa_set_key_algorithm( &attributes, alg );
3153 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003154
Gilles Peskine73676cb2019-05-15 20:15:10 +02003155 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003156
Gilles Peskine8817f612018-12-18 00:18:46 +01003157 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3158 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003159
Gilles Peskine423005e2019-05-06 15:22:57 +02003160 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003161 output_buffer_size = ( (size_t) input->len +
3162 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003163 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003164
Gilles Peskinee0866522019-02-19 19:44:00 +01003165 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003166 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3167 output, output_buffer_size,
3168 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003169 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003170 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003171 PSA_ASSERT( psa_cipher_update( &operation,
3172 input->x + first_part_size,
3173 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003174 output + total_output_length,
3175 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003177 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003178 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003179 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003180 output + total_output_length,
3181 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003183 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003184 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003185
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003186 ASSERT_COMPARE( expected_output->x, expected_output->len,
3187 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003188
3189exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003190 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003191 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003192 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003193}
3194/* END_CASE */
3195
3196/* BEGIN_CASE */
3197void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003198 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003199 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003200 int first_part_size_arg,
3201 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003202 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003203{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003204 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003205
3206 psa_key_type_t key_type = key_type_arg;
3207 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003208 size_t first_part_size = first_part_size_arg;
3209 size_t output1_length = output1_length_arg;
3210 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003211 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003212 size_t output_buffer_size = 0;
3213 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003214 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003215 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003216 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003217
Gilles Peskine8817f612018-12-18 00:18:46 +01003218 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003219
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3221 psa_set_key_algorithm( &attributes, alg );
3222 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003223
Gilles Peskine73676cb2019-05-15 20:15:10 +02003224 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003225
Gilles Peskine8817f612018-12-18 00:18:46 +01003226 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3227 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003228
Gilles Peskine423005e2019-05-06 15:22:57 +02003229 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003230
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003231 output_buffer_size = ( (size_t) input->len +
3232 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003233 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003234
Gilles Peskinee0866522019-02-19 19:44:00 +01003235 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003236 PSA_ASSERT( psa_cipher_update( &operation,
3237 input->x, first_part_size,
3238 output, output_buffer_size,
3239 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003240 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003241 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003242 PSA_ASSERT( psa_cipher_update( &operation,
3243 input->x + first_part_size,
3244 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003245 output + total_output_length,
3246 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003247 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003248 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003249 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003251 output + total_output_length,
3252 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003253 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003254 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003255 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003256
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003257 ASSERT_COMPARE( expected_output->x, expected_output->len,
3258 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003259
3260exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003261 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003262 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003263 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003264}
3265/* END_CASE */
3266
Gilles Peskine50e586b2018-06-08 14:28:46 +02003267/* BEGIN_CASE */
3268void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003269 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003270 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003271 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003272{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003273 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003274 psa_status_t status;
3275 psa_key_type_t key_type = key_type_arg;
3276 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003277 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003278 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003279 size_t output_buffer_size = 0;
3280 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003281 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003282 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003283 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003284
Gilles Peskine8817f612018-12-18 00:18:46 +01003285 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003286
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003287 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3288 psa_set_key_algorithm( &attributes, alg );
3289 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003290
Gilles Peskine73676cb2019-05-15 20:15:10 +02003291 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003292
Gilles Peskine8817f612018-12-18 00:18:46 +01003293 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3294 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003295
Gilles Peskine423005e2019-05-06 15:22:57 +02003296 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003297
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003298 output_buffer_size = ( (size_t) input->len +
3299 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003300 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003301
Gilles Peskine8817f612018-12-18 00:18:46 +01003302 PSA_ASSERT( psa_cipher_update( &operation,
3303 input->x, input->len,
3304 output, output_buffer_size,
3305 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003306 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003307 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003308 output + total_output_length,
3309 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003310 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003311 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003312 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003313
3314 if( expected_status == PSA_SUCCESS )
3315 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003316 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003317 ASSERT_COMPARE( expected_output->x, expected_output->len,
3318 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003319 }
3320
Gilles Peskine50e586b2018-06-08 14:28:46 +02003321exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003322 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003323 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003324 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003325}
3326/* END_CASE */
3327
Gilles Peskine50e586b2018-06-08 14:28:46 +02003328/* BEGIN_CASE */
3329void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003330 data_t *key,
3331 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003332{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003333 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003334 psa_key_type_t key_type = key_type_arg;
3335 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003336 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003337 size_t iv_size = 16;
3338 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003339 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003340 size_t output1_size = 0;
3341 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003342 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003343 size_t output2_size = 0;
3344 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003345 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003346 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3347 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003349
Gilles Peskine8817f612018-12-18 00:18:46 +01003350 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003351
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3353 psa_set_key_algorithm( &attributes, alg );
3354 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003355
Gilles Peskine73676cb2019-05-15 20:15:10 +02003356 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003357
Gilles Peskine8817f612018-12-18 00:18:46 +01003358 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3359 handle, alg ) );
3360 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3361 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003362
Gilles Peskine8817f612018-12-18 00:18:46 +01003363 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3364 iv, iv_size,
3365 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003366 output1_size = ( (size_t) input->len +
3367 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003368 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003369
Gilles Peskine8817f612018-12-18 00:18:46 +01003370 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3371 output1, output1_size,
3372 &output1_length ) );
3373 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003374 output1 + output1_length,
3375 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003376 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003377
Gilles Peskine048b7f02018-06-08 14:20:49 +02003378 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003379
Gilles Peskine8817f612018-12-18 00:18:46 +01003380 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003381
3382 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003383 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003384
Gilles Peskine8817f612018-12-18 00:18:46 +01003385 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3386 iv, iv_length ) );
3387 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3388 output2, output2_size,
3389 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003390 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003391 PSA_ASSERT( psa_cipher_finish( &operation2,
3392 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003393 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003394 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003395
Gilles Peskine048b7f02018-06-08 14:20:49 +02003396 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003397
Gilles Peskine8817f612018-12-18 00:18:46 +01003398 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003399
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003400 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003401
3402exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003403 mbedtls_free( output1 );
3404 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003405 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003406 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003407}
3408/* END_CASE */
3409
3410/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003411void cipher_verify_output_multipart( int alg_arg,
3412 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003413 data_t *key,
3414 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003415 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003416{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003417 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003418 psa_key_type_t key_type = key_type_arg;
3419 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003420 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003421 unsigned char iv[16] = {0};
3422 size_t iv_size = 16;
3423 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003424 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003425 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003426 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003427 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003428 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003429 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003430 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003431 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3432 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003434
Gilles Peskine8817f612018-12-18 00:18:46 +01003435 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003436
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003437 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3438 psa_set_key_algorithm( &attributes, alg );
3439 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003440
Gilles Peskine73676cb2019-05-15 20:15:10 +02003441 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003442
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3444 handle, alg ) );
3445 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3446 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003447
Gilles Peskine8817f612018-12-18 00:18:46 +01003448 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3449 iv, iv_size,
3450 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003451 output1_buffer_size = ( (size_t) input->len +
3452 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003453 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003454
Gilles Peskinee0866522019-02-19 19:44:00 +01003455 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003456
Gilles Peskine8817f612018-12-18 00:18:46 +01003457 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3458 output1, output1_buffer_size,
3459 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003460 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003461
Gilles Peskine8817f612018-12-18 00:18:46 +01003462 PSA_ASSERT( psa_cipher_update( &operation1,
3463 input->x + first_part_size,
3464 input->len - first_part_size,
3465 output1, output1_buffer_size,
3466 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003467 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003468
Gilles Peskine8817f612018-12-18 00:18:46 +01003469 PSA_ASSERT( psa_cipher_finish( &operation1,
3470 output1 + output1_length,
3471 output1_buffer_size - output1_length,
3472 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003473 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003474
Gilles Peskine8817f612018-12-18 00:18:46 +01003475 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003476
Gilles Peskine048b7f02018-06-08 14:20:49 +02003477 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003478 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003479
Gilles Peskine8817f612018-12-18 00:18:46 +01003480 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3481 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003482
Gilles Peskine8817f612018-12-18 00:18:46 +01003483 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3484 output2, output2_buffer_size,
3485 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003486 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003487
Gilles Peskine8817f612018-12-18 00:18:46 +01003488 PSA_ASSERT( psa_cipher_update( &operation2,
3489 output1 + first_part_size,
3490 output1_length - first_part_size,
3491 output2, output2_buffer_size,
3492 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003493 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003494
Gilles Peskine8817f612018-12-18 00:18:46 +01003495 PSA_ASSERT( psa_cipher_finish( &operation2,
3496 output2 + output2_length,
3497 output2_buffer_size - output2_length,
3498 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003499 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003500
Gilles Peskine8817f612018-12-18 00:18:46 +01003501 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003502
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003503 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003504
3505exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003506 mbedtls_free( output1 );
3507 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003508 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003509 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003510}
3511/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003512
Gilles Peskine20035e32018-02-03 22:44:14 +01003513/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003514void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003515 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003516 data_t *nonce,
3517 data_t *additional_data,
3518 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003519 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003520{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003521 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003522 psa_key_type_t key_type = key_type_arg;
3523 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003524 unsigned char *output_data = NULL;
3525 size_t output_size = 0;
3526 size_t output_length = 0;
3527 unsigned char *output_data2 = NULL;
3528 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003529 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003530 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003531 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003532
Gilles Peskine4abf7412018-06-18 16:35:34 +02003533 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003534 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3535 * should be exact. */
3536 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3537 TEST_EQUAL( output_size,
3538 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003539 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003540
Gilles Peskine8817f612018-12-18 00:18:46 +01003541 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003542
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003543 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3544 psa_set_key_algorithm( &attributes, alg );
3545 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003546
Gilles Peskine049c7532019-05-15 20:22:09 +02003547 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3548 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003549
Gilles Peskinefe11b722018-12-18 00:24:04 +01003550 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3551 nonce->x, nonce->len,
3552 additional_data->x,
3553 additional_data->len,
3554 input_data->x, input_data->len,
3555 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003556 &output_length ),
3557 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003558
3559 if( PSA_SUCCESS == expected_result )
3560 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003561 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003562
Gilles Peskine003a4a92019-05-14 16:09:40 +02003563 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3564 * should be exact. */
3565 TEST_EQUAL( input_data->len,
3566 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3567
Gilles Peskinefe11b722018-12-18 00:24:04 +01003568 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3569 nonce->x, nonce->len,
3570 additional_data->x,
3571 additional_data->len,
3572 output_data, output_length,
3573 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003574 &output_length2 ),
3575 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003576
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003577 ASSERT_COMPARE( input_data->x, input_data->len,
3578 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003579 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003580
Gilles Peskinea1cac842018-06-11 19:33:02 +02003581exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003582 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003583 mbedtls_free( output_data );
3584 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003585 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003586}
3587/* END_CASE */
3588
3589/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003590void aead_encrypt( int key_type_arg, data_t *key_data,
3591 int alg_arg,
3592 data_t *nonce,
3593 data_t *additional_data,
3594 data_t *input_data,
3595 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003596{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003597 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003598 psa_key_type_t key_type = key_type_arg;
3599 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003600 unsigned char *output_data = NULL;
3601 size_t output_size = 0;
3602 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003603 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003605
Gilles Peskine4abf7412018-06-18 16:35:34 +02003606 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003607 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3608 * should be exact. */
3609 TEST_EQUAL( output_size,
3610 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003611 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003612
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003614
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003615 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3616 psa_set_key_algorithm( &attributes, alg );
3617 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003618
Gilles Peskine049c7532019-05-15 20:22:09 +02003619 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3620 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003621
Gilles Peskine8817f612018-12-18 00:18:46 +01003622 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3623 nonce->x, nonce->len,
3624 additional_data->x, additional_data->len,
3625 input_data->x, input_data->len,
3626 output_data, output_size,
3627 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003628
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003629 ASSERT_COMPARE( expected_result->x, expected_result->len,
3630 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003631
Gilles Peskinea1cac842018-06-11 19:33:02 +02003632exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003633 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003634 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003635 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003636}
3637/* END_CASE */
3638
3639/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003640void aead_decrypt( int key_type_arg, data_t *key_data,
3641 int alg_arg,
3642 data_t *nonce,
3643 data_t *additional_data,
3644 data_t *input_data,
3645 data_t *expected_data,
3646 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003647{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003648 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003649 psa_key_type_t key_type = key_type_arg;
3650 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003651 unsigned char *output_data = NULL;
3652 size_t output_size = 0;
3653 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003654 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003655 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003656 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003657
Gilles Peskine003a4a92019-05-14 16:09:40 +02003658 output_size = input_data->len - tag_length;
3659 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3660 * should be exact. */
3661 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3662 TEST_EQUAL( output_size,
3663 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003664 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003665
Gilles Peskine8817f612018-12-18 00:18:46 +01003666 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003667
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003668 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3669 psa_set_key_algorithm( &attributes, alg );
3670 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003671
Gilles Peskine049c7532019-05-15 20:22:09 +02003672 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3673 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003674
Gilles Peskinefe11b722018-12-18 00:24:04 +01003675 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3676 nonce->x, nonce->len,
3677 additional_data->x,
3678 additional_data->len,
3679 input_data->x, input_data->len,
3680 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003681 &output_length ),
3682 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003683
Gilles Peskine2d277862018-06-18 15:41:12 +02003684 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003685 ASSERT_COMPARE( expected_data->x, expected_data->len,
3686 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003687
Gilles Peskinea1cac842018-06-11 19:33:02 +02003688exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003689 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003690 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003691 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003692}
3693/* END_CASE */
3694
3695/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003696void signature_size( int type_arg,
3697 int bits,
3698 int alg_arg,
3699 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003700{
3701 psa_key_type_t type = type_arg;
3702 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003703 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003704 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003705exit:
3706 ;
3707}
3708/* END_CASE */
3709
3710/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003711void sign_deterministic( int key_type_arg, data_t *key_data,
3712 int alg_arg, data_t *input_data,
3713 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003714{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003715 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003716 psa_key_type_t key_type = key_type_arg;
3717 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003718 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003719 unsigned char *signature = NULL;
3720 size_t signature_size;
3721 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003723
Gilles Peskine8817f612018-12-18 00:18:46 +01003724 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003725
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003726 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3727 psa_set_key_algorithm( &attributes, alg );
3728 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003729
Gilles Peskine049c7532019-05-15 20:22:09 +02003730 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3731 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003732 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3733 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003734
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003735 /* Allocate a buffer which has the size advertized by the
3736 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003737 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3738 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003739 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003740 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003741 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003742
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003743 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003744 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3745 input_data->x, input_data->len,
3746 signature, signature_size,
3747 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003748 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003749 ASSERT_COMPARE( output_data->x, output_data->len,
3750 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003751
3752exit:
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 Peskine0189e752018-02-03 23:57:22 +01003755 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003756 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003757}
3758/* END_CASE */
3759
3760/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003761void sign_fail( int key_type_arg, data_t *key_data,
3762 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003763 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003764{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003765 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003766 psa_key_type_t key_type = key_type_arg;
3767 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003768 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003769 psa_status_t actual_status;
3770 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003771 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003772 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003774
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003775 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003776
Gilles Peskine8817f612018-12-18 00:18:46 +01003777 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003778
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003779 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3780 psa_set_key_algorithm( &attributes, alg );
3781 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003782
Gilles Peskine049c7532019-05-15 20:22:09 +02003783 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3784 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003785
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003786 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003787 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003788 signature, signature_size,
3789 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003790 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003791 /* The value of *signature_length is unspecified on error, but
3792 * whatever it is, it should be less than signature_size, so that
3793 * if the caller tries to read *signature_length bytes without
3794 * checking the error code then they don't overflow a buffer. */
3795 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003796
3797exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003798 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003799 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003800 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003801 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003802}
3803/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003804
3805/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003806void sign_verify( int key_type_arg, data_t *key_data,
3807 int alg_arg, data_t *input_data )
3808{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003809 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003810 psa_key_type_t key_type = key_type_arg;
3811 psa_algorithm_t alg = alg_arg;
3812 size_t key_bits;
3813 unsigned char *signature = NULL;
3814 size_t signature_size;
3815 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003817
Gilles Peskine8817f612018-12-18 00:18:46 +01003818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003819
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003820 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3821 psa_set_key_algorithm( &attributes, alg );
3822 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003823
Gilles Peskine049c7532019-05-15 20:22:09 +02003824 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3825 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003826 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3827 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003828
3829 /* Allocate a buffer which has the size advertized by the
3830 * library. */
3831 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3832 key_bits, alg );
3833 TEST_ASSERT( signature_size != 0 );
3834 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003835 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003836
3837 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003838 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3839 input_data->x, input_data->len,
3840 signature, signature_size,
3841 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003842 /* Check that the signature length looks sensible. */
3843 TEST_ASSERT( signature_length <= signature_size );
3844 TEST_ASSERT( signature_length > 0 );
3845
3846 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003847 PSA_ASSERT( psa_asymmetric_verify(
3848 handle, alg,
3849 input_data->x, input_data->len,
3850 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003851
3852 if( input_data->len != 0 )
3853 {
3854 /* Flip a bit in the input and verify that the signature is now
3855 * detected as invalid. Flip a bit at the beginning, not at the end,
3856 * because ECDSA may ignore the last few bits of the input. */
3857 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003858 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3859 input_data->x, input_data->len,
3860 signature, signature_length ),
3861 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003862 }
3863
3864exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003865 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003866 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003867 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003868 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003869}
3870/* END_CASE */
3871
3872/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003873void asymmetric_verify( int key_type_arg, data_t *key_data,
3874 int alg_arg, data_t *hash_data,
3875 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003876{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003877 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003878 psa_key_type_t key_type = key_type_arg;
3879 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003881
Gilles Peskine69c12672018-06-28 00:07:19 +02003882 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3883
Gilles Peskine8817f612018-12-18 00:18:46 +01003884 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003885
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003886 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3887 psa_set_key_algorithm( &attributes, alg );
3888 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003889
Gilles Peskine049c7532019-05-15 20:22:09 +02003890 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3891 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003892
Gilles Peskine8817f612018-12-18 00:18:46 +01003893 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3894 hash_data->x, hash_data->len,
3895 signature_data->x,
3896 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003897exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003898 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003899 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003900 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003901}
3902/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003903
3904/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003905void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3906 int alg_arg, data_t *hash_data,
3907 data_t *signature_data,
3908 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003909{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003910 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003911 psa_key_type_t key_type = key_type_arg;
3912 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003913 psa_status_t actual_status;
3914 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003916
Gilles Peskine8817f612018-12-18 00:18:46 +01003917 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003919 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3920 psa_set_key_algorithm( &attributes, alg );
3921 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003922
Gilles Peskine049c7532019-05-15 20:22:09 +02003923 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3924 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003925
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003926 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003927 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003928 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003929 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003930
Gilles Peskinefe11b722018-12-18 00:24:04 +01003931 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932
3933exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003934 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003935 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003936 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003937}
3938/* END_CASE */
3939
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003941void asymmetric_encrypt( int key_type_arg,
3942 data_t *key_data,
3943 int alg_arg,
3944 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003945 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003946 int expected_output_length_arg,
3947 int expected_status_arg )
3948{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003949 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003950 psa_key_type_t key_type = key_type_arg;
3951 psa_algorithm_t alg = alg_arg;
3952 size_t expected_output_length = expected_output_length_arg;
3953 size_t key_bits;
3954 unsigned char *output = NULL;
3955 size_t output_size;
3956 size_t output_length = ~0;
3957 psa_status_t actual_status;
3958 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003960
Gilles Peskine8817f612018-12-18 00:18:46 +01003961 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003962
Gilles Peskine656896e2018-06-29 19:12:28 +02003963 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003964 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3965 psa_set_key_algorithm( &attributes, alg );
3966 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003967 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3968 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003969
3970 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003971 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3972 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003973 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003974 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003975
3976 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003977 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003978 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003979 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003980 output, output_size,
3981 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003982 TEST_EQUAL( actual_status, expected_status );
3983 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003984
Gilles Peskine68428122018-06-30 18:42:41 +02003985 /* If the label is empty, the test framework puts a non-null pointer
3986 * in label->x. Test that a null pointer works as well. */
3987 if( label->len == 0 )
3988 {
3989 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003990 if( output_size != 0 )
3991 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003992 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003993 input_data->x, input_data->len,
3994 NULL, label->len,
3995 output, output_size,
3996 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003997 TEST_EQUAL( actual_status, expected_status );
3998 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003999 }
4000
Gilles Peskine656896e2018-06-29 19:12:28 +02004001exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004002 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004003 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004004 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004005 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004006}
4007/* END_CASE */
4008
4009/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004010void asymmetric_encrypt_decrypt( int key_type_arg,
4011 data_t *key_data,
4012 int alg_arg,
4013 data_t *input_data,
4014 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004016 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004017 psa_key_type_t key_type = key_type_arg;
4018 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004019 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004020 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004021 size_t output_size;
4022 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004023 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004024 size_t output2_size;
4025 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027
Gilles Peskine8817f612018-12-18 00:18:46 +01004028 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004029
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004030 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4031 psa_set_key_algorithm( &attributes, alg );
4032 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004033
Gilles Peskine049c7532019-05-15 20:22:09 +02004034 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4035 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004036
4037 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004038 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4039 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004040 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004041 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004042 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004043 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004044
Gilles Peskineeebd7382018-06-08 18:11:54 +02004045 /* We test encryption by checking that encrypt-then-decrypt gives back
4046 * the original plaintext because of the non-optional random
4047 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004048 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4049 input_data->x, input_data->len,
4050 label->x, label->len,
4051 output, output_size,
4052 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004053 /* We don't know what ciphertext length to expect, but check that
4054 * it looks sensible. */
4055 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004056
Gilles Peskine8817f612018-12-18 00:18:46 +01004057 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4058 output, output_length,
4059 label->x, label->len,
4060 output2, output2_size,
4061 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004062 ASSERT_COMPARE( input_data->x, input_data->len,
4063 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004064
4065exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004066 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004067 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004068 mbedtls_free( output );
4069 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004070 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071}
4072/* END_CASE */
4073
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004074/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004075void asymmetric_decrypt( int key_type_arg,
4076 data_t *key_data,
4077 int alg_arg,
4078 data_t *input_data,
4079 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004080 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004081{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004082 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083 psa_key_type_t key_type = key_type_arg;
4084 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004085 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004086 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004087 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004089
Jaeden Amero412654a2019-02-06 12:57:46 +00004090 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004091 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004092
Gilles Peskine8817f612018-12-18 00:18:46 +01004093 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004094
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004095 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4096 psa_set_key_algorithm( &attributes, alg );
4097 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004098
Gilles Peskine049c7532019-05-15 20:22:09 +02004099 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4100 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004101
Gilles Peskine8817f612018-12-18 00:18:46 +01004102 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4103 input_data->x, input_data->len,
4104 label->x, label->len,
4105 output,
4106 output_size,
4107 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004108 ASSERT_COMPARE( expected_data->x, expected_data->len,
4109 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004110
Gilles Peskine68428122018-06-30 18:42:41 +02004111 /* If the label is empty, the test framework puts a non-null pointer
4112 * in label->x. Test that a null pointer works as well. */
4113 if( label->len == 0 )
4114 {
4115 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004116 if( output_size != 0 )
4117 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004118 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4119 input_data->x, input_data->len,
4120 NULL, label->len,
4121 output,
4122 output_size,
4123 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004124 ASSERT_COMPARE( expected_data->x, expected_data->len,
4125 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004126 }
4127
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004128exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004129 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004130 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004131 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004132 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004133}
4134/* END_CASE */
4135
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004136/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004137void asymmetric_decrypt_fail( int key_type_arg,
4138 data_t *key_data,
4139 int alg_arg,
4140 data_t *input_data,
4141 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004142 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004143 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004144{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004145 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004146 psa_key_type_t key_type = key_type_arg;
4147 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004148 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004149 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004150 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004151 psa_status_t actual_status;
4152 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004154
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004155 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004156
Gilles Peskine8817f612018-12-18 00:18:46 +01004157 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004158
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004159 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4160 psa_set_key_algorithm( &attributes, alg );
4161 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004162
Gilles Peskine049c7532019-05-15 20:22:09 +02004163 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4164 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004165
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004166 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004167 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004168 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004169 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004170 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004171 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004172 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004173
Gilles Peskine68428122018-06-30 18:42:41 +02004174 /* If the label is empty, the test framework puts a non-null pointer
4175 * in label->x. Test that a null pointer works as well. */
4176 if( label->len == 0 )
4177 {
4178 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004179 if( output_size != 0 )
4180 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004181 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004182 input_data->x, input_data->len,
4183 NULL, label->len,
4184 output, output_size,
4185 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004186 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004187 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004188 }
4189
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004190exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004191 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004192 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004193 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004194 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004195}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004196/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004197
4198/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004199void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004200{
4201 /* Test each valid way of initializing the object, except for `= {0}`, as
4202 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4203 * though it's OK by the C standard. We could test for this, but we'd need
4204 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004205 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004206 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4207 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4208 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004209
4210 memset( &zero, 0, sizeof( zero ) );
4211
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004212 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004213 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004214 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004215 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004216 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004217 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004218 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004219
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004220 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004221 PSA_ASSERT( psa_key_derivation_abort(&func) );
4222 PSA_ASSERT( psa_key_derivation_abort(&init) );
4223 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004224}
4225/* END_CASE */
4226
Janos Follath16de4a42019-06-13 16:32:24 +01004227/* BEGIN_CASE */
4228void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004229{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004230 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004231 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004232 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004233
Gilles Peskine8817f612018-12-18 00:18:46 +01004234 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004235
Janos Follath16de4a42019-06-13 16:32:24 +01004236 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004237 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004238
4239exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004240 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004241 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004242}
4243/* END_CASE */
4244
Janos Follathaf3c2a02019-06-12 12:34:34 +01004245/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004246void derive_set_capacity( int alg_arg, int capacity_arg,
4247 int expected_status_arg )
4248{
4249 psa_algorithm_t alg = alg_arg;
4250 size_t capacity = capacity_arg;
4251 psa_status_t expected_status = expected_status_arg;
4252 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4253
4254 PSA_ASSERT( psa_crypto_init( ) );
4255
4256 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4257
4258 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4259 expected_status );
4260
4261exit:
4262 psa_key_derivation_abort( &operation );
4263 PSA_DONE( );
4264}
4265/* END_CASE */
4266
4267/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004268void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004269 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004270 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004271 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004272 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004273 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004274 int expected_status_arg3,
4275 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004276{
4277 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004278 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4279 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004280 psa_status_t expected_statuses[] = {expected_status_arg1,
4281 expected_status_arg2,
4282 expected_status_arg3};
4283 data_t *inputs[] = {input1, input2, input3};
4284 psa_key_handle_t handles[] = {0, 0, 0};
4285 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4287 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004288 psa_key_type_t output_key_type = output_key_type_arg;
4289 psa_key_handle_t output_handle = 0;
4290 psa_status_t expected_output_status = expected_output_status_arg;
4291 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004292
4293 PSA_ASSERT( psa_crypto_init( ) );
4294
4295 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4296 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004297
4298 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4299
4300 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4301 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004302 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004303 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004304 psa_set_key_type( &attributes, key_types[i] );
4305 PSA_ASSERT( psa_import_key( &attributes,
4306 inputs[i]->x, inputs[i]->len,
4307 &handles[i] ) );
4308 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4309 handles[i] ),
4310 expected_statuses[i] );
4311 }
4312 else
4313 {
4314 TEST_EQUAL( psa_key_derivation_input_bytes(
4315 &operation, steps[i],
4316 inputs[i]->x, inputs[i]->len ),
4317 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004318 }
4319 }
4320
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004321 if( output_key_type != PSA_KEY_TYPE_NONE )
4322 {
4323 psa_reset_key_attributes( &attributes );
4324 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4325 psa_set_key_bits( &attributes, 8 );
4326 actual_output_status =
4327 psa_key_derivation_output_key( &attributes, &operation,
4328 &output_handle );
4329 }
4330 else
4331 {
4332 uint8_t buffer[1];
4333 actual_output_status =
4334 psa_key_derivation_output_bytes( &operation,
4335 buffer, sizeof( buffer ) );
4336 }
4337 TEST_EQUAL( actual_output_status, expected_output_status );
4338
Janos Follathaf3c2a02019-06-12 12:34:34 +01004339exit:
4340 psa_key_derivation_abort( &operation );
4341 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4342 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004343 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004344 PSA_DONE( );
4345}
4346/* END_CASE */
4347
Janos Follathd958bb72019-07-03 15:02:16 +01004348/* BEGIN_CASE */
4349void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004350{
Janos Follathd958bb72019-07-03 15:02:16 +01004351 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004352 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004353 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004354 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004355 unsigned char input1[] = "Input 1";
4356 size_t input1_length = sizeof( input1 );
4357 unsigned char input2[] = "Input 2";
4358 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004359 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004360 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004361 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4362 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4363 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004365
Gilles Peskine8817f612018-12-18 00:18:46 +01004366 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004367
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004368 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4369 psa_set_key_algorithm( &attributes, alg );
4370 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004371
Gilles Peskine73676cb2019-05-15 20:15:10 +02004372 PSA_ASSERT( psa_import_key( &attributes,
4373 key_data, sizeof( key_data ),
4374 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004375
4376 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004377 if( !setup_key_derivation_wrap( &operation, handle, alg,
4378 input1, input1_length,
4379 input2, input2_length,
4380 capacity ) )
4381 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004382
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004383 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004384 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004385 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004386
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004387 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004388
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004389 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004390 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004391
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004392exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004393 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004394 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004395 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004396}
4397/* END_CASE */
4398
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004399/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004400void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004401{
4402 uint8_t output_buffer[16];
4403 size_t buffer_size = 16;
4404 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004405 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004406
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004407 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4408 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004409 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004410
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004411 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004412 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004413
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004415
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004416 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4417 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004418 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004419
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004421 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004422
4423exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004425}
4426/* END_CASE */
4427
4428/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004429void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004430 int step1_arg, data_t *input1,
4431 int step2_arg, data_t *input2,
4432 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004433 int requested_capacity_arg,
4434 data_t *expected_output1,
4435 data_t *expected_output2 )
4436{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004437 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004438 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4439 data_t *inputs[] = {input1, input2, input3};
4440 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004441 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004443 uint8_t *expected_outputs[2] =
4444 {expected_output1->x, expected_output2->x};
4445 size_t output_sizes[2] =
4446 {expected_output1->len, expected_output2->len};
4447 size_t output_buffer_size = 0;
4448 uint8_t *output_buffer = NULL;
4449 size_t expected_capacity;
4450 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004452 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004453 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004454
4455 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4456 {
4457 if( output_sizes[i] > output_buffer_size )
4458 output_buffer_size = output_sizes[i];
4459 if( output_sizes[i] == 0 )
4460 expected_outputs[i] = NULL;
4461 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004462 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004463 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004464
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004465 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4466 psa_set_key_algorithm( &attributes, alg );
4467 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004468
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004469 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004470 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4471 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4472 requested_capacity ) );
4473 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004474 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004475 switch( steps[i] )
4476 {
4477 case 0:
4478 break;
4479 case PSA_KEY_DERIVATION_INPUT_SECRET:
4480 PSA_ASSERT( psa_import_key( &attributes,
4481 inputs[i]->x, inputs[i]->len,
4482 &handles[i] ) );
4483 PSA_ASSERT( psa_key_derivation_input_key(
4484 &operation, steps[i],
4485 handles[i] ) );
4486 break;
4487 default:
4488 PSA_ASSERT( psa_key_derivation_input_bytes(
4489 &operation, steps[i],
4490 inputs[i]->x, inputs[i]->len ) );
4491 break;
4492 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004493 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004496 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004497 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004498 expected_capacity = requested_capacity;
4499
4500 /* Expansion phase. */
4501 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4502 {
4503 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004504 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004505 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004506 if( expected_capacity == 0 && output_sizes[i] == 0 )
4507 {
4508 /* Reading 0 bytes when 0 bytes are available can go either way. */
4509 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004510 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004511 continue;
4512 }
4513 else if( expected_capacity == 0 ||
4514 output_sizes[i] > expected_capacity )
4515 {
4516 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004517 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004518 expected_capacity = 0;
4519 continue;
4520 }
4521 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004522 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004523 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004524 ASSERT_COMPARE( output_buffer, output_sizes[i],
4525 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004526 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004527 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004528 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004529 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004530 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004531 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004533
4534exit:
4535 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004536 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004537 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4538 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004539 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004540}
4541/* END_CASE */
4542
4543/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004544void derive_full( int alg_arg,
4545 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004546 data_t *input1,
4547 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004548 int requested_capacity_arg )
4549{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004550 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004551 psa_algorithm_t alg = alg_arg;
4552 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004553 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004554 unsigned char output_buffer[16];
4555 size_t expected_capacity = requested_capacity;
4556 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004558
Gilles Peskine8817f612018-12-18 00:18:46 +01004559 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004560
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004561 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4562 psa_set_key_algorithm( &attributes, alg );
4563 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004564
Gilles Peskine049c7532019-05-15 20:22:09 +02004565 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4566 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004567
Janos Follathf2815ea2019-07-03 12:41:36 +01004568 if( !setup_key_derivation_wrap( &operation, handle, alg,
4569 input1->x, input1->len,
4570 input2->x, input2->len,
4571 requested_capacity ) )
4572 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004573
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004574 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004575 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004576 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004577
4578 /* Expansion phase. */
4579 while( current_capacity > 0 )
4580 {
4581 size_t read_size = sizeof( output_buffer );
4582 if( read_size > current_capacity )
4583 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004584 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004585 output_buffer,
4586 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004587 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004588 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004589 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004590 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004591 }
4592
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004593 /* Check that the operation refuses to go over capacity. */
4594 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004595 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004596
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004597 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004598
4599exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004600 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004601 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004602 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004603}
4604/* END_CASE */
4605
Janos Follathe60c9052019-07-03 13:51:30 +01004606/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004607void derive_key_exercise( int alg_arg,
4608 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004609 data_t *input1,
4610 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004611 int derived_type_arg,
4612 int derived_bits_arg,
4613 int derived_usage_arg,
4614 int derived_alg_arg )
4615{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004616 psa_key_handle_t base_handle = 0;
4617 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004618 psa_algorithm_t alg = alg_arg;
4619 psa_key_type_t derived_type = derived_type_arg;
4620 size_t derived_bits = derived_bits_arg;
4621 psa_key_usage_t derived_usage = derived_usage_arg;
4622 psa_algorithm_t derived_alg = derived_alg_arg;
4623 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004624 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004626 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004627
Gilles Peskine8817f612018-12-18 00:18:46 +01004628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004629
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004630 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4631 psa_set_key_algorithm( &attributes, alg );
4632 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004633 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4634 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004635
4636 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004637 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4638 input1->x, input1->len,
4639 input2->x, input2->len, capacity ) )
4640 goto exit;
4641
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004642 psa_set_key_usage_flags( &attributes, derived_usage );
4643 psa_set_key_algorithm( &attributes, derived_alg );
4644 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004645 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004646 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004647 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004648
4649 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004650 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4651 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4652 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004653
4654 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004655 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004656 goto exit;
4657
4658exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004660 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004661 psa_destroy_key( base_handle );
4662 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004663 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004664}
4665/* END_CASE */
4666
Janos Follath42fd8882019-07-03 14:17:09 +01004667/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004668void derive_key_export( int alg_arg,
4669 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004670 data_t *input1,
4671 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004672 int bytes1_arg,
4673 int bytes2_arg )
4674{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004675 psa_key_handle_t base_handle = 0;
4676 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004677 psa_algorithm_t alg = alg_arg;
4678 size_t bytes1 = bytes1_arg;
4679 size_t bytes2 = bytes2_arg;
4680 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004682 uint8_t *output_buffer = NULL;
4683 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004684 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4685 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004686 size_t length;
4687
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004688 ASSERT_ALLOC( output_buffer, capacity );
4689 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004690 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004691
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004692 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4693 psa_set_key_algorithm( &base_attributes, alg );
4694 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004695 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4696 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004697
4698 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01004699 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4700 input1->x, input1->len,
4701 input2->x, input2->len, capacity ) )
4702 goto exit;
4703
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004704 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004705 output_buffer,
4706 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004707 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004708
4709 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01004710 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4711 input1->x, input1->len,
4712 input2->x, input2->len, capacity ) )
4713 goto exit;
4714
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004715 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4716 psa_set_key_algorithm( &derived_attributes, 0 );
4717 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004718 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004719 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004720 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004721 PSA_ASSERT( psa_export_key( derived_handle,
4722 export_buffer, bytes1,
4723 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004724 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004725 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004726 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004727 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004728 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004729 PSA_ASSERT( psa_export_key( derived_handle,
4730 export_buffer + bytes1, bytes2,
4731 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004732 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004733
4734 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004735 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4736 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004737
4738exit:
4739 mbedtls_free( output_buffer );
4740 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004742 psa_destroy_key( base_handle );
4743 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004744 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004745}
4746/* END_CASE */
4747
4748/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004749void derive_key( int alg_arg,
4750 data_t *key_data, data_t *input1, data_t *input2,
4751 int type_arg, int bits_arg,
4752 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02004753{
4754 psa_key_handle_t base_handle = 0;
4755 psa_key_handle_t derived_handle = 0;
4756 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004757 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004758 size_t bits = bits_arg;
4759 psa_status_t expected_status = expected_status_arg;
4760 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4761 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4762 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4763
4764 PSA_ASSERT( psa_crypto_init( ) );
4765
4766 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4767 psa_set_key_algorithm( &base_attributes, alg );
4768 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4769 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4770 &base_handle ) );
4771
4772 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4773 input1->x, input1->len,
4774 input2->x, input2->len, SIZE_MAX ) )
4775 goto exit;
4776
4777 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4778 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004779 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004780 psa_set_key_bits( &derived_attributes, bits );
4781 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
4782 &derived_handle ),
4783 expected_status );
4784
4785exit:
4786 psa_key_derivation_abort( &operation );
4787 psa_destroy_key( base_handle );
4788 psa_destroy_key( derived_handle );
4789 PSA_DONE( );
4790}
4791/* END_CASE */
4792
4793/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004794void key_agreement_setup( int alg_arg,
4795 int our_key_type_arg, data_t *our_key_data,
4796 data_t *peer_key_data,
4797 int expected_status_arg )
4798{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004799 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004800 psa_algorithm_t alg = alg_arg;
4801 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004803 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004804 psa_status_t expected_status = expected_status_arg;
4805 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004806
Gilles Peskine8817f612018-12-18 00:18:46 +01004807 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004808
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004809 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4810 psa_set_key_algorithm( &attributes, alg );
4811 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004812 PSA_ASSERT( psa_import_key( &attributes,
4813 our_key_data->x, our_key_data->len,
4814 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004815
Gilles Peskine77f40d82019-04-11 21:27:06 +02004816 /* The tests currently include inputs that should fail at either step.
4817 * Test cases that fail at the setup step should be changed to call
4818 * key_derivation_setup instead, and this function should be renamed
4819 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004821 if( status == PSA_SUCCESS )
4822 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004823 TEST_EQUAL( psa_key_derivation_key_agreement(
4824 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4825 our_key,
4826 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004827 expected_status );
4828 }
4829 else
4830 {
4831 TEST_ASSERT( status == expected_status );
4832 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004833
4834exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004836 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004837 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004838}
4839/* END_CASE */
4840
4841/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004842void raw_key_agreement( int alg_arg,
4843 int our_key_type_arg, data_t *our_key_data,
4844 data_t *peer_key_data,
4845 data_t *expected_output )
4846{
4847 psa_key_handle_t our_key = 0;
4848 psa_algorithm_t alg = alg_arg;
4849 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004851 unsigned char *output = NULL;
4852 size_t output_length = ~0;
4853
4854 ASSERT_ALLOC( output, expected_output->len );
4855 PSA_ASSERT( psa_crypto_init( ) );
4856
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004857 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4858 psa_set_key_algorithm( &attributes, alg );
4859 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004860 PSA_ASSERT( psa_import_key( &attributes,
4861 our_key_data->x, our_key_data->len,
4862 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004863
Gilles Peskinebe697d82019-05-16 18:00:41 +02004864 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4865 peer_key_data->x, peer_key_data->len,
4866 output, expected_output->len,
4867 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004868 ASSERT_COMPARE( output, output_length,
4869 expected_output->x, expected_output->len );
4870
4871exit:
4872 mbedtls_free( output );
4873 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004874 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004875}
4876/* END_CASE */
4877
4878/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004879void key_agreement_capacity( int alg_arg,
4880 int our_key_type_arg, data_t *our_key_data,
4881 data_t *peer_key_data,
4882 int expected_capacity_arg )
4883{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004884 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004885 psa_algorithm_t alg = alg_arg;
4886 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004887 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004888 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004889 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004890 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004891
Gilles Peskine8817f612018-12-18 00:18:46 +01004892 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004893
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004894 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4895 psa_set_key_algorithm( &attributes, alg );
4896 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004897 PSA_ASSERT( psa_import_key( &attributes,
4898 our_key_data->x, our_key_data->len,
4899 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004900
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004902 PSA_ASSERT( psa_key_derivation_key_agreement(
4903 &operation,
4904 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4905 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004906 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4907 {
4908 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004909 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004910 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004911 NULL, 0 ) );
4912 }
Gilles Peskine59685592018-09-18 12:11:34 +02004913
Gilles Peskinebf491972018-10-25 22:36:12 +02004914 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004915 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004917 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004918
Gilles Peskinebf491972018-10-25 22:36:12 +02004919 /* Test the actual capacity by reading the output. */
4920 while( actual_capacity > sizeof( output ) )
4921 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004922 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004923 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004924 actual_capacity -= sizeof( output );
4925 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004926 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004927 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004929 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004930
Gilles Peskine59685592018-09-18 12:11:34 +02004931exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004933 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004934 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004935}
4936/* END_CASE */
4937
4938/* BEGIN_CASE */
4939void key_agreement_output( int alg_arg,
4940 int our_key_type_arg, data_t *our_key_data,
4941 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004942 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004943{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004944 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004945 psa_algorithm_t alg = alg_arg;
4946 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004949 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004950
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004951 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4952 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004953
Gilles Peskine8817f612018-12-18 00:18:46 +01004954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004955
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004956 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4957 psa_set_key_algorithm( &attributes, alg );
4958 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004959 PSA_ASSERT( psa_import_key( &attributes,
4960 our_key_data->x, our_key_data->len,
4961 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004962
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004964 PSA_ASSERT( psa_key_derivation_key_agreement(
4965 &operation,
4966 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4967 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004968 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4969 {
4970 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004972 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004973 NULL, 0 ) );
4974 }
Gilles Peskine59685592018-09-18 12:11:34 +02004975
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004977 actual_output,
4978 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004979 ASSERT_COMPARE( actual_output, expected_output1->len,
4980 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004981 if( expected_output2->len != 0 )
4982 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004983 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004984 actual_output,
4985 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004986 ASSERT_COMPARE( actual_output, expected_output2->len,
4987 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004988 }
Gilles Peskine59685592018-09-18 12:11:34 +02004989
4990exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004991 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004992 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004993 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004994 mbedtls_free( actual_output );
4995}
4996/* END_CASE */
4997
4998/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004999void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005000{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005001 size_t bytes = bytes_arg;
5002 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005003 unsigned char *output = NULL;
5004 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005005 size_t i;
5006 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005007
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005008 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5009 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005010 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005011
Gilles Peskine8817f612018-12-18 00:18:46 +01005012 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005013
Gilles Peskinea50d7392018-06-21 10:22:13 +02005014 /* Run several times, to ensure that every output byte will be
5015 * nonzero at least once with overwhelming probability
5016 * (2^(-8*number_of_runs)). */
5017 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005018 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005019 if( bytes != 0 )
5020 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005021 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005022
5023 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005024 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5025 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005026
5027 for( i = 0; i < bytes; i++ )
5028 {
5029 if( output[i] != 0 )
5030 ++changed[i];
5031 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005032 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005033
5034 /* Check that every byte was changed to nonzero at least once. This
5035 * validates that psa_generate_random is overwriting every byte of
5036 * the output buffer. */
5037 for( i = 0; i < bytes; i++ )
5038 {
5039 TEST_ASSERT( changed[i] != 0 );
5040 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005041
5042exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005043 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005044 mbedtls_free( output );
5045 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005046}
5047/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005048
5049/* BEGIN_CASE */
5050void generate_key( int type_arg,
5051 int bits_arg,
5052 int usage_arg,
5053 int alg_arg,
5054 int expected_status_arg )
5055{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005056 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005057 psa_key_type_t type = type_arg;
5058 psa_key_usage_t usage = usage_arg;
5059 size_t bits = bits_arg;
5060 psa_algorithm_t alg = alg_arg;
5061 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005063 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005064
Gilles Peskine8817f612018-12-18 00:18:46 +01005065 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005066
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005067 psa_set_key_usage_flags( &attributes, usage );
5068 psa_set_key_algorithm( &attributes, alg );
5069 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005070 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005071
5072 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005073 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005074 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005075 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005076
5077 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005078 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5079 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5080 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005081
Gilles Peskine818ca122018-06-20 18:16:48 +02005082 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005083 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005084 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005085
5086exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005087 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005088 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005089 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005090}
5091/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005092
Gilles Peskinee56e8782019-04-26 17:34:02 +02005093/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5094void generate_key_rsa( int bits_arg,
5095 data_t *e_arg,
5096 int expected_status_arg )
5097{
5098 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005099 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005100 size_t bits = bits_arg;
5101 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5102 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5103 psa_status_t expected_status = expected_status_arg;
5104 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5105 uint8_t *exported = NULL;
5106 size_t exported_size =
5107 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5108 size_t exported_length = SIZE_MAX;
5109 uint8_t *e_read_buffer = NULL;
5110 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005111 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005112 size_t e_read_length = SIZE_MAX;
5113
5114 if( e_arg->len == 0 ||
5115 ( e_arg->len == 3 &&
5116 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5117 {
5118 is_default_public_exponent = 1;
5119 e_read_size = 0;
5120 }
5121 ASSERT_ALLOC( e_read_buffer, e_read_size );
5122 ASSERT_ALLOC( exported, exported_size );
5123
5124 PSA_ASSERT( psa_crypto_init( ) );
5125
5126 psa_set_key_usage_flags( &attributes, usage );
5127 psa_set_key_algorithm( &attributes, alg );
5128 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5129 e_arg->x, e_arg->len ) );
5130 psa_set_key_bits( &attributes, bits );
5131
5132 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005133 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005134 if( expected_status != PSA_SUCCESS )
5135 goto exit;
5136
5137 /* Test the key information */
5138 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5139 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5140 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5141 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5142 e_read_buffer, e_read_size,
5143 &e_read_length ) );
5144 if( is_default_public_exponent )
5145 TEST_EQUAL( e_read_length, 0 );
5146 else
5147 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5148
5149 /* Do something with the key according to its type and permitted usage. */
5150 if( ! exercise_key( handle, usage, alg ) )
5151 goto exit;
5152
5153 /* Export the key and check the public exponent. */
5154 PSA_ASSERT( psa_export_public_key( handle,
5155 exported, exported_size,
5156 &exported_length ) );
5157 {
5158 uint8_t *p = exported;
5159 uint8_t *end = exported + exported_length;
5160 size_t len;
5161 /* RSAPublicKey ::= SEQUENCE {
5162 * modulus INTEGER, -- n
5163 * publicExponent INTEGER } -- e
5164 */
5165 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005166 MBEDTLS_ASN1_SEQUENCE |
5167 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005168 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5169 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5170 MBEDTLS_ASN1_INTEGER ) );
5171 if( len >= 1 && p[0] == 0 )
5172 {
5173 ++p;
5174 --len;
5175 }
5176 if( e_arg->len == 0 )
5177 {
5178 TEST_EQUAL( len, 3 );
5179 TEST_EQUAL( p[0], 1 );
5180 TEST_EQUAL( p[1], 0 );
5181 TEST_EQUAL( p[2], 1 );
5182 }
5183 else
5184 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5185 }
5186
5187exit:
5188 psa_reset_key_attributes( &attributes );
5189 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005190 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005191 mbedtls_free( e_read_buffer );
5192 mbedtls_free( exported );
5193}
5194/* END_CASE */
5195
Darryl Greend49a4992018-06-18 17:27:26 +01005196/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005197void persistent_key_load_key_from_storage( data_t *data,
5198 int type_arg, int bits_arg,
5199 int usage_flags_arg, int alg_arg,
5200 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005201{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005202 psa_key_id_t key_id = 1;
5203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005204 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005205 psa_key_handle_t base_key = 0;
5206 psa_key_type_t type = type_arg;
5207 size_t bits = bits_arg;
5208 psa_key_usage_t usage_flags = usage_flags_arg;
5209 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005211 unsigned char *first_export = NULL;
5212 unsigned char *second_export = NULL;
5213 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5214 size_t first_exported_length;
5215 size_t second_exported_length;
5216
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005217 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5218 {
5219 ASSERT_ALLOC( first_export, export_size );
5220 ASSERT_ALLOC( second_export, export_size );
5221 }
Darryl Greend49a4992018-06-18 17:27:26 +01005222
Gilles Peskine8817f612018-12-18 00:18:46 +01005223 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005224
Gilles Peskinec87af662019-05-15 16:12:22 +02005225 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005226 psa_set_key_usage_flags( &attributes, usage_flags );
5227 psa_set_key_algorithm( &attributes, alg );
5228 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005229 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005230
Darryl Green0c6575a2018-11-07 16:05:30 +00005231 switch( generation_method )
5232 {
5233 case IMPORT_KEY:
5234 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005235 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5236 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005237 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005238
Darryl Green0c6575a2018-11-07 16:05:30 +00005239 case GENERATE_KEY:
5240 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005241 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005242 break;
5243
5244 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005245 {
5246 /* Create base key */
5247 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5248 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5249 psa_set_key_usage_flags( &base_attributes,
5250 PSA_KEY_USAGE_DERIVE );
5251 psa_set_key_algorithm( &base_attributes, derive_alg );
5252 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005253 PSA_ASSERT( psa_import_key( &base_attributes,
5254 data->x, data->len,
5255 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005256 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005258 PSA_ASSERT( psa_key_derivation_input_key(
5259 &operation,
5260 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005261 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005262 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005263 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005264 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5265 &operation,
5266 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005268 PSA_ASSERT( psa_destroy_key( base_key ) );
5269 base_key = 0;
5270 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005271 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005272 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005273 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005274
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005275 /* Export the key if permitted by the key policy. */
5276 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5277 {
5278 PSA_ASSERT( psa_export_key( handle,
5279 first_export, export_size,
5280 &first_exported_length ) );
5281 if( generation_method == IMPORT_KEY )
5282 ASSERT_COMPARE( data->x, data->len,
5283 first_export, first_exported_length );
5284 }
Darryl Greend49a4992018-06-18 17:27:26 +01005285
5286 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005287 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005288 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005289 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005290
Darryl Greend49a4992018-06-18 17:27:26 +01005291 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005292 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005293 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5294 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5295 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5296 PSA_KEY_LIFETIME_PERSISTENT );
5297 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5298 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5299 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5300 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005301
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005302 /* Export the key again if permitted by the key policy. */
5303 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005304 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005305 PSA_ASSERT( psa_export_key( handle,
5306 second_export, export_size,
5307 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005308 ASSERT_COMPARE( first_export, first_exported_length,
5309 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005310 }
5311
5312 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005313 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005314 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005315
5316exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005317 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005318 mbedtls_free( first_export );
5319 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005321 psa_destroy_key( base_key );
5322 if( handle == 0 )
5323 {
5324 /* In case there was a test failure after creating the persistent key
5325 * but while it was not open, try to re-open the persistent key
5326 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005327 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005328 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005329 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005330 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005331}
5332/* END_CASE */