blob: a2be082af1eb815c0560988dc34d7e947cffd436 [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 Peskine667c1112019-12-03 19:03:20 +0100109#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
110int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
111{
112 /* At the moment, anything that isn't a built-in lifetime is either
113 * a secure element or unassigned. */
114 return( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
115 lifetime != PSA_KEY_LIFETIME_PERSISTENT );
116}
117#else
118int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
119{
120 (void) lifetime;
121 return( 0 );
122}
123#endif
124
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200125/** Test if a buffer contains a constant byte value.
126 *
127 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200128 *
129 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200130 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200131 * \param size Size of the buffer in bytes.
132 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200133 * \return 1 if the buffer is all-bits-zero.
134 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200135 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200136static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200137{
138 size_t i;
139 for( i = 0; i < size; i++ )
140 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200141 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200142 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200143 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200144 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200145}
Gilles Peskine818ca122018-06-20 18:16:48 +0200146
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200147/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
148static int asn1_write_10x( unsigned char **p,
149 unsigned char *start,
150 size_t bits,
151 unsigned char x )
152{
153 int ret;
154 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200155 if( bits == 0 )
156 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
157 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200158 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300159 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200160 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
161 *p -= len;
162 ( *p )[len-1] = x;
163 if( bits % 8 == 0 )
164 ( *p )[1] |= 1;
165 else
166 ( *p )[0] |= 1 << ( bits % 8 );
167 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
168 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
169 MBEDTLS_ASN1_INTEGER ) );
170 return( len );
171}
172
173static int construct_fake_rsa_key( unsigned char *buffer,
174 size_t buffer_size,
175 unsigned char **p,
176 size_t bits,
177 int keypair )
178{
179 size_t half_bits = ( bits + 1 ) / 2;
180 int ret;
181 int len = 0;
182 /* Construct something that looks like a DER encoding of
183 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
184 * RSAPrivateKey ::= SEQUENCE {
185 * version Version,
186 * modulus INTEGER, -- n
187 * publicExponent INTEGER, -- e
188 * privateExponent INTEGER, -- d
189 * prime1 INTEGER, -- p
190 * prime2 INTEGER, -- q
191 * exponent1 INTEGER, -- d mod (p-1)
192 * exponent2 INTEGER, -- d mod (q-1)
193 * coefficient INTEGER, -- (inverse of q) mod p
194 * otherPrimeInfos OtherPrimeInfos OPTIONAL
195 * }
196 * Or, for a public key, the same structure with only
197 * version, modulus and publicExponent.
198 */
199 *p = buffer + buffer_size;
200 if( keypair )
201 {
202 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
203 asn1_write_10x( p, buffer, half_bits, 1 ) );
204 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
205 asn1_write_10x( p, buffer, half_bits, 1 ) );
206 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
207 asn1_write_10x( p, buffer, half_bits, 1 ) );
208 MBEDTLS_ASN1_CHK_ADD( len, /* q */
209 asn1_write_10x( p, buffer, half_bits, 1 ) );
210 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
211 asn1_write_10x( p, buffer, half_bits, 3 ) );
212 MBEDTLS_ASN1_CHK_ADD( len, /* d */
213 asn1_write_10x( p, buffer, bits, 1 ) );
214 }
215 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
216 asn1_write_10x( p, buffer, 17, 1 ) );
217 MBEDTLS_ASN1_CHK_ADD( len, /* n */
218 asn1_write_10x( p, buffer, bits, 1 ) );
219 if( keypair )
220 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
221 mbedtls_asn1_write_int( p, buffer, 0 ) );
222 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
223 {
224 const unsigned char tag =
225 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
226 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
227 }
228 return( len );
229}
230
Gilles Peskine667c1112019-12-03 19:03:20 +0100231int check_key_attributes_sanity( psa_key_handle_t key )
232{
233 int ok = 0;
234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
235 psa_key_lifetime_t lifetime;
236 psa_key_id_t id;
237 psa_key_type_t type;
238 psa_key_type_t bits;
239
240 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
241 lifetime = psa_get_key_lifetime( &attributes );
242 id = psa_get_key_id( &attributes );
243 type = psa_get_key_type( &attributes );
244 bits = psa_get_key_bits( &attributes );
245
246 /* Persistence */
247 if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
248 TEST_ASSERT( id == 0 );
249 else
250 {
251 TEST_ASSERT(
252 ( PSA_KEY_ID_USER_MIN <= id && id <= PSA_KEY_ID_USER_MAX ) ||
253 ( PSA_KEY_ID_USER_MIN <= id && id <= PSA_KEY_ID_USER_MAX ) );
254 }
255#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
256 /* randomly-generated 64-bit constant, should never appear in test data */
257 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
258 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
259 if( lifetime_is_secure_element( lifetime ) )
260 {
261 /* Mbed Crypto currently always exposes the slot number to
262 * applications. This is not mandated by the PSA specification
263 * and may change in future versions. */
264 TEST_EQUAL( status, 0 );
265 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
266 }
267 else
268 {
269 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
270 }
271#endif
272
273 /* Type and size */
274 TEST_ASSERT( type != 0 );
275 TEST_ASSERT( bits != 0 );
276 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
277 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
278 TEST_ASSERT( bits % 8 == 0 );
279
280 /* MAX macros concerning specific key types */
281 if( PSA_KEY_TYPE_IS_ECC( type ) )
282 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
283 else if( PSA_KEY_TYPE_IS_RSA( type ) )
284 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
285 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) <= PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE );
286
287 ok = 1;
288
289exit:
290 psa_reset_key_attributes( &attributes );
291 return( ok );
292}
293
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100294int exercise_mac_setup( psa_key_type_t key_type,
295 const unsigned char *key_bytes,
296 size_t key_length,
297 psa_algorithm_t alg,
298 psa_mac_operation_t *operation,
299 psa_status_t *status )
300{
301 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200302 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100303
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100304 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200305 psa_set_key_algorithm( &attributes, alg );
306 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200307 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
308 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100309
310 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100311 /* Whether setup succeeded or failed, abort must succeed. */
312 PSA_ASSERT( psa_mac_abort( operation ) );
313 /* If setup failed, reproduce the failure, so that the caller can
314 * test the resulting state of the operation object. */
315 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100316 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100317 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
318 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100319 }
320
321 psa_destroy_key( handle );
322 return( 1 );
323
324exit:
325 psa_destroy_key( handle );
326 return( 0 );
327}
328
329int exercise_cipher_setup( psa_key_type_t key_type,
330 const unsigned char *key_bytes,
331 size_t key_length,
332 psa_algorithm_t alg,
333 psa_cipher_operation_t *operation,
334 psa_status_t *status )
335{
336 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100338
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200339 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
340 psa_set_key_algorithm( &attributes, alg );
341 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200342 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
343 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100344
345 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100346 /* Whether setup succeeded or failed, abort must succeed. */
347 PSA_ASSERT( psa_cipher_abort( operation ) );
348 /* If setup failed, reproduce the failure, so that the caller can
349 * test the resulting state of the operation object. */
350 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100351 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100352 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
353 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100354 }
355
356 psa_destroy_key( handle );
357 return( 1 );
358
359exit:
360 psa_destroy_key( handle );
361 return( 0 );
362}
363
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100364static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200365 psa_key_usage_t usage,
366 psa_algorithm_t alg )
367{
Jaeden Amero769ce272019-01-04 11:48:03 +0000368 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200369 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200370 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200371 size_t mac_length = sizeof( mac );
372
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100373 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100375 PSA_ASSERT( psa_mac_sign_setup( &operation,
376 handle, alg ) );
377 PSA_ASSERT( psa_mac_update( &operation,
378 input, sizeof( input ) ) );
379 PSA_ASSERT( psa_mac_sign_finish( &operation,
380 mac, sizeof( mac ),
381 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200382 }
383
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100384 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200385 {
386 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100387 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200388 PSA_SUCCESS :
389 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100390 PSA_ASSERT( psa_mac_verify_setup( &operation,
391 handle, alg ) );
392 PSA_ASSERT( psa_mac_update( &operation,
393 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100394 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
395 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200396 }
397
398 return( 1 );
399
400exit:
401 psa_mac_abort( &operation );
402 return( 0 );
403}
404
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100405static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200406 psa_key_usage_t usage,
407 psa_algorithm_t alg )
408{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000409 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200410 unsigned char iv[16] = {0};
411 size_t iv_length = sizeof( iv );
412 const unsigned char plaintext[16] = "Hello, world...";
413 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
414 size_t ciphertext_length = sizeof( ciphertext );
415 unsigned char decrypted[sizeof( ciphertext )];
416 size_t part_length;
417
418 if( usage & PSA_KEY_USAGE_ENCRYPT )
419 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100420 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
421 handle, alg ) );
422 PSA_ASSERT( psa_cipher_generate_iv( &operation,
423 iv, sizeof( iv ),
424 &iv_length ) );
425 PSA_ASSERT( psa_cipher_update( &operation,
426 plaintext, sizeof( plaintext ),
427 ciphertext, sizeof( ciphertext ),
428 &ciphertext_length ) );
429 PSA_ASSERT( psa_cipher_finish( &operation,
430 ciphertext + ciphertext_length,
431 sizeof( ciphertext ) - ciphertext_length,
432 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200433 ciphertext_length += part_length;
434 }
435
436 if( usage & PSA_KEY_USAGE_DECRYPT )
437 {
438 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200439 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200440 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
441 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
443 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
444 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
445 * have this macro yet. */
446 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
447 psa_get_key_type( &attributes ) );
448 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200449 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100450 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
451 handle, alg ) );
452 PSA_ASSERT( psa_cipher_set_iv( &operation,
453 iv, iv_length ) );
454 PSA_ASSERT( psa_cipher_update( &operation,
455 ciphertext, ciphertext_length,
456 decrypted, sizeof( decrypted ),
457 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200458 status = psa_cipher_finish( &operation,
459 decrypted + part_length,
460 sizeof( decrypted ) - part_length,
461 &part_length );
462 /* For a stream cipher, all inputs are valid. For a block cipher,
463 * if the input is some aribtrary data rather than an actual
464 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200465 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200466 TEST_ASSERT( status == PSA_SUCCESS ||
467 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200468 else
469 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200470 }
471
472 return( 1 );
473
474exit:
475 psa_cipher_abort( &operation );
476 return( 0 );
477}
478
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100479static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200480 psa_key_usage_t usage,
481 psa_algorithm_t alg )
482{
483 unsigned char nonce[16] = {0};
484 size_t nonce_length = sizeof( nonce );
485 unsigned char plaintext[16] = "Hello, world...";
486 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
487 size_t ciphertext_length = sizeof( ciphertext );
488 size_t plaintext_length = sizeof( ciphertext );
489
490 if( usage & PSA_KEY_USAGE_ENCRYPT )
491 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100492 PSA_ASSERT( psa_aead_encrypt( handle, alg,
493 nonce, nonce_length,
494 NULL, 0,
495 plaintext, sizeof( plaintext ),
496 ciphertext, sizeof( ciphertext ),
497 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200498 }
499
500 if( usage & PSA_KEY_USAGE_DECRYPT )
501 {
502 psa_status_t verify_status =
503 ( usage & PSA_KEY_USAGE_ENCRYPT ?
504 PSA_SUCCESS :
505 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100506 TEST_EQUAL( psa_aead_decrypt( handle, alg,
507 nonce, nonce_length,
508 NULL, 0,
509 ciphertext, ciphertext_length,
510 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100511 &plaintext_length ),
512 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200513 }
514
515 return( 1 );
516
517exit:
518 return( 0 );
519}
520
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100521static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200522 psa_key_usage_t usage,
523 psa_algorithm_t alg )
524{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200525 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
526 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100527 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200528 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100529 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
530
531 /* If the policy allows signing with any hash, just pick one. */
532 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
533 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100534#if defined(KNOWN_SUPPORTED_HASH_ALG)
535 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
536 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100537#else
538 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100539 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100540#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100541 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200542
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100543 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200544 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200545 /* Some algorithms require the payload to have the size of
546 * the hash encoded in the algorithm. Use this input size
547 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200548 if( hash_alg != 0 )
549 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100550 PSA_ASSERT( psa_sign_hash( handle, alg,
551 payload, payload_length,
552 signature, sizeof( signature ),
553 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200554 }
555
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100556 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200557 {
558 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100559 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200560 PSA_SUCCESS :
561 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100562 TEST_EQUAL( psa_verify_hash( handle, alg,
563 payload, payload_length,
564 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100565 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200566 }
567
568 return( 1 );
569
570exit:
571 return( 0 );
572}
573
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100574static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200575 psa_key_usage_t usage,
576 psa_algorithm_t alg )
577{
578 unsigned char plaintext[256] = "Hello, world...";
579 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
580 size_t ciphertext_length = sizeof( ciphertext );
581 size_t plaintext_length = 16;
582
583 if( usage & PSA_KEY_USAGE_ENCRYPT )
584 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100585 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
586 plaintext, plaintext_length,
587 NULL, 0,
588 ciphertext, sizeof( ciphertext ),
589 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200590 }
591
592 if( usage & PSA_KEY_USAGE_DECRYPT )
593 {
594 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100595 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200596 ciphertext, ciphertext_length,
597 NULL, 0,
598 plaintext, sizeof( plaintext ),
599 &plaintext_length );
600 TEST_ASSERT( status == PSA_SUCCESS ||
601 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
602 ( status == PSA_ERROR_INVALID_ARGUMENT ||
603 status == PSA_ERROR_INVALID_PADDING ) ) );
604 }
605
606 return( 1 );
607
608exit:
609 return( 0 );
610}
Gilles Peskine02b75072018-07-01 22:31:34 +0200611
Janos Follathf2815ea2019-07-03 12:41:36 +0100612static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
613 psa_key_handle_t handle,
614 psa_algorithm_t alg,
615 unsigned char* input1, size_t input1_length,
616 unsigned char* input2, size_t input2_length,
617 size_t capacity )
618{
619 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
620 if( PSA_ALG_IS_HKDF( alg ) )
621 {
622 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
623 PSA_KEY_DERIVATION_INPUT_SALT,
624 input1, input1_length ) );
625 PSA_ASSERT( psa_key_derivation_input_key( operation,
626 PSA_KEY_DERIVATION_INPUT_SECRET,
627 handle ) );
628 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
629 PSA_KEY_DERIVATION_INPUT_INFO,
630 input2,
631 input2_length ) );
632 }
633 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
634 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
635 {
636 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
637 PSA_KEY_DERIVATION_INPUT_SEED,
638 input1, input1_length ) );
639 PSA_ASSERT( psa_key_derivation_input_key( operation,
640 PSA_KEY_DERIVATION_INPUT_SECRET,
641 handle ) );
642 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
643 PSA_KEY_DERIVATION_INPUT_LABEL,
644 input2, input2_length ) );
645 }
646 else
647 {
648 TEST_ASSERT( ! "Key derivation algorithm not supported" );
649 }
650
Gilles Peskinec744d992019-07-30 17:26:54 +0200651 if( capacity != SIZE_MAX )
652 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100653
654 return( 1 );
655
656exit:
657 return( 0 );
658}
659
660
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100661static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200662 psa_key_usage_t usage,
663 psa_algorithm_t alg )
664{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200665 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100666 unsigned char input1[] = "Input 1";
667 size_t input1_length = sizeof( input1 );
668 unsigned char input2[] = "Input 2";
669 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200670 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100671 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200672
673 if( usage & PSA_KEY_USAGE_DERIVE )
674 {
Janos Follathf2815ea2019-07-03 12:41:36 +0100675 if( !setup_key_derivation_wrap( &operation, handle, alg,
676 input1, input1_length,
677 input2, input2_length, capacity ) )
678 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200679
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200680 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200681 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100682 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200683 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200684 }
685
686 return( 1 );
687
688exit:
689 return( 0 );
690}
691
Gilles Peskinec7998b72018-11-07 18:45:02 +0100692/* We need two keys to exercise key agreement. Exercise the
693 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200694static psa_status_t key_agreement_with_self(
695 psa_key_derivation_operation_t *operation,
696 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100697{
698 psa_key_type_t private_key_type;
699 psa_key_type_t public_key_type;
700 size_t key_bits;
701 uint8_t *public_key = NULL;
702 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200703 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200704 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
705 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200706 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100708
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200709 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
710 private_key_type = psa_get_key_type( &attributes );
711 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200712 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100713 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
714 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100715 PSA_ASSERT( psa_export_public_key( handle,
716 public_key, public_key_length,
717 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100718
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200719 status = psa_key_derivation_key_agreement(
720 operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle,
721 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100722exit:
723 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200724 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100725 return( status );
726}
727
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200728/* We need two keys to exercise key agreement. Exercise the
729 * private key against its own public key. */
730static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
731 psa_key_handle_t handle )
732{
733 psa_key_type_t private_key_type;
734 psa_key_type_t public_key_type;
735 size_t key_bits;
736 uint8_t *public_key = NULL;
737 size_t public_key_length;
738 uint8_t output[1024];
739 size_t output_length;
740 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200741 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
742 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200743 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200745
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200746 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
747 private_key_type = psa_get_key_type( &attributes );
748 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200749 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200750 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
751 ASSERT_ALLOC( public_key, public_key_length );
752 PSA_ASSERT( psa_export_public_key( handle,
753 public_key, public_key_length,
754 &public_key_length ) );
755
Gilles Peskinebe697d82019-05-16 18:00:41 +0200756 status = psa_raw_key_agreement( alg, handle,
757 public_key, public_key_length,
758 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200759exit:
760 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200761 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200762 return( status );
763}
764
765static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
766 psa_key_usage_t usage,
767 psa_algorithm_t alg )
768{
769 int ok = 0;
770
771 if( usage & PSA_KEY_USAGE_DERIVE )
772 {
773 /* We need two keys to exercise key agreement. Exercise the
774 * private key against its own public key. */
775 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
776 }
777 ok = 1;
778
779exit:
780 return( ok );
781}
782
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100783static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200784 psa_key_usage_t usage,
785 psa_algorithm_t alg )
786{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200787 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200788 unsigned char output[1];
789 int ok = 0;
790
791 if( usage & PSA_KEY_USAGE_DERIVE )
792 {
793 /* We need two keys to exercise key agreement. Exercise the
794 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200795 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
796 PSA_ASSERT( key_agreement_with_self( &operation, handle ) );
797 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200798 output,
799 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200800 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200801 }
802 ok = 1;
803
804exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200805 return( ok );
806}
807
Jaeden Amerof7dca862019-06-27 17:31:33 +0100808int asn1_skip_integer( unsigned char **p, const unsigned char *end,
809 size_t min_bits, size_t max_bits,
810 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200811{
812 size_t len;
813 size_t actual_bits;
814 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100815 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100816 MBEDTLS_ASN1_INTEGER ),
817 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200818
819 /* Check if the retrieved length doesn't extend the actual buffer's size.
820 * It is assumed here, that end >= p, which validates casting to size_t. */
821 TEST_ASSERT( len <= (size_t)( end - *p) );
822
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200823 /* Tolerate a slight departure from DER encoding:
824 * - 0 may be represented by an empty string or a 1-byte string.
825 * - The sign bit may be used as a value bit. */
826 if( ( len == 1 && ( *p )[0] == 0 ) ||
827 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
828 {
829 ++( *p );
830 --len;
831 }
832 if( min_bits == 0 && len == 0 )
833 return( 1 );
834 msb = ( *p )[0];
835 TEST_ASSERT( msb != 0 );
836 actual_bits = 8 * ( len - 1 );
837 while( msb != 0 )
838 {
839 msb >>= 1;
840 ++actual_bits;
841 }
842 TEST_ASSERT( actual_bits >= min_bits );
843 TEST_ASSERT( actual_bits <= max_bits );
844 if( must_be_odd )
845 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
846 *p += len;
847 return( 1 );
848exit:
849 return( 0 );
850}
851
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200852static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
853 uint8_t *exported, size_t exported_length )
854{
855 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100856 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200857 else
858 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200859
860#if defined(MBEDTLS_DES_C)
861 if( type == PSA_KEY_TYPE_DES )
862 {
863 /* Check the parity bits. */
864 unsigned i;
865 for( i = 0; i < bits / 8; i++ )
866 {
867 unsigned bit_count = 0;
868 unsigned m;
869 for( m = 1; m <= 0x100; m <<= 1 )
870 {
871 if( exported[i] & m )
872 ++bit_count;
873 }
874 TEST_ASSERT( bit_count % 2 != 0 );
875 }
876 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200877 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200878#endif
879
880#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200881 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200882 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200883 uint8_t *p = exported;
884 uint8_t *end = exported + exported_length;
885 size_t len;
886 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200887 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200888 * modulus INTEGER, -- n
889 * publicExponent INTEGER, -- e
890 * privateExponent INTEGER, -- d
891 * prime1 INTEGER, -- p
892 * prime2 INTEGER, -- q
893 * exponent1 INTEGER, -- d mod (p-1)
894 * exponent2 INTEGER, -- d mod (q-1)
895 * coefficient INTEGER, -- (inverse of q) mod p
896 * }
897 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100898 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
899 MBEDTLS_ASN1_SEQUENCE |
900 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
901 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200902 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
903 goto exit;
904 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
905 goto exit;
906 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
907 goto exit;
908 /* Require d to be at least half the size of n. */
909 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
910 goto exit;
911 /* Require p and q to be at most half the size of n, rounded up. */
912 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
913 goto exit;
914 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
915 goto exit;
916 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
917 goto exit;
918 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
919 goto exit;
920 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
921 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100922 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100923 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200924 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200925#endif /* MBEDTLS_RSA_C */
926
927#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200928 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200929 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100930 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100931 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100932 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200933 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200934#endif /* MBEDTLS_ECP_C */
935
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200936 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
937 {
938 uint8_t *p = exported;
939 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200940#if defined(MBEDTLS_RSA_C)
941 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
942 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100943 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200944 /* RSAPublicKey ::= SEQUENCE {
945 * modulus INTEGER, -- n
946 * publicExponent INTEGER } -- e
947 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100948 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
949 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100950 MBEDTLS_ASN1_CONSTRUCTED ),
951 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100952 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200953 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
954 goto exit;
955 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
956 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100957 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200958 }
959 else
960#endif /* MBEDTLS_RSA_C */
961#if defined(MBEDTLS_ECP_C)
962 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
963 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000964 /* The representation of an ECC public key is:
965 * - The byte 0x04;
966 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
967 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
968 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000969 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100970 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
971 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200972 }
973 else
974#endif /* MBEDTLS_ECP_C */
975 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100976 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200977 mbedtls_snprintf( message, sizeof( message ),
978 "No sanity check for public key type=0x%08lx",
979 (unsigned long) type );
980 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200981 (void) p;
982 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200983 return( 0 );
984 }
985 }
986 else
987
988 {
989 /* No sanity checks for other types */
990 }
991
992 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200993
994exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200995 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200996}
997
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100998static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200999 psa_key_usage_t usage )
1000{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001002 uint8_t *exported = NULL;
1003 size_t exported_size = 0;
1004 size_t exported_length = 0;
1005 int ok = 0;
1006
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001007 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001008
1009 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001010 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001011 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001012 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
1013 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001014 ok = 1;
1015 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001016 }
1017
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001018 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
1019 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001020 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001021
Gilles Peskine8817f612018-12-18 00:18:46 +01001022 PSA_ASSERT( psa_export_key( handle,
1023 exported, exported_size,
1024 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001025 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1026 psa_get_key_bits( &attributes ),
1027 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001028
1029exit:
1030 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001031 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001032 return( ok );
1033}
1034
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001035static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +02001036{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001038 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001039 uint8_t *exported = NULL;
1040 size_t exported_size = 0;
1041 size_t exported_length = 0;
1042 int ok = 0;
1043
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001044 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1045 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001046 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001047 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001048 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +02001049 return( 1 );
1050 }
1051
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001052 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001053 psa_get_key_type( &attributes ) );
1054 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
1055 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001056 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001057
Gilles Peskine8817f612018-12-18 00:18:46 +01001058 PSA_ASSERT( psa_export_public_key( handle,
1059 exported, exported_size,
1060 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001061 ok = exported_key_sanity_check( public_type,
1062 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001063 exported, exported_length );
1064
1065exit:
1066 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001067 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001068 return( ok );
1069}
1070
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001071/** Do smoke tests on a key.
1072 *
1073 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1074 * sign/verify, or derivation) that is permitted according to \p usage.
1075 * \p usage and \p alg should correspond to the expected policy on the
1076 * key.
1077 *
1078 * Export the key if permitted by \p usage, and check that the output
1079 * looks sensible. If \p usage forbids export, check that
1080 * \p psa_export_key correctly rejects the attempt. If the key is
1081 * asymmetric, also check \p psa_export_public_key.
1082 *
1083 * If the key fails the tests, this function calls the test framework's
1084 * `test_fail` function and returns false. Otherwise this function returns
1085 * true. Therefore it should be used as follows:
1086 * ```
1087 * if( ! exercise_key( ... ) ) goto exit;
1088 * ```
1089 *
1090 * \param handle The key to exercise. It should be capable of performing
1091 * \p alg.
1092 * \param usage The usage flags to assume.
1093 * \param alg The algorithm to exercise.
1094 *
1095 * \retval 0 The key failed the smoke tests.
1096 * \retval 1 The key passed the smoke tests.
1097 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001098static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001099 psa_key_usage_t usage,
1100 psa_algorithm_t alg )
1101{
1102 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001103
1104 if( ! check_key_attributes_sanity( handle ) )
1105 return( 0 );
1106
Gilles Peskine02b75072018-07-01 22:31:34 +02001107 if( alg == 0 )
1108 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1109 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001110 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001111 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001112 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001113 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001114 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001115 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001116 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001117 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001118 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001119 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001120 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001121 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1122 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001123 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001124 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001125 else
1126 {
1127 char message[40];
1128 mbedtls_snprintf( message, sizeof( message ),
1129 "No code to exercise alg=0x%08lx",
1130 (unsigned long) alg );
1131 test_fail( message, __LINE__, __FILE__ );
1132 ok = 0;
1133 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001134
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001135 ok = ok && exercise_export_key( handle, usage );
1136 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001137
Gilles Peskine02b75072018-07-01 22:31:34 +02001138 return( ok );
1139}
1140
Gilles Peskine10df3412018-10-25 22:35:43 +02001141static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1142 psa_algorithm_t alg )
1143{
1144 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1145 {
1146 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001147 PSA_KEY_USAGE_VERIFY_HASH :
1148 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001149 }
1150 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1151 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1152 {
1153 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1154 PSA_KEY_USAGE_ENCRYPT :
1155 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1156 }
1157 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1158 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1159 {
1160 return( PSA_KEY_USAGE_DERIVE );
1161 }
1162 else
1163 {
1164 return( 0 );
1165 }
1166
1167}
Darryl Green0c6575a2018-11-07 16:05:30 +00001168
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001169static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1170{
1171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1172 uint8_t buffer[1];
1173 size_t length;
1174 int ok = 0;
1175
Gilles Peskinec87af662019-05-15 16:12:22 +02001176 psa_set_key_id( &attributes, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001177 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1178 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1179 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1180 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1181 PSA_ERROR_INVALID_HANDLE );
1182 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001183 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001184 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1185 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1186 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1187 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1188
1189 TEST_EQUAL( psa_export_key( handle,
1190 buffer, sizeof( buffer ), &length ),
1191 PSA_ERROR_INVALID_HANDLE );
1192 TEST_EQUAL( psa_export_public_key( handle,
1193 buffer, sizeof( buffer ), &length ),
1194 PSA_ERROR_INVALID_HANDLE );
1195
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001196 ok = 1;
1197
1198exit:
1199 psa_reset_key_attributes( &attributes );
1200 return( ok );
1201}
1202
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001203/* Assert that a key isn't reported as having a slot number. */
1204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1205#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1206 do \
1207 { \
1208 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1209 TEST_EQUAL( psa_get_key_slot_number( \
1210 attributes, \
1211 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1212 PSA_ERROR_INVALID_ARGUMENT ); \
1213 } \
1214 while( 0 )
1215#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1216#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1217 ( (void) 0 )
1218#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1219
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001220/* An overapproximation of the amount of storage needed for a key of the
1221 * given type and with the given content. The API doesn't make it easy
1222 * to find a good value for the size. The current implementation doesn't
1223 * care about the value anyway. */
1224#define KEY_BITS_FROM_DATA( type, data ) \
1225 ( data )->len
1226
Darryl Green0c6575a2018-11-07 16:05:30 +00001227typedef enum {
1228 IMPORT_KEY = 0,
1229 GENERATE_KEY = 1,
1230 DERIVE_KEY = 2
1231} generate_method;
1232
Gilles Peskinee59236f2018-01-27 23:32:46 +01001233/* END_HEADER */
1234
1235/* BEGIN_DEPENDENCIES
1236 * depends_on:MBEDTLS_PSA_CRYPTO_C
1237 * END_DEPENDENCIES
1238 */
1239
1240/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001241void static_checks( )
1242{
1243 size_t max_truncated_mac_size =
1244 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1245
1246 /* Check that the length for a truncated MAC always fits in the algorithm
1247 * encoding. The shifted mask is the maximum truncated value. The
1248 * untruncated algorithm may be one byte larger. */
1249 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001250
1251#if defined(MBEDTLS_TEST_DEPRECATED)
1252 /* Check deprecated constants. */
1253 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1254 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1255 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1256 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1257 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1258 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1259 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1260 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
1261#endif /* MBEDTLS_TEST_DEPRECATED */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001262}
1263/* END_CASE */
1264
1265/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001266void attributes_set_get( int id_arg, int lifetime_arg,
1267 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001268 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269{
Gilles Peskine4747d192019-04-17 15:05:45 +02001270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001271 psa_key_id_t id = id_arg;
1272 psa_key_lifetime_t lifetime = lifetime_arg;
1273 psa_key_usage_t usage_flags = usage_flags_arg;
1274 psa_algorithm_t alg = alg_arg;
1275 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001276 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001277
1278 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1279 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1280 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1281 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1282 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001283 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001284
Gilles Peskinec87af662019-05-15 16:12:22 +02001285 psa_set_key_id( &attributes, id );
1286 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001287 psa_set_key_usage_flags( &attributes, usage_flags );
1288 psa_set_key_algorithm( &attributes, alg );
1289 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001290 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001291
1292 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1293 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1294 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1295 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1296 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001297 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001298
1299 psa_reset_key_attributes( &attributes );
1300
1301 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1302 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1303 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1304 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1305 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001306 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001307}
1308/* END_CASE */
1309
1310/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001311void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1312 int expected_id_arg, int expected_lifetime_arg )
1313{
1314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1315 psa_key_id_t id1 = id1_arg;
1316 psa_key_lifetime_t lifetime = lifetime_arg;
1317 psa_key_id_t id2 = id2_arg;
1318 psa_key_id_t expected_id = expected_id_arg;
1319 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1320
1321 if( id1_arg != -1 )
1322 psa_set_key_id( &attributes, id1 );
1323 if( lifetime_arg != -1 )
1324 psa_set_key_lifetime( &attributes, lifetime );
1325 if( id2_arg != -1 )
1326 psa_set_key_id( &attributes, id2 );
1327
1328 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1329 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1330}
1331/* END_CASE */
1332
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001333/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1334void slot_number_attribute( )
1335{
1336 psa_key_slot_number_t slot_number = 0xdeadbeef;
1337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1338
1339 /* Initially, there is no slot number. */
1340 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1341 PSA_ERROR_INVALID_ARGUMENT );
1342
1343 /* Test setting a slot number. */
1344 psa_set_key_slot_number( &attributes, 0 );
1345 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1346 TEST_EQUAL( slot_number, 0 );
1347
1348 /* Test changing the slot number. */
1349 psa_set_key_slot_number( &attributes, 42 );
1350 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1351 TEST_EQUAL( slot_number, 42 );
1352
1353 /* Test clearing the slot number. */
1354 psa_clear_key_slot_number( &attributes );
1355 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1356 PSA_ERROR_INVALID_ARGUMENT );
1357
1358 /* Clearing again should have no effect. */
1359 psa_clear_key_slot_number( &attributes );
1360 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1361 PSA_ERROR_INVALID_ARGUMENT );
1362
1363 /* Test that reset clears the slot number. */
1364 psa_set_key_slot_number( &attributes, 42 );
1365 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1366 TEST_EQUAL( slot_number, 42 );
1367 psa_reset_key_attributes( &attributes );
1368 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1369 PSA_ERROR_INVALID_ARGUMENT );
1370}
1371/* END_CASE */
1372
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001373/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001374void import_with_policy( int type_arg,
1375 int usage_arg, int alg_arg,
1376 int expected_status_arg )
1377{
1378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1379 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1380 psa_key_handle_t handle = 0;
1381 psa_key_type_t type = type_arg;
1382 psa_key_usage_t usage = usage_arg;
1383 psa_algorithm_t alg = alg_arg;
1384 psa_status_t expected_status = expected_status_arg;
1385 const uint8_t key_material[16] = {0};
1386 psa_status_t status;
1387
1388 PSA_ASSERT( psa_crypto_init( ) );
1389
1390 psa_set_key_type( &attributes, type );
1391 psa_set_key_usage_flags( &attributes, usage );
1392 psa_set_key_algorithm( &attributes, alg );
1393
1394 status = psa_import_key( &attributes,
1395 key_material, sizeof( key_material ),
1396 &handle );
1397 TEST_EQUAL( status, expected_status );
1398 if( status != PSA_SUCCESS )
1399 goto exit;
1400
1401 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1402 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1403 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1404 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001405 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001406
1407 PSA_ASSERT( psa_destroy_key( handle ) );
1408 test_operations_on_invalid_handle( handle );
1409
1410exit:
1411 psa_destroy_key( handle );
1412 psa_reset_key_attributes( &got_attributes );
1413 PSA_DONE( );
1414}
1415/* END_CASE */
1416
1417/* BEGIN_CASE */
1418void import_with_data( data_t *data, int type_arg,
1419 int attr_bits_arg,
1420 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001421{
1422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1423 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001424 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001425 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001426 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001427 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001428 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001429
Gilles Peskine8817f612018-12-18 00:18:46 +01001430 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001431
Gilles Peskine4747d192019-04-17 15:05:45 +02001432 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001433 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001434
Gilles Peskine73676cb2019-05-15 20:15:10 +02001435 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001436 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001437 if( status != PSA_SUCCESS )
1438 goto exit;
1439
1440 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1441 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001442 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001443 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001444 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001445
1446 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001447 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001448
1449exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001450 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001451 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001452 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001453}
1454/* END_CASE */
1455
1456/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001457void import_large_key( int type_arg, int byte_size_arg,
1458 int expected_status_arg )
1459{
1460 psa_key_type_t type = type_arg;
1461 size_t byte_size = byte_size_arg;
1462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1463 psa_status_t expected_status = expected_status_arg;
1464 psa_key_handle_t handle = 0;
1465 psa_status_t status;
1466 uint8_t *buffer = NULL;
1467 size_t buffer_size = byte_size + 1;
1468 size_t n;
1469
1470 /* It would be better to skip the test than fail it if the allocation
1471 * fails, but the test framework doesn't support this yet. */
1472 ASSERT_ALLOC( buffer, buffer_size );
1473 memset( buffer, 'K', byte_size );
1474
1475 PSA_ASSERT( psa_crypto_init( ) );
1476
1477 /* Try importing the key */
1478 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1479 psa_set_key_type( &attributes, type );
1480 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1481 TEST_EQUAL( status, expected_status );
1482
1483 if( status == PSA_SUCCESS )
1484 {
1485 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1486 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1487 TEST_EQUAL( psa_get_key_bits( &attributes ),
1488 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001489 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001490 memset( buffer, 0, byte_size + 1 );
1491 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1492 for( n = 0; n < byte_size; n++ )
1493 TEST_EQUAL( buffer[n], 'K' );
1494 for( n = byte_size; n < buffer_size; n++ )
1495 TEST_EQUAL( buffer[n], 0 );
1496 }
1497
1498exit:
1499 psa_destroy_key( handle );
1500 PSA_DONE( );
1501 mbedtls_free( buffer );
1502}
1503/* END_CASE */
1504
1505/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001506void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1507{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001508 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001509 size_t bits = bits_arg;
1510 psa_status_t expected_status = expected_status_arg;
1511 psa_status_t status;
1512 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001513 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001514 size_t buffer_size = /* Slight overapproximations */
1515 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001516 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001517 unsigned char *p;
1518 int ret;
1519 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001521
Gilles Peskine8817f612018-12-18 00:18:46 +01001522 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001523 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001524
1525 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1526 bits, keypair ) ) >= 0 );
1527 length = ret;
1528
1529 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001530 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001531 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001532 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001533
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001534 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001535 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001536
1537exit:
1538 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001539 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001540}
1541/* END_CASE */
1542
1543/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001544void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001545 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001546 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001547 int expected_bits,
1548 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001549 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001550 int canonical_input )
1551{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001552 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001554 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001555 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001556 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001557 unsigned char *exported = NULL;
1558 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001559 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001560 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001561 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001562 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001563 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564
Moran Pekercb088e72018-07-17 17:36:59 +03001565 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001566 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001568 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001569 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001570
Gilles Peskine4747d192019-04-17 15:05:45 +02001571 psa_set_key_usage_flags( &attributes, usage_arg );
1572 psa_set_key_algorithm( &attributes, alg );
1573 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001574
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001575 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001576 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001577
1578 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001579 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1580 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1581 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001582 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001583
1584 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001585 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001586 exported, export_size,
1587 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001588 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001589
1590 /* The exported length must be set by psa_export_key() to a value between 0
1591 * and export_size. On errors, the exported length must be 0. */
1592 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1593 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1594 TEST_ASSERT( exported_length <= export_size );
1595
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001596 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001597 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001598 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001599 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001600 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001601 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001602 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001604 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001605 goto exit;
1606
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001607 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001608 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001609 else
1610 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001611 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001612 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1613 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001614 PSA_ASSERT( psa_export_key( handle2,
1615 reexported,
1616 export_size,
1617 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001618 ASSERT_COMPARE( exported, exported_length,
1619 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001620 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001621 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001622 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001623
1624destroy:
1625 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001626 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001627 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001628
1629exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001630 mbedtls_free( exported );
1631 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001632 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001633 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001634}
1635/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001636
Moran Pekerf709f4a2018-06-06 17:26:04 +03001637/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001638void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001639 int type_arg,
1640 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001641 int export_size_delta,
1642 int expected_export_status_arg,
1643 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001644{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001645 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001646 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001647 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001648 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001649 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001650 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001651 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001652 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001653 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001654
Gilles Peskine8817f612018-12-18 00:18:46 +01001655 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001656
Gilles Peskine4747d192019-04-17 15:05:45 +02001657 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1658 psa_set_key_algorithm( &attributes, alg );
1659 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001660
1661 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001662 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001663
Gilles Peskine49c25912018-10-29 15:15:31 +01001664 /* Export the public key */
1665 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001666 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001667 exported, export_size,
1668 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001669 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001670 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001671 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001672 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001673 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001674 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1675 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001676 TEST_ASSERT( expected_public_key->len <=
1677 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001678 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1679 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001680 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001681
1682exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001683 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001684 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001685 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001686 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001687}
1688/* END_CASE */
1689
Gilles Peskine20035e32018-02-03 22:44:14 +01001690/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001691void import_and_exercise_key( data_t *data,
1692 int type_arg,
1693 int bits_arg,
1694 int alg_arg )
1695{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001696 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001697 psa_key_type_t type = type_arg;
1698 size_t bits = bits_arg;
1699 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001700 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001702 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001703
Gilles Peskine8817f612018-12-18 00:18:46 +01001704 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001705
Gilles Peskine4747d192019-04-17 15:05:45 +02001706 psa_set_key_usage_flags( &attributes, usage );
1707 psa_set_key_algorithm( &attributes, alg );
1708 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001709
1710 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001711 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001712
1713 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001714 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1715 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1716 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001717
1718 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001719 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001720 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001721
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001722 PSA_ASSERT( psa_destroy_key( handle ) );
1723 test_operations_on_invalid_handle( handle );
1724
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001725exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001726 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001727 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001728 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001729}
1730/* END_CASE */
1731
1732/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001733void effective_key_attributes( int type_arg, int expected_type_arg,
1734 int bits_arg, int expected_bits_arg,
1735 int usage_arg, int expected_usage_arg,
1736 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001737{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001738 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001739 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001740 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001741 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001742 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001743 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001744 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001745 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001746 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001748
Gilles Peskine8817f612018-12-18 00:18:46 +01001749 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001750
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001751 psa_set_key_usage_flags( &attributes, usage );
1752 psa_set_key_algorithm( &attributes, alg );
1753 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001754 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001755
Gilles Peskine1a960492019-11-26 17:12:21 +01001756 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1757 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001758
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001759 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001760 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1761 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1762 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1763 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001764
1765exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001766 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001767 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001768 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001769}
1770/* END_CASE */
1771
1772/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001773void check_key_policy( int type_arg, int bits_arg,
1774 int usage_arg, int alg_arg )
1775{
1776 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1777 usage_arg, usage_arg, alg_arg, alg_arg );
1778 goto exit;
1779}
1780/* END_CASE */
1781
1782/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001783void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001784{
1785 /* Test each valid way of initializing the object, except for `= {0}`, as
1786 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1787 * though it's OK by the C standard. We could test for this, but we'd need
1788 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001789 psa_key_attributes_t func = psa_key_attributes_init( );
1790 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1791 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001792
1793 memset( &zero, 0, sizeof( zero ) );
1794
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001795 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1796 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1797 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001798
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001799 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1800 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1801 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1802
1803 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1804 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1805 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1806
1807 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1808 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1809 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1810
1811 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1812 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1813 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001818void mac_key_policy( int policy_usage,
1819 int policy_alg,
1820 int key_type,
1821 data_t *key_data,
1822 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001823{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001824 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001826 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001827 psa_status_t status;
1828 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001829
Gilles Peskine8817f612018-12-18 00:18:46 +01001830 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001831
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001832 psa_set_key_usage_flags( &attributes, policy_usage );
1833 psa_set_key_algorithm( &attributes, policy_alg );
1834 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001835
Gilles Peskine049c7532019-05-15 20:22:09 +02001836 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1837 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001838
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001839 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001840 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001841 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001842 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001843 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001844 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001845 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001846
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001847 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001848 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001849 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001850 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001851 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001852 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001853 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001854
1855exit:
1856 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001857 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001858 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001859}
1860/* END_CASE */
1861
1862/* BEGIN_CASE */
1863void cipher_key_policy( int policy_usage,
1864 int policy_alg,
1865 int key_type,
1866 data_t *key_data,
1867 int exercise_alg )
1868{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001869 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001870 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001871 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001872 psa_status_t status;
1873
Gilles Peskine8817f612018-12-18 00:18:46 +01001874 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001875
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001876 psa_set_key_usage_flags( &attributes, policy_usage );
1877 psa_set_key_algorithm( &attributes, policy_alg );
1878 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001879
Gilles Peskine049c7532019-05-15 20:22:09 +02001880 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1881 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001882
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001883 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001884 if( policy_alg == exercise_alg &&
1885 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001886 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001887 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001888 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001889 psa_cipher_abort( &operation );
1890
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001891 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001892 if( policy_alg == exercise_alg &&
1893 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001894 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001895 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001896 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001897
1898exit:
1899 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001900 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001901 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001902}
1903/* END_CASE */
1904
1905/* BEGIN_CASE */
1906void aead_key_policy( int policy_usage,
1907 int policy_alg,
1908 int key_type,
1909 data_t *key_data,
1910 int nonce_length_arg,
1911 int tag_length_arg,
1912 int exercise_alg )
1913{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001914 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001916 psa_status_t status;
1917 unsigned char nonce[16] = {0};
1918 size_t nonce_length = nonce_length_arg;
1919 unsigned char tag[16];
1920 size_t tag_length = tag_length_arg;
1921 size_t output_length;
1922
1923 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1924 TEST_ASSERT( tag_length <= sizeof( tag ) );
1925
Gilles Peskine8817f612018-12-18 00:18:46 +01001926 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001927
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001928 psa_set_key_usage_flags( &attributes, policy_usage );
1929 psa_set_key_algorithm( &attributes, policy_alg );
1930 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931
Gilles Peskine049c7532019-05-15 20:22:09 +02001932 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1933 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001935 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001936 nonce, nonce_length,
1937 NULL, 0,
1938 NULL, 0,
1939 tag, tag_length,
1940 &output_length );
1941 if( policy_alg == exercise_alg &&
1942 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001943 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001944 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001945 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946
1947 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001948 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001949 nonce, nonce_length,
1950 NULL, 0,
1951 tag, tag_length,
1952 NULL, 0,
1953 &output_length );
1954 if( policy_alg == exercise_alg &&
1955 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001956 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001957 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001958 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001959
1960exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001961 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001962 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001963}
1964/* END_CASE */
1965
1966/* BEGIN_CASE */
1967void asymmetric_encryption_key_policy( int policy_usage,
1968 int policy_alg,
1969 int key_type,
1970 data_t *key_data,
1971 int exercise_alg )
1972{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001973 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001975 psa_status_t status;
1976 size_t key_bits;
1977 size_t buffer_length;
1978 unsigned char *buffer = NULL;
1979 size_t output_length;
1980
Gilles Peskine8817f612018-12-18 00:18:46 +01001981 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001982
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001983 psa_set_key_usage_flags( &attributes, policy_usage );
1984 psa_set_key_algorithm( &attributes, policy_alg );
1985 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986
Gilles Peskine049c7532019-05-15 20:22:09 +02001987 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1988 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001989
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001990 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1991 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001992 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1993 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001994 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001996 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001997 NULL, 0,
1998 NULL, 0,
1999 buffer, buffer_length,
2000 &output_length );
2001 if( policy_alg == exercise_alg &&
2002 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002003 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002005 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002007 if( buffer_length != 0 )
2008 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002009 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002010 buffer, buffer_length,
2011 NULL, 0,
2012 buffer, buffer_length,
2013 &output_length );
2014 if( policy_alg == exercise_alg &&
2015 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002016 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002018 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019
2020exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002021 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002022 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002023 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 mbedtls_free( buffer );
2025}
2026/* END_CASE */
2027
2028/* BEGIN_CASE */
2029void asymmetric_signature_key_policy( int policy_usage,
2030 int policy_alg,
2031 int key_type,
2032 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002033 int exercise_alg,
2034 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002036 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002039 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2040 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2041 * compatible with the policy and `payload_length_arg` is supposed to be
2042 * a valid input length to sign. If `payload_length_arg <= 0`,
2043 * `exercise_alg` is supposed to be forbidden by the policy. */
2044 int compatible_alg = payload_length_arg > 0;
2045 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002046 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002047 size_t signature_length;
2048
Gilles Peskine8817f612018-12-18 00:18:46 +01002049 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002051 psa_set_key_usage_flags( &attributes, policy_usage );
2052 psa_set_key_algorithm( &attributes, policy_alg );
2053 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002054
Gilles Peskine049c7532019-05-15 20:22:09 +02002055 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2056 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002057
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002058 status = psa_sign_hash( handle, exercise_alg,
2059 payload, payload_length,
2060 signature, sizeof( signature ),
2061 &signature_length );
2062 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002063 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002064 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002065 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066
2067 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002068 status = psa_verify_hash( handle, exercise_alg,
2069 payload, payload_length,
2070 signature, sizeof( signature ) );
2071 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002072 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002074 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002075
2076exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002077 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002078 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002079}
2080/* END_CASE */
2081
Janos Follathba3fab92019-06-11 14:50:16 +01002082/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002083void derive_key_policy( int policy_usage,
2084 int policy_alg,
2085 int key_type,
2086 data_t *key_data,
2087 int exercise_alg )
2088{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002089 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002091 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002092 psa_status_t status;
2093
Gilles Peskine8817f612018-12-18 00:18:46 +01002094 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002096 psa_set_key_usage_flags( &attributes, policy_usage );
2097 psa_set_key_algorithm( &attributes, policy_alg );
2098 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002099
Gilles Peskine049c7532019-05-15 20:22:09 +02002100 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2101 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002102
Janos Follathba3fab92019-06-11 14:50:16 +01002103 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2104
2105 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2106 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002107 {
Janos Follathba3fab92019-06-11 14:50:16 +01002108 PSA_ASSERT( psa_key_derivation_input_bytes(
2109 &operation,
2110 PSA_KEY_DERIVATION_INPUT_SEED,
2111 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002112 }
Janos Follathba3fab92019-06-11 14:50:16 +01002113
2114 status = psa_key_derivation_input_key( &operation,
2115 PSA_KEY_DERIVATION_INPUT_SECRET,
2116 handle );
2117
Gilles Peskineea0fb492018-07-12 17:17:20 +02002118 if( policy_alg == exercise_alg &&
2119 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002120 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002121 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002122 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002123
2124exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002125 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002126 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002127 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002128}
2129/* END_CASE */
2130
2131/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002132void agreement_key_policy( int policy_usage,
2133 int policy_alg,
2134 int key_type_arg,
2135 data_t *key_data,
2136 int exercise_alg )
2137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002138 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002140 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002141 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002142 psa_status_t status;
2143
Gilles Peskine8817f612018-12-18 00:18:46 +01002144 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002145
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002146 psa_set_key_usage_flags( &attributes, policy_usage );
2147 psa_set_key_algorithm( &attributes, policy_alg );
2148 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002149
Gilles Peskine049c7532019-05-15 20:22:09 +02002150 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2151 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002152
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002153 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2154 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002155
Gilles Peskine01d718c2018-09-18 12:01:02 +02002156 if( policy_alg == exercise_alg &&
2157 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002158 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002159 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002160 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002161
2162exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002163 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002164 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002165 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002166}
2167/* END_CASE */
2168
2169/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002170void key_policy_alg2( int key_type_arg, data_t *key_data,
2171 int usage_arg, int alg_arg, int alg2_arg )
2172{
2173 psa_key_handle_t handle = 0;
2174 psa_key_type_t key_type = key_type_arg;
2175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2176 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2177 psa_key_usage_t usage = usage_arg;
2178 psa_algorithm_t alg = alg_arg;
2179 psa_algorithm_t alg2 = alg2_arg;
2180
2181 PSA_ASSERT( psa_crypto_init( ) );
2182
2183 psa_set_key_usage_flags( &attributes, usage );
2184 psa_set_key_algorithm( &attributes, alg );
2185 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2186 psa_set_key_type( &attributes, key_type );
2187 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2188 &handle ) );
2189
2190 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2191 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2192 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2193 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2194
2195 if( ! exercise_key( handle, usage, alg ) )
2196 goto exit;
2197 if( ! exercise_key( handle, usage, alg2 ) )
2198 goto exit;
2199
2200exit:
2201 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002202 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002203}
2204/* END_CASE */
2205
2206/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002207void raw_agreement_key_policy( int policy_usage,
2208 int policy_alg,
2209 int key_type_arg,
2210 data_t *key_data,
2211 int exercise_alg )
2212{
2213 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002215 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002216 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002217 psa_status_t status;
2218
2219 PSA_ASSERT( psa_crypto_init( ) );
2220
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002221 psa_set_key_usage_flags( &attributes, policy_usage );
2222 psa_set_key_algorithm( &attributes, policy_alg );
2223 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002224
Gilles Peskine049c7532019-05-15 20:22:09 +02002225 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2226 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002227
2228 status = raw_key_agreement_with_self( exercise_alg, handle );
2229
2230 if( policy_alg == exercise_alg &&
2231 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2232 PSA_ASSERT( status );
2233 else
2234 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2235
2236exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002237 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002238 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002239 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002240}
2241/* END_CASE */
2242
2243/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002244void copy_success( int source_usage_arg,
2245 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002246 int type_arg, data_t *material,
2247 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002248 int target_usage_arg,
2249 int target_alg_arg, int target_alg2_arg,
2250 int expected_usage_arg,
2251 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002252{
Gilles Peskineca25db92019-04-19 11:43:08 +02002253 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2254 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002255 psa_key_usage_t expected_usage = expected_usage_arg;
2256 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002257 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002258 psa_key_handle_t source_handle = 0;
2259 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002260 uint8_t *export_buffer = NULL;
2261
Gilles Peskine57ab7212019-01-28 13:03:09 +01002262 PSA_ASSERT( psa_crypto_init( ) );
2263
Gilles Peskineca25db92019-04-19 11:43:08 +02002264 /* Prepare the source key. */
2265 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2266 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002267 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002268 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002269 PSA_ASSERT( psa_import_key( &source_attributes,
2270 material->x, material->len,
2271 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002272 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002273
Gilles Peskineca25db92019-04-19 11:43:08 +02002274 /* Prepare the target attributes. */
2275 if( copy_attributes )
2276 target_attributes = source_attributes;
2277 if( target_usage_arg != -1 )
2278 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2279 if( target_alg_arg != -1 )
2280 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002281 if( target_alg2_arg != -1 )
2282 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002283
2284 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002285 PSA_ASSERT( psa_copy_key( source_handle,
2286 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002287
2288 /* Destroy the source to ensure that this doesn't affect the target. */
2289 PSA_ASSERT( psa_destroy_key( source_handle ) );
2290
2291 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002292 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2293 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2294 psa_get_key_type( &target_attributes ) );
2295 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2296 psa_get_key_bits( &target_attributes ) );
2297 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2298 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002299 TEST_EQUAL( expected_alg2,
2300 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002301 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2302 {
2303 size_t length;
2304 ASSERT_ALLOC( export_buffer, material->len );
2305 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2306 material->len, &length ) );
2307 ASSERT_COMPARE( material->x, material->len,
2308 export_buffer, length );
2309 }
2310 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2311 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002312 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2313 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002314
2315 PSA_ASSERT( psa_close_key( target_handle ) );
2316
2317exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002318 psa_reset_key_attributes( &source_attributes );
2319 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002320 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002321 mbedtls_free( export_buffer );
2322}
2323/* END_CASE */
2324
2325/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002326void copy_fail( int source_usage_arg,
2327 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002328 int type_arg, data_t *material,
2329 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002330 int target_usage_arg,
2331 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002332 int expected_status_arg )
2333{
2334 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2335 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2336 psa_key_handle_t source_handle = 0;
2337 psa_key_handle_t target_handle = 0;
2338
2339 PSA_ASSERT( psa_crypto_init( ) );
2340
2341 /* Prepare the source key. */
2342 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2343 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002344 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002345 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002346 PSA_ASSERT( psa_import_key( &source_attributes,
2347 material->x, material->len,
2348 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002349
2350 /* Prepare the target attributes. */
2351 psa_set_key_type( &target_attributes, target_type_arg );
2352 psa_set_key_bits( &target_attributes, target_bits_arg );
2353 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2354 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002355 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002356
2357 /* Try to copy the key. */
2358 TEST_EQUAL( psa_copy_key( source_handle,
2359 &target_attributes, &target_handle ),
2360 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002361
2362 PSA_ASSERT( psa_destroy_key( source_handle ) );
2363
Gilles Peskine4a644642019-05-03 17:14:08 +02002364exit:
2365 psa_reset_key_attributes( &source_attributes );
2366 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002367 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002368}
2369/* END_CASE */
2370
2371/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002372void hash_operation_init( )
2373{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002374 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002375 /* Test each valid way of initializing the object, except for `= {0}`, as
2376 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2377 * though it's OK by the C standard. We could test for this, but we'd need
2378 * to supress the Clang warning for the test. */
2379 psa_hash_operation_t func = psa_hash_operation_init( );
2380 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2381 psa_hash_operation_t zero;
2382
2383 memset( &zero, 0, sizeof( zero ) );
2384
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002385 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002386 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2387 PSA_ERROR_BAD_STATE );
2388 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2389 PSA_ERROR_BAD_STATE );
2390 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2391 PSA_ERROR_BAD_STATE );
2392
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002393 /* A default hash operation should be abortable without error. */
2394 PSA_ASSERT( psa_hash_abort( &func ) );
2395 PSA_ASSERT( psa_hash_abort( &init ) );
2396 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002397}
2398/* END_CASE */
2399
2400/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002401void hash_setup( int alg_arg,
2402 int expected_status_arg )
2403{
2404 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002405 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002406 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002407 psa_status_t status;
2408
Gilles Peskine8817f612018-12-18 00:18:46 +01002409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002410
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002411 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002412 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002413
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002414 /* Whether setup succeeded or failed, abort must succeed. */
2415 PSA_ASSERT( psa_hash_abort( &operation ) );
2416
2417 /* If setup failed, reproduce the failure, so as to
2418 * test the resulting state of the operation object. */
2419 if( status != PSA_SUCCESS )
2420 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2421
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002422 /* Now the operation object should be reusable. */
2423#if defined(KNOWN_SUPPORTED_HASH_ALG)
2424 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2425 PSA_ASSERT( psa_hash_abort( &operation ) );
2426#endif
2427
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002428exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002429 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002430}
2431/* END_CASE */
2432
2433/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002434void hash_compute_fail( int alg_arg, data_t *input,
2435 int output_size_arg, int expected_status_arg )
2436{
2437 psa_algorithm_t alg = alg_arg;
2438 uint8_t *output = NULL;
2439 size_t output_size = output_size_arg;
2440 size_t output_length = INVALID_EXPORT_LENGTH;
2441 psa_status_t expected_status = expected_status_arg;
2442 psa_status_t status;
2443
2444 ASSERT_ALLOC( output, output_size );
2445
2446 PSA_ASSERT( psa_crypto_init( ) );
2447
2448 status = psa_hash_compute( alg, input->x, input->len,
2449 output, output_size, &output_length );
2450 TEST_EQUAL( status, expected_status );
2451 TEST_ASSERT( output_length <= output_size );
2452
2453exit:
2454 mbedtls_free( output );
2455 PSA_DONE( );
2456}
2457/* END_CASE */
2458
2459/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002460void hash_compare_fail( int alg_arg, data_t *input,
2461 data_t *reference_hash,
2462 int expected_status_arg )
2463{
2464 psa_algorithm_t alg = alg_arg;
2465 psa_status_t expected_status = expected_status_arg;
2466 psa_status_t status;
2467
2468 PSA_ASSERT( psa_crypto_init( ) );
2469
2470 status = psa_hash_compare( alg, input->x, input->len,
2471 reference_hash->x, reference_hash->len );
2472 TEST_EQUAL( status, expected_status );
2473
2474exit:
2475 PSA_DONE( );
2476}
2477/* END_CASE */
2478
2479/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002480void hash_compute_compare( int alg_arg, data_t *input,
2481 data_t *expected_output )
2482{
2483 psa_algorithm_t alg = alg_arg;
2484 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2485 size_t output_length = INVALID_EXPORT_LENGTH;
2486 size_t i;
2487
2488 PSA_ASSERT( psa_crypto_init( ) );
2489
2490 /* Compute with tight buffer */
2491 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2492 output, PSA_HASH_SIZE( alg ),
2493 &output_length ) );
2494 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2495 ASSERT_COMPARE( output, output_length,
2496 expected_output->x, expected_output->len );
2497
2498 /* Compute with larger buffer */
2499 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2500 output, sizeof( output ),
2501 &output_length ) );
2502 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2503 ASSERT_COMPARE( output, output_length,
2504 expected_output->x, expected_output->len );
2505
2506 /* Compare with correct hash */
2507 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2508 output, output_length ) );
2509
2510 /* Compare with trailing garbage */
2511 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2512 output, output_length + 1 ),
2513 PSA_ERROR_INVALID_SIGNATURE );
2514
2515 /* Compare with truncated hash */
2516 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2517 output, output_length - 1 ),
2518 PSA_ERROR_INVALID_SIGNATURE );
2519
2520 /* Compare with corrupted value */
2521 for( i = 0; i < output_length; i++ )
2522 {
2523 test_set_step( i );
2524 output[i] ^= 1;
2525 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2526 output, output_length ),
2527 PSA_ERROR_INVALID_SIGNATURE );
2528 output[i] ^= 1;
2529 }
2530
2531exit:
2532 PSA_DONE( );
2533}
2534/* END_CASE */
2535
2536/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002537void hash_bad_order( )
2538{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002539 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002540 unsigned char input[] = "";
2541 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002542 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002543 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2544 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2545 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002546 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002547 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002548 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002549
Gilles Peskine8817f612018-12-18 00:18:46 +01002550 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002551
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002552 /* Call setup twice in a row. */
2553 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2554 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2555 PSA_ERROR_BAD_STATE );
2556 PSA_ASSERT( psa_hash_abort( &operation ) );
2557
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002558 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002559 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002560 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002561 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002562
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002563 /* Call update after finish. */
2564 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2565 PSA_ASSERT( psa_hash_finish( &operation,
2566 hash, sizeof( hash ), &hash_len ) );
2567 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002568 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002569 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002570
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002571 /* Call verify without calling setup beforehand. */
2572 TEST_EQUAL( psa_hash_verify( &operation,
2573 valid_hash, sizeof( valid_hash ) ),
2574 PSA_ERROR_BAD_STATE );
2575 PSA_ASSERT( psa_hash_abort( &operation ) );
2576
2577 /* Call verify after finish. */
2578 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2579 PSA_ASSERT( psa_hash_finish( &operation,
2580 hash, sizeof( hash ), &hash_len ) );
2581 TEST_EQUAL( psa_hash_verify( &operation,
2582 valid_hash, sizeof( valid_hash ) ),
2583 PSA_ERROR_BAD_STATE );
2584 PSA_ASSERT( psa_hash_abort( &operation ) );
2585
2586 /* Call verify twice in a row. */
2587 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2588 PSA_ASSERT( psa_hash_verify( &operation,
2589 valid_hash, sizeof( valid_hash ) ) );
2590 TEST_EQUAL( psa_hash_verify( &operation,
2591 valid_hash, sizeof( valid_hash ) ),
2592 PSA_ERROR_BAD_STATE );
2593 PSA_ASSERT( psa_hash_abort( &operation ) );
2594
2595 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002596 TEST_EQUAL( psa_hash_finish( &operation,
2597 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002598 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002599 PSA_ASSERT( psa_hash_abort( &operation ) );
2600
2601 /* Call finish twice in a row. */
2602 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2603 PSA_ASSERT( psa_hash_finish( &operation,
2604 hash, sizeof( hash ), &hash_len ) );
2605 TEST_EQUAL( psa_hash_finish( &operation,
2606 hash, sizeof( hash ), &hash_len ),
2607 PSA_ERROR_BAD_STATE );
2608 PSA_ASSERT( psa_hash_abort( &operation ) );
2609
2610 /* Call finish after calling verify. */
2611 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2612 PSA_ASSERT( psa_hash_verify( &operation,
2613 valid_hash, sizeof( valid_hash ) ) );
2614 TEST_EQUAL( psa_hash_finish( &operation,
2615 hash, sizeof( hash ), &hash_len ),
2616 PSA_ERROR_BAD_STATE );
2617 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002618
2619exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002620 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002621}
2622/* END_CASE */
2623
itayzafrir27e69452018-11-01 14:26:34 +02002624/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2625void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002626{
2627 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002628 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2629 * appended to it */
2630 unsigned char hash[] = {
2631 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2632 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2633 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002634 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002635 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002636
Gilles Peskine8817f612018-12-18 00:18:46 +01002637 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002638
itayzafrir27e69452018-11-01 14:26:34 +02002639 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002640 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002641 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002642 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002643
itayzafrir27e69452018-11-01 14:26:34 +02002644 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002645 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002646 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002647 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002648
itayzafrir27e69452018-11-01 14:26:34 +02002649 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002650 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002651 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002652 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002653
itayzafrirec93d302018-10-18 18:01:10 +03002654exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002655 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002656}
2657/* END_CASE */
2658
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002659/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2660void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002661{
2662 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002663 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002664 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002665 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002666 size_t hash_len;
2667
Gilles Peskine8817f612018-12-18 00:18:46 +01002668 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002669
itayzafrir58028322018-10-25 10:22:01 +03002670 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002671 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002672 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002673 hash, expected_size - 1, &hash_len ),
2674 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002675
2676exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002677 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002678}
2679/* END_CASE */
2680
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002681/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2682void hash_clone_source_state( )
2683{
2684 psa_algorithm_t alg = PSA_ALG_SHA_256;
2685 unsigned char hash[PSA_HASH_MAX_SIZE];
2686 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2687 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2688 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2689 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2690 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2691 size_t hash_len;
2692
2693 PSA_ASSERT( psa_crypto_init( ) );
2694 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2695
2696 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2697 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2698 PSA_ASSERT( psa_hash_finish( &op_finished,
2699 hash, sizeof( hash ), &hash_len ) );
2700 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2701 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2702
2703 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2704 PSA_ERROR_BAD_STATE );
2705
2706 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2707 PSA_ASSERT( psa_hash_finish( &op_init,
2708 hash, sizeof( hash ), &hash_len ) );
2709 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2710 PSA_ASSERT( psa_hash_finish( &op_finished,
2711 hash, sizeof( hash ), &hash_len ) );
2712 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2713 PSA_ASSERT( psa_hash_finish( &op_aborted,
2714 hash, sizeof( hash ), &hash_len ) );
2715
2716exit:
2717 psa_hash_abort( &op_source );
2718 psa_hash_abort( &op_init );
2719 psa_hash_abort( &op_setup );
2720 psa_hash_abort( &op_finished );
2721 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002722 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002723}
2724/* END_CASE */
2725
2726/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2727void hash_clone_target_state( )
2728{
2729 psa_algorithm_t alg = PSA_ALG_SHA_256;
2730 unsigned char hash[PSA_HASH_MAX_SIZE];
2731 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2732 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2733 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2734 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2735 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2736 size_t hash_len;
2737
2738 PSA_ASSERT( psa_crypto_init( ) );
2739
2740 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2741 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2742 PSA_ASSERT( psa_hash_finish( &op_finished,
2743 hash, sizeof( hash ), &hash_len ) );
2744 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2745 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2746
2747 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2748 PSA_ASSERT( psa_hash_finish( &op_target,
2749 hash, sizeof( hash ), &hash_len ) );
2750
2751 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2752 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2753 PSA_ERROR_BAD_STATE );
2754 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2755 PSA_ERROR_BAD_STATE );
2756
2757exit:
2758 psa_hash_abort( &op_target );
2759 psa_hash_abort( &op_init );
2760 psa_hash_abort( &op_setup );
2761 psa_hash_abort( &op_finished );
2762 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002763 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002764}
2765/* END_CASE */
2766
itayzafrir58028322018-10-25 10:22:01 +03002767/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002768void mac_operation_init( )
2769{
Jaeden Amero252ef282019-02-15 14:05:35 +00002770 const uint8_t input[1] = { 0 };
2771
Jaeden Amero769ce272019-01-04 11:48:03 +00002772 /* Test each valid way of initializing the object, except for `= {0}`, as
2773 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2774 * though it's OK by the C standard. We could test for this, but we'd need
2775 * to supress the Clang warning for the test. */
2776 psa_mac_operation_t func = psa_mac_operation_init( );
2777 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2778 psa_mac_operation_t zero;
2779
2780 memset( &zero, 0, sizeof( zero ) );
2781
Jaeden Amero252ef282019-02-15 14:05:35 +00002782 /* A freshly-initialized MAC operation should not be usable. */
2783 TEST_EQUAL( psa_mac_update( &func,
2784 input, sizeof( input ) ),
2785 PSA_ERROR_BAD_STATE );
2786 TEST_EQUAL( psa_mac_update( &init,
2787 input, sizeof( input ) ),
2788 PSA_ERROR_BAD_STATE );
2789 TEST_EQUAL( psa_mac_update( &zero,
2790 input, sizeof( input ) ),
2791 PSA_ERROR_BAD_STATE );
2792
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002793 /* A default MAC operation should be abortable without error. */
2794 PSA_ASSERT( psa_mac_abort( &func ) );
2795 PSA_ASSERT( psa_mac_abort( &init ) );
2796 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002797}
2798/* END_CASE */
2799
2800/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002801void mac_setup( int key_type_arg,
2802 data_t *key,
2803 int alg_arg,
2804 int expected_status_arg )
2805{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002806 psa_key_type_t key_type = key_type_arg;
2807 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002808 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002809 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002810 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2811#if defined(KNOWN_SUPPORTED_MAC_ALG)
2812 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2813#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002814
Gilles Peskine8817f612018-12-18 00:18:46 +01002815 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002816
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002817 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2818 &operation, &status ) )
2819 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002820 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002821
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002822 /* The operation object should be reusable. */
2823#if defined(KNOWN_SUPPORTED_MAC_ALG)
2824 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2825 smoke_test_key_data,
2826 sizeof( smoke_test_key_data ),
2827 KNOWN_SUPPORTED_MAC_ALG,
2828 &operation, &status ) )
2829 goto exit;
2830 TEST_EQUAL( status, PSA_SUCCESS );
2831#endif
2832
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002833exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002834 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002835}
2836/* END_CASE */
2837
2838/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002839void mac_bad_order( )
2840{
2841 psa_key_handle_t handle = 0;
2842 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2843 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2844 const uint8_t key[] = {
2845 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2846 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2847 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002848 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002849 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2850 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2851 size_t sign_mac_length = 0;
2852 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2853 const uint8_t verify_mac[] = {
2854 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2855 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2856 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2857
2858 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002859 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002860 psa_set_key_algorithm( &attributes, alg );
2861 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002862
Gilles Peskine73676cb2019-05-15 20:15:10 +02002863 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002864
Jaeden Amero252ef282019-02-15 14:05:35 +00002865 /* Call update without calling setup beforehand. */
2866 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2867 PSA_ERROR_BAD_STATE );
2868 PSA_ASSERT( psa_mac_abort( &operation ) );
2869
2870 /* Call sign finish without calling setup beforehand. */
2871 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2872 &sign_mac_length),
2873 PSA_ERROR_BAD_STATE );
2874 PSA_ASSERT( psa_mac_abort( &operation ) );
2875
2876 /* Call verify finish without calling setup beforehand. */
2877 TEST_EQUAL( psa_mac_verify_finish( &operation,
2878 verify_mac, sizeof( verify_mac ) ),
2879 PSA_ERROR_BAD_STATE );
2880 PSA_ASSERT( psa_mac_abort( &operation ) );
2881
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002882 /* Call setup twice in a row. */
2883 PSA_ASSERT( psa_mac_sign_setup( &operation,
2884 handle, alg ) );
2885 TEST_EQUAL( psa_mac_sign_setup( &operation,
2886 handle, alg ),
2887 PSA_ERROR_BAD_STATE );
2888 PSA_ASSERT( psa_mac_abort( &operation ) );
2889
Jaeden Amero252ef282019-02-15 14:05:35 +00002890 /* Call update after sign finish. */
2891 PSA_ASSERT( psa_mac_sign_setup( &operation,
2892 handle, alg ) );
2893 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2894 PSA_ASSERT( psa_mac_sign_finish( &operation,
2895 sign_mac, sizeof( sign_mac ),
2896 &sign_mac_length ) );
2897 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2898 PSA_ERROR_BAD_STATE );
2899 PSA_ASSERT( psa_mac_abort( &operation ) );
2900
2901 /* Call update after verify finish. */
2902 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002903 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002904 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2905 PSA_ASSERT( psa_mac_verify_finish( &operation,
2906 verify_mac, sizeof( verify_mac ) ) );
2907 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2908 PSA_ERROR_BAD_STATE );
2909 PSA_ASSERT( psa_mac_abort( &operation ) );
2910
2911 /* Call sign finish twice in a row. */
2912 PSA_ASSERT( psa_mac_sign_setup( &operation,
2913 handle, alg ) );
2914 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2915 PSA_ASSERT( psa_mac_sign_finish( &operation,
2916 sign_mac, sizeof( sign_mac ),
2917 &sign_mac_length ) );
2918 TEST_EQUAL( psa_mac_sign_finish( &operation,
2919 sign_mac, sizeof( sign_mac ),
2920 &sign_mac_length ),
2921 PSA_ERROR_BAD_STATE );
2922 PSA_ASSERT( psa_mac_abort( &operation ) );
2923
2924 /* Call verify finish twice in a row. */
2925 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002926 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002927 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2928 PSA_ASSERT( psa_mac_verify_finish( &operation,
2929 verify_mac, sizeof( verify_mac ) ) );
2930 TEST_EQUAL( psa_mac_verify_finish( &operation,
2931 verify_mac, sizeof( verify_mac ) ),
2932 PSA_ERROR_BAD_STATE );
2933 PSA_ASSERT( psa_mac_abort( &operation ) );
2934
2935 /* Setup sign but try verify. */
2936 PSA_ASSERT( psa_mac_sign_setup( &operation,
2937 handle, alg ) );
2938 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2939 TEST_EQUAL( psa_mac_verify_finish( &operation,
2940 verify_mac, sizeof( verify_mac ) ),
2941 PSA_ERROR_BAD_STATE );
2942 PSA_ASSERT( psa_mac_abort( &operation ) );
2943
2944 /* Setup verify but try sign. */
2945 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002946 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002947 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2948 TEST_EQUAL( psa_mac_sign_finish( &operation,
2949 sign_mac, sizeof( sign_mac ),
2950 &sign_mac_length ),
2951 PSA_ERROR_BAD_STATE );
2952 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002953
Gilles Peskine76b29a72019-05-28 14:08:50 +02002954 PSA_ASSERT( psa_destroy_key( handle ) );
2955
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002956exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002957 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002958}
2959/* END_CASE */
2960
2961/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002962void mac_sign( int key_type_arg,
2963 data_t *key,
2964 int alg_arg,
2965 data_t *input,
2966 data_t *expected_mac )
2967{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002968 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002969 psa_key_type_t key_type = key_type_arg;
2970 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002971 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002972 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002973 /* Leave a little extra room in the output buffer. At the end of the
2974 * test, we'll check that the implementation didn't overwrite onto
2975 * this extra room. */
2976 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2977 size_t mac_buffer_size =
2978 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2979 size_t mac_length = 0;
2980
2981 memset( actual_mac, '+', sizeof( actual_mac ) );
2982 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2983 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2984
Gilles Peskine8817f612018-12-18 00:18:46 +01002985 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002986
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002987 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002988 psa_set_key_algorithm( &attributes, alg );
2989 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002990
Gilles Peskine73676cb2019-05-15 20:15:10 +02002991 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002992
2993 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_mac_sign_setup( &operation,
2995 handle, alg ) );
2996 PSA_ASSERT( psa_mac_update( &operation,
2997 input->x, input->len ) );
2998 PSA_ASSERT( psa_mac_sign_finish( &operation,
2999 actual_mac, mac_buffer_size,
3000 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003001
3002 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003003 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3004 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003005
3006 /* Verify that the end of the buffer is untouched. */
3007 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
3008 sizeof( actual_mac ) - mac_length ) );
3009
3010exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003011 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003012 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003013}
3014/* END_CASE */
3015
3016/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003017void mac_verify( int key_type_arg,
3018 data_t *key,
3019 int alg_arg,
3020 data_t *input,
3021 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003023 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003024 psa_key_type_t key_type = key_type_arg;
3025 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003026 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028
Gilles Peskine69c12672018-06-28 00:07:19 +02003029 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3030
Gilles Peskine8817f612018-12-18 00:18:46 +01003031 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003032
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003034 psa_set_key_algorithm( &attributes, alg );
3035 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003036
Gilles Peskine73676cb2019-05-15 20:15:10 +02003037 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003038
Gilles Peskine8817f612018-12-18 00:18:46 +01003039 PSA_ASSERT( psa_mac_verify_setup( &operation,
3040 handle, alg ) );
3041 PSA_ASSERT( psa_destroy_key( handle ) );
3042 PSA_ASSERT( psa_mac_update( &operation,
3043 input->x, input->len ) );
3044 PSA_ASSERT( psa_mac_verify_finish( &operation,
3045 expected_mac->x,
3046 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003047
3048exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003049 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003050 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003051}
3052/* END_CASE */
3053
3054/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003055void cipher_operation_init( )
3056{
Jaeden Ameroab439972019-02-15 14:12:05 +00003057 const uint8_t input[1] = { 0 };
3058 unsigned char output[1] = { 0 };
3059 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003060 /* Test each valid way of initializing the object, except for `= {0}`, as
3061 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3062 * though it's OK by the C standard. We could test for this, but we'd need
3063 * to supress the Clang warning for the test. */
3064 psa_cipher_operation_t func = psa_cipher_operation_init( );
3065 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3066 psa_cipher_operation_t zero;
3067
3068 memset( &zero, 0, sizeof( zero ) );
3069
Jaeden Ameroab439972019-02-15 14:12:05 +00003070 /* A freshly-initialized cipher operation should not be usable. */
3071 TEST_EQUAL( psa_cipher_update( &func,
3072 input, sizeof( input ),
3073 output, sizeof( output ),
3074 &output_length ),
3075 PSA_ERROR_BAD_STATE );
3076 TEST_EQUAL( psa_cipher_update( &init,
3077 input, sizeof( input ),
3078 output, sizeof( output ),
3079 &output_length ),
3080 PSA_ERROR_BAD_STATE );
3081 TEST_EQUAL( psa_cipher_update( &zero,
3082 input, sizeof( input ),
3083 output, sizeof( output ),
3084 &output_length ),
3085 PSA_ERROR_BAD_STATE );
3086
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003087 /* A default cipher operation should be abortable without error. */
3088 PSA_ASSERT( psa_cipher_abort( &func ) );
3089 PSA_ASSERT( psa_cipher_abort( &init ) );
3090 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003091}
3092/* END_CASE */
3093
3094/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003095void cipher_setup( int key_type_arg,
3096 data_t *key,
3097 int alg_arg,
3098 int expected_status_arg )
3099{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003100 psa_key_type_t key_type = key_type_arg;
3101 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003102 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003103 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003104 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003105#if defined(KNOWN_SUPPORTED_MAC_ALG)
3106 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3107#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003108
Gilles Peskine8817f612018-12-18 00:18:46 +01003109 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003110
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003111 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3112 &operation, &status ) )
3113 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003114 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003115
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003116 /* The operation object should be reusable. */
3117#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3118 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3119 smoke_test_key_data,
3120 sizeof( smoke_test_key_data ),
3121 KNOWN_SUPPORTED_CIPHER_ALG,
3122 &operation, &status ) )
3123 goto exit;
3124 TEST_EQUAL( status, PSA_SUCCESS );
3125#endif
3126
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003127exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003128 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003129}
3130/* END_CASE */
3131
3132/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003133void cipher_bad_order( )
3134{
3135 psa_key_handle_t handle = 0;
3136 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3137 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003138 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003139 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3140 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3141 const uint8_t key[] = {
3142 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3143 0xaa, 0xaa, 0xaa, 0xaa };
3144 const uint8_t text[] = {
3145 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3146 0xbb, 0xbb, 0xbb, 0xbb };
3147 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3148 size_t length = 0;
3149
3150 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003151 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3152 psa_set_key_algorithm( &attributes, alg );
3153 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003154 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003155
3156
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003157 /* Call encrypt setup twice in a row. */
3158 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3159 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
3160 PSA_ERROR_BAD_STATE );
3161 PSA_ASSERT( psa_cipher_abort( &operation ) );
3162
3163 /* Call decrypt setup twice in a row. */
3164 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
3165 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
3166 PSA_ERROR_BAD_STATE );
3167 PSA_ASSERT( psa_cipher_abort( &operation ) );
3168
Jaeden Ameroab439972019-02-15 14:12:05 +00003169 /* Generate an IV without calling setup beforehand. */
3170 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3171 buffer, sizeof( buffer ),
3172 &length ),
3173 PSA_ERROR_BAD_STATE );
3174 PSA_ASSERT( psa_cipher_abort( &operation ) );
3175
3176 /* Generate an IV twice in a row. */
3177 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3178 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3179 buffer, sizeof( buffer ),
3180 &length ) );
3181 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3182 buffer, sizeof( buffer ),
3183 &length ),
3184 PSA_ERROR_BAD_STATE );
3185 PSA_ASSERT( psa_cipher_abort( &operation ) );
3186
3187 /* Generate an IV after it's already set. */
3188 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3189 PSA_ASSERT( psa_cipher_set_iv( &operation,
3190 iv, sizeof( iv ) ) );
3191 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3192 buffer, sizeof( buffer ),
3193 &length ),
3194 PSA_ERROR_BAD_STATE );
3195 PSA_ASSERT( psa_cipher_abort( &operation ) );
3196
3197 /* Set an IV without calling setup beforehand. */
3198 TEST_EQUAL( psa_cipher_set_iv( &operation,
3199 iv, sizeof( iv ) ),
3200 PSA_ERROR_BAD_STATE );
3201 PSA_ASSERT( psa_cipher_abort( &operation ) );
3202
3203 /* Set an IV after it's already set. */
3204 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3205 PSA_ASSERT( psa_cipher_set_iv( &operation,
3206 iv, sizeof( iv ) ) );
3207 TEST_EQUAL( psa_cipher_set_iv( &operation,
3208 iv, sizeof( iv ) ),
3209 PSA_ERROR_BAD_STATE );
3210 PSA_ASSERT( psa_cipher_abort( &operation ) );
3211
3212 /* Set an IV after it's already generated. */
3213 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3214 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3215 buffer, sizeof( buffer ),
3216 &length ) );
3217 TEST_EQUAL( psa_cipher_set_iv( &operation,
3218 iv, sizeof( iv ) ),
3219 PSA_ERROR_BAD_STATE );
3220 PSA_ASSERT( psa_cipher_abort( &operation ) );
3221
3222 /* Call update without calling setup beforehand. */
3223 TEST_EQUAL( psa_cipher_update( &operation,
3224 text, sizeof( text ),
3225 buffer, sizeof( buffer ),
3226 &length ),
3227 PSA_ERROR_BAD_STATE );
3228 PSA_ASSERT( psa_cipher_abort( &operation ) );
3229
3230 /* Call update without an IV where an IV is required. */
3231 TEST_EQUAL( psa_cipher_update( &operation,
3232 text, sizeof( text ),
3233 buffer, sizeof( buffer ),
3234 &length ),
3235 PSA_ERROR_BAD_STATE );
3236 PSA_ASSERT( psa_cipher_abort( &operation ) );
3237
3238 /* Call update after finish. */
3239 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3240 PSA_ASSERT( psa_cipher_set_iv( &operation,
3241 iv, sizeof( iv ) ) );
3242 PSA_ASSERT( psa_cipher_finish( &operation,
3243 buffer, sizeof( buffer ), &length ) );
3244 TEST_EQUAL( psa_cipher_update( &operation,
3245 text, sizeof( text ),
3246 buffer, sizeof( buffer ),
3247 &length ),
3248 PSA_ERROR_BAD_STATE );
3249 PSA_ASSERT( psa_cipher_abort( &operation ) );
3250
3251 /* Call finish without calling setup beforehand. */
3252 TEST_EQUAL( psa_cipher_finish( &operation,
3253 buffer, sizeof( buffer ), &length ),
3254 PSA_ERROR_BAD_STATE );
3255 PSA_ASSERT( psa_cipher_abort( &operation ) );
3256
3257 /* Call finish without an IV where an IV is required. */
3258 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3259 /* Not calling update means we are encrypting an empty buffer, which is OK
3260 * for cipher modes with padding. */
3261 TEST_EQUAL( psa_cipher_finish( &operation,
3262 buffer, sizeof( buffer ), &length ),
3263 PSA_ERROR_BAD_STATE );
3264 PSA_ASSERT( psa_cipher_abort( &operation ) );
3265
3266 /* Call finish twice in a row. */
3267 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3268 PSA_ASSERT( psa_cipher_set_iv( &operation,
3269 iv, sizeof( iv ) ) );
3270 PSA_ASSERT( psa_cipher_finish( &operation,
3271 buffer, sizeof( buffer ), &length ) );
3272 TEST_EQUAL( psa_cipher_finish( &operation,
3273 buffer, sizeof( buffer ), &length ),
3274 PSA_ERROR_BAD_STATE );
3275 PSA_ASSERT( psa_cipher_abort( &operation ) );
3276
Gilles Peskine76b29a72019-05-28 14:08:50 +02003277 PSA_ASSERT( psa_destroy_key( handle ) );
3278
Jaeden Ameroab439972019-02-15 14:12:05 +00003279exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003280 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003281}
3282/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003283
Gilles Peskine50e586b2018-06-08 14:28:46 +02003284/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003285void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003286 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003287 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003288 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003289{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003290 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003291 psa_status_t status;
3292 psa_key_type_t key_type = key_type_arg;
3293 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003294 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003295 unsigned char *output = NULL;
3296 size_t output_buffer_size = 0;
3297 size_t function_output_length = 0;
3298 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003299 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003301
Gilles Peskine8817f612018-12-18 00:18:46 +01003302 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003303
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003304 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3305 psa_set_key_algorithm( &attributes, alg );
3306 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003307
Gilles Peskine73676cb2019-05-15 20:15:10 +02003308 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003309
Gilles Peskine8817f612018-12-18 00:18:46 +01003310 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3311 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003312
Gilles Peskine423005e2019-05-06 15:22:57 +02003313 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003314 output_buffer_size = ( (size_t) input->len +
3315 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003316 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003317
Gilles Peskine8817f612018-12-18 00:18:46 +01003318 PSA_ASSERT( psa_cipher_update( &operation,
3319 input->x, input->len,
3320 output, output_buffer_size,
3321 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003322 total_output_length += function_output_length;
3323 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003324 output + total_output_length,
3325 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003326 &function_output_length );
3327 total_output_length += function_output_length;
3328
Gilles Peskinefe11b722018-12-18 00:24:04 +01003329 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003330 if( expected_status == PSA_SUCCESS )
3331 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003332 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003333 ASSERT_COMPARE( expected_output->x, expected_output->len,
3334 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003335 }
3336
3337exit:
3338 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003339 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003340 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003341}
3342/* END_CASE */
3343
3344/* BEGIN_CASE */
3345void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003346 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003347 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003348 int first_part_size_arg,
3349 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003350 data_t *expected_output )
3351{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003352 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003353 psa_key_type_t key_type = key_type_arg;
3354 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003355 size_t first_part_size = first_part_size_arg;
3356 size_t output1_length = output1_length_arg;
3357 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003358 unsigned char *output = NULL;
3359 size_t output_buffer_size = 0;
3360 size_t function_output_length = 0;
3361 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003362 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003364
Gilles Peskine8817f612018-12-18 00:18:46 +01003365 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003366
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003367 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3368 psa_set_key_algorithm( &attributes, alg );
3369 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003370
Gilles Peskine73676cb2019-05-15 20:15:10 +02003371 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003372
Gilles Peskine8817f612018-12-18 00:18:46 +01003373 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3374 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003375
Gilles Peskine423005e2019-05-06 15:22:57 +02003376 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003377 output_buffer_size = ( (size_t) input->len +
3378 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003379 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003380
Gilles Peskinee0866522019-02-19 19:44:00 +01003381 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003382 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3383 output, output_buffer_size,
3384 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003385 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003386 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003387 PSA_ASSERT( psa_cipher_update( &operation,
3388 input->x + first_part_size,
3389 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003390 output + total_output_length,
3391 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003392 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003393 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003394 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003395 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003396 output + total_output_length,
3397 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003398 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003399 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003400 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003401
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003402 ASSERT_COMPARE( expected_output->x, expected_output->len,
3403 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003404
3405exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003406 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003407 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003408 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003409}
3410/* END_CASE */
3411
3412/* BEGIN_CASE */
3413void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003414 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003415 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003416 int first_part_size_arg,
3417 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003418 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003419{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003420 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003421
3422 psa_key_type_t key_type = key_type_arg;
3423 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003424 size_t first_part_size = first_part_size_arg;
3425 size_t output1_length = output1_length_arg;
3426 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003427 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003428 size_t output_buffer_size = 0;
3429 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003430 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003431 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003432 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003433
Gilles Peskine8817f612018-12-18 00:18:46 +01003434 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003435
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003436 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3437 psa_set_key_algorithm( &attributes, alg );
3438 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003439
Gilles Peskine73676cb2019-05-15 20:15:10 +02003440 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003441
Gilles Peskine8817f612018-12-18 00:18:46 +01003442 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3443 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003444
Gilles Peskine423005e2019-05-06 15:22:57 +02003445 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003446
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003447 output_buffer_size = ( (size_t) input->len +
3448 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003449 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003450
Gilles Peskinee0866522019-02-19 19:44:00 +01003451 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003452 PSA_ASSERT( psa_cipher_update( &operation,
3453 input->x, first_part_size,
3454 output, output_buffer_size,
3455 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003456 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003457 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003458 PSA_ASSERT( psa_cipher_update( &operation,
3459 input->x + first_part_size,
3460 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003461 output + total_output_length,
3462 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003463 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003464 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003465 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003466 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003467 output + total_output_length,
3468 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003469 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003470 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003471 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003472
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003473 ASSERT_COMPARE( expected_output->x, expected_output->len,
3474 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003475
3476exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003477 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003478 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003479 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003480}
3481/* END_CASE */
3482
Gilles Peskine50e586b2018-06-08 14:28:46 +02003483/* BEGIN_CASE */
3484void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003485 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003486 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003487 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003488{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003489 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003490 psa_status_t status;
3491 psa_key_type_t key_type = key_type_arg;
3492 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003493 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003494 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003495 size_t output_buffer_size = 0;
3496 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003497 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003498 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003499 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003500
Gilles Peskine8817f612018-12-18 00:18:46 +01003501 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003502
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003503 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3504 psa_set_key_algorithm( &attributes, alg );
3505 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003506
Gilles Peskine73676cb2019-05-15 20:15:10 +02003507 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003508
Gilles Peskine8817f612018-12-18 00:18:46 +01003509 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3510 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003511
Gilles Peskine423005e2019-05-06 15:22:57 +02003512 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003513
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003514 output_buffer_size = ( (size_t) input->len +
3515 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003516 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003517
Gilles Peskine8817f612018-12-18 00:18:46 +01003518 PSA_ASSERT( psa_cipher_update( &operation,
3519 input->x, input->len,
3520 output, output_buffer_size,
3521 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003522 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003523 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003524 output + total_output_length,
3525 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003526 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003527 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003528 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003529
3530 if( expected_status == PSA_SUCCESS )
3531 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003532 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003533 ASSERT_COMPARE( expected_output->x, expected_output->len,
3534 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003535 }
3536
Gilles Peskine50e586b2018-06-08 14:28:46 +02003537exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003538 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003539 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003540 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003541}
3542/* END_CASE */
3543
Gilles Peskine50e586b2018-06-08 14:28:46 +02003544/* BEGIN_CASE */
3545void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003546 data_t *key,
3547 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003548{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003549 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003550 psa_key_type_t key_type = key_type_arg;
3551 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003552 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003553 size_t iv_size = 16;
3554 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003555 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003556 size_t output1_size = 0;
3557 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003558 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003559 size_t output2_size = 0;
3560 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003561 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003562 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3563 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003565
Gilles Peskine8817f612018-12-18 00:18:46 +01003566 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003567
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003568 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3569 psa_set_key_algorithm( &attributes, alg );
3570 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003571
Gilles Peskine73676cb2019-05-15 20:15:10 +02003572 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003573
Gilles Peskine8817f612018-12-18 00:18:46 +01003574 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3575 handle, alg ) );
3576 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3577 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003578
Gilles Peskine8817f612018-12-18 00:18:46 +01003579 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3580 iv, iv_size,
3581 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003582 output1_size = ( (size_t) input->len +
3583 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003584 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003585
Gilles Peskine8817f612018-12-18 00:18:46 +01003586 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3587 output1, output1_size,
3588 &output1_length ) );
3589 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003590 output1 + output1_length,
3591 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003592 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003593
Gilles Peskine048b7f02018-06-08 14:20:49 +02003594 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003595
Gilles Peskine8817f612018-12-18 00:18:46 +01003596 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003597
3598 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003599 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003600
Gilles Peskine8817f612018-12-18 00:18:46 +01003601 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3602 iv, iv_length ) );
3603 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3604 output2, output2_size,
3605 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003606 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003607 PSA_ASSERT( psa_cipher_finish( &operation2,
3608 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003609 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003610 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003611
Gilles Peskine048b7f02018-06-08 14:20:49 +02003612 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003613
Gilles Peskine8817f612018-12-18 00:18:46 +01003614 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003615
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003616 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003617
3618exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003619 mbedtls_free( output1 );
3620 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003621 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003622 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003623}
3624/* END_CASE */
3625
3626/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003627void cipher_verify_output_multipart( int alg_arg,
3628 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003629 data_t *key,
3630 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003631 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003632{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003633 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003634 psa_key_type_t key_type = key_type_arg;
3635 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003636 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003637 unsigned char iv[16] = {0};
3638 size_t iv_size = 16;
3639 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003640 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003641 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003642 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003643 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003644 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003645 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003646 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003647 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3648 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003650
Gilles Peskine8817f612018-12-18 00:18:46 +01003651 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003652
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003653 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3654 psa_set_key_algorithm( &attributes, alg );
3655 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003656
Gilles Peskine73676cb2019-05-15 20:15:10 +02003657 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003658
Gilles Peskine8817f612018-12-18 00:18:46 +01003659 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3660 handle, alg ) );
3661 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3662 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003663
Gilles Peskine8817f612018-12-18 00:18:46 +01003664 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3665 iv, iv_size,
3666 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003667 output1_buffer_size = ( (size_t) input->len +
3668 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003669 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003670
Gilles Peskinee0866522019-02-19 19:44:00 +01003671 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003672
Gilles Peskine8817f612018-12-18 00:18:46 +01003673 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3674 output1, output1_buffer_size,
3675 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003676 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003677
Gilles Peskine8817f612018-12-18 00:18:46 +01003678 PSA_ASSERT( psa_cipher_update( &operation1,
3679 input->x + first_part_size,
3680 input->len - first_part_size,
3681 output1, output1_buffer_size,
3682 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003683 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003684
Gilles Peskine8817f612018-12-18 00:18:46 +01003685 PSA_ASSERT( psa_cipher_finish( &operation1,
3686 output1 + output1_length,
3687 output1_buffer_size - output1_length,
3688 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003689 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003690
Gilles Peskine8817f612018-12-18 00:18:46 +01003691 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003692
Gilles Peskine048b7f02018-06-08 14:20:49 +02003693 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003694 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003695
Gilles Peskine8817f612018-12-18 00:18:46 +01003696 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3697 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003698
Gilles Peskine8817f612018-12-18 00:18:46 +01003699 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3700 output2, output2_buffer_size,
3701 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003702 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003703
Gilles Peskine8817f612018-12-18 00:18:46 +01003704 PSA_ASSERT( psa_cipher_update( &operation2,
3705 output1 + first_part_size,
3706 output1_length - first_part_size,
3707 output2, output2_buffer_size,
3708 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003709 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003710
Gilles Peskine8817f612018-12-18 00:18:46 +01003711 PSA_ASSERT( psa_cipher_finish( &operation2,
3712 output2 + output2_length,
3713 output2_buffer_size - output2_length,
3714 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003715 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003716
Gilles Peskine8817f612018-12-18 00:18:46 +01003717 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003718
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003719 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003720
3721exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003722 mbedtls_free( output1 );
3723 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003724 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003725 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003726}
3727/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003728
Gilles Peskine20035e32018-02-03 22:44:14 +01003729/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003730void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003731 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003732 data_t *nonce,
3733 data_t *additional_data,
3734 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003735 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003736{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003737 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003738 psa_key_type_t key_type = key_type_arg;
3739 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003740 unsigned char *output_data = NULL;
3741 size_t output_size = 0;
3742 size_t output_length = 0;
3743 unsigned char *output_data2 = NULL;
3744 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003745 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003746 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003748
Gilles Peskine4abf7412018-06-18 16:35:34 +02003749 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003750 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3751 * should be exact. */
3752 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3753 TEST_EQUAL( output_size,
3754 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003755 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003756
Gilles Peskine8817f612018-12-18 00:18:46 +01003757 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003758
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003759 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3760 psa_set_key_algorithm( &attributes, alg );
3761 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003762
Gilles Peskine049c7532019-05-15 20:22:09 +02003763 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3764 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003765
Gilles Peskinefe11b722018-12-18 00:24:04 +01003766 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3767 nonce->x, nonce->len,
3768 additional_data->x,
3769 additional_data->len,
3770 input_data->x, input_data->len,
3771 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003772 &output_length ),
3773 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003774
3775 if( PSA_SUCCESS == expected_result )
3776 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003777 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003778
Gilles Peskine003a4a92019-05-14 16:09:40 +02003779 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3780 * should be exact. */
3781 TEST_EQUAL( input_data->len,
3782 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3783
Gilles Peskinefe11b722018-12-18 00:24:04 +01003784 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3785 nonce->x, nonce->len,
3786 additional_data->x,
3787 additional_data->len,
3788 output_data, output_length,
3789 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003790 &output_length2 ),
3791 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003792
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003793 ASSERT_COMPARE( input_data->x, input_data->len,
3794 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003795 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003796
Gilles Peskinea1cac842018-06-11 19:33:02 +02003797exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003798 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003799 mbedtls_free( output_data );
3800 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003801 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003802}
3803/* END_CASE */
3804
3805/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003806void aead_encrypt( int key_type_arg, data_t *key_data,
3807 int alg_arg,
3808 data_t *nonce,
3809 data_t *additional_data,
3810 data_t *input_data,
3811 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003812{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003813 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003814 psa_key_type_t key_type = key_type_arg;
3815 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003816 unsigned char *output_data = NULL;
3817 size_t output_size = 0;
3818 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003819 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003821
Gilles Peskine4abf7412018-06-18 16:35:34 +02003822 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003823 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3824 * should be exact. */
3825 TEST_EQUAL( output_size,
3826 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003827 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003828
Gilles Peskine8817f612018-12-18 00:18:46 +01003829 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003830
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3832 psa_set_key_algorithm( &attributes, alg );
3833 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003834
Gilles Peskine049c7532019-05-15 20:22:09 +02003835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3836 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003837
Gilles Peskine8817f612018-12-18 00:18:46 +01003838 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3839 nonce->x, nonce->len,
3840 additional_data->x, additional_data->len,
3841 input_data->x, input_data->len,
3842 output_data, output_size,
3843 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003844
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003845 ASSERT_COMPARE( expected_result->x, expected_result->len,
3846 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003847
Gilles Peskinea1cac842018-06-11 19:33:02 +02003848exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003849 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003850 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003851 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003852}
3853/* END_CASE */
3854
3855/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003856void aead_decrypt( int key_type_arg, data_t *key_data,
3857 int alg_arg,
3858 data_t *nonce,
3859 data_t *additional_data,
3860 data_t *input_data,
3861 data_t *expected_data,
3862 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003863{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003864 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003865 psa_key_type_t key_type = key_type_arg;
3866 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003867 unsigned char *output_data = NULL;
3868 size_t output_size = 0;
3869 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003870 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003872 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003873
Gilles Peskine003a4a92019-05-14 16:09:40 +02003874 output_size = input_data->len - tag_length;
3875 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3876 * should be exact. */
3877 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3878 TEST_EQUAL( output_size,
3879 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003880 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003881
Gilles Peskine8817f612018-12-18 00:18:46 +01003882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003883
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003884 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3885 psa_set_key_algorithm( &attributes, alg );
3886 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003887
Gilles Peskine049c7532019-05-15 20:22:09 +02003888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3889 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003890
Gilles Peskinefe11b722018-12-18 00:24:04 +01003891 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3892 nonce->x, nonce->len,
3893 additional_data->x,
3894 additional_data->len,
3895 input_data->x, input_data->len,
3896 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003897 &output_length ),
3898 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003899
Gilles Peskine2d277862018-06-18 15:41:12 +02003900 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003901 ASSERT_COMPARE( expected_data->x, expected_data->len,
3902 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003903
Gilles Peskinea1cac842018-06-11 19:33:02 +02003904exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003905 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003906 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003907 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003908}
3909/* END_CASE */
3910
3911/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003912void signature_size( int type_arg,
3913 int bits,
3914 int alg_arg,
3915 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003916{
3917 psa_key_type_t type = type_arg;
3918 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003919 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003920
Gilles Peskinefe11b722018-12-18 00:24:04 +01003921 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003922#if defined(MBEDTLS_TEST_DEPRECATED)
3923 TEST_EQUAL( actual_size,
3924 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3925#endif /* MBEDTLS_TEST_DEPRECATED */
3926
Gilles Peskinee59236f2018-01-27 23:32:46 +01003927exit:
3928 ;
3929}
3930/* END_CASE */
3931
3932/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003933void sign_deterministic( int key_type_arg, data_t *key_data,
3934 int alg_arg, data_t *input_data,
3935 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003936{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003937 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003938 psa_key_type_t key_type = key_type_arg;
3939 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003940 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003941 unsigned char *signature = NULL;
3942 size_t signature_size;
3943 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003944 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003945
Gilles Peskine8817f612018-12-18 00:18:46 +01003946 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003947
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003948 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003949 psa_set_key_algorithm( &attributes, alg );
3950 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003951
Gilles Peskine049c7532019-05-15 20:22:09 +02003952 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3953 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003954 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3955 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003956
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003957 /* Allocate a buffer which has the size advertized by the
3958 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003959 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003960 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003961 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003962 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003963 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003964
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003965 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003966 PSA_ASSERT( psa_sign_hash( handle, alg,
3967 input_data->x, input_data->len,
3968 signature, signature_size,
3969 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003970 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003971 ASSERT_COMPARE( output_data->x, output_data->len,
3972 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003973
Gilles Peskine0627f982019-11-26 19:12:16 +01003974#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003975 memset( signature, 0, signature_size );
3976 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01003977 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3978 input_data->x, input_data->len,
3979 signature, signature_size,
3980 &signature_length ) );
3981 ASSERT_COMPARE( output_data->x, output_data->len,
3982 signature, signature_length );
3983#endif /* MBEDTLS_TEST_DEPRECATED */
3984
Gilles Peskine20035e32018-02-03 22:44:14 +01003985exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003986 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003987 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003988 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003989 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003990}
3991/* END_CASE */
3992
3993/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003994void sign_fail( int key_type_arg, data_t *key_data,
3995 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003996 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003997{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003998 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003999 psa_key_type_t key_type = key_type_arg;
4000 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004001 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004002 psa_status_t actual_status;
4003 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004004 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004005 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004007
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004008 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004009
Gilles Peskine8817f612018-12-18 00:18:46 +01004010 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004011
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004012 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004013 psa_set_key_algorithm( &attributes, alg );
4014 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004015
Gilles Peskine049c7532019-05-15 20:22:09 +02004016 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4017 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004018
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004019 actual_status = psa_sign_hash( handle, alg,
4020 input_data->x, input_data->len,
4021 signature, signature_size,
4022 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004023 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004024 /* The value of *signature_length is unspecified on error, but
4025 * whatever it is, it should be less than signature_size, so that
4026 * if the caller tries to read *signature_length bytes without
4027 * checking the error code then they don't overflow a buffer. */
4028 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004029
Gilles Peskine895242b2019-11-29 12:15:40 +01004030#if defined(MBEDTLS_TEST_DEPRECATED)
4031 signature_length = INVALID_EXPORT_LENGTH;
4032 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
4033 input_data->x, input_data->len,
4034 signature, signature_size,
4035 &signature_length ),
4036 expected_status );
4037 TEST_ASSERT( signature_length <= signature_size );
4038#endif /* MBEDTLS_TEST_DEPRECATED */
4039
Gilles Peskine20035e32018-02-03 22:44:14 +01004040exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004041 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004042 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01004043 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004044 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004045}
4046/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004047
4048/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004049void sign_verify( int key_type_arg, data_t *key_data,
4050 int alg_arg, data_t *input_data )
4051{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004052 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02004053 psa_key_type_t key_type = key_type_arg;
4054 psa_algorithm_t alg = alg_arg;
4055 size_t key_bits;
4056 unsigned char *signature = NULL;
4057 size_t signature_size;
4058 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004060
Gilles Peskine8817f612018-12-18 00:18:46 +01004061 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004062
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004063 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004064 psa_set_key_algorithm( &attributes, alg );
4065 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004066
Gilles Peskine049c7532019-05-15 20:22:09 +02004067 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4068 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004069 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4070 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004071
4072 /* Allocate a buffer which has the size advertized by the
4073 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004074 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004075 key_bits, alg );
4076 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004077 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004078 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004079
4080 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004081 PSA_ASSERT( psa_sign_hash( handle, alg,
4082 input_data->x, input_data->len,
4083 signature, signature_size,
4084 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004085 /* Check that the signature length looks sensible. */
4086 TEST_ASSERT( signature_length <= signature_size );
4087 TEST_ASSERT( signature_length > 0 );
4088
4089 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004090 PSA_ASSERT( psa_verify_hash( handle, alg,
4091 input_data->x, input_data->len,
4092 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004093
4094 if( input_data->len != 0 )
4095 {
4096 /* Flip a bit in the input and verify that the signature is now
4097 * detected as invalid. Flip a bit at the beginning, not at the end,
4098 * because ECDSA may ignore the last few bits of the input. */
4099 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004100 TEST_EQUAL( psa_verify_hash( handle, alg,
4101 input_data->x, input_data->len,
4102 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004103 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004104 }
4105
4106exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004107 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004108 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02004109 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004110 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004111}
4112/* END_CASE */
4113
4114/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004115void asymmetric_verify( int key_type_arg, data_t *key_data,
4116 int alg_arg, data_t *hash_data,
4117 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004118{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004119 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03004120 psa_key_type_t key_type = key_type_arg;
4121 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004123
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004124 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004125
Gilles Peskine8817f612018-12-18 00:18:46 +01004126 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004127
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004128 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004129 psa_set_key_algorithm( &attributes, alg );
4130 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004131
Gilles Peskine049c7532019-05-15 20:22:09 +02004132 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4133 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03004134
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004135 PSA_ASSERT( psa_verify_hash( handle, alg,
4136 hash_data->x, hash_data->len,
4137 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004138
4139#if defined(MBEDTLS_TEST_DEPRECATED)
4140 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
4141 hash_data->x, hash_data->len,
4142 signature_data->x,
4143 signature_data->len ) );
4144
4145#endif /* MBEDTLS_TEST_DEPRECATED */
4146
itayzafrir5c753392018-05-08 11:18:38 +03004147exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004148 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004149 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004150 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004151}
4152/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004153
4154/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004155void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4156 int alg_arg, data_t *hash_data,
4157 data_t *signature_data,
4158 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004159{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004160 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004161 psa_key_type_t key_type = key_type_arg;
4162 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004163 psa_status_t actual_status;
4164 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004166
Gilles Peskine8817f612018-12-18 00:18:46 +01004167 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004168
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004169 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004170 psa_set_key_algorithm( &attributes, alg );
4171 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004172
Gilles Peskine049c7532019-05-15 20:22:09 +02004173 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4174 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004175
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004176 actual_status = psa_verify_hash( handle, alg,
4177 hash_data->x, hash_data->len,
4178 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004179 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004180
Gilles Peskine895242b2019-11-29 12:15:40 +01004181#if defined(MBEDTLS_TEST_DEPRECATED)
4182 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
4183 hash_data->x, hash_data->len,
4184 signature_data->x, signature_data->len ),
4185 expected_status );
4186#endif /* MBEDTLS_TEST_DEPRECATED */
4187
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004188exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004189 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004190 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004191 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004192}
4193/* END_CASE */
4194
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004195/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004196void asymmetric_encrypt( int key_type_arg,
4197 data_t *key_data,
4198 int alg_arg,
4199 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004200 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004201 int expected_output_length_arg,
4202 int expected_status_arg )
4203{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004204 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004205 psa_key_type_t key_type = key_type_arg;
4206 psa_algorithm_t alg = alg_arg;
4207 size_t expected_output_length = expected_output_length_arg;
4208 size_t key_bits;
4209 unsigned char *output = NULL;
4210 size_t output_size;
4211 size_t output_length = ~0;
4212 psa_status_t actual_status;
4213 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004215
Gilles Peskine8817f612018-12-18 00:18:46 +01004216 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004217
Gilles Peskine656896e2018-06-29 19:12:28 +02004218 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4220 psa_set_key_algorithm( &attributes, alg );
4221 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004222 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4223 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004224
4225 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004226 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4227 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004228 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004229 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004230
4231 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004232 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004233 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004234 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004235 output, output_size,
4236 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004237 TEST_EQUAL( actual_status, expected_status );
4238 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004239
Gilles Peskine68428122018-06-30 18:42:41 +02004240 /* If the label is empty, the test framework puts a non-null pointer
4241 * in label->x. Test that a null pointer works as well. */
4242 if( label->len == 0 )
4243 {
4244 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004245 if( output_size != 0 )
4246 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004247 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004248 input_data->x, input_data->len,
4249 NULL, label->len,
4250 output, output_size,
4251 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004252 TEST_EQUAL( actual_status, expected_status );
4253 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004254 }
4255
Gilles Peskine656896e2018-06-29 19:12:28 +02004256exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004257 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004258 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004259 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004260 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004261}
4262/* END_CASE */
4263
4264/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004265void asymmetric_encrypt_decrypt( int key_type_arg,
4266 data_t *key_data,
4267 int alg_arg,
4268 data_t *input_data,
4269 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004270{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004271 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004272 psa_key_type_t key_type = key_type_arg;
4273 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004274 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004275 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004276 size_t output_size;
4277 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004278 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004279 size_t output2_size;
4280 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004282
Gilles Peskine8817f612018-12-18 00:18:46 +01004283 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004284
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004285 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4286 psa_set_key_algorithm( &attributes, alg );
4287 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004288
Gilles Peskine049c7532019-05-15 20:22:09 +02004289 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4290 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004291
4292 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004293 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4294 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004295 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004296 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004297 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004298 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004299
Gilles Peskineeebd7382018-06-08 18:11:54 +02004300 /* We test encryption by checking that encrypt-then-decrypt gives back
4301 * the original plaintext because of the non-optional random
4302 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004303 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4304 input_data->x, input_data->len,
4305 label->x, label->len,
4306 output, output_size,
4307 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004308 /* We don't know what ciphertext length to expect, but check that
4309 * it looks sensible. */
4310 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004311
Gilles Peskine8817f612018-12-18 00:18:46 +01004312 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4313 output, output_length,
4314 label->x, label->len,
4315 output2, output2_size,
4316 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004317 ASSERT_COMPARE( input_data->x, input_data->len,
4318 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004319
4320exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004321 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004322 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004323 mbedtls_free( output );
4324 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004325 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004326}
4327/* END_CASE */
4328
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004329/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004330void asymmetric_decrypt( int key_type_arg,
4331 data_t *key_data,
4332 int alg_arg,
4333 data_t *input_data,
4334 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004335 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004336{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004337 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004338 psa_key_type_t key_type = key_type_arg;
4339 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004341 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004342 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004344
Jaeden Amero412654a2019-02-06 12:57:46 +00004345 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004346 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004347
Gilles Peskine8817f612018-12-18 00:18:46 +01004348 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004349
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004350 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4351 psa_set_key_algorithm( &attributes, alg );
4352 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004353
Gilles Peskine049c7532019-05-15 20:22:09 +02004354 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4355 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004356
Gilles Peskine8817f612018-12-18 00:18:46 +01004357 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4358 input_data->x, input_data->len,
4359 label->x, label->len,
4360 output,
4361 output_size,
4362 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004363 ASSERT_COMPARE( expected_data->x, expected_data->len,
4364 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004365
Gilles Peskine68428122018-06-30 18:42:41 +02004366 /* If the label is empty, the test framework puts a non-null pointer
4367 * in label->x. Test that a null pointer works as well. */
4368 if( label->len == 0 )
4369 {
4370 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004371 if( output_size != 0 )
4372 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004373 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4374 input_data->x, input_data->len,
4375 NULL, label->len,
4376 output,
4377 output_size,
4378 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004379 ASSERT_COMPARE( expected_data->x, expected_data->len,
4380 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004381 }
4382
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004383exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004384 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004385 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004386 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004387 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004388}
4389/* END_CASE */
4390
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004391/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004392void asymmetric_decrypt_fail( int key_type_arg,
4393 data_t *key_data,
4394 int alg_arg,
4395 data_t *input_data,
4396 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004397 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004398 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004399{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004400 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004401 psa_key_type_t key_type = key_type_arg;
4402 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004403 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004404 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004405 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004406 psa_status_t actual_status;
4407 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004409
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004410 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004411
Gilles Peskine8817f612018-12-18 00:18:46 +01004412 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004413
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004414 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4415 psa_set_key_algorithm( &attributes, alg );
4416 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004417
Gilles Peskine049c7532019-05-15 20:22:09 +02004418 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4419 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004420
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004421 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004422 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004423 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004424 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004425 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004426 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004427 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004428
Gilles Peskine68428122018-06-30 18:42:41 +02004429 /* If the label is empty, the test framework puts a non-null pointer
4430 * in label->x. Test that a null pointer works as well. */
4431 if( label->len == 0 )
4432 {
4433 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004434 if( output_size != 0 )
4435 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004436 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004437 input_data->x, input_data->len,
4438 NULL, label->len,
4439 output, output_size,
4440 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004441 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004442 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004443 }
4444
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004445exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004446 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004447 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004448 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004449 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004450}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004451/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004452
4453/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004454void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004455{
4456 /* Test each valid way of initializing the object, except for `= {0}`, as
4457 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4458 * though it's OK by the C standard. We could test for this, but we'd need
4459 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004460 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004461 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4462 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4463 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004464
4465 memset( &zero, 0, sizeof( zero ) );
4466
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004467 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004468 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004469 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004470 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004471 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004472 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004473 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004474
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004475 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004476 PSA_ASSERT( psa_key_derivation_abort(&func) );
4477 PSA_ASSERT( psa_key_derivation_abort(&init) );
4478 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004479}
4480/* END_CASE */
4481
Janos Follath16de4a42019-06-13 16:32:24 +01004482/* BEGIN_CASE */
4483void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004484{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004485 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004486 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004487 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004488
Gilles Peskine8817f612018-12-18 00:18:46 +01004489 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004490
Janos Follath16de4a42019-06-13 16:32:24 +01004491 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004492 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004493
4494exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004496 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004497}
4498/* END_CASE */
4499
Janos Follathaf3c2a02019-06-12 12:34:34 +01004500/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004501void derive_set_capacity( int alg_arg, int capacity_arg,
4502 int expected_status_arg )
4503{
4504 psa_algorithm_t alg = alg_arg;
4505 size_t capacity = capacity_arg;
4506 psa_status_t expected_status = expected_status_arg;
4507 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4508
4509 PSA_ASSERT( psa_crypto_init( ) );
4510
4511 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4512
4513 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4514 expected_status );
4515
4516exit:
4517 psa_key_derivation_abort( &operation );
4518 PSA_DONE( );
4519}
4520/* END_CASE */
4521
4522/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004523void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004524 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004525 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004526 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004527 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004528 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004529 int expected_status_arg3,
4530 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004531{
4532 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004533 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4534 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004535 psa_status_t expected_statuses[] = {expected_status_arg1,
4536 expected_status_arg2,
4537 expected_status_arg3};
4538 data_t *inputs[] = {input1, input2, input3};
4539 psa_key_handle_t handles[] = {0, 0, 0};
4540 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4541 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4542 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004543 psa_key_type_t output_key_type = output_key_type_arg;
4544 psa_key_handle_t output_handle = 0;
4545 psa_status_t expected_output_status = expected_output_status_arg;
4546 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004547
4548 PSA_ASSERT( psa_crypto_init( ) );
4549
4550 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4551 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004552
4553 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4554
4555 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4556 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004557 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004558 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004559 psa_set_key_type( &attributes, key_types[i] );
4560 PSA_ASSERT( psa_import_key( &attributes,
4561 inputs[i]->x, inputs[i]->len,
4562 &handles[i] ) );
4563 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4564 handles[i] ),
4565 expected_statuses[i] );
4566 }
4567 else
4568 {
4569 TEST_EQUAL( psa_key_derivation_input_bytes(
4570 &operation, steps[i],
4571 inputs[i]->x, inputs[i]->len ),
4572 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004573 }
4574 }
4575
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004576 if( output_key_type != PSA_KEY_TYPE_NONE )
4577 {
4578 psa_reset_key_attributes( &attributes );
4579 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4580 psa_set_key_bits( &attributes, 8 );
4581 actual_output_status =
4582 psa_key_derivation_output_key( &attributes, &operation,
4583 &output_handle );
4584 }
4585 else
4586 {
4587 uint8_t buffer[1];
4588 actual_output_status =
4589 psa_key_derivation_output_bytes( &operation,
4590 buffer, sizeof( buffer ) );
4591 }
4592 TEST_EQUAL( actual_output_status, expected_output_status );
4593
Janos Follathaf3c2a02019-06-12 12:34:34 +01004594exit:
4595 psa_key_derivation_abort( &operation );
4596 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4597 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004598 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004599 PSA_DONE( );
4600}
4601/* END_CASE */
4602
Janos Follathd958bb72019-07-03 15:02:16 +01004603/* BEGIN_CASE */
4604void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004605{
Janos Follathd958bb72019-07-03 15:02:16 +01004606 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004607 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004608 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004609 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004610 unsigned char input1[] = "Input 1";
4611 size_t input1_length = sizeof( input1 );
4612 unsigned char input2[] = "Input 2";
4613 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004614 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004615 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004616 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4617 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4618 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004620
Gilles Peskine8817f612018-12-18 00:18:46 +01004621 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004622
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004623 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4624 psa_set_key_algorithm( &attributes, alg );
4625 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004626
Gilles Peskine73676cb2019-05-15 20:15:10 +02004627 PSA_ASSERT( psa_import_key( &attributes,
4628 key_data, sizeof( key_data ),
4629 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004630
4631 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004632 if( !setup_key_derivation_wrap( &operation, handle, alg,
4633 input1, input1_length,
4634 input2, input2_length,
4635 capacity ) )
4636 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004637
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004638 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004639 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004640 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004641
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004642 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004643
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004645 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004646
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004647exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004649 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004650 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004651}
4652/* END_CASE */
4653
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004654/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004655void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004656{
4657 uint8_t output_buffer[16];
4658 size_t buffer_size = 16;
4659 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004660 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004661
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004662 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4663 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004664 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004665
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004667 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004668
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004669 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004670
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004671 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4672 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004673 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004674
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004676 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004677
4678exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004679 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004680}
4681/* END_CASE */
4682
4683/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004684void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004685 int step1_arg, data_t *input1,
4686 int step2_arg, data_t *input2,
4687 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004688 int requested_capacity_arg,
4689 data_t *expected_output1,
4690 data_t *expected_output2 )
4691{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004692 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004693 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4694 data_t *inputs[] = {input1, input2, input3};
4695 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004696 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004698 uint8_t *expected_outputs[2] =
4699 {expected_output1->x, expected_output2->x};
4700 size_t output_sizes[2] =
4701 {expected_output1->len, expected_output2->len};
4702 size_t output_buffer_size = 0;
4703 uint8_t *output_buffer = NULL;
4704 size_t expected_capacity;
4705 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004706 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004707 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004708 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004709
4710 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4711 {
4712 if( output_sizes[i] > output_buffer_size )
4713 output_buffer_size = output_sizes[i];
4714 if( output_sizes[i] == 0 )
4715 expected_outputs[i] = NULL;
4716 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004717 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004719
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004720 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4721 psa_set_key_algorithm( &attributes, alg );
4722 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004723
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004724 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004725 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4726 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4727 requested_capacity ) );
4728 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004729 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004730 switch( steps[i] )
4731 {
4732 case 0:
4733 break;
4734 case PSA_KEY_DERIVATION_INPUT_SECRET:
4735 PSA_ASSERT( psa_import_key( &attributes,
4736 inputs[i]->x, inputs[i]->len,
4737 &handles[i] ) );
4738 PSA_ASSERT( psa_key_derivation_input_key(
4739 &operation, steps[i],
4740 handles[i] ) );
4741 break;
4742 default:
4743 PSA_ASSERT( psa_key_derivation_input_bytes(
4744 &operation, steps[i],
4745 inputs[i]->x, inputs[i]->len ) );
4746 break;
4747 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004748 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004749
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004751 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004752 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004753 expected_capacity = requested_capacity;
4754
4755 /* Expansion phase. */
4756 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4757 {
4758 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004760 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004761 if( expected_capacity == 0 && output_sizes[i] == 0 )
4762 {
4763 /* Reading 0 bytes when 0 bytes are available can go either way. */
4764 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004765 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004766 continue;
4767 }
4768 else if( expected_capacity == 0 ||
4769 output_sizes[i] > expected_capacity )
4770 {
4771 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004772 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004773 expected_capacity = 0;
4774 continue;
4775 }
4776 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004777 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004778 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004779 ASSERT_COMPARE( output_buffer, output_sizes[i],
4780 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004781 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004782 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004783 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004784 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004785 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004786 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004787 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004788
4789exit:
4790 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004791 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004792 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4793 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004794 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004795}
4796/* END_CASE */
4797
4798/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004799void derive_full( int alg_arg,
4800 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004801 data_t *input1,
4802 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004803 int requested_capacity_arg )
4804{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004805 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004806 psa_algorithm_t alg = alg_arg;
4807 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004809 unsigned char output_buffer[16];
4810 size_t expected_capacity = requested_capacity;
4811 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004812 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004813
Gilles Peskine8817f612018-12-18 00:18:46 +01004814 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004815
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004816 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4817 psa_set_key_algorithm( &attributes, alg );
4818 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004819
Gilles Peskine049c7532019-05-15 20:22:09 +02004820 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4821 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004822
Janos Follathf2815ea2019-07-03 12:41:36 +01004823 if( !setup_key_derivation_wrap( &operation, handle, alg,
4824 input1->x, input1->len,
4825 input2->x, input2->len,
4826 requested_capacity ) )
4827 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004828
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004830 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004831 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004832
4833 /* Expansion phase. */
4834 while( current_capacity > 0 )
4835 {
4836 size_t read_size = sizeof( output_buffer );
4837 if( read_size > current_capacity )
4838 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004840 output_buffer,
4841 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004842 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004844 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004845 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004846 }
4847
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 /* Check that the operation refuses to go over capacity. */
4849 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004850 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004851
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004853
4854exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004855 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004856 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004857 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004858}
4859/* END_CASE */
4860
Janos Follathe60c9052019-07-03 13:51:30 +01004861/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004862void derive_key_exercise( int alg_arg,
4863 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004864 data_t *input1,
4865 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004866 int derived_type_arg,
4867 int derived_bits_arg,
4868 int derived_usage_arg,
4869 int derived_alg_arg )
4870{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004871 psa_key_handle_t base_handle = 0;
4872 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004873 psa_algorithm_t alg = alg_arg;
4874 psa_key_type_t derived_type = derived_type_arg;
4875 size_t derived_bits = derived_bits_arg;
4876 psa_key_usage_t derived_usage = derived_usage_arg;
4877 psa_algorithm_t derived_alg = derived_alg_arg;
4878 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004879 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004881 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004882
Gilles Peskine8817f612018-12-18 00:18:46 +01004883 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004884
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004885 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4886 psa_set_key_algorithm( &attributes, alg );
4887 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4889 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004890
4891 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004892 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4893 input1->x, input1->len,
4894 input2->x, input2->len, capacity ) )
4895 goto exit;
4896
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004897 psa_set_key_usage_flags( &attributes, derived_usage );
4898 psa_set_key_algorithm( &attributes, derived_alg );
4899 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004900 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004902 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004903
4904 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004905 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4906 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4907 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004908
4909 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004910 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004911 goto exit;
4912
4913exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004915 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004916 psa_destroy_key( base_handle );
4917 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004918 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004919}
4920/* END_CASE */
4921
Janos Follath42fd8882019-07-03 14:17:09 +01004922/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004923void derive_key_export( int alg_arg,
4924 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004925 data_t *input1,
4926 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004927 int bytes1_arg,
4928 int bytes2_arg )
4929{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004930 psa_key_handle_t base_handle = 0;
4931 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004932 psa_algorithm_t alg = alg_arg;
4933 size_t bytes1 = bytes1_arg;
4934 size_t bytes2 = bytes2_arg;
4935 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004937 uint8_t *output_buffer = NULL;
4938 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004939 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4940 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004941 size_t length;
4942
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004943 ASSERT_ALLOC( output_buffer, capacity );
4944 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004945 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004946
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004947 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4948 psa_set_key_algorithm( &base_attributes, alg );
4949 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004950 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4951 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004952
4953 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01004954 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4955 input1->x, input1->len,
4956 input2->x, input2->len, capacity ) )
4957 goto exit;
4958
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004959 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004960 output_buffer,
4961 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004963
4964 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01004965 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4966 input1->x, input1->len,
4967 input2->x, input2->len, capacity ) )
4968 goto exit;
4969
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004970 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4971 psa_set_key_algorithm( &derived_attributes, 0 );
4972 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004973 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004974 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004975 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004976 PSA_ASSERT( psa_export_key( derived_handle,
4977 export_buffer, bytes1,
4978 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004979 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004980 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004981 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004983 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004984 PSA_ASSERT( psa_export_key( derived_handle,
4985 export_buffer + bytes1, bytes2,
4986 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004987 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004988
4989 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004990 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4991 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004992
4993exit:
4994 mbedtls_free( output_buffer );
4995 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004996 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004997 psa_destroy_key( base_handle );
4998 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004999 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005000}
5001/* END_CASE */
5002
5003/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005004void derive_key( int alg_arg,
5005 data_t *key_data, data_t *input1, data_t *input2,
5006 int type_arg, int bits_arg,
5007 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005008{
5009 psa_key_handle_t base_handle = 0;
5010 psa_key_handle_t derived_handle = 0;
5011 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005012 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005013 size_t bits = bits_arg;
5014 psa_status_t expected_status = expected_status_arg;
5015 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5016 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5017 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5018
5019 PSA_ASSERT( psa_crypto_init( ) );
5020
5021 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5022 psa_set_key_algorithm( &base_attributes, alg );
5023 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5024 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5025 &base_handle ) );
5026
5027 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5028 input1->x, input1->len,
5029 input2->x, input2->len, SIZE_MAX ) )
5030 goto exit;
5031
5032 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5033 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005034 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005035 psa_set_key_bits( &derived_attributes, bits );
5036 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
5037 &derived_handle ),
5038 expected_status );
5039
5040exit:
5041 psa_key_derivation_abort( &operation );
5042 psa_destroy_key( base_handle );
5043 psa_destroy_key( derived_handle );
5044 PSA_DONE( );
5045}
5046/* END_CASE */
5047
5048/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005049void key_agreement_setup( int alg_arg,
5050 int our_key_type_arg, data_t *our_key_data,
5051 data_t *peer_key_data,
5052 int expected_status_arg )
5053{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005054 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005055 psa_algorithm_t alg = alg_arg;
5056 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005057 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005058 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005059 psa_status_t expected_status = expected_status_arg;
5060 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005061
Gilles Peskine8817f612018-12-18 00:18:46 +01005062 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005063
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005064 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5065 psa_set_key_algorithm( &attributes, alg );
5066 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005067 PSA_ASSERT( psa_import_key( &attributes,
5068 our_key_data->x, our_key_data->len,
5069 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005070
Gilles Peskine77f40d82019-04-11 21:27:06 +02005071 /* The tests currently include inputs that should fail at either step.
5072 * Test cases that fail at the setup step should be changed to call
5073 * key_derivation_setup instead, and this function should be renamed
5074 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005075 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005076 if( status == PSA_SUCCESS )
5077 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005078 TEST_EQUAL( psa_key_derivation_key_agreement(
5079 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5080 our_key,
5081 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005082 expected_status );
5083 }
5084 else
5085 {
5086 TEST_ASSERT( status == expected_status );
5087 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005088
5089exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005090 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005091 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005092 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005093}
5094/* END_CASE */
5095
5096/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005097void raw_key_agreement( int alg_arg,
5098 int our_key_type_arg, data_t *our_key_data,
5099 data_t *peer_key_data,
5100 data_t *expected_output )
5101{
5102 psa_key_handle_t our_key = 0;
5103 psa_algorithm_t alg = alg_arg;
5104 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005106 unsigned char *output = NULL;
5107 size_t output_length = ~0;
5108
5109 ASSERT_ALLOC( output, expected_output->len );
5110 PSA_ASSERT( psa_crypto_init( ) );
5111
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005112 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5113 psa_set_key_algorithm( &attributes, alg );
5114 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005115 PSA_ASSERT( psa_import_key( &attributes,
5116 our_key_data->x, our_key_data->len,
5117 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005118
Gilles Peskinebe697d82019-05-16 18:00:41 +02005119 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5120 peer_key_data->x, peer_key_data->len,
5121 output, expected_output->len,
5122 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005123 ASSERT_COMPARE( output, output_length,
5124 expected_output->x, expected_output->len );
5125
5126exit:
5127 mbedtls_free( output );
5128 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005129 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005130}
5131/* END_CASE */
5132
5133/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005134void key_agreement_capacity( int alg_arg,
5135 int our_key_type_arg, data_t *our_key_data,
5136 data_t *peer_key_data,
5137 int expected_capacity_arg )
5138{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005139 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005140 psa_algorithm_t alg = alg_arg;
5141 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005142 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005144 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005145 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005146
Gilles Peskine8817f612018-12-18 00:18:46 +01005147 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005148
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005149 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5150 psa_set_key_algorithm( &attributes, alg );
5151 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005152 PSA_ASSERT( psa_import_key( &attributes,
5153 our_key_data->x, our_key_data->len,
5154 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005155
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005156 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005157 PSA_ASSERT( psa_key_derivation_key_agreement(
5158 &operation,
5159 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5160 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005161 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5162 {
5163 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005165 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005166 NULL, 0 ) );
5167 }
Gilles Peskine59685592018-09-18 12:11:34 +02005168
Gilles Peskinebf491972018-10-25 22:36:12 +02005169 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005170 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005171 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005172 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005173
Gilles Peskinebf491972018-10-25 22:36:12 +02005174 /* Test the actual capacity by reading the output. */
5175 while( actual_capacity > sizeof( output ) )
5176 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005177 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005178 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005179 actual_capacity -= sizeof( output );
5180 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005181 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005182 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005183 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005184 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005185
Gilles Peskine59685592018-09-18 12:11:34 +02005186exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005187 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005188 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005189 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005190}
5191/* END_CASE */
5192
5193/* BEGIN_CASE */
5194void key_agreement_output( int alg_arg,
5195 int our_key_type_arg, data_t *our_key_data,
5196 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005197 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005198{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005199 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005200 psa_algorithm_t alg = alg_arg;
5201 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005204 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005205
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005206 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5207 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005208
Gilles Peskine8817f612018-12-18 00:18:46 +01005209 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005210
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005211 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5212 psa_set_key_algorithm( &attributes, alg );
5213 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005214 PSA_ASSERT( psa_import_key( &attributes,
5215 our_key_data->x, our_key_data->len,
5216 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005217
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005219 PSA_ASSERT( psa_key_derivation_key_agreement(
5220 &operation,
5221 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5222 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005223 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5224 {
5225 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005227 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005228 NULL, 0 ) );
5229 }
Gilles Peskine59685592018-09-18 12:11:34 +02005230
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005231 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005232 actual_output,
5233 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005234 ASSERT_COMPARE( actual_output, expected_output1->len,
5235 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005236 if( expected_output2->len != 0 )
5237 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005238 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005239 actual_output,
5240 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005241 ASSERT_COMPARE( actual_output, expected_output2->len,
5242 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005243 }
Gilles Peskine59685592018-09-18 12:11:34 +02005244
5245exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005246 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005247 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005248 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005249 mbedtls_free( actual_output );
5250}
5251/* END_CASE */
5252
5253/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005254void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005255{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005256 size_t bytes = bytes_arg;
5257 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005258 unsigned char *output = NULL;
5259 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005260 size_t i;
5261 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005262
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005263 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5264 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005265 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005266
Gilles Peskine8817f612018-12-18 00:18:46 +01005267 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005268
Gilles Peskinea50d7392018-06-21 10:22:13 +02005269 /* Run several times, to ensure that every output byte will be
5270 * nonzero at least once with overwhelming probability
5271 * (2^(-8*number_of_runs)). */
5272 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005273 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005274 if( bytes != 0 )
5275 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005276 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005277
5278 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005279 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5280 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005281
5282 for( i = 0; i < bytes; i++ )
5283 {
5284 if( output[i] != 0 )
5285 ++changed[i];
5286 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005287 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005288
5289 /* Check that every byte was changed to nonzero at least once. This
5290 * validates that psa_generate_random is overwriting every byte of
5291 * the output buffer. */
5292 for( i = 0; i < bytes; i++ )
5293 {
5294 TEST_ASSERT( changed[i] != 0 );
5295 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005296
5297exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005298 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005299 mbedtls_free( output );
5300 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005301}
5302/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005303
5304/* BEGIN_CASE */
5305void generate_key( int type_arg,
5306 int bits_arg,
5307 int usage_arg,
5308 int alg_arg,
5309 int expected_status_arg )
5310{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005311 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005312 psa_key_type_t type = type_arg;
5313 psa_key_usage_t usage = usage_arg;
5314 size_t bits = bits_arg;
5315 psa_algorithm_t alg = alg_arg;
5316 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005318 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005319
Gilles Peskine8817f612018-12-18 00:18:46 +01005320 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005321
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005322 psa_set_key_usage_flags( &attributes, usage );
5323 psa_set_key_algorithm( &attributes, alg );
5324 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005325 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005326
5327 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005328 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005329 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005330 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005331
5332 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005333 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5334 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5335 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005336
Gilles Peskine818ca122018-06-20 18:16:48 +02005337 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005338 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005339 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005340
5341exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005342 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005343 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005344 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005345}
5346/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005347
Gilles Peskinee56e8782019-04-26 17:34:02 +02005348/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5349void generate_key_rsa( int bits_arg,
5350 data_t *e_arg,
5351 int expected_status_arg )
5352{
5353 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005354 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005355 size_t bits = bits_arg;
5356 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5357 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5358 psa_status_t expected_status = expected_status_arg;
5359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5360 uint8_t *exported = NULL;
5361 size_t exported_size =
5362 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5363 size_t exported_length = SIZE_MAX;
5364 uint8_t *e_read_buffer = NULL;
5365 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005366 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005367 size_t e_read_length = SIZE_MAX;
5368
5369 if( e_arg->len == 0 ||
5370 ( e_arg->len == 3 &&
5371 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5372 {
5373 is_default_public_exponent = 1;
5374 e_read_size = 0;
5375 }
5376 ASSERT_ALLOC( e_read_buffer, e_read_size );
5377 ASSERT_ALLOC( exported, exported_size );
5378
5379 PSA_ASSERT( psa_crypto_init( ) );
5380
5381 psa_set_key_usage_flags( &attributes, usage );
5382 psa_set_key_algorithm( &attributes, alg );
5383 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5384 e_arg->x, e_arg->len ) );
5385 psa_set_key_bits( &attributes, bits );
5386
5387 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005388 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005389 if( expected_status != PSA_SUCCESS )
5390 goto exit;
5391
5392 /* Test the key information */
5393 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5394 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5395 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5396 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5397 e_read_buffer, e_read_size,
5398 &e_read_length ) );
5399 if( is_default_public_exponent )
5400 TEST_EQUAL( e_read_length, 0 );
5401 else
5402 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5403
5404 /* Do something with the key according to its type and permitted usage. */
5405 if( ! exercise_key( handle, usage, alg ) )
5406 goto exit;
5407
5408 /* Export the key and check the public exponent. */
5409 PSA_ASSERT( psa_export_public_key( handle,
5410 exported, exported_size,
5411 &exported_length ) );
5412 {
5413 uint8_t *p = exported;
5414 uint8_t *end = exported + exported_length;
5415 size_t len;
5416 /* RSAPublicKey ::= SEQUENCE {
5417 * modulus INTEGER, -- n
5418 * publicExponent INTEGER } -- e
5419 */
5420 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005421 MBEDTLS_ASN1_SEQUENCE |
5422 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005423 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5424 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5425 MBEDTLS_ASN1_INTEGER ) );
5426 if( len >= 1 && p[0] == 0 )
5427 {
5428 ++p;
5429 --len;
5430 }
5431 if( e_arg->len == 0 )
5432 {
5433 TEST_EQUAL( len, 3 );
5434 TEST_EQUAL( p[0], 1 );
5435 TEST_EQUAL( p[1], 0 );
5436 TEST_EQUAL( p[2], 1 );
5437 }
5438 else
5439 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5440 }
5441
5442exit:
5443 psa_reset_key_attributes( &attributes );
5444 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005445 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005446 mbedtls_free( e_read_buffer );
5447 mbedtls_free( exported );
5448}
5449/* END_CASE */
5450
Darryl Greend49a4992018-06-18 17:27:26 +01005451/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005452void persistent_key_load_key_from_storage( data_t *data,
5453 int type_arg, int bits_arg,
5454 int usage_flags_arg, int alg_arg,
5455 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005456{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005457 psa_key_id_t key_id = 1;
5458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005459 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005460 psa_key_handle_t base_key = 0;
5461 psa_key_type_t type = type_arg;
5462 size_t bits = bits_arg;
5463 psa_key_usage_t usage_flags = usage_flags_arg;
5464 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005465 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005466 unsigned char *first_export = NULL;
5467 unsigned char *second_export = NULL;
5468 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5469 size_t first_exported_length;
5470 size_t second_exported_length;
5471
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005472 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5473 {
5474 ASSERT_ALLOC( first_export, export_size );
5475 ASSERT_ALLOC( second_export, export_size );
5476 }
Darryl Greend49a4992018-06-18 17:27:26 +01005477
Gilles Peskine8817f612018-12-18 00:18:46 +01005478 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005479
Gilles Peskinec87af662019-05-15 16:12:22 +02005480 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005481 psa_set_key_usage_flags( &attributes, usage_flags );
5482 psa_set_key_algorithm( &attributes, alg );
5483 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005484 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005485
Darryl Green0c6575a2018-11-07 16:05:30 +00005486 switch( generation_method )
5487 {
5488 case IMPORT_KEY:
5489 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005490 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5491 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005492 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005493
Darryl Green0c6575a2018-11-07 16:05:30 +00005494 case GENERATE_KEY:
5495 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005496 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005497 break;
5498
5499 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005500 {
5501 /* Create base key */
5502 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5503 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5504 psa_set_key_usage_flags( &base_attributes,
5505 PSA_KEY_USAGE_DERIVE );
5506 psa_set_key_algorithm( &base_attributes, derive_alg );
5507 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005508 PSA_ASSERT( psa_import_key( &base_attributes,
5509 data->x, data->len,
5510 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005511 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005512 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005513 PSA_ASSERT( psa_key_derivation_input_key(
5514 &operation,
5515 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005516 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005518 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005519 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5520 &operation,
5521 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005523 PSA_ASSERT( psa_destroy_key( base_key ) );
5524 base_key = 0;
5525 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005526 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005527 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005528 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005529
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005530 /* Export the key if permitted by the key policy. */
5531 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5532 {
5533 PSA_ASSERT( psa_export_key( handle,
5534 first_export, export_size,
5535 &first_exported_length ) );
5536 if( generation_method == IMPORT_KEY )
5537 ASSERT_COMPARE( data->x, data->len,
5538 first_export, first_exported_length );
5539 }
Darryl Greend49a4992018-06-18 17:27:26 +01005540
5541 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005542 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005543 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005544 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005545
Darryl Greend49a4992018-06-18 17:27:26 +01005546 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005547 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005548 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5549 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5550 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5551 PSA_KEY_LIFETIME_PERSISTENT );
5552 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5553 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5554 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5555 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005556
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005557 /* Export the key again if permitted by the key policy. */
5558 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005559 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005560 PSA_ASSERT( psa_export_key( handle,
5561 second_export, export_size,
5562 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005563 ASSERT_COMPARE( first_export, first_exported_length,
5564 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005565 }
5566
5567 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005568 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005569 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005570
5571exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005572 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005573 mbedtls_free( first_export );
5574 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005575 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005576 psa_destroy_key( base_key );
5577 if( handle == 0 )
5578 {
5579 /* In case there was a test failure after creating the persistent key
5580 * but while it was not open, try to re-open the persistent key
5581 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005582 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005583 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005584 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005585 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005586}
5587/* END_CASE */