blob: 543fe89e2f9b998735b617ce00834e1f3f6c8e33 [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)
3789 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3790 input_data->x, input_data->len,
3791 signature, signature_size,
3792 &signature_length ) );
3793 ASSERT_COMPARE( output_data->x, output_data->len,
3794 signature, signature_length );
3795#endif /* MBEDTLS_TEST_DEPRECATED */
3796
Gilles Peskine20035e32018-02-03 22:44:14 +01003797exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003798 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003799 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003800 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003801 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003802}
3803/* END_CASE */
3804
3805/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003806void sign_fail( int key_type_arg, data_t *key_data,
3807 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003808 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003809{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003810 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003811 psa_key_type_t key_type = key_type_arg;
3812 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003813 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003814 psa_status_t actual_status;
3815 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003816 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003817 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003819
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003820 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003821
Gilles Peskine8817f612018-12-18 00:18:46 +01003822 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003823
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003824 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003825 psa_set_key_algorithm( &attributes, alg );
3826 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003827
Gilles Peskine049c7532019-05-15 20:22:09 +02003828 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3829 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003830
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003831 actual_status = psa_sign_hash( handle, alg,
3832 input_data->x, input_data->len,
3833 signature, signature_size,
3834 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003835 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003836 /* The value of *signature_length is unspecified on error, but
3837 * whatever it is, it should be less than signature_size, so that
3838 * if the caller tries to read *signature_length bytes without
3839 * checking the error code then they don't overflow a buffer. */
3840 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003841
3842exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003843 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003844 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003845 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003846 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003847}
3848/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003849
3850/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003851void sign_verify( int key_type_arg, data_t *key_data,
3852 int alg_arg, data_t *input_data )
3853{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003854 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003855 psa_key_type_t key_type = key_type_arg;
3856 psa_algorithm_t alg = alg_arg;
3857 size_t key_bits;
3858 unsigned char *signature = NULL;
3859 size_t signature_size;
3860 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003861 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003862
Gilles Peskine8817f612018-12-18 00:18:46 +01003863 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003864
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003865 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003866 psa_set_key_algorithm( &attributes, alg );
3867 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003868
Gilles Peskine049c7532019-05-15 20:22:09 +02003869 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3870 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003871 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3872 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003873
3874 /* Allocate a buffer which has the size advertized by the
3875 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003876 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003877 key_bits, alg );
3878 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003879 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003880 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003881
3882 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003883 PSA_ASSERT( psa_sign_hash( handle, alg,
3884 input_data->x, input_data->len,
3885 signature, signature_size,
3886 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003887 /* Check that the signature length looks sensible. */
3888 TEST_ASSERT( signature_length <= signature_size );
3889 TEST_ASSERT( signature_length > 0 );
3890
3891 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003892 PSA_ASSERT( psa_verify_hash( handle, alg,
3893 input_data->x, input_data->len,
3894 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003895
3896 if( input_data->len != 0 )
3897 {
3898 /* Flip a bit in the input and verify that the signature is now
3899 * detected as invalid. Flip a bit at the beginning, not at the end,
3900 * because ECDSA may ignore the last few bits of the input. */
3901 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003902 TEST_EQUAL( psa_verify_hash( handle, alg,
3903 input_data->x, input_data->len,
3904 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003905 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003906 }
3907
3908exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003909 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003910 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003911 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003912 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003913}
3914/* END_CASE */
3915
3916/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003917void asymmetric_verify( int key_type_arg, data_t *key_data,
3918 int alg_arg, data_t *hash_data,
3919 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003920{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003921 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003922 psa_key_type_t key_type = key_type_arg;
3923 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003925
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003926 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003927
Gilles Peskine8817f612018-12-18 00:18:46 +01003928 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003929
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003930 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003931 psa_set_key_algorithm( &attributes, alg );
3932 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003933
Gilles Peskine049c7532019-05-15 20:22:09 +02003934 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3935 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003936
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003937 PSA_ASSERT( psa_verify_hash( handle, alg,
3938 hash_data->x, hash_data->len,
3939 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003940
3941#if defined(MBEDTLS_TEST_DEPRECATED)
3942 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3943 hash_data->x, hash_data->len,
3944 signature_data->x,
3945 signature_data->len ) );
3946
3947#endif /* MBEDTLS_TEST_DEPRECATED */
3948
itayzafrir5c753392018-05-08 11:18:38 +03003949exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003950 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003951 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003952 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003953}
3954/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003955
3956/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003957void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3958 int alg_arg, data_t *hash_data,
3959 data_t *signature_data,
3960 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003961{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003962 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003963 psa_key_type_t key_type = key_type_arg;
3964 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003965 psa_status_t actual_status;
3966 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003967 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968
Gilles Peskine8817f612018-12-18 00:18:46 +01003969 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003970
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003971 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003972 psa_set_key_algorithm( &attributes, alg );
3973 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003974
Gilles Peskine049c7532019-05-15 20:22:09 +02003975 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3976 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003978 actual_status = psa_verify_hash( handle, alg,
3979 hash_data->x, hash_data->len,
3980 signature_data->x, signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003981
Gilles Peskinefe11b722018-12-18 00:24:04 +01003982 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003983
3984exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003985 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003986 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003987 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003988}
3989/* END_CASE */
3990
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003991/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003992void asymmetric_encrypt( int key_type_arg,
3993 data_t *key_data,
3994 int alg_arg,
3995 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003996 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003997 int expected_output_length_arg,
3998 int expected_status_arg )
3999{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004000 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004001 psa_key_type_t key_type = key_type_arg;
4002 psa_algorithm_t alg = alg_arg;
4003 size_t expected_output_length = expected_output_length_arg;
4004 size_t key_bits;
4005 unsigned char *output = NULL;
4006 size_t output_size;
4007 size_t output_length = ~0;
4008 psa_status_t actual_status;
4009 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004011
Gilles Peskine8817f612018-12-18 00:18:46 +01004012 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004013
Gilles Peskine656896e2018-06-29 19:12:28 +02004014 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004015 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4016 psa_set_key_algorithm( &attributes, alg );
4017 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004018 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4019 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004020
4021 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004022 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4023 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004024 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004025 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004026
4027 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004028 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004029 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004030 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004031 output, output_size,
4032 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004033 TEST_EQUAL( actual_status, expected_status );
4034 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004035
Gilles Peskine68428122018-06-30 18:42:41 +02004036 /* If the label is empty, the test framework puts a non-null pointer
4037 * in label->x. Test that a null pointer works as well. */
4038 if( label->len == 0 )
4039 {
4040 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004041 if( output_size != 0 )
4042 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004043 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004044 input_data->x, input_data->len,
4045 NULL, label->len,
4046 output, output_size,
4047 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004048 TEST_EQUAL( actual_status, expected_status );
4049 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004050 }
4051
Gilles Peskine656896e2018-06-29 19:12:28 +02004052exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004053 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004054 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004055 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004056 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004057}
4058/* END_CASE */
4059
4060/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004061void asymmetric_encrypt_decrypt( int key_type_arg,
4062 data_t *key_data,
4063 int alg_arg,
4064 data_t *input_data,
4065 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004066{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004067 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004068 psa_key_type_t key_type = key_type_arg;
4069 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004070 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004072 size_t output_size;
4073 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004074 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004075 size_t output2_size;
4076 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004077 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004078
Gilles Peskine8817f612018-12-18 00:18:46 +01004079 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004080
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004081 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4082 psa_set_key_algorithm( &attributes, alg );
4083 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004084
Gilles Peskine049c7532019-05-15 20:22:09 +02004085 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4086 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004087
4088 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004089 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4090 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004091 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004092 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004093 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004094 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004095
Gilles Peskineeebd7382018-06-08 18:11:54 +02004096 /* We test encryption by checking that encrypt-then-decrypt gives back
4097 * the original plaintext because of the non-optional random
4098 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004099 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4100 input_data->x, input_data->len,
4101 label->x, label->len,
4102 output, output_size,
4103 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004104 /* We don't know what ciphertext length to expect, but check that
4105 * it looks sensible. */
4106 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004107
Gilles Peskine8817f612018-12-18 00:18:46 +01004108 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4109 output, output_length,
4110 label->x, label->len,
4111 output2, output2_size,
4112 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004113 ASSERT_COMPARE( input_data->x, input_data->len,
4114 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004115
4116exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004117 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004118 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004119 mbedtls_free( output );
4120 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004121 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004122}
4123/* END_CASE */
4124
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004125/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004126void asymmetric_decrypt( int key_type_arg,
4127 data_t *key_data,
4128 int alg_arg,
4129 data_t *input_data,
4130 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004131 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004132{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004133 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004134 psa_key_type_t key_type = key_type_arg;
4135 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004136 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004137 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004138 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004140
Jaeden Amero412654a2019-02-06 12:57:46 +00004141 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004142 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004143
Gilles Peskine8817f612018-12-18 00:18:46 +01004144 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004145
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4147 psa_set_key_algorithm( &attributes, alg );
4148 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004149
Gilles Peskine049c7532019-05-15 20:22:09 +02004150 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4151 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004152
Gilles Peskine8817f612018-12-18 00:18:46 +01004153 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4154 input_data->x, input_data->len,
4155 label->x, label->len,
4156 output,
4157 output_size,
4158 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004159 ASSERT_COMPARE( expected_data->x, expected_data->len,
4160 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004161
Gilles Peskine68428122018-06-30 18:42:41 +02004162 /* If the label is empty, the test framework puts a non-null pointer
4163 * in label->x. Test that a null pointer works as well. */
4164 if( label->len == 0 )
4165 {
4166 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004167 if( output_size != 0 )
4168 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004169 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4170 input_data->x, input_data->len,
4171 NULL, label->len,
4172 output,
4173 output_size,
4174 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004175 ASSERT_COMPARE( expected_data->x, expected_data->len,
4176 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004177 }
4178
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004179exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004180 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004181 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004182 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004183 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004184}
4185/* END_CASE */
4186
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004187/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004188void asymmetric_decrypt_fail( int key_type_arg,
4189 data_t *key_data,
4190 int alg_arg,
4191 data_t *input_data,
4192 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004193 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004194 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004195{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004196 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004197 psa_key_type_t key_type = key_type_arg;
4198 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004200 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004201 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004202 psa_status_t actual_status;
4203 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004204 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004205
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004206 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004207
Gilles Peskine8817f612018-12-18 00:18:46 +01004208 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004209
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004210 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4211 psa_set_key_algorithm( &attributes, alg );
4212 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004213
Gilles Peskine049c7532019-05-15 20:22:09 +02004214 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4215 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004216
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004217 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004218 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004219 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004220 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004221 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004222 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004223 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004224
Gilles Peskine68428122018-06-30 18:42:41 +02004225 /* If the label is empty, the test framework puts a non-null pointer
4226 * in label->x. Test that a null pointer works as well. */
4227 if( label->len == 0 )
4228 {
4229 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004230 if( output_size != 0 )
4231 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004232 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004233 input_data->x, input_data->len,
4234 NULL, label->len,
4235 output, output_size,
4236 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004237 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004238 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004239 }
4240
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004241exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004242 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004243 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004244 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004245 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004246}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004247/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004248
4249/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004250void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004251{
4252 /* Test each valid way of initializing the object, except for `= {0}`, as
4253 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4254 * though it's OK by the C standard. We could test for this, but we'd need
4255 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004256 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004257 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4258 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4259 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004260
4261 memset( &zero, 0, sizeof( zero ) );
4262
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004263 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004264 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004265 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004266 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004267 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004268 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004269 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004270
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004271 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004272 PSA_ASSERT( psa_key_derivation_abort(&func) );
4273 PSA_ASSERT( psa_key_derivation_abort(&init) );
4274 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004275}
4276/* END_CASE */
4277
Janos Follath16de4a42019-06-13 16:32:24 +01004278/* BEGIN_CASE */
4279void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004280{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004281 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004282 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004283 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004284
Gilles Peskine8817f612018-12-18 00:18:46 +01004285 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004286
Janos Follath16de4a42019-06-13 16:32:24 +01004287 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004288 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004289
4290exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004291 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004292 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004293}
4294/* END_CASE */
4295
Janos Follathaf3c2a02019-06-12 12:34:34 +01004296/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004297void derive_set_capacity( int alg_arg, int capacity_arg,
4298 int expected_status_arg )
4299{
4300 psa_algorithm_t alg = alg_arg;
4301 size_t capacity = capacity_arg;
4302 psa_status_t expected_status = expected_status_arg;
4303 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4304
4305 PSA_ASSERT( psa_crypto_init( ) );
4306
4307 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4308
4309 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4310 expected_status );
4311
4312exit:
4313 psa_key_derivation_abort( &operation );
4314 PSA_DONE( );
4315}
4316/* END_CASE */
4317
4318/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004319void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004320 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004321 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004322 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004323 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004324 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004325 int expected_status_arg3,
4326 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004327{
4328 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004329 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4330 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004331 psa_status_t expected_statuses[] = {expected_status_arg1,
4332 expected_status_arg2,
4333 expected_status_arg3};
4334 data_t *inputs[] = {input1, input2, input3};
4335 psa_key_handle_t handles[] = {0, 0, 0};
4336 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4338 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004339 psa_key_type_t output_key_type = output_key_type_arg;
4340 psa_key_handle_t output_handle = 0;
4341 psa_status_t expected_output_status = expected_output_status_arg;
4342 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004343
4344 PSA_ASSERT( psa_crypto_init( ) );
4345
4346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4347 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004348
4349 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4350
4351 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4352 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004353 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004354 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004355 psa_set_key_type( &attributes, key_types[i] );
4356 PSA_ASSERT( psa_import_key( &attributes,
4357 inputs[i]->x, inputs[i]->len,
4358 &handles[i] ) );
4359 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4360 handles[i] ),
4361 expected_statuses[i] );
4362 }
4363 else
4364 {
4365 TEST_EQUAL( psa_key_derivation_input_bytes(
4366 &operation, steps[i],
4367 inputs[i]->x, inputs[i]->len ),
4368 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004369 }
4370 }
4371
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004372 if( output_key_type != PSA_KEY_TYPE_NONE )
4373 {
4374 psa_reset_key_attributes( &attributes );
4375 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4376 psa_set_key_bits( &attributes, 8 );
4377 actual_output_status =
4378 psa_key_derivation_output_key( &attributes, &operation,
4379 &output_handle );
4380 }
4381 else
4382 {
4383 uint8_t buffer[1];
4384 actual_output_status =
4385 psa_key_derivation_output_bytes( &operation,
4386 buffer, sizeof( buffer ) );
4387 }
4388 TEST_EQUAL( actual_output_status, expected_output_status );
4389
Janos Follathaf3c2a02019-06-12 12:34:34 +01004390exit:
4391 psa_key_derivation_abort( &operation );
4392 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4393 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004394 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004395 PSA_DONE( );
4396}
4397/* END_CASE */
4398
Janos Follathd958bb72019-07-03 15:02:16 +01004399/* BEGIN_CASE */
4400void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004401{
Janos Follathd958bb72019-07-03 15:02:16 +01004402 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004403 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004404 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004405 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004406 unsigned char input1[] = "Input 1";
4407 size_t input1_length = sizeof( input1 );
4408 unsigned char input2[] = "Input 2";
4409 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004410 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004411 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004412 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4413 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4414 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004416
Gilles Peskine8817f612018-12-18 00:18:46 +01004417 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004418
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004419 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4420 psa_set_key_algorithm( &attributes, alg );
4421 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004422
Gilles Peskine73676cb2019-05-15 20:15:10 +02004423 PSA_ASSERT( psa_import_key( &attributes,
4424 key_data, sizeof( key_data ),
4425 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004426
4427 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004428 if( !setup_key_derivation_wrap( &operation, handle, alg,
4429 input1, input1_length,
4430 input2, input2_length,
4431 capacity ) )
4432 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004433
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004435 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004436 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004437
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004439
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004440 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004441 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004442
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004443exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004444 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004445 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004446 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004447}
4448/* END_CASE */
4449
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004450/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004451void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004452{
4453 uint8_t output_buffer[16];
4454 size_t buffer_size = 16;
4455 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004456 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004457
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004458 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4459 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004460 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004461
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004462 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004463 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004464
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004465 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004466
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004467 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4468 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004469 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004470
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004471 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004472 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004473
4474exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004475 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004476}
4477/* END_CASE */
4478
4479/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004480void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004481 int step1_arg, data_t *input1,
4482 int step2_arg, data_t *input2,
4483 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004484 int requested_capacity_arg,
4485 data_t *expected_output1,
4486 data_t *expected_output2 )
4487{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004488 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004489 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4490 data_t *inputs[] = {input1, input2, input3};
4491 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004492 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004494 uint8_t *expected_outputs[2] =
4495 {expected_output1->x, expected_output2->x};
4496 size_t output_sizes[2] =
4497 {expected_output1->len, expected_output2->len};
4498 size_t output_buffer_size = 0;
4499 uint8_t *output_buffer = NULL;
4500 size_t expected_capacity;
4501 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004503 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004504 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004505
4506 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4507 {
4508 if( output_sizes[i] > output_buffer_size )
4509 output_buffer_size = output_sizes[i];
4510 if( output_sizes[i] == 0 )
4511 expected_outputs[i] = NULL;
4512 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004513 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004514 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004515
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004516 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4517 psa_set_key_algorithm( &attributes, alg );
4518 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004519
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004520 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004521 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4522 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4523 requested_capacity ) );
4524 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004525 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004526 switch( steps[i] )
4527 {
4528 case 0:
4529 break;
4530 case PSA_KEY_DERIVATION_INPUT_SECRET:
4531 PSA_ASSERT( psa_import_key( &attributes,
4532 inputs[i]->x, inputs[i]->len,
4533 &handles[i] ) );
4534 PSA_ASSERT( psa_key_derivation_input_key(
4535 &operation, steps[i],
4536 handles[i] ) );
4537 break;
4538 default:
4539 PSA_ASSERT( psa_key_derivation_input_bytes(
4540 &operation, steps[i],
4541 inputs[i]->x, inputs[i]->len ) );
4542 break;
4543 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004544 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004545
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004546 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004547 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004548 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004549 expected_capacity = requested_capacity;
4550
4551 /* Expansion phase. */
4552 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4553 {
4554 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004555 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004556 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004557 if( expected_capacity == 0 && output_sizes[i] == 0 )
4558 {
4559 /* Reading 0 bytes when 0 bytes are available can go either way. */
4560 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004561 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004562 continue;
4563 }
4564 else if( expected_capacity == 0 ||
4565 output_sizes[i] > expected_capacity )
4566 {
4567 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004568 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004569 expected_capacity = 0;
4570 continue;
4571 }
4572 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004573 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004574 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004575 ASSERT_COMPARE( output_buffer, output_sizes[i],
4576 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004578 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004579 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004580 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004581 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004582 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004583 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004584
4585exit:
4586 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004588 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4589 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004590 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004591}
4592/* END_CASE */
4593
4594/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004595void derive_full( int alg_arg,
4596 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004597 data_t *input1,
4598 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004599 int requested_capacity_arg )
4600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004601 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004602 psa_algorithm_t alg = alg_arg;
4603 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004604 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004605 unsigned char output_buffer[16];
4606 size_t expected_capacity = requested_capacity;
4607 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004609
Gilles Peskine8817f612018-12-18 00:18:46 +01004610 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004611
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004612 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4613 psa_set_key_algorithm( &attributes, alg );
4614 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004615
Gilles Peskine049c7532019-05-15 20:22:09 +02004616 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4617 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004618
Janos Follathf2815ea2019-07-03 12:41:36 +01004619 if( !setup_key_derivation_wrap( &operation, handle, alg,
4620 input1->x, input1->len,
4621 input2->x, input2->len,
4622 requested_capacity ) )
4623 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004624
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004625 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004626 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004627 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004628
4629 /* Expansion phase. */
4630 while( current_capacity > 0 )
4631 {
4632 size_t read_size = sizeof( output_buffer );
4633 if( read_size > current_capacity )
4634 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004635 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004636 output_buffer,
4637 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004638 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004639 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004640 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004641 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004642 }
4643
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 /* Check that the operation refuses to go over capacity. */
4645 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004646 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004647
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004649
4650exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004651 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004652 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004653 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004654}
4655/* END_CASE */
4656
Janos Follathe60c9052019-07-03 13:51:30 +01004657/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004658void derive_key_exercise( int alg_arg,
4659 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004660 data_t *input1,
4661 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004662 int derived_type_arg,
4663 int derived_bits_arg,
4664 int derived_usage_arg,
4665 int derived_alg_arg )
4666{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004667 psa_key_handle_t base_handle = 0;
4668 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004669 psa_algorithm_t alg = alg_arg;
4670 psa_key_type_t derived_type = derived_type_arg;
4671 size_t derived_bits = derived_bits_arg;
4672 psa_key_usage_t derived_usage = derived_usage_arg;
4673 psa_algorithm_t derived_alg = derived_alg_arg;
4674 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004676 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004677 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004678
Gilles Peskine8817f612018-12-18 00:18:46 +01004679 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004680
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004681 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4682 psa_set_key_algorithm( &attributes, alg );
4683 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004684 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4685 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004686
4687 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004688 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4689 input1->x, input1->len,
4690 input2->x, input2->len, capacity ) )
4691 goto exit;
4692
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004693 psa_set_key_usage_flags( &attributes, derived_usage );
4694 psa_set_key_algorithm( &attributes, derived_alg );
4695 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004696 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004698 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004699
4700 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004701 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4702 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4703 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004704
4705 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004706 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004707 goto exit;
4708
4709exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004710 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004711 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004712 psa_destroy_key( base_handle );
4713 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004714 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004715}
4716/* END_CASE */
4717
Janos Follath42fd8882019-07-03 14:17:09 +01004718/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004719void derive_key_export( int alg_arg,
4720 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004721 data_t *input1,
4722 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004723 int bytes1_arg,
4724 int bytes2_arg )
4725{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004726 psa_key_handle_t base_handle = 0;
4727 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004728 psa_algorithm_t alg = alg_arg;
4729 size_t bytes1 = bytes1_arg;
4730 size_t bytes2 = bytes2_arg;
4731 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004733 uint8_t *output_buffer = NULL;
4734 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004735 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4736 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004737 size_t length;
4738
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004739 ASSERT_ALLOC( output_buffer, capacity );
4740 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004741 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004742
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004743 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4744 psa_set_key_algorithm( &base_attributes, alg );
4745 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004746 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4747 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004748
4749 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01004750 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4751 input1->x, input1->len,
4752 input2->x, input2->len, capacity ) )
4753 goto exit;
4754
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004756 output_buffer,
4757 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004758 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004759
4760 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01004761 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4762 input1->x, input1->len,
4763 input2->x, input2->len, capacity ) )
4764 goto exit;
4765
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004766 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4767 psa_set_key_algorithm( &derived_attributes, 0 );
4768 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004769 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004770 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004771 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004772 PSA_ASSERT( psa_export_key( derived_handle,
4773 export_buffer, bytes1,
4774 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004775 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004776 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004777 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004779 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004780 PSA_ASSERT( psa_export_key( derived_handle,
4781 export_buffer + bytes1, bytes2,
4782 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004783 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004784
4785 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004786 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4787 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004788
4789exit:
4790 mbedtls_free( output_buffer );
4791 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004792 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004793 psa_destroy_key( base_handle );
4794 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004795 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004796}
4797/* END_CASE */
4798
4799/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004800void derive_key( int alg_arg,
4801 data_t *key_data, data_t *input1, data_t *input2,
4802 int type_arg, int bits_arg,
4803 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02004804{
4805 psa_key_handle_t base_handle = 0;
4806 psa_key_handle_t derived_handle = 0;
4807 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004808 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004809 size_t bits = bits_arg;
4810 psa_status_t expected_status = expected_status_arg;
4811 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4812 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4813 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4814
4815 PSA_ASSERT( psa_crypto_init( ) );
4816
4817 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4818 psa_set_key_algorithm( &base_attributes, alg );
4819 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4820 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4821 &base_handle ) );
4822
4823 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4824 input1->x, input1->len,
4825 input2->x, input2->len, SIZE_MAX ) )
4826 goto exit;
4827
4828 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4829 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004830 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004831 psa_set_key_bits( &derived_attributes, bits );
4832 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
4833 &derived_handle ),
4834 expected_status );
4835
4836exit:
4837 psa_key_derivation_abort( &operation );
4838 psa_destroy_key( base_handle );
4839 psa_destroy_key( derived_handle );
4840 PSA_DONE( );
4841}
4842/* END_CASE */
4843
4844/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004845void key_agreement_setup( int alg_arg,
4846 int our_key_type_arg, data_t *our_key_data,
4847 data_t *peer_key_data,
4848 int expected_status_arg )
4849{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004850 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004851 psa_algorithm_t alg = alg_arg;
4852 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004854 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004855 psa_status_t expected_status = expected_status_arg;
4856 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004857
Gilles Peskine8817f612018-12-18 00:18:46 +01004858 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004859
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004860 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4861 psa_set_key_algorithm( &attributes, alg );
4862 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004863 PSA_ASSERT( psa_import_key( &attributes,
4864 our_key_data->x, our_key_data->len,
4865 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004866
Gilles Peskine77f40d82019-04-11 21:27:06 +02004867 /* The tests currently include inputs that should fail at either step.
4868 * Test cases that fail at the setup step should be changed to call
4869 * key_derivation_setup instead, and this function should be renamed
4870 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004871 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004872 if( status == PSA_SUCCESS )
4873 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004874 TEST_EQUAL( psa_key_derivation_key_agreement(
4875 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4876 our_key,
4877 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004878 expected_status );
4879 }
4880 else
4881 {
4882 TEST_ASSERT( status == expected_status );
4883 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004884
4885exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004886 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004887 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004888 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004889}
4890/* END_CASE */
4891
4892/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004893void raw_key_agreement( int alg_arg,
4894 int our_key_type_arg, data_t *our_key_data,
4895 data_t *peer_key_data,
4896 data_t *expected_output )
4897{
4898 psa_key_handle_t our_key = 0;
4899 psa_algorithm_t alg = alg_arg;
4900 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004902 unsigned char *output = NULL;
4903 size_t output_length = ~0;
4904
4905 ASSERT_ALLOC( output, expected_output->len );
4906 PSA_ASSERT( psa_crypto_init( ) );
4907
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004908 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4909 psa_set_key_algorithm( &attributes, alg );
4910 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004911 PSA_ASSERT( psa_import_key( &attributes,
4912 our_key_data->x, our_key_data->len,
4913 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004914
Gilles Peskinebe697d82019-05-16 18:00:41 +02004915 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4916 peer_key_data->x, peer_key_data->len,
4917 output, expected_output->len,
4918 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004919 ASSERT_COMPARE( output, output_length,
4920 expected_output->x, expected_output->len );
4921
4922exit:
4923 mbedtls_free( output );
4924 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004925 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004926}
4927/* END_CASE */
4928
4929/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004930void key_agreement_capacity( int alg_arg,
4931 int our_key_type_arg, data_t *our_key_data,
4932 data_t *peer_key_data,
4933 int expected_capacity_arg )
4934{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004935 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004936 psa_algorithm_t alg = alg_arg;
4937 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004938 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004940 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004941 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004942
Gilles Peskine8817f612018-12-18 00:18:46 +01004943 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004944
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004945 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4946 psa_set_key_algorithm( &attributes, alg );
4947 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004948 PSA_ASSERT( psa_import_key( &attributes,
4949 our_key_data->x, our_key_data->len,
4950 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004951
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004952 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004953 PSA_ASSERT( psa_key_derivation_key_agreement(
4954 &operation,
4955 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4956 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004957 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4958 {
4959 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004961 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004962 NULL, 0 ) );
4963 }
Gilles Peskine59685592018-09-18 12:11:34 +02004964
Gilles Peskinebf491972018-10-25 22:36:12 +02004965 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004966 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004968 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004969
Gilles Peskinebf491972018-10-25 22:36:12 +02004970 /* Test the actual capacity by reading the output. */
4971 while( actual_capacity > sizeof( output ) )
4972 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004974 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004975 actual_capacity -= sizeof( output );
4976 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004978 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004980 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004981
Gilles Peskine59685592018-09-18 12:11:34 +02004982exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004983 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004984 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004985 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004986}
4987/* END_CASE */
4988
4989/* BEGIN_CASE */
4990void key_agreement_output( int alg_arg,
4991 int our_key_type_arg, data_t *our_key_data,
4992 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004993 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004994{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004995 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004996 psa_algorithm_t alg = alg_arg;
4997 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004998 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005000 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005001
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005002 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5003 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005004
Gilles Peskine8817f612018-12-18 00:18:46 +01005005 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005006
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005007 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5008 psa_set_key_algorithm( &attributes, alg );
5009 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005010 PSA_ASSERT( psa_import_key( &attributes,
5011 our_key_data->x, our_key_data->len,
5012 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005013
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005014 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005015 PSA_ASSERT( psa_key_derivation_key_agreement(
5016 &operation,
5017 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5018 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005019 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5020 {
5021 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005023 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005024 NULL, 0 ) );
5025 }
Gilles Peskine59685592018-09-18 12:11:34 +02005026
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005027 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005028 actual_output,
5029 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005030 ASSERT_COMPARE( actual_output, expected_output1->len,
5031 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005032 if( expected_output2->len != 0 )
5033 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005034 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005035 actual_output,
5036 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005037 ASSERT_COMPARE( actual_output, expected_output2->len,
5038 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005039 }
Gilles Peskine59685592018-09-18 12:11:34 +02005040
5041exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005042 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005043 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005044 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005045 mbedtls_free( actual_output );
5046}
5047/* END_CASE */
5048
5049/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005050void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005051{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005052 size_t bytes = bytes_arg;
5053 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005054 unsigned char *output = NULL;
5055 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005056 size_t i;
5057 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005058
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005059 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5060 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005061 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005062
Gilles Peskine8817f612018-12-18 00:18:46 +01005063 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005064
Gilles Peskinea50d7392018-06-21 10:22:13 +02005065 /* Run several times, to ensure that every output byte will be
5066 * nonzero at least once with overwhelming probability
5067 * (2^(-8*number_of_runs)). */
5068 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005069 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005070 if( bytes != 0 )
5071 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005072 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005073
5074 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005075 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5076 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005077
5078 for( i = 0; i < bytes; i++ )
5079 {
5080 if( output[i] != 0 )
5081 ++changed[i];
5082 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005083 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005084
5085 /* Check that every byte was changed to nonzero at least once. This
5086 * validates that psa_generate_random is overwriting every byte of
5087 * the output buffer. */
5088 for( i = 0; i < bytes; i++ )
5089 {
5090 TEST_ASSERT( changed[i] != 0 );
5091 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005092
5093exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005094 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005095 mbedtls_free( output );
5096 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005097}
5098/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005099
5100/* BEGIN_CASE */
5101void generate_key( int type_arg,
5102 int bits_arg,
5103 int usage_arg,
5104 int alg_arg,
5105 int expected_status_arg )
5106{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005107 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005108 psa_key_type_t type = type_arg;
5109 psa_key_usage_t usage = usage_arg;
5110 size_t bits = bits_arg;
5111 psa_algorithm_t alg = alg_arg;
5112 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005114 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005115
Gilles Peskine8817f612018-12-18 00:18:46 +01005116 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005117
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005118 psa_set_key_usage_flags( &attributes, usage );
5119 psa_set_key_algorithm( &attributes, alg );
5120 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005121 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005122
5123 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005124 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005125 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005126 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005127
5128 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005129 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5130 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5131 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005132
Gilles Peskine818ca122018-06-20 18:16:48 +02005133 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005134 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005135 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005136
5137exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005138 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005139 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005140 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005141}
5142/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005143
Gilles Peskinee56e8782019-04-26 17:34:02 +02005144/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5145void generate_key_rsa( int bits_arg,
5146 data_t *e_arg,
5147 int expected_status_arg )
5148{
5149 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005150 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005151 size_t bits = bits_arg;
5152 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5153 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5154 psa_status_t expected_status = expected_status_arg;
5155 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5156 uint8_t *exported = NULL;
5157 size_t exported_size =
5158 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5159 size_t exported_length = SIZE_MAX;
5160 uint8_t *e_read_buffer = NULL;
5161 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005162 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005163 size_t e_read_length = SIZE_MAX;
5164
5165 if( e_arg->len == 0 ||
5166 ( e_arg->len == 3 &&
5167 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5168 {
5169 is_default_public_exponent = 1;
5170 e_read_size = 0;
5171 }
5172 ASSERT_ALLOC( e_read_buffer, e_read_size );
5173 ASSERT_ALLOC( exported, exported_size );
5174
5175 PSA_ASSERT( psa_crypto_init( ) );
5176
5177 psa_set_key_usage_flags( &attributes, usage );
5178 psa_set_key_algorithm( &attributes, alg );
5179 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5180 e_arg->x, e_arg->len ) );
5181 psa_set_key_bits( &attributes, bits );
5182
5183 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005184 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005185 if( expected_status != PSA_SUCCESS )
5186 goto exit;
5187
5188 /* Test the key information */
5189 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5190 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5191 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5192 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5193 e_read_buffer, e_read_size,
5194 &e_read_length ) );
5195 if( is_default_public_exponent )
5196 TEST_EQUAL( e_read_length, 0 );
5197 else
5198 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5199
5200 /* Do something with the key according to its type and permitted usage. */
5201 if( ! exercise_key( handle, usage, alg ) )
5202 goto exit;
5203
5204 /* Export the key and check the public exponent. */
5205 PSA_ASSERT( psa_export_public_key( handle,
5206 exported, exported_size,
5207 &exported_length ) );
5208 {
5209 uint8_t *p = exported;
5210 uint8_t *end = exported + exported_length;
5211 size_t len;
5212 /* RSAPublicKey ::= SEQUENCE {
5213 * modulus INTEGER, -- n
5214 * publicExponent INTEGER } -- e
5215 */
5216 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005217 MBEDTLS_ASN1_SEQUENCE |
5218 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005219 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5220 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5221 MBEDTLS_ASN1_INTEGER ) );
5222 if( len >= 1 && p[0] == 0 )
5223 {
5224 ++p;
5225 --len;
5226 }
5227 if( e_arg->len == 0 )
5228 {
5229 TEST_EQUAL( len, 3 );
5230 TEST_EQUAL( p[0], 1 );
5231 TEST_EQUAL( p[1], 0 );
5232 TEST_EQUAL( p[2], 1 );
5233 }
5234 else
5235 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5236 }
5237
5238exit:
5239 psa_reset_key_attributes( &attributes );
5240 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005241 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005242 mbedtls_free( e_read_buffer );
5243 mbedtls_free( exported );
5244}
5245/* END_CASE */
5246
Darryl Greend49a4992018-06-18 17:27:26 +01005247/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005248void persistent_key_load_key_from_storage( data_t *data,
5249 int type_arg, int bits_arg,
5250 int usage_flags_arg, int alg_arg,
5251 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005252{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005253 psa_key_id_t key_id = 1;
5254 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005255 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005256 psa_key_handle_t base_key = 0;
5257 psa_key_type_t type = type_arg;
5258 size_t bits = bits_arg;
5259 psa_key_usage_t usage_flags = usage_flags_arg;
5260 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005262 unsigned char *first_export = NULL;
5263 unsigned char *second_export = NULL;
5264 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5265 size_t first_exported_length;
5266 size_t second_exported_length;
5267
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005268 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5269 {
5270 ASSERT_ALLOC( first_export, export_size );
5271 ASSERT_ALLOC( second_export, export_size );
5272 }
Darryl Greend49a4992018-06-18 17:27:26 +01005273
Gilles Peskine8817f612018-12-18 00:18:46 +01005274 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005275
Gilles Peskinec87af662019-05-15 16:12:22 +02005276 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005277 psa_set_key_usage_flags( &attributes, usage_flags );
5278 psa_set_key_algorithm( &attributes, alg );
5279 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005280 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005281
Darryl Green0c6575a2018-11-07 16:05:30 +00005282 switch( generation_method )
5283 {
5284 case IMPORT_KEY:
5285 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005286 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5287 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005288 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005289
Darryl Green0c6575a2018-11-07 16:05:30 +00005290 case GENERATE_KEY:
5291 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005292 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005293 break;
5294
5295 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005296 {
5297 /* Create base key */
5298 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5299 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5300 psa_set_key_usage_flags( &base_attributes,
5301 PSA_KEY_USAGE_DERIVE );
5302 psa_set_key_algorithm( &base_attributes, derive_alg );
5303 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005304 PSA_ASSERT( psa_import_key( &base_attributes,
5305 data->x, data->len,
5306 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005307 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005308 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005309 PSA_ASSERT( psa_key_derivation_input_key(
5310 &operation,
5311 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005312 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005313 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005314 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005315 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5316 &operation,
5317 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005318 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005319 PSA_ASSERT( psa_destroy_key( base_key ) );
5320 base_key = 0;
5321 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005322 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005323 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005324 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005325
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005326 /* Export the key if permitted by the key policy. */
5327 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5328 {
5329 PSA_ASSERT( psa_export_key( handle,
5330 first_export, export_size,
5331 &first_exported_length ) );
5332 if( generation_method == IMPORT_KEY )
5333 ASSERT_COMPARE( data->x, data->len,
5334 first_export, first_exported_length );
5335 }
Darryl Greend49a4992018-06-18 17:27:26 +01005336
5337 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005338 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005339 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005340 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005341
Darryl Greend49a4992018-06-18 17:27:26 +01005342 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005343 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005344 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5345 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5346 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5347 PSA_KEY_LIFETIME_PERSISTENT );
5348 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5349 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5350 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5351 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005352
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005353 /* Export the key again if permitted by the key policy. */
5354 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005355 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005356 PSA_ASSERT( psa_export_key( handle,
5357 second_export, export_size,
5358 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005359 ASSERT_COMPARE( first_export, first_exported_length,
5360 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005361 }
5362
5363 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005364 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005365 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005366
5367exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005368 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005369 mbedtls_free( first_export );
5370 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005371 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005372 psa_destroy_key( base_key );
5373 if( handle == 0 )
5374 {
5375 /* In case there was a test failure after creating the persistent key
5376 * but while it was not open, try to re-open the persistent key
5377 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005378 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005379 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005380 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005381 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005382}
5383/* END_CASE */