blob: ba7c192b8b49992c587889554d665a31716fa989 [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 */
itayzafrirf86548d2018-11-01 10:44:32 +02002434void hash_bad_order( )
2435{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002436 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002437 unsigned char input[] = "";
2438 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002439 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002440 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2441 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2442 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002443 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002444 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002445 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002446
Gilles Peskine8817f612018-12-18 00:18:46 +01002447 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002448
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002449 /* Call setup twice in a row. */
2450 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2451 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2452 PSA_ERROR_BAD_STATE );
2453 PSA_ASSERT( psa_hash_abort( &operation ) );
2454
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002455 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002456 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002457 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002458 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002459
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002460 /* Call update after finish. */
2461 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2462 PSA_ASSERT( psa_hash_finish( &operation,
2463 hash, sizeof( hash ), &hash_len ) );
2464 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002465 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002466 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002467
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002468 /* Call verify without calling setup beforehand. */
2469 TEST_EQUAL( psa_hash_verify( &operation,
2470 valid_hash, sizeof( valid_hash ) ),
2471 PSA_ERROR_BAD_STATE );
2472 PSA_ASSERT( psa_hash_abort( &operation ) );
2473
2474 /* Call verify after finish. */
2475 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2476 PSA_ASSERT( psa_hash_finish( &operation,
2477 hash, sizeof( hash ), &hash_len ) );
2478 TEST_EQUAL( psa_hash_verify( &operation,
2479 valid_hash, sizeof( valid_hash ) ),
2480 PSA_ERROR_BAD_STATE );
2481 PSA_ASSERT( psa_hash_abort( &operation ) );
2482
2483 /* Call verify twice in a row. */
2484 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2485 PSA_ASSERT( psa_hash_verify( &operation,
2486 valid_hash, sizeof( valid_hash ) ) );
2487 TEST_EQUAL( psa_hash_verify( &operation,
2488 valid_hash, sizeof( valid_hash ) ),
2489 PSA_ERROR_BAD_STATE );
2490 PSA_ASSERT( psa_hash_abort( &operation ) );
2491
2492 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002493 TEST_EQUAL( psa_hash_finish( &operation,
2494 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002495 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002496 PSA_ASSERT( psa_hash_abort( &operation ) );
2497
2498 /* Call finish twice in a row. */
2499 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2500 PSA_ASSERT( psa_hash_finish( &operation,
2501 hash, sizeof( hash ), &hash_len ) );
2502 TEST_EQUAL( psa_hash_finish( &operation,
2503 hash, sizeof( hash ), &hash_len ),
2504 PSA_ERROR_BAD_STATE );
2505 PSA_ASSERT( psa_hash_abort( &operation ) );
2506
2507 /* Call finish after calling verify. */
2508 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2509 PSA_ASSERT( psa_hash_verify( &operation,
2510 valid_hash, sizeof( valid_hash ) ) );
2511 TEST_EQUAL( psa_hash_finish( &operation,
2512 hash, sizeof( hash ), &hash_len ),
2513 PSA_ERROR_BAD_STATE );
2514 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002515
2516exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002517 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002518}
2519/* END_CASE */
2520
itayzafrir27e69452018-11-01 14:26:34 +02002521/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2522void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002523{
2524 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002525 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2526 * appended to it */
2527 unsigned char hash[] = {
2528 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2529 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2530 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002531 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002532 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002533
Gilles Peskine8817f612018-12-18 00:18:46 +01002534 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002535
itayzafrir27e69452018-11-01 14:26:34 +02002536 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002537 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002538 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002539 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002540
itayzafrir27e69452018-11-01 14:26:34 +02002541 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002542 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002543 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002544 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002545
itayzafrir27e69452018-11-01 14:26:34 +02002546 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002547 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002548 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002549 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002550
itayzafrirec93d302018-10-18 18:01:10 +03002551exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002552 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002553}
2554/* END_CASE */
2555
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002556/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2557void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002558{
2559 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002560 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002561 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002562 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002563 size_t hash_len;
2564
Gilles Peskine8817f612018-12-18 00:18:46 +01002565 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002566
itayzafrir58028322018-10-25 10:22:01 +03002567 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002568 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002569 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002570 hash, expected_size - 1, &hash_len ),
2571 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002572
2573exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002574 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002575}
2576/* END_CASE */
2577
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002578/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2579void hash_clone_source_state( )
2580{
2581 psa_algorithm_t alg = PSA_ALG_SHA_256;
2582 unsigned char hash[PSA_HASH_MAX_SIZE];
2583 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2584 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2585 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2586 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2587 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2588 size_t hash_len;
2589
2590 PSA_ASSERT( psa_crypto_init( ) );
2591 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2592
2593 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2594 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2595 PSA_ASSERT( psa_hash_finish( &op_finished,
2596 hash, sizeof( hash ), &hash_len ) );
2597 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2598 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2599
2600 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2601 PSA_ERROR_BAD_STATE );
2602
2603 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2604 PSA_ASSERT( psa_hash_finish( &op_init,
2605 hash, sizeof( hash ), &hash_len ) );
2606 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2607 PSA_ASSERT( psa_hash_finish( &op_finished,
2608 hash, sizeof( hash ), &hash_len ) );
2609 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2610 PSA_ASSERT( psa_hash_finish( &op_aborted,
2611 hash, sizeof( hash ), &hash_len ) );
2612
2613exit:
2614 psa_hash_abort( &op_source );
2615 psa_hash_abort( &op_init );
2616 psa_hash_abort( &op_setup );
2617 psa_hash_abort( &op_finished );
2618 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002619 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002620}
2621/* END_CASE */
2622
2623/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2624void hash_clone_target_state( )
2625{
2626 psa_algorithm_t alg = PSA_ALG_SHA_256;
2627 unsigned char hash[PSA_HASH_MAX_SIZE];
2628 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2629 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2630 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2631 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2632 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2633 size_t hash_len;
2634
2635 PSA_ASSERT( psa_crypto_init( ) );
2636
2637 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2638 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2639 PSA_ASSERT( psa_hash_finish( &op_finished,
2640 hash, sizeof( hash ), &hash_len ) );
2641 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2642 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2643
2644 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2645 PSA_ASSERT( psa_hash_finish( &op_target,
2646 hash, sizeof( hash ), &hash_len ) );
2647
2648 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2649 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2650 PSA_ERROR_BAD_STATE );
2651 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2652 PSA_ERROR_BAD_STATE );
2653
2654exit:
2655 psa_hash_abort( &op_target );
2656 psa_hash_abort( &op_init );
2657 psa_hash_abort( &op_setup );
2658 psa_hash_abort( &op_finished );
2659 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002660 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002661}
2662/* END_CASE */
2663
itayzafrir58028322018-10-25 10:22:01 +03002664/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002665void mac_operation_init( )
2666{
Jaeden Amero252ef282019-02-15 14:05:35 +00002667 const uint8_t input[1] = { 0 };
2668
Jaeden Amero769ce272019-01-04 11:48:03 +00002669 /* Test each valid way of initializing the object, except for `= {0}`, as
2670 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2671 * though it's OK by the C standard. We could test for this, but we'd need
2672 * to supress the Clang warning for the test. */
2673 psa_mac_operation_t func = psa_mac_operation_init( );
2674 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2675 psa_mac_operation_t zero;
2676
2677 memset( &zero, 0, sizeof( zero ) );
2678
Jaeden Amero252ef282019-02-15 14:05:35 +00002679 /* A freshly-initialized MAC operation should not be usable. */
2680 TEST_EQUAL( psa_mac_update( &func,
2681 input, sizeof( input ) ),
2682 PSA_ERROR_BAD_STATE );
2683 TEST_EQUAL( psa_mac_update( &init,
2684 input, sizeof( input ) ),
2685 PSA_ERROR_BAD_STATE );
2686 TEST_EQUAL( psa_mac_update( &zero,
2687 input, sizeof( input ) ),
2688 PSA_ERROR_BAD_STATE );
2689
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002690 /* A default MAC operation should be abortable without error. */
2691 PSA_ASSERT( psa_mac_abort( &func ) );
2692 PSA_ASSERT( psa_mac_abort( &init ) );
2693 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002694}
2695/* END_CASE */
2696
2697/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002698void mac_setup( int key_type_arg,
2699 data_t *key,
2700 int alg_arg,
2701 int expected_status_arg )
2702{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002703 psa_key_type_t key_type = key_type_arg;
2704 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002705 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002706 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002707 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2708#if defined(KNOWN_SUPPORTED_MAC_ALG)
2709 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2710#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002711
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002713
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002714 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2715 &operation, &status ) )
2716 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002717 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002718
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002719 /* The operation object should be reusable. */
2720#if defined(KNOWN_SUPPORTED_MAC_ALG)
2721 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2722 smoke_test_key_data,
2723 sizeof( smoke_test_key_data ),
2724 KNOWN_SUPPORTED_MAC_ALG,
2725 &operation, &status ) )
2726 goto exit;
2727 TEST_EQUAL( status, PSA_SUCCESS );
2728#endif
2729
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002730exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002731 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002732}
2733/* END_CASE */
2734
2735/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002736void mac_bad_order( )
2737{
2738 psa_key_handle_t handle = 0;
2739 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2740 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2741 const uint8_t key[] = {
2742 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2743 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2744 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002746 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2747 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2748 size_t sign_mac_length = 0;
2749 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2750 const uint8_t verify_mac[] = {
2751 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2752 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2753 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2754
2755 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002756 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002757 psa_set_key_algorithm( &attributes, alg );
2758 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002759
Gilles Peskine73676cb2019-05-15 20:15:10 +02002760 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002761
Jaeden Amero252ef282019-02-15 14:05:35 +00002762 /* Call update without calling setup beforehand. */
2763 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2764 PSA_ERROR_BAD_STATE );
2765 PSA_ASSERT( psa_mac_abort( &operation ) );
2766
2767 /* Call sign finish without calling setup beforehand. */
2768 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2769 &sign_mac_length),
2770 PSA_ERROR_BAD_STATE );
2771 PSA_ASSERT( psa_mac_abort( &operation ) );
2772
2773 /* Call verify finish without calling setup beforehand. */
2774 TEST_EQUAL( psa_mac_verify_finish( &operation,
2775 verify_mac, sizeof( verify_mac ) ),
2776 PSA_ERROR_BAD_STATE );
2777 PSA_ASSERT( psa_mac_abort( &operation ) );
2778
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002779 /* Call setup twice in a row. */
2780 PSA_ASSERT( psa_mac_sign_setup( &operation,
2781 handle, alg ) );
2782 TEST_EQUAL( psa_mac_sign_setup( &operation,
2783 handle, alg ),
2784 PSA_ERROR_BAD_STATE );
2785 PSA_ASSERT( psa_mac_abort( &operation ) );
2786
Jaeden Amero252ef282019-02-15 14:05:35 +00002787 /* Call update after sign finish. */
2788 PSA_ASSERT( psa_mac_sign_setup( &operation,
2789 handle, alg ) );
2790 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2791 PSA_ASSERT( psa_mac_sign_finish( &operation,
2792 sign_mac, sizeof( sign_mac ),
2793 &sign_mac_length ) );
2794 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2795 PSA_ERROR_BAD_STATE );
2796 PSA_ASSERT( psa_mac_abort( &operation ) );
2797
2798 /* Call update after verify finish. */
2799 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002800 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002801 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2802 PSA_ASSERT( psa_mac_verify_finish( &operation,
2803 verify_mac, sizeof( verify_mac ) ) );
2804 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2805 PSA_ERROR_BAD_STATE );
2806 PSA_ASSERT( psa_mac_abort( &operation ) );
2807
2808 /* Call sign finish twice in a row. */
2809 PSA_ASSERT( psa_mac_sign_setup( &operation,
2810 handle, alg ) );
2811 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2812 PSA_ASSERT( psa_mac_sign_finish( &operation,
2813 sign_mac, sizeof( sign_mac ),
2814 &sign_mac_length ) );
2815 TEST_EQUAL( psa_mac_sign_finish( &operation,
2816 sign_mac, sizeof( sign_mac ),
2817 &sign_mac_length ),
2818 PSA_ERROR_BAD_STATE );
2819 PSA_ASSERT( psa_mac_abort( &operation ) );
2820
2821 /* Call verify finish twice in a row. */
2822 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002823 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002824 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2825 PSA_ASSERT( psa_mac_verify_finish( &operation,
2826 verify_mac, sizeof( verify_mac ) ) );
2827 TEST_EQUAL( psa_mac_verify_finish( &operation,
2828 verify_mac, sizeof( verify_mac ) ),
2829 PSA_ERROR_BAD_STATE );
2830 PSA_ASSERT( psa_mac_abort( &operation ) );
2831
2832 /* Setup sign but try verify. */
2833 PSA_ASSERT( psa_mac_sign_setup( &operation,
2834 handle, alg ) );
2835 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2836 TEST_EQUAL( psa_mac_verify_finish( &operation,
2837 verify_mac, sizeof( verify_mac ) ),
2838 PSA_ERROR_BAD_STATE );
2839 PSA_ASSERT( psa_mac_abort( &operation ) );
2840
2841 /* Setup verify but try sign. */
2842 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002843 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002844 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2845 TEST_EQUAL( psa_mac_sign_finish( &operation,
2846 sign_mac, sizeof( sign_mac ),
2847 &sign_mac_length ),
2848 PSA_ERROR_BAD_STATE );
2849 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002850
Gilles Peskine76b29a72019-05-28 14:08:50 +02002851 PSA_ASSERT( psa_destroy_key( handle ) );
2852
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002853exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002854 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002855}
2856/* END_CASE */
2857
2858/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002859void mac_sign( int key_type_arg,
2860 data_t *key,
2861 int alg_arg,
2862 data_t *input,
2863 data_t *expected_mac )
2864{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002865 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002866 psa_key_type_t key_type = key_type_arg;
2867 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002868 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002869 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002870 /* Leave a little extra room in the output buffer. At the end of the
2871 * test, we'll check that the implementation didn't overwrite onto
2872 * this extra room. */
2873 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2874 size_t mac_buffer_size =
2875 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2876 size_t mac_length = 0;
2877
2878 memset( actual_mac, '+', sizeof( actual_mac ) );
2879 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2880 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2881
Gilles Peskine8817f612018-12-18 00:18:46 +01002882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002883
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002884 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002885 psa_set_key_algorithm( &attributes, alg );
2886 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002887
Gilles Peskine73676cb2019-05-15 20:15:10 +02002888 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002889
2890 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002891 PSA_ASSERT( psa_mac_sign_setup( &operation,
2892 handle, alg ) );
2893 PSA_ASSERT( psa_mac_update( &operation,
2894 input->x, input->len ) );
2895 PSA_ASSERT( psa_mac_sign_finish( &operation,
2896 actual_mac, mac_buffer_size,
2897 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002898
2899 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002900 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2901 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002902
2903 /* Verify that the end of the buffer is untouched. */
2904 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2905 sizeof( actual_mac ) - mac_length ) );
2906
2907exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002908 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002909 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002910}
2911/* END_CASE */
2912
2913/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002914void mac_verify( int key_type_arg,
2915 data_t *key,
2916 int alg_arg,
2917 data_t *input,
2918 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002919{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002920 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921 psa_key_type_t key_type = key_type_arg;
2922 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002923 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002925
Gilles Peskine69c12672018-06-28 00:07:19 +02002926 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2927
Gilles Peskine8817f612018-12-18 00:18:46 +01002928 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002929
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002930 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002931 psa_set_key_algorithm( &attributes, alg );
2932 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002933
Gilles Peskine73676cb2019-05-15 20:15:10 +02002934 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002935
Gilles Peskine8817f612018-12-18 00:18:46 +01002936 PSA_ASSERT( psa_mac_verify_setup( &operation,
2937 handle, alg ) );
2938 PSA_ASSERT( psa_destroy_key( handle ) );
2939 PSA_ASSERT( psa_mac_update( &operation,
2940 input->x, input->len ) );
2941 PSA_ASSERT( psa_mac_verify_finish( &operation,
2942 expected_mac->x,
2943 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002944
2945exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002946 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002947 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002948}
2949/* END_CASE */
2950
2951/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002952void cipher_operation_init( )
2953{
Jaeden Ameroab439972019-02-15 14:12:05 +00002954 const uint8_t input[1] = { 0 };
2955 unsigned char output[1] = { 0 };
2956 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002957 /* Test each valid way of initializing the object, except for `= {0}`, as
2958 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2959 * though it's OK by the C standard. We could test for this, but we'd need
2960 * to supress the Clang warning for the test. */
2961 psa_cipher_operation_t func = psa_cipher_operation_init( );
2962 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2963 psa_cipher_operation_t zero;
2964
2965 memset( &zero, 0, sizeof( zero ) );
2966
Jaeden Ameroab439972019-02-15 14:12:05 +00002967 /* A freshly-initialized cipher operation should not be usable. */
2968 TEST_EQUAL( psa_cipher_update( &func,
2969 input, sizeof( input ),
2970 output, sizeof( output ),
2971 &output_length ),
2972 PSA_ERROR_BAD_STATE );
2973 TEST_EQUAL( psa_cipher_update( &init,
2974 input, sizeof( input ),
2975 output, sizeof( output ),
2976 &output_length ),
2977 PSA_ERROR_BAD_STATE );
2978 TEST_EQUAL( psa_cipher_update( &zero,
2979 input, sizeof( input ),
2980 output, sizeof( output ),
2981 &output_length ),
2982 PSA_ERROR_BAD_STATE );
2983
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002984 /* A default cipher operation should be abortable without error. */
2985 PSA_ASSERT( psa_cipher_abort( &func ) );
2986 PSA_ASSERT( psa_cipher_abort( &init ) );
2987 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002988}
2989/* END_CASE */
2990
2991/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002992void cipher_setup( int key_type_arg,
2993 data_t *key,
2994 int alg_arg,
2995 int expected_status_arg )
2996{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002997 psa_key_type_t key_type = key_type_arg;
2998 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002999 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003000 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003001 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003002#if defined(KNOWN_SUPPORTED_MAC_ALG)
3003 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3004#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003005
Gilles Peskine8817f612018-12-18 00:18:46 +01003006 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003007
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003008 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3009 &operation, &status ) )
3010 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003011 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003012
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003013 /* The operation object should be reusable. */
3014#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3015 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3016 smoke_test_key_data,
3017 sizeof( smoke_test_key_data ),
3018 KNOWN_SUPPORTED_CIPHER_ALG,
3019 &operation, &status ) )
3020 goto exit;
3021 TEST_EQUAL( status, PSA_SUCCESS );
3022#endif
3023
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003024exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003025 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003026}
3027/* END_CASE */
3028
3029/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003030void cipher_bad_order( )
3031{
3032 psa_key_handle_t handle = 0;
3033 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3034 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003036 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3037 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3038 const uint8_t key[] = {
3039 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3040 0xaa, 0xaa, 0xaa, 0xaa };
3041 const uint8_t text[] = {
3042 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3043 0xbb, 0xbb, 0xbb, 0xbb };
3044 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3045 size_t length = 0;
3046
3047 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003048 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3049 psa_set_key_algorithm( &attributes, alg );
3050 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003051 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003052
3053
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003054 /* Call encrypt setup twice in a row. */
3055 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3056 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
3057 PSA_ERROR_BAD_STATE );
3058 PSA_ASSERT( psa_cipher_abort( &operation ) );
3059
3060 /* Call decrypt setup twice in a row. */
3061 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
3062 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
3063 PSA_ERROR_BAD_STATE );
3064 PSA_ASSERT( psa_cipher_abort( &operation ) );
3065
Jaeden Ameroab439972019-02-15 14:12:05 +00003066 /* Generate an IV without calling setup beforehand. */
3067 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3068 buffer, sizeof( buffer ),
3069 &length ),
3070 PSA_ERROR_BAD_STATE );
3071 PSA_ASSERT( psa_cipher_abort( &operation ) );
3072
3073 /* Generate an IV twice in a row. */
3074 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3075 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3076 buffer, sizeof( buffer ),
3077 &length ) );
3078 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3079 buffer, sizeof( buffer ),
3080 &length ),
3081 PSA_ERROR_BAD_STATE );
3082 PSA_ASSERT( psa_cipher_abort( &operation ) );
3083
3084 /* Generate an IV after it's already set. */
3085 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3086 PSA_ASSERT( psa_cipher_set_iv( &operation,
3087 iv, sizeof( iv ) ) );
3088 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3089 buffer, sizeof( buffer ),
3090 &length ),
3091 PSA_ERROR_BAD_STATE );
3092 PSA_ASSERT( psa_cipher_abort( &operation ) );
3093
3094 /* Set an IV without calling setup beforehand. */
3095 TEST_EQUAL( psa_cipher_set_iv( &operation,
3096 iv, sizeof( iv ) ),
3097 PSA_ERROR_BAD_STATE );
3098 PSA_ASSERT( psa_cipher_abort( &operation ) );
3099
3100 /* Set an IV after it's already set. */
3101 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3102 PSA_ASSERT( psa_cipher_set_iv( &operation,
3103 iv, sizeof( iv ) ) );
3104 TEST_EQUAL( psa_cipher_set_iv( &operation,
3105 iv, sizeof( iv ) ),
3106 PSA_ERROR_BAD_STATE );
3107 PSA_ASSERT( psa_cipher_abort( &operation ) );
3108
3109 /* Set an IV after it's already generated. */
3110 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3111 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3112 buffer, sizeof( buffer ),
3113 &length ) );
3114 TEST_EQUAL( psa_cipher_set_iv( &operation,
3115 iv, sizeof( iv ) ),
3116 PSA_ERROR_BAD_STATE );
3117 PSA_ASSERT( psa_cipher_abort( &operation ) );
3118
3119 /* Call update without calling setup beforehand. */
3120 TEST_EQUAL( psa_cipher_update( &operation,
3121 text, sizeof( text ),
3122 buffer, sizeof( buffer ),
3123 &length ),
3124 PSA_ERROR_BAD_STATE );
3125 PSA_ASSERT( psa_cipher_abort( &operation ) );
3126
3127 /* Call update without an IV where an IV is required. */
3128 TEST_EQUAL( psa_cipher_update( &operation,
3129 text, sizeof( text ),
3130 buffer, sizeof( buffer ),
3131 &length ),
3132 PSA_ERROR_BAD_STATE );
3133 PSA_ASSERT( psa_cipher_abort( &operation ) );
3134
3135 /* Call update after finish. */
3136 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3137 PSA_ASSERT( psa_cipher_set_iv( &operation,
3138 iv, sizeof( iv ) ) );
3139 PSA_ASSERT( psa_cipher_finish( &operation,
3140 buffer, sizeof( buffer ), &length ) );
3141 TEST_EQUAL( psa_cipher_update( &operation,
3142 text, sizeof( text ),
3143 buffer, sizeof( buffer ),
3144 &length ),
3145 PSA_ERROR_BAD_STATE );
3146 PSA_ASSERT( psa_cipher_abort( &operation ) );
3147
3148 /* Call finish without calling setup beforehand. */
3149 TEST_EQUAL( psa_cipher_finish( &operation,
3150 buffer, sizeof( buffer ), &length ),
3151 PSA_ERROR_BAD_STATE );
3152 PSA_ASSERT( psa_cipher_abort( &operation ) );
3153
3154 /* Call finish without an IV where an IV is required. */
3155 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3156 /* Not calling update means we are encrypting an empty buffer, which is OK
3157 * for cipher modes with padding. */
3158 TEST_EQUAL( psa_cipher_finish( &operation,
3159 buffer, sizeof( buffer ), &length ),
3160 PSA_ERROR_BAD_STATE );
3161 PSA_ASSERT( psa_cipher_abort( &operation ) );
3162
3163 /* Call finish twice in a row. */
3164 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3165 PSA_ASSERT( psa_cipher_set_iv( &operation,
3166 iv, sizeof( iv ) ) );
3167 PSA_ASSERT( psa_cipher_finish( &operation,
3168 buffer, sizeof( buffer ), &length ) );
3169 TEST_EQUAL( psa_cipher_finish( &operation,
3170 buffer, sizeof( buffer ), &length ),
3171 PSA_ERROR_BAD_STATE );
3172 PSA_ASSERT( psa_cipher_abort( &operation ) );
3173
Gilles Peskine76b29a72019-05-28 14:08:50 +02003174 PSA_ASSERT( psa_destroy_key( handle ) );
3175
Jaeden Ameroab439972019-02-15 14:12:05 +00003176exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003177 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003178}
3179/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003180
Gilles Peskine50e586b2018-06-08 14:28:46 +02003181/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003182void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003183 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003184 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003185 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003186{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003187 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003188 psa_status_t status;
3189 psa_key_type_t key_type = key_type_arg;
3190 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003191 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003192 unsigned char *output = NULL;
3193 size_t output_buffer_size = 0;
3194 size_t function_output_length = 0;
3195 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003196 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003198
Gilles Peskine8817f612018-12-18 00:18:46 +01003199 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003200
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003201 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3202 psa_set_key_algorithm( &attributes, alg );
3203 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003204
Gilles Peskine73676cb2019-05-15 20:15:10 +02003205 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003206
Gilles Peskine8817f612018-12-18 00:18:46 +01003207 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3208 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003209
Gilles Peskine423005e2019-05-06 15:22:57 +02003210 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003211 output_buffer_size = ( (size_t) input->len +
3212 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003213 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_cipher_update( &operation,
3216 input->x, input->len,
3217 output, output_buffer_size,
3218 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003219 total_output_length += function_output_length;
3220 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003221 output + total_output_length,
3222 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003223 &function_output_length );
3224 total_output_length += function_output_length;
3225
Gilles Peskinefe11b722018-12-18 00:24:04 +01003226 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003227 if( expected_status == PSA_SUCCESS )
3228 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003229 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003230 ASSERT_COMPARE( expected_output->x, expected_output->len,
3231 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003232 }
3233
3234exit:
3235 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003236 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003237 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003238}
3239/* END_CASE */
3240
3241/* BEGIN_CASE */
3242void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003243 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003244 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003245 int first_part_size_arg,
3246 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003247 data_t *expected_output )
3248{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003249 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003250 psa_key_type_t key_type = key_type_arg;
3251 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003252 size_t first_part_size = first_part_size_arg;
3253 size_t output1_length = output1_length_arg;
3254 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003255 unsigned char *output = NULL;
3256 size_t output_buffer_size = 0;
3257 size_t function_output_length = 0;
3258 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003259 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003261
Gilles Peskine8817f612018-12-18 00:18:46 +01003262 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003263
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003264 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3265 psa_set_key_algorithm( &attributes, alg );
3266 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003267
Gilles Peskine73676cb2019-05-15 20:15:10 +02003268 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003269
Gilles Peskine8817f612018-12-18 00:18:46 +01003270 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3271 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003272
Gilles Peskine423005e2019-05-06 15:22:57 +02003273 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003274 output_buffer_size = ( (size_t) input->len +
3275 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003276 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003277
Gilles Peskinee0866522019-02-19 19:44:00 +01003278 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003279 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3280 output, output_buffer_size,
3281 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003282 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003283 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003284 PSA_ASSERT( psa_cipher_update( &operation,
3285 input->x + first_part_size,
3286 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003287 output + total_output_length,
3288 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003289 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003290 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003291 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003292 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003293 output + total_output_length,
3294 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003295 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003296 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003297 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003298
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003299 ASSERT_COMPARE( expected_output->x, expected_output->len,
3300 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003301
3302exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003303 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003304 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003305 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003306}
3307/* END_CASE */
3308
3309/* BEGIN_CASE */
3310void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003311 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003312 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003313 int first_part_size_arg,
3314 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003315 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003316{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003317 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003318
3319 psa_key_type_t key_type = key_type_arg;
3320 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003321 size_t first_part_size = first_part_size_arg;
3322 size_t output1_length = output1_length_arg;
3323 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003324 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003325 size_t output_buffer_size = 0;
3326 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003327 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003328 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003330
Gilles Peskine8817f612018-12-18 00:18:46 +01003331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003332
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003333 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3334 psa_set_key_algorithm( &attributes, alg );
3335 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003336
Gilles Peskine73676cb2019-05-15 20:15:10 +02003337 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003338
Gilles Peskine8817f612018-12-18 00:18:46 +01003339 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3340 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003341
Gilles Peskine423005e2019-05-06 15:22:57 +02003342 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003343
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003344 output_buffer_size = ( (size_t) input->len +
3345 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003346 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003347
Gilles Peskinee0866522019-02-19 19:44:00 +01003348 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003349 PSA_ASSERT( psa_cipher_update( &operation,
3350 input->x, first_part_size,
3351 output, output_buffer_size,
3352 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003353 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003354 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003355 PSA_ASSERT( psa_cipher_update( &operation,
3356 input->x + first_part_size,
3357 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003358 output + total_output_length,
3359 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003360 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003361 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003362 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003363 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003364 output + total_output_length,
3365 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003366 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003367 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003368 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003369
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003370 ASSERT_COMPARE( expected_output->x, expected_output->len,
3371 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003372
3373exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003374 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003375 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003376 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003377}
3378/* END_CASE */
3379
Gilles Peskine50e586b2018-06-08 14:28:46 +02003380/* BEGIN_CASE */
3381void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003382 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003383 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003384 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003385{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003386 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003387 psa_status_t status;
3388 psa_key_type_t key_type = key_type_arg;
3389 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003390 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003391 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003392 size_t output_buffer_size = 0;
3393 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003394 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003395 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003397
Gilles Peskine8817f612018-12-18 00:18:46 +01003398 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003399
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003400 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3401 psa_set_key_algorithm( &attributes, alg );
3402 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003403
Gilles Peskine73676cb2019-05-15 20:15:10 +02003404 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003405
Gilles Peskine8817f612018-12-18 00:18:46 +01003406 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3407 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003408
Gilles Peskine423005e2019-05-06 15:22:57 +02003409 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003410
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003411 output_buffer_size = ( (size_t) input->len +
3412 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003413 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003414
Gilles Peskine8817f612018-12-18 00:18:46 +01003415 PSA_ASSERT( psa_cipher_update( &operation,
3416 input->x, input->len,
3417 output, output_buffer_size,
3418 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003419 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003420 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003421 output + total_output_length,
3422 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003423 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003424 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003425 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003426
3427 if( expected_status == PSA_SUCCESS )
3428 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003429 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003430 ASSERT_COMPARE( expected_output->x, expected_output->len,
3431 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003432 }
3433
Gilles Peskine50e586b2018-06-08 14:28:46 +02003434exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003435 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003436 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003437 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003438}
3439/* END_CASE */
3440
Gilles Peskine50e586b2018-06-08 14:28:46 +02003441/* BEGIN_CASE */
3442void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003443 data_t *key,
3444 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003446 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003447 psa_key_type_t key_type = key_type_arg;
3448 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003449 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003450 size_t iv_size = 16;
3451 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003452 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003453 size_t output1_size = 0;
3454 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003455 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003456 size_t output2_size = 0;
3457 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003458 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003459 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3460 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003462
Gilles Peskine8817f612018-12-18 00:18:46 +01003463 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003464
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003465 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3466 psa_set_key_algorithm( &attributes, alg );
3467 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003468
Gilles Peskine73676cb2019-05-15 20:15:10 +02003469 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003470
Gilles Peskine8817f612018-12-18 00:18:46 +01003471 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3472 handle, alg ) );
3473 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3474 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003475
Gilles Peskine8817f612018-12-18 00:18:46 +01003476 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3477 iv, iv_size,
3478 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003479 output1_size = ( (size_t) input->len +
3480 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003481 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003482
Gilles Peskine8817f612018-12-18 00:18:46 +01003483 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3484 output1, output1_size,
3485 &output1_length ) );
3486 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003487 output1 + output1_length,
3488 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003489 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003490
Gilles Peskine048b7f02018-06-08 14:20:49 +02003491 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003492
Gilles Peskine8817f612018-12-18 00:18:46 +01003493 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003494
3495 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003496 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003497
Gilles Peskine8817f612018-12-18 00:18:46 +01003498 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3499 iv, iv_length ) );
3500 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3501 output2, output2_size,
3502 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003503 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003504 PSA_ASSERT( psa_cipher_finish( &operation2,
3505 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003506 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003507 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003508
Gilles Peskine048b7f02018-06-08 14:20:49 +02003509 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003510
Gilles Peskine8817f612018-12-18 00:18:46 +01003511 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003512
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003513 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003514
3515exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003516 mbedtls_free( output1 );
3517 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003518 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003519 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003520}
3521/* END_CASE */
3522
3523/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003524void cipher_verify_output_multipart( int alg_arg,
3525 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003526 data_t *key,
3527 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003528 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003529{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003530 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003531 psa_key_type_t key_type = key_type_arg;
3532 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003533 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003534 unsigned char iv[16] = {0};
3535 size_t iv_size = 16;
3536 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003537 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003538 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003539 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003540 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003541 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003542 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003543 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003544 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3545 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003546 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003547
Gilles Peskine8817f612018-12-18 00:18:46 +01003548 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003549
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003550 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3551 psa_set_key_algorithm( &attributes, alg );
3552 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003553
Gilles Peskine73676cb2019-05-15 20:15:10 +02003554 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003555
Gilles Peskine8817f612018-12-18 00:18:46 +01003556 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3557 handle, alg ) );
3558 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3559 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003560
Gilles Peskine8817f612018-12-18 00:18:46 +01003561 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3562 iv, iv_size,
3563 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003564 output1_buffer_size = ( (size_t) input->len +
3565 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003566 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003567
Gilles Peskinee0866522019-02-19 19:44:00 +01003568 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003569
Gilles Peskine8817f612018-12-18 00:18:46 +01003570 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3571 output1, output1_buffer_size,
3572 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003573 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003574
Gilles Peskine8817f612018-12-18 00:18:46 +01003575 PSA_ASSERT( psa_cipher_update( &operation1,
3576 input->x + first_part_size,
3577 input->len - first_part_size,
3578 output1, output1_buffer_size,
3579 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003580 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003581
Gilles Peskine8817f612018-12-18 00:18:46 +01003582 PSA_ASSERT( psa_cipher_finish( &operation1,
3583 output1 + output1_length,
3584 output1_buffer_size - output1_length,
3585 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003586 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003587
Gilles Peskine8817f612018-12-18 00:18:46 +01003588 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003589
Gilles Peskine048b7f02018-06-08 14:20:49 +02003590 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003591 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003592
Gilles Peskine8817f612018-12-18 00:18:46 +01003593 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3594 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003595
Gilles Peskine8817f612018-12-18 00:18:46 +01003596 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3597 output2, output2_buffer_size,
3598 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003599 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003600
Gilles Peskine8817f612018-12-18 00:18:46 +01003601 PSA_ASSERT( psa_cipher_update( &operation2,
3602 output1 + first_part_size,
3603 output1_length - first_part_size,
3604 output2, output2_buffer_size,
3605 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003606 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003607
Gilles Peskine8817f612018-12-18 00:18:46 +01003608 PSA_ASSERT( psa_cipher_finish( &operation2,
3609 output2 + output2_length,
3610 output2_buffer_size - output2_length,
3611 &function_output_length ) );
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 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003615
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003616 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003617
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( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003623}
3624/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003625
Gilles Peskine20035e32018-02-03 22:44:14 +01003626/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003627void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003628 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003629 data_t *nonce,
3630 data_t *additional_data,
3631 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003632 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003633{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003634 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003635 psa_key_type_t key_type = key_type_arg;
3636 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003637 unsigned char *output_data = NULL;
3638 size_t output_size = 0;
3639 size_t output_length = 0;
3640 unsigned char *output_data2 = NULL;
3641 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003642 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003643 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003644 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003645
Gilles Peskine4abf7412018-06-18 16:35:34 +02003646 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003647 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3648 * should be exact. */
3649 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3650 TEST_EQUAL( output_size,
3651 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003652 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003653
Gilles Peskine8817f612018-12-18 00:18:46 +01003654 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003655
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003656 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3657 psa_set_key_algorithm( &attributes, alg );
3658 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003659
Gilles Peskine049c7532019-05-15 20:22:09 +02003660 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3661 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003662
Gilles Peskinefe11b722018-12-18 00:24:04 +01003663 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3664 nonce->x, nonce->len,
3665 additional_data->x,
3666 additional_data->len,
3667 input_data->x, input_data->len,
3668 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003669 &output_length ),
3670 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003671
3672 if( PSA_SUCCESS == expected_result )
3673 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003674 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003675
Gilles Peskine003a4a92019-05-14 16:09:40 +02003676 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3677 * should be exact. */
3678 TEST_EQUAL( input_data->len,
3679 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3680
Gilles Peskinefe11b722018-12-18 00:24:04 +01003681 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3682 nonce->x, nonce->len,
3683 additional_data->x,
3684 additional_data->len,
3685 output_data, output_length,
3686 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003687 &output_length2 ),
3688 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003689
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003690 ASSERT_COMPARE( input_data->x, input_data->len,
3691 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003692 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003693
Gilles Peskinea1cac842018-06-11 19:33:02 +02003694exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003695 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003696 mbedtls_free( output_data );
3697 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003698 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003699}
3700/* END_CASE */
3701
3702/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003703void aead_encrypt( int key_type_arg, data_t *key_data,
3704 int alg_arg,
3705 data_t *nonce,
3706 data_t *additional_data,
3707 data_t *input_data,
3708 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003709{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003710 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003711 psa_key_type_t key_type = key_type_arg;
3712 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003713 unsigned char *output_data = NULL;
3714 size_t output_size = 0;
3715 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003716 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003718
Gilles Peskine4abf7412018-06-18 16:35:34 +02003719 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003720 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3721 * should be exact. */
3722 TEST_EQUAL( output_size,
3723 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003724 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003725
Gilles Peskine8817f612018-12-18 00:18:46 +01003726 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003727
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3729 psa_set_key_algorithm( &attributes, alg );
3730 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003731
Gilles Peskine049c7532019-05-15 20:22:09 +02003732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3733 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003734
Gilles Peskine8817f612018-12-18 00:18:46 +01003735 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3736 nonce->x, nonce->len,
3737 additional_data->x, additional_data->len,
3738 input_data->x, input_data->len,
3739 output_data, output_size,
3740 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003741
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003742 ASSERT_COMPARE( expected_result->x, expected_result->len,
3743 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003744
Gilles Peskinea1cac842018-06-11 19:33:02 +02003745exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003746 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003747 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003748 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003749}
3750/* END_CASE */
3751
3752/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003753void aead_decrypt( int key_type_arg, data_t *key_data,
3754 int alg_arg,
3755 data_t *nonce,
3756 data_t *additional_data,
3757 data_t *input_data,
3758 data_t *expected_data,
3759 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003760{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003761 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003762 psa_key_type_t key_type = key_type_arg;
3763 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003764 unsigned char *output_data = NULL;
3765 size_t output_size = 0;
3766 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003767 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003769 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003770
Gilles Peskine003a4a92019-05-14 16:09:40 +02003771 output_size = input_data->len - tag_length;
3772 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3773 * should be exact. */
3774 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3775 TEST_EQUAL( output_size,
3776 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003777 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003778
Gilles Peskine8817f612018-12-18 00:18:46 +01003779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003780
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003781 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3782 psa_set_key_algorithm( &attributes, alg );
3783 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003784
Gilles Peskine049c7532019-05-15 20:22:09 +02003785 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3786 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003787
Gilles Peskinefe11b722018-12-18 00:24:04 +01003788 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3789 nonce->x, nonce->len,
3790 additional_data->x,
3791 additional_data->len,
3792 input_data->x, input_data->len,
3793 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003794 &output_length ),
3795 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003796
Gilles Peskine2d277862018-06-18 15:41:12 +02003797 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003798 ASSERT_COMPARE( expected_data->x, expected_data->len,
3799 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003800
Gilles Peskinea1cac842018-06-11 19:33:02 +02003801exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003802 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003803 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003804 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003805}
3806/* END_CASE */
3807
3808/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003809void signature_size( int type_arg,
3810 int bits,
3811 int alg_arg,
3812 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003813{
3814 psa_key_type_t type = type_arg;
3815 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003816 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003817
Gilles Peskinefe11b722018-12-18 00:24:04 +01003818 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003819#if defined(MBEDTLS_TEST_DEPRECATED)
3820 TEST_EQUAL( actual_size,
3821 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3822#endif /* MBEDTLS_TEST_DEPRECATED */
3823
Gilles Peskinee59236f2018-01-27 23:32:46 +01003824exit:
3825 ;
3826}
3827/* END_CASE */
3828
3829/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003830void sign_deterministic( int key_type_arg, data_t *key_data,
3831 int alg_arg, data_t *input_data,
3832 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003833{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003834 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003835 psa_key_type_t key_type = key_type_arg;
3836 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003837 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003838 unsigned char *signature = NULL;
3839 size_t signature_size;
3840 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003841 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003842
Gilles Peskine8817f612018-12-18 00:18:46 +01003843 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003844
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003845 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003846 psa_set_key_algorithm( &attributes, alg );
3847 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003848
Gilles Peskine049c7532019-05-15 20:22:09 +02003849 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3850 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003851 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3852 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003853
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003854 /* Allocate a buffer which has the size advertized by the
3855 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003856 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003857 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003858 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003859 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003860 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003861
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003862 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003863 PSA_ASSERT( psa_sign_hash( handle, alg,
3864 input_data->x, input_data->len,
3865 signature, signature_size,
3866 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003867 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003868 ASSERT_COMPARE( output_data->x, output_data->len,
3869 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003870
Gilles Peskine0627f982019-11-26 19:12:16 +01003871#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003872 memset( signature, 0, signature_size );
3873 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01003874 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3875 input_data->x, input_data->len,
3876 signature, signature_size,
3877 &signature_length ) );
3878 ASSERT_COMPARE( output_data->x, output_data->len,
3879 signature, signature_length );
3880#endif /* MBEDTLS_TEST_DEPRECATED */
3881
Gilles Peskine20035e32018-02-03 22:44:14 +01003882exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003883 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003884 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003885 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003886 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003887}
3888/* END_CASE */
3889
3890/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003891void sign_fail( int key_type_arg, data_t *key_data,
3892 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003893 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003894{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003895 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003896 psa_key_type_t key_type = key_type_arg;
3897 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003898 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003899 psa_status_t actual_status;
3900 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003901 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003902 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003904
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003905 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003906
Gilles Peskine8817f612018-12-18 00:18:46 +01003907 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003908
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003909 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003910 psa_set_key_algorithm( &attributes, alg );
3911 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003912
Gilles Peskine049c7532019-05-15 20:22:09 +02003913 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3914 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003915
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003916 actual_status = psa_sign_hash( handle, alg,
3917 input_data->x, input_data->len,
3918 signature, signature_size,
3919 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003920 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003921 /* The value of *signature_length is unspecified on error, but
3922 * whatever it is, it should be less than signature_size, so that
3923 * if the caller tries to read *signature_length bytes without
3924 * checking the error code then they don't overflow a buffer. */
3925 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003926
Gilles Peskine895242b2019-11-29 12:15:40 +01003927#if defined(MBEDTLS_TEST_DEPRECATED)
3928 signature_length = INVALID_EXPORT_LENGTH;
3929 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
3930 input_data->x, input_data->len,
3931 signature, signature_size,
3932 &signature_length ),
3933 expected_status );
3934 TEST_ASSERT( signature_length <= signature_size );
3935#endif /* MBEDTLS_TEST_DEPRECATED */
3936
Gilles Peskine20035e32018-02-03 22:44:14 +01003937exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003938 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003939 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003940 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003941 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003942}
3943/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003944
3945/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003946void sign_verify( int key_type_arg, data_t *key_data,
3947 int alg_arg, data_t *input_data )
3948{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003949 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003950 psa_key_type_t key_type = key_type_arg;
3951 psa_algorithm_t alg = alg_arg;
3952 size_t key_bits;
3953 unsigned char *signature = NULL;
3954 size_t signature_size;
3955 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003956 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003957
Gilles Peskine8817f612018-12-18 00:18:46 +01003958 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003959
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003960 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003961 psa_set_key_algorithm( &attributes, alg );
3962 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003963
Gilles Peskine049c7532019-05-15 20:22:09 +02003964 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3965 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003966 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3967 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003968
3969 /* Allocate a buffer which has the size advertized by the
3970 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003971 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003972 key_bits, alg );
3973 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003974 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003975 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003976
3977 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003978 PSA_ASSERT( psa_sign_hash( handle, alg,
3979 input_data->x, input_data->len,
3980 signature, signature_size,
3981 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003982 /* Check that the signature length looks sensible. */
3983 TEST_ASSERT( signature_length <= signature_size );
3984 TEST_ASSERT( signature_length > 0 );
3985
3986 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003987 PSA_ASSERT( psa_verify_hash( handle, alg,
3988 input_data->x, input_data->len,
3989 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003990
3991 if( input_data->len != 0 )
3992 {
3993 /* Flip a bit in the input and verify that the signature is now
3994 * detected as invalid. Flip a bit at the beginning, not at the end,
3995 * because ECDSA may ignore the last few bits of the input. */
3996 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003997 TEST_EQUAL( psa_verify_hash( handle, alg,
3998 input_data->x, input_data->len,
3999 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004000 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004001 }
4002
4003exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004004 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004005 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02004006 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004007 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004008}
4009/* END_CASE */
4010
4011/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004012void asymmetric_verify( int key_type_arg, data_t *key_data,
4013 int alg_arg, data_t *hash_data,
4014 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004015{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004016 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03004017 psa_key_type_t key_type = key_type_arg;
4018 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004019 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004020
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004021 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004022
Gilles Peskine8817f612018-12-18 00:18:46 +01004023 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004024
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004025 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004026 psa_set_key_algorithm( &attributes, alg );
4027 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004028
Gilles Peskine049c7532019-05-15 20:22:09 +02004029 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4030 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03004031
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004032 PSA_ASSERT( psa_verify_hash( handle, alg,
4033 hash_data->x, hash_data->len,
4034 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004035
4036#if defined(MBEDTLS_TEST_DEPRECATED)
4037 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
4038 hash_data->x, hash_data->len,
4039 signature_data->x,
4040 signature_data->len ) );
4041
4042#endif /* MBEDTLS_TEST_DEPRECATED */
4043
itayzafrir5c753392018-05-08 11:18:38 +03004044exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004045 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004046 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004047 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004048}
4049/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050
4051/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004052void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4053 int alg_arg, data_t *hash_data,
4054 data_t *signature_data,
4055 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004056{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004057 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004058 psa_key_type_t key_type = key_type_arg;
4059 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004060 psa_status_t actual_status;
4061 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004063
Gilles Peskine8817f612018-12-18 00:18:46 +01004064 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004065
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004066 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004067 psa_set_key_algorithm( &attributes, alg );
4068 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004069
Gilles Peskine049c7532019-05-15 20:22:09 +02004070 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4071 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004072
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004073 actual_status = psa_verify_hash( handle, alg,
4074 hash_data->x, hash_data->len,
4075 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004076 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004077
Gilles Peskine895242b2019-11-29 12:15:40 +01004078#if defined(MBEDTLS_TEST_DEPRECATED)
4079 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
4080 hash_data->x, hash_data->len,
4081 signature_data->x, signature_data->len ),
4082 expected_status );
4083#endif /* MBEDTLS_TEST_DEPRECATED */
4084
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004085exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004086 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004087 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004088 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004089}
4090/* END_CASE */
4091
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004092/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004093void asymmetric_encrypt( int key_type_arg,
4094 data_t *key_data,
4095 int alg_arg,
4096 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004097 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004098 int expected_output_length_arg,
4099 int expected_status_arg )
4100{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004101 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004102 psa_key_type_t key_type = key_type_arg;
4103 psa_algorithm_t alg = alg_arg;
4104 size_t expected_output_length = expected_output_length_arg;
4105 size_t key_bits;
4106 unsigned char *output = NULL;
4107 size_t output_size;
4108 size_t output_length = ~0;
4109 psa_status_t actual_status;
4110 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004112
Gilles Peskine8817f612018-12-18 00:18:46 +01004113 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004114
Gilles Peskine656896e2018-06-29 19:12:28 +02004115 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004116 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4117 psa_set_key_algorithm( &attributes, alg );
4118 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004119 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4120 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004121
4122 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004123 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4124 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004125 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004126 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004127
4128 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004129 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004130 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004131 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004132 output, output_size,
4133 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004134 TEST_EQUAL( actual_status, expected_status );
4135 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004136
Gilles Peskine68428122018-06-30 18:42:41 +02004137 /* If the label is empty, the test framework puts a non-null pointer
4138 * in label->x. Test that a null pointer works as well. */
4139 if( label->len == 0 )
4140 {
4141 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004142 if( output_size != 0 )
4143 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004144 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004145 input_data->x, input_data->len,
4146 NULL, label->len,
4147 output, output_size,
4148 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004149 TEST_EQUAL( actual_status, expected_status );
4150 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004151 }
4152
Gilles Peskine656896e2018-06-29 19:12:28 +02004153exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004154 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004155 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004156 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004157 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004158}
4159/* END_CASE */
4160
4161/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004162void asymmetric_encrypt_decrypt( int key_type_arg,
4163 data_t *key_data,
4164 int alg_arg,
4165 data_t *input_data,
4166 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004167{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004168 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004169 psa_key_type_t key_type = key_type_arg;
4170 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004171 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004172 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004173 size_t output_size;
4174 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004175 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004176 size_t output2_size;
4177 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004179
Gilles Peskine8817f612018-12-18 00:18:46 +01004180 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004181
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004182 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4183 psa_set_key_algorithm( &attributes, alg );
4184 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004185
Gilles Peskine049c7532019-05-15 20:22:09 +02004186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4187 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004188
4189 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004190 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4191 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004192 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004193 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004194 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004195 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004196
Gilles Peskineeebd7382018-06-08 18:11:54 +02004197 /* We test encryption by checking that encrypt-then-decrypt gives back
4198 * the original plaintext because of the non-optional random
4199 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004200 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4201 input_data->x, input_data->len,
4202 label->x, label->len,
4203 output, output_size,
4204 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004205 /* We don't know what ciphertext length to expect, but check that
4206 * it looks sensible. */
4207 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004208
Gilles Peskine8817f612018-12-18 00:18:46 +01004209 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4210 output, output_length,
4211 label->x, label->len,
4212 output2, output2_size,
4213 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004214 ASSERT_COMPARE( input_data->x, input_data->len,
4215 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004216
4217exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004218 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004219 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004220 mbedtls_free( output );
4221 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004222 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004223}
4224/* END_CASE */
4225
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004226/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004227void asymmetric_decrypt( int key_type_arg,
4228 data_t *key_data,
4229 int alg_arg,
4230 data_t *input_data,
4231 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004232 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004233{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004234 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004235 psa_key_type_t key_type = key_type_arg;
4236 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004237 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004238 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004239 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004241
Jaeden Amero412654a2019-02-06 12:57:46 +00004242 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004243 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004244
Gilles Peskine8817f612018-12-18 00:18:46 +01004245 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004246
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004247 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4248 psa_set_key_algorithm( &attributes, alg );
4249 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004250
Gilles Peskine049c7532019-05-15 20:22:09 +02004251 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4252 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004253
Gilles Peskine8817f612018-12-18 00:18:46 +01004254 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4255 input_data->x, input_data->len,
4256 label->x, label->len,
4257 output,
4258 output_size,
4259 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004260 ASSERT_COMPARE( expected_data->x, expected_data->len,
4261 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004262
Gilles Peskine68428122018-06-30 18:42:41 +02004263 /* If the label is empty, the test framework puts a non-null pointer
4264 * in label->x. Test that a null pointer works as well. */
4265 if( label->len == 0 )
4266 {
4267 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004268 if( output_size != 0 )
4269 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004270 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4271 input_data->x, input_data->len,
4272 NULL, label->len,
4273 output,
4274 output_size,
4275 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004276 ASSERT_COMPARE( expected_data->x, expected_data->len,
4277 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004278 }
4279
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004280exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004281 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004282 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004283 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004284 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004285}
4286/* END_CASE */
4287
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004288/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004289void asymmetric_decrypt_fail( int key_type_arg,
4290 data_t *key_data,
4291 int alg_arg,
4292 data_t *input_data,
4293 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004294 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004295 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004296{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004297 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004298 psa_key_type_t key_type = key_type_arg;
4299 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004300 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004301 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004302 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004303 psa_status_t actual_status;
4304 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004305 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004306
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004307 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004308
Gilles Peskine8817f612018-12-18 00:18:46 +01004309 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004310
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004311 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4312 psa_set_key_algorithm( &attributes, alg );
4313 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004314
Gilles Peskine049c7532019-05-15 20:22:09 +02004315 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4316 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004317
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004318 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004319 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004320 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004321 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004322 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004323 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004324 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004325
Gilles Peskine68428122018-06-30 18:42:41 +02004326 /* If the label is empty, the test framework puts a non-null pointer
4327 * in label->x. Test that a null pointer works as well. */
4328 if( label->len == 0 )
4329 {
4330 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004331 if( output_size != 0 )
4332 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004333 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004334 input_data->x, input_data->len,
4335 NULL, label->len,
4336 output, output_size,
4337 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004338 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004339 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004340 }
4341
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004342exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004343 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004344 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004345 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004346 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004347}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004348/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004349
4350/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004351void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004352{
4353 /* Test each valid way of initializing the object, except for `= {0}`, as
4354 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4355 * though it's OK by the C standard. We could test for this, but we'd need
4356 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004357 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004358 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4359 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4360 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004361
4362 memset( &zero, 0, sizeof( zero ) );
4363
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004364 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004365 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004366 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004367 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004368 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004369 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004370 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004371
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004372 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004373 PSA_ASSERT( psa_key_derivation_abort(&func) );
4374 PSA_ASSERT( psa_key_derivation_abort(&init) );
4375 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004376}
4377/* END_CASE */
4378
Janos Follath16de4a42019-06-13 16:32:24 +01004379/* BEGIN_CASE */
4380void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004381{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004382 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004383 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004384 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004385
Gilles Peskine8817f612018-12-18 00:18:46 +01004386 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004387
Janos Follath16de4a42019-06-13 16:32:24 +01004388 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004389 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004390
4391exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004392 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004393 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004394}
4395/* END_CASE */
4396
Janos Follathaf3c2a02019-06-12 12:34:34 +01004397/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004398void derive_set_capacity( int alg_arg, int capacity_arg,
4399 int expected_status_arg )
4400{
4401 psa_algorithm_t alg = alg_arg;
4402 size_t capacity = capacity_arg;
4403 psa_status_t expected_status = expected_status_arg;
4404 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4405
4406 PSA_ASSERT( psa_crypto_init( ) );
4407
4408 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4409
4410 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4411 expected_status );
4412
4413exit:
4414 psa_key_derivation_abort( &operation );
4415 PSA_DONE( );
4416}
4417/* END_CASE */
4418
4419/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004420void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004421 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004422 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004423 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004424 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004425 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004426 int expected_status_arg3,
4427 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004428{
4429 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004430 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4431 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004432 psa_status_t expected_statuses[] = {expected_status_arg1,
4433 expected_status_arg2,
4434 expected_status_arg3};
4435 data_t *inputs[] = {input1, input2, input3};
4436 psa_key_handle_t handles[] = {0, 0, 0};
4437 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4439 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004440 psa_key_type_t output_key_type = output_key_type_arg;
4441 psa_key_handle_t output_handle = 0;
4442 psa_status_t expected_output_status = expected_output_status_arg;
4443 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004444
4445 PSA_ASSERT( psa_crypto_init( ) );
4446
4447 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4448 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004449
4450 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4451
4452 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4453 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004454 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004455 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004456 psa_set_key_type( &attributes, key_types[i] );
4457 PSA_ASSERT( psa_import_key( &attributes,
4458 inputs[i]->x, inputs[i]->len,
4459 &handles[i] ) );
4460 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4461 handles[i] ),
4462 expected_statuses[i] );
4463 }
4464 else
4465 {
4466 TEST_EQUAL( psa_key_derivation_input_bytes(
4467 &operation, steps[i],
4468 inputs[i]->x, inputs[i]->len ),
4469 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004470 }
4471 }
4472
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004473 if( output_key_type != PSA_KEY_TYPE_NONE )
4474 {
4475 psa_reset_key_attributes( &attributes );
4476 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4477 psa_set_key_bits( &attributes, 8 );
4478 actual_output_status =
4479 psa_key_derivation_output_key( &attributes, &operation,
4480 &output_handle );
4481 }
4482 else
4483 {
4484 uint8_t buffer[1];
4485 actual_output_status =
4486 psa_key_derivation_output_bytes( &operation,
4487 buffer, sizeof( buffer ) );
4488 }
4489 TEST_EQUAL( actual_output_status, expected_output_status );
4490
Janos Follathaf3c2a02019-06-12 12:34:34 +01004491exit:
4492 psa_key_derivation_abort( &operation );
4493 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4494 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004495 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004496 PSA_DONE( );
4497}
4498/* END_CASE */
4499
Janos Follathd958bb72019-07-03 15:02:16 +01004500/* BEGIN_CASE */
4501void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004502{
Janos Follathd958bb72019-07-03 15:02:16 +01004503 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004504 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004505 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004506 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004507 unsigned char input1[] = "Input 1";
4508 size_t input1_length = sizeof( input1 );
4509 unsigned char input2[] = "Input 2";
4510 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004511 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004512 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004513 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4514 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4515 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004517
Gilles Peskine8817f612018-12-18 00:18:46 +01004518 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004519
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004520 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4521 psa_set_key_algorithm( &attributes, alg );
4522 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004523
Gilles Peskine73676cb2019-05-15 20:15:10 +02004524 PSA_ASSERT( psa_import_key( &attributes,
4525 key_data, sizeof( key_data ),
4526 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004527
4528 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004529 if( !setup_key_derivation_wrap( &operation, handle, alg,
4530 input1, input1_length,
4531 input2, input2_length,
4532 capacity ) )
4533 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004534
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004535 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004536 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004537 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004538
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004539 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004540
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004541 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004542 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004543
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004544exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004546 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004547 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004548}
4549/* END_CASE */
4550
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004551/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004552void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004553{
4554 uint8_t output_buffer[16];
4555 size_t buffer_size = 16;
4556 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004557 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004558
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004559 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4560 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004561 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004562
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004563 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004564 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004565
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004566 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004567
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004568 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4569 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004570 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004571
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004572 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004573 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004574
4575exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004576 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004577}
4578/* END_CASE */
4579
4580/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004581void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004582 int step1_arg, data_t *input1,
4583 int step2_arg, data_t *input2,
4584 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004585 int requested_capacity_arg,
4586 data_t *expected_output1,
4587 data_t *expected_output2 )
4588{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004589 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004590 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4591 data_t *inputs[] = {input1, input2, input3};
4592 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004593 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004594 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004595 uint8_t *expected_outputs[2] =
4596 {expected_output1->x, expected_output2->x};
4597 size_t output_sizes[2] =
4598 {expected_output1->len, expected_output2->len};
4599 size_t output_buffer_size = 0;
4600 uint8_t *output_buffer = NULL;
4601 size_t expected_capacity;
4602 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004604 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004605 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004606
4607 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4608 {
4609 if( output_sizes[i] > output_buffer_size )
4610 output_buffer_size = output_sizes[i];
4611 if( output_sizes[i] == 0 )
4612 expected_outputs[i] = NULL;
4613 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004614 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004616
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4618 psa_set_key_algorithm( &attributes, alg );
4619 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004620
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004621 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004622 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4623 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4624 requested_capacity ) );
4625 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004626 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004627 switch( steps[i] )
4628 {
4629 case 0:
4630 break;
4631 case PSA_KEY_DERIVATION_INPUT_SECRET:
4632 PSA_ASSERT( psa_import_key( &attributes,
4633 inputs[i]->x, inputs[i]->len,
4634 &handles[i] ) );
4635 PSA_ASSERT( psa_key_derivation_input_key(
4636 &operation, steps[i],
4637 handles[i] ) );
4638 break;
4639 default:
4640 PSA_ASSERT( psa_key_derivation_input_bytes(
4641 &operation, steps[i],
4642 inputs[i]->x, inputs[i]->len ) );
4643 break;
4644 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004645 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004646
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004648 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004649 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004650 expected_capacity = requested_capacity;
4651
4652 /* Expansion phase. */
4653 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4654 {
4655 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004656 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004657 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004658 if( expected_capacity == 0 && output_sizes[i] == 0 )
4659 {
4660 /* Reading 0 bytes when 0 bytes are available can go either way. */
4661 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004662 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004663 continue;
4664 }
4665 else if( expected_capacity == 0 ||
4666 output_sizes[i] > expected_capacity )
4667 {
4668 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004669 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004670 expected_capacity = 0;
4671 continue;
4672 }
4673 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004674 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004675 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004676 ASSERT_COMPARE( output_buffer, output_sizes[i],
4677 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004678 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004679 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004680 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004681 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004682 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004683 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004684 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004685
4686exit:
4687 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004688 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004689 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4690 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004691 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004692}
4693/* END_CASE */
4694
4695/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004696void derive_full( int alg_arg,
4697 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004698 data_t *input1,
4699 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004700 int requested_capacity_arg )
4701{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004702 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004703 psa_algorithm_t alg = alg_arg;
4704 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004705 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004706 unsigned char output_buffer[16];
4707 size_t expected_capacity = requested_capacity;
4708 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004710
Gilles Peskine8817f612018-12-18 00:18:46 +01004711 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004712
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004713 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4714 psa_set_key_algorithm( &attributes, alg );
4715 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004716
Gilles Peskine049c7532019-05-15 20:22:09 +02004717 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4718 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004719
Janos Follathf2815ea2019-07-03 12:41:36 +01004720 if( !setup_key_derivation_wrap( &operation, handle, alg,
4721 input1->x, input1->len,
4722 input2->x, input2->len,
4723 requested_capacity ) )
4724 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004725
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004727 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004728 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004729
4730 /* Expansion phase. */
4731 while( current_capacity > 0 )
4732 {
4733 size_t read_size = sizeof( output_buffer );
4734 if( read_size > current_capacity )
4735 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004737 output_buffer,
4738 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004739 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004741 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004742 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004743 }
4744
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745 /* Check that the operation refuses to go over capacity. */
4746 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004747 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004748
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004750
4751exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004753 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004754 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004755}
4756/* END_CASE */
4757
Janos Follathe60c9052019-07-03 13:51:30 +01004758/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004759void derive_key_exercise( int alg_arg,
4760 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004761 data_t *input1,
4762 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004763 int derived_type_arg,
4764 int derived_bits_arg,
4765 int derived_usage_arg,
4766 int derived_alg_arg )
4767{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004768 psa_key_handle_t base_handle = 0;
4769 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004770 psa_algorithm_t alg = alg_arg;
4771 psa_key_type_t derived_type = derived_type_arg;
4772 size_t derived_bits = derived_bits_arg;
4773 psa_key_usage_t derived_usage = derived_usage_arg;
4774 psa_algorithm_t derived_alg = derived_alg_arg;
4775 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004776 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004778 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004779
Gilles Peskine8817f612018-12-18 00:18:46 +01004780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004781
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004782 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4783 psa_set_key_algorithm( &attributes, alg );
4784 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004785 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4786 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004787
4788 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004789 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4790 input1->x, input1->len,
4791 input2->x, input2->len, capacity ) )
4792 goto exit;
4793
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004794 psa_set_key_usage_flags( &attributes, derived_usage );
4795 psa_set_key_algorithm( &attributes, derived_alg );
4796 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004797 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004798 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004799 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004800
4801 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004802 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4803 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4804 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004805
4806 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004807 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004808 goto exit;
4809
4810exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004812 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004813 psa_destroy_key( base_handle );
4814 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004815 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004816}
4817/* END_CASE */
4818
Janos Follath42fd8882019-07-03 14:17:09 +01004819/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004820void derive_key_export( int alg_arg,
4821 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004822 data_t *input1,
4823 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004824 int bytes1_arg,
4825 int bytes2_arg )
4826{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004827 psa_key_handle_t base_handle = 0;
4828 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004829 psa_algorithm_t alg = alg_arg;
4830 size_t bytes1 = bytes1_arg;
4831 size_t bytes2 = bytes2_arg;
4832 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004834 uint8_t *output_buffer = NULL;
4835 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004836 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4837 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004838 size_t length;
4839
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004840 ASSERT_ALLOC( output_buffer, capacity );
4841 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004842 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004843
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004844 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4845 psa_set_key_algorithm( &base_attributes, alg );
4846 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004847 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4848 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004849
4850 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01004851 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4852 input1->x, input1->len,
4853 input2->x, input2->len, capacity ) )
4854 goto exit;
4855
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004857 output_buffer,
4858 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004859 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004860
4861 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01004862 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4863 input1->x, input1->len,
4864 input2->x, input2->len, capacity ) )
4865 goto exit;
4866
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004867 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4868 psa_set_key_algorithm( &derived_attributes, 0 );
4869 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004870 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004871 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004872 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004873 PSA_ASSERT( psa_export_key( derived_handle,
4874 export_buffer, bytes1,
4875 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004876 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004877 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004878 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004879 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004880 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004881 PSA_ASSERT( psa_export_key( derived_handle,
4882 export_buffer + bytes1, bytes2,
4883 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004884 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004885
4886 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004887 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4888 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004889
4890exit:
4891 mbedtls_free( output_buffer );
4892 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004893 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004894 psa_destroy_key( base_handle );
4895 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004896 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004897}
4898/* END_CASE */
4899
4900/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004901void derive_key( int alg_arg,
4902 data_t *key_data, data_t *input1, data_t *input2,
4903 int type_arg, int bits_arg,
4904 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02004905{
4906 psa_key_handle_t base_handle = 0;
4907 psa_key_handle_t derived_handle = 0;
4908 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004909 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004910 size_t bits = bits_arg;
4911 psa_status_t expected_status = expected_status_arg;
4912 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4913 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4914 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4915
4916 PSA_ASSERT( psa_crypto_init( ) );
4917
4918 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4919 psa_set_key_algorithm( &base_attributes, alg );
4920 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4921 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4922 &base_handle ) );
4923
4924 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4925 input1->x, input1->len,
4926 input2->x, input2->len, SIZE_MAX ) )
4927 goto exit;
4928
4929 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4930 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004931 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004932 psa_set_key_bits( &derived_attributes, bits );
4933 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
4934 &derived_handle ),
4935 expected_status );
4936
4937exit:
4938 psa_key_derivation_abort( &operation );
4939 psa_destroy_key( base_handle );
4940 psa_destroy_key( derived_handle );
4941 PSA_DONE( );
4942}
4943/* END_CASE */
4944
4945/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004946void key_agreement_setup( int alg_arg,
4947 int our_key_type_arg, data_t *our_key_data,
4948 data_t *peer_key_data,
4949 int expected_status_arg )
4950{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004951 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004952 psa_algorithm_t alg = alg_arg;
4953 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004956 psa_status_t expected_status = expected_status_arg;
4957 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004958
Gilles Peskine8817f612018-12-18 00:18:46 +01004959 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004960
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4962 psa_set_key_algorithm( &attributes, alg );
4963 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004964 PSA_ASSERT( psa_import_key( &attributes,
4965 our_key_data->x, our_key_data->len,
4966 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004967
Gilles Peskine77f40d82019-04-11 21:27:06 +02004968 /* The tests currently include inputs that should fail at either step.
4969 * Test cases that fail at the setup step should be changed to call
4970 * key_derivation_setup instead, and this function should be renamed
4971 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004973 if( status == PSA_SUCCESS )
4974 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004975 TEST_EQUAL( psa_key_derivation_key_agreement(
4976 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4977 our_key,
4978 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004979 expected_status );
4980 }
4981 else
4982 {
4983 TEST_ASSERT( status == expected_status );
4984 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004985
4986exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004987 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004988 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004989 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004990}
4991/* END_CASE */
4992
4993/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004994void raw_key_agreement( int alg_arg,
4995 int our_key_type_arg, data_t *our_key_data,
4996 data_t *peer_key_data,
4997 data_t *expected_output )
4998{
4999 psa_key_handle_t our_key = 0;
5000 psa_algorithm_t alg = alg_arg;
5001 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005003 unsigned char *output = NULL;
5004 size_t output_length = ~0;
5005
5006 ASSERT_ALLOC( output, expected_output->len );
5007 PSA_ASSERT( psa_crypto_init( ) );
5008
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005009 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5010 psa_set_key_algorithm( &attributes, alg );
5011 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005012 PSA_ASSERT( psa_import_key( &attributes,
5013 our_key_data->x, our_key_data->len,
5014 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005015
Gilles Peskinebe697d82019-05-16 18:00:41 +02005016 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5017 peer_key_data->x, peer_key_data->len,
5018 output, expected_output->len,
5019 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005020 ASSERT_COMPARE( output, output_length,
5021 expected_output->x, expected_output->len );
5022
5023exit:
5024 mbedtls_free( output );
5025 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005026 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005027}
5028/* END_CASE */
5029
5030/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005031void key_agreement_capacity( int alg_arg,
5032 int our_key_type_arg, data_t *our_key_data,
5033 data_t *peer_key_data,
5034 int expected_capacity_arg )
5035{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005036 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005037 psa_algorithm_t alg = alg_arg;
5038 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005039 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005041 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005042 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005043
Gilles Peskine8817f612018-12-18 00:18:46 +01005044 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005045
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005046 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5047 psa_set_key_algorithm( &attributes, alg );
5048 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005049 PSA_ASSERT( psa_import_key( &attributes,
5050 our_key_data->x, our_key_data->len,
5051 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005052
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005054 PSA_ASSERT( psa_key_derivation_key_agreement(
5055 &operation,
5056 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5057 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005058 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5059 {
5060 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005061 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005062 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005063 NULL, 0 ) );
5064 }
Gilles Peskine59685592018-09-18 12:11:34 +02005065
Gilles Peskinebf491972018-10-25 22:36:12 +02005066 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005067 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005068 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005069 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005070
Gilles Peskinebf491972018-10-25 22:36:12 +02005071 /* Test the actual capacity by reading the output. */
5072 while( actual_capacity > sizeof( output ) )
5073 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005074 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005075 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005076 actual_capacity -= sizeof( output );
5077 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005078 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005079 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005080 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005081 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005082
Gilles Peskine59685592018-09-18 12:11:34 +02005083exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005084 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005085 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005086 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005087}
5088/* END_CASE */
5089
5090/* BEGIN_CASE */
5091void key_agreement_output( int alg_arg,
5092 int our_key_type_arg, data_t *our_key_data,
5093 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005094 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005095{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005096 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005097 psa_algorithm_t alg = alg_arg;
5098 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005101 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005102
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005103 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5104 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005105
Gilles Peskine8817f612018-12-18 00:18:46 +01005106 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005107
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005108 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5109 psa_set_key_algorithm( &attributes, alg );
5110 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005111 PSA_ASSERT( psa_import_key( &attributes,
5112 our_key_data->x, our_key_data->len,
5113 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005114
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005116 PSA_ASSERT( psa_key_derivation_key_agreement(
5117 &operation,
5118 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5119 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005120 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5121 {
5122 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005123 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005124 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005125 NULL, 0 ) );
5126 }
Gilles Peskine59685592018-09-18 12:11:34 +02005127
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005129 actual_output,
5130 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005131 ASSERT_COMPARE( actual_output, expected_output1->len,
5132 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005133 if( expected_output2->len != 0 )
5134 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005136 actual_output,
5137 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005138 ASSERT_COMPARE( actual_output, expected_output2->len,
5139 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005140 }
Gilles Peskine59685592018-09-18 12:11:34 +02005141
5142exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005143 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005144 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005145 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005146 mbedtls_free( actual_output );
5147}
5148/* END_CASE */
5149
5150/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005151void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005152{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005153 size_t bytes = bytes_arg;
5154 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005155 unsigned char *output = NULL;
5156 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005157 size_t i;
5158 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005159
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005160 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5161 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005162 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005163
Gilles Peskine8817f612018-12-18 00:18:46 +01005164 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005165
Gilles Peskinea50d7392018-06-21 10:22:13 +02005166 /* Run several times, to ensure that every output byte will be
5167 * nonzero at least once with overwhelming probability
5168 * (2^(-8*number_of_runs)). */
5169 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005170 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005171 if( bytes != 0 )
5172 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005173 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005174
5175 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005176 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5177 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005178
5179 for( i = 0; i < bytes; i++ )
5180 {
5181 if( output[i] != 0 )
5182 ++changed[i];
5183 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005184 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005185
5186 /* Check that every byte was changed to nonzero at least once. This
5187 * validates that psa_generate_random is overwriting every byte of
5188 * the output buffer. */
5189 for( i = 0; i < bytes; i++ )
5190 {
5191 TEST_ASSERT( changed[i] != 0 );
5192 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005193
5194exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005195 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005196 mbedtls_free( output );
5197 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005198}
5199/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005200
5201/* BEGIN_CASE */
5202void generate_key( int type_arg,
5203 int bits_arg,
5204 int usage_arg,
5205 int alg_arg,
5206 int expected_status_arg )
5207{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005208 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005209 psa_key_type_t type = type_arg;
5210 psa_key_usage_t usage = usage_arg;
5211 size_t bits = bits_arg;
5212 psa_algorithm_t alg = alg_arg;
5213 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005215 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005216
Gilles Peskine8817f612018-12-18 00:18:46 +01005217 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005218
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005219 psa_set_key_usage_flags( &attributes, usage );
5220 psa_set_key_algorithm( &attributes, alg );
5221 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005222 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005223
5224 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005225 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005226 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005227 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005228
5229 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005230 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5231 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5232 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005233
Gilles Peskine818ca122018-06-20 18:16:48 +02005234 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005235 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005236 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005237
5238exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005239 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005240 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005241 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005242}
5243/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005244
Gilles Peskinee56e8782019-04-26 17:34:02 +02005245/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5246void generate_key_rsa( int bits_arg,
5247 data_t *e_arg,
5248 int expected_status_arg )
5249{
5250 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005251 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005252 size_t bits = bits_arg;
5253 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5254 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5255 psa_status_t expected_status = expected_status_arg;
5256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5257 uint8_t *exported = NULL;
5258 size_t exported_size =
5259 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5260 size_t exported_length = SIZE_MAX;
5261 uint8_t *e_read_buffer = NULL;
5262 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005263 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005264 size_t e_read_length = SIZE_MAX;
5265
5266 if( e_arg->len == 0 ||
5267 ( e_arg->len == 3 &&
5268 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5269 {
5270 is_default_public_exponent = 1;
5271 e_read_size = 0;
5272 }
5273 ASSERT_ALLOC( e_read_buffer, e_read_size );
5274 ASSERT_ALLOC( exported, exported_size );
5275
5276 PSA_ASSERT( psa_crypto_init( ) );
5277
5278 psa_set_key_usage_flags( &attributes, usage );
5279 psa_set_key_algorithm( &attributes, alg );
5280 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5281 e_arg->x, e_arg->len ) );
5282 psa_set_key_bits( &attributes, bits );
5283
5284 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005285 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005286 if( expected_status != PSA_SUCCESS )
5287 goto exit;
5288
5289 /* Test the key information */
5290 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5291 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5292 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5293 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5294 e_read_buffer, e_read_size,
5295 &e_read_length ) );
5296 if( is_default_public_exponent )
5297 TEST_EQUAL( e_read_length, 0 );
5298 else
5299 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5300
5301 /* Do something with the key according to its type and permitted usage. */
5302 if( ! exercise_key( handle, usage, alg ) )
5303 goto exit;
5304
5305 /* Export the key and check the public exponent. */
5306 PSA_ASSERT( psa_export_public_key( handle,
5307 exported, exported_size,
5308 &exported_length ) );
5309 {
5310 uint8_t *p = exported;
5311 uint8_t *end = exported + exported_length;
5312 size_t len;
5313 /* RSAPublicKey ::= SEQUENCE {
5314 * modulus INTEGER, -- n
5315 * publicExponent INTEGER } -- e
5316 */
5317 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005318 MBEDTLS_ASN1_SEQUENCE |
5319 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005320 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5321 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5322 MBEDTLS_ASN1_INTEGER ) );
5323 if( len >= 1 && p[0] == 0 )
5324 {
5325 ++p;
5326 --len;
5327 }
5328 if( e_arg->len == 0 )
5329 {
5330 TEST_EQUAL( len, 3 );
5331 TEST_EQUAL( p[0], 1 );
5332 TEST_EQUAL( p[1], 0 );
5333 TEST_EQUAL( p[2], 1 );
5334 }
5335 else
5336 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5337 }
5338
5339exit:
5340 psa_reset_key_attributes( &attributes );
5341 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005342 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005343 mbedtls_free( e_read_buffer );
5344 mbedtls_free( exported );
5345}
5346/* END_CASE */
5347
Darryl Greend49a4992018-06-18 17:27:26 +01005348/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005349void persistent_key_load_key_from_storage( data_t *data,
5350 int type_arg, int bits_arg,
5351 int usage_flags_arg, int alg_arg,
5352 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005353{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005354 psa_key_id_t key_id = 1;
5355 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005356 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005357 psa_key_handle_t base_key = 0;
5358 psa_key_type_t type = type_arg;
5359 size_t bits = bits_arg;
5360 psa_key_usage_t usage_flags = usage_flags_arg;
5361 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005362 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005363 unsigned char *first_export = NULL;
5364 unsigned char *second_export = NULL;
5365 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5366 size_t first_exported_length;
5367 size_t second_exported_length;
5368
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005369 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5370 {
5371 ASSERT_ALLOC( first_export, export_size );
5372 ASSERT_ALLOC( second_export, export_size );
5373 }
Darryl Greend49a4992018-06-18 17:27:26 +01005374
Gilles Peskine8817f612018-12-18 00:18:46 +01005375 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005376
Gilles Peskinec87af662019-05-15 16:12:22 +02005377 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005378 psa_set_key_usage_flags( &attributes, usage_flags );
5379 psa_set_key_algorithm( &attributes, alg );
5380 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005381 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005382
Darryl Green0c6575a2018-11-07 16:05:30 +00005383 switch( generation_method )
5384 {
5385 case IMPORT_KEY:
5386 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005387 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5388 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005389 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005390
Darryl Green0c6575a2018-11-07 16:05:30 +00005391 case GENERATE_KEY:
5392 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005393 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005394 break;
5395
5396 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005397 {
5398 /* Create base key */
5399 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5400 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5401 psa_set_key_usage_flags( &base_attributes,
5402 PSA_KEY_USAGE_DERIVE );
5403 psa_set_key_algorithm( &base_attributes, derive_alg );
5404 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005405 PSA_ASSERT( psa_import_key( &base_attributes,
5406 data->x, data->len,
5407 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005408 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005410 PSA_ASSERT( psa_key_derivation_input_key(
5411 &operation,
5412 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005413 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005414 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005415 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005416 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5417 &operation,
5418 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005419 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005420 PSA_ASSERT( psa_destroy_key( base_key ) );
5421 base_key = 0;
5422 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005423 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005424 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005425 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005426
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005427 /* Export the key if permitted by the key policy. */
5428 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5429 {
5430 PSA_ASSERT( psa_export_key( handle,
5431 first_export, export_size,
5432 &first_exported_length ) );
5433 if( generation_method == IMPORT_KEY )
5434 ASSERT_COMPARE( data->x, data->len,
5435 first_export, first_exported_length );
5436 }
Darryl Greend49a4992018-06-18 17:27:26 +01005437
5438 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005439 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005440 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005441 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005442
Darryl Greend49a4992018-06-18 17:27:26 +01005443 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005444 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005445 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5446 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5447 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5448 PSA_KEY_LIFETIME_PERSISTENT );
5449 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5450 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5451 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5452 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005453
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005454 /* Export the key again if permitted by the key policy. */
5455 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005456 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005457 PSA_ASSERT( psa_export_key( handle,
5458 second_export, export_size,
5459 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005460 ASSERT_COMPARE( first_export, first_exported_length,
5461 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005462 }
5463
5464 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005465 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005466 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005467
5468exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005469 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005470 mbedtls_free( first_export );
5471 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005472 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005473 psa_destroy_key( base_key );
5474 if( handle == 0 )
5475 {
5476 /* In case there was a test failure after creating the persistent key
5477 * but while it was not open, try to re-open the persistent key
5478 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005479 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005480 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005481 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005482 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005483}
5484/* END_CASE */