blob: 83b0c952d94d00ffde5fcfd3c67deb1d3fde512d [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 );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001167
1168#if defined(MBEDTLS_TEST_DEPRECATED)
1169 /* Check deprecated constants. */
1170 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1171 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1172 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1173 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1174 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1175 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1176 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1177 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
1178#endif /* MBEDTLS_TEST_DEPRECATED */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001179}
1180/* END_CASE */
1181
1182/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183void attributes_set_get( int id_arg, int lifetime_arg,
1184 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001185 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001186{
Gilles Peskine4747d192019-04-17 15:05:45 +02001187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001188 psa_key_id_t id = id_arg;
1189 psa_key_lifetime_t lifetime = lifetime_arg;
1190 psa_key_usage_t usage_flags = usage_flags_arg;
1191 psa_algorithm_t alg = alg_arg;
1192 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001193 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001194
1195 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1196 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1197 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1198 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1199 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001200 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001201
Gilles Peskinec87af662019-05-15 16:12:22 +02001202 psa_set_key_id( &attributes, id );
1203 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001204 psa_set_key_usage_flags( &attributes, usage_flags );
1205 psa_set_key_algorithm( &attributes, alg );
1206 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001207 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001208
1209 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1210 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1211 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1212 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1213 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001214 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001215
1216 psa_reset_key_attributes( &attributes );
1217
1218 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1219 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1220 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1221 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1222 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001223 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001224}
1225/* END_CASE */
1226
1227/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001228void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1229 int expected_id_arg, int expected_lifetime_arg )
1230{
1231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1232 psa_key_id_t id1 = id1_arg;
1233 psa_key_lifetime_t lifetime = lifetime_arg;
1234 psa_key_id_t id2 = id2_arg;
1235 psa_key_id_t expected_id = expected_id_arg;
1236 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1237
1238 if( id1_arg != -1 )
1239 psa_set_key_id( &attributes, id1 );
1240 if( lifetime_arg != -1 )
1241 psa_set_key_lifetime( &attributes, lifetime );
1242 if( id2_arg != -1 )
1243 psa_set_key_id( &attributes, id2 );
1244
1245 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1246 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1247}
1248/* END_CASE */
1249
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001250/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1251void slot_number_attribute( )
1252{
1253 psa_key_slot_number_t slot_number = 0xdeadbeef;
1254 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1255
1256 /* Initially, there is no slot number. */
1257 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1258 PSA_ERROR_INVALID_ARGUMENT );
1259
1260 /* Test setting a slot number. */
1261 psa_set_key_slot_number( &attributes, 0 );
1262 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1263 TEST_EQUAL( slot_number, 0 );
1264
1265 /* Test changing the slot number. */
1266 psa_set_key_slot_number( &attributes, 42 );
1267 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1268 TEST_EQUAL( slot_number, 42 );
1269
1270 /* Test clearing the slot number. */
1271 psa_clear_key_slot_number( &attributes );
1272 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1273 PSA_ERROR_INVALID_ARGUMENT );
1274
1275 /* Clearing again should have no effect. */
1276 psa_clear_key_slot_number( &attributes );
1277 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1278 PSA_ERROR_INVALID_ARGUMENT );
1279
1280 /* Test that reset clears the slot number. */
1281 psa_set_key_slot_number( &attributes, 42 );
1282 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1283 TEST_EQUAL( slot_number, 42 );
1284 psa_reset_key_attributes( &attributes );
1285 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1286 PSA_ERROR_INVALID_ARGUMENT );
1287}
1288/* END_CASE */
1289
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001290/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001291void import_with_policy( int type_arg,
1292 int usage_arg, int alg_arg,
1293 int expected_status_arg )
1294{
1295 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1296 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1297 psa_key_handle_t handle = 0;
1298 psa_key_type_t type = type_arg;
1299 psa_key_usage_t usage = usage_arg;
1300 psa_algorithm_t alg = alg_arg;
1301 psa_status_t expected_status = expected_status_arg;
1302 const uint8_t key_material[16] = {0};
1303 psa_status_t status;
1304
1305 PSA_ASSERT( psa_crypto_init( ) );
1306
1307 psa_set_key_type( &attributes, type );
1308 psa_set_key_usage_flags( &attributes, usage );
1309 psa_set_key_algorithm( &attributes, alg );
1310
1311 status = psa_import_key( &attributes,
1312 key_material, sizeof( key_material ),
1313 &handle );
1314 TEST_EQUAL( status, expected_status );
1315 if( status != PSA_SUCCESS )
1316 goto exit;
1317
1318 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1319 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1320 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1321 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001322 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001323
1324 PSA_ASSERT( psa_destroy_key( handle ) );
1325 test_operations_on_invalid_handle( handle );
1326
1327exit:
1328 psa_destroy_key( handle );
1329 psa_reset_key_attributes( &got_attributes );
1330 PSA_DONE( );
1331}
1332/* END_CASE */
1333
1334/* BEGIN_CASE */
1335void import_with_data( data_t *data, int type_arg,
1336 int attr_bits_arg,
1337 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001338{
1339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1340 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001341 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001342 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001343 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001344 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001345 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001346
Gilles Peskine8817f612018-12-18 00:18:46 +01001347 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001348
Gilles Peskine4747d192019-04-17 15:05:45 +02001349 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001350 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001351
Gilles Peskine73676cb2019-05-15 20:15:10 +02001352 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001353 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001354 if( status != PSA_SUCCESS )
1355 goto exit;
1356
1357 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1358 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001359 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001360 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001361 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001362
1363 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001364 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001365
1366exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001367 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001368 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001369 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370}
1371/* END_CASE */
1372
1373/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001374void import_large_key( int type_arg, int byte_size_arg,
1375 int expected_status_arg )
1376{
1377 psa_key_type_t type = type_arg;
1378 size_t byte_size = byte_size_arg;
1379 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1380 psa_status_t expected_status = expected_status_arg;
1381 psa_key_handle_t handle = 0;
1382 psa_status_t status;
1383 uint8_t *buffer = NULL;
1384 size_t buffer_size = byte_size + 1;
1385 size_t n;
1386
1387 /* It would be better to skip the test than fail it if the allocation
1388 * fails, but the test framework doesn't support this yet. */
1389 ASSERT_ALLOC( buffer, buffer_size );
1390 memset( buffer, 'K', byte_size );
1391
1392 PSA_ASSERT( psa_crypto_init( ) );
1393
1394 /* Try importing the key */
1395 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1396 psa_set_key_type( &attributes, type );
1397 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1398 TEST_EQUAL( status, expected_status );
1399
1400 if( status == PSA_SUCCESS )
1401 {
1402 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1403 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1404 TEST_EQUAL( psa_get_key_bits( &attributes ),
1405 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001406 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001407 memset( buffer, 0, byte_size + 1 );
1408 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1409 for( n = 0; n < byte_size; n++ )
1410 TEST_EQUAL( buffer[n], 'K' );
1411 for( n = byte_size; n < buffer_size; n++ )
1412 TEST_EQUAL( buffer[n], 0 );
1413 }
1414
1415exit:
1416 psa_destroy_key( handle );
1417 PSA_DONE( );
1418 mbedtls_free( buffer );
1419}
1420/* END_CASE */
1421
1422/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001423void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1424{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001425 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001426 size_t bits = bits_arg;
1427 psa_status_t expected_status = expected_status_arg;
1428 psa_status_t status;
1429 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001430 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001431 size_t buffer_size = /* Slight overapproximations */
1432 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001433 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001434 unsigned char *p;
1435 int ret;
1436 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001438
Gilles Peskine8817f612018-12-18 00:18:46 +01001439 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001440 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001441
1442 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1443 bits, keypair ) ) >= 0 );
1444 length = ret;
1445
1446 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001447 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001448 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001449 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001450
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001451 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001452 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001453
1454exit:
1455 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001456 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001457}
1458/* END_CASE */
1459
1460/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001461void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001462 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001463 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001464 int expected_bits,
1465 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001466 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001467 int canonical_input )
1468{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001469 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001470 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001471 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001472 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001473 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001474 unsigned char *exported = NULL;
1475 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001476 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001477 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001478 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001479 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001480 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001481
Moran Pekercb088e72018-07-17 17:36:59 +03001482 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001483 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001484 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001485 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001486 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001487
Gilles Peskine4747d192019-04-17 15:05:45 +02001488 psa_set_key_usage_flags( &attributes, usage_arg );
1489 psa_set_key_algorithm( &attributes, alg );
1490 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001491
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001492 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001493 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001494
1495 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001496 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1497 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1498 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001499 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001500
1501 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001502 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503 exported, export_size,
1504 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001505 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001506
1507 /* The exported length must be set by psa_export_key() to a value between 0
1508 * and export_size. On errors, the exported length must be 0. */
1509 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1510 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1511 TEST_ASSERT( exported_length <= export_size );
1512
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001513 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001514 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001515 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001516 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001517 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001518 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001519 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001520
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001521 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001522 goto exit;
1523
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001525 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001526 else
1527 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001528 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001529 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1530 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001531 PSA_ASSERT( psa_export_key( handle2,
1532 reexported,
1533 export_size,
1534 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001535 ASSERT_COMPARE( exported, exported_length,
1536 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001537 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001539 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540
1541destroy:
1542 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001543 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001544 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545
1546exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001547 mbedtls_free( exported );
1548 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001549 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001550 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001551}
1552/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001553
Moran Pekerf709f4a2018-06-06 17:26:04 +03001554/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001555void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001556 int type_arg,
1557 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001558 int export_size_delta,
1559 int expected_export_status_arg,
1560 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001561{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001562 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001563 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001564 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001565 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001566 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001567 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001568 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001569 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001571
Gilles Peskine8817f612018-12-18 00:18:46 +01001572 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001573
Gilles Peskine4747d192019-04-17 15:05:45 +02001574 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1575 psa_set_key_algorithm( &attributes, alg );
1576 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001577
1578 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001579 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001580
Gilles Peskine49c25912018-10-29 15:15:31 +01001581 /* Export the public key */
1582 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001583 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001584 exported, export_size,
1585 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001586 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001587 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001588 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001589 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001590 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001591 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1592 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001593 TEST_ASSERT( expected_public_key->len <=
1594 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001595 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1596 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001597 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001598
1599exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001600 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001601 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001602 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001603 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001604}
1605/* END_CASE */
1606
Gilles Peskine20035e32018-02-03 22:44:14 +01001607/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001608void import_and_exercise_key( data_t *data,
1609 int type_arg,
1610 int bits_arg,
1611 int alg_arg )
1612{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001613 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001614 psa_key_type_t type = type_arg;
1615 size_t bits = bits_arg;
1616 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001617 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001618 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001619 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001620
Gilles Peskine8817f612018-12-18 00:18:46 +01001621 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001622
Gilles Peskine4747d192019-04-17 15:05:45 +02001623 psa_set_key_usage_flags( &attributes, usage );
1624 psa_set_key_algorithm( &attributes, alg );
1625 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001626
1627 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001628 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001629
1630 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001631 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1632 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1633 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001634
1635 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001636 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001637 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001638
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001639 PSA_ASSERT( psa_destroy_key( handle ) );
1640 test_operations_on_invalid_handle( handle );
1641
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001642exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001643 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001644 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001645 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001646}
1647/* END_CASE */
1648
1649/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001650void effective_key_attributes( int type_arg, int expected_type_arg,
1651 int bits_arg, int expected_bits_arg,
1652 int usage_arg, int expected_usage_arg,
1653 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001654{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001655 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001656 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001657 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001658 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001659 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001660 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001661 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001662 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001663 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001665
Gilles Peskine8817f612018-12-18 00:18:46 +01001666 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001667
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001668 psa_set_key_usage_flags( &attributes, usage );
1669 psa_set_key_algorithm( &attributes, alg );
1670 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001671 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001672
Gilles Peskine1a960492019-11-26 17:12:21 +01001673 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1674 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001675
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001676 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001677 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1678 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1679 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1680 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001681
1682exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001683 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001684 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001685 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001686}
1687/* END_CASE */
1688
1689/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001690void check_key_policy( int type_arg, int bits_arg,
1691 int usage_arg, int alg_arg )
1692{
1693 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1694 usage_arg, usage_arg, alg_arg, alg_arg );
1695 goto exit;
1696}
1697/* END_CASE */
1698
1699/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001700void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001701{
1702 /* Test each valid way of initializing the object, except for `= {0}`, as
1703 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1704 * though it's OK by the C standard. We could test for this, but we'd need
1705 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001706 psa_key_attributes_t func = psa_key_attributes_init( );
1707 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1708 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001709
1710 memset( &zero, 0, sizeof( zero ) );
1711
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001712 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1713 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1714 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001715
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001716 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1717 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1718 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1719
1720 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1721 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1722 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1723
1724 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1725 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1726 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1727
1728 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1729 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1730 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001731}
1732/* END_CASE */
1733
1734/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001735void mac_key_policy( int policy_usage,
1736 int policy_alg,
1737 int key_type,
1738 data_t *key_data,
1739 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001740{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001741 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001742 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001743 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001744 psa_status_t status;
1745 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001746
Gilles Peskine8817f612018-12-18 00:18:46 +01001747 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001748
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001749 psa_set_key_usage_flags( &attributes, policy_usage );
1750 psa_set_key_algorithm( &attributes, policy_alg );
1751 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001752
Gilles Peskine049c7532019-05-15 20:22:09 +02001753 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1754 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001755
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001756 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001757 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001758 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001759 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001760 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001761 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001762 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001763
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001764 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001765 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001766 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001767 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001768 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001769 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001770 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001771
1772exit:
1773 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001774 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001775 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001776}
1777/* END_CASE */
1778
1779/* BEGIN_CASE */
1780void cipher_key_policy( int policy_usage,
1781 int policy_alg,
1782 int key_type,
1783 data_t *key_data,
1784 int exercise_alg )
1785{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001786 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001788 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001789 psa_status_t status;
1790
Gilles Peskine8817f612018-12-18 00:18:46 +01001791 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001792
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001793 psa_set_key_usage_flags( &attributes, policy_usage );
1794 psa_set_key_algorithm( &attributes, policy_alg );
1795 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001796
Gilles Peskine049c7532019-05-15 20:22:09 +02001797 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1798 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001799
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001800 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001801 if( policy_alg == exercise_alg &&
1802 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001803 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001804 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001805 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001806 psa_cipher_abort( &operation );
1807
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001808 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001809 if( policy_alg == exercise_alg &&
1810 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001811 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001812 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001813 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001814
1815exit:
1816 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001817 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001818 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001819}
1820/* END_CASE */
1821
1822/* BEGIN_CASE */
1823void aead_key_policy( int policy_usage,
1824 int policy_alg,
1825 int key_type,
1826 data_t *key_data,
1827 int nonce_length_arg,
1828 int tag_length_arg,
1829 int exercise_alg )
1830{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001831 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001832 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833 psa_status_t status;
1834 unsigned char nonce[16] = {0};
1835 size_t nonce_length = nonce_length_arg;
1836 unsigned char tag[16];
1837 size_t tag_length = tag_length_arg;
1838 size_t output_length;
1839
1840 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1841 TEST_ASSERT( tag_length <= sizeof( tag ) );
1842
Gilles Peskine8817f612018-12-18 00:18:46 +01001843 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001844
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001845 psa_set_key_usage_flags( &attributes, policy_usage );
1846 psa_set_key_algorithm( &attributes, policy_alg );
1847 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001848
Gilles Peskine049c7532019-05-15 20:22:09 +02001849 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1850 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001851
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001852 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001853 nonce, nonce_length,
1854 NULL, 0,
1855 NULL, 0,
1856 tag, tag_length,
1857 &output_length );
1858 if( policy_alg == exercise_alg &&
1859 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001860 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001861 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001862 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001863
1864 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001865 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001866 nonce, nonce_length,
1867 NULL, 0,
1868 tag, tag_length,
1869 NULL, 0,
1870 &output_length );
1871 if( policy_alg == exercise_alg &&
1872 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001873 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001874 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001875 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001876
1877exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001878 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001879 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001880}
1881/* END_CASE */
1882
1883/* BEGIN_CASE */
1884void asymmetric_encryption_key_policy( int policy_usage,
1885 int policy_alg,
1886 int key_type,
1887 data_t *key_data,
1888 int exercise_alg )
1889{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001890 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001892 psa_status_t status;
1893 size_t key_bits;
1894 size_t buffer_length;
1895 unsigned char *buffer = NULL;
1896 size_t output_length;
1897
Gilles Peskine8817f612018-12-18 00:18:46 +01001898 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001899
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001900 psa_set_key_usage_flags( &attributes, policy_usage );
1901 psa_set_key_algorithm( &attributes, policy_alg );
1902 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001903
Gilles Peskine049c7532019-05-15 20:22:09 +02001904 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1905 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001906
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001907 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1908 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001909 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1910 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001911 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001912
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001913 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914 NULL, 0,
1915 NULL, 0,
1916 buffer, buffer_length,
1917 &output_length );
1918 if( policy_alg == exercise_alg &&
1919 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001920 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001922 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001923
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001924 if( buffer_length != 0 )
1925 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001926 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001927 buffer, buffer_length,
1928 NULL, 0,
1929 buffer, buffer_length,
1930 &output_length );
1931 if( policy_alg == exercise_alg &&
1932 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001933 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001935 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001936
1937exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001938 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001939 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001940 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941 mbedtls_free( buffer );
1942}
1943/* END_CASE */
1944
1945/* BEGIN_CASE */
1946void asymmetric_signature_key_policy( int policy_usage,
1947 int policy_alg,
1948 int key_type,
1949 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001950 int exercise_alg,
1951 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001952{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001953 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001954 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001956 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1957 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1958 * compatible with the policy and `payload_length_arg` is supposed to be
1959 * a valid input length to sign. If `payload_length_arg <= 0`,
1960 * `exercise_alg` is supposed to be forbidden by the policy. */
1961 int compatible_alg = payload_length_arg > 0;
1962 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001963 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001964 size_t signature_length;
1965
Gilles Peskine8817f612018-12-18 00:18:46 +01001966 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001967
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001968 psa_set_key_usage_flags( &attributes, policy_usage );
1969 psa_set_key_algorithm( &attributes, policy_alg );
1970 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001971
Gilles Peskine049c7532019-05-15 20:22:09 +02001972 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1973 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001974
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001975 status = psa_sign_hash( handle, exercise_alg,
1976 payload, payload_length,
1977 signature, sizeof( signature ),
1978 &signature_length );
1979 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001980 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001981 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001982 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983
1984 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001985 status = psa_verify_hash( handle, exercise_alg,
1986 payload, payload_length,
1987 signature, sizeof( signature ) );
1988 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001989 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001991 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001992
1993exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001994 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001995 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001996}
1997/* END_CASE */
1998
Janos Follathba3fab92019-06-11 14:50:16 +01001999/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002000void derive_key_policy( int policy_usage,
2001 int policy_alg,
2002 int key_type,
2003 data_t *key_data,
2004 int exercise_alg )
2005{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002006 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002008 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002009 psa_status_t status;
2010
Gilles Peskine8817f612018-12-18 00:18:46 +01002011 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002012
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002013 psa_set_key_usage_flags( &attributes, policy_usage );
2014 psa_set_key_algorithm( &attributes, policy_alg );
2015 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002016
Gilles Peskine049c7532019-05-15 20:22:09 +02002017 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2018 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002019
Janos Follathba3fab92019-06-11 14:50:16 +01002020 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2021
2022 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2023 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002024 {
Janos Follathba3fab92019-06-11 14:50:16 +01002025 PSA_ASSERT( psa_key_derivation_input_bytes(
2026 &operation,
2027 PSA_KEY_DERIVATION_INPUT_SEED,
2028 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002029 }
Janos Follathba3fab92019-06-11 14:50:16 +01002030
2031 status = psa_key_derivation_input_key( &operation,
2032 PSA_KEY_DERIVATION_INPUT_SECRET,
2033 handle );
2034
Gilles Peskineea0fb492018-07-12 17:17:20 +02002035 if( policy_alg == exercise_alg &&
2036 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002037 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002038 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002039 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002040
2041exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002042 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002043 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002044 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002045}
2046/* END_CASE */
2047
2048/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002049void agreement_key_policy( int policy_usage,
2050 int policy_alg,
2051 int key_type_arg,
2052 data_t *key_data,
2053 int exercise_alg )
2054{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002055 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002057 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002058 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002059 psa_status_t status;
2060
Gilles Peskine8817f612018-12-18 00:18:46 +01002061 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002062
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002063 psa_set_key_usage_flags( &attributes, policy_usage );
2064 psa_set_key_algorithm( &attributes, policy_alg );
2065 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002066
Gilles Peskine049c7532019-05-15 20:22:09 +02002067 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2068 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002069
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002070 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2071 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002072
Gilles Peskine01d718c2018-09-18 12:01:02 +02002073 if( policy_alg == exercise_alg &&
2074 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002075 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002076 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002077 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002078
2079exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002080 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002081 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002082 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002083}
2084/* END_CASE */
2085
2086/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002087void key_policy_alg2( int key_type_arg, data_t *key_data,
2088 int usage_arg, int alg_arg, int alg2_arg )
2089{
2090 psa_key_handle_t handle = 0;
2091 psa_key_type_t key_type = key_type_arg;
2092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2093 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2094 psa_key_usage_t usage = usage_arg;
2095 psa_algorithm_t alg = alg_arg;
2096 psa_algorithm_t alg2 = alg2_arg;
2097
2098 PSA_ASSERT( psa_crypto_init( ) );
2099
2100 psa_set_key_usage_flags( &attributes, usage );
2101 psa_set_key_algorithm( &attributes, alg );
2102 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2103 psa_set_key_type( &attributes, key_type );
2104 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2105 &handle ) );
2106
2107 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2108 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2109 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2110 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2111
2112 if( ! exercise_key( handle, usage, alg ) )
2113 goto exit;
2114 if( ! exercise_key( handle, usage, alg2 ) )
2115 goto exit;
2116
2117exit:
2118 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002119 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002120}
2121/* END_CASE */
2122
2123/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002124void raw_agreement_key_policy( int policy_usage,
2125 int policy_alg,
2126 int key_type_arg,
2127 data_t *key_data,
2128 int exercise_alg )
2129{
2130 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002131 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002132 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002133 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002134 psa_status_t status;
2135
2136 PSA_ASSERT( psa_crypto_init( ) );
2137
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002138 psa_set_key_usage_flags( &attributes, policy_usage );
2139 psa_set_key_algorithm( &attributes, policy_alg );
2140 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002141
Gilles Peskine049c7532019-05-15 20:22:09 +02002142 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2143 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002144
2145 status = raw_key_agreement_with_self( exercise_alg, handle );
2146
2147 if( policy_alg == exercise_alg &&
2148 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2149 PSA_ASSERT( status );
2150 else
2151 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2152
2153exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002154 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002155 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002156 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002157}
2158/* END_CASE */
2159
2160/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002161void copy_success( int source_usage_arg,
2162 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002163 int type_arg, data_t *material,
2164 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002165 int target_usage_arg,
2166 int target_alg_arg, int target_alg2_arg,
2167 int expected_usage_arg,
2168 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002169{
Gilles Peskineca25db92019-04-19 11:43:08 +02002170 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2171 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002172 psa_key_usage_t expected_usage = expected_usage_arg;
2173 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002174 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002175 psa_key_handle_t source_handle = 0;
2176 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002177 uint8_t *export_buffer = NULL;
2178
Gilles Peskine57ab7212019-01-28 13:03:09 +01002179 PSA_ASSERT( psa_crypto_init( ) );
2180
Gilles Peskineca25db92019-04-19 11:43:08 +02002181 /* Prepare the source key. */
2182 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2183 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002184 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002185 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002186 PSA_ASSERT( psa_import_key( &source_attributes,
2187 material->x, material->len,
2188 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002189 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002190
Gilles Peskineca25db92019-04-19 11:43:08 +02002191 /* Prepare the target attributes. */
2192 if( copy_attributes )
2193 target_attributes = source_attributes;
2194 if( target_usage_arg != -1 )
2195 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2196 if( target_alg_arg != -1 )
2197 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002198 if( target_alg2_arg != -1 )
2199 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002200
2201 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002202 PSA_ASSERT( psa_copy_key( source_handle,
2203 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002204
2205 /* Destroy the source to ensure that this doesn't affect the target. */
2206 PSA_ASSERT( psa_destroy_key( source_handle ) );
2207
2208 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002209 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2210 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2211 psa_get_key_type( &target_attributes ) );
2212 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2213 psa_get_key_bits( &target_attributes ) );
2214 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2215 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002216 TEST_EQUAL( expected_alg2,
2217 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002218 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2219 {
2220 size_t length;
2221 ASSERT_ALLOC( export_buffer, material->len );
2222 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2223 material->len, &length ) );
2224 ASSERT_COMPARE( material->x, material->len,
2225 export_buffer, length );
2226 }
2227 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2228 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002229 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2230 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002231
2232 PSA_ASSERT( psa_close_key( target_handle ) );
2233
2234exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002235 psa_reset_key_attributes( &source_attributes );
2236 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002237 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002238 mbedtls_free( export_buffer );
2239}
2240/* END_CASE */
2241
2242/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002243void copy_fail( int source_usage_arg,
2244 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002245 int type_arg, data_t *material,
2246 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002247 int target_usage_arg,
2248 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002249 int expected_status_arg )
2250{
2251 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2252 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2253 psa_key_handle_t source_handle = 0;
2254 psa_key_handle_t target_handle = 0;
2255
2256 PSA_ASSERT( psa_crypto_init( ) );
2257
2258 /* Prepare the source key. */
2259 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2260 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002261 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002262 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002263 PSA_ASSERT( psa_import_key( &source_attributes,
2264 material->x, material->len,
2265 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002266
2267 /* Prepare the target attributes. */
2268 psa_set_key_type( &target_attributes, target_type_arg );
2269 psa_set_key_bits( &target_attributes, target_bits_arg );
2270 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2271 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002272 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002273
2274 /* Try to copy the key. */
2275 TEST_EQUAL( psa_copy_key( source_handle,
2276 &target_attributes, &target_handle ),
2277 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002278
2279 PSA_ASSERT( psa_destroy_key( source_handle ) );
2280
Gilles Peskine4a644642019-05-03 17:14:08 +02002281exit:
2282 psa_reset_key_attributes( &source_attributes );
2283 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002284 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002285}
2286/* END_CASE */
2287
2288/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002289void hash_operation_init( )
2290{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002291 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002292 /* Test each valid way of initializing the object, except for `= {0}`, as
2293 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2294 * though it's OK by the C standard. We could test for this, but we'd need
2295 * to supress the Clang warning for the test. */
2296 psa_hash_operation_t func = psa_hash_operation_init( );
2297 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2298 psa_hash_operation_t zero;
2299
2300 memset( &zero, 0, sizeof( zero ) );
2301
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002302 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002303 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2304 PSA_ERROR_BAD_STATE );
2305 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2306 PSA_ERROR_BAD_STATE );
2307 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2308 PSA_ERROR_BAD_STATE );
2309
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002310 /* A default hash operation should be abortable without error. */
2311 PSA_ASSERT( psa_hash_abort( &func ) );
2312 PSA_ASSERT( psa_hash_abort( &init ) );
2313 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002314}
2315/* END_CASE */
2316
2317/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002318void hash_setup( int alg_arg,
2319 int expected_status_arg )
2320{
2321 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002322 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002323 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002324 psa_status_t status;
2325
Gilles Peskine8817f612018-12-18 00:18:46 +01002326 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002327
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002328 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002329 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002330
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002331 /* Whether setup succeeded or failed, abort must succeed. */
2332 PSA_ASSERT( psa_hash_abort( &operation ) );
2333
2334 /* If setup failed, reproduce the failure, so as to
2335 * test the resulting state of the operation object. */
2336 if( status != PSA_SUCCESS )
2337 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2338
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002339 /* Now the operation object should be reusable. */
2340#if defined(KNOWN_SUPPORTED_HASH_ALG)
2341 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2342 PSA_ASSERT( psa_hash_abort( &operation ) );
2343#endif
2344
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002346 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347}
2348/* END_CASE */
2349
2350/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002351void hash_bad_order( )
2352{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002353 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002354 unsigned char input[] = "";
2355 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002356 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002357 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2358 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2359 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002360 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002361 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002362 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002363
Gilles Peskine8817f612018-12-18 00:18:46 +01002364 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002365
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002366 /* Call setup twice in a row. */
2367 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2368 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2369 PSA_ERROR_BAD_STATE );
2370 PSA_ASSERT( psa_hash_abort( &operation ) );
2371
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002372 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002373 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002374 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002375 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002376
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002377 /* Call update after finish. */
2378 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2379 PSA_ASSERT( psa_hash_finish( &operation,
2380 hash, sizeof( hash ), &hash_len ) );
2381 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002382 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002383 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002384
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002385 /* Call verify without calling setup beforehand. */
2386 TEST_EQUAL( psa_hash_verify( &operation,
2387 valid_hash, sizeof( valid_hash ) ),
2388 PSA_ERROR_BAD_STATE );
2389 PSA_ASSERT( psa_hash_abort( &operation ) );
2390
2391 /* Call verify after finish. */
2392 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2393 PSA_ASSERT( psa_hash_finish( &operation,
2394 hash, sizeof( hash ), &hash_len ) );
2395 TEST_EQUAL( psa_hash_verify( &operation,
2396 valid_hash, sizeof( valid_hash ) ),
2397 PSA_ERROR_BAD_STATE );
2398 PSA_ASSERT( psa_hash_abort( &operation ) );
2399
2400 /* Call verify twice in a row. */
2401 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2402 PSA_ASSERT( psa_hash_verify( &operation,
2403 valid_hash, sizeof( valid_hash ) ) );
2404 TEST_EQUAL( psa_hash_verify( &operation,
2405 valid_hash, sizeof( valid_hash ) ),
2406 PSA_ERROR_BAD_STATE );
2407 PSA_ASSERT( psa_hash_abort( &operation ) );
2408
2409 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002410 TEST_EQUAL( psa_hash_finish( &operation,
2411 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002412 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002413 PSA_ASSERT( psa_hash_abort( &operation ) );
2414
2415 /* Call finish twice in a row. */
2416 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2417 PSA_ASSERT( psa_hash_finish( &operation,
2418 hash, sizeof( hash ), &hash_len ) );
2419 TEST_EQUAL( psa_hash_finish( &operation,
2420 hash, sizeof( hash ), &hash_len ),
2421 PSA_ERROR_BAD_STATE );
2422 PSA_ASSERT( psa_hash_abort( &operation ) );
2423
2424 /* Call finish after calling verify. */
2425 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2426 PSA_ASSERT( psa_hash_verify( &operation,
2427 valid_hash, sizeof( valid_hash ) ) );
2428 TEST_EQUAL( psa_hash_finish( &operation,
2429 hash, sizeof( hash ), &hash_len ),
2430 PSA_ERROR_BAD_STATE );
2431 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002432
2433exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002434 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002435}
2436/* END_CASE */
2437
itayzafrir27e69452018-11-01 14:26:34 +02002438/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2439void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002440{
2441 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002442 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2443 * appended to it */
2444 unsigned char hash[] = {
2445 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2446 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2447 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002448 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002449 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002450
Gilles Peskine8817f612018-12-18 00:18:46 +01002451 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002452
itayzafrir27e69452018-11-01 14:26:34 +02002453 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002454 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002455 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002456 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002457
itayzafrir27e69452018-11-01 14:26:34 +02002458 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002459 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002460 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002461 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002462
itayzafrir27e69452018-11-01 14:26:34 +02002463 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002464 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002465 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002466 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002467
itayzafrirec93d302018-10-18 18:01:10 +03002468exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002469 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002470}
2471/* END_CASE */
2472
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002473/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2474void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002475{
2476 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002477 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002478 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002479 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002480 size_t hash_len;
2481
Gilles Peskine8817f612018-12-18 00:18:46 +01002482 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002483
itayzafrir58028322018-10-25 10:22:01 +03002484 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002485 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002486 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002487 hash, expected_size - 1, &hash_len ),
2488 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002489
2490exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002491 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002492}
2493/* END_CASE */
2494
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002495/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2496void hash_clone_source_state( )
2497{
2498 psa_algorithm_t alg = PSA_ALG_SHA_256;
2499 unsigned char hash[PSA_HASH_MAX_SIZE];
2500 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2501 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2502 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2503 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2504 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2505 size_t hash_len;
2506
2507 PSA_ASSERT( psa_crypto_init( ) );
2508 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2509
2510 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2511 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2512 PSA_ASSERT( psa_hash_finish( &op_finished,
2513 hash, sizeof( hash ), &hash_len ) );
2514 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2515 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2516
2517 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2518 PSA_ERROR_BAD_STATE );
2519
2520 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2521 PSA_ASSERT( psa_hash_finish( &op_init,
2522 hash, sizeof( hash ), &hash_len ) );
2523 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2524 PSA_ASSERT( psa_hash_finish( &op_finished,
2525 hash, sizeof( hash ), &hash_len ) );
2526 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2527 PSA_ASSERT( psa_hash_finish( &op_aborted,
2528 hash, sizeof( hash ), &hash_len ) );
2529
2530exit:
2531 psa_hash_abort( &op_source );
2532 psa_hash_abort( &op_init );
2533 psa_hash_abort( &op_setup );
2534 psa_hash_abort( &op_finished );
2535 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002536 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002537}
2538/* END_CASE */
2539
2540/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2541void hash_clone_target_state( )
2542{
2543 psa_algorithm_t alg = PSA_ALG_SHA_256;
2544 unsigned char hash[PSA_HASH_MAX_SIZE];
2545 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2546 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2547 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2548 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2549 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2550 size_t hash_len;
2551
2552 PSA_ASSERT( psa_crypto_init( ) );
2553
2554 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2555 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2556 PSA_ASSERT( psa_hash_finish( &op_finished,
2557 hash, sizeof( hash ), &hash_len ) );
2558 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2559 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2560
2561 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2562 PSA_ASSERT( psa_hash_finish( &op_target,
2563 hash, sizeof( hash ), &hash_len ) );
2564
2565 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2566 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2567 PSA_ERROR_BAD_STATE );
2568 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2569 PSA_ERROR_BAD_STATE );
2570
2571exit:
2572 psa_hash_abort( &op_target );
2573 psa_hash_abort( &op_init );
2574 psa_hash_abort( &op_setup );
2575 psa_hash_abort( &op_finished );
2576 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002577 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002578}
2579/* END_CASE */
2580
itayzafrir58028322018-10-25 10:22:01 +03002581/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002582void mac_operation_init( )
2583{
Jaeden Amero252ef282019-02-15 14:05:35 +00002584 const uint8_t input[1] = { 0 };
2585
Jaeden Amero769ce272019-01-04 11:48:03 +00002586 /* Test each valid way of initializing the object, except for `= {0}`, as
2587 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2588 * though it's OK by the C standard. We could test for this, but we'd need
2589 * to supress the Clang warning for the test. */
2590 psa_mac_operation_t func = psa_mac_operation_init( );
2591 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2592 psa_mac_operation_t zero;
2593
2594 memset( &zero, 0, sizeof( zero ) );
2595
Jaeden Amero252ef282019-02-15 14:05:35 +00002596 /* A freshly-initialized MAC operation should not be usable. */
2597 TEST_EQUAL( psa_mac_update( &func,
2598 input, sizeof( input ) ),
2599 PSA_ERROR_BAD_STATE );
2600 TEST_EQUAL( psa_mac_update( &init,
2601 input, sizeof( input ) ),
2602 PSA_ERROR_BAD_STATE );
2603 TEST_EQUAL( psa_mac_update( &zero,
2604 input, sizeof( input ) ),
2605 PSA_ERROR_BAD_STATE );
2606
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002607 /* A default MAC operation should be abortable without error. */
2608 PSA_ASSERT( psa_mac_abort( &func ) );
2609 PSA_ASSERT( psa_mac_abort( &init ) );
2610 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002611}
2612/* END_CASE */
2613
2614/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002615void mac_setup( int key_type_arg,
2616 data_t *key,
2617 int alg_arg,
2618 int expected_status_arg )
2619{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002620 psa_key_type_t key_type = key_type_arg;
2621 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002622 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002623 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002624 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2625#if defined(KNOWN_SUPPORTED_MAC_ALG)
2626 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2627#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002628
Gilles Peskine8817f612018-12-18 00:18:46 +01002629 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002630
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002631 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2632 &operation, &status ) )
2633 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002634 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002635
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002636 /* The operation object should be reusable. */
2637#if defined(KNOWN_SUPPORTED_MAC_ALG)
2638 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2639 smoke_test_key_data,
2640 sizeof( smoke_test_key_data ),
2641 KNOWN_SUPPORTED_MAC_ALG,
2642 &operation, &status ) )
2643 goto exit;
2644 TEST_EQUAL( status, PSA_SUCCESS );
2645#endif
2646
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002647exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002648 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002649}
2650/* END_CASE */
2651
2652/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002653void mac_bad_order( )
2654{
2655 psa_key_handle_t handle = 0;
2656 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2657 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2658 const uint8_t key[] = {
2659 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2660 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2661 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002662 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002663 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2664 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2665 size_t sign_mac_length = 0;
2666 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2667 const uint8_t verify_mac[] = {
2668 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2669 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2670 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2671
2672 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002673 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002674 psa_set_key_algorithm( &attributes, alg );
2675 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002676
Gilles Peskine73676cb2019-05-15 20:15:10 +02002677 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002678
Jaeden Amero252ef282019-02-15 14:05:35 +00002679 /* Call update without calling setup beforehand. */
2680 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2681 PSA_ERROR_BAD_STATE );
2682 PSA_ASSERT( psa_mac_abort( &operation ) );
2683
2684 /* Call sign finish without calling setup beforehand. */
2685 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2686 &sign_mac_length),
2687 PSA_ERROR_BAD_STATE );
2688 PSA_ASSERT( psa_mac_abort( &operation ) );
2689
2690 /* Call verify finish without calling setup beforehand. */
2691 TEST_EQUAL( psa_mac_verify_finish( &operation,
2692 verify_mac, sizeof( verify_mac ) ),
2693 PSA_ERROR_BAD_STATE );
2694 PSA_ASSERT( psa_mac_abort( &operation ) );
2695
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002696 /* Call setup twice in a row. */
2697 PSA_ASSERT( psa_mac_sign_setup( &operation,
2698 handle, alg ) );
2699 TEST_EQUAL( psa_mac_sign_setup( &operation,
2700 handle, alg ),
2701 PSA_ERROR_BAD_STATE );
2702 PSA_ASSERT( psa_mac_abort( &operation ) );
2703
Jaeden Amero252ef282019-02-15 14:05:35 +00002704 /* Call update after sign finish. */
2705 PSA_ASSERT( psa_mac_sign_setup( &operation,
2706 handle, alg ) );
2707 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2708 PSA_ASSERT( psa_mac_sign_finish( &operation,
2709 sign_mac, sizeof( sign_mac ),
2710 &sign_mac_length ) );
2711 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2712 PSA_ERROR_BAD_STATE );
2713 PSA_ASSERT( psa_mac_abort( &operation ) );
2714
2715 /* Call update after verify finish. */
2716 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002717 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002718 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2719 PSA_ASSERT( psa_mac_verify_finish( &operation,
2720 verify_mac, sizeof( verify_mac ) ) );
2721 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2722 PSA_ERROR_BAD_STATE );
2723 PSA_ASSERT( psa_mac_abort( &operation ) );
2724
2725 /* Call sign finish twice in a row. */
2726 PSA_ASSERT( psa_mac_sign_setup( &operation,
2727 handle, alg ) );
2728 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2729 PSA_ASSERT( psa_mac_sign_finish( &operation,
2730 sign_mac, sizeof( sign_mac ),
2731 &sign_mac_length ) );
2732 TEST_EQUAL( psa_mac_sign_finish( &operation,
2733 sign_mac, sizeof( sign_mac ),
2734 &sign_mac_length ),
2735 PSA_ERROR_BAD_STATE );
2736 PSA_ASSERT( psa_mac_abort( &operation ) );
2737
2738 /* Call verify finish twice in a row. */
2739 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002740 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002741 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2742 PSA_ASSERT( psa_mac_verify_finish( &operation,
2743 verify_mac, sizeof( verify_mac ) ) );
2744 TEST_EQUAL( psa_mac_verify_finish( &operation,
2745 verify_mac, sizeof( verify_mac ) ),
2746 PSA_ERROR_BAD_STATE );
2747 PSA_ASSERT( psa_mac_abort( &operation ) );
2748
2749 /* Setup sign but try verify. */
2750 PSA_ASSERT( psa_mac_sign_setup( &operation,
2751 handle, alg ) );
2752 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2753 TEST_EQUAL( psa_mac_verify_finish( &operation,
2754 verify_mac, sizeof( verify_mac ) ),
2755 PSA_ERROR_BAD_STATE );
2756 PSA_ASSERT( psa_mac_abort( &operation ) );
2757
2758 /* Setup verify but try sign. */
2759 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002760 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002761 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2762 TEST_EQUAL( psa_mac_sign_finish( &operation,
2763 sign_mac, sizeof( sign_mac ),
2764 &sign_mac_length ),
2765 PSA_ERROR_BAD_STATE );
2766 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002767
Gilles Peskine76b29a72019-05-28 14:08:50 +02002768 PSA_ASSERT( psa_destroy_key( handle ) );
2769
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002770exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002771 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002772}
2773/* END_CASE */
2774
2775/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002776void mac_sign( int key_type_arg,
2777 data_t *key,
2778 int alg_arg,
2779 data_t *input,
2780 data_t *expected_mac )
2781{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002782 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002783 psa_key_type_t key_type = key_type_arg;
2784 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002785 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002786 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002787 /* Leave a little extra room in the output buffer. At the end of the
2788 * test, we'll check that the implementation didn't overwrite onto
2789 * this extra room. */
2790 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2791 size_t mac_buffer_size =
2792 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2793 size_t mac_length = 0;
2794
2795 memset( actual_mac, '+', sizeof( actual_mac ) );
2796 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2797 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2798
Gilles Peskine8817f612018-12-18 00:18:46 +01002799 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002800
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002801 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002802 psa_set_key_algorithm( &attributes, alg );
2803 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002804
Gilles Peskine73676cb2019-05-15 20:15:10 +02002805 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002806
2807 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_mac_sign_setup( &operation,
2809 handle, alg ) );
2810 PSA_ASSERT( psa_mac_update( &operation,
2811 input->x, input->len ) );
2812 PSA_ASSERT( psa_mac_sign_finish( &operation,
2813 actual_mac, mac_buffer_size,
2814 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002815
2816 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002817 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2818 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002819
2820 /* Verify that the end of the buffer is untouched. */
2821 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2822 sizeof( actual_mac ) - mac_length ) );
2823
2824exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002825 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002826 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002827}
2828/* END_CASE */
2829
2830/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002831void mac_verify( int key_type_arg,
2832 data_t *key,
2833 int alg_arg,
2834 data_t *input,
2835 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002836{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002837 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002838 psa_key_type_t key_type = key_type_arg;
2839 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002840 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002841 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002842
Gilles Peskine69c12672018-06-28 00:07:19 +02002843 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2844
Gilles Peskine8817f612018-12-18 00:18:46 +01002845 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002846
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002847 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002848 psa_set_key_algorithm( &attributes, alg );
2849 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002850
Gilles Peskine73676cb2019-05-15 20:15:10 +02002851 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002852
Gilles Peskine8817f612018-12-18 00:18:46 +01002853 PSA_ASSERT( psa_mac_verify_setup( &operation,
2854 handle, alg ) );
2855 PSA_ASSERT( psa_destroy_key( handle ) );
2856 PSA_ASSERT( psa_mac_update( &operation,
2857 input->x, input->len ) );
2858 PSA_ASSERT( psa_mac_verify_finish( &operation,
2859 expected_mac->x,
2860 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002861
2862exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002863 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002864 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002865}
2866/* END_CASE */
2867
2868/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002869void cipher_operation_init( )
2870{
Jaeden Ameroab439972019-02-15 14:12:05 +00002871 const uint8_t input[1] = { 0 };
2872 unsigned char output[1] = { 0 };
2873 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002874 /* Test each valid way of initializing the object, except for `= {0}`, as
2875 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2876 * though it's OK by the C standard. We could test for this, but we'd need
2877 * to supress the Clang warning for the test. */
2878 psa_cipher_operation_t func = psa_cipher_operation_init( );
2879 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2880 psa_cipher_operation_t zero;
2881
2882 memset( &zero, 0, sizeof( zero ) );
2883
Jaeden Ameroab439972019-02-15 14:12:05 +00002884 /* A freshly-initialized cipher operation should not be usable. */
2885 TEST_EQUAL( psa_cipher_update( &func,
2886 input, sizeof( input ),
2887 output, sizeof( output ),
2888 &output_length ),
2889 PSA_ERROR_BAD_STATE );
2890 TEST_EQUAL( psa_cipher_update( &init,
2891 input, sizeof( input ),
2892 output, sizeof( output ),
2893 &output_length ),
2894 PSA_ERROR_BAD_STATE );
2895 TEST_EQUAL( psa_cipher_update( &zero,
2896 input, sizeof( input ),
2897 output, sizeof( output ),
2898 &output_length ),
2899 PSA_ERROR_BAD_STATE );
2900
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002901 /* A default cipher operation should be abortable without error. */
2902 PSA_ASSERT( psa_cipher_abort( &func ) );
2903 PSA_ASSERT( psa_cipher_abort( &init ) );
2904 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002905}
2906/* END_CASE */
2907
2908/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002909void cipher_setup( int key_type_arg,
2910 data_t *key,
2911 int alg_arg,
2912 int expected_status_arg )
2913{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002914 psa_key_type_t key_type = key_type_arg;
2915 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002916 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002917 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002918 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002919#if defined(KNOWN_SUPPORTED_MAC_ALG)
2920 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2921#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002922
Gilles Peskine8817f612018-12-18 00:18:46 +01002923 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002924
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002925 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2926 &operation, &status ) )
2927 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002928 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002929
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002930 /* The operation object should be reusable. */
2931#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2932 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2933 smoke_test_key_data,
2934 sizeof( smoke_test_key_data ),
2935 KNOWN_SUPPORTED_CIPHER_ALG,
2936 &operation, &status ) )
2937 goto exit;
2938 TEST_EQUAL( status, PSA_SUCCESS );
2939#endif
2940
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002941exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002942 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002943}
2944/* END_CASE */
2945
2946/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002947void cipher_bad_order( )
2948{
2949 psa_key_handle_t handle = 0;
2950 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2951 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002952 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002953 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2954 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2955 const uint8_t key[] = {
2956 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2957 0xaa, 0xaa, 0xaa, 0xaa };
2958 const uint8_t text[] = {
2959 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2960 0xbb, 0xbb, 0xbb, 0xbb };
2961 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2962 size_t length = 0;
2963
2964 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002965 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2966 psa_set_key_algorithm( &attributes, alg );
2967 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02002968 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002969
2970
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002971 /* Call encrypt setup twice in a row. */
2972 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2973 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2974 PSA_ERROR_BAD_STATE );
2975 PSA_ASSERT( psa_cipher_abort( &operation ) );
2976
2977 /* Call decrypt setup twice in a row. */
2978 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2979 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2980 PSA_ERROR_BAD_STATE );
2981 PSA_ASSERT( psa_cipher_abort( &operation ) );
2982
Jaeden Ameroab439972019-02-15 14:12:05 +00002983 /* Generate an IV without calling setup beforehand. */
2984 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2985 buffer, sizeof( buffer ),
2986 &length ),
2987 PSA_ERROR_BAD_STATE );
2988 PSA_ASSERT( psa_cipher_abort( &operation ) );
2989
2990 /* Generate an IV twice in a row. */
2991 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2992 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2993 buffer, sizeof( buffer ),
2994 &length ) );
2995 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2996 buffer, sizeof( buffer ),
2997 &length ),
2998 PSA_ERROR_BAD_STATE );
2999 PSA_ASSERT( psa_cipher_abort( &operation ) );
3000
3001 /* Generate an IV after it's already set. */
3002 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3003 PSA_ASSERT( psa_cipher_set_iv( &operation,
3004 iv, sizeof( iv ) ) );
3005 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3006 buffer, sizeof( buffer ),
3007 &length ),
3008 PSA_ERROR_BAD_STATE );
3009 PSA_ASSERT( psa_cipher_abort( &operation ) );
3010
3011 /* Set an IV without calling setup beforehand. */
3012 TEST_EQUAL( psa_cipher_set_iv( &operation,
3013 iv, sizeof( iv ) ),
3014 PSA_ERROR_BAD_STATE );
3015 PSA_ASSERT( psa_cipher_abort( &operation ) );
3016
3017 /* Set an IV after it's already set. */
3018 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3019 PSA_ASSERT( psa_cipher_set_iv( &operation,
3020 iv, sizeof( iv ) ) );
3021 TEST_EQUAL( psa_cipher_set_iv( &operation,
3022 iv, sizeof( iv ) ),
3023 PSA_ERROR_BAD_STATE );
3024 PSA_ASSERT( psa_cipher_abort( &operation ) );
3025
3026 /* Set an IV after it's already generated. */
3027 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3028 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3029 buffer, sizeof( buffer ),
3030 &length ) );
3031 TEST_EQUAL( psa_cipher_set_iv( &operation,
3032 iv, sizeof( iv ) ),
3033 PSA_ERROR_BAD_STATE );
3034 PSA_ASSERT( psa_cipher_abort( &operation ) );
3035
3036 /* Call update without calling setup beforehand. */
3037 TEST_EQUAL( psa_cipher_update( &operation,
3038 text, sizeof( text ),
3039 buffer, sizeof( buffer ),
3040 &length ),
3041 PSA_ERROR_BAD_STATE );
3042 PSA_ASSERT( psa_cipher_abort( &operation ) );
3043
3044 /* Call update without an IV where an IV is required. */
3045 TEST_EQUAL( psa_cipher_update( &operation,
3046 text, sizeof( text ),
3047 buffer, sizeof( buffer ),
3048 &length ),
3049 PSA_ERROR_BAD_STATE );
3050 PSA_ASSERT( psa_cipher_abort( &operation ) );
3051
3052 /* Call update after finish. */
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_update( &operation,
3059 text, sizeof( text ),
3060 buffer, sizeof( buffer ),
3061 &length ),
3062 PSA_ERROR_BAD_STATE );
3063 PSA_ASSERT( psa_cipher_abort( &operation ) );
3064
3065 /* Call finish without calling setup beforehand. */
3066 TEST_EQUAL( psa_cipher_finish( &operation,
3067 buffer, sizeof( buffer ), &length ),
3068 PSA_ERROR_BAD_STATE );
3069 PSA_ASSERT( psa_cipher_abort( &operation ) );
3070
3071 /* Call finish without an IV where an IV is required. */
3072 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3073 /* Not calling update means we are encrypting an empty buffer, which is OK
3074 * for cipher modes with padding. */
3075 TEST_EQUAL( psa_cipher_finish( &operation,
3076 buffer, sizeof( buffer ), &length ),
3077 PSA_ERROR_BAD_STATE );
3078 PSA_ASSERT( psa_cipher_abort( &operation ) );
3079
3080 /* Call finish twice in a row. */
3081 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3082 PSA_ASSERT( psa_cipher_set_iv( &operation,
3083 iv, sizeof( iv ) ) );
3084 PSA_ASSERT( psa_cipher_finish( &operation,
3085 buffer, sizeof( buffer ), &length ) );
3086 TEST_EQUAL( psa_cipher_finish( &operation,
3087 buffer, sizeof( buffer ), &length ),
3088 PSA_ERROR_BAD_STATE );
3089 PSA_ASSERT( psa_cipher_abort( &operation ) );
3090
Gilles Peskine76b29a72019-05-28 14:08:50 +02003091 PSA_ASSERT( psa_destroy_key( handle ) );
3092
Jaeden Ameroab439972019-02-15 14:12:05 +00003093exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003094 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003095}
3096/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003097
Gilles Peskine50e586b2018-06-08 14:28:46 +02003098/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003099void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003100 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003101 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003102 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003103{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003104 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003105 psa_status_t status;
3106 psa_key_type_t key_type = key_type_arg;
3107 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003108 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003109 unsigned char *output = NULL;
3110 size_t output_buffer_size = 0;
3111 size_t function_output_length = 0;
3112 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003113 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003115
Gilles Peskine8817f612018-12-18 00:18:46 +01003116 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003117
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003118 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3119 psa_set_key_algorithm( &attributes, alg );
3120 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003121
Gilles Peskine73676cb2019-05-15 20:15:10 +02003122 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003123
Gilles Peskine8817f612018-12-18 00:18:46 +01003124 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3125 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003126
Gilles Peskine423005e2019-05-06 15:22:57 +02003127 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003128 output_buffer_size = ( (size_t) input->len +
3129 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003130 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003131
Gilles Peskine8817f612018-12-18 00:18:46 +01003132 PSA_ASSERT( psa_cipher_update( &operation,
3133 input->x, input->len,
3134 output, output_buffer_size,
3135 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003136 total_output_length += function_output_length;
3137 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003138 output + total_output_length,
3139 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003140 &function_output_length );
3141 total_output_length += function_output_length;
3142
Gilles Peskinefe11b722018-12-18 00:24:04 +01003143 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003144 if( expected_status == PSA_SUCCESS )
3145 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003146 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003147 ASSERT_COMPARE( expected_output->x, expected_output->len,
3148 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003149 }
3150
3151exit:
3152 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003153 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003154 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003155}
3156/* END_CASE */
3157
3158/* BEGIN_CASE */
3159void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003160 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003161 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003162 int first_part_size_arg,
3163 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003164 data_t *expected_output )
3165{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003166 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003167 psa_key_type_t key_type = key_type_arg;
3168 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003169 size_t first_part_size = first_part_size_arg;
3170 size_t output1_length = output1_length_arg;
3171 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003172 unsigned char *output = NULL;
3173 size_t output_buffer_size = 0;
3174 size_t function_output_length = 0;
3175 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003176 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003178
Gilles Peskine8817f612018-12-18 00:18:46 +01003179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003180
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003181 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3182 psa_set_key_algorithm( &attributes, alg );
3183 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003184
Gilles Peskine73676cb2019-05-15 20:15:10 +02003185 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003186
Gilles Peskine8817f612018-12-18 00:18:46 +01003187 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3188 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003189
Gilles Peskine423005e2019-05-06 15:22:57 +02003190 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003191 output_buffer_size = ( (size_t) input->len +
3192 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003193 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003194
Gilles Peskinee0866522019-02-19 19:44:00 +01003195 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003196 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3197 output, output_buffer_size,
3198 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003199 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003200 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_cipher_update( &operation,
3202 input->x + first_part_size,
3203 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003204 output + total_output_length,
3205 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003206 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003207 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003208 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003209 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003210 output + total_output_length,
3211 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003212 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003213 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003214 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003215
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003216 ASSERT_COMPARE( expected_output->x, expected_output->len,
3217 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003218
3219exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003220 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003221 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003222 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003223}
3224/* END_CASE */
3225
3226/* BEGIN_CASE */
3227void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003228 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003229 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003230 int first_part_size_arg,
3231 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003232 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003233{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003234 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003235
3236 psa_key_type_t key_type = key_type_arg;
3237 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003238 size_t first_part_size = first_part_size_arg;
3239 size_t output1_length = output1_length_arg;
3240 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003241 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003242 size_t output_buffer_size = 0;
3243 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003244 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003245 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003247
Gilles Peskine8817f612018-12-18 00:18:46 +01003248 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003249
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3251 psa_set_key_algorithm( &attributes, alg );
3252 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003253
Gilles Peskine73676cb2019-05-15 20:15:10 +02003254 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003255
Gilles Peskine8817f612018-12-18 00:18:46 +01003256 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3257 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003258
Gilles Peskine423005e2019-05-06 15:22:57 +02003259 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003260
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003261 output_buffer_size = ( (size_t) input->len +
3262 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003263 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003264
Gilles Peskinee0866522019-02-19 19:44:00 +01003265 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003266 PSA_ASSERT( psa_cipher_update( &operation,
3267 input->x, first_part_size,
3268 output, output_buffer_size,
3269 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003270 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003271 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003272 PSA_ASSERT( psa_cipher_update( &operation,
3273 input->x + first_part_size,
3274 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003275 output + total_output_length,
3276 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003277 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003278 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003279 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003280 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003281 output + total_output_length,
3282 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003283 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003284 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003285 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003286
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003287 ASSERT_COMPARE( expected_output->x, expected_output->len,
3288 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003289
3290exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003291 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003292 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003293 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003294}
3295/* END_CASE */
3296
Gilles Peskine50e586b2018-06-08 14:28:46 +02003297/* BEGIN_CASE */
3298void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003299 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003300 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003301 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003302{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003303 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003304 psa_status_t status;
3305 psa_key_type_t key_type = key_type_arg;
3306 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003307 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003308 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003309 size_t output_buffer_size = 0;
3310 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003311 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003312 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003314
Gilles Peskine8817f612018-12-18 00:18:46 +01003315 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003316
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003317 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3318 psa_set_key_algorithm( &attributes, alg );
3319 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003320
Gilles Peskine73676cb2019-05-15 20:15:10 +02003321 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003322
Gilles Peskine8817f612018-12-18 00:18:46 +01003323 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3324 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003325
Gilles Peskine423005e2019-05-06 15:22:57 +02003326 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003327
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003328 output_buffer_size = ( (size_t) input->len +
3329 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003330 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003331
Gilles Peskine8817f612018-12-18 00:18:46 +01003332 PSA_ASSERT( psa_cipher_update( &operation,
3333 input->x, input->len,
3334 output, output_buffer_size,
3335 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003336 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003337 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003338 output + total_output_length,
3339 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003340 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003341 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003342 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003343
3344 if( expected_status == PSA_SUCCESS )
3345 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003346 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003347 ASSERT_COMPARE( expected_output->x, expected_output->len,
3348 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003349 }
3350
Gilles Peskine50e586b2018-06-08 14:28:46 +02003351exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003352 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003353 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003354 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003355}
3356/* END_CASE */
3357
Gilles Peskine50e586b2018-06-08 14:28:46 +02003358/* BEGIN_CASE */
3359void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003360 data_t *key,
3361 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003362{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003363 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003364 psa_key_type_t key_type = key_type_arg;
3365 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003366 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003367 size_t iv_size = 16;
3368 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003369 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003370 size_t output1_size = 0;
3371 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003372 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003373 size_t output2_size = 0;
3374 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003375 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003376 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3377 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003379
Gilles Peskine8817f612018-12-18 00:18:46 +01003380 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003381
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003382 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3383 psa_set_key_algorithm( &attributes, alg );
3384 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003385
Gilles Peskine73676cb2019-05-15 20:15:10 +02003386 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003387
Gilles Peskine8817f612018-12-18 00:18:46 +01003388 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3389 handle, alg ) );
3390 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3391 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003392
Gilles Peskine8817f612018-12-18 00:18:46 +01003393 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3394 iv, iv_size,
3395 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003396 output1_size = ( (size_t) input->len +
3397 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003398 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003399
Gilles Peskine8817f612018-12-18 00:18:46 +01003400 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3401 output1, output1_size,
3402 &output1_length ) );
3403 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003404 output1 + output1_length,
3405 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003406 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003407
Gilles Peskine048b7f02018-06-08 14:20:49 +02003408 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003409
Gilles Peskine8817f612018-12-18 00:18:46 +01003410 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003411
3412 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003413 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003414
Gilles Peskine8817f612018-12-18 00:18:46 +01003415 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3416 iv, iv_length ) );
3417 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3418 output2, output2_size,
3419 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003420 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003421 PSA_ASSERT( psa_cipher_finish( &operation2,
3422 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003423 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003424 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003425
Gilles Peskine048b7f02018-06-08 14:20:49 +02003426 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003427
Gilles Peskine8817f612018-12-18 00:18:46 +01003428 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003429
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003430 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003431
3432exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003433 mbedtls_free( output1 );
3434 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003435 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003436 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003437}
3438/* END_CASE */
3439
3440/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003441void cipher_verify_output_multipart( int alg_arg,
3442 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003443 data_t *key,
3444 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003445 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003446{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003447 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003448 psa_key_type_t key_type = key_type_arg;
3449 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003450 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003451 unsigned char iv[16] = {0};
3452 size_t iv_size = 16;
3453 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003454 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003455 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003456 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003457 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003458 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003459 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003460 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003461 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3462 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003463 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003464
Gilles Peskine8817f612018-12-18 00:18:46 +01003465 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003466
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3468 psa_set_key_algorithm( &attributes, alg );
3469 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003470
Gilles Peskine73676cb2019-05-15 20:15:10 +02003471 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003472
Gilles Peskine8817f612018-12-18 00:18:46 +01003473 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3474 handle, alg ) );
3475 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3476 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003477
Gilles Peskine8817f612018-12-18 00:18:46 +01003478 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3479 iv, iv_size,
3480 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003481 output1_buffer_size = ( (size_t) input->len +
3482 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003483 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003484
Gilles Peskinee0866522019-02-19 19:44:00 +01003485 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003486
Gilles Peskine8817f612018-12-18 00:18:46 +01003487 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3488 output1, output1_buffer_size,
3489 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003490 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003491
Gilles Peskine8817f612018-12-18 00:18:46 +01003492 PSA_ASSERT( psa_cipher_update( &operation1,
3493 input->x + first_part_size,
3494 input->len - first_part_size,
3495 output1, output1_buffer_size,
3496 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003497 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003498
Gilles Peskine8817f612018-12-18 00:18:46 +01003499 PSA_ASSERT( psa_cipher_finish( &operation1,
3500 output1 + output1_length,
3501 output1_buffer_size - output1_length,
3502 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003503 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003504
Gilles Peskine8817f612018-12-18 00:18:46 +01003505 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003506
Gilles Peskine048b7f02018-06-08 14:20:49 +02003507 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003508 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003509
Gilles Peskine8817f612018-12-18 00:18:46 +01003510 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3511 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003512
Gilles Peskine8817f612018-12-18 00:18:46 +01003513 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3514 output2, output2_buffer_size,
3515 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003516 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003517
Gilles Peskine8817f612018-12-18 00:18:46 +01003518 PSA_ASSERT( psa_cipher_update( &operation2,
3519 output1 + first_part_size,
3520 output1_length - first_part_size,
3521 output2, output2_buffer_size,
3522 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003523 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003524
Gilles Peskine8817f612018-12-18 00:18:46 +01003525 PSA_ASSERT( psa_cipher_finish( &operation2,
3526 output2 + output2_length,
3527 output2_buffer_size - output2_length,
3528 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003529 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003530
Gilles Peskine8817f612018-12-18 00:18:46 +01003531 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003532
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003533 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003534
3535exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003536 mbedtls_free( output1 );
3537 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003538 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003539 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003540}
3541/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003542
Gilles Peskine20035e32018-02-03 22:44:14 +01003543/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003544void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003545 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003546 data_t *nonce,
3547 data_t *additional_data,
3548 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003549 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003550{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003551 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003552 psa_key_type_t key_type = key_type_arg;
3553 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003554 unsigned char *output_data = NULL;
3555 size_t output_size = 0;
3556 size_t output_length = 0;
3557 unsigned char *output_data2 = NULL;
3558 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003559 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003560 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003561 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003562
Gilles Peskine4abf7412018-06-18 16:35:34 +02003563 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003564 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3565 * should be exact. */
3566 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3567 TEST_EQUAL( output_size,
3568 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003569 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003570
Gilles Peskine8817f612018-12-18 00:18:46 +01003571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003572
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003573 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3574 psa_set_key_algorithm( &attributes, alg );
3575 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003576
Gilles Peskine049c7532019-05-15 20:22:09 +02003577 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3578 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003579
Gilles Peskinefe11b722018-12-18 00:24:04 +01003580 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3581 nonce->x, nonce->len,
3582 additional_data->x,
3583 additional_data->len,
3584 input_data->x, input_data->len,
3585 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003586 &output_length ),
3587 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003588
3589 if( PSA_SUCCESS == expected_result )
3590 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003591 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003592
Gilles Peskine003a4a92019-05-14 16:09:40 +02003593 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3594 * should be exact. */
3595 TEST_EQUAL( input_data->len,
3596 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3597
Gilles Peskinefe11b722018-12-18 00:24:04 +01003598 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3599 nonce->x, nonce->len,
3600 additional_data->x,
3601 additional_data->len,
3602 output_data, output_length,
3603 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003604 &output_length2 ),
3605 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003606
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003607 ASSERT_COMPARE( input_data->x, input_data->len,
3608 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003609 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003610
Gilles Peskinea1cac842018-06-11 19:33:02 +02003611exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003612 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003613 mbedtls_free( output_data );
3614 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003615 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003616}
3617/* END_CASE */
3618
3619/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003620void aead_encrypt( int key_type_arg, data_t *key_data,
3621 int alg_arg,
3622 data_t *nonce,
3623 data_t *additional_data,
3624 data_t *input_data,
3625 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003626{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003627 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003628 psa_key_type_t key_type = key_type_arg;
3629 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003630 unsigned char *output_data = NULL;
3631 size_t output_size = 0;
3632 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003633 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003635
Gilles Peskine4abf7412018-06-18 16:35:34 +02003636 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003637 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3638 * should be exact. */
3639 TEST_EQUAL( output_size,
3640 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003641 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003642
Gilles Peskine8817f612018-12-18 00:18:46 +01003643 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003644
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003645 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3646 psa_set_key_algorithm( &attributes, alg );
3647 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003648
Gilles Peskine049c7532019-05-15 20:22:09 +02003649 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3650 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003651
Gilles Peskine8817f612018-12-18 00:18:46 +01003652 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3653 nonce->x, nonce->len,
3654 additional_data->x, additional_data->len,
3655 input_data->x, input_data->len,
3656 output_data, output_size,
3657 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003658
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003659 ASSERT_COMPARE( expected_result->x, expected_result->len,
3660 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003661
Gilles Peskinea1cac842018-06-11 19:33:02 +02003662exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003663 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003664 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003665 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003666}
3667/* END_CASE */
3668
3669/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003670void aead_decrypt( int key_type_arg, data_t *key_data,
3671 int alg_arg,
3672 data_t *nonce,
3673 data_t *additional_data,
3674 data_t *input_data,
3675 data_t *expected_data,
3676 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003677{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003678 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003679 psa_key_type_t key_type = key_type_arg;
3680 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003681 unsigned char *output_data = NULL;
3682 size_t output_size = 0;
3683 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003684 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003686 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003687
Gilles Peskine003a4a92019-05-14 16:09:40 +02003688 output_size = input_data->len - tag_length;
3689 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3690 * should be exact. */
3691 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3692 TEST_EQUAL( output_size,
3693 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003694 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003695
Gilles Peskine8817f612018-12-18 00:18:46 +01003696 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003697
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003698 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3699 psa_set_key_algorithm( &attributes, alg );
3700 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003701
Gilles Peskine049c7532019-05-15 20:22:09 +02003702 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3703 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003704
Gilles Peskinefe11b722018-12-18 00:24:04 +01003705 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3706 nonce->x, nonce->len,
3707 additional_data->x,
3708 additional_data->len,
3709 input_data->x, input_data->len,
3710 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003711 &output_length ),
3712 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003713
Gilles Peskine2d277862018-06-18 15:41:12 +02003714 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003715 ASSERT_COMPARE( expected_data->x, expected_data->len,
3716 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003717
Gilles Peskinea1cac842018-06-11 19:33:02 +02003718exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003719 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003720 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003721 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003722}
3723/* END_CASE */
3724
3725/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003726void signature_size( int type_arg,
3727 int bits,
3728 int alg_arg,
3729 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003730{
3731 psa_key_type_t type = type_arg;
3732 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003733 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003734
Gilles Peskinefe11b722018-12-18 00:24:04 +01003735 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003736#if defined(MBEDTLS_TEST_DEPRECATED)
3737 TEST_EQUAL( actual_size,
3738 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3739#endif /* MBEDTLS_TEST_DEPRECATED */
3740
Gilles Peskinee59236f2018-01-27 23:32:46 +01003741exit:
3742 ;
3743}
3744/* END_CASE */
3745
3746/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003747void sign_deterministic( int key_type_arg, data_t *key_data,
3748 int alg_arg, data_t *input_data,
3749 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003750{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003751 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003752 psa_key_type_t key_type = key_type_arg;
3753 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003754 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003755 unsigned char *signature = NULL;
3756 size_t signature_size;
3757 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003759
Gilles Peskine8817f612018-12-18 00:18:46 +01003760 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003761
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003762 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003763 psa_set_key_algorithm( &attributes, alg );
3764 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003765
Gilles Peskine049c7532019-05-15 20:22:09 +02003766 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3767 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003768 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3769 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003770
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003771 /* Allocate a buffer which has the size advertized by the
3772 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003773 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003774 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003775 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003776 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003777 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003778
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003779 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003780 PSA_ASSERT( psa_sign_hash( handle, alg,
3781 input_data->x, input_data->len,
3782 signature, signature_size,
3783 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003784 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003785 ASSERT_COMPARE( output_data->x, output_data->len,
3786 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003787
Gilles Peskine0627f982019-11-26 19:12:16 +01003788#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003789 memset( signature, 0, signature_size );
3790 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01003791 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3792 input_data->x, input_data->len,
3793 signature, signature_size,
3794 &signature_length ) );
3795 ASSERT_COMPARE( output_data->x, output_data->len,
3796 signature, signature_length );
3797#endif /* MBEDTLS_TEST_DEPRECATED */
3798
Gilles Peskine20035e32018-02-03 22:44:14 +01003799exit:
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 Peskine0189e752018-02-03 23:57:22 +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 */
3806
3807/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003808void sign_fail( int key_type_arg, data_t *key_data,
3809 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003810 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003811{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003812 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003813 psa_key_type_t key_type = key_type_arg;
3814 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003815 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003816 psa_status_t actual_status;
3817 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003818 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003819 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003821
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003822 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003823
Gilles Peskine8817f612018-12-18 00:18:46 +01003824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003825
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003827 psa_set_key_algorithm( &attributes, alg );
3828 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003829
Gilles Peskine049c7532019-05-15 20:22:09 +02003830 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3831 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003832
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003833 actual_status = psa_sign_hash( handle, alg,
3834 input_data->x, input_data->len,
3835 signature, signature_size,
3836 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003837 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003838 /* The value of *signature_length is unspecified on error, but
3839 * whatever it is, it should be less than signature_size, so that
3840 * if the caller tries to read *signature_length bytes without
3841 * checking the error code then they don't overflow a buffer. */
3842 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003843
Gilles Peskine895242b2019-11-29 12:15:40 +01003844#if defined(MBEDTLS_TEST_DEPRECATED)
3845 signature_length = INVALID_EXPORT_LENGTH;
3846 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
3847 input_data->x, input_data->len,
3848 signature, signature_size,
3849 &signature_length ),
3850 expected_status );
3851 TEST_ASSERT( signature_length <= signature_size );
3852#endif /* MBEDTLS_TEST_DEPRECATED */
3853
Gilles Peskine20035e32018-02-03 22:44:14 +01003854exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003855 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003856 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003857 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003858 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003859}
3860/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003861
3862/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003863void sign_verify( int key_type_arg, data_t *key_data,
3864 int alg_arg, data_t *input_data )
3865{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003866 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003867 psa_key_type_t key_type = key_type_arg;
3868 psa_algorithm_t alg = alg_arg;
3869 size_t key_bits;
3870 unsigned char *signature = NULL;
3871 size_t signature_size;
3872 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003874
Gilles Peskine8817f612018-12-18 00:18:46 +01003875 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003876
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003877 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003878 psa_set_key_algorithm( &attributes, alg );
3879 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003880
Gilles Peskine049c7532019-05-15 20:22:09 +02003881 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3882 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003883 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3884 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003885
3886 /* Allocate a buffer which has the size advertized by the
3887 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003888 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003889 key_bits, alg );
3890 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003891 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003892 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003893
3894 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003895 PSA_ASSERT( psa_sign_hash( handle, alg,
3896 input_data->x, input_data->len,
3897 signature, signature_size,
3898 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003899 /* Check that the signature length looks sensible. */
3900 TEST_ASSERT( signature_length <= signature_size );
3901 TEST_ASSERT( signature_length > 0 );
3902
3903 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003904 PSA_ASSERT( psa_verify_hash( handle, alg,
3905 input_data->x, input_data->len,
3906 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003907
3908 if( input_data->len != 0 )
3909 {
3910 /* Flip a bit in the input and verify that the signature is now
3911 * detected as invalid. Flip a bit at the beginning, not at the end,
3912 * because ECDSA may ignore the last few bits of the input. */
3913 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003914 TEST_EQUAL( psa_verify_hash( handle, alg,
3915 input_data->x, input_data->len,
3916 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003917 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003918 }
3919
3920exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003921 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003922 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003923 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003924 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003925}
3926/* END_CASE */
3927
3928/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003929void asymmetric_verify( int key_type_arg, data_t *key_data,
3930 int alg_arg, data_t *hash_data,
3931 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003932{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003933 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003934 psa_key_type_t key_type = key_type_arg;
3935 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003936 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003937
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003938 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003939
Gilles Peskine8817f612018-12-18 00:18:46 +01003940 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003941
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003942 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003943 psa_set_key_algorithm( &attributes, alg );
3944 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003945
Gilles Peskine049c7532019-05-15 20:22:09 +02003946 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3947 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003948
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003949 PSA_ASSERT( psa_verify_hash( handle, alg,
3950 hash_data->x, hash_data->len,
3951 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003952
3953#if defined(MBEDTLS_TEST_DEPRECATED)
3954 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3955 hash_data->x, hash_data->len,
3956 signature_data->x,
3957 signature_data->len ) );
3958
3959#endif /* MBEDTLS_TEST_DEPRECATED */
3960
itayzafrir5c753392018-05-08 11:18:38 +03003961exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003962 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003963 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003964 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003965}
3966/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003967
3968/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003969void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3970 int alg_arg, data_t *hash_data,
3971 data_t *signature_data,
3972 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003973{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003974 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003975 psa_key_type_t key_type = key_type_arg;
3976 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977 psa_status_t actual_status;
3978 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980
Gilles Peskine8817f612018-12-18 00:18:46 +01003981 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003982
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003983 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003984 psa_set_key_algorithm( &attributes, alg );
3985 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003986
Gilles Peskine049c7532019-05-15 20:22:09 +02003987 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3988 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003989
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003990 actual_status = psa_verify_hash( handle, alg,
3991 hash_data->x, hash_data->len,
3992 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003993 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003994
Gilles Peskine895242b2019-11-29 12:15:40 +01003995#if defined(MBEDTLS_TEST_DEPRECATED)
3996 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3997 hash_data->x, hash_data->len,
3998 signature_data->x, signature_data->len ),
3999 expected_status );
4000#endif /* MBEDTLS_TEST_DEPRECATED */
4001
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004002exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004003 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004004 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004005 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004006}
4007/* END_CASE */
4008
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004009/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004010void asymmetric_encrypt( int key_type_arg,
4011 data_t *key_data,
4012 int alg_arg,
4013 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004014 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004015 int expected_output_length_arg,
4016 int expected_status_arg )
4017{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004018 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004019 psa_key_type_t key_type = key_type_arg;
4020 psa_algorithm_t alg = alg_arg;
4021 size_t expected_output_length = expected_output_length_arg;
4022 size_t key_bits;
4023 unsigned char *output = NULL;
4024 size_t output_size;
4025 size_t output_length = ~0;
4026 psa_status_t actual_status;
4027 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004029
Gilles Peskine8817f612018-12-18 00:18:46 +01004030 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004031
Gilles Peskine656896e2018-06-29 19:12:28 +02004032 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4034 psa_set_key_algorithm( &attributes, alg );
4035 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004036 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4037 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004038
4039 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004040 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4041 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004042 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004043 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004044
4045 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004046 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004047 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004048 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004049 output, output_size,
4050 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004051 TEST_EQUAL( actual_status, expected_status );
4052 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004053
Gilles Peskine68428122018-06-30 18:42:41 +02004054 /* If the label is empty, the test framework puts a non-null pointer
4055 * in label->x. Test that a null pointer works as well. */
4056 if( label->len == 0 )
4057 {
4058 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004059 if( output_size != 0 )
4060 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004061 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004062 input_data->x, input_data->len,
4063 NULL, label->len,
4064 output, output_size,
4065 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004066 TEST_EQUAL( actual_status, expected_status );
4067 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004068 }
4069
Gilles Peskine656896e2018-06-29 19:12:28 +02004070exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004071 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004072 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004073 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004074 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004075}
4076/* END_CASE */
4077
4078/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004079void asymmetric_encrypt_decrypt( int key_type_arg,
4080 data_t *key_data,
4081 int alg_arg,
4082 data_t *input_data,
4083 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004084{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004085 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004086 psa_key_type_t key_type = key_type_arg;
4087 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004088 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004089 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004090 size_t output_size;
4091 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004092 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004093 size_t output2_size;
4094 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004095 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004096
Gilles Peskine8817f612018-12-18 00:18:46 +01004097 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004098
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004099 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4100 psa_set_key_algorithm( &attributes, alg );
4101 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004102
Gilles Peskine049c7532019-05-15 20:22:09 +02004103 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4104 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004105
4106 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004107 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4108 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004109 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004110 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004111 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004112 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004113
Gilles Peskineeebd7382018-06-08 18:11:54 +02004114 /* We test encryption by checking that encrypt-then-decrypt gives back
4115 * the original plaintext because of the non-optional random
4116 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004117 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4118 input_data->x, input_data->len,
4119 label->x, label->len,
4120 output, output_size,
4121 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004122 /* We don't know what ciphertext length to expect, but check that
4123 * it looks sensible. */
4124 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004125
Gilles Peskine8817f612018-12-18 00:18:46 +01004126 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4127 output, output_length,
4128 label->x, label->len,
4129 output2, output2_size,
4130 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004131 ASSERT_COMPARE( input_data->x, input_data->len,
4132 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004133
4134exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004135 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004136 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004137 mbedtls_free( output );
4138 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004139 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004140}
4141/* END_CASE */
4142
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004143/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004144void asymmetric_decrypt( int key_type_arg,
4145 data_t *key_data,
4146 int alg_arg,
4147 data_t *input_data,
4148 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004149 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004150{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004151 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004152 psa_key_type_t key_type = key_type_arg;
4153 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004154 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004155 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004156 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004158
Jaeden Amero412654a2019-02-06 12:57:46 +00004159 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004160 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004161
Gilles Peskine8817f612018-12-18 00:18:46 +01004162 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004163
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004164 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4165 psa_set_key_algorithm( &attributes, alg );
4166 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004167
Gilles Peskine049c7532019-05-15 20:22:09 +02004168 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4169 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004170
Gilles Peskine8817f612018-12-18 00:18:46 +01004171 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4172 input_data->x, input_data->len,
4173 label->x, label->len,
4174 output,
4175 output_size,
4176 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004177 ASSERT_COMPARE( expected_data->x, expected_data->len,
4178 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004179
Gilles Peskine68428122018-06-30 18:42:41 +02004180 /* If the label is empty, the test framework puts a non-null pointer
4181 * in label->x. Test that a null pointer works as well. */
4182 if( label->len == 0 )
4183 {
4184 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004185 if( output_size != 0 )
4186 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004187 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4188 input_data->x, input_data->len,
4189 NULL, label->len,
4190 output,
4191 output_size,
4192 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004193 ASSERT_COMPARE( expected_data->x, expected_data->len,
4194 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004195 }
4196
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004197exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004198 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004199 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004200 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004201 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004202}
4203/* END_CASE */
4204
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004205/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004206void asymmetric_decrypt_fail( int key_type_arg,
4207 data_t *key_data,
4208 int alg_arg,
4209 data_t *input_data,
4210 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004211 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004212 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004213{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004214 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004215 psa_key_type_t key_type = key_type_arg;
4216 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004217 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004218 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004219 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004220 psa_status_t actual_status;
4221 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004222 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004223
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004224 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004225
Gilles Peskine8817f612018-12-18 00:18:46 +01004226 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004227
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004228 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4229 psa_set_key_algorithm( &attributes, alg );
4230 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004231
Gilles Peskine049c7532019-05-15 20:22:09 +02004232 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4233 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004234
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004235 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004236 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004237 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004238 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004239 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004240 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004241 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004242
Gilles Peskine68428122018-06-30 18:42:41 +02004243 /* If the label is empty, the test framework puts a non-null pointer
4244 * in label->x. Test that a null pointer works as well. */
4245 if( label->len == 0 )
4246 {
4247 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004248 if( output_size != 0 )
4249 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004250 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004251 input_data->x, input_data->len,
4252 NULL, label->len,
4253 output, output_size,
4254 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004255 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004256 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004257 }
4258
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004259exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004260 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004261 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004262 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004263 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004264}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004265/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004266
4267/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004268void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004269{
4270 /* Test each valid way of initializing the object, except for `= {0}`, as
4271 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4272 * though it's OK by the C standard. We could test for this, but we'd need
4273 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004274 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004275 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4276 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4277 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004278
4279 memset( &zero, 0, sizeof( zero ) );
4280
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004281 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004282 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004283 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004284 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004285 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004286 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004287 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004288
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004289 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004290 PSA_ASSERT( psa_key_derivation_abort(&func) );
4291 PSA_ASSERT( psa_key_derivation_abort(&init) );
4292 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004293}
4294/* END_CASE */
4295
Janos Follath16de4a42019-06-13 16:32:24 +01004296/* BEGIN_CASE */
4297void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004298{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004299 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004300 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004301 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004302
Gilles Peskine8817f612018-12-18 00:18:46 +01004303 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004304
Janos Follath16de4a42019-06-13 16:32:24 +01004305 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004306 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004307
4308exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004309 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004310 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004311}
4312/* END_CASE */
4313
Janos Follathaf3c2a02019-06-12 12:34:34 +01004314/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004315void derive_set_capacity( int alg_arg, int capacity_arg,
4316 int expected_status_arg )
4317{
4318 psa_algorithm_t alg = alg_arg;
4319 size_t capacity = capacity_arg;
4320 psa_status_t expected_status = expected_status_arg;
4321 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4322
4323 PSA_ASSERT( psa_crypto_init( ) );
4324
4325 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4326
4327 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4328 expected_status );
4329
4330exit:
4331 psa_key_derivation_abort( &operation );
4332 PSA_DONE( );
4333}
4334/* END_CASE */
4335
4336/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004337void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004338 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004339 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004340 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004341 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004342 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004343 int expected_status_arg3,
4344 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004345{
4346 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004347 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4348 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004349 psa_status_t expected_statuses[] = {expected_status_arg1,
4350 expected_status_arg2,
4351 expected_status_arg3};
4352 data_t *inputs[] = {input1, input2, input3};
4353 psa_key_handle_t handles[] = {0, 0, 0};
4354 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4355 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4356 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004357 psa_key_type_t output_key_type = output_key_type_arg;
4358 psa_key_handle_t output_handle = 0;
4359 psa_status_t expected_output_status = expected_output_status_arg;
4360 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004361
4362 PSA_ASSERT( psa_crypto_init( ) );
4363
4364 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4365 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004366
4367 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4368
4369 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4370 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004371 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004372 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004373 psa_set_key_type( &attributes, key_types[i] );
4374 PSA_ASSERT( psa_import_key( &attributes,
4375 inputs[i]->x, inputs[i]->len,
4376 &handles[i] ) );
4377 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4378 handles[i] ),
4379 expected_statuses[i] );
4380 }
4381 else
4382 {
4383 TEST_EQUAL( psa_key_derivation_input_bytes(
4384 &operation, steps[i],
4385 inputs[i]->x, inputs[i]->len ),
4386 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004387 }
4388 }
4389
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004390 if( output_key_type != PSA_KEY_TYPE_NONE )
4391 {
4392 psa_reset_key_attributes( &attributes );
4393 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4394 psa_set_key_bits( &attributes, 8 );
4395 actual_output_status =
4396 psa_key_derivation_output_key( &attributes, &operation,
4397 &output_handle );
4398 }
4399 else
4400 {
4401 uint8_t buffer[1];
4402 actual_output_status =
4403 psa_key_derivation_output_bytes( &operation,
4404 buffer, sizeof( buffer ) );
4405 }
4406 TEST_EQUAL( actual_output_status, expected_output_status );
4407
Janos Follathaf3c2a02019-06-12 12:34:34 +01004408exit:
4409 psa_key_derivation_abort( &operation );
4410 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4411 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004412 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004413 PSA_DONE( );
4414}
4415/* END_CASE */
4416
Janos Follathd958bb72019-07-03 15:02:16 +01004417/* BEGIN_CASE */
4418void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004419{
Janos Follathd958bb72019-07-03 15:02:16 +01004420 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004421 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004422 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004424 unsigned char input1[] = "Input 1";
4425 size_t input1_length = sizeof( input1 );
4426 unsigned char input2[] = "Input 2";
4427 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004428 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004429 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004430 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4431 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4432 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004434
Gilles Peskine8817f612018-12-18 00:18:46 +01004435 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004436
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004437 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4438 psa_set_key_algorithm( &attributes, alg );
4439 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004440
Gilles Peskine73676cb2019-05-15 20:15:10 +02004441 PSA_ASSERT( psa_import_key( &attributes,
4442 key_data, sizeof( key_data ),
4443 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004444
4445 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004446 if( !setup_key_derivation_wrap( &operation, handle, alg,
4447 input1, input1_length,
4448 input2, input2_length,
4449 capacity ) )
4450 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004451
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004452 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004453 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004454 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004455
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004456 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004457
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004458 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004459 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004460
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004461exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004462 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004463 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004464 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004465}
4466/* END_CASE */
4467
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004468/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004469void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004470{
4471 uint8_t output_buffer[16];
4472 size_t buffer_size = 16;
4473 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004474 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004475
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004476 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4477 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004478 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004479
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004481 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004482
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004483 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004484
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004485 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4486 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004487 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004488
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004490 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004491
4492exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004494}
4495/* END_CASE */
4496
4497/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004498void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004499 int step1_arg, data_t *input1,
4500 int step2_arg, data_t *input2,
4501 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004502 int requested_capacity_arg,
4503 data_t *expected_output1,
4504 data_t *expected_output2 )
4505{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004506 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004507 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4508 data_t *inputs[] = {input1, input2, input3};
4509 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004510 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004511 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004512 uint8_t *expected_outputs[2] =
4513 {expected_output1->x, expected_output2->x};
4514 size_t output_sizes[2] =
4515 {expected_output1->len, expected_output2->len};
4516 size_t output_buffer_size = 0;
4517 uint8_t *output_buffer = NULL;
4518 size_t expected_capacity;
4519 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004521 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004522 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004523
4524 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4525 {
4526 if( output_sizes[i] > output_buffer_size )
4527 output_buffer_size = output_sizes[i];
4528 if( output_sizes[i] == 0 )
4529 expected_outputs[i] = NULL;
4530 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004531 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004532 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004533
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004534 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4535 psa_set_key_algorithm( &attributes, alg );
4536 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004537
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004538 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004539 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4540 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4541 requested_capacity ) );
4542 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004543 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004544 switch( steps[i] )
4545 {
4546 case 0:
4547 break;
4548 case PSA_KEY_DERIVATION_INPUT_SECRET:
4549 PSA_ASSERT( psa_import_key( &attributes,
4550 inputs[i]->x, inputs[i]->len,
4551 &handles[i] ) );
4552 PSA_ASSERT( psa_key_derivation_input_key(
4553 &operation, steps[i],
4554 handles[i] ) );
4555 break;
4556 default:
4557 PSA_ASSERT( psa_key_derivation_input_bytes(
4558 &operation, steps[i],
4559 inputs[i]->x, inputs[i]->len ) );
4560 break;
4561 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004562 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004563
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004564 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004565 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004566 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004567 expected_capacity = requested_capacity;
4568
4569 /* Expansion phase. */
4570 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4571 {
4572 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004573 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004574 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004575 if( expected_capacity == 0 && output_sizes[i] == 0 )
4576 {
4577 /* Reading 0 bytes when 0 bytes are available can go either way. */
4578 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004579 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004580 continue;
4581 }
4582 else if( expected_capacity == 0 ||
4583 output_sizes[i] > expected_capacity )
4584 {
4585 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004586 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004587 expected_capacity = 0;
4588 continue;
4589 }
4590 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004591 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004592 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004593 ASSERT_COMPARE( output_buffer, output_sizes[i],
4594 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004596 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004597 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004598 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004599 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004600 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004601 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004602
4603exit:
4604 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004605 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004606 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4607 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004608 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004609}
4610/* END_CASE */
4611
4612/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004613void derive_full( int alg_arg,
4614 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004615 data_t *input1,
4616 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004617 int requested_capacity_arg )
4618{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004619 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004620 psa_algorithm_t alg = alg_arg;
4621 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004623 unsigned char output_buffer[16];
4624 size_t expected_capacity = requested_capacity;
4625 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004627
Gilles Peskine8817f612018-12-18 00:18:46 +01004628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004629
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004630 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4631 psa_set_key_algorithm( &attributes, alg );
4632 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004633
Gilles Peskine049c7532019-05-15 20:22:09 +02004634 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4635 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004636
Janos Follathf2815ea2019-07-03 12:41:36 +01004637 if( !setup_key_derivation_wrap( &operation, handle, alg,
4638 input1->x, input1->len,
4639 input2->x, input2->len,
4640 requested_capacity ) )
4641 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004642
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004643 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004644 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004645 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004646
4647 /* Expansion phase. */
4648 while( current_capacity > 0 )
4649 {
4650 size_t read_size = sizeof( output_buffer );
4651 if( read_size > current_capacity )
4652 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004653 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004654 output_buffer,
4655 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004656 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004657 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004658 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004659 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004660 }
4661
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 /* Check that the operation refuses to go over capacity. */
4663 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004664 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004665
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004667
4668exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004669 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004670 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004671 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004672}
4673/* END_CASE */
4674
Janos Follathe60c9052019-07-03 13:51:30 +01004675/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004676void derive_key_exercise( int alg_arg,
4677 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004678 data_t *input1,
4679 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004680 int derived_type_arg,
4681 int derived_bits_arg,
4682 int derived_usage_arg,
4683 int derived_alg_arg )
4684{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004685 psa_key_handle_t base_handle = 0;
4686 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004687 psa_algorithm_t alg = alg_arg;
4688 psa_key_type_t derived_type = derived_type_arg;
4689 size_t derived_bits = derived_bits_arg;
4690 psa_key_usage_t derived_usage = derived_usage_arg;
4691 psa_algorithm_t derived_alg = derived_alg_arg;
4692 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004693 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004694 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004695 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004696
Gilles Peskine8817f612018-12-18 00:18:46 +01004697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004698
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004699 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4700 psa_set_key_algorithm( &attributes, alg );
4701 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004702 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4703 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004704
4705 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004706 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4707 input1->x, input1->len,
4708 input2->x, input2->len, capacity ) )
4709 goto exit;
4710
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004711 psa_set_key_usage_flags( &attributes, derived_usage );
4712 psa_set_key_algorithm( &attributes, derived_alg );
4713 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004714 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004715 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004716 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004717
4718 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004719 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4720 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4721 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004722
4723 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004724 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004725 goto exit;
4726
4727exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004729 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004730 psa_destroy_key( base_handle );
4731 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004732 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004733}
4734/* END_CASE */
4735
Janos Follath42fd8882019-07-03 14:17:09 +01004736/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004737void derive_key_export( int alg_arg,
4738 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004739 data_t *input1,
4740 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004741 int bytes1_arg,
4742 int bytes2_arg )
4743{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004744 psa_key_handle_t base_handle = 0;
4745 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004746 psa_algorithm_t alg = alg_arg;
4747 size_t bytes1 = bytes1_arg;
4748 size_t bytes2 = bytes2_arg;
4749 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004751 uint8_t *output_buffer = NULL;
4752 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004753 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4754 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004755 size_t length;
4756
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004757 ASSERT_ALLOC( output_buffer, capacity );
4758 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004759 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004760
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004761 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4762 psa_set_key_algorithm( &base_attributes, alg );
4763 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004764 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4765 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004766
4767 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01004768 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4769 input1->x, input1->len,
4770 input2->x, input2->len, capacity ) )
4771 goto exit;
4772
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004774 output_buffer,
4775 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004776 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004777
4778 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01004779 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4780 input1->x, input1->len,
4781 input2->x, input2->len, capacity ) )
4782 goto exit;
4783
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004784 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4785 psa_set_key_algorithm( &derived_attributes, 0 );
4786 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004787 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004788 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004789 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004790 PSA_ASSERT( psa_export_key( derived_handle,
4791 export_buffer, bytes1,
4792 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004793 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004794 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004795 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004796 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004797 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004798 PSA_ASSERT( psa_export_key( derived_handle,
4799 export_buffer + bytes1, bytes2,
4800 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004801 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004802
4803 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004804 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4805 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004806
4807exit:
4808 mbedtls_free( output_buffer );
4809 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004811 psa_destroy_key( base_handle );
4812 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004813 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004814}
4815/* END_CASE */
4816
4817/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004818void derive_key( int alg_arg,
4819 data_t *key_data, data_t *input1, data_t *input2,
4820 int type_arg, int bits_arg,
4821 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02004822{
4823 psa_key_handle_t base_handle = 0;
4824 psa_key_handle_t derived_handle = 0;
4825 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004826 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004827 size_t bits = bits_arg;
4828 psa_status_t expected_status = expected_status_arg;
4829 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4830 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4831 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4832
4833 PSA_ASSERT( psa_crypto_init( ) );
4834
4835 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4836 psa_set_key_algorithm( &base_attributes, alg );
4837 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4838 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4839 &base_handle ) );
4840
4841 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4842 input1->x, input1->len,
4843 input2->x, input2->len, SIZE_MAX ) )
4844 goto exit;
4845
4846 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4847 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004848 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004849 psa_set_key_bits( &derived_attributes, bits );
4850 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
4851 &derived_handle ),
4852 expected_status );
4853
4854exit:
4855 psa_key_derivation_abort( &operation );
4856 psa_destroy_key( base_handle );
4857 psa_destroy_key( derived_handle );
4858 PSA_DONE( );
4859}
4860/* END_CASE */
4861
4862/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004863void key_agreement_setup( int alg_arg,
4864 int our_key_type_arg, data_t *our_key_data,
4865 data_t *peer_key_data,
4866 int expected_status_arg )
4867{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004868 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004869 psa_algorithm_t alg = alg_arg;
4870 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004871 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004872 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004873 psa_status_t expected_status = expected_status_arg;
4874 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004875
Gilles Peskine8817f612018-12-18 00:18:46 +01004876 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004877
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004878 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4879 psa_set_key_algorithm( &attributes, alg );
4880 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004881 PSA_ASSERT( psa_import_key( &attributes,
4882 our_key_data->x, our_key_data->len,
4883 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004884
Gilles Peskine77f40d82019-04-11 21:27:06 +02004885 /* The tests currently include inputs that should fail at either step.
4886 * Test cases that fail at the setup step should be changed to call
4887 * key_derivation_setup instead, and this function should be renamed
4888 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004889 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004890 if( status == PSA_SUCCESS )
4891 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004892 TEST_EQUAL( psa_key_derivation_key_agreement(
4893 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4894 our_key,
4895 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004896 expected_status );
4897 }
4898 else
4899 {
4900 TEST_ASSERT( status == expected_status );
4901 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004902
4903exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004905 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004906 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004907}
4908/* END_CASE */
4909
4910/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004911void raw_key_agreement( int alg_arg,
4912 int our_key_type_arg, data_t *our_key_data,
4913 data_t *peer_key_data,
4914 data_t *expected_output )
4915{
4916 psa_key_handle_t our_key = 0;
4917 psa_algorithm_t alg = alg_arg;
4918 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004920 unsigned char *output = NULL;
4921 size_t output_length = ~0;
4922
4923 ASSERT_ALLOC( output, expected_output->len );
4924 PSA_ASSERT( psa_crypto_init( ) );
4925
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004926 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4927 psa_set_key_algorithm( &attributes, alg );
4928 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004929 PSA_ASSERT( psa_import_key( &attributes,
4930 our_key_data->x, our_key_data->len,
4931 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004932
Gilles Peskinebe697d82019-05-16 18:00:41 +02004933 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4934 peer_key_data->x, peer_key_data->len,
4935 output, expected_output->len,
4936 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004937 ASSERT_COMPARE( output, output_length,
4938 expected_output->x, expected_output->len );
4939
4940exit:
4941 mbedtls_free( output );
4942 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004943 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004944}
4945/* END_CASE */
4946
4947/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004948void key_agreement_capacity( int alg_arg,
4949 int our_key_type_arg, data_t *our_key_data,
4950 data_t *peer_key_data,
4951 int expected_capacity_arg )
4952{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004953 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004954 psa_algorithm_t alg = alg_arg;
4955 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004958 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004959 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004960
Gilles Peskine8817f612018-12-18 00:18:46 +01004961 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004962
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4964 psa_set_key_algorithm( &attributes, alg );
4965 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004966 PSA_ASSERT( psa_import_key( &attributes,
4967 our_key_data->x, our_key_data->len,
4968 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004969
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004971 PSA_ASSERT( psa_key_derivation_key_agreement(
4972 &operation,
4973 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4974 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004975 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4976 {
4977 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004978 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004979 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004980 NULL, 0 ) );
4981 }
Gilles Peskine59685592018-09-18 12:11:34 +02004982
Gilles Peskinebf491972018-10-25 22:36:12 +02004983 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004984 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004985 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004986 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004987
Gilles Peskinebf491972018-10-25 22:36:12 +02004988 /* Test the actual capacity by reading the output. */
4989 while( actual_capacity > sizeof( output ) )
4990 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004991 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004992 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004993 actual_capacity -= sizeof( output );
4994 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004995 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004996 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004997 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004998 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004999
Gilles Peskine59685592018-09-18 12:11:34 +02005000exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005001 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005002 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005003 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005004}
5005/* END_CASE */
5006
5007/* BEGIN_CASE */
5008void key_agreement_output( int alg_arg,
5009 int our_key_type_arg, data_t *our_key_data,
5010 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005011 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005012{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005013 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005014 psa_algorithm_t alg = alg_arg;
5015 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005016 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005018 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005019
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005020 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5021 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005022
Gilles Peskine8817f612018-12-18 00:18:46 +01005023 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005024
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005025 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5026 psa_set_key_algorithm( &attributes, alg );
5027 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005028 PSA_ASSERT( psa_import_key( &attributes,
5029 our_key_data->x, our_key_data->len,
5030 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005031
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005033 PSA_ASSERT( psa_key_derivation_key_agreement(
5034 &operation,
5035 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5036 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005037 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5038 {
5039 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005040 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005041 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005042 NULL, 0 ) );
5043 }
Gilles Peskine59685592018-09-18 12:11:34 +02005044
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005046 actual_output,
5047 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005048 ASSERT_COMPARE( actual_output, expected_output1->len,
5049 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005050 if( expected_output2->len != 0 )
5051 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005052 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005053 actual_output,
5054 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005055 ASSERT_COMPARE( actual_output, expected_output2->len,
5056 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005057 }
Gilles Peskine59685592018-09-18 12:11:34 +02005058
5059exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005060 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005061 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005062 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005063 mbedtls_free( actual_output );
5064}
5065/* END_CASE */
5066
5067/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005068void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005069{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005070 size_t bytes = bytes_arg;
5071 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005072 unsigned char *output = NULL;
5073 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005074 size_t i;
5075 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005076
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005077 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5078 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005079 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005080
Gilles Peskine8817f612018-12-18 00:18:46 +01005081 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005082
Gilles Peskinea50d7392018-06-21 10:22:13 +02005083 /* Run several times, to ensure that every output byte will be
5084 * nonzero at least once with overwhelming probability
5085 * (2^(-8*number_of_runs)). */
5086 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005087 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005088 if( bytes != 0 )
5089 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005090 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005091
5092 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005093 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5094 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005095
5096 for( i = 0; i < bytes; i++ )
5097 {
5098 if( output[i] != 0 )
5099 ++changed[i];
5100 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005101 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005102
5103 /* Check that every byte was changed to nonzero at least once. This
5104 * validates that psa_generate_random is overwriting every byte of
5105 * the output buffer. */
5106 for( i = 0; i < bytes; i++ )
5107 {
5108 TEST_ASSERT( changed[i] != 0 );
5109 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005110
5111exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005112 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005113 mbedtls_free( output );
5114 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005115}
5116/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005117
5118/* BEGIN_CASE */
5119void generate_key( int type_arg,
5120 int bits_arg,
5121 int usage_arg,
5122 int alg_arg,
5123 int expected_status_arg )
5124{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005125 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005126 psa_key_type_t type = type_arg;
5127 psa_key_usage_t usage = usage_arg;
5128 size_t bits = bits_arg;
5129 psa_algorithm_t alg = alg_arg;
5130 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005131 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005132 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005133
Gilles Peskine8817f612018-12-18 00:18:46 +01005134 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005135
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005136 psa_set_key_usage_flags( &attributes, usage );
5137 psa_set_key_algorithm( &attributes, alg );
5138 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005139 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005140
5141 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005142 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005143 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005144 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005145
5146 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005147 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5148 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5149 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005150
Gilles Peskine818ca122018-06-20 18:16:48 +02005151 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005152 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005153 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005154
5155exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005156 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005157 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005158 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005159}
5160/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005161
Gilles Peskinee56e8782019-04-26 17:34:02 +02005162/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5163void generate_key_rsa( int bits_arg,
5164 data_t *e_arg,
5165 int expected_status_arg )
5166{
5167 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005168 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005169 size_t bits = bits_arg;
5170 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5171 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5172 psa_status_t expected_status = expected_status_arg;
5173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5174 uint8_t *exported = NULL;
5175 size_t exported_size =
5176 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5177 size_t exported_length = SIZE_MAX;
5178 uint8_t *e_read_buffer = NULL;
5179 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005180 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005181 size_t e_read_length = SIZE_MAX;
5182
5183 if( e_arg->len == 0 ||
5184 ( e_arg->len == 3 &&
5185 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5186 {
5187 is_default_public_exponent = 1;
5188 e_read_size = 0;
5189 }
5190 ASSERT_ALLOC( e_read_buffer, e_read_size );
5191 ASSERT_ALLOC( exported, exported_size );
5192
5193 PSA_ASSERT( psa_crypto_init( ) );
5194
5195 psa_set_key_usage_flags( &attributes, usage );
5196 psa_set_key_algorithm( &attributes, alg );
5197 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5198 e_arg->x, e_arg->len ) );
5199 psa_set_key_bits( &attributes, bits );
5200
5201 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005202 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005203 if( expected_status != PSA_SUCCESS )
5204 goto exit;
5205
5206 /* Test the key information */
5207 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5208 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5209 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5210 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5211 e_read_buffer, e_read_size,
5212 &e_read_length ) );
5213 if( is_default_public_exponent )
5214 TEST_EQUAL( e_read_length, 0 );
5215 else
5216 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5217
5218 /* Do something with the key according to its type and permitted usage. */
5219 if( ! exercise_key( handle, usage, alg ) )
5220 goto exit;
5221
5222 /* Export the key and check the public exponent. */
5223 PSA_ASSERT( psa_export_public_key( handle,
5224 exported, exported_size,
5225 &exported_length ) );
5226 {
5227 uint8_t *p = exported;
5228 uint8_t *end = exported + exported_length;
5229 size_t len;
5230 /* RSAPublicKey ::= SEQUENCE {
5231 * modulus INTEGER, -- n
5232 * publicExponent INTEGER } -- e
5233 */
5234 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005235 MBEDTLS_ASN1_SEQUENCE |
5236 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005237 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5238 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5239 MBEDTLS_ASN1_INTEGER ) );
5240 if( len >= 1 && p[0] == 0 )
5241 {
5242 ++p;
5243 --len;
5244 }
5245 if( e_arg->len == 0 )
5246 {
5247 TEST_EQUAL( len, 3 );
5248 TEST_EQUAL( p[0], 1 );
5249 TEST_EQUAL( p[1], 0 );
5250 TEST_EQUAL( p[2], 1 );
5251 }
5252 else
5253 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5254 }
5255
5256exit:
5257 psa_reset_key_attributes( &attributes );
5258 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005259 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005260 mbedtls_free( e_read_buffer );
5261 mbedtls_free( exported );
5262}
5263/* END_CASE */
5264
Darryl Greend49a4992018-06-18 17:27:26 +01005265/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005266void persistent_key_load_key_from_storage( data_t *data,
5267 int type_arg, int bits_arg,
5268 int usage_flags_arg, int alg_arg,
5269 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005270{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005271 psa_key_id_t key_id = 1;
5272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005273 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005274 psa_key_handle_t base_key = 0;
5275 psa_key_type_t type = type_arg;
5276 size_t bits = bits_arg;
5277 psa_key_usage_t usage_flags = usage_flags_arg;
5278 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005280 unsigned char *first_export = NULL;
5281 unsigned char *second_export = NULL;
5282 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5283 size_t first_exported_length;
5284 size_t second_exported_length;
5285
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005286 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5287 {
5288 ASSERT_ALLOC( first_export, export_size );
5289 ASSERT_ALLOC( second_export, export_size );
5290 }
Darryl Greend49a4992018-06-18 17:27:26 +01005291
Gilles Peskine8817f612018-12-18 00:18:46 +01005292 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005293
Gilles Peskinec87af662019-05-15 16:12:22 +02005294 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005295 psa_set_key_usage_flags( &attributes, usage_flags );
5296 psa_set_key_algorithm( &attributes, alg );
5297 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005298 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005299
Darryl Green0c6575a2018-11-07 16:05:30 +00005300 switch( generation_method )
5301 {
5302 case IMPORT_KEY:
5303 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005304 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5305 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005306 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005307
Darryl Green0c6575a2018-11-07 16:05:30 +00005308 case GENERATE_KEY:
5309 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005310 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005311 break;
5312
5313 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005314 {
5315 /* Create base key */
5316 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5317 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5318 psa_set_key_usage_flags( &base_attributes,
5319 PSA_KEY_USAGE_DERIVE );
5320 psa_set_key_algorithm( &base_attributes, derive_alg );
5321 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005322 PSA_ASSERT( psa_import_key( &base_attributes,
5323 data->x, data->len,
5324 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005325 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005326 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005327 PSA_ASSERT( psa_key_derivation_input_key(
5328 &operation,
5329 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005330 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005332 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005333 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5334 &operation,
5335 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005337 PSA_ASSERT( psa_destroy_key( base_key ) );
5338 base_key = 0;
5339 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005340 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005341 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005342 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005343
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005344 /* Export the key if permitted by the key policy. */
5345 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5346 {
5347 PSA_ASSERT( psa_export_key( handle,
5348 first_export, export_size,
5349 &first_exported_length ) );
5350 if( generation_method == IMPORT_KEY )
5351 ASSERT_COMPARE( data->x, data->len,
5352 first_export, first_exported_length );
5353 }
Darryl Greend49a4992018-06-18 17:27:26 +01005354
5355 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005356 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005357 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005358 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005359
Darryl Greend49a4992018-06-18 17:27:26 +01005360 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005361 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005362 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5363 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5364 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5365 PSA_KEY_LIFETIME_PERSISTENT );
5366 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5367 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5368 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5369 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005370
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005371 /* Export the key again if permitted by the key policy. */
5372 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005373 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005374 PSA_ASSERT( psa_export_key( handle,
5375 second_export, export_size,
5376 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005377 ASSERT_COMPARE( first_export, first_exported_length,
5378 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005379 }
5380
5381 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005382 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005383 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005384
5385exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005386 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005387 mbedtls_free( first_export );
5388 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005389 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005390 psa_destroy_key( base_key );
5391 if( handle == 0 )
5392 {
5393 /* In case there was a test failure after creating the persistent key
5394 * but while it was not open, try to re-open the persistent key
5395 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005396 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005397 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005398 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005399 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005400}
5401/* END_CASE */