blob: d62d3c1986985845afc9b76838427c90b714a8fc [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 Peskine89d8c5c2019-11-26 17:01:59 +0100225 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200226 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
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100294 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200295 {
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
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100305 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 {
307 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100308 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200309 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 Peskine89d8c5c2019-11-26 17:01:59 +0100448 unsigned char signature[PSA_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
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100464 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200465 {
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 Peskine89d8c5c2019-11-26 17:01:59 +0100471 PSA_ASSERT( psa_sign_hash( handle, alg,
472 payload, payload_length,
473 signature, sizeof( signature ),
474 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200475 }
476
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100477 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200478 {
479 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100480 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200481 PSA_SUCCESS :
482 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100483 TEST_EQUAL( psa_verify_hash( handle, alg,
484 payload, payload_length,
485 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100486 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 ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001064 PSA_KEY_USAGE_VERIFY_HASH :
1065 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001066 }
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 Peskine1a960492019-11-26 17:12:21 +01001638void check_key_policy( int type_arg, int bits_arg,
1639 int usage_arg, int alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001640{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001641 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001642 psa_key_type_t key_type = type_arg;
1643 size_t bits = bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001644 psa_algorithm_t alg = alg_arg;
1645 psa_key_usage_t usage = usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001647
Gilles Peskine8817f612018-12-18 00:18:46 +01001648 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001649
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001650 psa_set_key_usage_flags( &attributes, usage );
1651 psa_set_key_algorithm( &attributes, alg );
1652 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001653 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001654
Gilles Peskine1a960492019-11-26 17:12:21 +01001655 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1656 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001657
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001658 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1659 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001660 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001661 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1662 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001663
1664exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001665 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001666 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001667 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001668}
1669/* END_CASE */
1670
1671/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001672void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001673{
1674 /* Test each valid way of initializing the object, except for `= {0}`, as
1675 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1676 * though it's OK by the C standard. We could test for this, but we'd need
1677 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001678 psa_key_attributes_t func = psa_key_attributes_init( );
1679 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1680 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001681
1682 memset( &zero, 0, sizeof( zero ) );
1683
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001684 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1685 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1686 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001687
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001688 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1689 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1690 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1691
1692 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1693 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1694 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1695
1696 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1697 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1698 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1699
1700 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1701 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1702 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001703}
1704/* END_CASE */
1705
1706/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001707void mac_key_policy( int policy_usage,
1708 int policy_alg,
1709 int key_type,
1710 data_t *key_data,
1711 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001712{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001713 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001714 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001715 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001716 psa_status_t status;
1717 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001718
Gilles Peskine8817f612018-12-18 00:18:46 +01001719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001720
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001721 psa_set_key_usage_flags( &attributes, policy_usage );
1722 psa_set_key_algorithm( &attributes, policy_alg );
1723 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001724
Gilles Peskine049c7532019-05-15 20:22:09 +02001725 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1726 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001727
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001728 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001729 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001730 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001731 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001732 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001733 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001734 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001735
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001736 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001737 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001738 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001739 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001740 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001741 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001742 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001743
1744exit:
1745 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001746 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001747 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001748}
1749/* END_CASE */
1750
1751/* BEGIN_CASE */
1752void cipher_key_policy( int policy_usage,
1753 int policy_alg,
1754 int key_type,
1755 data_t *key_data,
1756 int exercise_alg )
1757{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001758 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001760 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001761 psa_status_t status;
1762
Gilles Peskine8817f612018-12-18 00:18:46 +01001763 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001764
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001765 psa_set_key_usage_flags( &attributes, policy_usage );
1766 psa_set_key_algorithm( &attributes, policy_alg );
1767 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001768
Gilles Peskine049c7532019-05-15 20:22:09 +02001769 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1770 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001771
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001772 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001773 if( policy_alg == exercise_alg &&
1774 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001775 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001776 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001777 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001778 psa_cipher_abort( &operation );
1779
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001780 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001781 if( policy_alg == exercise_alg &&
1782 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001783 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001784 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001785 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001786
1787exit:
1788 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001789 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001790 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001791}
1792/* END_CASE */
1793
1794/* BEGIN_CASE */
1795void aead_key_policy( int policy_usage,
1796 int policy_alg,
1797 int key_type,
1798 data_t *key_data,
1799 int nonce_length_arg,
1800 int tag_length_arg,
1801 int exercise_alg )
1802{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001803 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001805 psa_status_t status;
1806 unsigned char nonce[16] = {0};
1807 size_t nonce_length = nonce_length_arg;
1808 unsigned char tag[16];
1809 size_t tag_length = tag_length_arg;
1810 size_t output_length;
1811
1812 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1813 TEST_ASSERT( tag_length <= sizeof( tag ) );
1814
Gilles Peskine8817f612018-12-18 00:18:46 +01001815 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001816
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001817 psa_set_key_usage_flags( &attributes, policy_usage );
1818 psa_set_key_algorithm( &attributes, policy_alg );
1819 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001820
Gilles Peskine049c7532019-05-15 20:22:09 +02001821 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1822 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001823
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001824 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001825 nonce, nonce_length,
1826 NULL, 0,
1827 NULL, 0,
1828 tag, tag_length,
1829 &output_length );
1830 if( policy_alg == exercise_alg &&
1831 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001832 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001834 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001835
1836 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001837 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001838 nonce, nonce_length,
1839 NULL, 0,
1840 tag, tag_length,
1841 NULL, 0,
1842 &output_length );
1843 if( policy_alg == exercise_alg &&
1844 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001845 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001846 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001847 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001848
1849exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001850 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001851 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001852}
1853/* END_CASE */
1854
1855/* BEGIN_CASE */
1856void asymmetric_encryption_key_policy( int policy_usage,
1857 int policy_alg,
1858 int key_type,
1859 data_t *key_data,
1860 int exercise_alg )
1861{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001862 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001863 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001864 psa_status_t status;
1865 size_t key_bits;
1866 size_t buffer_length;
1867 unsigned char *buffer = NULL;
1868 size_t output_length;
1869
Gilles Peskine8817f612018-12-18 00:18:46 +01001870 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001871
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001872 psa_set_key_usage_flags( &attributes, policy_usage );
1873 psa_set_key_algorithm( &attributes, policy_alg );
1874 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001875
Gilles Peskine049c7532019-05-15 20:22:09 +02001876 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1877 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001878
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001879 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1880 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001881 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1882 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001883 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001884
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001885 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001886 NULL, 0,
1887 NULL, 0,
1888 buffer, buffer_length,
1889 &output_length );
1890 if( policy_alg == exercise_alg &&
1891 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001892 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001893 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001894 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001895
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001896 if( buffer_length != 0 )
1897 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001898 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001899 buffer, buffer_length,
1900 NULL, 0,
1901 buffer, buffer_length,
1902 &output_length );
1903 if( policy_alg == exercise_alg &&
1904 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001905 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001906 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001907 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001908
1909exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001910 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001911 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001912 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001913 mbedtls_free( buffer );
1914}
1915/* END_CASE */
1916
1917/* BEGIN_CASE */
1918void asymmetric_signature_key_policy( int policy_usage,
1919 int policy_alg,
1920 int key_type,
1921 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001922 int exercise_alg,
1923 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001925 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001927 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001928 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1929 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1930 * compatible with the policy and `payload_length_arg` is supposed to be
1931 * a valid input length to sign. If `payload_length_arg <= 0`,
1932 * `exercise_alg` is supposed to be forbidden by the policy. */
1933 int compatible_alg = payload_length_arg > 0;
1934 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001935 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001936 size_t signature_length;
1937
Gilles Peskine8817f612018-12-18 00:18:46 +01001938 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001939
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001940 psa_set_key_usage_flags( &attributes, policy_usage );
1941 psa_set_key_algorithm( &attributes, policy_alg );
1942 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001943
Gilles Peskine049c7532019-05-15 20:22:09 +02001944 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1945 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001947 status = psa_sign_hash( handle, exercise_alg,
1948 payload, payload_length,
1949 signature, sizeof( signature ),
1950 &signature_length );
1951 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001952 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001954 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955
1956 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001957 status = psa_verify_hash( handle, exercise_alg,
1958 payload, payload_length,
1959 signature, sizeof( signature ) );
1960 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001961 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001962 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001963 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001964
1965exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001966 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001967 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001968}
1969/* END_CASE */
1970
Janos Follathba3fab92019-06-11 14:50:16 +01001971/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001972void derive_key_policy( int policy_usage,
1973 int policy_alg,
1974 int key_type,
1975 data_t *key_data,
1976 int exercise_alg )
1977{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001978 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001980 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001981 psa_status_t status;
1982
Gilles Peskine8817f612018-12-18 00:18:46 +01001983 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001984
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001985 psa_set_key_usage_flags( &attributes, policy_usage );
1986 psa_set_key_algorithm( &attributes, policy_alg );
1987 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001988
Gilles Peskine049c7532019-05-15 20:22:09 +02001989 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1990 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001991
Janos Follathba3fab92019-06-11 14:50:16 +01001992 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1993
1994 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1995 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001996 {
Janos Follathba3fab92019-06-11 14:50:16 +01001997 PSA_ASSERT( psa_key_derivation_input_bytes(
1998 &operation,
1999 PSA_KEY_DERIVATION_INPUT_SEED,
2000 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002001 }
Janos Follathba3fab92019-06-11 14:50:16 +01002002
2003 status = psa_key_derivation_input_key( &operation,
2004 PSA_KEY_DERIVATION_INPUT_SECRET,
2005 handle );
2006
Gilles Peskineea0fb492018-07-12 17:17:20 +02002007 if( policy_alg == exercise_alg &&
2008 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002009 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002010 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002011 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002012
2013exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002014 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002015 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002016 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002017}
2018/* END_CASE */
2019
2020/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002021void agreement_key_policy( int policy_usage,
2022 int policy_alg,
2023 int key_type_arg,
2024 data_t *key_data,
2025 int exercise_alg )
2026{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002027 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002029 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002030 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002031 psa_status_t status;
2032
Gilles Peskine8817f612018-12-18 00:18:46 +01002033 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002034
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002035 psa_set_key_usage_flags( &attributes, policy_usage );
2036 psa_set_key_algorithm( &attributes, policy_alg );
2037 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002038
Gilles Peskine049c7532019-05-15 20:22:09 +02002039 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2040 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002041
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002042 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2043 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002044
Gilles Peskine01d718c2018-09-18 12:01:02 +02002045 if( policy_alg == exercise_alg &&
2046 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002047 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002048 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002049 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002050
2051exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002052 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002053 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002054 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002055}
2056/* END_CASE */
2057
2058/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002059void key_policy_alg2( int key_type_arg, data_t *key_data,
2060 int usage_arg, int alg_arg, int alg2_arg )
2061{
2062 psa_key_handle_t handle = 0;
2063 psa_key_type_t key_type = key_type_arg;
2064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2065 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2066 psa_key_usage_t usage = usage_arg;
2067 psa_algorithm_t alg = alg_arg;
2068 psa_algorithm_t alg2 = alg2_arg;
2069
2070 PSA_ASSERT( psa_crypto_init( ) );
2071
2072 psa_set_key_usage_flags( &attributes, usage );
2073 psa_set_key_algorithm( &attributes, alg );
2074 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2075 psa_set_key_type( &attributes, key_type );
2076 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2077 &handle ) );
2078
2079 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2080 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2081 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2082 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2083
2084 if( ! exercise_key( handle, usage, alg ) )
2085 goto exit;
2086 if( ! exercise_key( handle, usage, alg2 ) )
2087 goto exit;
2088
2089exit:
2090 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002091 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002092}
2093/* END_CASE */
2094
2095/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002096void raw_agreement_key_policy( int policy_usage,
2097 int policy_alg,
2098 int key_type_arg,
2099 data_t *key_data,
2100 int exercise_alg )
2101{
2102 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002103 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002104 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002105 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002106 psa_status_t status;
2107
2108 PSA_ASSERT( psa_crypto_init( ) );
2109
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002110 psa_set_key_usage_flags( &attributes, policy_usage );
2111 psa_set_key_algorithm( &attributes, policy_alg );
2112 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002113
Gilles Peskine049c7532019-05-15 20:22:09 +02002114 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2115 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002116
2117 status = raw_key_agreement_with_self( exercise_alg, handle );
2118
2119 if( policy_alg == exercise_alg &&
2120 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2121 PSA_ASSERT( status );
2122 else
2123 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2124
2125exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002126 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002127 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002128 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002129}
2130/* END_CASE */
2131
2132/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002133void copy_success( int source_usage_arg,
2134 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002135 int type_arg, data_t *material,
2136 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002137 int target_usage_arg,
2138 int target_alg_arg, int target_alg2_arg,
2139 int expected_usage_arg,
2140 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002141{
Gilles Peskineca25db92019-04-19 11:43:08 +02002142 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2143 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002144 psa_key_usage_t expected_usage = expected_usage_arg;
2145 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002146 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002147 psa_key_handle_t source_handle = 0;
2148 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002149 uint8_t *export_buffer = NULL;
2150
Gilles Peskine57ab7212019-01-28 13:03:09 +01002151 PSA_ASSERT( psa_crypto_init( ) );
2152
Gilles Peskineca25db92019-04-19 11:43:08 +02002153 /* Prepare the source key. */
2154 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2155 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002156 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002157 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002158 PSA_ASSERT( psa_import_key( &source_attributes,
2159 material->x, material->len,
2160 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002161 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002162
Gilles Peskineca25db92019-04-19 11:43:08 +02002163 /* Prepare the target attributes. */
2164 if( copy_attributes )
2165 target_attributes = source_attributes;
2166 if( target_usage_arg != -1 )
2167 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2168 if( target_alg_arg != -1 )
2169 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002170 if( target_alg2_arg != -1 )
2171 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002172
2173 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002174 PSA_ASSERT( psa_copy_key( source_handle,
2175 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002176
2177 /* Destroy the source to ensure that this doesn't affect the target. */
2178 PSA_ASSERT( psa_destroy_key( source_handle ) );
2179
2180 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002181 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2182 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2183 psa_get_key_type( &target_attributes ) );
2184 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2185 psa_get_key_bits( &target_attributes ) );
2186 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2187 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002188 TEST_EQUAL( expected_alg2,
2189 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002190 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2191 {
2192 size_t length;
2193 ASSERT_ALLOC( export_buffer, material->len );
2194 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2195 material->len, &length ) );
2196 ASSERT_COMPARE( material->x, material->len,
2197 export_buffer, length );
2198 }
2199 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2200 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002201 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2202 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002203
2204 PSA_ASSERT( psa_close_key( target_handle ) );
2205
2206exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002207 psa_reset_key_attributes( &source_attributes );
2208 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002209 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002210 mbedtls_free( export_buffer );
2211}
2212/* END_CASE */
2213
2214/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002215void copy_fail( int source_usage_arg,
2216 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002217 int type_arg, data_t *material,
2218 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002219 int target_usage_arg,
2220 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002221 int expected_status_arg )
2222{
2223 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2224 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2225 psa_key_handle_t source_handle = 0;
2226 psa_key_handle_t target_handle = 0;
2227
2228 PSA_ASSERT( psa_crypto_init( ) );
2229
2230 /* Prepare the source key. */
2231 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2232 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002233 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002234 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002235 PSA_ASSERT( psa_import_key( &source_attributes,
2236 material->x, material->len,
2237 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002238
2239 /* Prepare the target attributes. */
2240 psa_set_key_type( &target_attributes, target_type_arg );
2241 psa_set_key_bits( &target_attributes, target_bits_arg );
2242 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2243 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002244 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002245
2246 /* Try to copy the key. */
2247 TEST_EQUAL( psa_copy_key( source_handle,
2248 &target_attributes, &target_handle ),
2249 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002250
2251 PSA_ASSERT( psa_destroy_key( source_handle ) );
2252
Gilles Peskine4a644642019-05-03 17:14:08 +02002253exit:
2254 psa_reset_key_attributes( &source_attributes );
2255 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002256 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002257}
2258/* END_CASE */
2259
2260/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002261void hash_operation_init( )
2262{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002263 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002264 /* Test each valid way of initializing the object, except for `= {0}`, as
2265 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2266 * though it's OK by the C standard. We could test for this, but we'd need
2267 * to supress the Clang warning for the test. */
2268 psa_hash_operation_t func = psa_hash_operation_init( );
2269 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2270 psa_hash_operation_t zero;
2271
2272 memset( &zero, 0, sizeof( zero ) );
2273
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002274 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002275 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2276 PSA_ERROR_BAD_STATE );
2277 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2278 PSA_ERROR_BAD_STATE );
2279 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2280 PSA_ERROR_BAD_STATE );
2281
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002282 /* A default hash operation should be abortable without error. */
2283 PSA_ASSERT( psa_hash_abort( &func ) );
2284 PSA_ASSERT( psa_hash_abort( &init ) );
2285 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002286}
2287/* END_CASE */
2288
2289/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002290void hash_setup( int alg_arg,
2291 int expected_status_arg )
2292{
2293 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002294 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002295 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002296 psa_status_t status;
2297
Gilles Peskine8817f612018-12-18 00:18:46 +01002298 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002299
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002300 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002301 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002302
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002303 /* Whether setup succeeded or failed, abort must succeed. */
2304 PSA_ASSERT( psa_hash_abort( &operation ) );
2305
2306 /* If setup failed, reproduce the failure, so as to
2307 * test the resulting state of the operation object. */
2308 if( status != PSA_SUCCESS )
2309 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2310
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002311 /* Now the operation object should be reusable. */
2312#if defined(KNOWN_SUPPORTED_HASH_ALG)
2313 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2314 PSA_ASSERT( psa_hash_abort( &operation ) );
2315#endif
2316
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002317exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002318 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002319}
2320/* END_CASE */
2321
2322/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002323void hash_bad_order( )
2324{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002325 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002326 unsigned char input[] = "";
2327 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002328 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002329 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2330 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2331 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002332 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002333 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002334 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002335
Gilles Peskine8817f612018-12-18 00:18:46 +01002336 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002337
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002338 /* Call setup twice in a row. */
2339 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2340 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2341 PSA_ERROR_BAD_STATE );
2342 PSA_ASSERT( psa_hash_abort( &operation ) );
2343
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002344 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002345 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002346 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002347 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002348
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002349 /* Call update after finish. */
2350 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2351 PSA_ASSERT( psa_hash_finish( &operation,
2352 hash, sizeof( hash ), &hash_len ) );
2353 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002354 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002355 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002356
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002357 /* Call verify without calling setup beforehand. */
2358 TEST_EQUAL( psa_hash_verify( &operation,
2359 valid_hash, sizeof( valid_hash ) ),
2360 PSA_ERROR_BAD_STATE );
2361 PSA_ASSERT( psa_hash_abort( &operation ) );
2362
2363 /* Call verify after finish. */
2364 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2365 PSA_ASSERT( psa_hash_finish( &operation,
2366 hash, sizeof( hash ), &hash_len ) );
2367 TEST_EQUAL( psa_hash_verify( &operation,
2368 valid_hash, sizeof( valid_hash ) ),
2369 PSA_ERROR_BAD_STATE );
2370 PSA_ASSERT( psa_hash_abort( &operation ) );
2371
2372 /* Call verify twice in a row. */
2373 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2374 PSA_ASSERT( psa_hash_verify( &operation,
2375 valid_hash, sizeof( valid_hash ) ) );
2376 TEST_EQUAL( psa_hash_verify( &operation,
2377 valid_hash, sizeof( valid_hash ) ),
2378 PSA_ERROR_BAD_STATE );
2379 PSA_ASSERT( psa_hash_abort( &operation ) );
2380
2381 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002382 TEST_EQUAL( psa_hash_finish( &operation,
2383 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002384 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002385 PSA_ASSERT( psa_hash_abort( &operation ) );
2386
2387 /* Call finish twice in a row. */
2388 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2389 PSA_ASSERT( psa_hash_finish( &operation,
2390 hash, sizeof( hash ), &hash_len ) );
2391 TEST_EQUAL( psa_hash_finish( &operation,
2392 hash, sizeof( hash ), &hash_len ),
2393 PSA_ERROR_BAD_STATE );
2394 PSA_ASSERT( psa_hash_abort( &operation ) );
2395
2396 /* Call finish after calling verify. */
2397 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2398 PSA_ASSERT( psa_hash_verify( &operation,
2399 valid_hash, sizeof( valid_hash ) ) );
2400 TEST_EQUAL( psa_hash_finish( &operation,
2401 hash, sizeof( hash ), &hash_len ),
2402 PSA_ERROR_BAD_STATE );
2403 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002404
2405exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002406 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002407}
2408/* END_CASE */
2409
itayzafrir27e69452018-11-01 14:26:34 +02002410/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2411void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002412{
2413 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002414 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2415 * appended to it */
2416 unsigned char hash[] = {
2417 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2418 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2419 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002420 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002421 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002422
Gilles Peskine8817f612018-12-18 00:18:46 +01002423 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002424
itayzafrir27e69452018-11-01 14:26:34 +02002425 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002426 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002427 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002428 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002429
itayzafrir27e69452018-11-01 14:26:34 +02002430 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002431 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002432 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002433 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002434
itayzafrir27e69452018-11-01 14:26:34 +02002435 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002436 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002437 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002438 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002439
itayzafrirec93d302018-10-18 18:01:10 +03002440exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002441 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002442}
2443/* END_CASE */
2444
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002445/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2446void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002447{
2448 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002449 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002450 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002451 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002452 size_t hash_len;
2453
Gilles Peskine8817f612018-12-18 00:18:46 +01002454 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002455
itayzafrir58028322018-10-25 10:22:01 +03002456 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002457 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002458 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002459 hash, expected_size - 1, &hash_len ),
2460 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002461
2462exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002463 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002464}
2465/* END_CASE */
2466
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002467/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2468void hash_clone_source_state( )
2469{
2470 psa_algorithm_t alg = PSA_ALG_SHA_256;
2471 unsigned char hash[PSA_HASH_MAX_SIZE];
2472 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2473 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2474 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2475 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2476 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2477 size_t hash_len;
2478
2479 PSA_ASSERT( psa_crypto_init( ) );
2480 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2481
2482 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2483 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2484 PSA_ASSERT( psa_hash_finish( &op_finished,
2485 hash, sizeof( hash ), &hash_len ) );
2486 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2487 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2488
2489 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2490 PSA_ERROR_BAD_STATE );
2491
2492 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2493 PSA_ASSERT( psa_hash_finish( &op_init,
2494 hash, sizeof( hash ), &hash_len ) );
2495 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2496 PSA_ASSERT( psa_hash_finish( &op_finished,
2497 hash, sizeof( hash ), &hash_len ) );
2498 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2499 PSA_ASSERT( psa_hash_finish( &op_aborted,
2500 hash, sizeof( hash ), &hash_len ) );
2501
2502exit:
2503 psa_hash_abort( &op_source );
2504 psa_hash_abort( &op_init );
2505 psa_hash_abort( &op_setup );
2506 psa_hash_abort( &op_finished );
2507 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002508 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002509}
2510/* END_CASE */
2511
2512/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2513void hash_clone_target_state( )
2514{
2515 psa_algorithm_t alg = PSA_ALG_SHA_256;
2516 unsigned char hash[PSA_HASH_MAX_SIZE];
2517 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2518 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2519 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2520 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2521 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2522 size_t hash_len;
2523
2524 PSA_ASSERT( psa_crypto_init( ) );
2525
2526 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2527 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2528 PSA_ASSERT( psa_hash_finish( &op_finished,
2529 hash, sizeof( hash ), &hash_len ) );
2530 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2531 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2532
2533 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2534 PSA_ASSERT( psa_hash_finish( &op_target,
2535 hash, sizeof( hash ), &hash_len ) );
2536
2537 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2538 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2539 PSA_ERROR_BAD_STATE );
2540 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2541 PSA_ERROR_BAD_STATE );
2542
2543exit:
2544 psa_hash_abort( &op_target );
2545 psa_hash_abort( &op_init );
2546 psa_hash_abort( &op_setup );
2547 psa_hash_abort( &op_finished );
2548 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002549 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002550}
2551/* END_CASE */
2552
itayzafrir58028322018-10-25 10:22:01 +03002553/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002554void mac_operation_init( )
2555{
Jaeden Amero252ef282019-02-15 14:05:35 +00002556 const uint8_t input[1] = { 0 };
2557
Jaeden Amero769ce272019-01-04 11:48:03 +00002558 /* Test each valid way of initializing the object, except for `= {0}`, as
2559 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2560 * though it's OK by the C standard. We could test for this, but we'd need
2561 * to supress the Clang warning for the test. */
2562 psa_mac_operation_t func = psa_mac_operation_init( );
2563 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2564 psa_mac_operation_t zero;
2565
2566 memset( &zero, 0, sizeof( zero ) );
2567
Jaeden Amero252ef282019-02-15 14:05:35 +00002568 /* A freshly-initialized MAC operation should not be usable. */
2569 TEST_EQUAL( psa_mac_update( &func,
2570 input, sizeof( input ) ),
2571 PSA_ERROR_BAD_STATE );
2572 TEST_EQUAL( psa_mac_update( &init,
2573 input, sizeof( input ) ),
2574 PSA_ERROR_BAD_STATE );
2575 TEST_EQUAL( psa_mac_update( &zero,
2576 input, sizeof( input ) ),
2577 PSA_ERROR_BAD_STATE );
2578
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002579 /* A default MAC operation should be abortable without error. */
2580 PSA_ASSERT( psa_mac_abort( &func ) );
2581 PSA_ASSERT( psa_mac_abort( &init ) );
2582 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002583}
2584/* END_CASE */
2585
2586/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002587void mac_setup( int key_type_arg,
2588 data_t *key,
2589 int alg_arg,
2590 int expected_status_arg )
2591{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002592 psa_key_type_t key_type = key_type_arg;
2593 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002594 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002595 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002596 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2597#if defined(KNOWN_SUPPORTED_MAC_ALG)
2598 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2599#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002600
Gilles Peskine8817f612018-12-18 00:18:46 +01002601 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002602
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002603 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2604 &operation, &status ) )
2605 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002606 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002607
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002608 /* The operation object should be reusable. */
2609#if defined(KNOWN_SUPPORTED_MAC_ALG)
2610 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2611 smoke_test_key_data,
2612 sizeof( smoke_test_key_data ),
2613 KNOWN_SUPPORTED_MAC_ALG,
2614 &operation, &status ) )
2615 goto exit;
2616 TEST_EQUAL( status, PSA_SUCCESS );
2617#endif
2618
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002620 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002621}
2622/* END_CASE */
2623
2624/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002625void mac_bad_order( )
2626{
2627 psa_key_handle_t handle = 0;
2628 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2629 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2630 const uint8_t key[] = {
2631 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2632 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2633 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002635 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2636 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2637 size_t sign_mac_length = 0;
2638 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2639 const uint8_t verify_mac[] = {
2640 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2641 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2642 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2643
2644 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002645 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002646 psa_set_key_algorithm( &attributes, alg );
2647 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002648
Gilles Peskine73676cb2019-05-15 20:15:10 +02002649 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002650
Jaeden Amero252ef282019-02-15 14:05:35 +00002651 /* Call update without calling setup beforehand. */
2652 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2653 PSA_ERROR_BAD_STATE );
2654 PSA_ASSERT( psa_mac_abort( &operation ) );
2655
2656 /* Call sign finish without calling setup beforehand. */
2657 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2658 &sign_mac_length),
2659 PSA_ERROR_BAD_STATE );
2660 PSA_ASSERT( psa_mac_abort( &operation ) );
2661
2662 /* Call verify finish without calling setup beforehand. */
2663 TEST_EQUAL( psa_mac_verify_finish( &operation,
2664 verify_mac, sizeof( verify_mac ) ),
2665 PSA_ERROR_BAD_STATE );
2666 PSA_ASSERT( psa_mac_abort( &operation ) );
2667
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002668 /* Call setup twice in a row. */
2669 PSA_ASSERT( psa_mac_sign_setup( &operation,
2670 handle, alg ) );
2671 TEST_EQUAL( psa_mac_sign_setup( &operation,
2672 handle, alg ),
2673 PSA_ERROR_BAD_STATE );
2674 PSA_ASSERT( psa_mac_abort( &operation ) );
2675
Jaeden Amero252ef282019-02-15 14:05:35 +00002676 /* Call update after sign finish. */
2677 PSA_ASSERT( psa_mac_sign_setup( &operation,
2678 handle, alg ) );
2679 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2680 PSA_ASSERT( psa_mac_sign_finish( &operation,
2681 sign_mac, sizeof( sign_mac ),
2682 &sign_mac_length ) );
2683 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2684 PSA_ERROR_BAD_STATE );
2685 PSA_ASSERT( psa_mac_abort( &operation ) );
2686
2687 /* Call update after verify finish. */
2688 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002689 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002690 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2691 PSA_ASSERT( psa_mac_verify_finish( &operation,
2692 verify_mac, sizeof( verify_mac ) ) );
2693 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2694 PSA_ERROR_BAD_STATE );
2695 PSA_ASSERT( psa_mac_abort( &operation ) );
2696
2697 /* Call sign finish twice in a row. */
2698 PSA_ASSERT( psa_mac_sign_setup( &operation,
2699 handle, alg ) );
2700 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2701 PSA_ASSERT( psa_mac_sign_finish( &operation,
2702 sign_mac, sizeof( sign_mac ),
2703 &sign_mac_length ) );
2704 TEST_EQUAL( psa_mac_sign_finish( &operation,
2705 sign_mac, sizeof( sign_mac ),
2706 &sign_mac_length ),
2707 PSA_ERROR_BAD_STATE );
2708 PSA_ASSERT( psa_mac_abort( &operation ) );
2709
2710 /* Call verify finish twice in a row. */
2711 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002712 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002713 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2714 PSA_ASSERT( psa_mac_verify_finish( &operation,
2715 verify_mac, sizeof( verify_mac ) ) );
2716 TEST_EQUAL( psa_mac_verify_finish( &operation,
2717 verify_mac, sizeof( verify_mac ) ),
2718 PSA_ERROR_BAD_STATE );
2719 PSA_ASSERT( psa_mac_abort( &operation ) );
2720
2721 /* Setup sign but try verify. */
2722 PSA_ASSERT( psa_mac_sign_setup( &operation,
2723 handle, alg ) );
2724 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2725 TEST_EQUAL( psa_mac_verify_finish( &operation,
2726 verify_mac, sizeof( verify_mac ) ),
2727 PSA_ERROR_BAD_STATE );
2728 PSA_ASSERT( psa_mac_abort( &operation ) );
2729
2730 /* Setup verify but try sign. */
2731 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002732 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002733 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2734 TEST_EQUAL( psa_mac_sign_finish( &operation,
2735 sign_mac, sizeof( sign_mac ),
2736 &sign_mac_length ),
2737 PSA_ERROR_BAD_STATE );
2738 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002739
Gilles Peskine76b29a72019-05-28 14:08:50 +02002740 PSA_ASSERT( psa_destroy_key( handle ) );
2741
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002742exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002743 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002744}
2745/* END_CASE */
2746
2747/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002748void mac_sign( int key_type_arg,
2749 data_t *key,
2750 int alg_arg,
2751 data_t *input,
2752 data_t *expected_mac )
2753{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002754 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002755 psa_key_type_t key_type = key_type_arg;
2756 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002757 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002759 /* Leave a little extra room in the output buffer. At the end of the
2760 * test, we'll check that the implementation didn't overwrite onto
2761 * this extra room. */
2762 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2763 size_t mac_buffer_size =
2764 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2765 size_t mac_length = 0;
2766
2767 memset( actual_mac, '+', sizeof( actual_mac ) );
2768 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2769 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2770
Gilles Peskine8817f612018-12-18 00:18:46 +01002771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002772
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002773 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002774 psa_set_key_algorithm( &attributes, alg );
2775 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002776
Gilles Peskine73676cb2019-05-15 20:15:10 +02002777 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002778
2779 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 PSA_ASSERT( psa_mac_sign_setup( &operation,
2781 handle, alg ) );
2782 PSA_ASSERT( psa_mac_update( &operation,
2783 input->x, input->len ) );
2784 PSA_ASSERT( psa_mac_sign_finish( &operation,
2785 actual_mac, mac_buffer_size,
2786 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002787
2788 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002789 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2790 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002791
2792 /* Verify that the end of the buffer is untouched. */
2793 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2794 sizeof( actual_mac ) - mac_length ) );
2795
2796exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002797 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002798 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002799}
2800/* END_CASE */
2801
2802/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002803void mac_verify( int key_type_arg,
2804 data_t *key,
2805 int alg_arg,
2806 data_t *input,
2807 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002808{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002809 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002810 psa_key_type_t key_type = key_type_arg;
2811 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002812 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002813 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002814
Gilles Peskine69c12672018-06-28 00:07:19 +02002815 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2816
Gilles Peskine8817f612018-12-18 00:18:46 +01002817 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002818
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002819 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002820 psa_set_key_algorithm( &attributes, alg );
2821 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002822
Gilles Peskine73676cb2019-05-15 20:15:10 +02002823 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002824
Gilles Peskine8817f612018-12-18 00:18:46 +01002825 PSA_ASSERT( psa_mac_verify_setup( &operation,
2826 handle, alg ) );
2827 PSA_ASSERT( psa_destroy_key( handle ) );
2828 PSA_ASSERT( psa_mac_update( &operation,
2829 input->x, input->len ) );
2830 PSA_ASSERT( psa_mac_verify_finish( &operation,
2831 expected_mac->x,
2832 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002833
2834exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002835 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002836 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002837}
2838/* END_CASE */
2839
2840/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002841void cipher_operation_init( )
2842{
Jaeden Ameroab439972019-02-15 14:12:05 +00002843 const uint8_t input[1] = { 0 };
2844 unsigned char output[1] = { 0 };
2845 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002846 /* Test each valid way of initializing the object, except for `= {0}`, as
2847 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2848 * though it's OK by the C standard. We could test for this, but we'd need
2849 * to supress the Clang warning for the test. */
2850 psa_cipher_operation_t func = psa_cipher_operation_init( );
2851 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2852 psa_cipher_operation_t zero;
2853
2854 memset( &zero, 0, sizeof( zero ) );
2855
Jaeden Ameroab439972019-02-15 14:12:05 +00002856 /* A freshly-initialized cipher operation should not be usable. */
2857 TEST_EQUAL( psa_cipher_update( &func,
2858 input, sizeof( input ),
2859 output, sizeof( output ),
2860 &output_length ),
2861 PSA_ERROR_BAD_STATE );
2862 TEST_EQUAL( psa_cipher_update( &init,
2863 input, sizeof( input ),
2864 output, sizeof( output ),
2865 &output_length ),
2866 PSA_ERROR_BAD_STATE );
2867 TEST_EQUAL( psa_cipher_update( &zero,
2868 input, sizeof( input ),
2869 output, sizeof( output ),
2870 &output_length ),
2871 PSA_ERROR_BAD_STATE );
2872
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002873 /* A default cipher operation should be abortable without error. */
2874 PSA_ASSERT( psa_cipher_abort( &func ) );
2875 PSA_ASSERT( psa_cipher_abort( &init ) );
2876 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002877}
2878/* END_CASE */
2879
2880/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002881void cipher_setup( int key_type_arg,
2882 data_t *key,
2883 int alg_arg,
2884 int expected_status_arg )
2885{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002886 psa_key_type_t key_type = key_type_arg;
2887 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002888 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002889 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002890 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002891#if defined(KNOWN_SUPPORTED_MAC_ALG)
2892 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2893#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002894
Gilles Peskine8817f612018-12-18 00:18:46 +01002895 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002896
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002897 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2898 &operation, &status ) )
2899 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002900 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002901
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002902 /* The operation object should be reusable. */
2903#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2904 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2905 smoke_test_key_data,
2906 sizeof( smoke_test_key_data ),
2907 KNOWN_SUPPORTED_CIPHER_ALG,
2908 &operation, &status ) )
2909 goto exit;
2910 TEST_EQUAL( status, PSA_SUCCESS );
2911#endif
2912
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002913exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002914 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002915}
2916/* END_CASE */
2917
2918/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002919void cipher_bad_order( )
2920{
2921 psa_key_handle_t handle = 0;
2922 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2923 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002925 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2926 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2927 const uint8_t key[] = {
2928 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2929 0xaa, 0xaa, 0xaa, 0xaa };
2930 const uint8_t text[] = {
2931 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2932 0xbb, 0xbb, 0xbb, 0xbb };
2933 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2934 size_t length = 0;
2935
2936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002937 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2938 psa_set_key_algorithm( &attributes, alg );
2939 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02002940 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002941
2942
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002943 /* Call encrypt setup twice in a row. */
2944 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2945 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2946 PSA_ERROR_BAD_STATE );
2947 PSA_ASSERT( psa_cipher_abort( &operation ) );
2948
2949 /* Call decrypt setup twice in a row. */
2950 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2951 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2952 PSA_ERROR_BAD_STATE );
2953 PSA_ASSERT( psa_cipher_abort( &operation ) );
2954
Jaeden Ameroab439972019-02-15 14:12:05 +00002955 /* Generate an IV without calling setup beforehand. */
2956 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2957 buffer, sizeof( buffer ),
2958 &length ),
2959 PSA_ERROR_BAD_STATE );
2960 PSA_ASSERT( psa_cipher_abort( &operation ) );
2961
2962 /* Generate an IV twice in a row. */
2963 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2964 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2965 buffer, sizeof( buffer ),
2966 &length ) );
2967 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2968 buffer, sizeof( buffer ),
2969 &length ),
2970 PSA_ERROR_BAD_STATE );
2971 PSA_ASSERT( psa_cipher_abort( &operation ) );
2972
2973 /* Generate an IV after it's already set. */
2974 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2975 PSA_ASSERT( psa_cipher_set_iv( &operation,
2976 iv, sizeof( iv ) ) );
2977 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2978 buffer, sizeof( buffer ),
2979 &length ),
2980 PSA_ERROR_BAD_STATE );
2981 PSA_ASSERT( psa_cipher_abort( &operation ) );
2982
2983 /* Set an IV without calling setup beforehand. */
2984 TEST_EQUAL( psa_cipher_set_iv( &operation,
2985 iv, sizeof( iv ) ),
2986 PSA_ERROR_BAD_STATE );
2987 PSA_ASSERT( psa_cipher_abort( &operation ) );
2988
2989 /* Set an IV after it's already set. */
2990 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2991 PSA_ASSERT( psa_cipher_set_iv( &operation,
2992 iv, sizeof( iv ) ) );
2993 TEST_EQUAL( psa_cipher_set_iv( &operation,
2994 iv, sizeof( iv ) ),
2995 PSA_ERROR_BAD_STATE );
2996 PSA_ASSERT( psa_cipher_abort( &operation ) );
2997
2998 /* Set an IV after it's already generated. */
2999 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3000 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3001 buffer, sizeof( buffer ),
3002 &length ) );
3003 TEST_EQUAL( psa_cipher_set_iv( &operation,
3004 iv, sizeof( iv ) ),
3005 PSA_ERROR_BAD_STATE );
3006 PSA_ASSERT( psa_cipher_abort( &operation ) );
3007
3008 /* Call update without calling setup beforehand. */
3009 TEST_EQUAL( psa_cipher_update( &operation,
3010 text, sizeof( text ),
3011 buffer, sizeof( buffer ),
3012 &length ),
3013 PSA_ERROR_BAD_STATE );
3014 PSA_ASSERT( psa_cipher_abort( &operation ) );
3015
3016 /* Call update without an IV where an IV is required. */
3017 TEST_EQUAL( psa_cipher_update( &operation,
3018 text, sizeof( text ),
3019 buffer, sizeof( buffer ),
3020 &length ),
3021 PSA_ERROR_BAD_STATE );
3022 PSA_ASSERT( psa_cipher_abort( &operation ) );
3023
3024 /* Call update after finish. */
3025 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3026 PSA_ASSERT( psa_cipher_set_iv( &operation,
3027 iv, sizeof( iv ) ) );
3028 PSA_ASSERT( psa_cipher_finish( &operation,
3029 buffer, sizeof( buffer ), &length ) );
3030 TEST_EQUAL( psa_cipher_update( &operation,
3031 text, sizeof( text ),
3032 buffer, sizeof( buffer ),
3033 &length ),
3034 PSA_ERROR_BAD_STATE );
3035 PSA_ASSERT( psa_cipher_abort( &operation ) );
3036
3037 /* Call finish without calling setup beforehand. */
3038 TEST_EQUAL( psa_cipher_finish( &operation,
3039 buffer, sizeof( buffer ), &length ),
3040 PSA_ERROR_BAD_STATE );
3041 PSA_ASSERT( psa_cipher_abort( &operation ) );
3042
3043 /* Call finish without an IV where an IV is required. */
3044 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3045 /* Not calling update means we are encrypting an empty buffer, which is OK
3046 * for cipher modes with padding. */
3047 TEST_EQUAL( psa_cipher_finish( &operation,
3048 buffer, sizeof( buffer ), &length ),
3049 PSA_ERROR_BAD_STATE );
3050 PSA_ASSERT( psa_cipher_abort( &operation ) );
3051
3052 /* Call finish twice in a row. */
3053 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3054 PSA_ASSERT( psa_cipher_set_iv( &operation,
3055 iv, sizeof( iv ) ) );
3056 PSA_ASSERT( psa_cipher_finish( &operation,
3057 buffer, sizeof( buffer ), &length ) );
3058 TEST_EQUAL( psa_cipher_finish( &operation,
3059 buffer, sizeof( buffer ), &length ),
3060 PSA_ERROR_BAD_STATE );
3061 PSA_ASSERT( psa_cipher_abort( &operation ) );
3062
Gilles Peskine76b29a72019-05-28 14:08:50 +02003063 PSA_ASSERT( psa_destroy_key( handle ) );
3064
Jaeden Ameroab439972019-02-15 14:12:05 +00003065exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003066 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003067}
3068/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003069
Gilles Peskine50e586b2018-06-08 14:28:46 +02003070/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003071void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003072 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003073 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003074 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003075{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003076 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003077 psa_status_t status;
3078 psa_key_type_t key_type = key_type_arg;
3079 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003080 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003081 unsigned char *output = NULL;
3082 size_t output_buffer_size = 0;
3083 size_t function_output_length = 0;
3084 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003085 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003086 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003087
Gilles Peskine8817f612018-12-18 00:18:46 +01003088 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003089
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003090 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3091 psa_set_key_algorithm( &attributes, alg );
3092 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003093
Gilles Peskine73676cb2019-05-15 20:15:10 +02003094 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003095
Gilles Peskine8817f612018-12-18 00:18:46 +01003096 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3097 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003098
Gilles Peskine423005e2019-05-06 15:22:57 +02003099 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003100 output_buffer_size = ( (size_t) input->len +
3101 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003102 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003103
Gilles Peskine8817f612018-12-18 00:18:46 +01003104 PSA_ASSERT( psa_cipher_update( &operation,
3105 input->x, input->len,
3106 output, output_buffer_size,
3107 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003108 total_output_length += function_output_length;
3109 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003110 output + total_output_length,
3111 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003112 &function_output_length );
3113 total_output_length += function_output_length;
3114
Gilles Peskinefe11b722018-12-18 00:24:04 +01003115 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003116 if( expected_status == PSA_SUCCESS )
3117 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003118 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003119 ASSERT_COMPARE( expected_output->x, expected_output->len,
3120 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003121 }
3122
3123exit:
3124 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003125 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003126 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003127}
3128/* END_CASE */
3129
3130/* BEGIN_CASE */
3131void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003132 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003133 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003134 int first_part_size_arg,
3135 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003136 data_t *expected_output )
3137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003138 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003139 psa_key_type_t key_type = key_type_arg;
3140 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003141 size_t first_part_size = first_part_size_arg;
3142 size_t output1_length = output1_length_arg;
3143 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003144 unsigned char *output = NULL;
3145 size_t output_buffer_size = 0;
3146 size_t function_output_length = 0;
3147 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003148 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003150
Gilles Peskine8817f612018-12-18 00:18:46 +01003151 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003152
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3154 psa_set_key_algorithm( &attributes, alg );
3155 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003156
Gilles Peskine73676cb2019-05-15 20:15:10 +02003157 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003158
Gilles Peskine8817f612018-12-18 00:18:46 +01003159 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3160 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003161
Gilles Peskine423005e2019-05-06 15:22:57 +02003162 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003163 output_buffer_size = ( (size_t) input->len +
3164 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003165 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003166
Gilles Peskinee0866522019-02-19 19:44:00 +01003167 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3169 output, output_buffer_size,
3170 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003171 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003172 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003173 PSA_ASSERT( psa_cipher_update( &operation,
3174 input->x + first_part_size,
3175 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003176 output + total_output_length,
3177 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003178 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003179 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003180 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003181 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003182 output + total_output_length,
3183 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003184 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003185 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003187
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003188 ASSERT_COMPARE( expected_output->x, expected_output->len,
3189 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003190
3191exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003192 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003193 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003194 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003195}
3196/* END_CASE */
3197
3198/* BEGIN_CASE */
3199void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003200 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003201 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003202 int first_part_size_arg,
3203 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003204 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003205{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003206 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003207
3208 psa_key_type_t key_type = key_type_arg;
3209 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003210 size_t first_part_size = first_part_size_arg;
3211 size_t output1_length = output1_length_arg;
3212 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003213 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003214 size_t output_buffer_size = 0;
3215 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003216 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003217 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003218 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003219
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003221
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003222 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3223 psa_set_key_algorithm( &attributes, alg );
3224 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003225
Gilles Peskine73676cb2019-05-15 20:15:10 +02003226 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003227
Gilles Peskine8817f612018-12-18 00:18:46 +01003228 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3229 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003230
Gilles Peskine423005e2019-05-06 15:22:57 +02003231 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003232
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003233 output_buffer_size = ( (size_t) input->len +
3234 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003235 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003236
Gilles Peskinee0866522019-02-19 19:44:00 +01003237 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003238 PSA_ASSERT( psa_cipher_update( &operation,
3239 input->x, first_part_size,
3240 output, output_buffer_size,
3241 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003242 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003243 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003244 PSA_ASSERT( psa_cipher_update( &operation,
3245 input->x + first_part_size,
3246 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003247 output + total_output_length,
3248 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003249 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003250 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003251 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003252 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003253 output + total_output_length,
3254 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003255 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003256 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003257 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003258
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003259 ASSERT_COMPARE( expected_output->x, expected_output->len,
3260 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003261
3262exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003263 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003264 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003265 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003266}
3267/* END_CASE */
3268
Gilles Peskine50e586b2018-06-08 14:28:46 +02003269/* BEGIN_CASE */
3270void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003271 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003272 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003273 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003274{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003275 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003276 psa_status_t status;
3277 psa_key_type_t key_type = key_type_arg;
3278 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003279 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003280 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003281 size_t output_buffer_size = 0;
3282 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003283 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003284 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003286
Gilles Peskine8817f612018-12-18 00:18:46 +01003287 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003288
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003289 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3290 psa_set_key_algorithm( &attributes, alg );
3291 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003292
Gilles Peskine73676cb2019-05-15 20:15:10 +02003293 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003294
Gilles Peskine8817f612018-12-18 00:18:46 +01003295 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3296 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003297
Gilles Peskine423005e2019-05-06 15:22:57 +02003298 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003299
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003300 output_buffer_size = ( (size_t) input->len +
3301 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003302 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003303
Gilles Peskine8817f612018-12-18 00:18:46 +01003304 PSA_ASSERT( psa_cipher_update( &operation,
3305 input->x, input->len,
3306 output, output_buffer_size,
3307 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003308 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003309 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003310 output + total_output_length,
3311 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003312 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003313 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003314 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003315
3316 if( expected_status == PSA_SUCCESS )
3317 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003318 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003319 ASSERT_COMPARE( expected_output->x, expected_output->len,
3320 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003321 }
3322
Gilles Peskine50e586b2018-06-08 14:28:46 +02003323exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003324 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003325 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003326 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003327}
3328/* END_CASE */
3329
Gilles Peskine50e586b2018-06-08 14:28:46 +02003330/* BEGIN_CASE */
3331void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003332 data_t *key,
3333 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003334{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003335 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003336 psa_key_type_t key_type = key_type_arg;
3337 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003338 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003339 size_t iv_size = 16;
3340 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003341 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003342 size_t output1_size = 0;
3343 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003344 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003345 size_t output2_size = 0;
3346 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003347 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003348 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3349 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003351
Gilles Peskine8817f612018-12-18 00:18:46 +01003352 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003353
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003354 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3355 psa_set_key_algorithm( &attributes, alg );
3356 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003357
Gilles Peskine73676cb2019-05-15 20:15:10 +02003358 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003359
Gilles Peskine8817f612018-12-18 00:18:46 +01003360 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3361 handle, alg ) );
3362 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3363 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003364
Gilles Peskine8817f612018-12-18 00:18:46 +01003365 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3366 iv, iv_size,
3367 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003368 output1_size = ( (size_t) input->len +
3369 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003370 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003371
Gilles Peskine8817f612018-12-18 00:18:46 +01003372 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3373 output1, output1_size,
3374 &output1_length ) );
3375 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003376 output1 + output1_length,
3377 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003378 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003379
Gilles Peskine048b7f02018-06-08 14:20:49 +02003380 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003381
Gilles Peskine8817f612018-12-18 00:18:46 +01003382 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003383
3384 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003385 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003386
Gilles Peskine8817f612018-12-18 00:18:46 +01003387 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3388 iv, iv_length ) );
3389 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3390 output2, output2_size,
3391 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003392 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003393 PSA_ASSERT( psa_cipher_finish( &operation2,
3394 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003395 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003396 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003397
Gilles Peskine048b7f02018-06-08 14:20:49 +02003398 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003399
Gilles Peskine8817f612018-12-18 00:18:46 +01003400 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003401
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003402 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003403
3404exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003405 mbedtls_free( output1 );
3406 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003407 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003408 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003409}
3410/* END_CASE */
3411
3412/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003413void cipher_verify_output_multipart( int alg_arg,
3414 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003415 data_t *key,
3416 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003417 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003418{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003419 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003420 psa_key_type_t key_type = key_type_arg;
3421 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003422 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003423 unsigned char iv[16] = {0};
3424 size_t iv_size = 16;
3425 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003426 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003427 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003428 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003429 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003430 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003431 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003432 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003433 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3434 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003436
Gilles Peskine8817f612018-12-18 00:18:46 +01003437 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003438
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003439 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3440 psa_set_key_algorithm( &attributes, alg );
3441 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003442
Gilles Peskine73676cb2019-05-15 20:15:10 +02003443 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003444
Gilles Peskine8817f612018-12-18 00:18:46 +01003445 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3446 handle, alg ) );
3447 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3448 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003449
Gilles Peskine8817f612018-12-18 00:18:46 +01003450 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3451 iv, iv_size,
3452 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003453 output1_buffer_size = ( (size_t) input->len +
3454 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003455 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003456
Gilles Peskinee0866522019-02-19 19:44:00 +01003457 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003458
Gilles Peskine8817f612018-12-18 00:18:46 +01003459 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3460 output1, output1_buffer_size,
3461 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003462 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003463
Gilles Peskine8817f612018-12-18 00:18:46 +01003464 PSA_ASSERT( psa_cipher_update( &operation1,
3465 input->x + first_part_size,
3466 input->len - first_part_size,
3467 output1, output1_buffer_size,
3468 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003469 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003470
Gilles Peskine8817f612018-12-18 00:18:46 +01003471 PSA_ASSERT( psa_cipher_finish( &operation1,
3472 output1 + output1_length,
3473 output1_buffer_size - output1_length,
3474 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003475 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003476
Gilles Peskine8817f612018-12-18 00:18:46 +01003477 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003478
Gilles Peskine048b7f02018-06-08 14:20:49 +02003479 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003480 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003481
Gilles Peskine8817f612018-12-18 00:18:46 +01003482 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3483 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003484
Gilles Peskine8817f612018-12-18 00:18:46 +01003485 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3486 output2, output2_buffer_size,
3487 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003488 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003489
Gilles Peskine8817f612018-12-18 00:18:46 +01003490 PSA_ASSERT( psa_cipher_update( &operation2,
3491 output1 + first_part_size,
3492 output1_length - first_part_size,
3493 output2, output2_buffer_size,
3494 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003495 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003496
Gilles Peskine8817f612018-12-18 00:18:46 +01003497 PSA_ASSERT( psa_cipher_finish( &operation2,
3498 output2 + output2_length,
3499 output2_buffer_size - output2_length,
3500 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003501 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003502
Gilles Peskine8817f612018-12-18 00:18:46 +01003503 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003504
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003505 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003506
3507exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003508 mbedtls_free( output1 );
3509 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003510 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003511 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003512}
3513/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003514
Gilles Peskine20035e32018-02-03 22:44:14 +01003515/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003516void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003517 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003518 data_t *nonce,
3519 data_t *additional_data,
3520 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003521 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003522{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003523 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003524 psa_key_type_t key_type = key_type_arg;
3525 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003526 unsigned char *output_data = NULL;
3527 size_t output_size = 0;
3528 size_t output_length = 0;
3529 unsigned char *output_data2 = NULL;
3530 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003531 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003532 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003534
Gilles Peskine4abf7412018-06-18 16:35:34 +02003535 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003536 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3537 * should be exact. */
3538 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3539 TEST_EQUAL( output_size,
3540 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003541 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003542
Gilles Peskine8817f612018-12-18 00:18:46 +01003543 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003544
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003545 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3546 psa_set_key_algorithm( &attributes, alg );
3547 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003548
Gilles Peskine049c7532019-05-15 20:22:09 +02003549 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3550 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003551
Gilles Peskinefe11b722018-12-18 00:24:04 +01003552 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3553 nonce->x, nonce->len,
3554 additional_data->x,
3555 additional_data->len,
3556 input_data->x, input_data->len,
3557 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003558 &output_length ),
3559 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003560
3561 if( PSA_SUCCESS == expected_result )
3562 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003563 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003564
Gilles Peskine003a4a92019-05-14 16:09:40 +02003565 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3566 * should be exact. */
3567 TEST_EQUAL( input_data->len,
3568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3569
Gilles Peskinefe11b722018-12-18 00:24:04 +01003570 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3571 nonce->x, nonce->len,
3572 additional_data->x,
3573 additional_data->len,
3574 output_data, output_length,
3575 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003576 &output_length2 ),
3577 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003578
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003579 ASSERT_COMPARE( input_data->x, input_data->len,
3580 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003581 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003582
Gilles Peskinea1cac842018-06-11 19:33:02 +02003583exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003584 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003585 mbedtls_free( output_data );
3586 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003587 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003588}
3589/* END_CASE */
3590
3591/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003592void aead_encrypt( int key_type_arg, data_t *key_data,
3593 int alg_arg,
3594 data_t *nonce,
3595 data_t *additional_data,
3596 data_t *input_data,
3597 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003598{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003599 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003600 psa_key_type_t key_type = key_type_arg;
3601 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003602 unsigned char *output_data = NULL;
3603 size_t output_size = 0;
3604 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003605 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003606 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003607
Gilles Peskine4abf7412018-06-18 16:35:34 +02003608 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003609 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3610 * should be exact. */
3611 TEST_EQUAL( output_size,
3612 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003613 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003614
Gilles Peskine8817f612018-12-18 00:18:46 +01003615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003616
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3618 psa_set_key_algorithm( &attributes, alg );
3619 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003620
Gilles Peskine049c7532019-05-15 20:22:09 +02003621 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3622 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003623
Gilles Peskine8817f612018-12-18 00:18:46 +01003624 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3625 nonce->x, nonce->len,
3626 additional_data->x, additional_data->len,
3627 input_data->x, input_data->len,
3628 output_data, output_size,
3629 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003630
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003631 ASSERT_COMPARE( expected_result->x, expected_result->len,
3632 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003633
Gilles Peskinea1cac842018-06-11 19:33:02 +02003634exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003635 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003636 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003637 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003638}
3639/* END_CASE */
3640
3641/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003642void aead_decrypt( int key_type_arg, data_t *key_data,
3643 int alg_arg,
3644 data_t *nonce,
3645 data_t *additional_data,
3646 data_t *input_data,
3647 data_t *expected_data,
3648 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003649{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003650 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003651 psa_key_type_t key_type = key_type_arg;
3652 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003653 unsigned char *output_data = NULL;
3654 size_t output_size = 0;
3655 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003656 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003657 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003658 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003659
Gilles Peskine003a4a92019-05-14 16:09:40 +02003660 output_size = input_data->len - tag_length;
3661 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3662 * should be exact. */
3663 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3664 TEST_EQUAL( output_size,
3665 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003666 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003667
Gilles Peskine8817f612018-12-18 00:18:46 +01003668 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003669
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003670 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3671 psa_set_key_algorithm( &attributes, alg );
3672 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003673
Gilles Peskine049c7532019-05-15 20:22:09 +02003674 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3675 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003676
Gilles Peskinefe11b722018-12-18 00:24:04 +01003677 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3678 nonce->x, nonce->len,
3679 additional_data->x,
3680 additional_data->len,
3681 input_data->x, input_data->len,
3682 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003683 &output_length ),
3684 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003685
Gilles Peskine2d277862018-06-18 15:41:12 +02003686 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003687 ASSERT_COMPARE( expected_data->x, expected_data->len,
3688 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003689
Gilles Peskinea1cac842018-06-11 19:33:02 +02003690exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003691 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003692 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003693 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003694}
3695/* END_CASE */
3696
3697/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003698void signature_size( int type_arg,
3699 int bits,
3700 int alg_arg,
3701 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003702{
3703 psa_key_type_t type = type_arg;
3704 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003705 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003706 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003707exit:
3708 ;
3709}
3710/* END_CASE */
3711
3712/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003713void sign_deterministic( int key_type_arg, data_t *key_data,
3714 int alg_arg, data_t *input_data,
3715 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003716{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003717 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003718 psa_key_type_t key_type = key_type_arg;
3719 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003720 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003721 unsigned char *signature = NULL;
3722 size_t signature_size;
3723 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003725
Gilles Peskine8817f612018-12-18 00:18:46 +01003726 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003727
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003729 psa_set_key_algorithm( &attributes, alg );
3730 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003731
Gilles Peskine049c7532019-05-15 20:22:09 +02003732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3733 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003734 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3735 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003736
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003737 /* Allocate a buffer which has the size advertized by the
3738 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003739 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003740 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003741 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003742 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003743 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003744
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003745 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003746 PSA_ASSERT( psa_sign_hash( handle, alg,
3747 input_data->x, input_data->len,
3748 signature, signature_size,
3749 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003750 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003751 ASSERT_COMPARE( output_data->x, output_data->len,
3752 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003753
3754exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003755 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003756 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003757 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003758 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003759}
3760/* END_CASE */
3761
3762/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003763void sign_fail( int key_type_arg, data_t *key_data,
3764 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003765 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003766{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003767 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003768 psa_key_type_t key_type = key_type_arg;
3769 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003770 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003771 psa_status_t actual_status;
3772 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003773 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003774 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003775 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003776
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003777 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003778
Gilles Peskine8817f612018-12-18 00:18:46 +01003779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003780
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003781 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003782 psa_set_key_algorithm( &attributes, alg );
3783 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003784
Gilles Peskine049c7532019-05-15 20:22:09 +02003785 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3786 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003787
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003788 actual_status = psa_sign_hash( handle, alg,
3789 input_data->x, input_data->len,
3790 signature, signature_size,
3791 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003792 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003793 /* The value of *signature_length is unspecified on error, but
3794 * whatever it is, it should be less than signature_size, so that
3795 * if the caller tries to read *signature_length bytes without
3796 * checking the error code then they don't overflow a buffer. */
3797 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003798
3799exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003800 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003801 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003802 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003803 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003804}
3805/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003806
3807/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003808void sign_verify( int key_type_arg, data_t *key_data,
3809 int alg_arg, data_t *input_data )
3810{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003811 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003812 psa_key_type_t key_type = key_type_arg;
3813 psa_algorithm_t alg = alg_arg;
3814 size_t key_bits;
3815 unsigned char *signature = NULL;
3816 size_t signature_size;
3817 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003819
Gilles Peskine8817f612018-12-18 00:18:46 +01003820 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003821
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003822 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003823 psa_set_key_algorithm( &attributes, alg );
3824 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003825
Gilles Peskine049c7532019-05-15 20:22:09 +02003826 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3827 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003828 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3829 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003830
3831 /* Allocate a buffer which has the size advertized by the
3832 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003833 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003834 key_bits, alg );
3835 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003836 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003837 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003838
3839 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003840 PSA_ASSERT( psa_sign_hash( handle, alg,
3841 input_data->x, input_data->len,
3842 signature, signature_size,
3843 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003844 /* Check that the signature length looks sensible. */
3845 TEST_ASSERT( signature_length <= signature_size );
3846 TEST_ASSERT( signature_length > 0 );
3847
3848 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003849 PSA_ASSERT( psa_verify_hash( handle, alg,
3850 input_data->x, input_data->len,
3851 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003852
3853 if( input_data->len != 0 )
3854 {
3855 /* Flip a bit in the input and verify that the signature is now
3856 * detected as invalid. Flip a bit at the beginning, not at the end,
3857 * because ECDSA may ignore the last few bits of the input. */
3858 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003859 TEST_EQUAL( psa_verify_hash( handle, alg,
3860 input_data->x, input_data->len,
3861 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003862 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003863 }
3864
3865exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003866 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003867 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003868 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003869 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003870}
3871/* END_CASE */
3872
3873/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003874void asymmetric_verify( int key_type_arg, data_t *key_data,
3875 int alg_arg, data_t *hash_data,
3876 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003877{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003878 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003879 psa_key_type_t key_type = key_type_arg;
3880 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003881 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003882
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003883 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003884
Gilles Peskine8817f612018-12-18 00:18:46 +01003885 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003886
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003887 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003888 psa_set_key_algorithm( &attributes, alg );
3889 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003890
Gilles Peskine049c7532019-05-15 20:22:09 +02003891 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3892 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003893
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003894 PSA_ASSERT( psa_verify_hash( handle, alg,
3895 hash_data->x, hash_data->len,
3896 signature_data->x, 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 Peskine89d8c5c2019-11-26 17:01:59 +01003919 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003920 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 Peskine89d8c5c2019-11-26 17:01:59 +01003926 actual_status = psa_verify_hash( handle, alg,
3927 hash_data->x, hash_data->len,
3928 signature_data->x, signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003929
Gilles Peskinefe11b722018-12-18 00:24:04 +01003930 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931
3932exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003933 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003934 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003935 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003936}
3937/* END_CASE */
3938
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003939/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003940void asymmetric_encrypt( int key_type_arg,
3941 data_t *key_data,
3942 int alg_arg,
3943 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003944 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003945 int expected_output_length_arg,
3946 int expected_status_arg )
3947{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003948 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003949 psa_key_type_t key_type = key_type_arg;
3950 psa_algorithm_t alg = alg_arg;
3951 size_t expected_output_length = expected_output_length_arg;
3952 size_t key_bits;
3953 unsigned char *output = NULL;
3954 size_t output_size;
3955 size_t output_length = ~0;
3956 psa_status_t actual_status;
3957 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003958 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003959
Gilles Peskine8817f612018-12-18 00:18:46 +01003960 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003961
Gilles Peskine656896e2018-06-29 19:12:28 +02003962 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3964 psa_set_key_algorithm( &attributes, alg );
3965 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003966 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3967 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003968
3969 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003970 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3971 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003972 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003973 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003974
3975 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003976 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003977 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003978 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003979 output, output_size,
3980 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003981 TEST_EQUAL( actual_status, expected_status );
3982 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003983
Gilles Peskine68428122018-06-30 18:42:41 +02003984 /* If the label is empty, the test framework puts a non-null pointer
3985 * in label->x. Test that a null pointer works as well. */
3986 if( label->len == 0 )
3987 {
3988 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003989 if( output_size != 0 )
3990 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003991 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003992 input_data->x, input_data->len,
3993 NULL, label->len,
3994 output, output_size,
3995 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003996 TEST_EQUAL( actual_status, expected_status );
3997 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003998 }
3999
Gilles Peskine656896e2018-06-29 19:12:28 +02004000exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004001 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004002 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004003 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004004 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004005}
4006/* END_CASE */
4007
4008/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004009void asymmetric_encrypt_decrypt( int key_type_arg,
4010 data_t *key_data,
4011 int alg_arg,
4012 data_t *input_data,
4013 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004014{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004015 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016 psa_key_type_t key_type = key_type_arg;
4017 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004018 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004019 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004020 size_t output_size;
4021 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004022 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004023 size_t output2_size;
4024 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026
Gilles Peskine8817f612018-12-18 00:18:46 +01004027 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004029 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4030 psa_set_key_algorithm( &attributes, alg );
4031 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004032
Gilles Peskine049c7532019-05-15 20:22:09 +02004033 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4034 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004035
4036 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004037 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4038 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004039 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004040 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004041 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004042 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004043
Gilles Peskineeebd7382018-06-08 18:11:54 +02004044 /* We test encryption by checking that encrypt-then-decrypt gives back
4045 * the original plaintext because of the non-optional random
4046 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004047 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4048 input_data->x, input_data->len,
4049 label->x, label->len,
4050 output, output_size,
4051 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004052 /* We don't know what ciphertext length to expect, but check that
4053 * it looks sensible. */
4054 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004055
Gilles Peskine8817f612018-12-18 00:18:46 +01004056 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4057 output, output_length,
4058 label->x, label->len,
4059 output2, output2_size,
4060 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004061 ASSERT_COMPARE( input_data->x, input_data->len,
4062 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004063
4064exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004065 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004066 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004067 mbedtls_free( output );
4068 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004069 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004070}
4071/* END_CASE */
4072
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004073/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004074void asymmetric_decrypt( int key_type_arg,
4075 data_t *key_data,
4076 int alg_arg,
4077 data_t *input_data,
4078 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004079 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004080{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004081 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004082 psa_key_type_t key_type = key_type_arg;
4083 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004084 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004085 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004086 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004088
Jaeden Amero412654a2019-02-06 12:57:46 +00004089 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004090 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004091
Gilles Peskine8817f612018-12-18 00:18:46 +01004092 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004093
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004094 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4095 psa_set_key_algorithm( &attributes, alg );
4096 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004097
Gilles Peskine049c7532019-05-15 20:22:09 +02004098 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4099 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004100
Gilles Peskine8817f612018-12-18 00:18:46 +01004101 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4102 input_data->x, input_data->len,
4103 label->x, label->len,
4104 output,
4105 output_size,
4106 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004107 ASSERT_COMPARE( expected_data->x, expected_data->len,
4108 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004109
Gilles Peskine68428122018-06-30 18:42:41 +02004110 /* If the label is empty, the test framework puts a non-null pointer
4111 * in label->x. Test that a null pointer works as well. */
4112 if( label->len == 0 )
4113 {
4114 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004115 if( output_size != 0 )
4116 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004117 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4118 input_data->x, input_data->len,
4119 NULL, label->len,
4120 output,
4121 output_size,
4122 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004123 ASSERT_COMPARE( expected_data->x, expected_data->len,
4124 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004125 }
4126
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004127exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004128 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004129 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004130 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004131 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004132}
4133/* END_CASE */
4134
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004135/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004136void asymmetric_decrypt_fail( int key_type_arg,
4137 data_t *key_data,
4138 int alg_arg,
4139 data_t *input_data,
4140 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004141 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004142 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004143{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004144 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004145 psa_key_type_t key_type = key_type_arg;
4146 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004147 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004148 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004149 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004150 psa_status_t actual_status;
4151 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004153
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004154 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004155
Gilles Peskine8817f612018-12-18 00:18:46 +01004156 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004157
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004158 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4159 psa_set_key_algorithm( &attributes, alg );
4160 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004161
Gilles Peskine049c7532019-05-15 20:22:09 +02004162 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4163 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004164
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004165 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004166 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004167 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004168 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004169 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004170 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004171 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004172
Gilles Peskine68428122018-06-30 18:42:41 +02004173 /* If the label is empty, the test framework puts a non-null pointer
4174 * in label->x. Test that a null pointer works as well. */
4175 if( label->len == 0 )
4176 {
4177 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004178 if( output_size != 0 )
4179 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004180 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004181 input_data->x, input_data->len,
4182 NULL, label->len,
4183 output, output_size,
4184 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004185 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004186 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004187 }
4188
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004189exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004190 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004191 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004192 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004193 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004194}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004195/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004196
4197/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004198void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004199{
4200 /* Test each valid way of initializing the object, except for `= {0}`, as
4201 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4202 * though it's OK by the C standard. We could test for this, but we'd need
4203 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004204 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004205 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4206 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4207 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004208
4209 memset( &zero, 0, sizeof( zero ) );
4210
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004211 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004212 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004213 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004214 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004215 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004216 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004217 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004218
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004219 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004220 PSA_ASSERT( psa_key_derivation_abort(&func) );
4221 PSA_ASSERT( psa_key_derivation_abort(&init) );
4222 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004223}
4224/* END_CASE */
4225
Janos Follath16de4a42019-06-13 16:32:24 +01004226/* BEGIN_CASE */
4227void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004228{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004229 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004230 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004231 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004232
Gilles Peskine8817f612018-12-18 00:18:46 +01004233 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004234
Janos Follath16de4a42019-06-13 16:32:24 +01004235 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004236 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004237
4238exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004239 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004240 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004241}
4242/* END_CASE */
4243
Janos Follathaf3c2a02019-06-12 12:34:34 +01004244/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004245void derive_set_capacity( int alg_arg, int capacity_arg,
4246 int expected_status_arg )
4247{
4248 psa_algorithm_t alg = alg_arg;
4249 size_t capacity = capacity_arg;
4250 psa_status_t expected_status = expected_status_arg;
4251 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4252
4253 PSA_ASSERT( psa_crypto_init( ) );
4254
4255 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4256
4257 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4258 expected_status );
4259
4260exit:
4261 psa_key_derivation_abort( &operation );
4262 PSA_DONE( );
4263}
4264/* END_CASE */
4265
4266/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004267void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004268 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004269 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004270 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004271 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004272 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004273 int expected_status_arg3,
4274 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004275{
4276 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004277 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4278 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004279 psa_status_t expected_statuses[] = {expected_status_arg1,
4280 expected_status_arg2,
4281 expected_status_arg3};
4282 data_t *inputs[] = {input1, input2, input3};
4283 psa_key_handle_t handles[] = {0, 0, 0};
4284 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4286 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004287 psa_key_type_t output_key_type = output_key_type_arg;
4288 psa_key_handle_t output_handle = 0;
4289 psa_status_t expected_output_status = expected_output_status_arg;
4290 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004291
4292 PSA_ASSERT( psa_crypto_init( ) );
4293
4294 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4295 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004296
4297 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4298
4299 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4300 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004301 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004302 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004303 psa_set_key_type( &attributes, key_types[i] );
4304 PSA_ASSERT( psa_import_key( &attributes,
4305 inputs[i]->x, inputs[i]->len,
4306 &handles[i] ) );
4307 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4308 handles[i] ),
4309 expected_statuses[i] );
4310 }
4311 else
4312 {
4313 TEST_EQUAL( psa_key_derivation_input_bytes(
4314 &operation, steps[i],
4315 inputs[i]->x, inputs[i]->len ),
4316 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004317 }
4318 }
4319
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004320 if( output_key_type != PSA_KEY_TYPE_NONE )
4321 {
4322 psa_reset_key_attributes( &attributes );
4323 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4324 psa_set_key_bits( &attributes, 8 );
4325 actual_output_status =
4326 psa_key_derivation_output_key( &attributes, &operation,
4327 &output_handle );
4328 }
4329 else
4330 {
4331 uint8_t buffer[1];
4332 actual_output_status =
4333 psa_key_derivation_output_bytes( &operation,
4334 buffer, sizeof( buffer ) );
4335 }
4336 TEST_EQUAL( actual_output_status, expected_output_status );
4337
Janos Follathaf3c2a02019-06-12 12:34:34 +01004338exit:
4339 psa_key_derivation_abort( &operation );
4340 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4341 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004342 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004343 PSA_DONE( );
4344}
4345/* END_CASE */
4346
Janos Follathd958bb72019-07-03 15:02:16 +01004347/* BEGIN_CASE */
4348void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004349{
Janos Follathd958bb72019-07-03 15:02:16 +01004350 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004351 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004352 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004353 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004354 unsigned char input1[] = "Input 1";
4355 size_t input1_length = sizeof( input1 );
4356 unsigned char input2[] = "Input 2";
4357 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004358 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004359 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004360 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4361 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4362 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004364
Gilles Peskine8817f612018-12-18 00:18:46 +01004365 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004366
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004367 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4368 psa_set_key_algorithm( &attributes, alg );
4369 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004370
Gilles Peskine73676cb2019-05-15 20:15:10 +02004371 PSA_ASSERT( psa_import_key( &attributes,
4372 key_data, sizeof( key_data ),
4373 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004374
4375 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004376 if( !setup_key_derivation_wrap( &operation, handle, alg,
4377 input1, input1_length,
4378 input2, input2_length,
4379 capacity ) )
4380 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004381
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004382 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004383 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004384 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004385
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004386 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004387
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004389 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004390
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004391exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004392 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004393 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004394 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004395}
4396/* END_CASE */
4397
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004398/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004399void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004400{
4401 uint8_t output_buffer[16];
4402 size_t buffer_size = 16;
4403 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004404 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004405
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004406 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4407 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004408 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004409
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004410 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004411 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004412
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004413 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004414
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004415 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4416 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004417 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004418
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004420 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004421
4422exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004424}
4425/* END_CASE */
4426
4427/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004428void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004429 int step1_arg, data_t *input1,
4430 int step2_arg, data_t *input2,
4431 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004432 int requested_capacity_arg,
4433 data_t *expected_output1,
4434 data_t *expected_output2 )
4435{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004436 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004437 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4438 data_t *inputs[] = {input1, input2, input3};
4439 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004440 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004441 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004442 uint8_t *expected_outputs[2] =
4443 {expected_output1->x, expected_output2->x};
4444 size_t output_sizes[2] =
4445 {expected_output1->len, expected_output2->len};
4446 size_t output_buffer_size = 0;
4447 uint8_t *output_buffer = NULL;
4448 size_t expected_capacity;
4449 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004451 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004452 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004453
4454 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4455 {
4456 if( output_sizes[i] > output_buffer_size )
4457 output_buffer_size = output_sizes[i];
4458 if( output_sizes[i] == 0 )
4459 expected_outputs[i] = NULL;
4460 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004461 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004463
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004464 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4465 psa_set_key_algorithm( &attributes, alg );
4466 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004467
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004468 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004469 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4470 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4471 requested_capacity ) );
4472 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004473 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004474 switch( steps[i] )
4475 {
4476 case 0:
4477 break;
4478 case PSA_KEY_DERIVATION_INPUT_SECRET:
4479 PSA_ASSERT( psa_import_key( &attributes,
4480 inputs[i]->x, inputs[i]->len,
4481 &handles[i] ) );
4482 PSA_ASSERT( psa_key_derivation_input_key(
4483 &operation, steps[i],
4484 handles[i] ) );
4485 break;
4486 default:
4487 PSA_ASSERT( psa_key_derivation_input_bytes(
4488 &operation, steps[i],
4489 inputs[i]->x, inputs[i]->len ) );
4490 break;
4491 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004492 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004493
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004495 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004496 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004497 expected_capacity = requested_capacity;
4498
4499 /* Expansion phase. */
4500 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4501 {
4502 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004503 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004504 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004505 if( expected_capacity == 0 && output_sizes[i] == 0 )
4506 {
4507 /* Reading 0 bytes when 0 bytes are available can go either way. */
4508 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004509 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004510 continue;
4511 }
4512 else if( expected_capacity == 0 ||
4513 output_sizes[i] > expected_capacity )
4514 {
4515 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004516 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004517 expected_capacity = 0;
4518 continue;
4519 }
4520 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004521 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004522 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004523 ASSERT_COMPARE( output_buffer, output_sizes[i],
4524 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004525 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004526 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004527 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004528 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004529 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004530 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004531 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004532
4533exit:
4534 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004535 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004536 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4537 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004538 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004539}
4540/* END_CASE */
4541
4542/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004543void derive_full( int alg_arg,
4544 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004545 data_t *input1,
4546 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004547 int requested_capacity_arg )
4548{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004549 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004550 psa_algorithm_t alg = alg_arg;
4551 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004552 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004553 unsigned char output_buffer[16];
4554 size_t expected_capacity = requested_capacity;
4555 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004556 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004557
Gilles Peskine8817f612018-12-18 00:18:46 +01004558 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004559
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004560 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4561 psa_set_key_algorithm( &attributes, alg );
4562 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004563
Gilles Peskine049c7532019-05-15 20:22:09 +02004564 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4565 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004566
Janos Follathf2815ea2019-07-03 12:41:36 +01004567 if( !setup_key_derivation_wrap( &operation, handle, alg,
4568 input1->x, input1->len,
4569 input2->x, input2->len,
4570 requested_capacity ) )
4571 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004572
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004573 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004574 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004575 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004576
4577 /* Expansion phase. */
4578 while( current_capacity > 0 )
4579 {
4580 size_t read_size = sizeof( output_buffer );
4581 if( read_size > current_capacity )
4582 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004583 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004584 output_buffer,
4585 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004586 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004588 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004589 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004590 }
4591
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004592 /* Check that the operation refuses to go over capacity. */
4593 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004594 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004595
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004596 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004597
4598exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004599 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004600 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004601 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004602}
4603/* END_CASE */
4604
Janos Follathe60c9052019-07-03 13:51:30 +01004605/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004606void derive_key_exercise( int alg_arg,
4607 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004608 data_t *input1,
4609 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004610 int derived_type_arg,
4611 int derived_bits_arg,
4612 int derived_usage_arg,
4613 int derived_alg_arg )
4614{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004615 psa_key_handle_t base_handle = 0;
4616 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004617 psa_algorithm_t alg = alg_arg;
4618 psa_key_type_t derived_type = derived_type_arg;
4619 size_t derived_bits = derived_bits_arg;
4620 psa_key_usage_t derived_usage = derived_usage_arg;
4621 psa_algorithm_t derived_alg = derived_alg_arg;
4622 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004623 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004625 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004626
Gilles Peskine8817f612018-12-18 00:18:46 +01004627 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004628
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004629 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4630 psa_set_key_algorithm( &attributes, alg );
4631 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004632 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4633 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004634
4635 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004636 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4637 input1->x, input1->len,
4638 input2->x, input2->len, capacity ) )
4639 goto exit;
4640
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004641 psa_set_key_usage_flags( &attributes, derived_usage );
4642 psa_set_key_algorithm( &attributes, derived_alg );
4643 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004644 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004645 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004646 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004647
4648 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004649 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4650 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4651 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004652
4653 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004654 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004655 goto exit;
4656
4657exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004658 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004659 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004660 psa_destroy_key( base_handle );
4661 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004662 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004663}
4664/* END_CASE */
4665
Janos Follath42fd8882019-07-03 14:17:09 +01004666/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004667void derive_key_export( int alg_arg,
4668 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004669 data_t *input1,
4670 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004671 int bytes1_arg,
4672 int bytes2_arg )
4673{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004674 psa_key_handle_t base_handle = 0;
4675 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004676 psa_algorithm_t alg = alg_arg;
4677 size_t bytes1 = bytes1_arg;
4678 size_t bytes2 = bytes2_arg;
4679 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004680 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004681 uint8_t *output_buffer = NULL;
4682 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004683 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4684 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004685 size_t length;
4686
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004687 ASSERT_ALLOC( output_buffer, capacity );
4688 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004689 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004690
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004691 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4692 psa_set_key_algorithm( &base_attributes, alg );
4693 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004694 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4695 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004696
4697 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01004698 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4699 input1->x, input1->len,
4700 input2->x, input2->len, capacity ) )
4701 goto exit;
4702
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004704 output_buffer,
4705 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004706 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004707
4708 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01004709 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4710 input1->x, input1->len,
4711 input2->x, input2->len, capacity ) )
4712 goto exit;
4713
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004714 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4715 psa_set_key_algorithm( &derived_attributes, 0 );
4716 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004717 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004718 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004719 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004720 PSA_ASSERT( psa_export_key( derived_handle,
4721 export_buffer, bytes1,
4722 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004723 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004724 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004725 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004727 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004728 PSA_ASSERT( psa_export_key( derived_handle,
4729 export_buffer + bytes1, bytes2,
4730 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004731 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004732
4733 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004734 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4735 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004736
4737exit:
4738 mbedtls_free( output_buffer );
4739 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004741 psa_destroy_key( base_handle );
4742 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004743 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004744}
4745/* END_CASE */
4746
4747/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004748void derive_key( int alg_arg,
4749 data_t *key_data, data_t *input1, data_t *input2,
4750 int type_arg, int bits_arg,
4751 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02004752{
4753 psa_key_handle_t base_handle = 0;
4754 psa_key_handle_t derived_handle = 0;
4755 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004756 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004757 size_t bits = bits_arg;
4758 psa_status_t expected_status = expected_status_arg;
4759 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4760 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4761 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4762
4763 PSA_ASSERT( psa_crypto_init( ) );
4764
4765 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4766 psa_set_key_algorithm( &base_attributes, alg );
4767 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4768 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4769 &base_handle ) );
4770
4771 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4772 input1->x, input1->len,
4773 input2->x, input2->len, SIZE_MAX ) )
4774 goto exit;
4775
4776 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4777 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004778 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004779 psa_set_key_bits( &derived_attributes, bits );
4780 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
4781 &derived_handle ),
4782 expected_status );
4783
4784exit:
4785 psa_key_derivation_abort( &operation );
4786 psa_destroy_key( base_handle );
4787 psa_destroy_key( derived_handle );
4788 PSA_DONE( );
4789}
4790/* END_CASE */
4791
4792/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004793void key_agreement_setup( int alg_arg,
4794 int our_key_type_arg, data_t *our_key_data,
4795 data_t *peer_key_data,
4796 int expected_status_arg )
4797{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004798 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004799 psa_algorithm_t alg = alg_arg;
4800 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004801 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004803 psa_status_t expected_status = expected_status_arg;
4804 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004805
Gilles Peskine8817f612018-12-18 00:18:46 +01004806 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004807
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004808 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4809 psa_set_key_algorithm( &attributes, alg );
4810 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004811 PSA_ASSERT( psa_import_key( &attributes,
4812 our_key_data->x, our_key_data->len,
4813 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004814
Gilles Peskine77f40d82019-04-11 21:27:06 +02004815 /* The tests currently include inputs that should fail at either step.
4816 * Test cases that fail at the setup step should be changed to call
4817 * key_derivation_setup instead, and this function should be renamed
4818 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004819 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004820 if( status == PSA_SUCCESS )
4821 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004822 TEST_EQUAL( psa_key_derivation_key_agreement(
4823 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4824 our_key,
4825 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004826 expected_status );
4827 }
4828 else
4829 {
4830 TEST_ASSERT( status == expected_status );
4831 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004832
4833exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004834 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004835 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004836 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004837}
4838/* END_CASE */
4839
4840/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004841void raw_key_agreement( int alg_arg,
4842 int our_key_type_arg, data_t *our_key_data,
4843 data_t *peer_key_data,
4844 data_t *expected_output )
4845{
4846 psa_key_handle_t our_key = 0;
4847 psa_algorithm_t alg = alg_arg;
4848 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004849 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004850 unsigned char *output = NULL;
4851 size_t output_length = ~0;
4852
4853 ASSERT_ALLOC( output, expected_output->len );
4854 PSA_ASSERT( psa_crypto_init( ) );
4855
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004856 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4857 psa_set_key_algorithm( &attributes, alg );
4858 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004859 PSA_ASSERT( psa_import_key( &attributes,
4860 our_key_data->x, our_key_data->len,
4861 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004862
Gilles Peskinebe697d82019-05-16 18:00:41 +02004863 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4864 peer_key_data->x, peer_key_data->len,
4865 output, expected_output->len,
4866 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004867 ASSERT_COMPARE( output, output_length,
4868 expected_output->x, expected_output->len );
4869
4870exit:
4871 mbedtls_free( output );
4872 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004873 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004874}
4875/* END_CASE */
4876
4877/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004878void key_agreement_capacity( int alg_arg,
4879 int our_key_type_arg, data_t *our_key_data,
4880 data_t *peer_key_data,
4881 int expected_capacity_arg )
4882{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004883 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004884 psa_algorithm_t alg = alg_arg;
4885 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004886 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004888 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004889 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004890
Gilles Peskine8817f612018-12-18 00:18:46 +01004891 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004892
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004893 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4894 psa_set_key_algorithm( &attributes, alg );
4895 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004896 PSA_ASSERT( psa_import_key( &attributes,
4897 our_key_data->x, our_key_data->len,
4898 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004899
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004900 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004901 PSA_ASSERT( psa_key_derivation_key_agreement(
4902 &operation,
4903 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4904 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004905 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4906 {
4907 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004908 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004909 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004910 NULL, 0 ) );
4911 }
Gilles Peskine59685592018-09-18 12:11:34 +02004912
Gilles Peskinebf491972018-10-25 22:36:12 +02004913 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004914 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004916 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004917
Gilles Peskinebf491972018-10-25 22:36:12 +02004918 /* Test the actual capacity by reading the output. */
4919 while( actual_capacity > sizeof( output ) )
4920 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004921 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004922 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004923 actual_capacity -= sizeof( output );
4924 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004926 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004927 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004928 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004929
Gilles Peskine59685592018-09-18 12:11:34 +02004930exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004932 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004933 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004934}
4935/* END_CASE */
4936
4937/* BEGIN_CASE */
4938void key_agreement_output( int alg_arg,
4939 int our_key_type_arg, data_t *our_key_data,
4940 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004941 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004942{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004943 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004944 psa_algorithm_t alg = alg_arg;
4945 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004946 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004947 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004948 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004949
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004950 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4951 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004952
Gilles Peskine8817f612018-12-18 00:18:46 +01004953 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004954
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004955 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4956 psa_set_key_algorithm( &attributes, alg );
4957 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004958 PSA_ASSERT( psa_import_key( &attributes,
4959 our_key_data->x, our_key_data->len,
4960 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004961
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004963 PSA_ASSERT( psa_key_derivation_key_agreement(
4964 &operation,
4965 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4966 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004967 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4968 {
4969 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004971 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004972 NULL, 0 ) );
4973 }
Gilles Peskine59685592018-09-18 12:11:34 +02004974
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004976 actual_output,
4977 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004978 ASSERT_COMPARE( actual_output, expected_output1->len,
4979 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004980 if( expected_output2->len != 0 )
4981 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004983 actual_output,
4984 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004985 ASSERT_COMPARE( actual_output, expected_output2->len,
4986 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004987 }
Gilles Peskine59685592018-09-18 12:11:34 +02004988
4989exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004990 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004991 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004992 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004993 mbedtls_free( actual_output );
4994}
4995/* END_CASE */
4996
4997/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004998void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004999{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005000 size_t bytes = bytes_arg;
5001 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005002 unsigned char *output = NULL;
5003 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005004 size_t i;
5005 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005006
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005007 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5008 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005009 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005010
Gilles Peskine8817f612018-12-18 00:18:46 +01005011 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005012
Gilles Peskinea50d7392018-06-21 10:22:13 +02005013 /* Run several times, to ensure that every output byte will be
5014 * nonzero at least once with overwhelming probability
5015 * (2^(-8*number_of_runs)). */
5016 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005017 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005018 if( bytes != 0 )
5019 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005020 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005021
5022 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005023 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5024 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005025
5026 for( i = 0; i < bytes; i++ )
5027 {
5028 if( output[i] != 0 )
5029 ++changed[i];
5030 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005031 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005032
5033 /* Check that every byte was changed to nonzero at least once. This
5034 * validates that psa_generate_random is overwriting every byte of
5035 * the output buffer. */
5036 for( i = 0; i < bytes; i++ )
5037 {
5038 TEST_ASSERT( changed[i] != 0 );
5039 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005040
5041exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005042 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005043 mbedtls_free( output );
5044 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005045}
5046/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005047
5048/* BEGIN_CASE */
5049void generate_key( int type_arg,
5050 int bits_arg,
5051 int usage_arg,
5052 int alg_arg,
5053 int expected_status_arg )
5054{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005055 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005056 psa_key_type_t type = type_arg;
5057 psa_key_usage_t usage = usage_arg;
5058 size_t bits = bits_arg;
5059 psa_algorithm_t alg = alg_arg;
5060 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005061 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005062 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005063
Gilles Peskine8817f612018-12-18 00:18:46 +01005064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005065
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005066 psa_set_key_usage_flags( &attributes, usage );
5067 psa_set_key_algorithm( &attributes, alg );
5068 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005069 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005070
5071 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005072 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005073 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005074 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005075
5076 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005077 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5078 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5079 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005080
Gilles Peskine818ca122018-06-20 18:16:48 +02005081 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005082 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005083 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005084
5085exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005086 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005087 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005088 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005089}
5090/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005091
Gilles Peskinee56e8782019-04-26 17:34:02 +02005092/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5093void generate_key_rsa( int bits_arg,
5094 data_t *e_arg,
5095 int expected_status_arg )
5096{
5097 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005098 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005099 size_t bits = bits_arg;
5100 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5101 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5102 psa_status_t expected_status = expected_status_arg;
5103 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5104 uint8_t *exported = NULL;
5105 size_t exported_size =
5106 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5107 size_t exported_length = SIZE_MAX;
5108 uint8_t *e_read_buffer = NULL;
5109 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005110 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005111 size_t e_read_length = SIZE_MAX;
5112
5113 if( e_arg->len == 0 ||
5114 ( e_arg->len == 3 &&
5115 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5116 {
5117 is_default_public_exponent = 1;
5118 e_read_size = 0;
5119 }
5120 ASSERT_ALLOC( e_read_buffer, e_read_size );
5121 ASSERT_ALLOC( exported, exported_size );
5122
5123 PSA_ASSERT( psa_crypto_init( ) );
5124
5125 psa_set_key_usage_flags( &attributes, usage );
5126 psa_set_key_algorithm( &attributes, alg );
5127 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5128 e_arg->x, e_arg->len ) );
5129 psa_set_key_bits( &attributes, bits );
5130
5131 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005132 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005133 if( expected_status != PSA_SUCCESS )
5134 goto exit;
5135
5136 /* Test the key information */
5137 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5138 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5139 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5140 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5141 e_read_buffer, e_read_size,
5142 &e_read_length ) );
5143 if( is_default_public_exponent )
5144 TEST_EQUAL( e_read_length, 0 );
5145 else
5146 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5147
5148 /* Do something with the key according to its type and permitted usage. */
5149 if( ! exercise_key( handle, usage, alg ) )
5150 goto exit;
5151
5152 /* Export the key and check the public exponent. */
5153 PSA_ASSERT( psa_export_public_key( handle,
5154 exported, exported_size,
5155 &exported_length ) );
5156 {
5157 uint8_t *p = exported;
5158 uint8_t *end = exported + exported_length;
5159 size_t len;
5160 /* RSAPublicKey ::= SEQUENCE {
5161 * modulus INTEGER, -- n
5162 * publicExponent INTEGER } -- e
5163 */
5164 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005165 MBEDTLS_ASN1_SEQUENCE |
5166 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005167 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5168 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5169 MBEDTLS_ASN1_INTEGER ) );
5170 if( len >= 1 && p[0] == 0 )
5171 {
5172 ++p;
5173 --len;
5174 }
5175 if( e_arg->len == 0 )
5176 {
5177 TEST_EQUAL( len, 3 );
5178 TEST_EQUAL( p[0], 1 );
5179 TEST_EQUAL( p[1], 0 );
5180 TEST_EQUAL( p[2], 1 );
5181 }
5182 else
5183 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5184 }
5185
5186exit:
5187 psa_reset_key_attributes( &attributes );
5188 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005189 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005190 mbedtls_free( e_read_buffer );
5191 mbedtls_free( exported );
5192}
5193/* END_CASE */
5194
Darryl Greend49a4992018-06-18 17:27:26 +01005195/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005196void persistent_key_load_key_from_storage( data_t *data,
5197 int type_arg, int bits_arg,
5198 int usage_flags_arg, int alg_arg,
5199 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005200{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005201 psa_key_id_t key_id = 1;
5202 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005203 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005204 psa_key_handle_t base_key = 0;
5205 psa_key_type_t type = type_arg;
5206 size_t bits = bits_arg;
5207 psa_key_usage_t usage_flags = usage_flags_arg;
5208 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005210 unsigned char *first_export = NULL;
5211 unsigned char *second_export = NULL;
5212 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5213 size_t first_exported_length;
5214 size_t second_exported_length;
5215
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005216 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5217 {
5218 ASSERT_ALLOC( first_export, export_size );
5219 ASSERT_ALLOC( second_export, export_size );
5220 }
Darryl Greend49a4992018-06-18 17:27:26 +01005221
Gilles Peskine8817f612018-12-18 00:18:46 +01005222 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005223
Gilles Peskinec87af662019-05-15 16:12:22 +02005224 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005225 psa_set_key_usage_flags( &attributes, usage_flags );
5226 psa_set_key_algorithm( &attributes, alg );
5227 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005228 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005229
Darryl Green0c6575a2018-11-07 16:05:30 +00005230 switch( generation_method )
5231 {
5232 case IMPORT_KEY:
5233 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005234 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5235 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005236 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005237
Darryl Green0c6575a2018-11-07 16:05:30 +00005238 case GENERATE_KEY:
5239 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005240 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005241 break;
5242
5243 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005244 {
5245 /* Create base key */
5246 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5247 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5248 psa_set_key_usage_flags( &base_attributes,
5249 PSA_KEY_USAGE_DERIVE );
5250 psa_set_key_algorithm( &base_attributes, derive_alg );
5251 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005252 PSA_ASSERT( psa_import_key( &base_attributes,
5253 data->x, data->len,
5254 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005255 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005257 PSA_ASSERT( psa_key_derivation_input_key(
5258 &operation,
5259 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005260 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005262 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005263 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5264 &operation,
5265 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005266 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005267 PSA_ASSERT( psa_destroy_key( base_key ) );
5268 base_key = 0;
5269 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005270 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005271 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005272 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005273
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005274 /* Export the key if permitted by the key policy. */
5275 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5276 {
5277 PSA_ASSERT( psa_export_key( handle,
5278 first_export, export_size,
5279 &first_exported_length ) );
5280 if( generation_method == IMPORT_KEY )
5281 ASSERT_COMPARE( data->x, data->len,
5282 first_export, first_exported_length );
5283 }
Darryl Greend49a4992018-06-18 17:27:26 +01005284
5285 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005286 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005287 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005288 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005289
Darryl Greend49a4992018-06-18 17:27:26 +01005290 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005291 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005292 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5293 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5294 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5295 PSA_KEY_LIFETIME_PERSISTENT );
5296 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5297 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5298 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5299 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005300
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005301 /* Export the key again if permitted by the key policy. */
5302 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005303 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005304 PSA_ASSERT( psa_export_key( handle,
5305 second_export, export_size,
5306 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005307 ASSERT_COMPARE( first_export, first_exported_length,
5308 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005309 }
5310
5311 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005312 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005313 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005314
5315exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005316 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005317 mbedtls_free( first_export );
5318 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005319 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005320 psa_destroy_key( base_key );
5321 if( handle == 0 )
5322 {
5323 /* In case there was a test failure after creating the persistent key
5324 * but while it was not open, try to re-open the persistent key
5325 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005326 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005327 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005328 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005329 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005330}
5331/* END_CASE */