blob: 3e3a7a268ee2577afd054ccefece272a7d8695b5 [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 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001328void attributes_set_get( int id_arg, int lifetime_arg,
1329 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 Cron71016a92020-08-28 19:01:50 +02001333 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( 1, 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,
2211 int exercise_alg )
2212{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002213 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002215 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002216 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002217 psa_status_t status;
2218
Gilles Peskine8817f612018-12-18 00:18:46 +01002219 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002220
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002221 psa_set_key_usage_flags( &attributes, policy_usage );
2222 psa_set_key_algorithm( &attributes, policy_alg );
2223 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002224
Gilles Peskine049c7532019-05-15 20:22:09 +02002225 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2226 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002227
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002228 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2229 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002230
Gilles Peskine01d718c2018-09-18 12:01:02 +02002231 if( policy_alg == exercise_alg &&
2232 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002233 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002234 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002235 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002236
2237exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002238 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002239 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002240 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002241}
2242/* END_CASE */
2243
2244/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002245void key_policy_alg2( int key_type_arg, data_t *key_data,
2246 int usage_arg, int alg_arg, int alg2_arg )
2247{
2248 psa_key_handle_t handle = 0;
2249 psa_key_type_t key_type = key_type_arg;
2250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2251 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2252 psa_key_usage_t usage = usage_arg;
2253 psa_algorithm_t alg = alg_arg;
2254 psa_algorithm_t alg2 = alg2_arg;
2255
2256 PSA_ASSERT( psa_crypto_init( ) );
2257
2258 psa_set_key_usage_flags( &attributes, usage );
2259 psa_set_key_algorithm( &attributes, alg );
2260 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2261 psa_set_key_type( &attributes, key_type );
2262 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2263 &handle ) );
2264
2265 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2266 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2267 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2268 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2269
2270 if( ! exercise_key( handle, usage, alg ) )
2271 goto exit;
2272 if( ! exercise_key( handle, usage, alg2 ) )
2273 goto exit;
2274
2275exit:
2276 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002277 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002278}
2279/* END_CASE */
2280
2281/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002282void raw_agreement_key_policy( int policy_usage,
2283 int policy_alg,
2284 int key_type_arg,
2285 data_t *key_data,
2286 int exercise_alg )
2287{
2288 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002290 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002291 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002292 psa_status_t status;
2293
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
2305 if( policy_alg == exercise_alg &&
2306 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2307 PSA_ASSERT( status );
2308 else
2309 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2310
2311exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002312 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002313 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002314 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002315}
2316/* END_CASE */
2317
2318/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002319void copy_success( int source_usage_arg,
2320 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002321 int type_arg, data_t *material,
2322 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002323 int target_usage_arg,
2324 int target_alg_arg, int target_alg2_arg,
2325 int expected_usage_arg,
2326 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002327{
Gilles Peskineca25db92019-04-19 11:43:08 +02002328 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2329 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002330 psa_key_usage_t expected_usage = expected_usage_arg;
2331 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002332 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002333 psa_key_handle_t source_handle = 0;
2334 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002335 uint8_t *export_buffer = NULL;
2336
Gilles Peskine57ab7212019-01-28 13:03:09 +01002337 PSA_ASSERT( psa_crypto_init( ) );
2338
Gilles Peskineca25db92019-04-19 11:43:08 +02002339 /* Prepare the source key. */
2340 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2341 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002342 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002343 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002344 PSA_ASSERT( psa_import_key( &source_attributes,
2345 material->x, material->len,
2346 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002347 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002348
Gilles Peskineca25db92019-04-19 11:43:08 +02002349 /* Prepare the target attributes. */
2350 if( copy_attributes )
2351 target_attributes = source_attributes;
2352 if( target_usage_arg != -1 )
2353 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2354 if( target_alg_arg != -1 )
2355 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002356 if( target_alg2_arg != -1 )
2357 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002358
2359 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002360 PSA_ASSERT( psa_copy_key( source_handle,
2361 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002362
2363 /* Destroy the source to ensure that this doesn't affect the target. */
2364 PSA_ASSERT( psa_destroy_key( source_handle ) );
2365
2366 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002367 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2368 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2369 psa_get_key_type( &target_attributes ) );
2370 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2371 psa_get_key_bits( &target_attributes ) );
2372 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2373 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002374 TEST_EQUAL( expected_alg2,
2375 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002376 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2377 {
2378 size_t length;
2379 ASSERT_ALLOC( export_buffer, material->len );
2380 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2381 material->len, &length ) );
2382 ASSERT_COMPARE( material->x, material->len,
2383 export_buffer, length );
2384 }
2385 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2386 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002387 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2388 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002389
2390 PSA_ASSERT( psa_close_key( target_handle ) );
2391
2392exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002393 psa_reset_key_attributes( &source_attributes );
2394 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002395 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002396 mbedtls_free( export_buffer );
2397}
2398/* END_CASE */
2399
2400/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002401void copy_fail( int source_usage_arg,
2402 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002403 int type_arg, data_t *material,
2404 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002405 int target_usage_arg,
2406 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002407 int expected_status_arg )
2408{
2409 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2410 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2411 psa_key_handle_t source_handle = 0;
2412 psa_key_handle_t target_handle = 0;
2413
2414 PSA_ASSERT( psa_crypto_init( ) );
2415
2416 /* Prepare the source key. */
2417 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2418 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002419 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002420 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002421 PSA_ASSERT( psa_import_key( &source_attributes,
2422 material->x, material->len,
2423 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002424
2425 /* Prepare the target attributes. */
2426 psa_set_key_type( &target_attributes, target_type_arg );
2427 psa_set_key_bits( &target_attributes, target_bits_arg );
2428 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2429 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002430 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002431
2432 /* Try to copy the key. */
2433 TEST_EQUAL( psa_copy_key( source_handle,
2434 &target_attributes, &target_handle ),
2435 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002436
2437 PSA_ASSERT( psa_destroy_key( source_handle ) );
2438
Gilles Peskine4a644642019-05-03 17:14:08 +02002439exit:
2440 psa_reset_key_attributes( &source_attributes );
2441 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002442 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002443}
2444/* END_CASE */
2445
2446/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002447void hash_operation_init( )
2448{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002449 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002450 /* Test each valid way of initializing the object, except for `= {0}`, as
2451 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2452 * though it's OK by the C standard. We could test for this, but we'd need
2453 * to supress the Clang warning for the test. */
2454 psa_hash_operation_t func = psa_hash_operation_init( );
2455 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2456 psa_hash_operation_t zero;
2457
2458 memset( &zero, 0, sizeof( zero ) );
2459
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002460 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002461 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2462 PSA_ERROR_BAD_STATE );
2463 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2464 PSA_ERROR_BAD_STATE );
2465 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2466 PSA_ERROR_BAD_STATE );
2467
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002468 /* A default hash operation should be abortable without error. */
2469 PSA_ASSERT( psa_hash_abort( &func ) );
2470 PSA_ASSERT( psa_hash_abort( &init ) );
2471 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002472}
2473/* END_CASE */
2474
2475/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476void hash_setup( int alg_arg,
2477 int expected_status_arg )
2478{
2479 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002480 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002481 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002482 psa_status_t status;
2483
Gilles Peskine8817f612018-12-18 00:18:46 +01002484 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002485
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002486 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002487 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002488
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002489 /* Whether setup succeeded or failed, abort must succeed. */
2490 PSA_ASSERT( psa_hash_abort( &operation ) );
2491
2492 /* If setup failed, reproduce the failure, so as to
2493 * test the resulting state of the operation object. */
2494 if( status != PSA_SUCCESS )
2495 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2496
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002497 /* Now the operation object should be reusable. */
2498#if defined(KNOWN_SUPPORTED_HASH_ALG)
2499 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2500 PSA_ASSERT( psa_hash_abort( &operation ) );
2501#endif
2502
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002503exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002504 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002505}
2506/* END_CASE */
2507
2508/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002509void hash_compute_fail( int alg_arg, data_t *input,
2510 int output_size_arg, int expected_status_arg )
2511{
2512 psa_algorithm_t alg = alg_arg;
2513 uint8_t *output = NULL;
2514 size_t output_size = output_size_arg;
2515 size_t output_length = INVALID_EXPORT_LENGTH;
2516 psa_status_t expected_status = expected_status_arg;
2517 psa_status_t status;
2518
2519 ASSERT_ALLOC( output, output_size );
2520
2521 PSA_ASSERT( psa_crypto_init( ) );
2522
2523 status = psa_hash_compute( alg, input->x, input->len,
2524 output, output_size, &output_length );
2525 TEST_EQUAL( status, expected_status );
2526 TEST_ASSERT( output_length <= output_size );
2527
2528exit:
2529 mbedtls_free( output );
2530 PSA_DONE( );
2531}
2532/* END_CASE */
2533
2534/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002535void hash_compare_fail( int alg_arg, data_t *input,
2536 data_t *reference_hash,
2537 int expected_status_arg )
2538{
2539 psa_algorithm_t alg = alg_arg;
2540 psa_status_t expected_status = expected_status_arg;
2541 psa_status_t status;
2542
2543 PSA_ASSERT( psa_crypto_init( ) );
2544
2545 status = psa_hash_compare( alg, input->x, input->len,
2546 reference_hash->x, reference_hash->len );
2547 TEST_EQUAL( status, expected_status );
2548
2549exit:
2550 PSA_DONE( );
2551}
2552/* END_CASE */
2553
2554/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002555void hash_compute_compare( int alg_arg, data_t *input,
2556 data_t *expected_output )
2557{
2558 psa_algorithm_t alg = alg_arg;
2559 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2560 size_t output_length = INVALID_EXPORT_LENGTH;
2561 size_t i;
2562
2563 PSA_ASSERT( psa_crypto_init( ) );
2564
2565 /* Compute with tight buffer */
2566 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2567 output, PSA_HASH_SIZE( alg ),
2568 &output_length ) );
2569 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2570 ASSERT_COMPARE( output, output_length,
2571 expected_output->x, expected_output->len );
2572
2573 /* Compute with larger buffer */
2574 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2575 output, sizeof( output ),
2576 &output_length ) );
2577 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2578 ASSERT_COMPARE( output, output_length,
2579 expected_output->x, expected_output->len );
2580
2581 /* Compare with correct hash */
2582 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2583 output, output_length ) );
2584
2585 /* Compare with trailing garbage */
2586 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2587 output, output_length + 1 ),
2588 PSA_ERROR_INVALID_SIGNATURE );
2589
2590 /* Compare with truncated hash */
2591 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2592 output, output_length - 1 ),
2593 PSA_ERROR_INVALID_SIGNATURE );
2594
2595 /* Compare with corrupted value */
2596 for( i = 0; i < output_length; i++ )
2597 {
2598 test_set_step( i );
2599 output[i] ^= 1;
2600 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2601 output, output_length ),
2602 PSA_ERROR_INVALID_SIGNATURE );
2603 output[i] ^= 1;
2604 }
2605
2606exit:
2607 PSA_DONE( );
2608}
2609/* END_CASE */
2610
2611/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002612void hash_bad_order( )
2613{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002614 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002615 unsigned char input[] = "";
2616 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002617 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002618 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2619 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2620 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002621 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002622 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002623 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002626
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002627 /* Call setup twice in a row. */
2628 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2629 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2630 PSA_ERROR_BAD_STATE );
2631 PSA_ASSERT( psa_hash_abort( &operation ) );
2632
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002633 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002634 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002635 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002636 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002637
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002638 /* Call update after finish. */
2639 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2640 PSA_ASSERT( psa_hash_finish( &operation,
2641 hash, sizeof( hash ), &hash_len ) );
2642 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002643 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002644 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002645
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002646 /* Call verify without calling setup beforehand. */
2647 TEST_EQUAL( psa_hash_verify( &operation,
2648 valid_hash, sizeof( valid_hash ) ),
2649 PSA_ERROR_BAD_STATE );
2650 PSA_ASSERT( psa_hash_abort( &operation ) );
2651
2652 /* Call verify after finish. */
2653 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2654 PSA_ASSERT( psa_hash_finish( &operation,
2655 hash, sizeof( hash ), &hash_len ) );
2656 TEST_EQUAL( psa_hash_verify( &operation,
2657 valid_hash, sizeof( valid_hash ) ),
2658 PSA_ERROR_BAD_STATE );
2659 PSA_ASSERT( psa_hash_abort( &operation ) );
2660
2661 /* Call verify twice in a row. */
2662 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2663 PSA_ASSERT( psa_hash_verify( &operation,
2664 valid_hash, sizeof( valid_hash ) ) );
2665 TEST_EQUAL( psa_hash_verify( &operation,
2666 valid_hash, sizeof( valid_hash ) ),
2667 PSA_ERROR_BAD_STATE );
2668 PSA_ASSERT( psa_hash_abort( &operation ) );
2669
2670 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002671 TEST_EQUAL( psa_hash_finish( &operation,
2672 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002673 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002674 PSA_ASSERT( psa_hash_abort( &operation ) );
2675
2676 /* Call finish twice in a row. */
2677 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2678 PSA_ASSERT( psa_hash_finish( &operation,
2679 hash, sizeof( hash ), &hash_len ) );
2680 TEST_EQUAL( psa_hash_finish( &operation,
2681 hash, sizeof( hash ), &hash_len ),
2682 PSA_ERROR_BAD_STATE );
2683 PSA_ASSERT( psa_hash_abort( &operation ) );
2684
2685 /* Call finish after calling verify. */
2686 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2687 PSA_ASSERT( psa_hash_verify( &operation,
2688 valid_hash, sizeof( valid_hash ) ) );
2689 TEST_EQUAL( psa_hash_finish( &operation,
2690 hash, sizeof( hash ), &hash_len ),
2691 PSA_ERROR_BAD_STATE );
2692 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002693
2694exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002695 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002696}
2697/* END_CASE */
2698
itayzafrir27e69452018-11-01 14:26:34 +02002699/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2700void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002701{
2702 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002703 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2704 * appended to it */
2705 unsigned char hash[] = {
2706 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2707 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2708 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002709 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002710 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002711
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002713
itayzafrir27e69452018-11-01 14:26:34 +02002714 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002715 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002716 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002717 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002718
itayzafrir27e69452018-11-01 14:26:34 +02002719 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002720 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002721 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002722 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002723
itayzafrir27e69452018-11-01 14:26:34 +02002724 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002725 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002726 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002727 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002728
itayzafrirec93d302018-10-18 18:01:10 +03002729exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002730 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002731}
2732/* END_CASE */
2733
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002734/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2735void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002736{
2737 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002738 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002739 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002740 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002741 size_t hash_len;
2742
Gilles Peskine8817f612018-12-18 00:18:46 +01002743 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002744
itayzafrir58028322018-10-25 10:22:01 +03002745 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002746 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002747 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002748 hash, expected_size - 1, &hash_len ),
2749 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002750
2751exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002752 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002753}
2754/* END_CASE */
2755
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002756/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2757void hash_clone_source_state( )
2758{
2759 psa_algorithm_t alg = PSA_ALG_SHA_256;
2760 unsigned char hash[PSA_HASH_MAX_SIZE];
2761 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2762 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2763 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2764 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2765 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2766 size_t hash_len;
2767
2768 PSA_ASSERT( psa_crypto_init( ) );
2769 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2770
2771 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2772 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2773 PSA_ASSERT( psa_hash_finish( &op_finished,
2774 hash, sizeof( hash ), &hash_len ) );
2775 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2776 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2777
2778 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2779 PSA_ERROR_BAD_STATE );
2780
2781 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2782 PSA_ASSERT( psa_hash_finish( &op_init,
2783 hash, sizeof( hash ), &hash_len ) );
2784 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2785 PSA_ASSERT( psa_hash_finish( &op_finished,
2786 hash, sizeof( hash ), &hash_len ) );
2787 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2788 PSA_ASSERT( psa_hash_finish( &op_aborted,
2789 hash, sizeof( hash ), &hash_len ) );
2790
2791exit:
2792 psa_hash_abort( &op_source );
2793 psa_hash_abort( &op_init );
2794 psa_hash_abort( &op_setup );
2795 psa_hash_abort( &op_finished );
2796 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002797 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002798}
2799/* END_CASE */
2800
2801/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2802void hash_clone_target_state( )
2803{
2804 psa_algorithm_t alg = PSA_ALG_SHA_256;
2805 unsigned char hash[PSA_HASH_MAX_SIZE];
2806 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2807 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2808 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2809 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2810 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2811 size_t hash_len;
2812
2813 PSA_ASSERT( psa_crypto_init( ) );
2814
2815 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2816 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2817 PSA_ASSERT( psa_hash_finish( &op_finished,
2818 hash, sizeof( hash ), &hash_len ) );
2819 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2820 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2821
2822 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2823 PSA_ASSERT( psa_hash_finish( &op_target,
2824 hash, sizeof( hash ), &hash_len ) );
2825
2826 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2827 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2828 PSA_ERROR_BAD_STATE );
2829 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2830 PSA_ERROR_BAD_STATE );
2831
2832exit:
2833 psa_hash_abort( &op_target );
2834 psa_hash_abort( &op_init );
2835 psa_hash_abort( &op_setup );
2836 psa_hash_abort( &op_finished );
2837 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002838 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002839}
2840/* END_CASE */
2841
itayzafrir58028322018-10-25 10:22:01 +03002842/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002843void mac_operation_init( )
2844{
Jaeden Amero252ef282019-02-15 14:05:35 +00002845 const uint8_t input[1] = { 0 };
2846
Jaeden Amero769ce272019-01-04 11:48:03 +00002847 /* Test each valid way of initializing the object, except for `= {0}`, as
2848 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2849 * though it's OK by the C standard. We could test for this, but we'd need
2850 * to supress the Clang warning for the test. */
2851 psa_mac_operation_t func = psa_mac_operation_init( );
2852 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2853 psa_mac_operation_t zero;
2854
2855 memset( &zero, 0, sizeof( zero ) );
2856
Jaeden Amero252ef282019-02-15 14:05:35 +00002857 /* A freshly-initialized MAC operation should not be usable. */
2858 TEST_EQUAL( psa_mac_update( &func,
2859 input, sizeof( input ) ),
2860 PSA_ERROR_BAD_STATE );
2861 TEST_EQUAL( psa_mac_update( &init,
2862 input, sizeof( input ) ),
2863 PSA_ERROR_BAD_STATE );
2864 TEST_EQUAL( psa_mac_update( &zero,
2865 input, sizeof( input ) ),
2866 PSA_ERROR_BAD_STATE );
2867
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002868 /* A default MAC operation should be abortable without error. */
2869 PSA_ASSERT( psa_mac_abort( &func ) );
2870 PSA_ASSERT( psa_mac_abort( &init ) );
2871 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002872}
2873/* END_CASE */
2874
2875/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002876void mac_setup( int key_type_arg,
2877 data_t *key,
2878 int alg_arg,
2879 int expected_status_arg )
2880{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002881 psa_key_type_t key_type = key_type_arg;
2882 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002883 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002884 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002885 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2886#if defined(KNOWN_SUPPORTED_MAC_ALG)
2887 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2888#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002889
Gilles Peskine8817f612018-12-18 00:18:46 +01002890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002891
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002892 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2893 &operation, &status ) )
2894 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002895 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002896
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002897 /* The operation object should be reusable. */
2898#if defined(KNOWN_SUPPORTED_MAC_ALG)
2899 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2900 smoke_test_key_data,
2901 sizeof( smoke_test_key_data ),
2902 KNOWN_SUPPORTED_MAC_ALG,
2903 &operation, &status ) )
2904 goto exit;
2905 TEST_EQUAL( status, PSA_SUCCESS );
2906#endif
2907
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002908exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002909 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002910}
2911/* END_CASE */
2912
2913/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002914void mac_bad_order( )
2915{
2916 psa_key_handle_t handle = 0;
2917 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2918 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2919 const uint8_t key[] = {
2920 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2921 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2922 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002923 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002924 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2925 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2926 size_t sign_mac_length = 0;
2927 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2928 const uint8_t verify_mac[] = {
2929 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2930 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2931 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2932
2933 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002935 psa_set_key_algorithm( &attributes, alg );
2936 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002937
Gilles Peskine73676cb2019-05-15 20:15:10 +02002938 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002939
Jaeden Amero252ef282019-02-15 14:05:35 +00002940 /* Call update without calling setup beforehand. */
2941 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2942 PSA_ERROR_BAD_STATE );
2943 PSA_ASSERT( psa_mac_abort( &operation ) );
2944
2945 /* Call sign finish without calling setup beforehand. */
2946 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2947 &sign_mac_length),
2948 PSA_ERROR_BAD_STATE );
2949 PSA_ASSERT( psa_mac_abort( &operation ) );
2950
2951 /* Call verify finish without calling setup beforehand. */
2952 TEST_EQUAL( psa_mac_verify_finish( &operation,
2953 verify_mac, sizeof( verify_mac ) ),
2954 PSA_ERROR_BAD_STATE );
2955 PSA_ASSERT( psa_mac_abort( &operation ) );
2956
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002957 /* Call setup twice in a row. */
2958 PSA_ASSERT( psa_mac_sign_setup( &operation,
2959 handle, alg ) );
2960 TEST_EQUAL( psa_mac_sign_setup( &operation,
2961 handle, alg ),
2962 PSA_ERROR_BAD_STATE );
2963 PSA_ASSERT( psa_mac_abort( &operation ) );
2964
Jaeden Amero252ef282019-02-15 14:05:35 +00002965 /* Call update after sign finish. */
2966 PSA_ASSERT( psa_mac_sign_setup( &operation,
2967 handle, alg ) );
2968 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2969 PSA_ASSERT( psa_mac_sign_finish( &operation,
2970 sign_mac, sizeof( sign_mac ),
2971 &sign_mac_length ) );
2972 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2973 PSA_ERROR_BAD_STATE );
2974 PSA_ASSERT( psa_mac_abort( &operation ) );
2975
2976 /* Call update after verify finish. */
2977 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002978 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002979 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2980 PSA_ASSERT( psa_mac_verify_finish( &operation,
2981 verify_mac, sizeof( verify_mac ) ) );
2982 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2983 PSA_ERROR_BAD_STATE );
2984 PSA_ASSERT( psa_mac_abort( &operation ) );
2985
2986 /* Call sign finish twice in a row. */
2987 PSA_ASSERT( psa_mac_sign_setup( &operation,
2988 handle, alg ) );
2989 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2990 PSA_ASSERT( psa_mac_sign_finish( &operation,
2991 sign_mac, sizeof( sign_mac ),
2992 &sign_mac_length ) );
2993 TEST_EQUAL( psa_mac_sign_finish( &operation,
2994 sign_mac, sizeof( sign_mac ),
2995 &sign_mac_length ),
2996 PSA_ERROR_BAD_STATE );
2997 PSA_ASSERT( psa_mac_abort( &operation ) );
2998
2999 /* Call verify finish twice in a row. */
3000 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003001 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003002 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3003 PSA_ASSERT( psa_mac_verify_finish( &operation,
3004 verify_mac, sizeof( verify_mac ) ) );
3005 TEST_EQUAL( psa_mac_verify_finish( &operation,
3006 verify_mac, sizeof( verify_mac ) ),
3007 PSA_ERROR_BAD_STATE );
3008 PSA_ASSERT( psa_mac_abort( &operation ) );
3009
3010 /* Setup sign but try verify. */
3011 PSA_ASSERT( psa_mac_sign_setup( &operation,
3012 handle, alg ) );
3013 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3014 TEST_EQUAL( psa_mac_verify_finish( &operation,
3015 verify_mac, sizeof( verify_mac ) ),
3016 PSA_ERROR_BAD_STATE );
3017 PSA_ASSERT( psa_mac_abort( &operation ) );
3018
3019 /* Setup verify but try sign. */
3020 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003021 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003022 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3023 TEST_EQUAL( psa_mac_sign_finish( &operation,
3024 sign_mac, sizeof( sign_mac ),
3025 &sign_mac_length ),
3026 PSA_ERROR_BAD_STATE );
3027 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003028
Gilles Peskine76b29a72019-05-28 14:08:50 +02003029 PSA_ASSERT( psa_destroy_key( handle ) );
3030
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003031exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003032 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003033}
3034/* END_CASE */
3035
3036/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003037void mac_sign( int key_type_arg,
3038 data_t *key,
3039 int alg_arg,
3040 data_t *input,
3041 data_t *expected_mac )
3042{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003043 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003044 psa_key_type_t key_type = key_type_arg;
3045 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003046 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003048 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003049 size_t mac_buffer_size =
3050 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
3051 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003052 const size_t output_sizes_to_test[] = {
3053 0,
3054 1,
3055 expected_mac->len - 1,
3056 expected_mac->len,
3057 expected_mac->len + 1,
3058 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003059
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003060 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
Gilles Peskine3d404d62020-08-25 23:47:36 +02003061 /* We expect PSA_MAC_FINAL_SIZE to be exact. */
3062 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003063
Gilles Peskine8817f612018-12-18 00:18:46 +01003064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003065
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003066 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003067 psa_set_key_algorithm( &attributes, alg );
3068 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003069
Gilles Peskine73676cb2019-05-15 20:15:10 +02003070 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003071
Gilles Peskine8b356b52020-08-25 23:44:59 +02003072 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3073 {
3074 const size_t output_size = output_sizes_to_test[i];
3075 psa_status_t expected_status =
3076 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3077 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003078
Gilles Peskine8b356b52020-08-25 23:44:59 +02003079 test_set_step( output_size );
3080 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003081
Gilles Peskine8b356b52020-08-25 23:44:59 +02003082 /* Calculate the MAC. */
3083 PSA_ASSERT( psa_mac_sign_setup( &operation,
3084 handle, alg ) );
3085 PSA_ASSERT( psa_mac_update( &operation,
3086 input->x, input->len ) );
3087 TEST_EQUAL( psa_mac_sign_finish( &operation,
3088 actual_mac, output_size,
3089 &mac_length ),
3090 expected_status );
3091 PSA_ASSERT( psa_mac_abort( &operation ) );
3092
3093 if( expected_status == PSA_SUCCESS )
3094 {
3095 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3096 actual_mac, mac_length );
3097 }
3098 mbedtls_free( actual_mac );
3099 actual_mac = NULL;
3100 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003101
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003102exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003103 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003104 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003105 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003106 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003107}
3108/* END_CASE */
3109
3110/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003111void mac_verify( int key_type_arg,
3112 data_t *key,
3113 int alg_arg,
3114 data_t *input,
3115 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003116{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003117 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003118 psa_key_type_t key_type = key_type_arg;
3119 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003120 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003121 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003122 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003123
Gilles Peskine69c12672018-06-28 00:07:19 +02003124 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3125
Gilles Peskine8817f612018-12-18 00:18:46 +01003126 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003128 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003129 psa_set_key_algorithm( &attributes, alg );
3130 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003131
Gilles Peskine73676cb2019-05-15 20:15:10 +02003132 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003133
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003134 /* Test the correct MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003135 PSA_ASSERT( psa_mac_verify_setup( &operation,
3136 handle, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003137 PSA_ASSERT( psa_mac_update( &operation,
3138 input->x, input->len ) );
3139 PSA_ASSERT( psa_mac_verify_finish( &operation,
3140 expected_mac->x,
3141 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003143 /* Test a MAC that's too short. */
3144 PSA_ASSERT( psa_mac_verify_setup( &operation,
3145 handle, alg ) );
3146 PSA_ASSERT( psa_mac_update( &operation,
3147 input->x, input->len ) );
3148 TEST_EQUAL( psa_mac_verify_finish( &operation,
3149 expected_mac->x,
3150 expected_mac->len - 1 ),
3151 PSA_ERROR_INVALID_SIGNATURE );
3152
3153 /* Test a MAC that's too long. */
3154 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3155 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
3156 PSA_ASSERT( psa_mac_verify_setup( &operation,
3157 handle, alg ) );
3158 PSA_ASSERT( psa_mac_update( &operation,
3159 input->x, input->len ) );
3160 TEST_EQUAL( psa_mac_verify_finish( &operation,
3161 perturbed_mac,
3162 expected_mac->len + 1 ),
3163 PSA_ERROR_INVALID_SIGNATURE );
3164
3165 /* Test changing one byte. */
3166 for( size_t i = 0; i < expected_mac->len; i++ )
3167 {
3168 test_set_step( i );
3169 perturbed_mac[i] ^= 1;
3170 PSA_ASSERT( psa_mac_verify_setup( &operation,
3171 handle, alg ) );
3172 PSA_ASSERT( psa_mac_update( &operation,
3173 input->x, input->len ) );
3174 TEST_EQUAL( psa_mac_verify_finish( &operation,
3175 perturbed_mac,
3176 expected_mac->len ),
3177 PSA_ERROR_INVALID_SIGNATURE );
3178 perturbed_mac[i] ^= 1;
3179 }
3180
Gilles Peskine8c9def32018-02-08 10:02:12 +01003181exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003182 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003183 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003184 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003185 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003186}
3187/* END_CASE */
3188
3189/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003190void cipher_operation_init( )
3191{
Jaeden Ameroab439972019-02-15 14:12:05 +00003192 const uint8_t input[1] = { 0 };
3193 unsigned char output[1] = { 0 };
3194 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003195 /* Test each valid way of initializing the object, except for `= {0}`, as
3196 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3197 * though it's OK by the C standard. We could test for this, but we'd need
3198 * to supress the Clang warning for the test. */
3199 psa_cipher_operation_t func = psa_cipher_operation_init( );
3200 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3201 psa_cipher_operation_t zero;
3202
3203 memset( &zero, 0, sizeof( zero ) );
3204
Jaeden Ameroab439972019-02-15 14:12:05 +00003205 /* A freshly-initialized cipher operation should not be usable. */
3206 TEST_EQUAL( psa_cipher_update( &func,
3207 input, sizeof( input ),
3208 output, sizeof( output ),
3209 &output_length ),
3210 PSA_ERROR_BAD_STATE );
3211 TEST_EQUAL( psa_cipher_update( &init,
3212 input, sizeof( input ),
3213 output, sizeof( output ),
3214 &output_length ),
3215 PSA_ERROR_BAD_STATE );
3216 TEST_EQUAL( psa_cipher_update( &zero,
3217 input, sizeof( input ),
3218 output, sizeof( output ),
3219 &output_length ),
3220 PSA_ERROR_BAD_STATE );
3221
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003222 /* A default cipher operation should be abortable without error. */
3223 PSA_ASSERT( psa_cipher_abort( &func ) );
3224 PSA_ASSERT( psa_cipher_abort( &init ) );
3225 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003226}
3227/* END_CASE */
3228
3229/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003230void cipher_setup( int key_type_arg,
3231 data_t *key,
3232 int alg_arg,
3233 int expected_status_arg )
3234{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003235 psa_key_type_t key_type = key_type_arg;
3236 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003237 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003238 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003239 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003240#if defined(KNOWN_SUPPORTED_MAC_ALG)
3241 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3242#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003243
Gilles Peskine8817f612018-12-18 00:18:46 +01003244 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003245
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003246 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3247 &operation, &status ) )
3248 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003249 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003250
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003251 /* The operation object should be reusable. */
3252#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3253 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3254 smoke_test_key_data,
3255 sizeof( smoke_test_key_data ),
3256 KNOWN_SUPPORTED_CIPHER_ALG,
3257 &operation, &status ) )
3258 goto exit;
3259 TEST_EQUAL( status, PSA_SUCCESS );
3260#endif
3261
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003262exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003263 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003264 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003265}
3266/* END_CASE */
3267
3268/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003269void cipher_bad_order( )
3270{
3271 psa_key_handle_t handle = 0;
3272 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3273 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003274 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003275 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3276 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3277 const uint8_t key[] = {
3278 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3279 0xaa, 0xaa, 0xaa, 0xaa };
3280 const uint8_t text[] = {
3281 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3282 0xbb, 0xbb, 0xbb, 0xbb };
3283 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3284 size_t length = 0;
3285
3286 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003287 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3288 psa_set_key_algorithm( &attributes, alg );
3289 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003290 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003291
3292
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003293 /* Call encrypt setup twice in a row. */
3294 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3295 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
3296 PSA_ERROR_BAD_STATE );
3297 PSA_ASSERT( psa_cipher_abort( &operation ) );
3298
3299 /* Call decrypt setup twice in a row. */
3300 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
3301 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
3302 PSA_ERROR_BAD_STATE );
3303 PSA_ASSERT( psa_cipher_abort( &operation ) );
3304
Jaeden Ameroab439972019-02-15 14:12:05 +00003305 /* Generate an IV without calling setup beforehand. */
3306 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3307 buffer, sizeof( buffer ),
3308 &length ),
3309 PSA_ERROR_BAD_STATE );
3310 PSA_ASSERT( psa_cipher_abort( &operation ) );
3311
3312 /* Generate an IV twice in a row. */
3313 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3314 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3315 buffer, sizeof( buffer ),
3316 &length ) );
3317 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3318 buffer, sizeof( buffer ),
3319 &length ),
3320 PSA_ERROR_BAD_STATE );
3321 PSA_ASSERT( psa_cipher_abort( &operation ) );
3322
3323 /* Generate an IV after it's already set. */
3324 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3325 PSA_ASSERT( psa_cipher_set_iv( &operation,
3326 iv, sizeof( iv ) ) );
3327 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3328 buffer, sizeof( buffer ),
3329 &length ),
3330 PSA_ERROR_BAD_STATE );
3331 PSA_ASSERT( psa_cipher_abort( &operation ) );
3332
3333 /* Set an IV without calling setup beforehand. */
3334 TEST_EQUAL( psa_cipher_set_iv( &operation,
3335 iv, sizeof( iv ) ),
3336 PSA_ERROR_BAD_STATE );
3337 PSA_ASSERT( psa_cipher_abort( &operation ) );
3338
3339 /* Set an IV after it's already set. */
3340 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3341 PSA_ASSERT( psa_cipher_set_iv( &operation,
3342 iv, sizeof( iv ) ) );
3343 TEST_EQUAL( psa_cipher_set_iv( &operation,
3344 iv, sizeof( iv ) ),
3345 PSA_ERROR_BAD_STATE );
3346 PSA_ASSERT( psa_cipher_abort( &operation ) );
3347
3348 /* Set an IV after it's already generated. */
3349 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3350 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3351 buffer, sizeof( buffer ),
3352 &length ) );
3353 TEST_EQUAL( psa_cipher_set_iv( &operation,
3354 iv, sizeof( iv ) ),
3355 PSA_ERROR_BAD_STATE );
3356 PSA_ASSERT( psa_cipher_abort( &operation ) );
3357
3358 /* Call update without calling setup beforehand. */
3359 TEST_EQUAL( psa_cipher_update( &operation,
3360 text, sizeof( text ),
3361 buffer, sizeof( buffer ),
3362 &length ),
3363 PSA_ERROR_BAD_STATE );
3364 PSA_ASSERT( psa_cipher_abort( &operation ) );
3365
3366 /* Call update without an IV where an IV is required. */
3367 TEST_EQUAL( psa_cipher_update( &operation,
3368 text, sizeof( text ),
3369 buffer, sizeof( buffer ),
3370 &length ),
3371 PSA_ERROR_BAD_STATE );
3372 PSA_ASSERT( psa_cipher_abort( &operation ) );
3373
3374 /* Call update after finish. */
3375 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3376 PSA_ASSERT( psa_cipher_set_iv( &operation,
3377 iv, sizeof( iv ) ) );
3378 PSA_ASSERT( psa_cipher_finish( &operation,
3379 buffer, sizeof( buffer ), &length ) );
3380 TEST_EQUAL( psa_cipher_update( &operation,
3381 text, sizeof( text ),
3382 buffer, sizeof( buffer ),
3383 &length ),
3384 PSA_ERROR_BAD_STATE );
3385 PSA_ASSERT( psa_cipher_abort( &operation ) );
3386
3387 /* Call finish without calling setup beforehand. */
3388 TEST_EQUAL( psa_cipher_finish( &operation,
3389 buffer, sizeof( buffer ), &length ),
3390 PSA_ERROR_BAD_STATE );
3391 PSA_ASSERT( psa_cipher_abort( &operation ) );
3392
3393 /* Call finish without an IV where an IV is required. */
3394 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3395 /* Not calling update means we are encrypting an empty buffer, which is OK
3396 * for cipher modes with padding. */
3397 TEST_EQUAL( psa_cipher_finish( &operation,
3398 buffer, sizeof( buffer ), &length ),
3399 PSA_ERROR_BAD_STATE );
3400 PSA_ASSERT( psa_cipher_abort( &operation ) );
3401
3402 /* Call finish twice in a row. */
3403 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3404 PSA_ASSERT( psa_cipher_set_iv( &operation,
3405 iv, sizeof( iv ) ) );
3406 PSA_ASSERT( psa_cipher_finish( &operation,
3407 buffer, sizeof( buffer ), &length ) );
3408 TEST_EQUAL( psa_cipher_finish( &operation,
3409 buffer, sizeof( buffer ), &length ),
3410 PSA_ERROR_BAD_STATE );
3411 PSA_ASSERT( psa_cipher_abort( &operation ) );
3412
Gilles Peskine76b29a72019-05-28 14:08:50 +02003413 PSA_ASSERT( psa_destroy_key( handle ) );
3414
Jaeden Ameroab439972019-02-15 14:12:05 +00003415exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003416 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003417 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003418}
3419/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003420
Gilles Peskine50e586b2018-06-08 14:28:46 +02003421/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003422void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003423 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003424 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003425 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003426{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003427 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003428 psa_status_t status;
3429 psa_key_type_t key_type = key_type_arg;
3430 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003431 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003432 unsigned char *output = NULL;
3433 size_t output_buffer_size = 0;
3434 size_t function_output_length = 0;
3435 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003436 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003438
Gilles Peskine8817f612018-12-18 00:18:46 +01003439 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003440
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003441 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3442 psa_set_key_algorithm( &attributes, alg );
3443 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003444
Gilles Peskine73676cb2019-05-15 20:15:10 +02003445 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003446
Gilles Peskine8817f612018-12-18 00:18:46 +01003447 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3448 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003449
Gilles Peskine423005e2019-05-06 15:22:57 +02003450 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
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
Gilles Peskine423005e2019-05-06 15:22:57 +02003514 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003515 output_buffer_size = ( (size_t) input->len +
3516 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003517 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003518
Gilles Peskinee0866522019-02-19 19:44:00 +01003519 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003520 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3521 output, output_buffer_size,
3522 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003523 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003524 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003525 PSA_ASSERT( psa_cipher_update( &operation,
3526 input->x + first_part_size,
3527 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003528 output + total_output_length,
3529 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003530 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003531 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003532 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003533 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003534 output + total_output_length,
3535 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003536 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003537 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003538 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003539
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003540 ASSERT_COMPARE( expected_output->x, expected_output->len,
3541 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003542
3543exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003544 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003545 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003546 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003547 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003548}
3549/* END_CASE */
3550
3551/* BEGIN_CASE */
3552void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003553 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003554 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003555 int first_part_size_arg,
3556 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003557 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003558{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003559 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003560
3561 psa_key_type_t key_type = key_type_arg;
3562 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003563 size_t first_part_size = first_part_size_arg;
3564 size_t output1_length = output1_length_arg;
3565 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003566 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003567 size_t output_buffer_size = 0;
3568 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003569 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003570 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003572
Gilles Peskine8817f612018-12-18 00:18:46 +01003573 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003574
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003575 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3576 psa_set_key_algorithm( &attributes, alg );
3577 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003578
Gilles Peskine73676cb2019-05-15 20:15:10 +02003579 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003580
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3582 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003583
Gilles Peskine423005e2019-05-06 15:22:57 +02003584 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003585
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003586 output_buffer_size = ( (size_t) input->len +
3587 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003588 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003589
Gilles Peskinee0866522019-02-19 19:44:00 +01003590 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003591 PSA_ASSERT( psa_cipher_update( &operation,
3592 input->x, first_part_size,
3593 output, output_buffer_size,
3594 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003595 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003596 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003597 PSA_ASSERT( psa_cipher_update( &operation,
3598 input->x + first_part_size,
3599 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003600 output + total_output_length,
3601 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003602 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003603 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003604 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003606 output + total_output_length,
3607 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003608 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003609 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003610 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003611
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003612 ASSERT_COMPARE( expected_output->x, expected_output->len,
3613 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003614
3615exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003616 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003617 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003618 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003619 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003620}
3621/* END_CASE */
3622
Gilles Peskine50e586b2018-06-08 14:28:46 +02003623/* BEGIN_CASE */
3624void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003625 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003626 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003627 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003628{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003629 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003630 psa_status_t status;
3631 psa_key_type_t key_type = key_type_arg;
3632 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003633 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003634 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003635 size_t output_buffer_size = 0;
3636 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003637 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003638 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003639 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003640
Gilles Peskine8817f612018-12-18 00:18:46 +01003641 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003642
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003643 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3644 psa_set_key_algorithm( &attributes, alg );
3645 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003646
Gilles Peskine73676cb2019-05-15 20:15:10 +02003647 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003648
Gilles Peskine8817f612018-12-18 00:18:46 +01003649 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3650 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003651
Gilles Peskine423005e2019-05-06 15:22:57 +02003652 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003653
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003654 output_buffer_size = ( (size_t) input->len +
3655 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003656 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003657
Gilles Peskine8817f612018-12-18 00:18:46 +01003658 PSA_ASSERT( psa_cipher_update( &operation,
3659 input->x, input->len,
3660 output, output_buffer_size,
3661 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003662 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003663 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003664 output + total_output_length,
3665 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003666 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003667 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003668 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003669
3670 if( expected_status == PSA_SUCCESS )
3671 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003672 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003673 ASSERT_COMPARE( expected_output->x, expected_output->len,
3674 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003675 }
3676
Gilles Peskine50e586b2018-06-08 14:28:46 +02003677exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003678 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003679 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003680 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003681 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003682}
3683/* END_CASE */
3684
Gilles Peskine50e586b2018-06-08 14:28:46 +02003685/* BEGIN_CASE */
3686void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003687 data_t *key,
3688 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003689{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003690 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003691 psa_key_type_t key_type = key_type_arg;
3692 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003693 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003694 size_t iv_size = 16;
3695 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003696 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003697 size_t output1_size = 0;
3698 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003699 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003700 size_t output2_size = 0;
3701 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003702 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003703 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3704 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003706
Gilles Peskine8817f612018-12-18 00:18:46 +01003707 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003708
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003709 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3710 psa_set_key_algorithm( &attributes, alg );
3711 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003712
Gilles Peskine73676cb2019-05-15 20:15:10 +02003713 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003714
Gilles Peskine8817f612018-12-18 00:18:46 +01003715 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3716 handle, alg ) );
3717 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3718 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003719
Gilles Peskine8817f612018-12-18 00:18:46 +01003720 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3721 iv, iv_size,
3722 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003723 output1_size = ( (size_t) input->len +
3724 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003725 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003726
Gilles Peskine8817f612018-12-18 00:18:46 +01003727 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3728 output1, output1_size,
3729 &output1_length ) );
3730 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003731 output1 + output1_length,
3732 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003733 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003734
Gilles Peskine048b7f02018-06-08 14:20:49 +02003735 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003736
Gilles Peskine8817f612018-12-18 00:18:46 +01003737 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003738
3739 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003740 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003741
Gilles Peskine8817f612018-12-18 00:18:46 +01003742 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3743 iv, iv_length ) );
3744 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3745 output2, output2_size,
3746 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003747 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003748 PSA_ASSERT( psa_cipher_finish( &operation2,
3749 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003750 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003751 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003752
Gilles Peskine048b7f02018-06-08 14:20:49 +02003753 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003754
Gilles Peskine8817f612018-12-18 00:18:46 +01003755 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003756
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003757 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003758
3759exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003760 psa_cipher_abort( &operation1 );
3761 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003762 mbedtls_free( output1 );
3763 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003764 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003765 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003766}
3767/* END_CASE */
3768
3769/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003770void cipher_verify_output_multipart( int alg_arg,
3771 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003772 data_t *key,
3773 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003774 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003775{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003776 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003777 psa_key_type_t key_type = key_type_arg;
3778 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003779 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003780 unsigned char iv[16] = {0};
3781 size_t iv_size = 16;
3782 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003783 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003784 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003785 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003786 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003787 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003788 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003789 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003790 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3791 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003792 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003793
Gilles Peskine8817f612018-12-18 00:18:46 +01003794 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003795
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003796 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3797 psa_set_key_algorithm( &attributes, alg );
3798 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003799
Gilles Peskine73676cb2019-05-15 20:15:10 +02003800 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003801
Gilles Peskine8817f612018-12-18 00:18:46 +01003802 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3803 handle, alg ) );
3804 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3805 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003806
Gilles Peskine8817f612018-12-18 00:18:46 +01003807 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3808 iv, iv_size,
3809 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003810 output1_buffer_size = ( (size_t) input->len +
3811 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003812 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003813
Gilles Peskinee0866522019-02-19 19:44:00 +01003814 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003815
Gilles Peskine8817f612018-12-18 00:18:46 +01003816 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3817 output1, output1_buffer_size,
3818 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003819 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003820
Gilles Peskine8817f612018-12-18 00:18:46 +01003821 PSA_ASSERT( psa_cipher_update( &operation1,
3822 input->x + first_part_size,
3823 input->len - first_part_size,
3824 output1, output1_buffer_size,
3825 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003826 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003827
Gilles Peskine8817f612018-12-18 00:18:46 +01003828 PSA_ASSERT( psa_cipher_finish( &operation1,
3829 output1 + output1_length,
3830 output1_buffer_size - output1_length,
3831 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003832 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003833
Gilles Peskine8817f612018-12-18 00:18:46 +01003834 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003835
Gilles Peskine048b7f02018-06-08 14:20:49 +02003836 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003837 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003838
Gilles Peskine8817f612018-12-18 00:18:46 +01003839 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3840 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003841
Gilles Peskine8817f612018-12-18 00:18:46 +01003842 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3843 output2, output2_buffer_size,
3844 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003845 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003846
Gilles Peskine8817f612018-12-18 00:18:46 +01003847 PSA_ASSERT( psa_cipher_update( &operation2,
3848 output1 + first_part_size,
3849 output1_length - first_part_size,
3850 output2, output2_buffer_size,
3851 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003852 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003853
Gilles Peskine8817f612018-12-18 00:18:46 +01003854 PSA_ASSERT( psa_cipher_finish( &operation2,
3855 output2 + output2_length,
3856 output2_buffer_size - output2_length,
3857 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003858 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003859
Gilles Peskine8817f612018-12-18 00:18:46 +01003860 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003861
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003862 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003863
3864exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003865 psa_cipher_abort( &operation1 );
3866 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003867 mbedtls_free( output1 );
3868 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003869 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003870 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003871}
3872/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003873
Gilles Peskine20035e32018-02-03 22:44:14 +01003874/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003875void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003876 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003877 data_t *nonce,
3878 data_t *additional_data,
3879 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003880 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003881{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003882 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003883 psa_key_type_t key_type = key_type_arg;
3884 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003885 unsigned char *output_data = NULL;
3886 size_t output_size = 0;
3887 size_t output_length = 0;
3888 unsigned char *output_data2 = NULL;
3889 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003890 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003891 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003893
Gilles Peskine4abf7412018-06-18 16:35:34 +02003894 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003895 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3896 * should be exact. */
3897 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3898 TEST_EQUAL( output_size,
3899 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003900 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003901
Gilles Peskine8817f612018-12-18 00:18:46 +01003902 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003903
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003904 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3905 psa_set_key_algorithm( &attributes, alg );
3906 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003907
Gilles Peskine049c7532019-05-15 20:22:09 +02003908 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3909 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003910
Gilles Peskinefe11b722018-12-18 00:24:04 +01003911 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3912 nonce->x, nonce->len,
3913 additional_data->x,
3914 additional_data->len,
3915 input_data->x, input_data->len,
3916 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003917 &output_length ),
3918 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003919
3920 if( PSA_SUCCESS == expected_result )
3921 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003922 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003923
Gilles Peskine003a4a92019-05-14 16:09:40 +02003924 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3925 * should be exact. */
3926 TEST_EQUAL( input_data->len,
3927 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3928
Gilles Peskinefe11b722018-12-18 00:24:04 +01003929 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3930 nonce->x, nonce->len,
3931 additional_data->x,
3932 additional_data->len,
3933 output_data, output_length,
3934 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003935 &output_length2 ),
3936 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003937
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003938 ASSERT_COMPARE( input_data->x, input_data->len,
3939 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003940 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003941
Gilles Peskinea1cac842018-06-11 19:33:02 +02003942exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003943 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003944 mbedtls_free( output_data );
3945 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003946 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003947}
3948/* END_CASE */
3949
3950/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003951void aead_encrypt( int key_type_arg, data_t *key_data,
3952 int alg_arg,
3953 data_t *nonce,
3954 data_t *additional_data,
3955 data_t *input_data,
3956 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003957{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003958 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003959 psa_key_type_t key_type = key_type_arg;
3960 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003961 unsigned char *output_data = NULL;
3962 size_t output_size = 0;
3963 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003964 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003965 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003966
Gilles Peskine4abf7412018-06-18 16:35:34 +02003967 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003968 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3969 * should be exact. */
3970 TEST_EQUAL( output_size,
3971 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003972 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003973
Gilles Peskine8817f612018-12-18 00:18:46 +01003974 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003975
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003976 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3977 psa_set_key_algorithm( &attributes, alg );
3978 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003979
Gilles Peskine049c7532019-05-15 20:22:09 +02003980 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3981 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003982
Gilles Peskine8817f612018-12-18 00:18:46 +01003983 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3984 nonce->x, nonce->len,
3985 additional_data->x, additional_data->len,
3986 input_data->x, input_data->len,
3987 output_data, output_size,
3988 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003989
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003990 ASSERT_COMPARE( expected_result->x, expected_result->len,
3991 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003992
Gilles Peskinea1cac842018-06-11 19:33:02 +02003993exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003994 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003995 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003996 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003997}
3998/* END_CASE */
3999
4000/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004001void aead_decrypt( int key_type_arg, data_t *key_data,
4002 int alg_arg,
4003 data_t *nonce,
4004 data_t *additional_data,
4005 data_t *input_data,
4006 data_t *expected_data,
4007 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004008{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004009 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004010 psa_key_type_t key_type = key_type_arg;
4011 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004012 unsigned char *output_data = NULL;
4013 size_t output_size = 0;
4014 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004015 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004017 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004018
Gilles Peskine003a4a92019-05-14 16:09:40 +02004019 output_size = input_data->len - tag_length;
4020 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4021 * should be exact. */
4022 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4023 TEST_EQUAL( output_size,
4024 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004025 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004026
Gilles Peskine8817f612018-12-18 00:18:46 +01004027 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004028
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004029 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4030 psa_set_key_algorithm( &attributes, alg );
4031 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004032
Gilles Peskine049c7532019-05-15 20:22:09 +02004033 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4034 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004035
Gilles Peskinefe11b722018-12-18 00:24:04 +01004036 TEST_EQUAL( psa_aead_decrypt( handle, alg,
4037 nonce->x, nonce->len,
4038 additional_data->x,
4039 additional_data->len,
4040 input_data->x, input_data->len,
4041 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004042 &output_length ),
4043 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004044
Gilles Peskine2d277862018-06-18 15:41:12 +02004045 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004046 ASSERT_COMPARE( expected_data->x, expected_data->len,
4047 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004048
Gilles Peskinea1cac842018-06-11 19:33:02 +02004049exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004050 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004051 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004052 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004053}
4054/* END_CASE */
4055
4056/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004057void signature_size( int type_arg,
4058 int bits,
4059 int alg_arg,
4060 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004061{
4062 psa_key_type_t type = type_arg;
4063 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004064 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004065
Gilles Peskinefe11b722018-12-18 00:24:04 +01004066 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004067#if defined(MBEDTLS_TEST_DEPRECATED)
4068 TEST_EQUAL( actual_size,
4069 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4070#endif /* MBEDTLS_TEST_DEPRECATED */
4071
Gilles Peskinee59236f2018-01-27 23:32:46 +01004072exit:
4073 ;
4074}
4075/* END_CASE */
4076
4077/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004078void sign_deterministic( int key_type_arg, data_t *key_data,
4079 int alg_arg, data_t *input_data,
4080 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004081{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004082 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004083 psa_key_type_t key_type = key_type_arg;
4084 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004085 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004086 unsigned char *signature = NULL;
4087 size_t signature_size;
4088 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004089 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004090
Gilles Peskine8817f612018-12-18 00:18:46 +01004091 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004092
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004093 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004094 psa_set_key_algorithm( &attributes, alg );
4095 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004096
Gilles Peskine049c7532019-05-15 20:22:09 +02004097 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4098 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004099 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4100 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004101
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004102 /* Allocate a buffer which has the size advertized by the
4103 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004104 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004105 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004106 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004107 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004108 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004109
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004110 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004111 PSA_ASSERT( psa_sign_hash( handle, alg,
4112 input_data->x, input_data->len,
4113 signature, signature_size,
4114 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004115 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004116 ASSERT_COMPARE( output_data->x, output_data->len,
4117 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004118
Gilles Peskine0627f982019-11-26 19:12:16 +01004119#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004120 memset( signature, 0, signature_size );
4121 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01004122 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
4123 input_data->x, input_data->len,
4124 signature, signature_size,
4125 &signature_length ) );
4126 ASSERT_COMPARE( output_data->x, output_data->len,
4127 signature, signature_length );
4128#endif /* MBEDTLS_TEST_DEPRECATED */
4129
Gilles Peskine20035e32018-02-03 22:44:14 +01004130exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004131 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004132 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01004133 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004134 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004135}
4136/* END_CASE */
4137
4138/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004139void sign_fail( int key_type_arg, data_t *key_data,
4140 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004141 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004142{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004143 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01004144 psa_key_type_t key_type = key_type_arg;
4145 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004146 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004147 psa_status_t actual_status;
4148 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004149 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004150 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004152
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004153 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004154
Gilles Peskine8817f612018-12-18 00:18:46 +01004155 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004156
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004158 psa_set_key_algorithm( &attributes, alg );
4159 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004160
Gilles Peskine049c7532019-05-15 20:22:09 +02004161 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4162 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004163
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004164 actual_status = psa_sign_hash( handle, alg,
4165 input_data->x, input_data->len,
4166 signature, signature_size,
4167 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004168 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004169 /* The value of *signature_length is unspecified on error, but
4170 * whatever it is, it should be less than signature_size, so that
4171 * if the caller tries to read *signature_length bytes without
4172 * checking the error code then they don't overflow a buffer. */
4173 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004174
Gilles Peskine895242b2019-11-29 12:15:40 +01004175#if defined(MBEDTLS_TEST_DEPRECATED)
4176 signature_length = INVALID_EXPORT_LENGTH;
4177 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
4178 input_data->x, input_data->len,
4179 signature, signature_size,
4180 &signature_length ),
4181 expected_status );
4182 TEST_ASSERT( signature_length <= signature_size );
4183#endif /* MBEDTLS_TEST_DEPRECATED */
4184
Gilles Peskine20035e32018-02-03 22:44:14 +01004185exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004186 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004187 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01004188 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004189 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004190}
4191/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004192
4193/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004194void sign_verify( int key_type_arg, data_t *key_data,
4195 int alg_arg, data_t *input_data )
4196{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004197 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02004198 psa_key_type_t key_type = key_type_arg;
4199 psa_algorithm_t alg = alg_arg;
4200 size_t key_bits;
4201 unsigned char *signature = NULL;
4202 size_t signature_size;
4203 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004204 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004205
Gilles Peskine8817f612018-12-18 00:18:46 +01004206 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004207
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004208 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004209 psa_set_key_algorithm( &attributes, alg );
4210 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004211
Gilles Peskine049c7532019-05-15 20:22:09 +02004212 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4213 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004214 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4215 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004216
4217 /* Allocate a buffer which has the size advertized by the
4218 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004219 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004220 key_bits, alg );
4221 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004222 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004223 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004224
4225 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004226 PSA_ASSERT( psa_sign_hash( handle, alg,
4227 input_data->x, input_data->len,
4228 signature, signature_size,
4229 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004230 /* Check that the signature length looks sensible. */
4231 TEST_ASSERT( signature_length <= signature_size );
4232 TEST_ASSERT( signature_length > 0 );
4233
4234 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004235 PSA_ASSERT( psa_verify_hash( handle, alg,
4236 input_data->x, input_data->len,
4237 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004238
4239 if( input_data->len != 0 )
4240 {
4241 /* Flip a bit in the input and verify that the signature is now
4242 * detected as invalid. Flip a bit at the beginning, not at the end,
4243 * because ECDSA may ignore the last few bits of the input. */
4244 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004245 TEST_EQUAL( psa_verify_hash( handle, alg,
4246 input_data->x, input_data->len,
4247 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004248 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004249 }
4250
4251exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004252 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004253 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02004254 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004255 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004256}
4257/* END_CASE */
4258
4259/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004260void asymmetric_verify( int key_type_arg, data_t *key_data,
4261 int alg_arg, data_t *hash_data,
4262 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004263{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004264 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03004265 psa_key_type_t key_type = key_type_arg;
4266 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004268
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004269 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004270
Gilles Peskine8817f612018-12-18 00:18:46 +01004271 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004272
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004273 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004274 psa_set_key_algorithm( &attributes, alg );
4275 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004276
Gilles Peskine049c7532019-05-15 20:22:09 +02004277 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4278 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03004279
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004280 PSA_ASSERT( psa_verify_hash( handle, alg,
4281 hash_data->x, hash_data->len,
4282 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004283
4284#if defined(MBEDTLS_TEST_DEPRECATED)
4285 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
4286 hash_data->x, hash_data->len,
4287 signature_data->x,
4288 signature_data->len ) );
4289
4290#endif /* MBEDTLS_TEST_DEPRECATED */
4291
itayzafrir5c753392018-05-08 11:18:38 +03004292exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004293 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004294 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004295 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004296}
4297/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004298
4299/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004300void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4301 int alg_arg, data_t *hash_data,
4302 data_t *signature_data,
4303 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004304{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004305 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004306 psa_key_type_t key_type = key_type_arg;
4307 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004308 psa_status_t actual_status;
4309 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004310 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004311
Gilles Peskine8817f612018-12-18 00:18:46 +01004312 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004313
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004314 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004315 psa_set_key_algorithm( &attributes, alg );
4316 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004317
Gilles Peskine049c7532019-05-15 20:22:09 +02004318 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4319 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004320
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004321 actual_status = psa_verify_hash( handle, alg,
4322 hash_data->x, hash_data->len,
4323 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004324 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004325
Gilles Peskine895242b2019-11-29 12:15:40 +01004326#if defined(MBEDTLS_TEST_DEPRECATED)
4327 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
4328 hash_data->x, hash_data->len,
4329 signature_data->x, signature_data->len ),
4330 expected_status );
4331#endif /* MBEDTLS_TEST_DEPRECATED */
4332
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004333exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004334 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004335 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004336 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004337}
4338/* END_CASE */
4339
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004341void asymmetric_encrypt( int key_type_arg,
4342 data_t *key_data,
4343 int alg_arg,
4344 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004345 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004346 int expected_output_length_arg,
4347 int expected_status_arg )
4348{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004349 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004350 psa_key_type_t key_type = key_type_arg;
4351 psa_algorithm_t alg = alg_arg;
4352 size_t expected_output_length = expected_output_length_arg;
4353 size_t key_bits;
4354 unsigned char *output = NULL;
4355 size_t output_size;
4356 size_t output_length = ~0;
4357 psa_status_t actual_status;
4358 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004360
Gilles Peskine8817f612018-12-18 00:18:46 +01004361 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004362
Gilles Peskine656896e2018-06-29 19:12:28 +02004363 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004364 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4365 psa_set_key_algorithm( &attributes, alg );
4366 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4368 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004369
4370 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004371 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4372 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004373 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004374 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004375
4376 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004377 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004378 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004379 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004380 output, output_size,
4381 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004382 TEST_EQUAL( actual_status, expected_status );
4383 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004384
Gilles Peskine68428122018-06-30 18:42:41 +02004385 /* If the label is empty, the test framework puts a non-null pointer
4386 * in label->x. Test that a null pointer works as well. */
4387 if( label->len == 0 )
4388 {
4389 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004390 if( output_size != 0 )
4391 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004392 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004393 input_data->x, input_data->len,
4394 NULL, label->len,
4395 output, output_size,
4396 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004397 TEST_EQUAL( actual_status, expected_status );
4398 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004399 }
4400
Gilles Peskine656896e2018-06-29 19:12:28 +02004401exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004402 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004403 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004404 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004405 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004406}
4407/* END_CASE */
4408
4409/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004410void asymmetric_encrypt_decrypt( int key_type_arg,
4411 data_t *key_data,
4412 int alg_arg,
4413 data_t *input_data,
4414 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004415{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004416 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004417 psa_key_type_t key_type = key_type_arg;
4418 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004419 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004420 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004421 size_t output_size;
4422 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004423 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004424 size_t output2_size;
4425 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004426 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004427
Gilles Peskine8817f612018-12-18 00:18:46 +01004428 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004429
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004430 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4431 psa_set_key_algorithm( &attributes, alg );
4432 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004433
Gilles Peskine049c7532019-05-15 20:22:09 +02004434 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4435 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004436
4437 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004438 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4439 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004440 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004441 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004442 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004443 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004444
Gilles Peskineeebd7382018-06-08 18:11:54 +02004445 /* We test encryption by checking that encrypt-then-decrypt gives back
4446 * the original plaintext because of the non-optional random
4447 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004448 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4449 input_data->x, input_data->len,
4450 label->x, label->len,
4451 output, output_size,
4452 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004453 /* We don't know what ciphertext length to expect, but check that
4454 * it looks sensible. */
4455 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004456
Gilles Peskine8817f612018-12-18 00:18:46 +01004457 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4458 output, output_length,
4459 label->x, label->len,
4460 output2, output2_size,
4461 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004462 ASSERT_COMPARE( input_data->x, input_data->len,
4463 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004464
4465exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004466 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004467 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004468 mbedtls_free( output );
4469 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004470 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004471}
4472/* END_CASE */
4473
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004474/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004475void asymmetric_decrypt( int key_type_arg,
4476 data_t *key_data,
4477 int alg_arg,
4478 data_t *input_data,
4479 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004480 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004481{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004482 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004483 psa_key_type_t key_type = key_type_arg;
4484 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004485 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004486 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004487 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004489
Jaeden Amero412654a2019-02-06 12:57:46 +00004490 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004491 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004492
Gilles Peskine8817f612018-12-18 00:18:46 +01004493 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004494
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004495 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4496 psa_set_key_algorithm( &attributes, alg );
4497 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004498
Gilles Peskine049c7532019-05-15 20:22:09 +02004499 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4500 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004501
Gilles Peskine8817f612018-12-18 00:18:46 +01004502 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4503 input_data->x, input_data->len,
4504 label->x, label->len,
4505 output,
4506 output_size,
4507 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004508 ASSERT_COMPARE( expected_data->x, expected_data->len,
4509 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004510
Gilles Peskine68428122018-06-30 18:42:41 +02004511 /* If the label is empty, the test framework puts a non-null pointer
4512 * in label->x. Test that a null pointer works as well. */
4513 if( label->len == 0 )
4514 {
4515 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004516 if( output_size != 0 )
4517 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004518 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4519 input_data->x, input_data->len,
4520 NULL, label->len,
4521 output,
4522 output_size,
4523 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004524 ASSERT_COMPARE( expected_data->x, expected_data->len,
4525 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004526 }
4527
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004528exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004529 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004530 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004531 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004532 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004533}
4534/* END_CASE */
4535
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004536/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004537void asymmetric_decrypt_fail( int key_type_arg,
4538 data_t *key_data,
4539 int alg_arg,
4540 data_t *input_data,
4541 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004542 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004543 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004544{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004545 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004546 psa_key_type_t key_type = key_type_arg;
4547 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004548 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004549 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004550 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004551 psa_status_t actual_status;
4552 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004553 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004554
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004555 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004556
Gilles Peskine8817f612018-12-18 00:18:46 +01004557 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004558
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004559 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4560 psa_set_key_algorithm( &attributes, alg );
4561 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004562
Gilles Peskine049c7532019-05-15 20:22:09 +02004563 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4564 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004565
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004566 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004567 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004568 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004569 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004570 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004571 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004572 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004573
Gilles Peskine68428122018-06-30 18:42:41 +02004574 /* If the label is empty, the test framework puts a non-null pointer
4575 * in label->x. Test that a null pointer works as well. */
4576 if( label->len == 0 )
4577 {
4578 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004579 if( output_size != 0 )
4580 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004581 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004582 input_data->x, input_data->len,
4583 NULL, label->len,
4584 output, output_size,
4585 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004586 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004587 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004588 }
4589
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004590exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004591 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004592 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004593 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004594 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004595}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004596/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004597
4598/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004599void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004600{
4601 /* Test each valid way of initializing the object, except for `= {0}`, as
4602 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4603 * though it's OK by the C standard. We could test for this, but we'd need
4604 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004605 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004606 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4607 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4608 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004609
4610 memset( &zero, 0, sizeof( zero ) );
4611
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004612 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004613 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004614 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004615 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004616 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004617 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004618 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004619
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004620 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004621 PSA_ASSERT( psa_key_derivation_abort(&func) );
4622 PSA_ASSERT( psa_key_derivation_abort(&init) );
4623 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004624}
4625/* END_CASE */
4626
Janos Follath16de4a42019-06-13 16:32:24 +01004627/* BEGIN_CASE */
4628void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004629{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004630 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004631 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004632 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004633
Gilles Peskine8817f612018-12-18 00:18:46 +01004634 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004635
Janos Follath16de4a42019-06-13 16:32:24 +01004636 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004637 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004638
4639exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004640 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004641 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004642}
4643/* END_CASE */
4644
Janos Follathaf3c2a02019-06-12 12:34:34 +01004645/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004646void derive_set_capacity( int alg_arg, int capacity_arg,
4647 int expected_status_arg )
4648{
4649 psa_algorithm_t alg = alg_arg;
4650 size_t capacity = capacity_arg;
4651 psa_status_t expected_status = expected_status_arg;
4652 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4653
4654 PSA_ASSERT( psa_crypto_init( ) );
4655
4656 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4657
4658 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4659 expected_status );
4660
4661exit:
4662 psa_key_derivation_abort( &operation );
4663 PSA_DONE( );
4664}
4665/* END_CASE */
4666
4667/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004668void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004669 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004670 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004671 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004672 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004673 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004674 int expected_status_arg3,
4675 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004676{
4677 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004678 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4679 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004680 psa_status_t expected_statuses[] = {expected_status_arg1,
4681 expected_status_arg2,
4682 expected_status_arg3};
4683 data_t *inputs[] = {input1, input2, input3};
4684 psa_key_handle_t handles[] = {0, 0, 0};
4685 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4686 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4687 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004688 psa_key_type_t output_key_type = output_key_type_arg;
4689 psa_key_handle_t output_handle = 0;
4690 psa_status_t expected_output_status = expected_output_status_arg;
4691 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004692
4693 PSA_ASSERT( psa_crypto_init( ) );
4694
4695 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4696 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004697
4698 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4699
4700 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4701 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004702 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004703 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004704 psa_set_key_type( &attributes, key_types[i] );
4705 PSA_ASSERT( psa_import_key( &attributes,
4706 inputs[i]->x, inputs[i]->len,
4707 &handles[i] ) );
4708 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4709 handles[i] ),
4710 expected_statuses[i] );
4711 }
4712 else
4713 {
4714 TEST_EQUAL( psa_key_derivation_input_bytes(
4715 &operation, steps[i],
4716 inputs[i]->x, inputs[i]->len ),
4717 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004718 }
4719 }
4720
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004721 if( output_key_type != PSA_KEY_TYPE_NONE )
4722 {
4723 psa_reset_key_attributes( &attributes );
4724 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4725 psa_set_key_bits( &attributes, 8 );
4726 actual_output_status =
4727 psa_key_derivation_output_key( &attributes, &operation,
4728 &output_handle );
4729 }
4730 else
4731 {
4732 uint8_t buffer[1];
4733 actual_output_status =
4734 psa_key_derivation_output_bytes( &operation,
4735 buffer, sizeof( buffer ) );
4736 }
4737 TEST_EQUAL( actual_output_status, expected_output_status );
4738
Janos Follathaf3c2a02019-06-12 12:34:34 +01004739exit:
4740 psa_key_derivation_abort( &operation );
4741 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4742 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004743 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004744 PSA_DONE( );
4745}
4746/* END_CASE */
4747
Janos Follathd958bb72019-07-03 15:02:16 +01004748/* BEGIN_CASE */
4749void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004750{
Janos Follathd958bb72019-07-03 15:02:16 +01004751 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004752 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004753 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004754 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004755 unsigned char input1[] = "Input 1";
4756 size_t input1_length = sizeof( input1 );
4757 unsigned char input2[] = "Input 2";
4758 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004759 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004760 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004761 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4762 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4763 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004765
Gilles Peskine8817f612018-12-18 00:18:46 +01004766 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004767
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004768 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4769 psa_set_key_algorithm( &attributes, alg );
4770 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004771
Gilles Peskine73676cb2019-05-15 20:15:10 +02004772 PSA_ASSERT( psa_import_key( &attributes,
4773 key_data, sizeof( key_data ),
4774 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004775
4776 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004777 if( !setup_key_derivation_wrap( &operation, handle, alg,
4778 input1, input1_length,
4779 input2, input2_length,
4780 capacity ) )
4781 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004782
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004783 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004784 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004785 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004786
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004787 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004788
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004790 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004791
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004792exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004793 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004794 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004795 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004796}
4797/* END_CASE */
4798
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004799/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004800void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004801{
4802 uint8_t output_buffer[16];
4803 size_t buffer_size = 16;
4804 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004806
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004807 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4808 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004809 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004810
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004812 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004813
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004814 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004815
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004816 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4817 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004818 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004819
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004821 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004822
4823exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004825}
4826/* END_CASE */
4827
4828/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004829void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004830 int step1_arg, data_t *input1,
4831 int step2_arg, data_t *input2,
4832 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004833 int requested_capacity_arg,
4834 data_t *expected_output1,
4835 data_t *expected_output2 )
4836{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004837 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004838 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4839 data_t *inputs[] = {input1, input2, input3};
4840 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004841 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004842 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004843 uint8_t *expected_outputs[2] =
4844 {expected_output1->x, expected_output2->x};
4845 size_t output_sizes[2] =
4846 {expected_output1->len, expected_output2->len};
4847 size_t output_buffer_size = 0;
4848 uint8_t *output_buffer = NULL;
4849 size_t expected_capacity;
4850 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004851 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004852 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004853 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004854
4855 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4856 {
4857 if( output_sizes[i] > output_buffer_size )
4858 output_buffer_size = output_sizes[i];
4859 if( output_sizes[i] == 0 )
4860 expected_outputs[i] = NULL;
4861 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004862 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004863 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004864
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004865 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4866 psa_set_key_algorithm( &attributes, alg );
4867 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004868
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004869 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004870 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4871 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4872 requested_capacity ) );
4873 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004874 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004875 switch( steps[i] )
4876 {
4877 case 0:
4878 break;
4879 case PSA_KEY_DERIVATION_INPUT_SECRET:
4880 PSA_ASSERT( psa_import_key( &attributes,
4881 inputs[i]->x, inputs[i]->len,
4882 &handles[i] ) );
4883 PSA_ASSERT( psa_key_derivation_input_key(
4884 &operation, steps[i],
4885 handles[i] ) );
4886 break;
4887 default:
4888 PSA_ASSERT( psa_key_derivation_input_bytes(
4889 &operation, steps[i],
4890 inputs[i]->x, inputs[i]->len ) );
4891 break;
4892 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004893 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004894
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004896 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004897 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004898 expected_capacity = requested_capacity;
4899
4900 /* Expansion phase. */
4901 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4902 {
4903 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004905 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004906 if( expected_capacity == 0 && output_sizes[i] == 0 )
4907 {
4908 /* Reading 0 bytes when 0 bytes are available can go either way. */
4909 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004910 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004911 continue;
4912 }
4913 else if( expected_capacity == 0 ||
4914 output_sizes[i] > expected_capacity )
4915 {
4916 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004917 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004918 expected_capacity = 0;
4919 continue;
4920 }
4921 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004922 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004923 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004924 ASSERT_COMPARE( output_buffer, output_sizes[i],
4925 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004926 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004927 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004929 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004930 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004931 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004933
4934exit:
4935 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004937 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4938 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004939 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004940}
4941/* END_CASE */
4942
4943/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004944void derive_full( int alg_arg,
4945 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004946 data_t *input1,
4947 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004948 int requested_capacity_arg )
4949{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004950 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004951 psa_algorithm_t alg = alg_arg;
4952 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004954 unsigned char output_buffer[16];
4955 size_t expected_capacity = requested_capacity;
4956 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004958
Gilles Peskine8817f612018-12-18 00:18:46 +01004959 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004960
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4962 psa_set_key_algorithm( &attributes, alg );
4963 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004964
Gilles Peskine049c7532019-05-15 20:22:09 +02004965 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4966 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004967
Janos Follathf2815ea2019-07-03 12:41:36 +01004968 if( !setup_key_derivation_wrap( &operation, handle, alg,
4969 input1->x, input1->len,
4970 input2->x, input2->len,
4971 requested_capacity ) )
4972 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004973
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004974 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004975 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004976 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004977
4978 /* Expansion phase. */
4979 while( current_capacity > 0 )
4980 {
4981 size_t read_size = sizeof( output_buffer );
4982 if( read_size > current_capacity )
4983 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004985 output_buffer,
4986 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004987 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004988 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004989 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004990 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004991 }
4992
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004993 /* Check that the operation refuses to go over capacity. */
4994 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004995 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004996
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004997 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004998
4999exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005000 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005001 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005002 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005003}
5004/* END_CASE */
5005
Janos Follathe60c9052019-07-03 13:51:30 +01005006/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005007void derive_key_exercise( int alg_arg,
5008 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005009 data_t *input1,
5010 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005011 int derived_type_arg,
5012 int derived_bits_arg,
5013 int derived_usage_arg,
5014 int derived_alg_arg )
5015{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005016 psa_key_handle_t base_handle = 0;
5017 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005018 psa_algorithm_t alg = alg_arg;
5019 psa_key_type_t derived_type = derived_type_arg;
5020 size_t derived_bits = derived_bits_arg;
5021 psa_key_usage_t derived_usage = derived_usage_arg;
5022 psa_algorithm_t derived_alg = derived_alg_arg;
5023 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005026 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005027
Gilles Peskine8817f612018-12-18 00:18:46 +01005028 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005029
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005030 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5031 psa_set_key_algorithm( &attributes, alg );
5032 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005033 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5034 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005035
5036 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01005037 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
5038 input1->x, input1->len,
5039 input2->x, input2->len, capacity ) )
5040 goto exit;
5041
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005042 psa_set_key_usage_flags( &attributes, derived_usage );
5043 psa_set_key_algorithm( &attributes, derived_alg );
5044 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005045 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005047 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005048
5049 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005050 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
5051 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5052 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005053
5054 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005055 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005056 goto exit;
5057
5058exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005059 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005060 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005061 psa_destroy_key( base_handle );
5062 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005063 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005064}
5065/* END_CASE */
5066
Janos Follath42fd8882019-07-03 14:17:09 +01005067/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005068void derive_key_export( int alg_arg,
5069 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005070 data_t *input1,
5071 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005072 int bytes1_arg,
5073 int bytes2_arg )
5074{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005075 psa_key_handle_t base_handle = 0;
5076 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005077 psa_algorithm_t alg = alg_arg;
5078 size_t bytes1 = bytes1_arg;
5079 size_t bytes2 = bytes2_arg;
5080 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005082 uint8_t *output_buffer = NULL;
5083 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005084 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5085 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005086 size_t length;
5087
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005088 ASSERT_ALLOC( output_buffer, capacity );
5089 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005090 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005091
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005092 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5093 psa_set_key_algorithm( &base_attributes, alg );
5094 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005095 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5096 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005097
5098 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01005099 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5100 input1->x, input1->len,
5101 input2->x, input2->len, capacity ) )
5102 goto exit;
5103
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005104 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005105 output_buffer,
5106 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005107 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005108
5109 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01005110 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5111 input1->x, input1->len,
5112 input2->x, input2->len, capacity ) )
5113 goto exit;
5114
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005115 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5116 psa_set_key_algorithm( &derived_attributes, 0 );
5117 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005118 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005119 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005120 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005121 PSA_ASSERT( psa_export_key( derived_handle,
5122 export_buffer, bytes1,
5123 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005124 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01005125 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005126 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005128 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005129 PSA_ASSERT( psa_export_key( derived_handle,
5130 export_buffer + bytes1, bytes2,
5131 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005132 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005133
5134 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005135 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5136 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005137
5138exit:
5139 mbedtls_free( output_buffer );
5140 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005141 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005142 psa_destroy_key( base_handle );
5143 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005144 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005145}
5146/* END_CASE */
5147
5148/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005149void derive_key( int alg_arg,
5150 data_t *key_data, data_t *input1, data_t *input2,
5151 int type_arg, int bits_arg,
5152 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005153{
5154 psa_key_handle_t base_handle = 0;
5155 psa_key_handle_t derived_handle = 0;
5156 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005157 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005158 size_t bits = bits_arg;
5159 psa_status_t expected_status = expected_status_arg;
5160 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5161 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5162 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5163
5164 PSA_ASSERT( psa_crypto_init( ) );
5165
5166 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5167 psa_set_key_algorithm( &base_attributes, alg );
5168 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5169 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5170 &base_handle ) );
5171
5172 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5173 input1->x, input1->len,
5174 input2->x, input2->len, SIZE_MAX ) )
5175 goto exit;
5176
5177 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5178 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005179 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005180 psa_set_key_bits( &derived_attributes, bits );
5181 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
5182 &derived_handle ),
5183 expected_status );
5184
5185exit:
5186 psa_key_derivation_abort( &operation );
5187 psa_destroy_key( base_handle );
5188 psa_destroy_key( derived_handle );
5189 PSA_DONE( );
5190}
5191/* END_CASE */
5192
5193/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005194void key_agreement_setup( int alg_arg,
5195 int our_key_type_arg, data_t *our_key_data,
5196 data_t *peer_key_data,
5197 int expected_status_arg )
5198{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005199 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005200 psa_algorithm_t alg = alg_arg;
5201 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005204 psa_status_t expected_status = expected_status_arg;
5205 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005206
Gilles Peskine8817f612018-12-18 00:18:46 +01005207 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005208
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005209 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5210 psa_set_key_algorithm( &attributes, alg );
5211 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005212 PSA_ASSERT( psa_import_key( &attributes,
5213 our_key_data->x, our_key_data->len,
5214 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005215
Gilles Peskine77f40d82019-04-11 21:27:06 +02005216 /* The tests currently include inputs that should fail at either step.
5217 * Test cases that fail at the setup step should be changed to call
5218 * key_derivation_setup instead, and this function should be renamed
5219 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005220 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005221 if( status == PSA_SUCCESS )
5222 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005223 TEST_EQUAL( psa_key_derivation_key_agreement(
5224 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5225 our_key,
5226 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005227 expected_status );
5228 }
5229 else
5230 {
5231 TEST_ASSERT( status == expected_status );
5232 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005233
5234exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005235 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005236 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005237 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005238}
5239/* END_CASE */
5240
5241/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005242void raw_key_agreement( int alg_arg,
5243 int our_key_type_arg, data_t *our_key_data,
5244 data_t *peer_key_data,
5245 data_t *expected_output )
5246{
5247 psa_key_handle_t our_key = 0;
5248 psa_algorithm_t alg = alg_arg;
5249 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005251 unsigned char *output = NULL;
5252 size_t output_length = ~0;
5253
5254 ASSERT_ALLOC( output, expected_output->len );
5255 PSA_ASSERT( psa_crypto_init( ) );
5256
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5258 psa_set_key_algorithm( &attributes, alg );
5259 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005260 PSA_ASSERT( psa_import_key( &attributes,
5261 our_key_data->x, our_key_data->len,
5262 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005263
Gilles Peskinebe697d82019-05-16 18:00:41 +02005264 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5265 peer_key_data->x, peer_key_data->len,
5266 output, expected_output->len,
5267 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005268 ASSERT_COMPARE( output, output_length,
5269 expected_output->x, expected_output->len );
5270
5271exit:
5272 mbedtls_free( output );
5273 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005274 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005275}
5276/* END_CASE */
5277
5278/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005279void key_agreement_capacity( int alg_arg,
5280 int our_key_type_arg, data_t *our_key_data,
5281 data_t *peer_key_data,
5282 int expected_capacity_arg )
5283{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005284 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005285 psa_algorithm_t alg = alg_arg;
5286 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005289 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005290 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005291
Gilles Peskine8817f612018-12-18 00:18:46 +01005292 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005293
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005294 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5295 psa_set_key_algorithm( &attributes, alg );
5296 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005297 PSA_ASSERT( psa_import_key( &attributes,
5298 our_key_data->x, our_key_data->len,
5299 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005300
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005301 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005302 PSA_ASSERT( psa_key_derivation_key_agreement(
5303 &operation,
5304 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5305 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005306 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5307 {
5308 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005310 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005311 NULL, 0 ) );
5312 }
Gilles Peskine59685592018-09-18 12:11:34 +02005313
Gilles Peskinebf491972018-10-25 22:36:12 +02005314 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005315 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005316 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005317 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005318
Gilles Peskinebf491972018-10-25 22:36:12 +02005319 /* Test the actual capacity by reading the output. */
5320 while( actual_capacity > sizeof( output ) )
5321 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005322 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005323 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005324 actual_capacity -= sizeof( output );
5325 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005326 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005327 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005328 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005329 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005330
Gilles Peskine59685592018-09-18 12:11:34 +02005331exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005333 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005334 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005335}
5336/* END_CASE */
5337
5338/* BEGIN_CASE */
5339void key_agreement_output( int alg_arg,
5340 int our_key_type_arg, data_t *our_key_data,
5341 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005342 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005343{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005344 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005345 psa_algorithm_t alg = alg_arg;
5346 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005349 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005350
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005351 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5352 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005353
Gilles Peskine8817f612018-12-18 00:18:46 +01005354 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005355
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005356 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5357 psa_set_key_algorithm( &attributes, alg );
5358 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005359 PSA_ASSERT( psa_import_key( &attributes,
5360 our_key_data->x, our_key_data->len,
5361 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005362
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005363 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005364 PSA_ASSERT( psa_key_derivation_key_agreement(
5365 &operation,
5366 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5367 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005368 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5369 {
5370 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005371 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005372 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005373 NULL, 0 ) );
5374 }
Gilles Peskine59685592018-09-18 12:11:34 +02005375
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005376 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005377 actual_output,
5378 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005379 ASSERT_COMPARE( actual_output, expected_output1->len,
5380 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005381 if( expected_output2->len != 0 )
5382 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005383 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005384 actual_output,
5385 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005386 ASSERT_COMPARE( actual_output, expected_output2->len,
5387 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005388 }
Gilles Peskine59685592018-09-18 12:11:34 +02005389
5390exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005391 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005392 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005393 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005394 mbedtls_free( actual_output );
5395}
5396/* END_CASE */
5397
5398/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005399void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005400{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005401 size_t bytes = bytes_arg;
5402 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005403 unsigned char *output = NULL;
5404 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005405 size_t i;
5406 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005407
Simon Butcher49f8e312020-03-03 15:51:50 +00005408 TEST_ASSERT( bytes_arg >= 0 );
5409
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005410 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5411 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005412 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005413
Gilles Peskine8817f612018-12-18 00:18:46 +01005414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005415
Gilles Peskinea50d7392018-06-21 10:22:13 +02005416 /* Run several times, to ensure that every output byte will be
5417 * nonzero at least once with overwhelming probability
5418 * (2^(-8*number_of_runs)). */
5419 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005420 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005421 if( bytes != 0 )
5422 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005423 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005424
5425 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005426 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5427 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005428
5429 for( i = 0; i < bytes; i++ )
5430 {
5431 if( output[i] != 0 )
5432 ++changed[i];
5433 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005434 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005435
5436 /* Check that every byte was changed to nonzero at least once. This
5437 * validates that psa_generate_random is overwriting every byte of
5438 * the output buffer. */
5439 for( i = 0; i < bytes; i++ )
5440 {
5441 TEST_ASSERT( changed[i] != 0 );
5442 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005443
5444exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005445 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005446 mbedtls_free( output );
5447 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005448}
5449/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005450
5451/* BEGIN_CASE */
5452void generate_key( int type_arg,
5453 int bits_arg,
5454 int usage_arg,
5455 int alg_arg,
5456 int expected_status_arg )
5457{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005458 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005459 psa_key_type_t type = type_arg;
5460 psa_key_usage_t usage = usage_arg;
5461 size_t bits = bits_arg;
5462 psa_algorithm_t alg = alg_arg;
5463 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005464 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005465 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005466
Gilles Peskine8817f612018-12-18 00:18:46 +01005467 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005468
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005469 psa_set_key_usage_flags( &attributes, usage );
5470 psa_set_key_algorithm( &attributes, alg );
5471 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005472 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005473
5474 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005475 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005476 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005477 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005478
5479 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005480 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5481 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5482 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005483
Gilles Peskine818ca122018-06-20 18:16:48 +02005484 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005485 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005486 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005487
5488exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005489 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005490 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005491 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005492}
5493/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005494
Gilles Peskinee56e8782019-04-26 17:34:02 +02005495/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5496void generate_key_rsa( int bits_arg,
5497 data_t *e_arg,
5498 int expected_status_arg )
5499{
5500 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005501 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005502 size_t bits = bits_arg;
5503 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5504 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5505 psa_status_t expected_status = expected_status_arg;
5506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5507 uint8_t *exported = NULL;
5508 size_t exported_size =
5509 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5510 size_t exported_length = SIZE_MAX;
5511 uint8_t *e_read_buffer = NULL;
5512 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005513 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005514 size_t e_read_length = SIZE_MAX;
5515
5516 if( e_arg->len == 0 ||
5517 ( e_arg->len == 3 &&
5518 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5519 {
5520 is_default_public_exponent = 1;
5521 e_read_size = 0;
5522 }
5523 ASSERT_ALLOC( e_read_buffer, e_read_size );
5524 ASSERT_ALLOC( exported, exported_size );
5525
5526 PSA_ASSERT( psa_crypto_init( ) );
5527
5528 psa_set_key_usage_flags( &attributes, usage );
5529 psa_set_key_algorithm( &attributes, alg );
5530 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5531 e_arg->x, e_arg->len ) );
5532 psa_set_key_bits( &attributes, bits );
5533
5534 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005535 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005536 if( expected_status != PSA_SUCCESS )
5537 goto exit;
5538
5539 /* Test the key information */
5540 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5541 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5542 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5543 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5544 e_read_buffer, e_read_size,
5545 &e_read_length ) );
5546 if( is_default_public_exponent )
5547 TEST_EQUAL( e_read_length, 0 );
5548 else
5549 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5550
5551 /* Do something with the key according to its type and permitted usage. */
5552 if( ! exercise_key( handle, usage, alg ) )
5553 goto exit;
5554
5555 /* Export the key and check the public exponent. */
5556 PSA_ASSERT( psa_export_public_key( handle,
5557 exported, exported_size,
5558 &exported_length ) );
5559 {
5560 uint8_t *p = exported;
5561 uint8_t *end = exported + exported_length;
5562 size_t len;
5563 /* RSAPublicKey ::= SEQUENCE {
5564 * modulus INTEGER, -- n
5565 * publicExponent INTEGER } -- e
5566 */
5567 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005568 MBEDTLS_ASN1_SEQUENCE |
5569 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005570 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5571 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5572 MBEDTLS_ASN1_INTEGER ) );
5573 if( len >= 1 && p[0] == 0 )
5574 {
5575 ++p;
5576 --len;
5577 }
5578 if( e_arg->len == 0 )
5579 {
5580 TEST_EQUAL( len, 3 );
5581 TEST_EQUAL( p[0], 1 );
5582 TEST_EQUAL( p[1], 0 );
5583 TEST_EQUAL( p[2], 1 );
5584 }
5585 else
5586 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5587 }
5588
5589exit:
5590 psa_reset_key_attributes( &attributes );
5591 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005592 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005593 mbedtls_free( e_read_buffer );
5594 mbedtls_free( exported );
5595}
5596/* END_CASE */
5597
Darryl Greend49a4992018-06-18 17:27:26 +01005598/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005599void persistent_key_load_key_from_storage( data_t *data,
5600 int type_arg, int bits_arg,
5601 int usage_flags_arg, int alg_arg,
5602 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005603{
Ronald Cron71016a92020-08-28 19:01:50 +02005604 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005606 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005607 psa_key_handle_t base_key = 0;
5608 psa_key_type_t type = type_arg;
5609 size_t bits = bits_arg;
5610 psa_key_usage_t usage_flags = usage_flags_arg;
5611 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005612 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005613 unsigned char *first_export = NULL;
5614 unsigned char *second_export = NULL;
5615 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5616 size_t first_exported_length;
5617 size_t second_exported_length;
5618
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005619 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5620 {
5621 ASSERT_ALLOC( first_export, export_size );
5622 ASSERT_ALLOC( second_export, export_size );
5623 }
Darryl Greend49a4992018-06-18 17:27:26 +01005624
Gilles Peskine8817f612018-12-18 00:18:46 +01005625 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005626
Gilles Peskinec87af662019-05-15 16:12:22 +02005627 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005628 psa_set_key_usage_flags( &attributes, usage_flags );
5629 psa_set_key_algorithm( &attributes, alg );
5630 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005631 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005632
Darryl Green0c6575a2018-11-07 16:05:30 +00005633 switch( generation_method )
5634 {
5635 case IMPORT_KEY:
5636 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005637 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5638 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005639 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005640
Darryl Green0c6575a2018-11-07 16:05:30 +00005641 case GENERATE_KEY:
5642 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005643 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005644 break;
5645
5646 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005647 {
5648 /* Create base key */
5649 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5650 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5651 psa_set_key_usage_flags( &base_attributes,
5652 PSA_KEY_USAGE_DERIVE );
5653 psa_set_key_algorithm( &base_attributes, derive_alg );
5654 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005655 PSA_ASSERT( psa_import_key( &base_attributes,
5656 data->x, data->len,
5657 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005658 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005659 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005660 PSA_ASSERT( psa_key_derivation_input_key(
5661 &operation,
5662 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005663 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005664 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005665 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005666 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5667 &operation,
5668 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005669 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005670 PSA_ASSERT( psa_destroy_key( base_key ) );
5671 base_key = 0;
5672 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005673 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005674 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005675 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005676
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005677 /* Export the key if permitted by the key policy. */
5678 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5679 {
5680 PSA_ASSERT( psa_export_key( handle,
5681 first_export, export_size,
5682 &first_exported_length ) );
5683 if( generation_method == IMPORT_KEY )
5684 ASSERT_COMPARE( data->x, data->len,
5685 first_export, first_exported_length );
5686 }
Darryl Greend49a4992018-06-18 17:27:26 +01005687
5688 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005689 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005690 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005691 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005692
Darryl Greend49a4992018-06-18 17:27:26 +01005693 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005694 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005695 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005696 TEST_ASSERT( mbedtls_svc_key_id_equal(
5697 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005698 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5699 PSA_KEY_LIFETIME_PERSISTENT );
5700 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5701 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5702 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5703 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005704
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005705 /* Export the key again if permitted by the key policy. */
5706 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005707 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005708 PSA_ASSERT( psa_export_key( handle,
5709 second_export, export_size,
5710 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005711 ASSERT_COMPARE( first_export, first_exported_length,
5712 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005713 }
5714
5715 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005716 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005717 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005718
5719exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005720 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005721 mbedtls_free( first_export );
5722 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005724 psa_destroy_key( base_key );
5725 if( handle == 0 )
5726 {
5727 /* In case there was a test failure after creating the persistent key
5728 * but while it was not open, try to re-open the persistent key
5729 * to delete it. */
Gilles Peskinee92c68a2020-08-26 00:06:25 +02005730 (void) psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005731 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005732 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005733 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005734}
5735/* END_CASE */