blob: 5236b4e2b2e665b18b595761600201470a914241 [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 Peskine1a960492019-11-26 17:12:21 +01001650void check_key_policy( int type_arg, int bits_arg,
1651 int usage_arg, int alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001652{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001653 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001654 psa_key_type_t key_type = type_arg;
1655 size_t bits = bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001656 psa_algorithm_t alg = alg_arg;
1657 psa_key_usage_t usage = usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001659
Gilles Peskine8817f612018-12-18 00:18:46 +01001660 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001661
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001662 psa_set_key_usage_flags( &attributes, usage );
1663 psa_set_key_algorithm( &attributes, alg );
1664 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001665 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001666
Gilles Peskine1a960492019-11-26 17:12:21 +01001667 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1668 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001669
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001670 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1671 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001672 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001673 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1674 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001675
1676exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001677 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001678 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001679 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001680}
1681/* END_CASE */
1682
1683/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001684void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001685{
1686 /* Test each valid way of initializing the object, except for `= {0}`, as
1687 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1688 * though it's OK by the C standard. We could test for this, but we'd need
1689 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001690 psa_key_attributes_t func = psa_key_attributes_init( );
1691 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1692 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001693
1694 memset( &zero, 0, sizeof( zero ) );
1695
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001696 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1697 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1698 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001699
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001700 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1701 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1702 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1703
1704 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1705 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1706 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1707
1708 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1709 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1710 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1711
1712 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1713 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1714 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001715}
1716/* END_CASE */
1717
1718/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001719void mac_key_policy( int policy_usage,
1720 int policy_alg,
1721 int key_type,
1722 data_t *key_data,
1723 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001724{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001725 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001727 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001728 psa_status_t status;
1729 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001730
Gilles Peskine8817f612018-12-18 00:18:46 +01001731 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001732
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001733 psa_set_key_usage_flags( &attributes, policy_usage );
1734 psa_set_key_algorithm( &attributes, policy_alg );
1735 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001736
Gilles Peskine049c7532019-05-15 20:22:09 +02001737 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1738 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001739
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001740 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001741 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001742 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001743 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001744 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001745 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001746 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001747
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001748 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001749 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001750 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001751 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001752 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001753 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001754 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001755
1756exit:
1757 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001758 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001759 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001760}
1761/* END_CASE */
1762
1763/* BEGIN_CASE */
1764void cipher_key_policy( int policy_usage,
1765 int policy_alg,
1766 int key_type,
1767 data_t *key_data,
1768 int exercise_alg )
1769{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001770 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001772 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001773 psa_status_t status;
1774
Gilles Peskine8817f612018-12-18 00:18:46 +01001775 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001776
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001777 psa_set_key_usage_flags( &attributes, policy_usage );
1778 psa_set_key_algorithm( &attributes, policy_alg );
1779 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001780
Gilles Peskine049c7532019-05-15 20:22:09 +02001781 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1782 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001783
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001784 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001785 if( policy_alg == exercise_alg &&
1786 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001787 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001788 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001789 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001790 psa_cipher_abort( &operation );
1791
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001792 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001793 if( policy_alg == exercise_alg &&
1794 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001795 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001796 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001797 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001798
1799exit:
1800 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001801 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001802 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001803}
1804/* END_CASE */
1805
1806/* BEGIN_CASE */
1807void aead_key_policy( int policy_usage,
1808 int policy_alg,
1809 int key_type,
1810 data_t *key_data,
1811 int nonce_length_arg,
1812 int tag_length_arg,
1813 int exercise_alg )
1814{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001815 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001817 psa_status_t status;
1818 unsigned char nonce[16] = {0};
1819 size_t nonce_length = nonce_length_arg;
1820 unsigned char tag[16];
1821 size_t tag_length = tag_length_arg;
1822 size_t output_length;
1823
1824 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1825 TEST_ASSERT( tag_length <= sizeof( tag ) );
1826
Gilles Peskine8817f612018-12-18 00:18:46 +01001827 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001828
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001829 psa_set_key_usage_flags( &attributes, policy_usage );
1830 psa_set_key_algorithm( &attributes, policy_alg );
1831 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001832
Gilles Peskine049c7532019-05-15 20:22:09 +02001833 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1834 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001835
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001836 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001837 nonce, nonce_length,
1838 NULL, 0,
1839 NULL, 0,
1840 tag, tag_length,
1841 &output_length );
1842 if( policy_alg == exercise_alg &&
1843 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001844 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001845 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001846 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001847
1848 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001849 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001850 nonce, nonce_length,
1851 NULL, 0,
1852 tag, tag_length,
1853 NULL, 0,
1854 &output_length );
1855 if( policy_alg == exercise_alg &&
1856 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001857 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001858 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001859 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001860
1861exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001862 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001863 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001864}
1865/* END_CASE */
1866
1867/* BEGIN_CASE */
1868void asymmetric_encryption_key_policy( int policy_usage,
1869 int policy_alg,
1870 int key_type,
1871 data_t *key_data,
1872 int exercise_alg )
1873{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001874 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001875 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001876 psa_status_t status;
1877 size_t key_bits;
1878 size_t buffer_length;
1879 unsigned char *buffer = NULL;
1880 size_t output_length;
1881
Gilles Peskine8817f612018-12-18 00:18:46 +01001882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001883
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001884 psa_set_key_usage_flags( &attributes, policy_usage );
1885 psa_set_key_algorithm( &attributes, policy_alg );
1886 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001887
Gilles Peskine049c7532019-05-15 20:22:09 +02001888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1889 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001890
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001891 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1892 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001893 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1894 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001895 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001896
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001897 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001898 NULL, 0,
1899 NULL, 0,
1900 buffer, buffer_length,
1901 &output_length );
1902 if( policy_alg == exercise_alg &&
1903 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001904 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001905 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001906 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001907
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001908 if( buffer_length != 0 )
1909 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001910 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911 buffer, buffer_length,
1912 NULL, 0,
1913 buffer, buffer_length,
1914 &output_length );
1915 if( policy_alg == exercise_alg &&
1916 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001917 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001919 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920
1921exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001922 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001923 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001924 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001925 mbedtls_free( buffer );
1926}
1927/* END_CASE */
1928
1929/* BEGIN_CASE */
1930void asymmetric_signature_key_policy( int policy_usage,
1931 int policy_alg,
1932 int key_type,
1933 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001934 int exercise_alg,
1935 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001936{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001937 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001938 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001939 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001940 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1941 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1942 * compatible with the policy and `payload_length_arg` is supposed to be
1943 * a valid input length to sign. If `payload_length_arg <= 0`,
1944 * `exercise_alg` is supposed to be forbidden by the policy. */
1945 int compatible_alg = payload_length_arg > 0;
1946 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001947 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001948 size_t signature_length;
1949
Gilles Peskine8817f612018-12-18 00:18:46 +01001950 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001951
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001952 psa_set_key_usage_flags( &attributes, policy_usage );
1953 psa_set_key_algorithm( &attributes, policy_alg );
1954 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955
Gilles Peskine049c7532019-05-15 20:22:09 +02001956 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1957 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001959 status = psa_sign_hash( handle, exercise_alg,
1960 payload, payload_length,
1961 signature, sizeof( signature ),
1962 &signature_length );
1963 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001964 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001965 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001966 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001967
1968 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001969 status = psa_verify_hash( handle, exercise_alg,
1970 payload, payload_length,
1971 signature, sizeof( signature ) );
1972 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001973 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001974 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001975 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001976
1977exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001978 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001979 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001980}
1981/* END_CASE */
1982
Janos Follathba3fab92019-06-11 14:50:16 +01001983/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001984void derive_key_policy( int policy_usage,
1985 int policy_alg,
1986 int key_type,
1987 data_t *key_data,
1988 int exercise_alg )
1989{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001990 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001991 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001992 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001993 psa_status_t status;
1994
Gilles Peskine8817f612018-12-18 00:18:46 +01001995 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001996
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001997 psa_set_key_usage_flags( &attributes, policy_usage );
1998 psa_set_key_algorithm( &attributes, policy_alg );
1999 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002000
Gilles Peskine049c7532019-05-15 20:22:09 +02002001 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2002 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002003
Janos Follathba3fab92019-06-11 14:50:16 +01002004 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2005
2006 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2007 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002008 {
Janos Follathba3fab92019-06-11 14:50:16 +01002009 PSA_ASSERT( psa_key_derivation_input_bytes(
2010 &operation,
2011 PSA_KEY_DERIVATION_INPUT_SEED,
2012 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002013 }
Janos Follathba3fab92019-06-11 14:50:16 +01002014
2015 status = psa_key_derivation_input_key( &operation,
2016 PSA_KEY_DERIVATION_INPUT_SECRET,
2017 handle );
2018
Gilles Peskineea0fb492018-07-12 17:17:20 +02002019 if( policy_alg == exercise_alg &&
2020 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002021 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002022 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002023 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002024
2025exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002026 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002027 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002028 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002029}
2030/* END_CASE */
2031
2032/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002033void agreement_key_policy( int policy_usage,
2034 int policy_alg,
2035 int key_type_arg,
2036 data_t *key_data,
2037 int exercise_alg )
2038{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002039 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002041 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002042 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002043 psa_status_t status;
2044
Gilles Peskine8817f612018-12-18 00:18:46 +01002045 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002046
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002047 psa_set_key_usage_flags( &attributes, policy_usage );
2048 psa_set_key_algorithm( &attributes, policy_alg );
2049 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002050
Gilles Peskine049c7532019-05-15 20:22:09 +02002051 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2052 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002053
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002054 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2055 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002056
Gilles Peskine01d718c2018-09-18 12:01:02 +02002057 if( policy_alg == exercise_alg &&
2058 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002059 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002060 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002061 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002062
2063exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002064 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002065 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002066 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002067}
2068/* END_CASE */
2069
2070/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002071void key_policy_alg2( int key_type_arg, data_t *key_data,
2072 int usage_arg, int alg_arg, int alg2_arg )
2073{
2074 psa_key_handle_t handle = 0;
2075 psa_key_type_t key_type = key_type_arg;
2076 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2077 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2078 psa_key_usage_t usage = usage_arg;
2079 psa_algorithm_t alg = alg_arg;
2080 psa_algorithm_t alg2 = alg2_arg;
2081
2082 PSA_ASSERT( psa_crypto_init( ) );
2083
2084 psa_set_key_usage_flags( &attributes, usage );
2085 psa_set_key_algorithm( &attributes, alg );
2086 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2087 psa_set_key_type( &attributes, key_type );
2088 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2089 &handle ) );
2090
2091 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2092 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2093 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2094 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2095
2096 if( ! exercise_key( handle, usage, alg ) )
2097 goto exit;
2098 if( ! exercise_key( handle, usage, alg2 ) )
2099 goto exit;
2100
2101exit:
2102 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002103 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002104}
2105/* END_CASE */
2106
2107/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002108void raw_agreement_key_policy( int policy_usage,
2109 int policy_alg,
2110 int key_type_arg,
2111 data_t *key_data,
2112 int exercise_alg )
2113{
2114 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002116 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002117 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002118 psa_status_t status;
2119
2120 PSA_ASSERT( psa_crypto_init( ) );
2121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002122 psa_set_key_usage_flags( &attributes, policy_usage );
2123 psa_set_key_algorithm( &attributes, policy_alg );
2124 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002125
Gilles Peskine049c7532019-05-15 20:22:09 +02002126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2127 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002128
2129 status = raw_key_agreement_with_self( exercise_alg, handle );
2130
2131 if( policy_alg == exercise_alg &&
2132 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2133 PSA_ASSERT( status );
2134 else
2135 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2136
2137exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002138 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002139 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002140 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002141}
2142/* END_CASE */
2143
2144/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002145void copy_success( int source_usage_arg,
2146 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002147 int type_arg, data_t *material,
2148 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002149 int target_usage_arg,
2150 int target_alg_arg, int target_alg2_arg,
2151 int expected_usage_arg,
2152 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002153{
Gilles Peskineca25db92019-04-19 11:43:08 +02002154 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2155 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002156 psa_key_usage_t expected_usage = expected_usage_arg;
2157 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002158 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002159 psa_key_handle_t source_handle = 0;
2160 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002161 uint8_t *export_buffer = NULL;
2162
Gilles Peskine57ab7212019-01-28 13:03:09 +01002163 PSA_ASSERT( psa_crypto_init( ) );
2164
Gilles Peskineca25db92019-04-19 11:43:08 +02002165 /* Prepare the source key. */
2166 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2167 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002168 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002169 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002170 PSA_ASSERT( psa_import_key( &source_attributes,
2171 material->x, material->len,
2172 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002173 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002174
Gilles Peskineca25db92019-04-19 11:43:08 +02002175 /* Prepare the target attributes. */
2176 if( copy_attributes )
2177 target_attributes = source_attributes;
2178 if( target_usage_arg != -1 )
2179 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2180 if( target_alg_arg != -1 )
2181 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002182 if( target_alg2_arg != -1 )
2183 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002184
2185 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002186 PSA_ASSERT( psa_copy_key( source_handle,
2187 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002188
2189 /* Destroy the source to ensure that this doesn't affect the target. */
2190 PSA_ASSERT( psa_destroy_key( source_handle ) );
2191
2192 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002193 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2194 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2195 psa_get_key_type( &target_attributes ) );
2196 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2197 psa_get_key_bits( &target_attributes ) );
2198 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2199 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002200 TEST_EQUAL( expected_alg2,
2201 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002202 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2203 {
2204 size_t length;
2205 ASSERT_ALLOC( export_buffer, material->len );
2206 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2207 material->len, &length ) );
2208 ASSERT_COMPARE( material->x, material->len,
2209 export_buffer, length );
2210 }
2211 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2212 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002213 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2214 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002215
2216 PSA_ASSERT( psa_close_key( target_handle ) );
2217
2218exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002219 psa_reset_key_attributes( &source_attributes );
2220 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002221 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002222 mbedtls_free( export_buffer );
2223}
2224/* END_CASE */
2225
2226/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002227void copy_fail( int source_usage_arg,
2228 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002229 int type_arg, data_t *material,
2230 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002231 int target_usage_arg,
2232 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002233 int expected_status_arg )
2234{
2235 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2236 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2237 psa_key_handle_t source_handle = 0;
2238 psa_key_handle_t target_handle = 0;
2239
2240 PSA_ASSERT( psa_crypto_init( ) );
2241
2242 /* Prepare the source key. */
2243 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2244 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002245 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002246 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002247 PSA_ASSERT( psa_import_key( &source_attributes,
2248 material->x, material->len,
2249 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002250
2251 /* Prepare the target attributes. */
2252 psa_set_key_type( &target_attributes, target_type_arg );
2253 psa_set_key_bits( &target_attributes, target_bits_arg );
2254 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2255 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002256 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002257
2258 /* Try to copy the key. */
2259 TEST_EQUAL( psa_copy_key( source_handle,
2260 &target_attributes, &target_handle ),
2261 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002262
2263 PSA_ASSERT( psa_destroy_key( source_handle ) );
2264
Gilles Peskine4a644642019-05-03 17:14:08 +02002265exit:
2266 psa_reset_key_attributes( &source_attributes );
2267 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002268 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002269}
2270/* END_CASE */
2271
2272/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002273void hash_operation_init( )
2274{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002275 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002276 /* Test each valid way of initializing the object, except for `= {0}`, as
2277 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2278 * though it's OK by the C standard. We could test for this, but we'd need
2279 * to supress the Clang warning for the test. */
2280 psa_hash_operation_t func = psa_hash_operation_init( );
2281 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2282 psa_hash_operation_t zero;
2283
2284 memset( &zero, 0, sizeof( zero ) );
2285
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002286 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002287 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2288 PSA_ERROR_BAD_STATE );
2289 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2290 PSA_ERROR_BAD_STATE );
2291 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2292 PSA_ERROR_BAD_STATE );
2293
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002294 /* A default hash operation should be abortable without error. */
2295 PSA_ASSERT( psa_hash_abort( &func ) );
2296 PSA_ASSERT( psa_hash_abort( &init ) );
2297 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002298}
2299/* END_CASE */
2300
2301/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002302void hash_setup( int alg_arg,
2303 int expected_status_arg )
2304{
2305 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002306 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002307 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002308 psa_status_t status;
2309
Gilles Peskine8817f612018-12-18 00:18:46 +01002310 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002311
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002312 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002313 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002314
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002315 /* Whether setup succeeded or failed, abort must succeed. */
2316 PSA_ASSERT( psa_hash_abort( &operation ) );
2317
2318 /* If setup failed, reproduce the failure, so as to
2319 * test the resulting state of the operation object. */
2320 if( status != PSA_SUCCESS )
2321 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2322
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002323 /* Now the operation object should be reusable. */
2324#if defined(KNOWN_SUPPORTED_HASH_ALG)
2325 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2326 PSA_ASSERT( psa_hash_abort( &operation ) );
2327#endif
2328
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002329exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002330 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002331}
2332/* END_CASE */
2333
2334/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002335void hash_bad_order( )
2336{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002337 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002338 unsigned char input[] = "";
2339 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002340 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002341 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2342 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2343 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002344 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002345 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002346 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002347
Gilles Peskine8817f612018-12-18 00:18:46 +01002348 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002349
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002350 /* Call setup twice in a row. */
2351 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2352 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2353 PSA_ERROR_BAD_STATE );
2354 PSA_ASSERT( psa_hash_abort( &operation ) );
2355
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002356 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002357 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002358 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002359 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002360
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002361 /* Call update after finish. */
2362 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2363 PSA_ASSERT( psa_hash_finish( &operation,
2364 hash, sizeof( hash ), &hash_len ) );
2365 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002366 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002367 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002368
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002369 /* Call verify without calling setup beforehand. */
2370 TEST_EQUAL( psa_hash_verify( &operation,
2371 valid_hash, sizeof( valid_hash ) ),
2372 PSA_ERROR_BAD_STATE );
2373 PSA_ASSERT( psa_hash_abort( &operation ) );
2374
2375 /* Call verify after finish. */
2376 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2377 PSA_ASSERT( psa_hash_finish( &operation,
2378 hash, sizeof( hash ), &hash_len ) );
2379 TEST_EQUAL( psa_hash_verify( &operation,
2380 valid_hash, sizeof( valid_hash ) ),
2381 PSA_ERROR_BAD_STATE );
2382 PSA_ASSERT( psa_hash_abort( &operation ) );
2383
2384 /* Call verify twice in a row. */
2385 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2386 PSA_ASSERT( psa_hash_verify( &operation,
2387 valid_hash, sizeof( valid_hash ) ) );
2388 TEST_EQUAL( psa_hash_verify( &operation,
2389 valid_hash, sizeof( valid_hash ) ),
2390 PSA_ERROR_BAD_STATE );
2391 PSA_ASSERT( psa_hash_abort( &operation ) );
2392
2393 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002394 TEST_EQUAL( psa_hash_finish( &operation,
2395 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002396 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002397 PSA_ASSERT( psa_hash_abort( &operation ) );
2398
2399 /* Call finish twice in a row. */
2400 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2401 PSA_ASSERT( psa_hash_finish( &operation,
2402 hash, sizeof( hash ), &hash_len ) );
2403 TEST_EQUAL( psa_hash_finish( &operation,
2404 hash, sizeof( hash ), &hash_len ),
2405 PSA_ERROR_BAD_STATE );
2406 PSA_ASSERT( psa_hash_abort( &operation ) );
2407
2408 /* Call finish after calling verify. */
2409 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2410 PSA_ASSERT( psa_hash_verify( &operation,
2411 valid_hash, sizeof( valid_hash ) ) );
2412 TEST_EQUAL( psa_hash_finish( &operation,
2413 hash, sizeof( hash ), &hash_len ),
2414 PSA_ERROR_BAD_STATE );
2415 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002416
2417exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002418 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002419}
2420/* END_CASE */
2421
itayzafrir27e69452018-11-01 14:26:34 +02002422/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2423void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002424{
2425 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002426 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2427 * appended to it */
2428 unsigned char hash[] = {
2429 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2430 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2431 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002432 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002433 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002434
Gilles Peskine8817f612018-12-18 00:18:46 +01002435 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002436
itayzafrir27e69452018-11-01 14:26:34 +02002437 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002438 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002439 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002440 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002441
itayzafrir27e69452018-11-01 14:26:34 +02002442 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002443 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002444 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002445 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002446
itayzafrir27e69452018-11-01 14:26:34 +02002447 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002448 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002449 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002450 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002451
itayzafrirec93d302018-10-18 18:01:10 +03002452exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002453 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002454}
2455/* END_CASE */
2456
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002457/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2458void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002459{
2460 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002461 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002462 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002463 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002464 size_t hash_len;
2465
Gilles Peskine8817f612018-12-18 00:18:46 +01002466 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002467
itayzafrir58028322018-10-25 10:22:01 +03002468 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002469 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002470 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002471 hash, expected_size - 1, &hash_len ),
2472 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002473
2474exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002475 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002476}
2477/* END_CASE */
2478
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002479/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2480void hash_clone_source_state( )
2481{
2482 psa_algorithm_t alg = PSA_ALG_SHA_256;
2483 unsigned char hash[PSA_HASH_MAX_SIZE];
2484 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2485 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2486 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2487 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2488 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2489 size_t hash_len;
2490
2491 PSA_ASSERT( psa_crypto_init( ) );
2492 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2493
2494 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2495 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2496 PSA_ASSERT( psa_hash_finish( &op_finished,
2497 hash, sizeof( hash ), &hash_len ) );
2498 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2499 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2500
2501 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2502 PSA_ERROR_BAD_STATE );
2503
2504 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2505 PSA_ASSERT( psa_hash_finish( &op_init,
2506 hash, sizeof( hash ), &hash_len ) );
2507 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2508 PSA_ASSERT( psa_hash_finish( &op_finished,
2509 hash, sizeof( hash ), &hash_len ) );
2510 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2511 PSA_ASSERT( psa_hash_finish( &op_aborted,
2512 hash, sizeof( hash ), &hash_len ) );
2513
2514exit:
2515 psa_hash_abort( &op_source );
2516 psa_hash_abort( &op_init );
2517 psa_hash_abort( &op_setup );
2518 psa_hash_abort( &op_finished );
2519 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002520 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002521}
2522/* END_CASE */
2523
2524/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2525void hash_clone_target_state( )
2526{
2527 psa_algorithm_t alg = PSA_ALG_SHA_256;
2528 unsigned char hash[PSA_HASH_MAX_SIZE];
2529 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2530 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2531 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2532 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2533 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2534 size_t hash_len;
2535
2536 PSA_ASSERT( psa_crypto_init( ) );
2537
2538 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2539 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2540 PSA_ASSERT( psa_hash_finish( &op_finished,
2541 hash, sizeof( hash ), &hash_len ) );
2542 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2543 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2544
2545 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2546 PSA_ASSERT( psa_hash_finish( &op_target,
2547 hash, sizeof( hash ), &hash_len ) );
2548
2549 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2550 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2551 PSA_ERROR_BAD_STATE );
2552 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2553 PSA_ERROR_BAD_STATE );
2554
2555exit:
2556 psa_hash_abort( &op_target );
2557 psa_hash_abort( &op_init );
2558 psa_hash_abort( &op_setup );
2559 psa_hash_abort( &op_finished );
2560 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002561 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002562}
2563/* END_CASE */
2564
itayzafrir58028322018-10-25 10:22:01 +03002565/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002566void mac_operation_init( )
2567{
Jaeden Amero252ef282019-02-15 14:05:35 +00002568 const uint8_t input[1] = { 0 };
2569
Jaeden Amero769ce272019-01-04 11:48:03 +00002570 /* Test each valid way of initializing the object, except for `= {0}`, as
2571 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2572 * though it's OK by the C standard. We could test for this, but we'd need
2573 * to supress the Clang warning for the test. */
2574 psa_mac_operation_t func = psa_mac_operation_init( );
2575 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2576 psa_mac_operation_t zero;
2577
2578 memset( &zero, 0, sizeof( zero ) );
2579
Jaeden Amero252ef282019-02-15 14:05:35 +00002580 /* A freshly-initialized MAC operation should not be usable. */
2581 TEST_EQUAL( psa_mac_update( &func,
2582 input, sizeof( input ) ),
2583 PSA_ERROR_BAD_STATE );
2584 TEST_EQUAL( psa_mac_update( &init,
2585 input, sizeof( input ) ),
2586 PSA_ERROR_BAD_STATE );
2587 TEST_EQUAL( psa_mac_update( &zero,
2588 input, sizeof( input ) ),
2589 PSA_ERROR_BAD_STATE );
2590
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002591 /* A default MAC operation should be abortable without error. */
2592 PSA_ASSERT( psa_mac_abort( &func ) );
2593 PSA_ASSERT( psa_mac_abort( &init ) );
2594 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002595}
2596/* END_CASE */
2597
2598/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002599void mac_setup( int key_type_arg,
2600 data_t *key,
2601 int alg_arg,
2602 int expected_status_arg )
2603{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002604 psa_key_type_t key_type = key_type_arg;
2605 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002606 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002607 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002608 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2609#if defined(KNOWN_SUPPORTED_MAC_ALG)
2610 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2611#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002612
Gilles Peskine8817f612018-12-18 00:18:46 +01002613 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002614
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002615 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2616 &operation, &status ) )
2617 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002618 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002620 /* The operation object should be reusable. */
2621#if defined(KNOWN_SUPPORTED_MAC_ALG)
2622 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2623 smoke_test_key_data,
2624 sizeof( smoke_test_key_data ),
2625 KNOWN_SUPPORTED_MAC_ALG,
2626 &operation, &status ) )
2627 goto exit;
2628 TEST_EQUAL( status, PSA_SUCCESS );
2629#endif
2630
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002631exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002632 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002633}
2634/* END_CASE */
2635
2636/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002637void mac_bad_order( )
2638{
2639 psa_key_handle_t handle = 0;
2640 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2641 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2642 const uint8_t key[] = {
2643 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2644 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2645 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002647 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2648 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2649 size_t sign_mac_length = 0;
2650 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2651 const uint8_t verify_mac[] = {
2652 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2653 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2654 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2655
2656 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002657 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002658 psa_set_key_algorithm( &attributes, alg );
2659 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002660
Gilles Peskine73676cb2019-05-15 20:15:10 +02002661 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002662
Jaeden Amero252ef282019-02-15 14:05:35 +00002663 /* Call update without calling setup beforehand. */
2664 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2665 PSA_ERROR_BAD_STATE );
2666 PSA_ASSERT( psa_mac_abort( &operation ) );
2667
2668 /* Call sign finish without calling setup beforehand. */
2669 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2670 &sign_mac_length),
2671 PSA_ERROR_BAD_STATE );
2672 PSA_ASSERT( psa_mac_abort( &operation ) );
2673
2674 /* Call verify finish without calling setup beforehand. */
2675 TEST_EQUAL( psa_mac_verify_finish( &operation,
2676 verify_mac, sizeof( verify_mac ) ),
2677 PSA_ERROR_BAD_STATE );
2678 PSA_ASSERT( psa_mac_abort( &operation ) );
2679
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002680 /* Call setup twice in a row. */
2681 PSA_ASSERT( psa_mac_sign_setup( &operation,
2682 handle, alg ) );
2683 TEST_EQUAL( psa_mac_sign_setup( &operation,
2684 handle, alg ),
2685 PSA_ERROR_BAD_STATE );
2686 PSA_ASSERT( psa_mac_abort( &operation ) );
2687
Jaeden Amero252ef282019-02-15 14:05:35 +00002688 /* Call update after sign finish. */
2689 PSA_ASSERT( psa_mac_sign_setup( &operation,
2690 handle, alg ) );
2691 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2692 PSA_ASSERT( psa_mac_sign_finish( &operation,
2693 sign_mac, sizeof( sign_mac ),
2694 &sign_mac_length ) );
2695 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2696 PSA_ERROR_BAD_STATE );
2697 PSA_ASSERT( psa_mac_abort( &operation ) );
2698
2699 /* Call update after verify finish. */
2700 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002701 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002702 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2703 PSA_ASSERT( psa_mac_verify_finish( &operation,
2704 verify_mac, sizeof( verify_mac ) ) );
2705 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2706 PSA_ERROR_BAD_STATE );
2707 PSA_ASSERT( psa_mac_abort( &operation ) );
2708
2709 /* Call sign finish twice in a row. */
2710 PSA_ASSERT( psa_mac_sign_setup( &operation,
2711 handle, alg ) );
2712 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2713 PSA_ASSERT( psa_mac_sign_finish( &operation,
2714 sign_mac, sizeof( sign_mac ),
2715 &sign_mac_length ) );
2716 TEST_EQUAL( psa_mac_sign_finish( &operation,
2717 sign_mac, sizeof( sign_mac ),
2718 &sign_mac_length ),
2719 PSA_ERROR_BAD_STATE );
2720 PSA_ASSERT( psa_mac_abort( &operation ) );
2721
2722 /* Call verify finish twice in a row. */
2723 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002724 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002725 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2726 PSA_ASSERT( psa_mac_verify_finish( &operation,
2727 verify_mac, sizeof( verify_mac ) ) );
2728 TEST_EQUAL( psa_mac_verify_finish( &operation,
2729 verify_mac, sizeof( verify_mac ) ),
2730 PSA_ERROR_BAD_STATE );
2731 PSA_ASSERT( psa_mac_abort( &operation ) );
2732
2733 /* Setup sign but try verify. */
2734 PSA_ASSERT( psa_mac_sign_setup( &operation,
2735 handle, alg ) );
2736 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2737 TEST_EQUAL( psa_mac_verify_finish( &operation,
2738 verify_mac, sizeof( verify_mac ) ),
2739 PSA_ERROR_BAD_STATE );
2740 PSA_ASSERT( psa_mac_abort( &operation ) );
2741
2742 /* Setup verify but try sign. */
2743 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002744 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002745 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2746 TEST_EQUAL( psa_mac_sign_finish( &operation,
2747 sign_mac, sizeof( sign_mac ),
2748 &sign_mac_length ),
2749 PSA_ERROR_BAD_STATE );
2750 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002751
Gilles Peskine76b29a72019-05-28 14:08:50 +02002752 PSA_ASSERT( psa_destroy_key( handle ) );
2753
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002754exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002755 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002756}
2757/* END_CASE */
2758
2759/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002760void mac_sign( int key_type_arg,
2761 data_t *key,
2762 int alg_arg,
2763 data_t *input,
2764 data_t *expected_mac )
2765{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002766 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002767 psa_key_type_t key_type = key_type_arg;
2768 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002769 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002770 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002771 /* Leave a little extra room in the output buffer. At the end of the
2772 * test, we'll check that the implementation didn't overwrite onto
2773 * this extra room. */
2774 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2775 size_t mac_buffer_size =
2776 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2777 size_t mac_length = 0;
2778
2779 memset( actual_mac, '+', sizeof( actual_mac ) );
2780 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2781 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2782
Gilles Peskine8817f612018-12-18 00:18:46 +01002783 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002784
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002785 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002786 psa_set_key_algorithm( &attributes, alg );
2787 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002788
Gilles Peskine73676cb2019-05-15 20:15:10 +02002789 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002790
2791 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002792 PSA_ASSERT( psa_mac_sign_setup( &operation,
2793 handle, alg ) );
2794 PSA_ASSERT( psa_mac_update( &operation,
2795 input->x, input->len ) );
2796 PSA_ASSERT( psa_mac_sign_finish( &operation,
2797 actual_mac, mac_buffer_size,
2798 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002799
2800 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002801 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2802 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002803
2804 /* Verify that the end of the buffer is untouched. */
2805 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2806 sizeof( actual_mac ) - mac_length ) );
2807
2808exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002809 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002810 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002811}
2812/* END_CASE */
2813
2814/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002815void mac_verify( int key_type_arg,
2816 data_t *key,
2817 int alg_arg,
2818 data_t *input,
2819 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002820{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002821 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002822 psa_key_type_t key_type = key_type_arg;
2823 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002824 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002826
Gilles Peskine69c12672018-06-28 00:07:19 +02002827 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2828
Gilles Peskine8817f612018-12-18 00:18:46 +01002829 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002830
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002832 psa_set_key_algorithm( &attributes, alg );
2833 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002834
Gilles Peskine73676cb2019-05-15 20:15:10 +02002835 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002836
Gilles Peskine8817f612018-12-18 00:18:46 +01002837 PSA_ASSERT( psa_mac_verify_setup( &operation,
2838 handle, alg ) );
2839 PSA_ASSERT( psa_destroy_key( handle ) );
2840 PSA_ASSERT( psa_mac_update( &operation,
2841 input->x, input->len ) );
2842 PSA_ASSERT( psa_mac_verify_finish( &operation,
2843 expected_mac->x,
2844 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002845
2846exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002847 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002848 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002849}
2850/* END_CASE */
2851
2852/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002853void cipher_operation_init( )
2854{
Jaeden Ameroab439972019-02-15 14:12:05 +00002855 const uint8_t input[1] = { 0 };
2856 unsigned char output[1] = { 0 };
2857 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002858 /* Test each valid way of initializing the object, except for `= {0}`, as
2859 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2860 * though it's OK by the C standard. We could test for this, but we'd need
2861 * to supress the Clang warning for the test. */
2862 psa_cipher_operation_t func = psa_cipher_operation_init( );
2863 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2864 psa_cipher_operation_t zero;
2865
2866 memset( &zero, 0, sizeof( zero ) );
2867
Jaeden Ameroab439972019-02-15 14:12:05 +00002868 /* A freshly-initialized cipher operation should not be usable. */
2869 TEST_EQUAL( psa_cipher_update( &func,
2870 input, sizeof( input ),
2871 output, sizeof( output ),
2872 &output_length ),
2873 PSA_ERROR_BAD_STATE );
2874 TEST_EQUAL( psa_cipher_update( &init,
2875 input, sizeof( input ),
2876 output, sizeof( output ),
2877 &output_length ),
2878 PSA_ERROR_BAD_STATE );
2879 TEST_EQUAL( psa_cipher_update( &zero,
2880 input, sizeof( input ),
2881 output, sizeof( output ),
2882 &output_length ),
2883 PSA_ERROR_BAD_STATE );
2884
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002885 /* A default cipher operation should be abortable without error. */
2886 PSA_ASSERT( psa_cipher_abort( &func ) );
2887 PSA_ASSERT( psa_cipher_abort( &init ) );
2888 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002889}
2890/* END_CASE */
2891
2892/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002893void cipher_setup( int key_type_arg,
2894 data_t *key,
2895 int alg_arg,
2896 int expected_status_arg )
2897{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002898 psa_key_type_t key_type = key_type_arg;
2899 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002900 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002901 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002902 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002903#if defined(KNOWN_SUPPORTED_MAC_ALG)
2904 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2905#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002906
Gilles Peskine8817f612018-12-18 00:18:46 +01002907 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002908
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002909 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2910 &operation, &status ) )
2911 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002912 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002913
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002914 /* The operation object should be reusable. */
2915#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2916 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2917 smoke_test_key_data,
2918 sizeof( smoke_test_key_data ),
2919 KNOWN_SUPPORTED_CIPHER_ALG,
2920 &operation, &status ) )
2921 goto exit;
2922 TEST_EQUAL( status, PSA_SUCCESS );
2923#endif
2924
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002925exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002926 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002927}
2928/* END_CASE */
2929
2930/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002931void cipher_bad_order( )
2932{
2933 psa_key_handle_t handle = 0;
2934 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2935 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002936 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002937 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2938 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2939 const uint8_t key[] = {
2940 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2941 0xaa, 0xaa, 0xaa, 0xaa };
2942 const uint8_t text[] = {
2943 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2944 0xbb, 0xbb, 0xbb, 0xbb };
2945 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2946 size_t length = 0;
2947
2948 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002949 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2950 psa_set_key_algorithm( &attributes, alg );
2951 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02002952 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002953
2954
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002955 /* Call encrypt setup twice in a row. */
2956 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2957 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2958 PSA_ERROR_BAD_STATE );
2959 PSA_ASSERT( psa_cipher_abort( &operation ) );
2960
2961 /* Call decrypt setup twice in a row. */
2962 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2963 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2964 PSA_ERROR_BAD_STATE );
2965 PSA_ASSERT( psa_cipher_abort( &operation ) );
2966
Jaeden Ameroab439972019-02-15 14:12:05 +00002967 /* Generate an IV without calling setup beforehand. */
2968 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2969 buffer, sizeof( buffer ),
2970 &length ),
2971 PSA_ERROR_BAD_STATE );
2972 PSA_ASSERT( psa_cipher_abort( &operation ) );
2973
2974 /* Generate an IV twice in a row. */
2975 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2976 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2977 buffer, sizeof( buffer ),
2978 &length ) );
2979 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2980 buffer, sizeof( buffer ),
2981 &length ),
2982 PSA_ERROR_BAD_STATE );
2983 PSA_ASSERT( psa_cipher_abort( &operation ) );
2984
2985 /* Generate an IV after it's already set. */
2986 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2987 PSA_ASSERT( psa_cipher_set_iv( &operation,
2988 iv, sizeof( iv ) ) );
2989 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2990 buffer, sizeof( buffer ),
2991 &length ),
2992 PSA_ERROR_BAD_STATE );
2993 PSA_ASSERT( psa_cipher_abort( &operation ) );
2994
2995 /* Set an IV without calling setup beforehand. */
2996 TEST_EQUAL( psa_cipher_set_iv( &operation,
2997 iv, sizeof( iv ) ),
2998 PSA_ERROR_BAD_STATE );
2999 PSA_ASSERT( psa_cipher_abort( &operation ) );
3000
3001 /* Set 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_set_iv( &operation,
3006 iv, sizeof( iv ) ),
3007 PSA_ERROR_BAD_STATE );
3008 PSA_ASSERT( psa_cipher_abort( &operation ) );
3009
3010 /* Set an IV after it's already generated. */
3011 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3012 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3013 buffer, sizeof( buffer ),
3014 &length ) );
3015 TEST_EQUAL( psa_cipher_set_iv( &operation,
3016 iv, sizeof( iv ) ),
3017 PSA_ERROR_BAD_STATE );
3018 PSA_ASSERT( psa_cipher_abort( &operation ) );
3019
3020 /* Call update without calling setup beforehand. */
3021 TEST_EQUAL( psa_cipher_update( &operation,
3022 text, sizeof( text ),
3023 buffer, sizeof( buffer ),
3024 &length ),
3025 PSA_ERROR_BAD_STATE );
3026 PSA_ASSERT( psa_cipher_abort( &operation ) );
3027
3028 /* Call update without an IV where an IV is required. */
3029 TEST_EQUAL( psa_cipher_update( &operation,
3030 text, sizeof( text ),
3031 buffer, sizeof( buffer ),
3032 &length ),
3033 PSA_ERROR_BAD_STATE );
3034 PSA_ASSERT( psa_cipher_abort( &operation ) );
3035
3036 /* Call update after finish. */
3037 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3038 PSA_ASSERT( psa_cipher_set_iv( &operation,
3039 iv, sizeof( iv ) ) );
3040 PSA_ASSERT( psa_cipher_finish( &operation,
3041 buffer, sizeof( buffer ), &length ) );
3042 TEST_EQUAL( psa_cipher_update( &operation,
3043 text, sizeof( text ),
3044 buffer, sizeof( buffer ),
3045 &length ),
3046 PSA_ERROR_BAD_STATE );
3047 PSA_ASSERT( psa_cipher_abort( &operation ) );
3048
3049 /* Call finish without calling setup beforehand. */
3050 TEST_EQUAL( psa_cipher_finish( &operation,
3051 buffer, sizeof( buffer ), &length ),
3052 PSA_ERROR_BAD_STATE );
3053 PSA_ASSERT( psa_cipher_abort( &operation ) );
3054
3055 /* Call finish without an IV where an IV is required. */
3056 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3057 /* Not calling update means we are encrypting an empty buffer, which is OK
3058 * for cipher modes with padding. */
3059 TEST_EQUAL( psa_cipher_finish( &operation,
3060 buffer, sizeof( buffer ), &length ),
3061 PSA_ERROR_BAD_STATE );
3062 PSA_ASSERT( psa_cipher_abort( &operation ) );
3063
3064 /* Call finish twice in a row. */
3065 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3066 PSA_ASSERT( psa_cipher_set_iv( &operation,
3067 iv, sizeof( iv ) ) );
3068 PSA_ASSERT( psa_cipher_finish( &operation,
3069 buffer, sizeof( buffer ), &length ) );
3070 TEST_EQUAL( psa_cipher_finish( &operation,
3071 buffer, sizeof( buffer ), &length ),
3072 PSA_ERROR_BAD_STATE );
3073 PSA_ASSERT( psa_cipher_abort( &operation ) );
3074
Gilles Peskine76b29a72019-05-28 14:08:50 +02003075 PSA_ASSERT( psa_destroy_key( handle ) );
3076
Jaeden Ameroab439972019-02-15 14:12:05 +00003077exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003078 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003079}
3080/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003081
Gilles Peskine50e586b2018-06-08 14:28:46 +02003082/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003083void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003084 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003085 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003086 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003087{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003088 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003089 psa_status_t status;
3090 psa_key_type_t key_type = key_type_arg;
3091 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003092 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003093 unsigned char *output = NULL;
3094 size_t output_buffer_size = 0;
3095 size_t function_output_length = 0;
3096 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003097 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003098 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003099
Gilles Peskine8817f612018-12-18 00:18:46 +01003100 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003101
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003102 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3103 psa_set_key_algorithm( &attributes, alg );
3104 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003105
Gilles Peskine73676cb2019-05-15 20:15:10 +02003106 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003107
Gilles Peskine8817f612018-12-18 00:18:46 +01003108 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3109 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003110
Gilles Peskine423005e2019-05-06 15:22:57 +02003111 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003112 output_buffer_size = ( (size_t) input->len +
3113 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003114 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003115
Gilles Peskine8817f612018-12-18 00:18:46 +01003116 PSA_ASSERT( psa_cipher_update( &operation,
3117 input->x, input->len,
3118 output, output_buffer_size,
3119 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003120 total_output_length += function_output_length;
3121 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003122 output + total_output_length,
3123 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003124 &function_output_length );
3125 total_output_length += function_output_length;
3126
Gilles Peskinefe11b722018-12-18 00:24:04 +01003127 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003128 if( expected_status == PSA_SUCCESS )
3129 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003130 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003131 ASSERT_COMPARE( expected_output->x, expected_output->len,
3132 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003133 }
3134
3135exit:
3136 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003137 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003138 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003139}
3140/* END_CASE */
3141
3142/* BEGIN_CASE */
3143void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003144 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003145 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003146 int first_part_size_arg,
3147 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003148 data_t *expected_output )
3149{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003150 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003151 psa_key_type_t key_type = key_type_arg;
3152 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003153 size_t first_part_size = first_part_size_arg;
3154 size_t output1_length = output1_length_arg;
3155 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003156 unsigned char *output = NULL;
3157 size_t output_buffer_size = 0;
3158 size_t function_output_length = 0;
3159 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003160 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003161 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003162
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003164
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003165 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3166 psa_set_key_algorithm( &attributes, alg );
3167 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003168
Gilles Peskine73676cb2019-05-15 20:15:10 +02003169 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003170
Gilles Peskine8817f612018-12-18 00:18:46 +01003171 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3172 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003173
Gilles Peskine423005e2019-05-06 15:22:57 +02003174 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003175 output_buffer_size = ( (size_t) input->len +
3176 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003177 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003178
Gilles Peskinee0866522019-02-19 19:44:00 +01003179 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003180 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3181 output, output_buffer_size,
3182 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003183 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003184 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003185 PSA_ASSERT( psa_cipher_update( &operation,
3186 input->x + first_part_size,
3187 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003188 output + total_output_length,
3189 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003190 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003191 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003192 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003193 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003194 output + total_output_length,
3195 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003196 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003197 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003198 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003199
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003200 ASSERT_COMPARE( expected_output->x, expected_output->len,
3201 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003202
3203exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003204 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003205 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003206 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003207}
3208/* END_CASE */
3209
3210/* BEGIN_CASE */
3211void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003212 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003213 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003214 int first_part_size_arg,
3215 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003216 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003217{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003218 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003219
3220 psa_key_type_t key_type = key_type_arg;
3221 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003222 size_t first_part_size = first_part_size_arg;
3223 size_t output1_length = output1_length_arg;
3224 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003225 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003226 size_t output_buffer_size = 0;
3227 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003228 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003229 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003231
Gilles Peskine8817f612018-12-18 00:18:46 +01003232 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003233
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003234 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3235 psa_set_key_algorithm( &attributes, alg );
3236 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003237
Gilles Peskine73676cb2019-05-15 20:15:10 +02003238 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003239
Gilles Peskine8817f612018-12-18 00:18:46 +01003240 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3241 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003242
Gilles Peskine423005e2019-05-06 15:22:57 +02003243 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003244
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003245 output_buffer_size = ( (size_t) input->len +
3246 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003247 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003248
Gilles Peskinee0866522019-02-19 19:44:00 +01003249 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_cipher_update( &operation,
3251 input->x, first_part_size,
3252 output, output_buffer_size,
3253 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003254 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003255 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003256 PSA_ASSERT( psa_cipher_update( &operation,
3257 input->x + first_part_size,
3258 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003259 output + total_output_length,
3260 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003261 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003262 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003263 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003265 output + total_output_length,
3266 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003267 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003268 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003269 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003270
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003271 ASSERT_COMPARE( expected_output->x, expected_output->len,
3272 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003273
3274exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003275 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003276 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003277 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003278}
3279/* END_CASE */
3280
Gilles Peskine50e586b2018-06-08 14:28:46 +02003281/* BEGIN_CASE */
3282void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003283 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003284 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003285 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003286{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003287 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003288 psa_status_t status;
3289 psa_key_type_t key_type = key_type_arg;
3290 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003291 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003292 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003293 size_t output_buffer_size = 0;
3294 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003295 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003296 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003297 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003298
Gilles Peskine8817f612018-12-18 00:18:46 +01003299 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003300
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003301 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3302 psa_set_key_algorithm( &attributes, alg );
3303 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003304
Gilles Peskine73676cb2019-05-15 20:15:10 +02003305 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003306
Gilles Peskine8817f612018-12-18 00:18:46 +01003307 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3308 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003309
Gilles Peskine423005e2019-05-06 15:22:57 +02003310 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003311
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003312 output_buffer_size = ( (size_t) input->len +
3313 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003314 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003315
Gilles Peskine8817f612018-12-18 00:18:46 +01003316 PSA_ASSERT( psa_cipher_update( &operation,
3317 input->x, input->len,
3318 output, output_buffer_size,
3319 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003320 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003321 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003322 output + total_output_length,
3323 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003324 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003325 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003326 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003327
3328 if( expected_status == PSA_SUCCESS )
3329 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003330 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003331 ASSERT_COMPARE( expected_output->x, expected_output->len,
3332 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003333 }
3334
Gilles Peskine50e586b2018-06-08 14:28:46 +02003335exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003336 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003337 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003338 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003339}
3340/* END_CASE */
3341
Gilles Peskine50e586b2018-06-08 14:28:46 +02003342/* BEGIN_CASE */
3343void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003344 data_t *key,
3345 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003346{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003347 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003348 psa_key_type_t key_type = key_type_arg;
3349 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003350 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003351 size_t iv_size = 16;
3352 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003353 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003354 size_t output1_size = 0;
3355 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003356 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003357 size_t output2_size = 0;
3358 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003359 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003360 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3361 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003363
Gilles Peskine8817f612018-12-18 00:18:46 +01003364 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003365
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003366 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3367 psa_set_key_algorithm( &attributes, alg );
3368 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003369
Gilles Peskine73676cb2019-05-15 20:15:10 +02003370 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003371
Gilles Peskine8817f612018-12-18 00:18:46 +01003372 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3373 handle, alg ) );
3374 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3375 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003376
Gilles Peskine8817f612018-12-18 00:18:46 +01003377 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3378 iv, iv_size,
3379 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003380 output1_size = ( (size_t) input->len +
3381 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003382 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003383
Gilles Peskine8817f612018-12-18 00:18:46 +01003384 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3385 output1, output1_size,
3386 &output1_length ) );
3387 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003388 output1 + output1_length,
3389 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003390 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003391
Gilles Peskine048b7f02018-06-08 14:20:49 +02003392 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003393
Gilles Peskine8817f612018-12-18 00:18:46 +01003394 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003395
3396 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003397 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003398
Gilles Peskine8817f612018-12-18 00:18:46 +01003399 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3400 iv, iv_length ) );
3401 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3402 output2, output2_size,
3403 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003404 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003405 PSA_ASSERT( psa_cipher_finish( &operation2,
3406 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003407 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003408 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003409
Gilles Peskine048b7f02018-06-08 14:20:49 +02003410 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003411
Gilles Peskine8817f612018-12-18 00:18:46 +01003412 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003413
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003414 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003415
3416exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003417 mbedtls_free( output1 );
3418 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003419 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003420 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003421}
3422/* END_CASE */
3423
3424/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003425void cipher_verify_output_multipart( int alg_arg,
3426 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003427 data_t *key,
3428 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003429 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003430{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003431 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003432 psa_key_type_t key_type = key_type_arg;
3433 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003434 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003435 unsigned char iv[16] = {0};
3436 size_t iv_size = 16;
3437 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003438 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003439 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003440 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003441 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003442 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003443 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003444 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003445 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3446 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003448
Gilles Peskine8817f612018-12-18 00:18:46 +01003449 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003450
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3452 psa_set_key_algorithm( &attributes, alg );
3453 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003454
Gilles Peskine73676cb2019-05-15 20:15:10 +02003455 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003456
Gilles Peskine8817f612018-12-18 00:18:46 +01003457 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3458 handle, alg ) );
3459 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3460 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003461
Gilles Peskine8817f612018-12-18 00:18:46 +01003462 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3463 iv, iv_size,
3464 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003465 output1_buffer_size = ( (size_t) input->len +
3466 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003467 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003468
Gilles Peskinee0866522019-02-19 19:44:00 +01003469 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003470
Gilles Peskine8817f612018-12-18 00:18:46 +01003471 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3472 output1, output1_buffer_size,
3473 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003474 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003475
Gilles Peskine8817f612018-12-18 00:18:46 +01003476 PSA_ASSERT( psa_cipher_update( &operation1,
3477 input->x + first_part_size,
3478 input->len - first_part_size,
3479 output1, output1_buffer_size,
3480 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003481 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003482
Gilles Peskine8817f612018-12-18 00:18:46 +01003483 PSA_ASSERT( psa_cipher_finish( &operation1,
3484 output1 + output1_length,
3485 output1_buffer_size - output1_length,
3486 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003487 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003488
Gilles Peskine8817f612018-12-18 00:18:46 +01003489 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003490
Gilles Peskine048b7f02018-06-08 14:20:49 +02003491 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003492 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003493
Gilles Peskine8817f612018-12-18 00:18:46 +01003494 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3495 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003496
Gilles Peskine8817f612018-12-18 00:18:46 +01003497 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3498 output2, output2_buffer_size,
3499 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003500 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003501
Gilles Peskine8817f612018-12-18 00:18:46 +01003502 PSA_ASSERT( psa_cipher_update( &operation2,
3503 output1 + first_part_size,
3504 output1_length - first_part_size,
3505 output2, output2_buffer_size,
3506 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003507 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003508
Gilles Peskine8817f612018-12-18 00:18:46 +01003509 PSA_ASSERT( psa_cipher_finish( &operation2,
3510 output2 + output2_length,
3511 output2_buffer_size - output2_length,
3512 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003513 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003514
Gilles Peskine8817f612018-12-18 00:18:46 +01003515 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003516
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003517 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003518
3519exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003520 mbedtls_free( output1 );
3521 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003522 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003523 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003524}
3525/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003526
Gilles Peskine20035e32018-02-03 22:44:14 +01003527/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003528void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003529 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003530 data_t *nonce,
3531 data_t *additional_data,
3532 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003533 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003534{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003535 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003536 psa_key_type_t key_type = key_type_arg;
3537 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003538 unsigned char *output_data = NULL;
3539 size_t output_size = 0;
3540 size_t output_length = 0;
3541 unsigned char *output_data2 = NULL;
3542 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003543 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003544 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003546
Gilles Peskine4abf7412018-06-18 16:35:34 +02003547 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003548 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3549 * should be exact. */
3550 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3551 TEST_EQUAL( output_size,
3552 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003553 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003554
Gilles Peskine8817f612018-12-18 00:18:46 +01003555 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003556
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003557 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3558 psa_set_key_algorithm( &attributes, alg );
3559 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003560
Gilles Peskine049c7532019-05-15 20:22:09 +02003561 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3562 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003563
Gilles Peskinefe11b722018-12-18 00:24:04 +01003564 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3565 nonce->x, nonce->len,
3566 additional_data->x,
3567 additional_data->len,
3568 input_data->x, input_data->len,
3569 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003570 &output_length ),
3571 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003572
3573 if( PSA_SUCCESS == expected_result )
3574 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003575 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003576
Gilles Peskine003a4a92019-05-14 16:09:40 +02003577 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3578 * should be exact. */
3579 TEST_EQUAL( input_data->len,
3580 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3581
Gilles Peskinefe11b722018-12-18 00:24:04 +01003582 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3583 nonce->x, nonce->len,
3584 additional_data->x,
3585 additional_data->len,
3586 output_data, output_length,
3587 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003588 &output_length2 ),
3589 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003590
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003591 ASSERT_COMPARE( input_data->x, input_data->len,
3592 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003593 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003594
Gilles Peskinea1cac842018-06-11 19:33:02 +02003595exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003596 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003597 mbedtls_free( output_data );
3598 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003599 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003600}
3601/* END_CASE */
3602
3603/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003604void aead_encrypt( int key_type_arg, data_t *key_data,
3605 int alg_arg,
3606 data_t *nonce,
3607 data_t *additional_data,
3608 data_t *input_data,
3609 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003610{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003611 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003612 psa_key_type_t key_type = key_type_arg;
3613 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003614 unsigned char *output_data = NULL;
3615 size_t output_size = 0;
3616 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003617 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003618 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003619
Gilles Peskine4abf7412018-06-18 16:35:34 +02003620 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003621 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3622 * should be exact. */
3623 TEST_EQUAL( output_size,
3624 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003625 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003626
Gilles Peskine8817f612018-12-18 00:18:46 +01003627 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003628
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003629 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3630 psa_set_key_algorithm( &attributes, alg );
3631 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003632
Gilles Peskine049c7532019-05-15 20:22:09 +02003633 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3634 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003635
Gilles Peskine8817f612018-12-18 00:18:46 +01003636 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3637 nonce->x, nonce->len,
3638 additional_data->x, additional_data->len,
3639 input_data->x, input_data->len,
3640 output_data, output_size,
3641 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003642
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003643 ASSERT_COMPARE( expected_result->x, expected_result->len,
3644 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003645
Gilles Peskinea1cac842018-06-11 19:33:02 +02003646exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003647 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003648 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003649 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003650}
3651/* END_CASE */
3652
3653/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003654void aead_decrypt( int key_type_arg, data_t *key_data,
3655 int alg_arg,
3656 data_t *nonce,
3657 data_t *additional_data,
3658 data_t *input_data,
3659 data_t *expected_data,
3660 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003661{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003662 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003663 psa_key_type_t key_type = key_type_arg;
3664 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003665 unsigned char *output_data = NULL;
3666 size_t output_size = 0;
3667 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003668 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003670 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003671
Gilles Peskine003a4a92019-05-14 16:09:40 +02003672 output_size = input_data->len - tag_length;
3673 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3674 * should be exact. */
3675 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3676 TEST_EQUAL( output_size,
3677 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003678 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003679
Gilles Peskine8817f612018-12-18 00:18:46 +01003680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003681
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003682 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3683 psa_set_key_algorithm( &attributes, alg );
3684 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003685
Gilles Peskine049c7532019-05-15 20:22:09 +02003686 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3687 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003688
Gilles Peskinefe11b722018-12-18 00:24:04 +01003689 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3690 nonce->x, nonce->len,
3691 additional_data->x,
3692 additional_data->len,
3693 input_data->x, input_data->len,
3694 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003695 &output_length ),
3696 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003697
Gilles Peskine2d277862018-06-18 15:41:12 +02003698 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003699 ASSERT_COMPARE( expected_data->x, expected_data->len,
3700 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003701
Gilles Peskinea1cac842018-06-11 19:33:02 +02003702exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003703 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003704 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003705 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003706}
3707/* END_CASE */
3708
3709/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003710void signature_size( int type_arg,
3711 int bits,
3712 int alg_arg,
3713 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003714{
3715 psa_key_type_t type = type_arg;
3716 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003717 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003718
Gilles Peskinefe11b722018-12-18 00:24:04 +01003719 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003720#if defined(MBEDTLS_TEST_DEPRECATED)
3721 TEST_EQUAL( actual_size,
3722 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3723#endif /* MBEDTLS_TEST_DEPRECATED */
3724
Gilles Peskinee59236f2018-01-27 23:32:46 +01003725exit:
3726 ;
3727}
3728/* END_CASE */
3729
3730/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003731void sign_deterministic( int key_type_arg, data_t *key_data,
3732 int alg_arg, data_t *input_data,
3733 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003734{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003735 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003736 psa_key_type_t key_type = key_type_arg;
3737 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003738 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003739 unsigned char *signature = NULL;
3740 size_t signature_size;
3741 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003742 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003743
Gilles Peskine8817f612018-12-18 00:18:46 +01003744 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003745
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003746 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003747 psa_set_key_algorithm( &attributes, alg );
3748 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003749
Gilles Peskine049c7532019-05-15 20:22:09 +02003750 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3751 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003752 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3753 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003754
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003755 /* Allocate a buffer which has the size advertized by the
3756 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003757 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003758 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003759 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003760 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003761 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003762
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003763 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003764 PSA_ASSERT( psa_sign_hash( handle, alg,
3765 input_data->x, input_data->len,
3766 signature, signature_size,
3767 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003768 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003769 ASSERT_COMPARE( output_data->x, output_data->len,
3770 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003771
3772exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003773 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003774 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003775 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003776 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003777}
3778/* END_CASE */
3779
3780/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003781void sign_fail( int key_type_arg, data_t *key_data,
3782 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003783 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003784{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003785 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003786 psa_key_type_t key_type = key_type_arg;
3787 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003788 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003789 psa_status_t actual_status;
3790 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003791 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003792 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003793 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003794
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003795 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003796
Gilles Peskine8817f612018-12-18 00:18:46 +01003797 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003798
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003799 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003800 psa_set_key_algorithm( &attributes, alg );
3801 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003802
Gilles Peskine049c7532019-05-15 20:22:09 +02003803 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3804 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003805
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003806 actual_status = psa_sign_hash( handle, alg,
3807 input_data->x, input_data->len,
3808 signature, signature_size,
3809 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003810 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003811 /* The value of *signature_length is unspecified on error, but
3812 * whatever it is, it should be less than signature_size, so that
3813 * if the caller tries to read *signature_length bytes without
3814 * checking the error code then they don't overflow a buffer. */
3815 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003816
3817exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003818 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003819 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003820 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003821 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003822}
3823/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003824
3825/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003826void sign_verify( int key_type_arg, data_t *key_data,
3827 int alg_arg, data_t *input_data )
3828{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003829 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003830 psa_key_type_t key_type = key_type_arg;
3831 psa_algorithm_t alg = alg_arg;
3832 size_t key_bits;
3833 unsigned char *signature = NULL;
3834 size_t signature_size;
3835 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003837
Gilles Peskine8817f612018-12-18 00:18:46 +01003838 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003839
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003840 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003841 psa_set_key_algorithm( &attributes, alg );
3842 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003843
Gilles Peskine049c7532019-05-15 20:22:09 +02003844 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3845 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003846 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3847 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003848
3849 /* Allocate a buffer which has the size advertized by the
3850 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003851 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003852 key_bits, alg );
3853 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003854 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003855 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003856
3857 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003858 PSA_ASSERT( psa_sign_hash( handle, alg,
3859 input_data->x, input_data->len,
3860 signature, signature_size,
3861 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003862 /* Check that the signature length looks sensible. */
3863 TEST_ASSERT( signature_length <= signature_size );
3864 TEST_ASSERT( signature_length > 0 );
3865
3866 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003867 PSA_ASSERT( psa_verify_hash( handle, alg,
3868 input_data->x, input_data->len,
3869 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003870
3871 if( input_data->len != 0 )
3872 {
3873 /* Flip a bit in the input and verify that the signature is now
3874 * detected as invalid. Flip a bit at the beginning, not at the end,
3875 * because ECDSA may ignore the last few bits of the input. */
3876 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003877 TEST_EQUAL( psa_verify_hash( handle, alg,
3878 input_data->x, input_data->len,
3879 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003880 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003881 }
3882
3883exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003884 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003885 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003886 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003887 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003888}
3889/* END_CASE */
3890
3891/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003892void asymmetric_verify( int key_type_arg, data_t *key_data,
3893 int alg_arg, data_t *hash_data,
3894 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003895{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003896 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003897 psa_key_type_t key_type = key_type_arg;
3898 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003899 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003900
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003901 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003902
Gilles Peskine8817f612018-12-18 00:18:46 +01003903 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003904
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003905 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003906 psa_set_key_algorithm( &attributes, alg );
3907 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003908
Gilles Peskine049c7532019-05-15 20:22:09 +02003909 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3910 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003911
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003912 PSA_ASSERT( psa_verify_hash( handle, alg,
3913 hash_data->x, hash_data->len,
3914 signature_data->x, signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003915exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003916 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003917 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003918 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003919}
3920/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003921
3922/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003923void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3924 int alg_arg, data_t *hash_data,
3925 data_t *signature_data,
3926 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003927{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003928 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003929 psa_key_type_t key_type = key_type_arg;
3930 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931 psa_status_t actual_status;
3932 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003934
Gilles Peskine8817f612018-12-18 00:18:46 +01003935 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003936
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003937 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003938 psa_set_key_algorithm( &attributes, alg );
3939 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003940
Gilles Peskine049c7532019-05-15 20:22:09 +02003941 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3942 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003943
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003944 actual_status = psa_verify_hash( handle, alg,
3945 hash_data->x, hash_data->len,
3946 signature_data->x, signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947
Gilles Peskinefe11b722018-12-18 00:24:04 +01003948 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003949
3950exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003951 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003952 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003953 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954}
3955/* END_CASE */
3956
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003957/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003958void asymmetric_encrypt( int key_type_arg,
3959 data_t *key_data,
3960 int alg_arg,
3961 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003962 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003963 int expected_output_length_arg,
3964 int expected_status_arg )
3965{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003966 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003967 psa_key_type_t key_type = key_type_arg;
3968 psa_algorithm_t alg = alg_arg;
3969 size_t expected_output_length = expected_output_length_arg;
3970 size_t key_bits;
3971 unsigned char *output = NULL;
3972 size_t output_size;
3973 size_t output_length = ~0;
3974 psa_status_t actual_status;
3975 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003976 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003977
Gilles Peskine8817f612018-12-18 00:18:46 +01003978 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003979
Gilles Peskine656896e2018-06-29 19:12:28 +02003980 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003981 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3982 psa_set_key_algorithm( &attributes, alg );
3983 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003984 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3985 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003986
3987 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003988 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3989 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003990 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003991 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003992
3993 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003994 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003995 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003996 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003997 output, output_size,
3998 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003999 TEST_EQUAL( actual_status, expected_status );
4000 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004001
Gilles Peskine68428122018-06-30 18:42:41 +02004002 /* If the label is empty, the test framework puts a non-null pointer
4003 * in label->x. Test that a null pointer works as well. */
4004 if( label->len == 0 )
4005 {
4006 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004007 if( output_size != 0 )
4008 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004009 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004010 input_data->x, input_data->len,
4011 NULL, label->len,
4012 output, output_size,
4013 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004014 TEST_EQUAL( actual_status, expected_status );
4015 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004016 }
4017
Gilles Peskine656896e2018-06-29 19:12:28 +02004018exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004019 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004020 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004021 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004022 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004023}
4024/* END_CASE */
4025
4026/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004027void asymmetric_encrypt_decrypt( int key_type_arg,
4028 data_t *key_data,
4029 int alg_arg,
4030 data_t *input_data,
4031 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004032{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004033 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004034 psa_key_type_t key_type = key_type_arg;
4035 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004036 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004037 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004038 size_t output_size;
4039 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004040 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004041 size_t output2_size;
4042 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004044
Gilles Peskine8817f612018-12-18 00:18:46 +01004045 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004046
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004047 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4048 psa_set_key_algorithm( &attributes, alg );
4049 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004050
Gilles Peskine049c7532019-05-15 20:22:09 +02004051 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4052 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004053
4054 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004055 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4056 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004057 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004058 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004059 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004060 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004061
Gilles Peskineeebd7382018-06-08 18:11:54 +02004062 /* We test encryption by checking that encrypt-then-decrypt gives back
4063 * the original plaintext because of the non-optional random
4064 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004065 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4066 input_data->x, input_data->len,
4067 label->x, label->len,
4068 output, output_size,
4069 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004070 /* We don't know what ciphertext length to expect, but check that
4071 * it looks sensible. */
4072 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004073
Gilles Peskine8817f612018-12-18 00:18:46 +01004074 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4075 output, output_length,
4076 label->x, label->len,
4077 output2, output2_size,
4078 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004079 ASSERT_COMPARE( input_data->x, input_data->len,
4080 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004081
4082exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004083 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004084 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004085 mbedtls_free( output );
4086 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004087 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004088}
4089/* END_CASE */
4090
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004092void asymmetric_decrypt( int key_type_arg,
4093 data_t *key_data,
4094 int alg_arg,
4095 data_t *input_data,
4096 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004097 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004098{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004099 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004100 psa_key_type_t key_type = key_type_arg;
4101 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004102 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004103 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004104 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004106
Jaeden Amero412654a2019-02-06 12:57:46 +00004107 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004108 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004109
Gilles Peskine8817f612018-12-18 00:18:46 +01004110 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004111
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004112 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4113 psa_set_key_algorithm( &attributes, alg );
4114 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004115
Gilles Peskine049c7532019-05-15 20:22:09 +02004116 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4117 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004118
Gilles Peskine8817f612018-12-18 00:18:46 +01004119 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4120 input_data->x, input_data->len,
4121 label->x, label->len,
4122 output,
4123 output_size,
4124 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004125 ASSERT_COMPARE( expected_data->x, expected_data->len,
4126 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004127
Gilles Peskine68428122018-06-30 18:42:41 +02004128 /* If the label is empty, the test framework puts a non-null pointer
4129 * in label->x. Test that a null pointer works as well. */
4130 if( label->len == 0 )
4131 {
4132 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004133 if( output_size != 0 )
4134 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004135 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4136 input_data->x, input_data->len,
4137 NULL, label->len,
4138 output,
4139 output_size,
4140 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004141 ASSERT_COMPARE( expected_data->x, expected_data->len,
4142 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004143 }
4144
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004145exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004146 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004147 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004148 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004149 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004150}
4151/* END_CASE */
4152
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004153/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004154void asymmetric_decrypt_fail( int key_type_arg,
4155 data_t *key_data,
4156 int alg_arg,
4157 data_t *input_data,
4158 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004159 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004160 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004161{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004162 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004163 psa_key_type_t key_type = key_type_arg;
4164 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004165 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004166 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004167 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004168 psa_status_t actual_status;
4169 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004171
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004172 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004173
Gilles Peskine8817f612018-12-18 00:18:46 +01004174 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004175
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004176 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4177 psa_set_key_algorithm( &attributes, alg );
4178 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004179
Gilles Peskine049c7532019-05-15 20:22:09 +02004180 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4181 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004182
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004183 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004184 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004185 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004186 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004187 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004188 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004189 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004190
Gilles Peskine68428122018-06-30 18:42:41 +02004191 /* If the label is empty, the test framework puts a non-null pointer
4192 * in label->x. Test that a null pointer works as well. */
4193 if( label->len == 0 )
4194 {
4195 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004196 if( output_size != 0 )
4197 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004198 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004199 input_data->x, input_data->len,
4200 NULL, label->len,
4201 output, output_size,
4202 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004203 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004204 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004205 }
4206
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004207exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004208 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004209 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004210 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004211 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004212}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004213/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004214
4215/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004216void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004217{
4218 /* Test each valid way of initializing the object, except for `= {0}`, as
4219 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4220 * though it's OK by the C standard. We could test for this, but we'd need
4221 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004222 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004223 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4224 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4225 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004226
4227 memset( &zero, 0, sizeof( zero ) );
4228
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004229 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004230 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004231 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004232 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004233 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004234 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004235 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004236
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004237 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004238 PSA_ASSERT( psa_key_derivation_abort(&func) );
4239 PSA_ASSERT( psa_key_derivation_abort(&init) );
4240 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004241}
4242/* END_CASE */
4243
Janos Follath16de4a42019-06-13 16:32:24 +01004244/* BEGIN_CASE */
4245void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004246{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004247 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004248 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004249 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004250
Gilles Peskine8817f612018-12-18 00:18:46 +01004251 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004252
Janos Follath16de4a42019-06-13 16:32:24 +01004253 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004254 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004255
4256exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004257 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004258 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004259}
4260/* END_CASE */
4261
Janos Follathaf3c2a02019-06-12 12:34:34 +01004262/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004263void derive_set_capacity( int alg_arg, int capacity_arg,
4264 int expected_status_arg )
4265{
4266 psa_algorithm_t alg = alg_arg;
4267 size_t capacity = capacity_arg;
4268 psa_status_t expected_status = expected_status_arg;
4269 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4270
4271 PSA_ASSERT( psa_crypto_init( ) );
4272
4273 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4274
4275 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4276 expected_status );
4277
4278exit:
4279 psa_key_derivation_abort( &operation );
4280 PSA_DONE( );
4281}
4282/* END_CASE */
4283
4284/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004285void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004286 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004287 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004288 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004289 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004290 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004291 int expected_status_arg3,
4292 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004293{
4294 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004295 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4296 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004297 psa_status_t expected_statuses[] = {expected_status_arg1,
4298 expected_status_arg2,
4299 expected_status_arg3};
4300 data_t *inputs[] = {input1, input2, input3};
4301 psa_key_handle_t handles[] = {0, 0, 0};
4302 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4303 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4304 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004305 psa_key_type_t output_key_type = output_key_type_arg;
4306 psa_key_handle_t output_handle = 0;
4307 psa_status_t expected_output_status = expected_output_status_arg;
4308 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004309
4310 PSA_ASSERT( psa_crypto_init( ) );
4311
4312 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4313 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004314
4315 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4316
4317 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4318 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004319 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004320 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004321 psa_set_key_type( &attributes, key_types[i] );
4322 PSA_ASSERT( psa_import_key( &attributes,
4323 inputs[i]->x, inputs[i]->len,
4324 &handles[i] ) );
4325 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4326 handles[i] ),
4327 expected_statuses[i] );
4328 }
4329 else
4330 {
4331 TEST_EQUAL( psa_key_derivation_input_bytes(
4332 &operation, steps[i],
4333 inputs[i]->x, inputs[i]->len ),
4334 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004335 }
4336 }
4337
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004338 if( output_key_type != PSA_KEY_TYPE_NONE )
4339 {
4340 psa_reset_key_attributes( &attributes );
4341 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4342 psa_set_key_bits( &attributes, 8 );
4343 actual_output_status =
4344 psa_key_derivation_output_key( &attributes, &operation,
4345 &output_handle );
4346 }
4347 else
4348 {
4349 uint8_t buffer[1];
4350 actual_output_status =
4351 psa_key_derivation_output_bytes( &operation,
4352 buffer, sizeof( buffer ) );
4353 }
4354 TEST_EQUAL( actual_output_status, expected_output_status );
4355
Janos Follathaf3c2a02019-06-12 12:34:34 +01004356exit:
4357 psa_key_derivation_abort( &operation );
4358 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4359 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004360 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004361 PSA_DONE( );
4362}
4363/* END_CASE */
4364
Janos Follathd958bb72019-07-03 15:02:16 +01004365/* BEGIN_CASE */
4366void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004367{
Janos Follathd958bb72019-07-03 15:02:16 +01004368 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004369 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004370 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004371 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004372 unsigned char input1[] = "Input 1";
4373 size_t input1_length = sizeof( input1 );
4374 unsigned char input2[] = "Input 2";
4375 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004376 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004377 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004378 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4379 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4380 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004381 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004382
Gilles Peskine8817f612018-12-18 00:18:46 +01004383 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004384
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004385 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4386 psa_set_key_algorithm( &attributes, alg );
4387 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004388
Gilles Peskine73676cb2019-05-15 20:15:10 +02004389 PSA_ASSERT( psa_import_key( &attributes,
4390 key_data, sizeof( key_data ),
4391 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004392
4393 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004394 if( !setup_key_derivation_wrap( &operation, handle, alg,
4395 input1, input1_length,
4396 input2, input2_length,
4397 capacity ) )
4398 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004399
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004400 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004401 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004402 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004403
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004404 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004405
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004407 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004408
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004409exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004410 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004411 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004412 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004413}
4414/* END_CASE */
4415
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004416/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004417void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004418{
4419 uint8_t output_buffer[16];
4420 size_t buffer_size = 16;
4421 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004423
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004424 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4425 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004426 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004427
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004429 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004430
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004431 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004432
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004433 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4434 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004435 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004436
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004437 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004438 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004439
4440exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004441 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004442}
4443/* END_CASE */
4444
4445/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004446void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004447 int step1_arg, data_t *input1,
4448 int step2_arg, data_t *input2,
4449 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004450 int requested_capacity_arg,
4451 data_t *expected_output1,
4452 data_t *expected_output2 )
4453{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004454 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004455 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4456 data_t *inputs[] = {input1, input2, input3};
4457 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004458 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004459 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004460 uint8_t *expected_outputs[2] =
4461 {expected_output1->x, expected_output2->x};
4462 size_t output_sizes[2] =
4463 {expected_output1->len, expected_output2->len};
4464 size_t output_buffer_size = 0;
4465 uint8_t *output_buffer = NULL;
4466 size_t expected_capacity;
4467 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004469 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004470 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004471
4472 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4473 {
4474 if( output_sizes[i] > output_buffer_size )
4475 output_buffer_size = output_sizes[i];
4476 if( output_sizes[i] == 0 )
4477 expected_outputs[i] = NULL;
4478 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004479 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004480 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004481
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004482 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4483 psa_set_key_algorithm( &attributes, alg );
4484 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004485
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004486 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004487 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4488 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4489 requested_capacity ) );
4490 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004491 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004492 switch( steps[i] )
4493 {
4494 case 0:
4495 break;
4496 case PSA_KEY_DERIVATION_INPUT_SECRET:
4497 PSA_ASSERT( psa_import_key( &attributes,
4498 inputs[i]->x, inputs[i]->len,
4499 &handles[i] ) );
4500 PSA_ASSERT( psa_key_derivation_input_key(
4501 &operation, steps[i],
4502 handles[i] ) );
4503 break;
4504 default:
4505 PSA_ASSERT( psa_key_derivation_input_bytes(
4506 &operation, steps[i],
4507 inputs[i]->x, inputs[i]->len ) );
4508 break;
4509 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004510 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004511
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004512 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004513 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004514 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004515 expected_capacity = requested_capacity;
4516
4517 /* Expansion phase. */
4518 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4519 {
4520 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004521 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004522 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004523 if( expected_capacity == 0 && output_sizes[i] == 0 )
4524 {
4525 /* Reading 0 bytes when 0 bytes are available can go either way. */
4526 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004527 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004528 continue;
4529 }
4530 else if( expected_capacity == 0 ||
4531 output_sizes[i] > expected_capacity )
4532 {
4533 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004534 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004535 expected_capacity = 0;
4536 continue;
4537 }
4538 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004539 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004540 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004541 ASSERT_COMPARE( output_buffer, output_sizes[i],
4542 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004543 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004544 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004546 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004547 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004548 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004549 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004550
4551exit:
4552 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004553 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004554 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4555 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004556 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004557}
4558/* END_CASE */
4559
4560/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004561void derive_full( int alg_arg,
4562 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004563 data_t *input1,
4564 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004565 int requested_capacity_arg )
4566{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004567 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004568 psa_algorithm_t alg = alg_arg;
4569 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004570 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004571 unsigned char output_buffer[16];
4572 size_t expected_capacity = requested_capacity;
4573 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004575
Gilles Peskine8817f612018-12-18 00:18:46 +01004576 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004577
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004578 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4579 psa_set_key_algorithm( &attributes, alg );
4580 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004581
Gilles Peskine049c7532019-05-15 20:22:09 +02004582 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4583 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004584
Janos Follathf2815ea2019-07-03 12:41:36 +01004585 if( !setup_key_derivation_wrap( &operation, handle, alg,
4586 input1->x, input1->len,
4587 input2->x, input2->len,
4588 requested_capacity ) )
4589 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004590
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004591 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004592 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004593 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004594
4595 /* Expansion phase. */
4596 while( current_capacity > 0 )
4597 {
4598 size_t read_size = sizeof( output_buffer );
4599 if( read_size > current_capacity )
4600 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004601 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004602 output_buffer,
4603 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004604 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004605 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004606 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004607 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004608 }
4609
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004610 /* Check that the operation refuses to go over capacity. */
4611 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004612 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004613
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004614 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004615
4616exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004617 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004618 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004619 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004620}
4621/* END_CASE */
4622
Janos Follathe60c9052019-07-03 13:51:30 +01004623/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004624void derive_key_exercise( int alg_arg,
4625 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004626 data_t *input1,
4627 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004628 int derived_type_arg,
4629 int derived_bits_arg,
4630 int derived_usage_arg,
4631 int derived_alg_arg )
4632{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004633 psa_key_handle_t base_handle = 0;
4634 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004635 psa_algorithm_t alg = alg_arg;
4636 psa_key_type_t derived_type = derived_type_arg;
4637 size_t derived_bits = derived_bits_arg;
4638 psa_key_usage_t derived_usage = derived_usage_arg;
4639 psa_algorithm_t derived_alg = derived_alg_arg;
4640 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004641 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004643 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004644
Gilles Peskine8817f612018-12-18 00:18:46 +01004645 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004646
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004647 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4648 psa_set_key_algorithm( &attributes, alg );
4649 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004650 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4651 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004652
4653 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004654 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4655 input1->x, input1->len,
4656 input2->x, input2->len, capacity ) )
4657 goto exit;
4658
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004659 psa_set_key_usage_flags( &attributes, derived_usage );
4660 psa_set_key_algorithm( &attributes, derived_alg );
4661 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004662 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004664 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004665
4666 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004667 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4668 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4669 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004670
4671 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004672 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004673 goto exit;
4674
4675exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004676 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004677 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004678 psa_destroy_key( base_handle );
4679 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004680 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004681}
4682/* END_CASE */
4683
Janos Follath42fd8882019-07-03 14:17:09 +01004684/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004685void derive_key_export( int alg_arg,
4686 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004687 data_t *input1,
4688 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004689 int bytes1_arg,
4690 int bytes2_arg )
4691{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004692 psa_key_handle_t base_handle = 0;
4693 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004694 psa_algorithm_t alg = alg_arg;
4695 size_t bytes1 = bytes1_arg;
4696 size_t bytes2 = bytes2_arg;
4697 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004698 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004699 uint8_t *output_buffer = NULL;
4700 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004701 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4702 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004703 size_t length;
4704
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004705 ASSERT_ALLOC( output_buffer, capacity );
4706 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004707 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004708
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004709 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4710 psa_set_key_algorithm( &base_attributes, alg );
4711 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004712 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4713 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004714
4715 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01004716 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4717 input1->x, input1->len,
4718 input2->x, input2->len, capacity ) )
4719 goto exit;
4720
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004722 output_buffer,
4723 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004725
4726 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01004727 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4728 input1->x, input1->len,
4729 input2->x, input2->len, capacity ) )
4730 goto exit;
4731
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004732 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4733 psa_set_key_algorithm( &derived_attributes, 0 );
4734 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004735 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004737 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004738 PSA_ASSERT( psa_export_key( derived_handle,
4739 export_buffer, bytes1,
4740 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004741 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004742 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004743 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004745 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004746 PSA_ASSERT( psa_export_key( derived_handle,
4747 export_buffer + bytes1, bytes2,
4748 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004749 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004750
4751 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004752 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4753 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004754
4755exit:
4756 mbedtls_free( output_buffer );
4757 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004758 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004759 psa_destroy_key( base_handle );
4760 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004761 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004762}
4763/* END_CASE */
4764
4765/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004766void derive_key( int alg_arg,
4767 data_t *key_data, data_t *input1, data_t *input2,
4768 int type_arg, int bits_arg,
4769 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02004770{
4771 psa_key_handle_t base_handle = 0;
4772 psa_key_handle_t derived_handle = 0;
4773 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004774 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004775 size_t bits = bits_arg;
4776 psa_status_t expected_status = expected_status_arg;
4777 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4778 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4779 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4780
4781 PSA_ASSERT( psa_crypto_init( ) );
4782
4783 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4784 psa_set_key_algorithm( &base_attributes, alg );
4785 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4786 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4787 &base_handle ) );
4788
4789 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4790 input1->x, input1->len,
4791 input2->x, input2->len, SIZE_MAX ) )
4792 goto exit;
4793
4794 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4795 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004796 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004797 psa_set_key_bits( &derived_attributes, bits );
4798 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
4799 &derived_handle ),
4800 expected_status );
4801
4802exit:
4803 psa_key_derivation_abort( &operation );
4804 psa_destroy_key( base_handle );
4805 psa_destroy_key( derived_handle );
4806 PSA_DONE( );
4807}
4808/* END_CASE */
4809
4810/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004811void key_agreement_setup( int alg_arg,
4812 int our_key_type_arg, data_t *our_key_data,
4813 data_t *peer_key_data,
4814 int expected_status_arg )
4815{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004816 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004817 psa_algorithm_t alg = alg_arg;
4818 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004819 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004821 psa_status_t expected_status = expected_status_arg;
4822 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004823
Gilles Peskine8817f612018-12-18 00:18:46 +01004824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004825
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4827 psa_set_key_algorithm( &attributes, alg );
4828 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004829 PSA_ASSERT( psa_import_key( &attributes,
4830 our_key_data->x, our_key_data->len,
4831 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004832
Gilles Peskine77f40d82019-04-11 21:27:06 +02004833 /* The tests currently include inputs that should fail at either step.
4834 * Test cases that fail at the setup step should be changed to call
4835 * key_derivation_setup instead, and this function should be renamed
4836 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004838 if( status == PSA_SUCCESS )
4839 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004840 TEST_EQUAL( psa_key_derivation_key_agreement(
4841 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4842 our_key,
4843 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004844 expected_status );
4845 }
4846 else
4847 {
4848 TEST_ASSERT( status == expected_status );
4849 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004850
4851exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004853 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004854 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004855}
4856/* END_CASE */
4857
4858/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004859void raw_key_agreement( int alg_arg,
4860 int our_key_type_arg, data_t *our_key_data,
4861 data_t *peer_key_data,
4862 data_t *expected_output )
4863{
4864 psa_key_handle_t our_key = 0;
4865 psa_algorithm_t alg = alg_arg;
4866 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004868 unsigned char *output = NULL;
4869 size_t output_length = ~0;
4870
4871 ASSERT_ALLOC( output, expected_output->len );
4872 PSA_ASSERT( psa_crypto_init( ) );
4873
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004874 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4875 psa_set_key_algorithm( &attributes, alg );
4876 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004877 PSA_ASSERT( psa_import_key( &attributes,
4878 our_key_data->x, our_key_data->len,
4879 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004880
Gilles Peskinebe697d82019-05-16 18:00:41 +02004881 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4882 peer_key_data->x, peer_key_data->len,
4883 output, expected_output->len,
4884 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004885 ASSERT_COMPARE( output, output_length,
4886 expected_output->x, expected_output->len );
4887
4888exit:
4889 mbedtls_free( output );
4890 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004891 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004892}
4893/* END_CASE */
4894
4895/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004896void key_agreement_capacity( int alg_arg,
4897 int our_key_type_arg, data_t *our_key_data,
4898 data_t *peer_key_data,
4899 int expected_capacity_arg )
4900{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004901 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004902 psa_algorithm_t alg = alg_arg;
4903 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004906 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004907 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004908
Gilles Peskine8817f612018-12-18 00:18:46 +01004909 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004910
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004911 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4912 psa_set_key_algorithm( &attributes, alg );
4913 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004914 PSA_ASSERT( psa_import_key( &attributes,
4915 our_key_data->x, our_key_data->len,
4916 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004917
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004919 PSA_ASSERT( psa_key_derivation_key_agreement(
4920 &operation,
4921 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4922 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004923 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4924 {
4925 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004926 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004927 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004928 NULL, 0 ) );
4929 }
Gilles Peskine59685592018-09-18 12:11:34 +02004930
Gilles Peskinebf491972018-10-25 22:36:12 +02004931 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004932 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004933 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004934 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004935
Gilles Peskinebf491972018-10-25 22:36:12 +02004936 /* Test the actual capacity by reading the output. */
4937 while( actual_capacity > sizeof( output ) )
4938 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004939 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004940 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004941 actual_capacity -= sizeof( output );
4942 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004943 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004944 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004946 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004947
Gilles Peskine59685592018-09-18 12:11:34 +02004948exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004950 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004951 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004952}
4953/* END_CASE */
4954
4955/* BEGIN_CASE */
4956void key_agreement_output( int alg_arg,
4957 int our_key_type_arg, data_t *our_key_data,
4958 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004959 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004960{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004961 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004962 psa_algorithm_t alg = alg_arg;
4963 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004965 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004966 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004967
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004968 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4969 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004970
Gilles Peskine8817f612018-12-18 00:18:46 +01004971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004972
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004973 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4974 psa_set_key_algorithm( &attributes, alg );
4975 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004976 PSA_ASSERT( psa_import_key( &attributes,
4977 our_key_data->x, our_key_data->len,
4978 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004979
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004980 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004981 PSA_ASSERT( psa_key_derivation_key_agreement(
4982 &operation,
4983 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4984 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004985 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4986 {
4987 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004988 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004989 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004990 NULL, 0 ) );
4991 }
Gilles Peskine59685592018-09-18 12:11:34 +02004992
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004993 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004994 actual_output,
4995 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004996 ASSERT_COMPARE( actual_output, expected_output1->len,
4997 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004998 if( expected_output2->len != 0 )
4999 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005000 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005001 actual_output,
5002 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005003 ASSERT_COMPARE( actual_output, expected_output2->len,
5004 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005005 }
Gilles Peskine59685592018-09-18 12:11:34 +02005006
5007exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005009 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005010 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005011 mbedtls_free( actual_output );
5012}
5013/* END_CASE */
5014
5015/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005016void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005017{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005018 size_t bytes = bytes_arg;
5019 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005020 unsigned char *output = NULL;
5021 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005022 size_t i;
5023 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005024
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005025 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5026 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005027 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005028
Gilles Peskine8817f612018-12-18 00:18:46 +01005029 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005030
Gilles Peskinea50d7392018-06-21 10:22:13 +02005031 /* Run several times, to ensure that every output byte will be
5032 * nonzero at least once with overwhelming probability
5033 * (2^(-8*number_of_runs)). */
5034 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005035 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005036 if( bytes != 0 )
5037 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005038 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005039
5040 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005041 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5042 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005043
5044 for( i = 0; i < bytes; i++ )
5045 {
5046 if( output[i] != 0 )
5047 ++changed[i];
5048 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005049 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005050
5051 /* Check that every byte was changed to nonzero at least once. This
5052 * validates that psa_generate_random is overwriting every byte of
5053 * the output buffer. */
5054 for( i = 0; i < bytes; i++ )
5055 {
5056 TEST_ASSERT( changed[i] != 0 );
5057 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005058
5059exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005060 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005061 mbedtls_free( output );
5062 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005063}
5064/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005065
5066/* BEGIN_CASE */
5067void generate_key( int type_arg,
5068 int bits_arg,
5069 int usage_arg,
5070 int alg_arg,
5071 int expected_status_arg )
5072{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005073 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005074 psa_key_type_t type = type_arg;
5075 psa_key_usage_t usage = usage_arg;
5076 size_t bits = bits_arg;
5077 psa_algorithm_t alg = alg_arg;
5078 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005079 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005080 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005081
Gilles Peskine8817f612018-12-18 00:18:46 +01005082 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005083
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005084 psa_set_key_usage_flags( &attributes, usage );
5085 psa_set_key_algorithm( &attributes, alg );
5086 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005087 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005088
5089 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005090 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005091 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005092 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005093
5094 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005095 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5096 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5097 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005098
Gilles Peskine818ca122018-06-20 18:16:48 +02005099 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005100 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005101 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005102
5103exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005104 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005105 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005106 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005107}
5108/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005109
Gilles Peskinee56e8782019-04-26 17:34:02 +02005110/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5111void generate_key_rsa( int bits_arg,
5112 data_t *e_arg,
5113 int expected_status_arg )
5114{
5115 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005116 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005117 size_t bits = bits_arg;
5118 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5119 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5120 psa_status_t expected_status = expected_status_arg;
5121 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5122 uint8_t *exported = NULL;
5123 size_t exported_size =
5124 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5125 size_t exported_length = SIZE_MAX;
5126 uint8_t *e_read_buffer = NULL;
5127 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005128 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005129 size_t e_read_length = SIZE_MAX;
5130
5131 if( e_arg->len == 0 ||
5132 ( e_arg->len == 3 &&
5133 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5134 {
5135 is_default_public_exponent = 1;
5136 e_read_size = 0;
5137 }
5138 ASSERT_ALLOC( e_read_buffer, e_read_size );
5139 ASSERT_ALLOC( exported, exported_size );
5140
5141 PSA_ASSERT( psa_crypto_init( ) );
5142
5143 psa_set_key_usage_flags( &attributes, usage );
5144 psa_set_key_algorithm( &attributes, alg );
5145 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5146 e_arg->x, e_arg->len ) );
5147 psa_set_key_bits( &attributes, bits );
5148
5149 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005150 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005151 if( expected_status != PSA_SUCCESS )
5152 goto exit;
5153
5154 /* Test the key information */
5155 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5156 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5157 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5158 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5159 e_read_buffer, e_read_size,
5160 &e_read_length ) );
5161 if( is_default_public_exponent )
5162 TEST_EQUAL( e_read_length, 0 );
5163 else
5164 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5165
5166 /* Do something with the key according to its type and permitted usage. */
5167 if( ! exercise_key( handle, usage, alg ) )
5168 goto exit;
5169
5170 /* Export the key and check the public exponent. */
5171 PSA_ASSERT( psa_export_public_key( handle,
5172 exported, exported_size,
5173 &exported_length ) );
5174 {
5175 uint8_t *p = exported;
5176 uint8_t *end = exported + exported_length;
5177 size_t len;
5178 /* RSAPublicKey ::= SEQUENCE {
5179 * modulus INTEGER, -- n
5180 * publicExponent INTEGER } -- e
5181 */
5182 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005183 MBEDTLS_ASN1_SEQUENCE |
5184 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005185 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5186 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5187 MBEDTLS_ASN1_INTEGER ) );
5188 if( len >= 1 && p[0] == 0 )
5189 {
5190 ++p;
5191 --len;
5192 }
5193 if( e_arg->len == 0 )
5194 {
5195 TEST_EQUAL( len, 3 );
5196 TEST_EQUAL( p[0], 1 );
5197 TEST_EQUAL( p[1], 0 );
5198 TEST_EQUAL( p[2], 1 );
5199 }
5200 else
5201 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5202 }
5203
5204exit:
5205 psa_reset_key_attributes( &attributes );
5206 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005207 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005208 mbedtls_free( e_read_buffer );
5209 mbedtls_free( exported );
5210}
5211/* END_CASE */
5212
Darryl Greend49a4992018-06-18 17:27:26 +01005213/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005214void persistent_key_load_key_from_storage( data_t *data,
5215 int type_arg, int bits_arg,
5216 int usage_flags_arg, int alg_arg,
5217 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005218{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005219 psa_key_id_t key_id = 1;
5220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005221 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005222 psa_key_handle_t base_key = 0;
5223 psa_key_type_t type = type_arg;
5224 size_t bits = bits_arg;
5225 psa_key_usage_t usage_flags = usage_flags_arg;
5226 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005227 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005228 unsigned char *first_export = NULL;
5229 unsigned char *second_export = NULL;
5230 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5231 size_t first_exported_length;
5232 size_t second_exported_length;
5233
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005234 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5235 {
5236 ASSERT_ALLOC( first_export, export_size );
5237 ASSERT_ALLOC( second_export, export_size );
5238 }
Darryl Greend49a4992018-06-18 17:27:26 +01005239
Gilles Peskine8817f612018-12-18 00:18:46 +01005240 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005241
Gilles Peskinec87af662019-05-15 16:12:22 +02005242 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005243 psa_set_key_usage_flags( &attributes, usage_flags );
5244 psa_set_key_algorithm( &attributes, alg );
5245 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005246 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005247
Darryl Green0c6575a2018-11-07 16:05:30 +00005248 switch( generation_method )
5249 {
5250 case IMPORT_KEY:
5251 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005252 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5253 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005254 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005255
Darryl Green0c6575a2018-11-07 16:05:30 +00005256 case GENERATE_KEY:
5257 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005258 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005259 break;
5260
5261 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005262 {
5263 /* Create base key */
5264 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5265 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5266 psa_set_key_usage_flags( &base_attributes,
5267 PSA_KEY_USAGE_DERIVE );
5268 psa_set_key_algorithm( &base_attributes, derive_alg );
5269 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005270 PSA_ASSERT( psa_import_key( &base_attributes,
5271 data->x, data->len,
5272 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005273 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005275 PSA_ASSERT( psa_key_derivation_input_key(
5276 &operation,
5277 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005278 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005280 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005281 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5282 &operation,
5283 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005285 PSA_ASSERT( psa_destroy_key( base_key ) );
5286 base_key = 0;
5287 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005288 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005289 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005290 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005291
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005292 /* Export the key if permitted by the key policy. */
5293 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5294 {
5295 PSA_ASSERT( psa_export_key( handle,
5296 first_export, export_size,
5297 &first_exported_length ) );
5298 if( generation_method == IMPORT_KEY )
5299 ASSERT_COMPARE( data->x, data->len,
5300 first_export, first_exported_length );
5301 }
Darryl Greend49a4992018-06-18 17:27:26 +01005302
5303 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005304 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005305 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005306 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005307
Darryl Greend49a4992018-06-18 17:27:26 +01005308 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005309 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005310 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5311 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5312 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5313 PSA_KEY_LIFETIME_PERSISTENT );
5314 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5315 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5316 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5317 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005318
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005319 /* Export the key again if permitted by the key policy. */
5320 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005321 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005322 PSA_ASSERT( psa_export_key( handle,
5323 second_export, export_size,
5324 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005325 ASSERT_COMPARE( first_export, first_exported_length,
5326 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005327 }
5328
5329 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005330 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005331 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005332
5333exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005334 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005335 mbedtls_free( first_export );
5336 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005338 psa_destroy_key( base_key );
5339 if( handle == 0 )
5340 {
5341 /* In case there was a test failure after creating the persistent key
5342 * but while it was not open, try to re-open the persistent key
5343 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005344 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005345 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005346 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005347 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005348}
5349/* END_CASE */