blob: fc563cb15d8c24d3ceb0544902cd38ae82efec7e [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
Ronald Cron02c78b72020-05-27 09:22:32 +020012#include "test/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;
Ronald Cron71016a92020-08-28 19:01:50 +0200236 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100237 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 )
Ronald Cronecfb2372020-07-23 17:13:42 +0200248 TEST_ASSERT( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) == 0 );
Gilles Peskine667c1112019-12-03 19:03:20 +0100249 else
250 {
251 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200252 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
253 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100254 }
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 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200964 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
965 {
966 /* The representation of an ECC Montgomery public key is
967 * the raw compressed point */
968 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
969 }
970 else
971 {
972 /* The representation of an ECC Weierstrass public key is:
973 * - The byte 0x04;
974 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
975 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
976 * - where m is the bit size associated with the curve.
977 */
978 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
979 TEST_EQUAL( p[0], 4 );
980 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200981 }
982 else
983#endif /* MBEDTLS_ECP_C */
984 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100985 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200986 mbedtls_snprintf( message, sizeof( message ),
987 "No sanity check for public key type=0x%08lx",
988 (unsigned long) type );
989 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200990 (void) p;
991 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200992 return( 0 );
993 }
994 }
995 else
996
997 {
998 /* No sanity checks for other types */
999 }
1000
1001 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001002
1003exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001004 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001005}
1006
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001007static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +02001008 psa_key_usage_t usage )
1009{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001011 uint8_t *exported = NULL;
1012 size_t exported_size = 0;
1013 size_t exported_length = 0;
1014 int ok = 0;
1015
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001017
1018 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001019 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001020 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001021 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
1022 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001023 ok = 1;
1024 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001025 }
1026
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001027 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
1028 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001029 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001030
Gilles Peskine8817f612018-12-18 00:18:46 +01001031 PSA_ASSERT( psa_export_key( handle,
1032 exported, exported_size,
1033 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001034 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1035 psa_get_key_bits( &attributes ),
1036 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001037
1038exit:
1039 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001040 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001041 return( ok );
1042}
1043
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001044static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +02001045{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001047 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001048 uint8_t *exported = NULL;
1049 size_t exported_size = 0;
1050 size_t exported_length = 0;
1051 int ok = 0;
1052
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001053 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1054 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001055 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001056 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001057 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +02001058 return( 1 );
1059 }
1060
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001061 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001062 psa_get_key_type( &attributes ) );
1063 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
1064 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001065 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001066
Gilles Peskine8817f612018-12-18 00:18:46 +01001067 PSA_ASSERT( psa_export_public_key( handle,
1068 exported, exported_size,
1069 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001070 ok = exported_key_sanity_check( public_type,
1071 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001072 exported, exported_length );
1073
1074exit:
1075 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001076 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001077 return( ok );
1078}
1079
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001080/** Do smoke tests on a key.
1081 *
1082 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1083 * sign/verify, or derivation) that is permitted according to \p usage.
1084 * \p usage and \p alg should correspond to the expected policy on the
1085 * key.
1086 *
1087 * Export the key if permitted by \p usage, and check that the output
1088 * looks sensible. If \p usage forbids export, check that
1089 * \p psa_export_key correctly rejects the attempt. If the key is
1090 * asymmetric, also check \p psa_export_public_key.
1091 *
1092 * If the key fails the tests, this function calls the test framework's
1093 * `test_fail` function and returns false. Otherwise this function returns
1094 * true. Therefore it should be used as follows:
1095 * ```
1096 * if( ! exercise_key( ... ) ) goto exit;
1097 * ```
1098 *
1099 * \param handle The key to exercise. It should be capable of performing
1100 * \p alg.
1101 * \param usage The usage flags to assume.
1102 * \param alg The algorithm to exercise.
1103 *
1104 * \retval 0 The key failed the smoke tests.
1105 * \retval 1 The key passed the smoke tests.
1106 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001107static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001108 psa_key_usage_t usage,
1109 psa_algorithm_t alg )
1110{
1111 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001112
1113 if( ! check_key_attributes_sanity( handle ) )
1114 return( 0 );
1115
Gilles Peskine02b75072018-07-01 22:31:34 +02001116 if( alg == 0 )
1117 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1118 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001119 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001120 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001121 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001122 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001123 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001124 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001125 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001126 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001127 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001128 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001129 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001130 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1131 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001132 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001133 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001134 else
1135 {
1136 char message[40];
1137 mbedtls_snprintf( message, sizeof( message ),
1138 "No code to exercise alg=0x%08lx",
1139 (unsigned long) alg );
1140 test_fail( message, __LINE__, __FILE__ );
1141 ok = 0;
1142 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001143
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001144 ok = ok && exercise_export_key( handle, usage );
1145 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001146
Gilles Peskine02b75072018-07-01 22:31:34 +02001147 return( ok );
1148}
1149
Gilles Peskine10df3412018-10-25 22:35:43 +02001150static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1151 psa_algorithm_t alg )
1152{
1153 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1154 {
1155 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001156 PSA_KEY_USAGE_VERIFY_HASH :
1157 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001158 }
1159 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1160 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1161 {
1162 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1163 PSA_KEY_USAGE_ENCRYPT :
1164 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1165 }
1166 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1167 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1168 {
1169 return( PSA_KEY_USAGE_DERIVE );
1170 }
1171 else
1172 {
1173 return( 0 );
1174 }
1175
1176}
Darryl Green0c6575a2018-11-07 16:05:30 +00001177
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001178static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1179{
1180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001181 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001182 uint8_t buffer[1];
1183 size_t length;
1184 int ok = 0;
1185
Ronald Cronecfb2372020-07-23 17:13:42 +02001186 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001187 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1188 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1189 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1190 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1191 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +02001192 TEST_EQUAL(
1193 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1194 TEST_EQUAL(
1195 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001196 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001197 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1198 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1199 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1200 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1201
1202 TEST_EQUAL( psa_export_key( handle,
1203 buffer, sizeof( buffer ), &length ),
1204 PSA_ERROR_INVALID_HANDLE );
1205 TEST_EQUAL( psa_export_public_key( handle,
1206 buffer, sizeof( buffer ), &length ),
1207 PSA_ERROR_INVALID_HANDLE );
1208
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001209 ok = 1;
1210
1211exit:
1212 psa_reset_key_attributes( &attributes );
1213 return( ok );
1214}
1215
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001216/* Assert that a key isn't reported as having a slot number. */
1217#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1218#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1219 do \
1220 { \
1221 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1222 TEST_EQUAL( psa_get_key_slot_number( \
1223 attributes, \
1224 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1225 PSA_ERROR_INVALID_ARGUMENT ); \
1226 } \
1227 while( 0 )
1228#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1229#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1230 ( (void) 0 )
1231#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1232
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001233/* An overapproximation of the amount of storage needed for a key of the
1234 * given type and with the given content. The API doesn't make it easy
1235 * to find a good value for the size. The current implementation doesn't
1236 * care about the value anyway. */
1237#define KEY_BITS_FROM_DATA( type, data ) \
1238 ( data )->len
1239
Darryl Green0c6575a2018-11-07 16:05:30 +00001240typedef enum {
1241 IMPORT_KEY = 0,
1242 GENERATE_KEY = 1,
1243 DERIVE_KEY = 2
1244} generate_method;
1245
Gilles Peskinee59236f2018-01-27 23:32:46 +01001246/* END_HEADER */
1247
1248/* BEGIN_DEPENDENCIES
1249 * depends_on:MBEDTLS_PSA_CRYPTO_C
1250 * END_DEPENDENCIES
1251 */
1252
1253/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001254void static_checks( )
1255{
1256 size_t max_truncated_mac_size =
1257 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1258
1259 /* Check that the length for a truncated MAC always fits in the algorithm
1260 * encoding. The shifted mask is the maximum truncated value. The
1261 * untruncated algorithm may be one byte larger. */
1262 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001263
1264#if defined(MBEDTLS_TEST_DEPRECATED)
1265 /* Check deprecated constants. */
1266 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1267 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1268 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1269 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1270 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1271 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1272 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1273 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001274
Paul Elliott8ff510a2020-06-02 17:19:28 +01001275 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1276 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1277 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1278 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1279 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1280 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1281 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1282 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1283 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1284 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1285 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1286 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1287 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1288 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1289 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1290 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1291 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1292 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1293 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1294 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1295 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1296 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1297 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1298 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1299 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1300 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1301 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1302 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1303 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1304 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1305
1306 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1308 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1309 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1310 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1311 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1312 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001314
Paul Elliott75e27032020-06-03 15:17:39 +01001315 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1316 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1317 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1318 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1319 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1320
1321 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1322 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001323#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001324}
1325/* END_CASE */
1326
1327/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001328void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001329 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001330 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001331{
Gilles Peskine4747d192019-04-17 15:05:45 +02001332 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001333 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001334 psa_key_lifetime_t lifetime = lifetime_arg;
1335 psa_key_usage_t usage_flags = usage_flags_arg;
1336 psa_algorithm_t alg = alg_arg;
1337 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001338 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001339
Ronald Cronecfb2372020-07-23 17:13:42 +02001340 TEST_EQUAL(
1341 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1342 TEST_EQUAL(
1343 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001344 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1345 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1346 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1347 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001348 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001349
Gilles Peskinec87af662019-05-15 16:12:22 +02001350 psa_set_key_id( &attributes, id );
1351 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001352 psa_set_key_usage_flags( &attributes, usage_flags );
1353 psa_set_key_algorithm( &attributes, alg );
1354 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001355 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001356
Ronald Cronecfb2372020-07-23 17:13:42 +02001357 TEST_ASSERT( mbedtls_svc_key_id_equal(
1358 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001359 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1360 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1361 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1362 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001363 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001364
1365 psa_reset_key_attributes( &attributes );
1366
Ronald Cronecfb2372020-07-23 17:13:42 +02001367 TEST_EQUAL(
1368 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1369 TEST_EQUAL(
1370 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001371 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1372 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1373 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1374 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001375 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001376}
1377/* END_CASE */
1378
1379/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001380void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1381 int id2_arg, int owner_id2_arg,
1382 int expected_id_arg, int expected_owner_id_arg,
1383 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001384{
1385 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001386 mbedtls_svc_key_id_t id1 =
1387 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001388 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001389 mbedtls_svc_key_id_t id2 =
1390 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001391 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001392 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001393 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1394
1395 if( id1_arg != -1 )
1396 psa_set_key_id( &attributes, id1 );
1397 if( lifetime_arg != -1 )
1398 psa_set_key_lifetime( &attributes, lifetime );
1399 if( id2_arg != -1 )
1400 psa_set_key_id( &attributes, id2 );
1401
Ronald Cronecfb2372020-07-23 17:13:42 +02001402 TEST_ASSERT( mbedtls_svc_key_id_equal(
1403 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001404 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1405}
1406/* END_CASE */
1407
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001408/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1409void slot_number_attribute( )
1410{
1411 psa_key_slot_number_t slot_number = 0xdeadbeef;
1412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1413
1414 /* Initially, there is no slot number. */
1415 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1416 PSA_ERROR_INVALID_ARGUMENT );
1417
1418 /* Test setting a slot number. */
1419 psa_set_key_slot_number( &attributes, 0 );
1420 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1421 TEST_EQUAL( slot_number, 0 );
1422
1423 /* Test changing the slot number. */
1424 psa_set_key_slot_number( &attributes, 42 );
1425 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1426 TEST_EQUAL( slot_number, 42 );
1427
1428 /* Test clearing the slot number. */
1429 psa_clear_key_slot_number( &attributes );
1430 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1431 PSA_ERROR_INVALID_ARGUMENT );
1432
1433 /* Clearing again should have no effect. */
1434 psa_clear_key_slot_number( &attributes );
1435 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1436 PSA_ERROR_INVALID_ARGUMENT );
1437
1438 /* Test that reset clears the slot number. */
1439 psa_set_key_slot_number( &attributes, 42 );
1440 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1441 TEST_EQUAL( slot_number, 42 );
1442 psa_reset_key_attributes( &attributes );
1443 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1444 PSA_ERROR_INVALID_ARGUMENT );
1445}
1446/* END_CASE */
1447
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001448/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001449void import_with_policy( int type_arg,
1450 int usage_arg, int alg_arg,
1451 int expected_status_arg )
1452{
1453 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1454 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1455 psa_key_handle_t handle = 0;
1456 psa_key_type_t type = type_arg;
1457 psa_key_usage_t usage = usage_arg;
1458 psa_algorithm_t alg = alg_arg;
1459 psa_status_t expected_status = expected_status_arg;
1460 const uint8_t key_material[16] = {0};
1461 psa_status_t status;
1462
1463 PSA_ASSERT( psa_crypto_init( ) );
1464
1465 psa_set_key_type( &attributes, type );
1466 psa_set_key_usage_flags( &attributes, usage );
1467 psa_set_key_algorithm( &attributes, alg );
1468
1469 status = psa_import_key( &attributes,
1470 key_material, sizeof( key_material ),
1471 &handle );
1472 TEST_EQUAL( status, expected_status );
1473 if( status != PSA_SUCCESS )
1474 goto exit;
1475
1476 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1477 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1478 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1479 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001480 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001481
1482 PSA_ASSERT( psa_destroy_key( handle ) );
1483 test_operations_on_invalid_handle( handle );
1484
1485exit:
1486 psa_destroy_key( handle );
1487 psa_reset_key_attributes( &got_attributes );
1488 PSA_DONE( );
1489}
1490/* END_CASE */
1491
1492/* BEGIN_CASE */
1493void import_with_data( data_t *data, int type_arg,
1494 int attr_bits_arg,
1495 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001496{
1497 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1498 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001499 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001500 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001501 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001502 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001504
Gilles Peskine8817f612018-12-18 00:18:46 +01001505 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001506
Gilles Peskine4747d192019-04-17 15:05:45 +02001507 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001508 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001509
Gilles Peskine73676cb2019-05-15 20:15:10 +02001510 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001511 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001512 if( status != PSA_SUCCESS )
1513 goto exit;
1514
1515 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1516 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001517 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001518 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001519 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001520
1521 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001522 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001523
1524exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001525 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001526 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001527 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001528}
1529/* END_CASE */
1530
1531/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001532void import_large_key( int type_arg, int byte_size_arg,
1533 int expected_status_arg )
1534{
1535 psa_key_type_t type = type_arg;
1536 size_t byte_size = byte_size_arg;
1537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1538 psa_status_t expected_status = expected_status_arg;
1539 psa_key_handle_t handle = 0;
1540 psa_status_t status;
1541 uint8_t *buffer = NULL;
1542 size_t buffer_size = byte_size + 1;
1543 size_t n;
1544
1545 /* It would be better to skip the test than fail it if the allocation
1546 * fails, but the test framework doesn't support this yet. */
1547 ASSERT_ALLOC( buffer, buffer_size );
1548 memset( buffer, 'K', byte_size );
1549
1550 PSA_ASSERT( psa_crypto_init( ) );
1551
1552 /* Try importing the key */
1553 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1554 psa_set_key_type( &attributes, type );
1555 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1556 TEST_EQUAL( status, expected_status );
1557
1558 if( status == PSA_SUCCESS )
1559 {
1560 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1561 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1562 TEST_EQUAL( psa_get_key_bits( &attributes ),
1563 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001564 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001565 memset( buffer, 0, byte_size + 1 );
1566 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1567 for( n = 0; n < byte_size; n++ )
1568 TEST_EQUAL( buffer[n], 'K' );
1569 for( n = byte_size; n < buffer_size; n++ )
1570 TEST_EQUAL( buffer[n], 0 );
1571 }
1572
1573exit:
1574 psa_destroy_key( handle );
1575 PSA_DONE( );
1576 mbedtls_free( buffer );
1577}
1578/* END_CASE */
1579
1580/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001581void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1582{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001583 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001584 size_t bits = bits_arg;
1585 psa_status_t expected_status = expected_status_arg;
1586 psa_status_t status;
1587 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001588 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001589 size_t buffer_size = /* Slight overapproximations */
1590 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001591 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001592 unsigned char *p;
1593 int ret;
1594 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001595 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001596
Gilles Peskine8817f612018-12-18 00:18:46 +01001597 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001598 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001599
1600 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1601 bits, keypair ) ) >= 0 );
1602 length = ret;
1603
1604 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001605 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001606 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001607 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001608
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001609 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001611
1612exit:
1613 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001614 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001615}
1616/* END_CASE */
1617
1618/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001619void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001620 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001621 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001622 int expected_bits,
1623 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001624 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001625 int canonical_input )
1626{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001627 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001628 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001629 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001630 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001631 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001632 unsigned char *exported = NULL;
1633 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001634 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001635 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001636 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001637 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001638 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001639
Moran Pekercb088e72018-07-17 17:36:59 +03001640 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001641 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001642 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001643 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001644 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001645
Gilles Peskine4747d192019-04-17 15:05:45 +02001646 psa_set_key_usage_flags( &attributes, usage_arg );
1647 psa_set_key_algorithm( &attributes, alg );
1648 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001649
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001650 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001651 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001652
1653 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001654 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1655 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1656 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001657 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001658
1659 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001660 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001661 exported, export_size,
1662 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001663 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001664
1665 /* The exported length must be set by psa_export_key() to a value between 0
1666 * and export_size. On errors, the exported length must be 0. */
1667 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1668 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1669 TEST_ASSERT( exported_length <= export_size );
1670
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001671 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001672 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001673 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001674 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001675 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001676 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001677 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001678
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001679 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001680 goto exit;
1681
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001682 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001683 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001684 else
1685 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001686 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001687 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1688 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001689 PSA_ASSERT( psa_export_key( handle2,
1690 reexported,
1691 export_size,
1692 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001693 ASSERT_COMPARE( exported, exported_length,
1694 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001695 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001696 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001697 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001698
1699destroy:
1700 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001701 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001702 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001703
1704exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001705 mbedtls_free( exported );
1706 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001707 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001708 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001709}
1710/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001711
Moran Pekerf709f4a2018-06-06 17:26:04 +03001712/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001713void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001714 int type_arg,
1715 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001716 int export_size_delta,
1717 int expected_export_status_arg,
1718 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001719{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001720 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001721 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001722 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001723 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001724 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001725 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001726 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001727 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001729
Gilles Peskine8817f612018-12-18 00:18:46 +01001730 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001731
Gilles Peskine4747d192019-04-17 15:05:45 +02001732 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1733 psa_set_key_algorithm( &attributes, alg );
1734 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001735
1736 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001737 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001738
Gilles Peskine49c25912018-10-29 15:15:31 +01001739 /* Export the public key */
1740 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001741 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001742 exported, export_size,
1743 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001744 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001745 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001746 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001747 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001748 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001749 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1750 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001751 TEST_ASSERT( expected_public_key->len <=
1752 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001753 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1754 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001755 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001756
1757exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001758 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001759 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001760 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001761 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001762}
1763/* END_CASE */
1764
Gilles Peskine20035e32018-02-03 22:44:14 +01001765/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001766void import_and_exercise_key( data_t *data,
1767 int type_arg,
1768 int bits_arg,
1769 int alg_arg )
1770{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001771 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001772 psa_key_type_t type = type_arg;
1773 size_t bits = bits_arg;
1774 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001775 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001777 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001778
Gilles Peskine8817f612018-12-18 00:18:46 +01001779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001780
Gilles Peskine4747d192019-04-17 15:05:45 +02001781 psa_set_key_usage_flags( &attributes, usage );
1782 psa_set_key_algorithm( &attributes, alg );
1783 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001784
1785 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001786 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001787
1788 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001789 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1790 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1791 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001792
1793 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001794 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001795 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001796
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001797 PSA_ASSERT( psa_destroy_key( handle ) );
1798 test_operations_on_invalid_handle( handle );
1799
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001800exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001801 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001802 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001803 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001804}
1805/* END_CASE */
1806
1807/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001808void effective_key_attributes( int type_arg, int expected_type_arg,
1809 int bits_arg, int expected_bits_arg,
1810 int usage_arg, int expected_usage_arg,
1811 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001812{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001813 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001814 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001815 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001816 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001817 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001818 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001819 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001820 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001821 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001823
Gilles Peskine8817f612018-12-18 00:18:46 +01001824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001825
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001826 psa_set_key_usage_flags( &attributes, usage );
1827 psa_set_key_algorithm( &attributes, alg );
1828 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001829 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001830
Gilles Peskine1a960492019-11-26 17:12:21 +01001831 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1832 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001833
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001834 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001835 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1836 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1837 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1838 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001839
1840exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001841 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001842 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001843 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001844}
1845/* END_CASE */
1846
1847/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001848void check_key_policy( int type_arg, int bits_arg,
1849 int usage_arg, int alg_arg )
1850{
1851 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1852 usage_arg, usage_arg, alg_arg, alg_arg );
1853 goto exit;
1854}
1855/* END_CASE */
1856
1857/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001858void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001859{
1860 /* Test each valid way of initializing the object, except for `= {0}`, as
1861 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1862 * though it's OK by the C standard. We could test for this, but we'd need
1863 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001864 psa_key_attributes_t func = psa_key_attributes_init( );
1865 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1866 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001867
1868 memset( &zero, 0, sizeof( zero ) );
1869
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001870 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1871 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1872 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001873
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001874 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1875 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1876 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1877
1878 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1879 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1880 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1881
1882 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1883 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1884 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1885
1886 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1887 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1888 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001889}
1890/* END_CASE */
1891
1892/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001893void mac_key_policy( int policy_usage,
1894 int policy_alg,
1895 int key_type,
1896 data_t *key_data,
1897 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001898{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001899 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001900 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001901 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001902 psa_status_t status;
1903 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001904
Gilles Peskine8817f612018-12-18 00:18:46 +01001905 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001906
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001907 psa_set_key_usage_flags( &attributes, policy_usage );
1908 psa_set_key_algorithm( &attributes, policy_alg );
1909 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001910
Gilles Peskine049c7532019-05-15 20:22:09 +02001911 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1912 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001913
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001914 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001915 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001916 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001917 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001919 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001921
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001923 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001925 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001926 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001927 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001928 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001929
1930exit:
1931 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001932 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001933 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934}
1935/* END_CASE */
1936
1937/* BEGIN_CASE */
1938void cipher_key_policy( int policy_usage,
1939 int policy_alg,
1940 int key_type,
1941 data_t *key_data,
1942 int exercise_alg )
1943{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001944 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001945 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001946 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001947 psa_status_t status;
1948
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001950
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001951 psa_set_key_usage_flags( &attributes, policy_usage );
1952 psa_set_key_algorithm( &attributes, policy_alg );
1953 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001954
Gilles Peskine049c7532019-05-15 20:22:09 +02001955 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1956 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001957
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001958 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001959 if( policy_alg == exercise_alg &&
1960 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001961 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001962 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001963 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001964 psa_cipher_abort( &operation );
1965
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001966 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001967 if( policy_alg == exercise_alg &&
1968 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001970 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001971 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001972
1973exit:
1974 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001975 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001976 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977}
1978/* END_CASE */
1979
1980/* BEGIN_CASE */
1981void aead_key_policy( int policy_usage,
1982 int policy_alg,
1983 int key_type,
1984 data_t *key_data,
1985 int nonce_length_arg,
1986 int tag_length_arg,
1987 int exercise_alg )
1988{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001989 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001991 psa_status_t status;
1992 unsigned char nonce[16] = {0};
1993 size_t nonce_length = nonce_length_arg;
1994 unsigned char tag[16];
1995 size_t tag_length = tag_length_arg;
1996 size_t output_length;
1997
1998 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1999 TEST_ASSERT( tag_length <= sizeof( tag ) );
2000
Gilles Peskine8817f612018-12-18 00:18:46 +01002001 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002003 psa_set_key_usage_flags( &attributes, policy_usage );
2004 psa_set_key_algorithm( &attributes, policy_alg );
2005 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006
Gilles Peskine049c7532019-05-15 20:22:09 +02002007 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2008 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002009
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002010 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002011 nonce, nonce_length,
2012 NULL, 0,
2013 NULL, 0,
2014 tag, tag_length,
2015 &output_length );
2016 if( policy_alg == exercise_alg &&
2017 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002018 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002020 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021
2022 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002023 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 nonce, nonce_length,
2025 NULL, 0,
2026 tag, tag_length,
2027 NULL, 0,
2028 &output_length );
2029 if( policy_alg == exercise_alg &&
2030 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002031 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002033 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034
2035exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002036 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002037 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038}
2039/* END_CASE */
2040
2041/* BEGIN_CASE */
2042void asymmetric_encryption_key_policy( int policy_usage,
2043 int policy_alg,
2044 int key_type,
2045 data_t *key_data,
2046 int exercise_alg )
2047{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002048 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002049 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050 psa_status_t status;
2051 size_t key_bits;
2052 size_t buffer_length;
2053 unsigned char *buffer = NULL;
2054 size_t output_length;
2055
Gilles Peskine8817f612018-12-18 00:18:46 +01002056 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002057
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002058 psa_set_key_usage_flags( &attributes, policy_usage );
2059 psa_set_key_algorithm( &attributes, policy_alg );
2060 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061
Gilles Peskine049c7532019-05-15 20:22:09 +02002062 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2063 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002064
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002065 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
2066 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002067 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2068 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002069 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002071 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002072 NULL, 0,
2073 NULL, 0,
2074 buffer, buffer_length,
2075 &output_length );
2076 if( policy_alg == exercise_alg &&
2077 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002078 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002080 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002082 if( buffer_length != 0 )
2083 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002084 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002085 buffer, buffer_length,
2086 NULL, 0,
2087 buffer, buffer_length,
2088 &output_length );
2089 if( policy_alg == exercise_alg &&
2090 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002091 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002092 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002093 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094
2095exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002096 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002097 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002098 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099 mbedtls_free( buffer );
2100}
2101/* END_CASE */
2102
2103/* BEGIN_CASE */
2104void asymmetric_signature_key_policy( int policy_usage,
2105 int policy_alg,
2106 int key_type,
2107 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002108 int exercise_alg,
2109 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002110{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002111 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002114 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2115 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2116 * compatible with the policy and `payload_length_arg` is supposed to be
2117 * a valid input length to sign. If `payload_length_arg <= 0`,
2118 * `exercise_alg` is supposed to be forbidden by the policy. */
2119 int compatible_alg = payload_length_arg > 0;
2120 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002121 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122 size_t signature_length;
2123
Gilles Peskine8817f612018-12-18 00:18:46 +01002124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002126 psa_set_key_usage_flags( &attributes, policy_usage );
2127 psa_set_key_algorithm( &attributes, policy_alg );
2128 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002129
Gilles Peskine049c7532019-05-15 20:22:09 +02002130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2131 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002133 status = psa_sign_hash( handle, exercise_alg,
2134 payload, payload_length,
2135 signature, sizeof( signature ),
2136 &signature_length );
2137 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002138 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002139 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002140 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002141
2142 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002143 status = psa_verify_hash( handle, exercise_alg,
2144 payload, payload_length,
2145 signature, sizeof( signature ) );
2146 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002147 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002148 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002149 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002150
2151exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002152 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002153 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002154}
2155/* END_CASE */
2156
Janos Follathba3fab92019-06-11 14:50:16 +01002157/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002158void derive_key_policy( int policy_usage,
2159 int policy_alg,
2160 int key_type,
2161 data_t *key_data,
2162 int exercise_alg )
2163{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002164 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002166 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002167 psa_status_t status;
2168
Gilles Peskine8817f612018-12-18 00:18:46 +01002169 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002171 psa_set_key_usage_flags( &attributes, policy_usage );
2172 psa_set_key_algorithm( &attributes, policy_alg );
2173 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002174
Gilles Peskine049c7532019-05-15 20:22:09 +02002175 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2176 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002177
Janos Follathba3fab92019-06-11 14:50:16 +01002178 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2179
2180 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2181 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002182 {
Janos Follathba3fab92019-06-11 14:50:16 +01002183 PSA_ASSERT( psa_key_derivation_input_bytes(
2184 &operation,
2185 PSA_KEY_DERIVATION_INPUT_SEED,
2186 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002187 }
Janos Follathba3fab92019-06-11 14:50:16 +01002188
2189 status = psa_key_derivation_input_key( &operation,
2190 PSA_KEY_DERIVATION_INPUT_SECRET,
2191 handle );
2192
Gilles Peskineea0fb492018-07-12 17:17:20 +02002193 if( policy_alg == exercise_alg &&
2194 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002195 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002196 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002197 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002198
2199exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002200 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002201 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002202 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002203}
2204/* END_CASE */
2205
2206/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002207void agreement_key_policy( int policy_usage,
2208 int policy_alg,
2209 int key_type_arg,
2210 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002211 int exercise_alg,
2212 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002213{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002214 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002216 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002217 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002218 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002219 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002220
Gilles Peskine8817f612018-12-18 00:18:46 +01002221 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002222
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002223 psa_set_key_usage_flags( &attributes, policy_usage );
2224 psa_set_key_algorithm( &attributes, policy_alg );
2225 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002226
Gilles Peskine049c7532019-05-15 20:22:09 +02002227 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2228 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002229
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002230 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2231 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002232
Steven Cooremance48e852020-10-05 16:02:45 +02002233 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002234
2235exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002236 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002237 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002238 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002239}
2240/* END_CASE */
2241
2242/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002243void key_policy_alg2( int key_type_arg, data_t *key_data,
2244 int usage_arg, int alg_arg, int alg2_arg )
2245{
2246 psa_key_handle_t handle = 0;
2247 psa_key_type_t key_type = key_type_arg;
2248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2249 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2250 psa_key_usage_t usage = usage_arg;
2251 psa_algorithm_t alg = alg_arg;
2252 psa_algorithm_t alg2 = alg2_arg;
2253
2254 PSA_ASSERT( psa_crypto_init( ) );
2255
2256 psa_set_key_usage_flags( &attributes, usage );
2257 psa_set_key_algorithm( &attributes, alg );
2258 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2259 psa_set_key_type( &attributes, key_type );
2260 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2261 &handle ) );
2262
2263 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2264 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2265 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2266 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2267
2268 if( ! exercise_key( handle, usage, alg ) )
2269 goto exit;
2270 if( ! exercise_key( handle, usage, alg2 ) )
2271 goto exit;
2272
2273exit:
2274 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002275 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002276}
2277/* END_CASE */
2278
2279/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002280void raw_agreement_key_policy( int policy_usage,
2281 int policy_alg,
2282 int key_type_arg,
2283 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002284 int exercise_alg,
2285 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002286{
2287 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002289 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002290 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002291 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002292 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002293
2294 PSA_ASSERT( psa_crypto_init( ) );
2295
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002296 psa_set_key_usage_flags( &attributes, policy_usage );
2297 psa_set_key_algorithm( &attributes, policy_alg );
2298 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002299
Gilles Peskine049c7532019-05-15 20:22:09 +02002300 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2301 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002302
2303 status = raw_key_agreement_with_self( exercise_alg, handle );
2304
Steven Cooremance48e852020-10-05 16:02:45 +02002305 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002306
2307exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002308 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002309 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002310 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002311}
2312/* END_CASE */
2313
2314/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002315void copy_success( int source_usage_arg,
2316 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002317 int type_arg, data_t *material,
2318 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002319 int target_usage_arg,
2320 int target_alg_arg, int target_alg2_arg,
2321 int expected_usage_arg,
2322 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002323{
Gilles Peskineca25db92019-04-19 11:43:08 +02002324 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2325 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002326 psa_key_usage_t expected_usage = expected_usage_arg;
2327 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002328 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002329 psa_key_handle_t source_handle = 0;
2330 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002331 uint8_t *export_buffer = NULL;
2332
Gilles Peskine57ab7212019-01-28 13:03:09 +01002333 PSA_ASSERT( psa_crypto_init( ) );
2334
Gilles Peskineca25db92019-04-19 11:43:08 +02002335 /* Prepare the source key. */
2336 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2337 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002338 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002339 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002340 PSA_ASSERT( psa_import_key( &source_attributes,
2341 material->x, material->len,
2342 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002343 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002344
Gilles Peskineca25db92019-04-19 11:43:08 +02002345 /* Prepare the target attributes. */
2346 if( copy_attributes )
2347 target_attributes = source_attributes;
2348 if( target_usage_arg != -1 )
2349 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2350 if( target_alg_arg != -1 )
2351 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002352 if( target_alg2_arg != -1 )
2353 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002354
2355 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002356 PSA_ASSERT( psa_copy_key( source_handle,
2357 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002358
2359 /* Destroy the source to ensure that this doesn't affect the target. */
2360 PSA_ASSERT( psa_destroy_key( source_handle ) );
2361
2362 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002363 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2364 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2365 psa_get_key_type( &target_attributes ) );
2366 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2367 psa_get_key_bits( &target_attributes ) );
2368 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2369 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002370 TEST_EQUAL( expected_alg2,
2371 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002372 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2373 {
2374 size_t length;
2375 ASSERT_ALLOC( export_buffer, material->len );
2376 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2377 material->len, &length ) );
2378 ASSERT_COMPARE( material->x, material->len,
2379 export_buffer, length );
2380 }
2381 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2382 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002383 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2384 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002385
2386 PSA_ASSERT( psa_close_key( target_handle ) );
2387
2388exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002389 psa_reset_key_attributes( &source_attributes );
2390 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002391 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002392 mbedtls_free( export_buffer );
2393}
2394/* END_CASE */
2395
2396/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002397void copy_fail( int source_usage_arg,
2398 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002399 int type_arg, data_t *material,
2400 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002401 int target_usage_arg,
2402 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002403 int expected_status_arg )
2404{
2405 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2406 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2407 psa_key_handle_t source_handle = 0;
2408 psa_key_handle_t target_handle = 0;
2409
2410 PSA_ASSERT( psa_crypto_init( ) );
2411
2412 /* Prepare the source key. */
2413 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2414 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002415 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002416 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002417 PSA_ASSERT( psa_import_key( &source_attributes,
2418 material->x, material->len,
2419 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002420
2421 /* Prepare the target attributes. */
2422 psa_set_key_type( &target_attributes, target_type_arg );
2423 psa_set_key_bits( &target_attributes, target_bits_arg );
2424 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2425 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002426 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002427
2428 /* Try to copy the key. */
2429 TEST_EQUAL( psa_copy_key( source_handle,
2430 &target_attributes, &target_handle ),
2431 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002432
2433 PSA_ASSERT( psa_destroy_key( source_handle ) );
2434
Gilles Peskine4a644642019-05-03 17:14:08 +02002435exit:
2436 psa_reset_key_attributes( &source_attributes );
2437 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002438 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002439}
2440/* END_CASE */
2441
2442/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002443void hash_operation_init( )
2444{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002445 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002446 /* Test each valid way of initializing the object, except for `= {0}`, as
2447 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2448 * though it's OK by the C standard. We could test for this, but we'd need
2449 * to supress the Clang warning for the test. */
2450 psa_hash_operation_t func = psa_hash_operation_init( );
2451 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2452 psa_hash_operation_t zero;
2453
2454 memset( &zero, 0, sizeof( zero ) );
2455
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002456 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002457 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2458 PSA_ERROR_BAD_STATE );
2459 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2460 PSA_ERROR_BAD_STATE );
2461 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2462 PSA_ERROR_BAD_STATE );
2463
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002464 /* A default hash operation should be abortable without error. */
2465 PSA_ASSERT( psa_hash_abort( &func ) );
2466 PSA_ASSERT( psa_hash_abort( &init ) );
2467 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002468}
2469/* END_CASE */
2470
2471/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002472void hash_setup( int alg_arg,
2473 int expected_status_arg )
2474{
2475 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002476 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002477 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002478 psa_status_t status;
2479
Gilles Peskine8817f612018-12-18 00:18:46 +01002480 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002481
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002482 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002483 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002484
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002485 /* Whether setup succeeded or failed, abort must succeed. */
2486 PSA_ASSERT( psa_hash_abort( &operation ) );
2487
2488 /* If setup failed, reproduce the failure, so as to
2489 * test the resulting state of the operation object. */
2490 if( status != PSA_SUCCESS )
2491 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2492
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002493 /* Now the operation object should be reusable. */
2494#if defined(KNOWN_SUPPORTED_HASH_ALG)
2495 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2496 PSA_ASSERT( psa_hash_abort( &operation ) );
2497#endif
2498
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002499exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002500 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002501}
2502/* END_CASE */
2503
2504/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002505void hash_compute_fail( int alg_arg, data_t *input,
2506 int output_size_arg, int expected_status_arg )
2507{
2508 psa_algorithm_t alg = alg_arg;
2509 uint8_t *output = NULL;
2510 size_t output_size = output_size_arg;
2511 size_t output_length = INVALID_EXPORT_LENGTH;
2512 psa_status_t expected_status = expected_status_arg;
2513 psa_status_t status;
2514
2515 ASSERT_ALLOC( output, output_size );
2516
2517 PSA_ASSERT( psa_crypto_init( ) );
2518
2519 status = psa_hash_compute( alg, input->x, input->len,
2520 output, output_size, &output_length );
2521 TEST_EQUAL( status, expected_status );
2522 TEST_ASSERT( output_length <= output_size );
2523
2524exit:
2525 mbedtls_free( output );
2526 PSA_DONE( );
2527}
2528/* END_CASE */
2529
2530/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002531void hash_compare_fail( int alg_arg, data_t *input,
2532 data_t *reference_hash,
2533 int expected_status_arg )
2534{
2535 psa_algorithm_t alg = alg_arg;
2536 psa_status_t expected_status = expected_status_arg;
2537 psa_status_t status;
2538
2539 PSA_ASSERT( psa_crypto_init( ) );
2540
2541 status = psa_hash_compare( alg, input->x, input->len,
2542 reference_hash->x, reference_hash->len );
2543 TEST_EQUAL( status, expected_status );
2544
2545exit:
2546 PSA_DONE( );
2547}
2548/* END_CASE */
2549
2550/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002551void hash_compute_compare( int alg_arg, data_t *input,
2552 data_t *expected_output )
2553{
2554 psa_algorithm_t alg = alg_arg;
2555 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2556 size_t output_length = INVALID_EXPORT_LENGTH;
2557 size_t i;
2558
2559 PSA_ASSERT( psa_crypto_init( ) );
2560
2561 /* Compute with tight buffer */
2562 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2563 output, PSA_HASH_SIZE( alg ),
2564 &output_length ) );
2565 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2566 ASSERT_COMPARE( output, output_length,
2567 expected_output->x, expected_output->len );
2568
2569 /* Compute with larger buffer */
2570 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2571 output, sizeof( output ),
2572 &output_length ) );
2573 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2574 ASSERT_COMPARE( output, output_length,
2575 expected_output->x, expected_output->len );
2576
2577 /* Compare with correct hash */
2578 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2579 output, output_length ) );
2580
2581 /* Compare with trailing garbage */
2582 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2583 output, output_length + 1 ),
2584 PSA_ERROR_INVALID_SIGNATURE );
2585
2586 /* Compare with truncated hash */
2587 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2588 output, output_length - 1 ),
2589 PSA_ERROR_INVALID_SIGNATURE );
2590
2591 /* Compare with corrupted value */
2592 for( i = 0; i < output_length; i++ )
2593 {
2594 test_set_step( i );
2595 output[i] ^= 1;
2596 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2597 output, output_length ),
2598 PSA_ERROR_INVALID_SIGNATURE );
2599 output[i] ^= 1;
2600 }
2601
2602exit:
2603 PSA_DONE( );
2604}
2605/* END_CASE */
2606
2607/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002608void hash_bad_order( )
2609{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002610 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002611 unsigned char input[] = "";
2612 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002613 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002614 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2615 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2616 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002617 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002618 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002619 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002620
Gilles Peskine8817f612018-12-18 00:18:46 +01002621 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002622
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002623 /* Call setup twice in a row. */
2624 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2625 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2626 PSA_ERROR_BAD_STATE );
2627 PSA_ASSERT( psa_hash_abort( &operation ) );
2628
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002629 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002630 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002631 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002632 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002633
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002634 /* Call update after finish. */
2635 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2636 PSA_ASSERT( psa_hash_finish( &operation,
2637 hash, sizeof( hash ), &hash_len ) );
2638 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002639 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002640 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002641
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002642 /* Call verify without calling setup beforehand. */
2643 TEST_EQUAL( psa_hash_verify( &operation,
2644 valid_hash, sizeof( valid_hash ) ),
2645 PSA_ERROR_BAD_STATE );
2646 PSA_ASSERT( psa_hash_abort( &operation ) );
2647
2648 /* Call verify after finish. */
2649 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2650 PSA_ASSERT( psa_hash_finish( &operation,
2651 hash, sizeof( hash ), &hash_len ) );
2652 TEST_EQUAL( psa_hash_verify( &operation,
2653 valid_hash, sizeof( valid_hash ) ),
2654 PSA_ERROR_BAD_STATE );
2655 PSA_ASSERT( psa_hash_abort( &operation ) );
2656
2657 /* Call verify twice in a row. */
2658 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2659 PSA_ASSERT( psa_hash_verify( &operation,
2660 valid_hash, sizeof( valid_hash ) ) );
2661 TEST_EQUAL( psa_hash_verify( &operation,
2662 valid_hash, sizeof( valid_hash ) ),
2663 PSA_ERROR_BAD_STATE );
2664 PSA_ASSERT( psa_hash_abort( &operation ) );
2665
2666 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002667 TEST_EQUAL( psa_hash_finish( &operation,
2668 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002669 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002670 PSA_ASSERT( psa_hash_abort( &operation ) );
2671
2672 /* Call finish twice in a row. */
2673 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2674 PSA_ASSERT( psa_hash_finish( &operation,
2675 hash, sizeof( hash ), &hash_len ) );
2676 TEST_EQUAL( psa_hash_finish( &operation,
2677 hash, sizeof( hash ), &hash_len ),
2678 PSA_ERROR_BAD_STATE );
2679 PSA_ASSERT( psa_hash_abort( &operation ) );
2680
2681 /* Call finish after calling verify. */
2682 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2683 PSA_ASSERT( psa_hash_verify( &operation,
2684 valid_hash, sizeof( valid_hash ) ) );
2685 TEST_EQUAL( psa_hash_finish( &operation,
2686 hash, sizeof( hash ), &hash_len ),
2687 PSA_ERROR_BAD_STATE );
2688 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002689
2690exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002691 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002692}
2693/* END_CASE */
2694
itayzafrir27e69452018-11-01 14:26:34 +02002695/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2696void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002697{
2698 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002699 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2700 * appended to it */
2701 unsigned char hash[] = {
2702 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2703 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2704 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002705 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002706 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002707
Gilles Peskine8817f612018-12-18 00:18:46 +01002708 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002709
itayzafrir27e69452018-11-01 14:26:34 +02002710 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002711 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002712 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002713 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002714
itayzafrir27e69452018-11-01 14:26:34 +02002715 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002716 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002717 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002718 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002719
itayzafrir27e69452018-11-01 14:26:34 +02002720 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002721 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002722 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002723 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002724
itayzafrirec93d302018-10-18 18:01:10 +03002725exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002726 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002727}
2728/* END_CASE */
2729
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002730/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2731void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002732{
2733 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002734 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002735 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002736 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002737 size_t hash_len;
2738
Gilles Peskine8817f612018-12-18 00:18:46 +01002739 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002740
itayzafrir58028322018-10-25 10:22:01 +03002741 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002742 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002743 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002744 hash, expected_size - 1, &hash_len ),
2745 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002746
2747exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002748 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002749}
2750/* END_CASE */
2751
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002752/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2753void hash_clone_source_state( )
2754{
2755 psa_algorithm_t alg = PSA_ALG_SHA_256;
2756 unsigned char hash[PSA_HASH_MAX_SIZE];
2757 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2758 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2759 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2760 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2761 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2762 size_t hash_len;
2763
2764 PSA_ASSERT( psa_crypto_init( ) );
2765 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2766
2767 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2768 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2769 PSA_ASSERT( psa_hash_finish( &op_finished,
2770 hash, sizeof( hash ), &hash_len ) );
2771 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2772 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2773
2774 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2775 PSA_ERROR_BAD_STATE );
2776
2777 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2778 PSA_ASSERT( psa_hash_finish( &op_init,
2779 hash, sizeof( hash ), &hash_len ) );
2780 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2781 PSA_ASSERT( psa_hash_finish( &op_finished,
2782 hash, sizeof( hash ), &hash_len ) );
2783 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2784 PSA_ASSERT( psa_hash_finish( &op_aborted,
2785 hash, sizeof( hash ), &hash_len ) );
2786
2787exit:
2788 psa_hash_abort( &op_source );
2789 psa_hash_abort( &op_init );
2790 psa_hash_abort( &op_setup );
2791 psa_hash_abort( &op_finished );
2792 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002793 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002794}
2795/* END_CASE */
2796
2797/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2798void hash_clone_target_state( )
2799{
2800 psa_algorithm_t alg = PSA_ALG_SHA_256;
2801 unsigned char hash[PSA_HASH_MAX_SIZE];
2802 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2803 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2804 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2805 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2806 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2807 size_t hash_len;
2808
2809 PSA_ASSERT( psa_crypto_init( ) );
2810
2811 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2812 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2813 PSA_ASSERT( psa_hash_finish( &op_finished,
2814 hash, sizeof( hash ), &hash_len ) );
2815 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2816 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2817
2818 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2819 PSA_ASSERT( psa_hash_finish( &op_target,
2820 hash, sizeof( hash ), &hash_len ) );
2821
2822 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2823 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2824 PSA_ERROR_BAD_STATE );
2825 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2826 PSA_ERROR_BAD_STATE );
2827
2828exit:
2829 psa_hash_abort( &op_target );
2830 psa_hash_abort( &op_init );
2831 psa_hash_abort( &op_setup );
2832 psa_hash_abort( &op_finished );
2833 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002834 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002835}
2836/* END_CASE */
2837
itayzafrir58028322018-10-25 10:22:01 +03002838/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002839void mac_operation_init( )
2840{
Jaeden Amero252ef282019-02-15 14:05:35 +00002841 const uint8_t input[1] = { 0 };
2842
Jaeden Amero769ce272019-01-04 11:48:03 +00002843 /* Test each valid way of initializing the object, except for `= {0}`, as
2844 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2845 * though it's OK by the C standard. We could test for this, but we'd need
2846 * to supress the Clang warning for the test. */
2847 psa_mac_operation_t func = psa_mac_operation_init( );
2848 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2849 psa_mac_operation_t zero;
2850
2851 memset( &zero, 0, sizeof( zero ) );
2852
Jaeden Amero252ef282019-02-15 14:05:35 +00002853 /* A freshly-initialized MAC operation should not be usable. */
2854 TEST_EQUAL( psa_mac_update( &func,
2855 input, sizeof( input ) ),
2856 PSA_ERROR_BAD_STATE );
2857 TEST_EQUAL( psa_mac_update( &init,
2858 input, sizeof( input ) ),
2859 PSA_ERROR_BAD_STATE );
2860 TEST_EQUAL( psa_mac_update( &zero,
2861 input, sizeof( input ) ),
2862 PSA_ERROR_BAD_STATE );
2863
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002864 /* A default MAC operation should be abortable without error. */
2865 PSA_ASSERT( psa_mac_abort( &func ) );
2866 PSA_ASSERT( psa_mac_abort( &init ) );
2867 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002868}
2869/* END_CASE */
2870
2871/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002872void mac_setup( int key_type_arg,
2873 data_t *key,
2874 int alg_arg,
2875 int expected_status_arg )
2876{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002877 psa_key_type_t key_type = key_type_arg;
2878 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002879 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002880 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002881 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2882#if defined(KNOWN_SUPPORTED_MAC_ALG)
2883 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2884#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002885
Gilles Peskine8817f612018-12-18 00:18:46 +01002886 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002887
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002888 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2889 &operation, &status ) )
2890 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002891 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002892
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002893 /* The operation object should be reusable. */
2894#if defined(KNOWN_SUPPORTED_MAC_ALG)
2895 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2896 smoke_test_key_data,
2897 sizeof( smoke_test_key_data ),
2898 KNOWN_SUPPORTED_MAC_ALG,
2899 &operation, &status ) )
2900 goto exit;
2901 TEST_EQUAL( status, PSA_SUCCESS );
2902#endif
2903
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002904exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002905 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002906}
2907/* END_CASE */
2908
2909/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002910void mac_bad_order( )
2911{
2912 psa_key_handle_t handle = 0;
2913 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2914 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2915 const uint8_t key[] = {
2916 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2917 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2918 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002920 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2921 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2922 size_t sign_mac_length = 0;
2923 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2924 const uint8_t verify_mac[] = {
2925 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2926 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2927 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2928
2929 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002930 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | 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 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002933
Gilles Peskine73676cb2019-05-15 20:15:10 +02002934 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002935
Jaeden Amero252ef282019-02-15 14:05:35 +00002936 /* Call update without calling setup beforehand. */
2937 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2938 PSA_ERROR_BAD_STATE );
2939 PSA_ASSERT( psa_mac_abort( &operation ) );
2940
2941 /* Call sign finish without calling setup beforehand. */
2942 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2943 &sign_mac_length),
2944 PSA_ERROR_BAD_STATE );
2945 PSA_ASSERT( psa_mac_abort( &operation ) );
2946
2947 /* Call verify finish without calling setup beforehand. */
2948 TEST_EQUAL( psa_mac_verify_finish( &operation,
2949 verify_mac, sizeof( verify_mac ) ),
2950 PSA_ERROR_BAD_STATE );
2951 PSA_ASSERT( psa_mac_abort( &operation ) );
2952
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002953 /* Call setup twice in a row. */
2954 PSA_ASSERT( psa_mac_sign_setup( &operation,
2955 handle, alg ) );
2956 TEST_EQUAL( psa_mac_sign_setup( &operation,
2957 handle, alg ),
2958 PSA_ERROR_BAD_STATE );
2959 PSA_ASSERT( psa_mac_abort( &operation ) );
2960
Jaeden Amero252ef282019-02-15 14:05:35 +00002961 /* Call update after sign finish. */
2962 PSA_ASSERT( psa_mac_sign_setup( &operation,
2963 handle, alg ) );
2964 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2965 PSA_ASSERT( psa_mac_sign_finish( &operation,
2966 sign_mac, sizeof( sign_mac ),
2967 &sign_mac_length ) );
2968 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2969 PSA_ERROR_BAD_STATE );
2970 PSA_ASSERT( psa_mac_abort( &operation ) );
2971
2972 /* Call update after verify finish. */
2973 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002974 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002975 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2976 PSA_ASSERT( psa_mac_verify_finish( &operation,
2977 verify_mac, sizeof( verify_mac ) ) );
2978 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2979 PSA_ERROR_BAD_STATE );
2980 PSA_ASSERT( psa_mac_abort( &operation ) );
2981
2982 /* Call sign finish twice in a row. */
2983 PSA_ASSERT( psa_mac_sign_setup( &operation,
2984 handle, alg ) );
2985 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2986 PSA_ASSERT( psa_mac_sign_finish( &operation,
2987 sign_mac, sizeof( sign_mac ),
2988 &sign_mac_length ) );
2989 TEST_EQUAL( psa_mac_sign_finish( &operation,
2990 sign_mac, sizeof( sign_mac ),
2991 &sign_mac_length ),
2992 PSA_ERROR_BAD_STATE );
2993 PSA_ASSERT( psa_mac_abort( &operation ) );
2994
2995 /* Call verify finish twice in a row. */
2996 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002997 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002998 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2999 PSA_ASSERT( psa_mac_verify_finish( &operation,
3000 verify_mac, sizeof( verify_mac ) ) );
3001 TEST_EQUAL( psa_mac_verify_finish( &operation,
3002 verify_mac, sizeof( verify_mac ) ),
3003 PSA_ERROR_BAD_STATE );
3004 PSA_ASSERT( psa_mac_abort( &operation ) );
3005
3006 /* Setup sign but try verify. */
3007 PSA_ASSERT( psa_mac_sign_setup( &operation,
3008 handle, alg ) );
3009 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3010 TEST_EQUAL( psa_mac_verify_finish( &operation,
3011 verify_mac, sizeof( verify_mac ) ),
3012 PSA_ERROR_BAD_STATE );
3013 PSA_ASSERT( psa_mac_abort( &operation ) );
3014
3015 /* Setup verify but try sign. */
3016 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003017 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003018 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3019 TEST_EQUAL( psa_mac_sign_finish( &operation,
3020 sign_mac, sizeof( sign_mac ),
3021 &sign_mac_length ),
3022 PSA_ERROR_BAD_STATE );
3023 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003024
Gilles Peskine76b29a72019-05-28 14:08:50 +02003025 PSA_ASSERT( psa_destroy_key( handle ) );
3026
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003027exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003028 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003029}
3030/* END_CASE */
3031
3032/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003033void mac_sign( int key_type_arg,
3034 data_t *key,
3035 int alg_arg,
3036 data_t *input,
3037 data_t *expected_mac )
3038{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003039 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003040 psa_key_type_t key_type = key_type_arg;
3041 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003042 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003044 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003045 size_t mac_buffer_size =
3046 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
3047 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003048 const size_t output_sizes_to_test[] = {
3049 0,
3050 1,
3051 expected_mac->len - 1,
3052 expected_mac->len,
3053 expected_mac->len + 1,
3054 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003055
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003056 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
Gilles Peskine3d404d62020-08-25 23:47:36 +02003057 /* We expect PSA_MAC_FINAL_SIZE to be exact. */
3058 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003059
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003061
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003062 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003063 psa_set_key_algorithm( &attributes, alg );
3064 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003065
Gilles Peskine73676cb2019-05-15 20:15:10 +02003066 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003067
Gilles Peskine8b356b52020-08-25 23:44:59 +02003068 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3069 {
3070 const size_t output_size = output_sizes_to_test[i];
3071 psa_status_t expected_status =
3072 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3073 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003074
Gilles Peskine8b356b52020-08-25 23:44:59 +02003075 test_set_step( output_size );
3076 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003077
Gilles Peskine8b356b52020-08-25 23:44:59 +02003078 /* Calculate the MAC. */
3079 PSA_ASSERT( psa_mac_sign_setup( &operation,
3080 handle, alg ) );
3081 PSA_ASSERT( psa_mac_update( &operation,
3082 input->x, input->len ) );
3083 TEST_EQUAL( psa_mac_sign_finish( &operation,
3084 actual_mac, output_size,
3085 &mac_length ),
3086 expected_status );
3087 PSA_ASSERT( psa_mac_abort( &operation ) );
3088
3089 if( expected_status == PSA_SUCCESS )
3090 {
3091 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3092 actual_mac, mac_length );
3093 }
3094 mbedtls_free( actual_mac );
3095 actual_mac = NULL;
3096 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003097
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003098exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003099 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003100 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003101 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003102 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003103}
3104/* END_CASE */
3105
3106/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003107void mac_verify( int key_type_arg,
3108 data_t *key,
3109 int alg_arg,
3110 data_t *input,
3111 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003112{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003113 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003114 psa_key_type_t key_type = key_type_arg;
3115 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003116 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003118 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003119
Gilles Peskine69c12672018-06-28 00:07:19 +02003120 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3121
Gilles Peskine8817f612018-12-18 00:18:46 +01003122 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003123
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003124 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003125 psa_set_key_algorithm( &attributes, alg );
3126 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003127
Gilles Peskine73676cb2019-05-15 20:15:10 +02003128 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003129
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003130 /* Test the correct MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003131 PSA_ASSERT( psa_mac_verify_setup( &operation,
3132 handle, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003133 PSA_ASSERT( psa_mac_update( &operation,
3134 input->x, input->len ) );
3135 PSA_ASSERT( psa_mac_verify_finish( &operation,
3136 expected_mac->x,
3137 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003138
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003139 /* Test a MAC that's too short. */
3140 PSA_ASSERT( psa_mac_verify_setup( &operation,
3141 handle, alg ) );
3142 PSA_ASSERT( psa_mac_update( &operation,
3143 input->x, input->len ) );
3144 TEST_EQUAL( psa_mac_verify_finish( &operation,
3145 expected_mac->x,
3146 expected_mac->len - 1 ),
3147 PSA_ERROR_INVALID_SIGNATURE );
3148
3149 /* Test a MAC that's too long. */
3150 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3151 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
3152 PSA_ASSERT( psa_mac_verify_setup( &operation,
3153 handle, alg ) );
3154 PSA_ASSERT( psa_mac_update( &operation,
3155 input->x, input->len ) );
3156 TEST_EQUAL( psa_mac_verify_finish( &operation,
3157 perturbed_mac,
3158 expected_mac->len + 1 ),
3159 PSA_ERROR_INVALID_SIGNATURE );
3160
3161 /* Test changing one byte. */
3162 for( size_t i = 0; i < expected_mac->len; i++ )
3163 {
3164 test_set_step( i );
3165 perturbed_mac[i] ^= 1;
3166 PSA_ASSERT( psa_mac_verify_setup( &operation,
3167 handle, alg ) );
3168 PSA_ASSERT( psa_mac_update( &operation,
3169 input->x, input->len ) );
3170 TEST_EQUAL( psa_mac_verify_finish( &operation,
3171 perturbed_mac,
3172 expected_mac->len ),
3173 PSA_ERROR_INVALID_SIGNATURE );
3174 perturbed_mac[i] ^= 1;
3175 }
3176
Gilles Peskine8c9def32018-02-08 10:02:12 +01003177exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003178 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003179 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003180 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003181 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003182}
3183/* END_CASE */
3184
3185/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003186void cipher_operation_init( )
3187{
Jaeden Ameroab439972019-02-15 14:12:05 +00003188 const uint8_t input[1] = { 0 };
3189 unsigned char output[1] = { 0 };
3190 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003191 /* Test each valid way of initializing the object, except for `= {0}`, as
3192 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3193 * though it's OK by the C standard. We could test for this, but we'd need
3194 * to supress the Clang warning for the test. */
3195 psa_cipher_operation_t func = psa_cipher_operation_init( );
3196 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3197 psa_cipher_operation_t zero;
3198
3199 memset( &zero, 0, sizeof( zero ) );
3200
Jaeden Ameroab439972019-02-15 14:12:05 +00003201 /* A freshly-initialized cipher operation should not be usable. */
3202 TEST_EQUAL( psa_cipher_update( &func,
3203 input, sizeof( input ),
3204 output, sizeof( output ),
3205 &output_length ),
3206 PSA_ERROR_BAD_STATE );
3207 TEST_EQUAL( psa_cipher_update( &init,
3208 input, sizeof( input ),
3209 output, sizeof( output ),
3210 &output_length ),
3211 PSA_ERROR_BAD_STATE );
3212 TEST_EQUAL( psa_cipher_update( &zero,
3213 input, sizeof( input ),
3214 output, sizeof( output ),
3215 &output_length ),
3216 PSA_ERROR_BAD_STATE );
3217
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003218 /* A default cipher operation should be abortable without error. */
3219 PSA_ASSERT( psa_cipher_abort( &func ) );
3220 PSA_ASSERT( psa_cipher_abort( &init ) );
3221 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003222}
3223/* END_CASE */
3224
3225/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003226void cipher_setup( int key_type_arg,
3227 data_t *key,
3228 int alg_arg,
3229 int expected_status_arg )
3230{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003231 psa_key_type_t key_type = key_type_arg;
3232 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003233 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003234 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003235 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003236#if defined(KNOWN_SUPPORTED_MAC_ALG)
3237 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3238#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003239
Gilles Peskine8817f612018-12-18 00:18:46 +01003240 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003241
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003242 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3243 &operation, &status ) )
3244 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003245 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003246
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003247 /* The operation object should be reusable. */
3248#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3249 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3250 smoke_test_key_data,
3251 sizeof( smoke_test_key_data ),
3252 KNOWN_SUPPORTED_CIPHER_ALG,
3253 &operation, &status ) )
3254 goto exit;
3255 TEST_EQUAL( status, PSA_SUCCESS );
3256#endif
3257
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003258exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003259 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003260 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003261}
3262/* END_CASE */
3263
3264/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003265void cipher_bad_order( )
3266{
3267 psa_key_handle_t handle = 0;
3268 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3269 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003271 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3272 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3273 const uint8_t key[] = {
3274 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3275 0xaa, 0xaa, 0xaa, 0xaa };
3276 const uint8_t text[] = {
3277 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3278 0xbb, 0xbb, 0xbb, 0xbb };
3279 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3280 size_t length = 0;
3281
3282 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003283 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3284 psa_set_key_algorithm( &attributes, alg );
3285 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003286 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003287
3288
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003289 /* Call encrypt setup twice in a row. */
3290 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3291 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
3292 PSA_ERROR_BAD_STATE );
3293 PSA_ASSERT( psa_cipher_abort( &operation ) );
3294
3295 /* Call decrypt setup twice in a row. */
3296 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
3297 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
3298 PSA_ERROR_BAD_STATE );
3299 PSA_ASSERT( psa_cipher_abort( &operation ) );
3300
Jaeden Ameroab439972019-02-15 14:12:05 +00003301 /* Generate an IV without calling setup beforehand. */
3302 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3303 buffer, sizeof( buffer ),
3304 &length ),
3305 PSA_ERROR_BAD_STATE );
3306 PSA_ASSERT( psa_cipher_abort( &operation ) );
3307
3308 /* Generate an IV twice in a row. */
3309 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3310 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3311 buffer, sizeof( buffer ),
3312 &length ) );
3313 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3314 buffer, sizeof( buffer ),
3315 &length ),
3316 PSA_ERROR_BAD_STATE );
3317 PSA_ASSERT( psa_cipher_abort( &operation ) );
3318
3319 /* Generate an IV after it's already set. */
3320 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3321 PSA_ASSERT( psa_cipher_set_iv( &operation,
3322 iv, sizeof( iv ) ) );
3323 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3324 buffer, sizeof( buffer ),
3325 &length ),
3326 PSA_ERROR_BAD_STATE );
3327 PSA_ASSERT( psa_cipher_abort( &operation ) );
3328
3329 /* Set an IV without calling setup beforehand. */
3330 TEST_EQUAL( psa_cipher_set_iv( &operation,
3331 iv, sizeof( iv ) ),
3332 PSA_ERROR_BAD_STATE );
3333 PSA_ASSERT( psa_cipher_abort( &operation ) );
3334
3335 /* Set an IV after it's already set. */
3336 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3337 PSA_ASSERT( psa_cipher_set_iv( &operation,
3338 iv, sizeof( iv ) ) );
3339 TEST_EQUAL( psa_cipher_set_iv( &operation,
3340 iv, sizeof( iv ) ),
3341 PSA_ERROR_BAD_STATE );
3342 PSA_ASSERT( psa_cipher_abort( &operation ) );
3343
3344 /* Set an IV after it's already generated. */
3345 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3346 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3347 buffer, sizeof( buffer ),
3348 &length ) );
3349 TEST_EQUAL( psa_cipher_set_iv( &operation,
3350 iv, sizeof( iv ) ),
3351 PSA_ERROR_BAD_STATE );
3352 PSA_ASSERT( psa_cipher_abort( &operation ) );
3353
3354 /* Call update without calling setup beforehand. */
3355 TEST_EQUAL( psa_cipher_update( &operation,
3356 text, sizeof( text ),
3357 buffer, sizeof( buffer ),
3358 &length ),
3359 PSA_ERROR_BAD_STATE );
3360 PSA_ASSERT( psa_cipher_abort( &operation ) );
3361
3362 /* Call update without an IV where an IV is required. */
3363 TEST_EQUAL( psa_cipher_update( &operation,
3364 text, sizeof( text ),
3365 buffer, sizeof( buffer ),
3366 &length ),
3367 PSA_ERROR_BAD_STATE );
3368 PSA_ASSERT( psa_cipher_abort( &operation ) );
3369
3370 /* Call update after finish. */
3371 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3372 PSA_ASSERT( psa_cipher_set_iv( &operation,
3373 iv, sizeof( iv ) ) );
3374 PSA_ASSERT( psa_cipher_finish( &operation,
3375 buffer, sizeof( buffer ), &length ) );
3376 TEST_EQUAL( psa_cipher_update( &operation,
3377 text, sizeof( text ),
3378 buffer, sizeof( buffer ),
3379 &length ),
3380 PSA_ERROR_BAD_STATE );
3381 PSA_ASSERT( psa_cipher_abort( &operation ) );
3382
3383 /* Call finish without calling setup beforehand. */
3384 TEST_EQUAL( psa_cipher_finish( &operation,
3385 buffer, sizeof( buffer ), &length ),
3386 PSA_ERROR_BAD_STATE );
3387 PSA_ASSERT( psa_cipher_abort( &operation ) );
3388
3389 /* Call finish without an IV where an IV is required. */
3390 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3391 /* Not calling update means we are encrypting an empty buffer, which is OK
3392 * for cipher modes with padding. */
3393 TEST_EQUAL( psa_cipher_finish( &operation,
3394 buffer, sizeof( buffer ), &length ),
3395 PSA_ERROR_BAD_STATE );
3396 PSA_ASSERT( psa_cipher_abort( &operation ) );
3397
3398 /* Call finish twice in a row. */
3399 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3400 PSA_ASSERT( psa_cipher_set_iv( &operation,
3401 iv, sizeof( iv ) ) );
3402 PSA_ASSERT( psa_cipher_finish( &operation,
3403 buffer, sizeof( buffer ), &length ) );
3404 TEST_EQUAL( psa_cipher_finish( &operation,
3405 buffer, sizeof( buffer ), &length ),
3406 PSA_ERROR_BAD_STATE );
3407 PSA_ASSERT( psa_cipher_abort( &operation ) );
3408
Gilles Peskine76b29a72019-05-28 14:08:50 +02003409 PSA_ASSERT( psa_destroy_key( handle ) );
3410
Jaeden Ameroab439972019-02-15 14:12:05 +00003411exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003412 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003413 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003414}
3415/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003416
Gilles Peskine50e586b2018-06-08 14:28:46 +02003417/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003418void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003419 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003420 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003421 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003422{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003423 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003424 psa_status_t status;
3425 psa_key_type_t key_type = key_type_arg;
3426 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003427 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003428 unsigned char *output = NULL;
3429 size_t output_buffer_size = 0;
3430 size_t function_output_length = 0;
3431 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003432 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003434
Gilles Peskine8817f612018-12-18 00:18:46 +01003435 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003436
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003437 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3438 psa_set_key_algorithm( &attributes, alg );
3439 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003440
Gilles Peskine73676cb2019-05-15 20:15:10 +02003441 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003442
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3444 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003445
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003446 if( iv->len > 0 )
3447 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003448 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003449 }
3450
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003451 output_buffer_size = ( (size_t) input->len +
3452 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003453 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003454
Gilles Peskine8817f612018-12-18 00:18:46 +01003455 PSA_ASSERT( psa_cipher_update( &operation,
3456 input->x, input->len,
3457 output, output_buffer_size,
3458 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003459 total_output_length += function_output_length;
3460 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003461 output + total_output_length,
3462 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003463 &function_output_length );
3464 total_output_length += function_output_length;
3465
Gilles Peskinefe11b722018-12-18 00:24:04 +01003466 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003467 if( expected_status == PSA_SUCCESS )
3468 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003469 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003470 ASSERT_COMPARE( expected_output->x, expected_output->len,
3471 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003472 }
3473
3474exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003475 psa_cipher_abort( &operation );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003476 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003477 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003478 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003479}
3480/* END_CASE */
3481
3482/* BEGIN_CASE */
3483void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003484 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003485 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003486 int first_part_size_arg,
3487 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003488 data_t *expected_output )
3489{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003490 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003491 psa_key_type_t key_type = key_type_arg;
3492 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003493 size_t first_part_size = first_part_size_arg;
3494 size_t output1_length = output1_length_arg;
3495 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003496 unsigned char *output = NULL;
3497 size_t output_buffer_size = 0;
3498 size_t function_output_length = 0;
3499 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003500 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003502
Gilles Peskine8817f612018-12-18 00:18:46 +01003503 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003504
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003505 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3506 psa_set_key_algorithm( &attributes, alg );
3507 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003508
Gilles Peskine73676cb2019-05-15 20:15:10 +02003509 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003510
Gilles Peskine8817f612018-12-18 00:18:46 +01003511 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3512 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003513
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003514 if( iv->len > 0 )
3515 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003516 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003517 }
3518
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003519 output_buffer_size = ( (size_t) input->len +
3520 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003521 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003522
Gilles Peskinee0866522019-02-19 19:44:00 +01003523 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003524 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3525 output, output_buffer_size,
3526 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003527 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003528 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003529 PSA_ASSERT( psa_cipher_update( &operation,
3530 input->x + first_part_size,
3531 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003532 output + total_output_length,
3533 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003534 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003535 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003536 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003537 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003538 output + total_output_length,
3539 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003541 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003542 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003543
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003544 ASSERT_COMPARE( expected_output->x, expected_output->len,
3545 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003546
3547exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003548 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003549 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003550 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003551 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003552}
3553/* END_CASE */
3554
3555/* BEGIN_CASE */
3556void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003557 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003558 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003559 int first_part_size_arg,
3560 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003561 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003562{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003563 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003564
3565 psa_key_type_t key_type = key_type_arg;
3566 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003567 size_t first_part_size = first_part_size_arg;
3568 size_t output1_length = output1_length_arg;
3569 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003570 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003571 size_t output_buffer_size = 0;
3572 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003573 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003574 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003575 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003576
Gilles Peskine8817f612018-12-18 00:18:46 +01003577 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003578
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003579 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3580 psa_set_key_algorithm( &attributes, alg );
3581 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003582
Gilles Peskine73676cb2019-05-15 20:15:10 +02003583 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003584
Gilles Peskine8817f612018-12-18 00:18:46 +01003585 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3586 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003587
Steven Cooreman177deba2020-09-07 17:14:14 +02003588 if( iv->len > 0 )
3589 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003590 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003591 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003592
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003593 output_buffer_size = ( (size_t) input->len +
3594 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003595 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003596
Gilles Peskinee0866522019-02-19 19:44:00 +01003597 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003598 PSA_ASSERT( psa_cipher_update( &operation,
3599 input->x, first_part_size,
3600 output, output_buffer_size,
3601 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003602 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003603 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003604 PSA_ASSERT( psa_cipher_update( &operation,
3605 input->x + first_part_size,
3606 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003607 output + total_output_length,
3608 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003609 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003610 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003611 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003612 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003613 output + total_output_length,
3614 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003615 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003616 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003617 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003618
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003619 ASSERT_COMPARE( expected_output->x, expected_output->len,
3620 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003621
3622exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003623 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003624 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003625 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003626 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003627}
3628/* END_CASE */
3629
Gilles Peskine50e586b2018-06-08 14:28:46 +02003630/* BEGIN_CASE */
3631void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003632 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003633 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003634 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003635{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003636 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003637 psa_status_t status;
3638 psa_key_type_t key_type = key_type_arg;
3639 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003640 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003641 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003642 size_t output_buffer_size = 0;
3643 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003644 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003645 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003647
Gilles Peskine8817f612018-12-18 00:18:46 +01003648 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003649
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003650 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3651 psa_set_key_algorithm( &attributes, alg );
3652 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003653
Gilles Peskine73676cb2019-05-15 20:15:10 +02003654 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003655
Gilles Peskine8817f612018-12-18 00:18:46 +01003656 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3657 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003658
Steven Cooreman177deba2020-09-07 17:14:14 +02003659 if( iv->len > 0 )
3660 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003661 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003662 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003663
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003664 output_buffer_size = ( (size_t) input->len +
3665 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003666 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003667
Gilles Peskine8817f612018-12-18 00:18:46 +01003668 PSA_ASSERT( psa_cipher_update( &operation,
3669 input->x, input->len,
3670 output, output_buffer_size,
3671 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003672 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003673 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003674 output + total_output_length,
3675 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003676 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003677 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003678 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003679
3680 if( expected_status == PSA_SUCCESS )
3681 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003682 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003683 ASSERT_COMPARE( expected_output->x, expected_output->len,
3684 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003685 }
3686
Gilles Peskine50e586b2018-06-08 14:28:46 +02003687exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003688 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003689 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003690 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003691 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003692}
3693/* END_CASE */
3694
Gilles Peskine50e586b2018-06-08 14:28:46 +02003695/* BEGIN_CASE */
3696void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003697 data_t *key,
3698 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003699{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003700 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003701 psa_key_type_t key_type = key_type_arg;
3702 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003703 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003704 size_t iv_size = 16;
3705 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003706 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003707 size_t output1_size = 0;
3708 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003709 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003710 size_t output2_size = 0;
3711 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003712 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003713 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3714 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003715 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003716
Gilles Peskine8817f612018-12-18 00:18:46 +01003717 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003718
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003719 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3720 psa_set_key_algorithm( &attributes, alg );
3721 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003722
Gilles Peskine73676cb2019-05-15 20:15:10 +02003723 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003724
Gilles Peskine8817f612018-12-18 00:18:46 +01003725 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3726 handle, alg ) );
3727 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3728 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003729
Steven Cooreman177deba2020-09-07 17:14:14 +02003730 if( alg != PSA_ALG_ECB_NO_PADDING )
3731 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003732 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3733 iv, iv_size,
3734 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003735 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003736 output1_size = ( (size_t) input->len +
3737 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003738 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003739
Gilles Peskine8817f612018-12-18 00:18:46 +01003740 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3741 output1, output1_size,
3742 &output1_length ) );
3743 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003744 output1 + output1_length,
3745 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003746 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003747
Gilles Peskine048b7f02018-06-08 14:20:49 +02003748 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003749
Gilles Peskine8817f612018-12-18 00:18:46 +01003750 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003751
3752 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003753 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003754
Steven Cooreman177deba2020-09-07 17:14:14 +02003755 if( iv_length > 0 )
3756 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003757 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3758 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003759 }
3760
Gilles Peskine8817f612018-12-18 00:18:46 +01003761 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3762 output2, output2_size,
3763 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003764 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003765 PSA_ASSERT( psa_cipher_finish( &operation2,
3766 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003767 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003768 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003769
Gilles Peskine048b7f02018-06-08 14:20:49 +02003770 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003771
Gilles Peskine8817f612018-12-18 00:18:46 +01003772 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003773
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003774 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003775
3776exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003777 psa_cipher_abort( &operation1 );
3778 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003779 mbedtls_free( output1 );
3780 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003781 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003782 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003783}
3784/* END_CASE */
3785
3786/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003787void cipher_verify_output_multipart( int alg_arg,
3788 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003789 data_t *key,
3790 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003791 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003792{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003793 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003794 psa_key_type_t key_type = key_type_arg;
3795 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003796 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003797 unsigned char iv[16] = {0};
3798 size_t iv_size = 16;
3799 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003800 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003801 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003802 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003803 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003804 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003805 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003806 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003807 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3808 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003809 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003810
Gilles Peskine8817f612018-12-18 00:18:46 +01003811 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003812
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003813 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3814 psa_set_key_algorithm( &attributes, alg );
3815 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003816
Gilles Peskine73676cb2019-05-15 20:15:10 +02003817 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003818
Gilles Peskine8817f612018-12-18 00:18:46 +01003819 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3820 handle, alg ) );
3821 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3822 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003823
Steven Cooreman177deba2020-09-07 17:14:14 +02003824 if( alg != PSA_ALG_ECB_NO_PADDING )
3825 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003826 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3827 iv, iv_size,
3828 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003829 }
3830
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003831 output1_buffer_size = ( (size_t) input->len +
3832 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003833 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003834
Gilles Peskinee0866522019-02-19 19:44:00 +01003835 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003836
Gilles Peskine8817f612018-12-18 00:18:46 +01003837 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3838 output1, output1_buffer_size,
3839 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003840 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003841
Gilles Peskine8817f612018-12-18 00:18:46 +01003842 PSA_ASSERT( psa_cipher_update( &operation1,
3843 input->x + first_part_size,
3844 input->len - first_part_size,
3845 output1, output1_buffer_size,
3846 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003847 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003848
Gilles Peskine8817f612018-12-18 00:18:46 +01003849 PSA_ASSERT( psa_cipher_finish( &operation1,
3850 output1 + output1_length,
3851 output1_buffer_size - output1_length,
3852 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003853 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003854
Gilles Peskine8817f612018-12-18 00:18:46 +01003855 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003856
Gilles Peskine048b7f02018-06-08 14:20:49 +02003857 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003858 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003859
Steven Cooreman177deba2020-09-07 17:14:14 +02003860 if( iv_length > 0 )
3861 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003862 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3863 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003864 }
Moran Pekerded84402018-06-06 16:36:50 +03003865
Gilles Peskine8817f612018-12-18 00:18:46 +01003866 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3867 output2, output2_buffer_size,
3868 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003869 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003870
Gilles Peskine8817f612018-12-18 00:18:46 +01003871 PSA_ASSERT( psa_cipher_update( &operation2,
3872 output1 + first_part_size,
3873 output1_length - first_part_size,
3874 output2, output2_buffer_size,
3875 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003876 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003877
Gilles Peskine8817f612018-12-18 00:18:46 +01003878 PSA_ASSERT( psa_cipher_finish( &operation2,
3879 output2 + output2_length,
3880 output2_buffer_size - output2_length,
3881 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003882 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003883
Gilles Peskine8817f612018-12-18 00:18:46 +01003884 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003885
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003886 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003887
3888exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003889 psa_cipher_abort( &operation1 );
3890 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003891 mbedtls_free( output1 );
3892 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003893 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003894 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003895}
3896/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003897
Gilles Peskine20035e32018-02-03 22:44:14 +01003898/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003899void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003900 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003901 data_t *nonce,
3902 data_t *additional_data,
3903 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003904 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003905{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003906 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003907 psa_key_type_t key_type = key_type_arg;
3908 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003909 unsigned char *output_data = NULL;
3910 size_t output_size = 0;
3911 size_t output_length = 0;
3912 unsigned char *output_data2 = NULL;
3913 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003914 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003915 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003916 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003917
Gilles Peskine4abf7412018-06-18 16:35:34 +02003918 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003919 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3920 * should be exact. */
3921 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3922 TEST_EQUAL( output_size,
3923 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003924 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003925
Gilles Peskine8817f612018-12-18 00:18:46 +01003926 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003927
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003928 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3929 psa_set_key_algorithm( &attributes, alg );
3930 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003931
Gilles Peskine049c7532019-05-15 20:22:09 +02003932 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3933 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003934
Gilles Peskinefe11b722018-12-18 00:24:04 +01003935 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3936 nonce->x, nonce->len,
3937 additional_data->x,
3938 additional_data->len,
3939 input_data->x, input_data->len,
3940 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003941 &output_length ),
3942 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003943
3944 if( PSA_SUCCESS == expected_result )
3945 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003946 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003947
Gilles Peskine003a4a92019-05-14 16:09:40 +02003948 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3949 * should be exact. */
3950 TEST_EQUAL( input_data->len,
3951 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3952
Gilles Peskinefe11b722018-12-18 00:24:04 +01003953 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3954 nonce->x, nonce->len,
3955 additional_data->x,
3956 additional_data->len,
3957 output_data, output_length,
3958 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003959 &output_length2 ),
3960 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003961
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003962 ASSERT_COMPARE( input_data->x, input_data->len,
3963 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003964 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003965
Gilles Peskinea1cac842018-06-11 19:33:02 +02003966exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003967 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003968 mbedtls_free( output_data );
3969 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003970 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003971}
3972/* END_CASE */
3973
3974/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003975void aead_encrypt( int key_type_arg, data_t *key_data,
3976 int alg_arg,
3977 data_t *nonce,
3978 data_t *additional_data,
3979 data_t *input_data,
3980 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003981{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003982 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003983 psa_key_type_t key_type = key_type_arg;
3984 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003985 unsigned char *output_data = NULL;
3986 size_t output_size = 0;
3987 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003988 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003990
Gilles Peskine4abf7412018-06-18 16:35:34 +02003991 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003992 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3993 * should be exact. */
3994 TEST_EQUAL( output_size,
3995 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003996 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003997
Gilles Peskine8817f612018-12-18 00:18:46 +01003998 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003999
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004000 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4001 psa_set_key_algorithm( &attributes, alg );
4002 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004003
Gilles Peskine049c7532019-05-15 20:22:09 +02004004 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4005 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004006
Gilles Peskine8817f612018-12-18 00:18:46 +01004007 PSA_ASSERT( psa_aead_encrypt( handle, alg,
4008 nonce->x, nonce->len,
4009 additional_data->x, additional_data->len,
4010 input_data->x, input_data->len,
4011 output_data, output_size,
4012 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004013
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004014 ASSERT_COMPARE( expected_result->x, expected_result->len,
4015 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004016
Gilles Peskinea1cac842018-06-11 19:33:02 +02004017exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004018 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004019 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004020 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004021}
4022/* END_CASE */
4023
4024/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004025void aead_decrypt( int key_type_arg, data_t *key_data,
4026 int alg_arg,
4027 data_t *nonce,
4028 data_t *additional_data,
4029 data_t *input_data,
4030 data_t *expected_data,
4031 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004032{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004033 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004034 psa_key_type_t key_type = key_type_arg;
4035 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004036 unsigned char *output_data = NULL;
4037 size_t output_size = 0;
4038 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004039 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004041 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004042
Gilles Peskine003a4a92019-05-14 16:09:40 +02004043 output_size = input_data->len - tag_length;
4044 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4045 * should be exact. */
4046 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4047 TEST_EQUAL( output_size,
4048 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004049 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004050
Gilles Peskine8817f612018-12-18 00:18:46 +01004051 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004052
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004053 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4054 psa_set_key_algorithm( &attributes, alg );
4055 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004056
Gilles Peskine049c7532019-05-15 20:22:09 +02004057 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4058 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004059
Gilles Peskinefe11b722018-12-18 00:24:04 +01004060 TEST_EQUAL( psa_aead_decrypt( handle, alg,
4061 nonce->x, nonce->len,
4062 additional_data->x,
4063 additional_data->len,
4064 input_data->x, input_data->len,
4065 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004066 &output_length ),
4067 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004068
Gilles Peskine2d277862018-06-18 15:41:12 +02004069 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004070 ASSERT_COMPARE( expected_data->x, expected_data->len,
4071 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004072
Gilles Peskinea1cac842018-06-11 19:33:02 +02004073exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004074 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004075 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004076 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004077}
4078/* END_CASE */
4079
4080/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004081void signature_size( int type_arg,
4082 int bits,
4083 int alg_arg,
4084 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004085{
4086 psa_key_type_t type = type_arg;
4087 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004088 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004089
Gilles Peskinefe11b722018-12-18 00:24:04 +01004090 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004091#if defined(MBEDTLS_TEST_DEPRECATED)
4092 TEST_EQUAL( actual_size,
4093 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4094#endif /* MBEDTLS_TEST_DEPRECATED */
4095
Gilles Peskinee59236f2018-01-27 23:32:46 +01004096exit:
4097 ;
4098}
4099/* END_CASE */
4100
4101/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004102void sign_deterministic( int key_type_arg, data_t *key_data,
4103 int alg_arg, data_t *input_data,
4104 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004105{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004106 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004107 psa_key_type_t key_type = key_type_arg;
4108 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004109 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004110 unsigned char *signature = NULL;
4111 size_t signature_size;
4112 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004114
Gilles Peskine8817f612018-12-18 00:18:46 +01004115 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004116
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004117 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004118 psa_set_key_algorithm( &attributes, alg );
4119 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004120
Gilles Peskine049c7532019-05-15 20:22:09 +02004121 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4122 &handle ) );
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 Peskine20035e32018-02-03 22:44:14 +01004125
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004126 /* Allocate a buffer which has the size advertized by the
4127 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004128 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004129 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004130 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004131 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004132 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004133
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004134 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004135 PSA_ASSERT( psa_sign_hash( handle, alg,
4136 input_data->x, input_data->len,
4137 signature, signature_size,
4138 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004139 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004140 ASSERT_COMPARE( output_data->x, output_data->len,
4141 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004142
Gilles Peskine0627f982019-11-26 19:12:16 +01004143#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004144 memset( signature, 0, signature_size );
4145 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01004146 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
4147 input_data->x, input_data->len,
4148 signature, signature_size,
4149 &signature_length ) );
4150 ASSERT_COMPARE( output_data->x, output_data->len,
4151 signature, signature_length );
4152#endif /* MBEDTLS_TEST_DEPRECATED */
4153
Gilles Peskine20035e32018-02-03 22:44:14 +01004154exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004155 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004156 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01004157 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004158 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004159}
4160/* END_CASE */
4161
4162/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004163void sign_fail( int key_type_arg, data_t *key_data,
4164 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004165 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004166{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004167 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01004168 psa_key_type_t key_type = key_type_arg;
4169 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004170 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004171 psa_status_t actual_status;
4172 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004173 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004174 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004176
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004177 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004178
Gilles Peskine8817f612018-12-18 00:18:46 +01004179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004180
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004181 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004182 psa_set_key_algorithm( &attributes, alg );
4183 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004184
Gilles Peskine049c7532019-05-15 20:22:09 +02004185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4186 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004187
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004188 actual_status = psa_sign_hash( handle, alg,
4189 input_data->x, input_data->len,
4190 signature, signature_size,
4191 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004192 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004193 /* The value of *signature_length is unspecified on error, but
4194 * whatever it is, it should be less than signature_size, so that
4195 * if the caller tries to read *signature_length bytes without
4196 * checking the error code then they don't overflow a buffer. */
4197 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004198
Gilles Peskine895242b2019-11-29 12:15:40 +01004199#if defined(MBEDTLS_TEST_DEPRECATED)
4200 signature_length = INVALID_EXPORT_LENGTH;
4201 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
4202 input_data->x, input_data->len,
4203 signature, signature_size,
4204 &signature_length ),
4205 expected_status );
4206 TEST_ASSERT( signature_length <= signature_size );
4207#endif /* MBEDTLS_TEST_DEPRECATED */
4208
Gilles Peskine20035e32018-02-03 22:44:14 +01004209exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004210 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004211 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01004212 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004213 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004214}
4215/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004216
4217/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004218void sign_verify( int key_type_arg, data_t *key_data,
4219 int alg_arg, data_t *input_data )
4220{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004221 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02004222 psa_key_type_t key_type = key_type_arg;
4223 psa_algorithm_t alg = alg_arg;
4224 size_t key_bits;
4225 unsigned char *signature = NULL;
4226 size_t signature_size;
4227 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004229
Gilles Peskine8817f612018-12-18 00:18:46 +01004230 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004231
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004232 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004233 psa_set_key_algorithm( &attributes, alg );
4234 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004235
Gilles Peskine049c7532019-05-15 20:22:09 +02004236 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4237 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004238 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4239 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004240
4241 /* Allocate a buffer which has the size advertized by the
4242 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004243 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004244 key_bits, alg );
4245 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004246 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004247 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004248
4249 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004250 PSA_ASSERT( psa_sign_hash( handle, alg,
4251 input_data->x, input_data->len,
4252 signature, signature_size,
4253 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004254 /* Check that the signature length looks sensible. */
4255 TEST_ASSERT( signature_length <= signature_size );
4256 TEST_ASSERT( signature_length > 0 );
4257
4258 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004259 PSA_ASSERT( psa_verify_hash( handle, alg,
4260 input_data->x, input_data->len,
4261 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004262
4263 if( input_data->len != 0 )
4264 {
4265 /* Flip a bit in the input and verify that the signature is now
4266 * detected as invalid. Flip a bit at the beginning, not at the end,
4267 * because ECDSA may ignore the last few bits of the input. */
4268 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004269 TEST_EQUAL( psa_verify_hash( handle, alg,
4270 input_data->x, input_data->len,
4271 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004272 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004273 }
4274
4275exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004276 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004277 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02004278 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004279 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004280}
4281/* END_CASE */
4282
4283/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004284void asymmetric_verify( int key_type_arg, data_t *key_data,
4285 int alg_arg, data_t *hash_data,
4286 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004287{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004288 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03004289 psa_key_type_t key_type = key_type_arg;
4290 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004291 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004292
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004293 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004294
Gilles Peskine8817f612018-12-18 00:18:46 +01004295 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004296
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004297 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004298 psa_set_key_algorithm( &attributes, alg );
4299 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004300
Gilles Peskine049c7532019-05-15 20:22:09 +02004301 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4302 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03004303
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004304 PSA_ASSERT( psa_verify_hash( handle, alg,
4305 hash_data->x, hash_data->len,
4306 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004307
4308#if defined(MBEDTLS_TEST_DEPRECATED)
4309 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
4310 hash_data->x, hash_data->len,
4311 signature_data->x,
4312 signature_data->len ) );
4313
4314#endif /* MBEDTLS_TEST_DEPRECATED */
4315
itayzafrir5c753392018-05-08 11:18:38 +03004316exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004317 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004318 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004319 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004320}
4321/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004322
4323/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004324void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4325 int alg_arg, data_t *hash_data,
4326 data_t *signature_data,
4327 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004328{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004329 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004330 psa_key_type_t key_type = key_type_arg;
4331 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004332 psa_status_t actual_status;
4333 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004335
Gilles Peskine8817f612018-12-18 00:18:46 +01004336 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004337
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004338 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004339 psa_set_key_algorithm( &attributes, alg );
4340 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004341
Gilles Peskine049c7532019-05-15 20:22:09 +02004342 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4343 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004344
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004345 actual_status = psa_verify_hash( handle, alg,
4346 hash_data->x, hash_data->len,
4347 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004348 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004349
Gilles Peskine895242b2019-11-29 12:15:40 +01004350#if defined(MBEDTLS_TEST_DEPRECATED)
4351 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
4352 hash_data->x, hash_data->len,
4353 signature_data->x, signature_data->len ),
4354 expected_status );
4355#endif /* MBEDTLS_TEST_DEPRECATED */
4356
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004357exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004358 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004359 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004360 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004361}
4362/* END_CASE */
4363
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004364/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004365void asymmetric_encrypt( int key_type_arg,
4366 data_t *key_data,
4367 int alg_arg,
4368 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004369 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004370 int expected_output_length_arg,
4371 int expected_status_arg )
4372{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004373 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004374 psa_key_type_t key_type = key_type_arg;
4375 psa_algorithm_t alg = alg_arg;
4376 size_t expected_output_length = expected_output_length_arg;
4377 size_t key_bits;
4378 unsigned char *output = NULL;
4379 size_t output_size;
4380 size_t output_length = ~0;
4381 psa_status_t actual_status;
4382 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004384
Gilles Peskine8817f612018-12-18 00:18:46 +01004385 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004386
Gilles Peskine656896e2018-06-29 19:12:28 +02004387 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004388 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4389 psa_set_key_algorithm( &attributes, alg );
4390 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004391 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4392 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004393
4394 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004395 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4396 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004397 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004398 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004399
4400 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004401 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004402 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004403 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004404 output, output_size,
4405 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004406 TEST_EQUAL( actual_status, expected_status );
4407 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004408
Gilles Peskine68428122018-06-30 18:42:41 +02004409 /* If the label is empty, the test framework puts a non-null pointer
4410 * in label->x. Test that a null pointer works as well. */
4411 if( label->len == 0 )
4412 {
4413 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004414 if( output_size != 0 )
4415 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004416 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004417 input_data->x, input_data->len,
4418 NULL, label->len,
4419 output, output_size,
4420 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004421 TEST_EQUAL( actual_status, expected_status );
4422 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004423 }
4424
Gilles Peskine656896e2018-06-29 19:12:28 +02004425exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004426 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004427 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004428 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004429 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004430}
4431/* END_CASE */
4432
4433/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004434void asymmetric_encrypt_decrypt( int key_type_arg,
4435 data_t *key_data,
4436 int alg_arg,
4437 data_t *input_data,
4438 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004439{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004440 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004441 psa_key_type_t key_type = key_type_arg;
4442 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004443 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004444 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004445 size_t output_size;
4446 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004447 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004448 size_t output2_size;
4449 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004451
Gilles Peskine8817f612018-12-18 00:18:46 +01004452 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004453
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004454 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4455 psa_set_key_algorithm( &attributes, alg );
4456 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004457
Gilles Peskine049c7532019-05-15 20:22:09 +02004458 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4459 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004460
4461 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004462 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4463 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004464 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004465 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004466 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004467 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004468
Gilles Peskineeebd7382018-06-08 18:11:54 +02004469 /* We test encryption by checking that encrypt-then-decrypt gives back
4470 * the original plaintext because of the non-optional random
4471 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004472 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4473 input_data->x, input_data->len,
4474 label->x, label->len,
4475 output, output_size,
4476 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004477 /* We don't know what ciphertext length to expect, but check that
4478 * it looks sensible. */
4479 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004480
Gilles Peskine8817f612018-12-18 00:18:46 +01004481 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4482 output, output_length,
4483 label->x, label->len,
4484 output2, output2_size,
4485 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004486 ASSERT_COMPARE( input_data->x, input_data->len,
4487 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004488
4489exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004490 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004491 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004492 mbedtls_free( output );
4493 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004494 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004495}
4496/* END_CASE */
4497
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004498/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004499void asymmetric_decrypt( int key_type_arg,
4500 data_t *key_data,
4501 int alg_arg,
4502 data_t *input_data,
4503 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004504 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004505{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004506 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004507 psa_key_type_t key_type = key_type_arg;
4508 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004509 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004510 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004511 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004513
Jaeden Amero412654a2019-02-06 12:57:46 +00004514 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004515 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004516
Gilles Peskine8817f612018-12-18 00:18:46 +01004517 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004518
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004519 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4520 psa_set_key_algorithm( &attributes, alg );
4521 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004522
Gilles Peskine049c7532019-05-15 20:22:09 +02004523 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4524 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004525
Gilles Peskine8817f612018-12-18 00:18:46 +01004526 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4527 input_data->x, input_data->len,
4528 label->x, label->len,
4529 output,
4530 output_size,
4531 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004532 ASSERT_COMPARE( expected_data->x, expected_data->len,
4533 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004534
Gilles Peskine68428122018-06-30 18:42:41 +02004535 /* If the label is empty, the test framework puts a non-null pointer
4536 * in label->x. Test that a null pointer works as well. */
4537 if( label->len == 0 )
4538 {
4539 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004540 if( output_size != 0 )
4541 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004542 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4543 input_data->x, input_data->len,
4544 NULL, label->len,
4545 output,
4546 output_size,
4547 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004548 ASSERT_COMPARE( expected_data->x, expected_data->len,
4549 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004550 }
4551
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004552exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004553 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004554 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004555 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004556 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004557}
4558/* END_CASE */
4559
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004560/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004561void asymmetric_decrypt_fail( int key_type_arg,
4562 data_t *key_data,
4563 int alg_arg,
4564 data_t *input_data,
4565 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004566 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004567 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004568{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004569 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004570 psa_key_type_t key_type = key_type_arg;
4571 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004572 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004573 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004574 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004575 psa_status_t actual_status;
4576 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004577 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004578
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004579 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004580
Gilles Peskine8817f612018-12-18 00:18:46 +01004581 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004582
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004583 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4584 psa_set_key_algorithm( &attributes, alg );
4585 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004586
Gilles Peskine049c7532019-05-15 20:22:09 +02004587 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4588 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004589
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004590 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004591 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004592 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004593 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004594 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004595 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004596 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004597
Gilles Peskine68428122018-06-30 18:42:41 +02004598 /* If the label is empty, the test framework puts a non-null pointer
4599 * in label->x. Test that a null pointer works as well. */
4600 if( label->len == 0 )
4601 {
4602 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004603 if( output_size != 0 )
4604 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004605 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004606 input_data->x, input_data->len,
4607 NULL, label->len,
4608 output, output_size,
4609 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004610 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004611 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004612 }
4613
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004614exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004615 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004616 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004617 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004618 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004619}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004620/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004621
4622/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004623void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004624{
4625 /* Test each valid way of initializing the object, except for `= {0}`, as
4626 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4627 * though it's OK by the C standard. We could test for this, but we'd need
4628 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004629 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004630 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4631 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4632 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004633
4634 memset( &zero, 0, sizeof( zero ) );
4635
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004636 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004637 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004638 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004639 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004640 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004641 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004642 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004643
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004645 PSA_ASSERT( psa_key_derivation_abort(&func) );
4646 PSA_ASSERT( psa_key_derivation_abort(&init) );
4647 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004648}
4649/* END_CASE */
4650
Janos Follath16de4a42019-06-13 16:32:24 +01004651/* BEGIN_CASE */
4652void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004653{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004654 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004655 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004656 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004657
Gilles Peskine8817f612018-12-18 00:18:46 +01004658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004659
Janos Follath16de4a42019-06-13 16:32:24 +01004660 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004661 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004662
4663exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004664 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004665 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004666}
4667/* END_CASE */
4668
Janos Follathaf3c2a02019-06-12 12:34:34 +01004669/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004670void derive_set_capacity( int alg_arg, int capacity_arg,
4671 int expected_status_arg )
4672{
4673 psa_algorithm_t alg = alg_arg;
4674 size_t capacity = capacity_arg;
4675 psa_status_t expected_status = expected_status_arg;
4676 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4677
4678 PSA_ASSERT( psa_crypto_init( ) );
4679
4680 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4681
4682 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4683 expected_status );
4684
4685exit:
4686 psa_key_derivation_abort( &operation );
4687 PSA_DONE( );
4688}
4689/* END_CASE */
4690
4691/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004692void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004693 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004694 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004695 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004696 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004697 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004698 int expected_status_arg3,
4699 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004700{
4701 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004702 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4703 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004704 psa_status_t expected_statuses[] = {expected_status_arg1,
4705 expected_status_arg2,
4706 expected_status_arg3};
4707 data_t *inputs[] = {input1, input2, input3};
4708 psa_key_handle_t handles[] = {0, 0, 0};
4709 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4710 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4711 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004712 psa_key_type_t output_key_type = output_key_type_arg;
4713 psa_key_handle_t output_handle = 0;
4714 psa_status_t expected_output_status = expected_output_status_arg;
4715 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004716
4717 PSA_ASSERT( psa_crypto_init( ) );
4718
4719 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4720 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004721
4722 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4723
4724 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4725 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004726 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004727 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004728 psa_set_key_type( &attributes, key_types[i] );
4729 PSA_ASSERT( psa_import_key( &attributes,
4730 inputs[i]->x, inputs[i]->len,
4731 &handles[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004732 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4733 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4734 {
4735 // When taking a private key as secret input, use key agreement
4736 // to add the shared secret to the derivation
4737 TEST_EQUAL( key_agreement_with_self( &operation, handles[i] ),
4738 expected_statuses[i] );
4739 }
4740 else
4741 {
4742 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4743 handles[i] ),
4744 expected_statuses[i] );
4745 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004746 }
4747 else
4748 {
4749 TEST_EQUAL( psa_key_derivation_input_bytes(
4750 &operation, steps[i],
4751 inputs[i]->x, inputs[i]->len ),
4752 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004753 }
4754 }
4755
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004756 if( output_key_type != PSA_KEY_TYPE_NONE )
4757 {
4758 psa_reset_key_attributes( &attributes );
4759 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4760 psa_set_key_bits( &attributes, 8 );
4761 actual_output_status =
4762 psa_key_derivation_output_key( &attributes, &operation,
4763 &output_handle );
4764 }
4765 else
4766 {
4767 uint8_t buffer[1];
4768 actual_output_status =
4769 psa_key_derivation_output_bytes( &operation,
4770 buffer, sizeof( buffer ) );
4771 }
4772 TEST_EQUAL( actual_output_status, expected_output_status );
4773
Janos Follathaf3c2a02019-06-12 12:34:34 +01004774exit:
4775 psa_key_derivation_abort( &operation );
4776 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4777 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004778 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004779 PSA_DONE( );
4780}
4781/* END_CASE */
4782
Janos Follathd958bb72019-07-03 15:02:16 +01004783/* BEGIN_CASE */
4784void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004785{
Janos Follathd958bb72019-07-03 15:02:16 +01004786 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004787 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004788 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004790 unsigned char input1[] = "Input 1";
4791 size_t input1_length = sizeof( input1 );
4792 unsigned char input2[] = "Input 2";
4793 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004794 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004795 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004796 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4797 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4798 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004799 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004800
Gilles Peskine8817f612018-12-18 00:18:46 +01004801 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004802
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004803 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4804 psa_set_key_algorithm( &attributes, alg );
4805 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004806
Gilles Peskine73676cb2019-05-15 20:15:10 +02004807 PSA_ASSERT( psa_import_key( &attributes,
4808 key_data, sizeof( key_data ),
4809 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004810
4811 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004812 if( !setup_key_derivation_wrap( &operation, handle, alg,
4813 input1, input1_length,
4814 input2, input2_length,
4815 capacity ) )
4816 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004817
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004818 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004819 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004820 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004821
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004822 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004823
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004825 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004826
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004827exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004829 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004830 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004831}
4832/* END_CASE */
4833
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004834/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004835void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004836{
4837 uint8_t output_buffer[16];
4838 size_t buffer_size = 16;
4839 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004840 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004841
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004842 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4843 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004844 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004845
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004846 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004847 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004848
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004850
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004851 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4852 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004853 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004854
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004855 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004856 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004857
4858exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004859 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004860}
4861/* END_CASE */
4862
4863/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004864void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004865 int step1_arg, data_t *input1,
4866 int step2_arg, data_t *input2,
4867 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004868 int requested_capacity_arg,
4869 data_t *expected_output1,
4870 data_t *expected_output2 )
4871{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004872 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004873 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4874 data_t *inputs[] = {input1, input2, input3};
4875 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004876 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004877 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004878 uint8_t *expected_outputs[2] =
4879 {expected_output1->x, expected_output2->x};
4880 size_t output_sizes[2] =
4881 {expected_output1->len, expected_output2->len};
4882 size_t output_buffer_size = 0;
4883 uint8_t *output_buffer = NULL;
4884 size_t expected_capacity;
4885 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004887 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004888 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004889
4890 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4891 {
4892 if( output_sizes[i] > output_buffer_size )
4893 output_buffer_size = output_sizes[i];
4894 if( output_sizes[i] == 0 )
4895 expected_outputs[i] = NULL;
4896 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004897 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004898 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004899
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004900 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4901 psa_set_key_algorithm( &attributes, alg );
4902 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004903
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004904 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004905 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4906 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4907 requested_capacity ) );
4908 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004910 switch( steps[i] )
4911 {
4912 case 0:
4913 break;
4914 case PSA_KEY_DERIVATION_INPUT_SECRET:
4915 PSA_ASSERT( psa_import_key( &attributes,
4916 inputs[i]->x, inputs[i]->len,
4917 &handles[i] ) );
4918 PSA_ASSERT( psa_key_derivation_input_key(
4919 &operation, steps[i],
4920 handles[i] ) );
4921 break;
4922 default:
4923 PSA_ASSERT( psa_key_derivation_input_bytes(
4924 &operation, steps[i],
4925 inputs[i]->x, inputs[i]->len ) );
4926 break;
4927 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004928 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004929
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004930 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004931 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004932 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004933 expected_capacity = requested_capacity;
4934
4935 /* Expansion phase. */
4936 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4937 {
4938 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004939 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004940 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004941 if( expected_capacity == 0 && output_sizes[i] == 0 )
4942 {
4943 /* Reading 0 bytes when 0 bytes are available can go either way. */
4944 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004945 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004946 continue;
4947 }
4948 else if( expected_capacity == 0 ||
4949 output_sizes[i] > expected_capacity )
4950 {
4951 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004952 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004953 expected_capacity = 0;
4954 continue;
4955 }
4956 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004957 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004958 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004959 ASSERT_COMPARE( output_buffer, output_sizes[i],
4960 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004962 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004964 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004965 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004966 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004968
4969exit:
4970 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004972 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4973 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004974 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004975}
4976/* END_CASE */
4977
4978/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004979void derive_full( int alg_arg,
4980 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004981 data_t *input1,
4982 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004983 int requested_capacity_arg )
4984{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004985 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004986 psa_algorithm_t alg = alg_arg;
4987 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004988 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004989 unsigned char output_buffer[16];
4990 size_t expected_capacity = requested_capacity;
4991 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004993
Gilles Peskine8817f612018-12-18 00:18:46 +01004994 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004995
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4997 psa_set_key_algorithm( &attributes, alg );
4998 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004999
Gilles Peskine049c7532019-05-15 20:22:09 +02005000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5001 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005002
Janos Follathf2815ea2019-07-03 12:41:36 +01005003 if( !setup_key_derivation_wrap( &operation, handle, alg,
5004 input1->x, input1->len,
5005 input2->x, input2->len,
5006 requested_capacity ) )
5007 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005008
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005010 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005011 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005012
5013 /* Expansion phase. */
5014 while( current_capacity > 0 )
5015 {
5016 size_t read_size = sizeof( output_buffer );
5017 if( read_size > current_capacity )
5018 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005019 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005020 output_buffer,
5021 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005022 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005023 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005024 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005025 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005026 }
5027
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005028 /* Check that the operation refuses to go over capacity. */
5029 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005030 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005031
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005033
5034exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005035 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005036 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005037 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005038}
5039/* END_CASE */
5040
Janos Follathe60c9052019-07-03 13:51:30 +01005041/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005042void derive_key_exercise( int alg_arg,
5043 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005044 data_t *input1,
5045 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005046 int derived_type_arg,
5047 int derived_bits_arg,
5048 int derived_usage_arg,
5049 int derived_alg_arg )
5050{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005051 psa_key_handle_t base_handle = 0;
5052 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005053 psa_algorithm_t alg = alg_arg;
5054 psa_key_type_t derived_type = derived_type_arg;
5055 size_t derived_bits = derived_bits_arg;
5056 psa_key_usage_t derived_usage = derived_usage_arg;
5057 psa_algorithm_t derived_alg = derived_alg_arg;
5058 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005059 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005061 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005062
Gilles Peskine8817f612018-12-18 00:18:46 +01005063 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005064
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005065 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5066 psa_set_key_algorithm( &attributes, alg );
5067 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005068 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5069 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005070
5071 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01005072 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
5073 input1->x, input1->len,
5074 input2->x, input2->len, capacity ) )
5075 goto exit;
5076
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005077 psa_set_key_usage_flags( &attributes, derived_usage );
5078 psa_set_key_algorithm( &attributes, derived_alg );
5079 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005080 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005082 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005083
5084 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005085 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
5086 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5087 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005088
5089 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005090 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005091 goto exit;
5092
5093exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005095 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005096 psa_destroy_key( base_handle );
5097 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005098 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005099}
5100/* END_CASE */
5101
Janos Follath42fd8882019-07-03 14:17:09 +01005102/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005103void derive_key_export( int alg_arg,
5104 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005105 data_t *input1,
5106 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005107 int bytes1_arg,
5108 int bytes2_arg )
5109{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005110 psa_key_handle_t base_handle = 0;
5111 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005112 psa_algorithm_t alg = alg_arg;
5113 size_t bytes1 = bytes1_arg;
5114 size_t bytes2 = bytes2_arg;
5115 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005117 uint8_t *output_buffer = NULL;
5118 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005119 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5120 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005121 size_t length;
5122
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005123 ASSERT_ALLOC( output_buffer, capacity );
5124 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005126
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005127 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5128 psa_set_key_algorithm( &base_attributes, alg );
5129 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005130 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5131 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005132
5133 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01005134 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5135 input1->x, input1->len,
5136 input2->x, input2->len, capacity ) )
5137 goto exit;
5138
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005140 output_buffer,
5141 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005142 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005143
5144 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01005145 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5146 input1->x, input1->len,
5147 input2->x, input2->len, capacity ) )
5148 goto exit;
5149
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005150 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5151 psa_set_key_algorithm( &derived_attributes, 0 );
5152 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005153 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005154 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005155 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005156 PSA_ASSERT( psa_export_key( derived_handle,
5157 export_buffer, bytes1,
5158 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005159 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01005160 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005161 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005163 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005164 PSA_ASSERT( psa_export_key( derived_handle,
5165 export_buffer + bytes1, bytes2,
5166 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005167 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005168
5169 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005170 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5171 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005172
5173exit:
5174 mbedtls_free( output_buffer );
5175 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005176 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005177 psa_destroy_key( base_handle );
5178 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005179 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005180}
5181/* END_CASE */
5182
5183/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005184void derive_key( int alg_arg,
5185 data_t *key_data, data_t *input1, data_t *input2,
5186 int type_arg, int bits_arg,
5187 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005188{
5189 psa_key_handle_t base_handle = 0;
5190 psa_key_handle_t derived_handle = 0;
5191 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005192 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005193 size_t bits = bits_arg;
5194 psa_status_t expected_status = expected_status_arg;
5195 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5196 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5197 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5198
5199 PSA_ASSERT( psa_crypto_init( ) );
5200
5201 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5202 psa_set_key_algorithm( &base_attributes, alg );
5203 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5204 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5205 &base_handle ) );
5206
5207 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5208 input1->x, input1->len,
5209 input2->x, input2->len, SIZE_MAX ) )
5210 goto exit;
5211
5212 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5213 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005214 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005215 psa_set_key_bits( &derived_attributes, bits );
5216 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
5217 &derived_handle ),
5218 expected_status );
5219
5220exit:
5221 psa_key_derivation_abort( &operation );
5222 psa_destroy_key( base_handle );
5223 psa_destroy_key( derived_handle );
5224 PSA_DONE( );
5225}
5226/* END_CASE */
5227
5228/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005229void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005230 int our_key_type_arg, int our_key_alg_arg,
5231 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005232 int expected_status_arg )
5233{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005234 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005235 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005236 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005237 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005238 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005240 psa_status_t expected_status = expected_status_arg;
5241 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005242
Gilles Peskine8817f612018-12-18 00:18:46 +01005243 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005244
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005245 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005246 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005247 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005248 PSA_ASSERT( psa_import_key( &attributes,
5249 our_key_data->x, our_key_data->len,
5250 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005251
Gilles Peskine77f40d82019-04-11 21:27:06 +02005252 /* The tests currently include inputs that should fail at either step.
5253 * Test cases that fail at the setup step should be changed to call
5254 * key_derivation_setup instead, and this function should be renamed
5255 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005257 if( status == PSA_SUCCESS )
5258 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005259 TEST_EQUAL( psa_key_derivation_key_agreement(
5260 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5261 our_key,
5262 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005263 expected_status );
5264 }
5265 else
5266 {
5267 TEST_ASSERT( status == expected_status );
5268 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005269
5270exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005272 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005273 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005274}
5275/* END_CASE */
5276
5277/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005278void raw_key_agreement( int alg_arg,
5279 int our_key_type_arg, data_t *our_key_data,
5280 data_t *peer_key_data,
5281 data_t *expected_output )
5282{
5283 psa_key_handle_t our_key = 0;
5284 psa_algorithm_t alg = alg_arg;
5285 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005287 unsigned char *output = NULL;
5288 size_t output_length = ~0;
5289
5290 ASSERT_ALLOC( output, expected_output->len );
5291 PSA_ASSERT( psa_crypto_init( ) );
5292
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005293 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5294 psa_set_key_algorithm( &attributes, alg );
5295 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005296 PSA_ASSERT( psa_import_key( &attributes,
5297 our_key_data->x, our_key_data->len,
5298 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005299
Gilles Peskinebe697d82019-05-16 18:00:41 +02005300 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5301 peer_key_data->x, peer_key_data->len,
5302 output, expected_output->len,
5303 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005304 ASSERT_COMPARE( output, output_length,
5305 expected_output->x, expected_output->len );
5306
5307exit:
5308 mbedtls_free( output );
5309 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005310 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005311}
5312/* END_CASE */
5313
5314/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005315void key_agreement_capacity( int alg_arg,
5316 int our_key_type_arg, data_t *our_key_data,
5317 data_t *peer_key_data,
5318 int expected_capacity_arg )
5319{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005320 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005321 psa_algorithm_t alg = alg_arg;
5322 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005323 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005325 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005326 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005327
Gilles Peskine8817f612018-12-18 00:18:46 +01005328 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005329
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005330 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5331 psa_set_key_algorithm( &attributes, alg );
5332 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005333 PSA_ASSERT( psa_import_key( &attributes,
5334 our_key_data->x, our_key_data->len,
5335 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005336
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005338 PSA_ASSERT( psa_key_derivation_key_agreement(
5339 &operation,
5340 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5341 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005342 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5343 {
5344 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005345 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005346 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005347 NULL, 0 ) );
5348 }
Gilles Peskine59685592018-09-18 12:11:34 +02005349
Gilles Peskinebf491972018-10-25 22:36:12 +02005350 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005351 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005352 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005353 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005354
Gilles Peskinebf491972018-10-25 22:36:12 +02005355 /* Test the actual capacity by reading the output. */
5356 while( actual_capacity > sizeof( output ) )
5357 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005358 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005359 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005360 actual_capacity -= sizeof( output );
5361 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005362 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005363 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005364 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005365 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005366
Gilles Peskine59685592018-09-18 12:11:34 +02005367exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005368 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005369 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005370 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005371}
5372/* END_CASE */
5373
5374/* BEGIN_CASE */
5375void key_agreement_output( int alg_arg,
5376 int our_key_type_arg, data_t *our_key_data,
5377 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005378 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005379{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005380 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005381 psa_algorithm_t alg = alg_arg;
5382 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005383 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005384 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005385 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005386
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005387 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5388 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005389
Gilles Peskine8817f612018-12-18 00:18:46 +01005390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005391
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005392 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5393 psa_set_key_algorithm( &attributes, alg );
5394 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005395 PSA_ASSERT( psa_import_key( &attributes,
5396 our_key_data->x, our_key_data->len,
5397 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005398
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005399 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005400 PSA_ASSERT( psa_key_derivation_key_agreement(
5401 &operation,
5402 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5403 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005404 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5405 {
5406 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005407 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005408 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005409 NULL, 0 ) );
5410 }
Gilles Peskine59685592018-09-18 12:11:34 +02005411
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005412 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005413 actual_output,
5414 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005415 ASSERT_COMPARE( actual_output, expected_output1->len,
5416 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005417 if( expected_output2->len != 0 )
5418 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005419 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005420 actual_output,
5421 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005422 ASSERT_COMPARE( actual_output, expected_output2->len,
5423 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005424 }
Gilles Peskine59685592018-09-18 12:11:34 +02005425
5426exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005427 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005428 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005429 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005430 mbedtls_free( actual_output );
5431}
5432/* END_CASE */
5433
5434/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005435void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005436{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005437 size_t bytes = bytes_arg;
5438 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005439 unsigned char *output = NULL;
5440 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005441 size_t i;
5442 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005443
Simon Butcher49f8e312020-03-03 15:51:50 +00005444 TEST_ASSERT( bytes_arg >= 0 );
5445
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005446 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5447 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005448 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005449
Gilles Peskine8817f612018-12-18 00:18:46 +01005450 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005451
Gilles Peskinea50d7392018-06-21 10:22:13 +02005452 /* Run several times, to ensure that every output byte will be
5453 * nonzero at least once with overwhelming probability
5454 * (2^(-8*number_of_runs)). */
5455 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005456 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005457 if( bytes != 0 )
5458 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005459 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005460
5461 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005462 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5463 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005464
5465 for( i = 0; i < bytes; i++ )
5466 {
5467 if( output[i] != 0 )
5468 ++changed[i];
5469 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005470 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005471
5472 /* Check that every byte was changed to nonzero at least once. This
5473 * validates that psa_generate_random is overwriting every byte of
5474 * the output buffer. */
5475 for( i = 0; i < bytes; i++ )
5476 {
5477 TEST_ASSERT( changed[i] != 0 );
5478 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005479
5480exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005481 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005482 mbedtls_free( output );
5483 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005484}
5485/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005486
5487/* BEGIN_CASE */
5488void generate_key( int type_arg,
5489 int bits_arg,
5490 int usage_arg,
5491 int alg_arg,
5492 int expected_status_arg )
5493{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005494 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005495 psa_key_type_t type = type_arg;
5496 psa_key_usage_t usage = usage_arg;
5497 size_t bits = bits_arg;
5498 psa_algorithm_t alg = alg_arg;
5499 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005501 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005502
Gilles Peskine8817f612018-12-18 00:18:46 +01005503 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005504
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005505 psa_set_key_usage_flags( &attributes, usage );
5506 psa_set_key_algorithm( &attributes, alg );
5507 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005508 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005509
5510 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005511 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005512 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005513 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005514
5515 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005516 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5517 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5518 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005519
Gilles Peskine818ca122018-06-20 18:16:48 +02005520 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005521 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005522 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005523
5524exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005525 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005526 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005527 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005528}
5529/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005530
Gilles Peskinee56e8782019-04-26 17:34:02 +02005531/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5532void generate_key_rsa( int bits_arg,
5533 data_t *e_arg,
5534 int expected_status_arg )
5535{
5536 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005537 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005538 size_t bits = bits_arg;
5539 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5540 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5541 psa_status_t expected_status = expected_status_arg;
5542 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5543 uint8_t *exported = NULL;
5544 size_t exported_size =
5545 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5546 size_t exported_length = SIZE_MAX;
5547 uint8_t *e_read_buffer = NULL;
5548 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005549 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005550 size_t e_read_length = SIZE_MAX;
5551
5552 if( e_arg->len == 0 ||
5553 ( e_arg->len == 3 &&
5554 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5555 {
5556 is_default_public_exponent = 1;
5557 e_read_size = 0;
5558 }
5559 ASSERT_ALLOC( e_read_buffer, e_read_size );
5560 ASSERT_ALLOC( exported, exported_size );
5561
5562 PSA_ASSERT( psa_crypto_init( ) );
5563
5564 psa_set_key_usage_flags( &attributes, usage );
5565 psa_set_key_algorithm( &attributes, alg );
5566 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5567 e_arg->x, e_arg->len ) );
5568 psa_set_key_bits( &attributes, bits );
5569
5570 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005571 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005572 if( expected_status != PSA_SUCCESS )
5573 goto exit;
5574
5575 /* Test the key information */
5576 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5577 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5578 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5579 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5580 e_read_buffer, e_read_size,
5581 &e_read_length ) );
5582 if( is_default_public_exponent )
5583 TEST_EQUAL( e_read_length, 0 );
5584 else
5585 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5586
5587 /* Do something with the key according to its type and permitted usage. */
5588 if( ! exercise_key( handle, usage, alg ) )
5589 goto exit;
5590
5591 /* Export the key and check the public exponent. */
5592 PSA_ASSERT( psa_export_public_key( handle,
5593 exported, exported_size,
5594 &exported_length ) );
5595 {
5596 uint8_t *p = exported;
5597 uint8_t *end = exported + exported_length;
5598 size_t len;
5599 /* RSAPublicKey ::= SEQUENCE {
5600 * modulus INTEGER, -- n
5601 * publicExponent INTEGER } -- e
5602 */
5603 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005604 MBEDTLS_ASN1_SEQUENCE |
5605 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005606 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5607 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5608 MBEDTLS_ASN1_INTEGER ) );
5609 if( len >= 1 && p[0] == 0 )
5610 {
5611 ++p;
5612 --len;
5613 }
5614 if( e_arg->len == 0 )
5615 {
5616 TEST_EQUAL( len, 3 );
5617 TEST_EQUAL( p[0], 1 );
5618 TEST_EQUAL( p[1], 0 );
5619 TEST_EQUAL( p[2], 1 );
5620 }
5621 else
5622 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5623 }
5624
5625exit:
5626 psa_reset_key_attributes( &attributes );
5627 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005628 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005629 mbedtls_free( e_read_buffer );
5630 mbedtls_free( exported );
5631}
5632/* END_CASE */
5633
Darryl Greend49a4992018-06-18 17:27:26 +01005634/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005635void persistent_key_load_key_from_storage( data_t *data,
5636 int type_arg, int bits_arg,
5637 int usage_flags_arg, int alg_arg,
5638 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005639{
Ronald Cron71016a92020-08-28 19:01:50 +02005640 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005642 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005643 psa_key_handle_t base_key = 0;
5644 psa_key_type_t type = type_arg;
5645 size_t bits = bits_arg;
5646 psa_key_usage_t usage_flags = usage_flags_arg;
5647 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005648 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005649 unsigned char *first_export = NULL;
5650 unsigned char *second_export = NULL;
5651 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5652 size_t first_exported_length;
5653 size_t second_exported_length;
5654
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005655 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5656 {
5657 ASSERT_ALLOC( first_export, export_size );
5658 ASSERT_ALLOC( second_export, export_size );
5659 }
Darryl Greend49a4992018-06-18 17:27:26 +01005660
Gilles Peskine8817f612018-12-18 00:18:46 +01005661 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005662
Gilles Peskinec87af662019-05-15 16:12:22 +02005663 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005664 psa_set_key_usage_flags( &attributes, usage_flags );
5665 psa_set_key_algorithm( &attributes, alg );
5666 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005667 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005668
Darryl Green0c6575a2018-11-07 16:05:30 +00005669 switch( generation_method )
5670 {
5671 case IMPORT_KEY:
5672 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005673 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5674 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005675 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005676
Darryl Green0c6575a2018-11-07 16:05:30 +00005677 case GENERATE_KEY:
5678 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005679 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005680 break;
5681
5682 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005683 {
5684 /* Create base key */
5685 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5686 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5687 psa_set_key_usage_flags( &base_attributes,
5688 PSA_KEY_USAGE_DERIVE );
5689 psa_set_key_algorithm( &base_attributes, derive_alg );
5690 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005691 PSA_ASSERT( psa_import_key( &base_attributes,
5692 data->x, data->len,
5693 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005694 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005695 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005696 PSA_ASSERT( psa_key_derivation_input_key(
5697 &operation,
5698 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005699 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005700 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005701 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005702 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5703 &operation,
5704 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005705 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005706 PSA_ASSERT( psa_destroy_key( base_key ) );
5707 base_key = 0;
5708 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005709 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005710 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005711 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005712
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005713 /* Export the key if permitted by the key policy. */
5714 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5715 {
5716 PSA_ASSERT( psa_export_key( handle,
5717 first_export, export_size,
5718 &first_exported_length ) );
5719 if( generation_method == IMPORT_KEY )
5720 ASSERT_COMPARE( data->x, data->len,
5721 first_export, first_exported_length );
5722 }
Darryl Greend49a4992018-06-18 17:27:26 +01005723
5724 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005725 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005726 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005727 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005728
Darryl Greend49a4992018-06-18 17:27:26 +01005729 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005730 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005731 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005732 TEST_ASSERT( mbedtls_svc_key_id_equal(
5733 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005734 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5735 PSA_KEY_LIFETIME_PERSISTENT );
5736 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5737 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5738 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5739 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005740
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005741 /* Export the key again if permitted by the key policy. */
5742 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005743 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005744 PSA_ASSERT( psa_export_key( handle,
5745 second_export, export_size,
5746 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005747 ASSERT_COMPARE( first_export, first_exported_length,
5748 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005749 }
5750
5751 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005752 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005753 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005754
5755exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005756 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005757 mbedtls_free( first_export );
5758 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005759 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005760 psa_destroy_key( base_key );
5761 if( handle == 0 )
5762 {
5763 /* In case there was a test failure after creating the persistent key
5764 * but while it was not open, try to re-open the persistent key
5765 * to delete it. */
Gilles Peskinee92c68a2020-08-26 00:06:25 +02005766 (void) psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005767 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005768 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005769 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005770}
5771/* END_CASE */