blob: a45d7e0b10cbb65763a3ec527c3109bf3bbfd998 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinec744d992019-07-30 17:26:54 +020012/* Tests that require more than 128kB of RAM plus change have this symbol
13 * as a dependency. Currently we always define this symbol, so the tests
14 * are always executed. In the future we should make this conditional
15 * so that tests that require a lot of memory are skipped on constrained
16 * platforms. */
Gilles Peskine49232e82019-08-07 11:01:30 +020017#define HAVE_RAM_AVAILABLE_128K
Gilles Peskinec744d992019-07-30 17:26:54 +020018
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020019#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020020#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020021
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinef426e0f2019-02-25 17:42:03 +010025/* A hash algorithm that is known to be supported.
26 *
27 * This is used in some smoke tests.
28 */
29#if defined(MBEDTLS_MD2_C)
30#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
31#elif defined(MBEDTLS_MD4_C)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
33#elif defined(MBEDTLS_MD5_C)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
35/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
36 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
37 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
38 * implausible anyway. */
39#elif defined(MBEDTLS_SHA1_C)
40#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
41#elif defined(MBEDTLS_SHA256_C)
42#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
43#elif defined(MBEDTLS_SHA512_C)
44#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
45#elif defined(MBEDTLS_SHA3_C)
46#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
47#else
48#undef KNOWN_SUPPORTED_HASH_ALG
49#endif
50
51/* A block cipher that is known to be supported.
52 *
53 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
54 */
55#if defined(MBEDTLS_AES_C)
56#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
57#elif defined(MBEDTLS_ARIA_C)
58#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
59#elif defined(MBEDTLS_CAMELLIA_C)
60#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
61#undef KNOWN_SUPPORTED_BLOCK_CIPHER
62#endif
63
64/* A MAC mode that is known to be supported.
65 *
66 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
67 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
68 *
69 * This is used in some smoke tests.
70 */
71#if defined(KNOWN_SUPPORTED_HASH_ALG)
72#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
73#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
74#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
75#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
76#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
77#else
78#undef KNOWN_SUPPORTED_MAC_ALG
79#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
80#endif
81
82/* A cipher algorithm and key type that are known to be supported.
83 *
84 * This is used in some smoke tests.
85 */
86#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
87#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
88#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
89#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
90#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
91#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
92#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
93#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
94#else
95#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
96#endif
97#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
98#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
99#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
100#elif defined(MBEDTLS_RC4_C)
101#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
102#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
103#else
104#undef KNOWN_SUPPORTED_CIPHER_ALG
105#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
106#endif
107
Gilles Peskine667c1112019-12-03 19:03:20 +0100108#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100109int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
Gilles Peskine667c1112019-12-03 19:03:20 +0100110{
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100111 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
112 PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100113}
114#else
115int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
116{
117 (void) lifetime;
118 return( 0 );
119}
120#endif
121
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200122/** Test if a buffer contains a constant byte value.
123 *
124 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200125 *
126 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200127 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200128 * \param size Size of the buffer in bytes.
129 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200130 * \return 1 if the buffer is all-bits-zero.
131 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200132 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200133static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200134{
135 size_t i;
136 for( i = 0; i < size; i++ )
137 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200138 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200139 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200140 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200141 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200142}
Gilles Peskine818ca122018-06-20 18:16:48 +0200143
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200144/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
145static int asn1_write_10x( unsigned char **p,
146 unsigned char *start,
147 size_t bits,
148 unsigned char x )
149{
150 int ret;
151 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200152 if( bits == 0 )
153 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
154 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200155 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300156 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200157 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
158 *p -= len;
159 ( *p )[len-1] = x;
160 if( bits % 8 == 0 )
161 ( *p )[1] |= 1;
162 else
163 ( *p )[0] |= 1 << ( bits % 8 );
164 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
165 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
166 MBEDTLS_ASN1_INTEGER ) );
167 return( len );
168}
169
170static int construct_fake_rsa_key( unsigned char *buffer,
171 size_t buffer_size,
172 unsigned char **p,
173 size_t bits,
174 int keypair )
175{
176 size_t half_bits = ( bits + 1 ) / 2;
177 int ret;
178 int len = 0;
179 /* Construct something that looks like a DER encoding of
180 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
181 * RSAPrivateKey ::= SEQUENCE {
182 * version Version,
183 * modulus INTEGER, -- n
184 * publicExponent INTEGER, -- e
185 * privateExponent INTEGER, -- d
186 * prime1 INTEGER, -- p
187 * prime2 INTEGER, -- q
188 * exponent1 INTEGER, -- d mod (p-1)
189 * exponent2 INTEGER, -- d mod (q-1)
190 * coefficient INTEGER, -- (inverse of q) mod p
191 * otherPrimeInfos OtherPrimeInfos OPTIONAL
192 * }
193 * Or, for a public key, the same structure with only
194 * version, modulus and publicExponent.
195 */
196 *p = buffer + buffer_size;
197 if( keypair )
198 {
199 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
200 asn1_write_10x( p, buffer, half_bits, 1 ) );
201 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
202 asn1_write_10x( p, buffer, half_bits, 1 ) );
203 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
204 asn1_write_10x( p, buffer, half_bits, 1 ) );
205 MBEDTLS_ASN1_CHK_ADD( len, /* q */
206 asn1_write_10x( p, buffer, half_bits, 1 ) );
207 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
208 asn1_write_10x( p, buffer, half_bits, 3 ) );
209 MBEDTLS_ASN1_CHK_ADD( len, /* d */
210 asn1_write_10x( p, buffer, bits, 1 ) );
211 }
212 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
213 asn1_write_10x( p, buffer, 17, 1 ) );
214 MBEDTLS_ASN1_CHK_ADD( len, /* n */
215 asn1_write_10x( p, buffer, bits, 1 ) );
216 if( keypair )
217 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
218 mbedtls_asn1_write_int( p, buffer, 0 ) );
219 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
220 {
221 const unsigned char tag =
222 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
223 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
224 }
225 return( len );
226}
227
Ronald Cron5425a212020-08-04 14:58:35 +0200228int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100229{
230 int ok = 0;
231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
232 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200233 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100234 psa_key_type_t type;
235 psa_key_type_t bits;
236
237 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
238 lifetime = psa_get_key_lifetime( &attributes );
239 id = psa_get_key_id( &attributes );
240 type = psa_get_key_type( &attributes );
241 bits = psa_get_key_bits( &attributes );
242
243 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200244 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200245 {
246 TEST_ASSERT(
247 ( PSA_KEY_ID_VOLATILE_MIN <=
248 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
249 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
250 PSA_KEY_ID_VOLATILE_MAX ) );
251 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100252 else
253 {
254 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200255 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
256 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100257 }
258#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
259 /* randomly-generated 64-bit constant, should never appear in test data */
260 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
261 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100262 if( lifetime_is_dynamic_secure_element( lifetime ) )
Gilles Peskine667c1112019-12-03 19:03:20 +0100263 {
264 /* Mbed Crypto currently always exposes the slot number to
265 * applications. This is not mandated by the PSA specification
266 * and may change in future versions. */
267 TEST_EQUAL( status, 0 );
268 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
269 }
270 else
271 {
272 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
273 }
274#endif
275
276 /* Type and size */
277 TEST_ASSERT( type != 0 );
278 TEST_ASSERT( bits != 0 );
279 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
280 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
281 TEST_ASSERT( bits % 8 == 0 );
282
283 /* MAX macros concerning specific key types */
284 if( PSA_KEY_TYPE_IS_ECC( type ) )
285 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
286 else if( PSA_KEY_TYPE_IS_RSA( type ) )
287 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
288 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) <= PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE );
289
290 ok = 1;
291
292exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100293 /*
294 * Key attributes may have been returned by psa_get_key_attributes()
295 * thus reset them as required.
296 */
Gilles Peskine667c1112019-12-03 19:03:20 +0100297 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100298
Gilles Peskine667c1112019-12-03 19:03:20 +0100299 return( ok );
300}
301
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100302int exercise_mac_setup( psa_key_type_t key_type,
303 const unsigned char *key_bytes,
304 size_t key_length,
305 psa_algorithm_t alg,
306 psa_mac_operation_t *operation,
307 psa_status_t *status )
308{
Ronald Cron5425a212020-08-04 14:58:35 +0200309 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200310 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100311
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100312 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200313 psa_set_key_algorithm( &attributes, alg );
314 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200315 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100316
Ronald Cron5425a212020-08-04 14:58:35 +0200317 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100318 /* Whether setup succeeded or failed, abort must succeed. */
319 PSA_ASSERT( psa_mac_abort( operation ) );
320 /* If setup failed, reproduce the failure, so that the caller can
321 * test the resulting state of the operation object. */
322 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100323 {
Ronald Cron5425a212020-08-04 14:58:35 +0200324 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100325 }
326
Ronald Cron5425a212020-08-04 14:58:35 +0200327 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100328 return( 1 );
329
330exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200331 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100332 return( 0 );
333}
334
335int exercise_cipher_setup( psa_key_type_t key_type,
336 const unsigned char *key_bytes,
337 size_t key_length,
338 psa_algorithm_t alg,
339 psa_cipher_operation_t *operation,
340 psa_status_t *status )
341{
Ronald Cron5425a212020-08-04 14:58:35 +0200342 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100344
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200345 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
346 psa_set_key_algorithm( &attributes, alg );
347 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200348 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100349
Ronald Cron5425a212020-08-04 14:58:35 +0200350 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100351 /* Whether setup succeeded or failed, abort must succeed. */
352 PSA_ASSERT( psa_cipher_abort( operation ) );
353 /* If setup failed, reproduce the failure, so that the caller can
354 * test the resulting state of the operation object. */
355 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100356 {
Ronald Cron5425a212020-08-04 14:58:35 +0200357 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100358 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100359 }
360
Ronald Cron5425a212020-08-04 14:58:35 +0200361 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100362 return( 1 );
363
364exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200365 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100366 return( 0 );
367}
368
Ronald Cron5425a212020-08-04 14:58:35 +0200369static int exercise_mac_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200370 psa_key_usage_t usage,
371 psa_algorithm_t alg )
372{
Jaeden Amero769ce272019-01-04 11:48:03 +0000373 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200375 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200376 size_t mac_length = sizeof( mac );
377
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100378 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200379 {
Ronald Cron5425a212020-08-04 14:58:35 +0200380 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100381 PSA_ASSERT( psa_mac_update( &operation,
382 input, sizeof( input ) ) );
383 PSA_ASSERT( psa_mac_sign_finish( &operation,
384 mac, sizeof( mac ),
385 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200386 }
387
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100388 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200389 {
390 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100391 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200392 PSA_SUCCESS :
393 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200394 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100395 PSA_ASSERT( psa_mac_update( &operation,
396 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100397 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
398 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200399 }
400
401 return( 1 );
402
403exit:
404 psa_mac_abort( &operation );
405 return( 0 );
406}
407
Ronald Cron5425a212020-08-04 14:58:35 +0200408static int exercise_cipher_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200409 psa_key_usage_t usage,
410 psa_algorithm_t alg )
411{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000412 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200413 unsigned char iv[16] = {0};
414 size_t iv_length = sizeof( iv );
415 const unsigned char plaintext[16] = "Hello, world...";
416 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
417 size_t ciphertext_length = sizeof( ciphertext );
418 unsigned char decrypted[sizeof( ciphertext )];
419 size_t part_length;
420
421 if( usage & PSA_KEY_USAGE_ENCRYPT )
422 {
Ronald Cron5425a212020-08-04 14:58:35 +0200423 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100424 PSA_ASSERT( psa_cipher_generate_iv( &operation,
425 iv, sizeof( iv ),
426 &iv_length ) );
427 PSA_ASSERT( psa_cipher_update( &operation,
428 plaintext, sizeof( plaintext ),
429 ciphertext, sizeof( ciphertext ),
430 &ciphertext_length ) );
431 PSA_ASSERT( psa_cipher_finish( &operation,
432 ciphertext + ciphertext_length,
433 sizeof( ciphertext ) - ciphertext_length,
434 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200435 ciphertext_length += part_length;
436 }
437
438 if( usage & PSA_KEY_USAGE_DECRYPT )
439 {
440 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200441 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200442 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
443 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200445 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200446 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
447 * have this macro yet. */
448 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
449 psa_get_key_type( &attributes ) );
450 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100451 psa_reset_key_attributes( &attributes );
Gilles Peskine818ca122018-06-20 18:16:48 +0200452 }
Ronald Cron5425a212020-08-04 14:58:35 +0200453 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100454 PSA_ASSERT( psa_cipher_set_iv( &operation,
455 iv, iv_length ) );
456 PSA_ASSERT( psa_cipher_update( &operation,
457 ciphertext, ciphertext_length,
458 decrypted, sizeof( decrypted ),
459 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200460 status = psa_cipher_finish( &operation,
461 decrypted + part_length,
462 sizeof( decrypted ) - part_length,
463 &part_length );
464 /* For a stream cipher, all inputs are valid. For a block cipher,
465 * if the input is some aribtrary data rather than an actual
466 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200467 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200468 TEST_ASSERT( status == PSA_SUCCESS ||
469 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200470 else
471 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200472 }
473
474 return( 1 );
475
476exit:
477 psa_cipher_abort( &operation );
478 return( 0 );
479}
480
Ronald Cron5425a212020-08-04 14:58:35 +0200481static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200482 psa_key_usage_t usage,
483 psa_algorithm_t alg )
484{
485 unsigned char nonce[16] = {0};
486 size_t nonce_length = sizeof( nonce );
487 unsigned char plaintext[16] = "Hello, world...";
488 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
489 size_t ciphertext_length = sizeof( ciphertext );
490 size_t plaintext_length = sizeof( ciphertext );
491
492 if( usage & PSA_KEY_USAGE_ENCRYPT )
493 {
Ronald Cron5425a212020-08-04 14:58:35 +0200494 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100495 nonce, nonce_length,
496 NULL, 0,
497 plaintext, sizeof( plaintext ),
498 ciphertext, sizeof( ciphertext ),
499 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200500 }
501
502 if( usage & PSA_KEY_USAGE_DECRYPT )
503 {
504 psa_status_t verify_status =
505 ( usage & PSA_KEY_USAGE_ENCRYPT ?
506 PSA_SUCCESS :
507 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200508 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100509 nonce, nonce_length,
510 NULL, 0,
511 ciphertext, ciphertext_length,
512 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100513 &plaintext_length ),
514 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200515 }
516
517 return( 1 );
518
519exit:
520 return( 0 );
521}
522
Ronald Cron5425a212020-08-04 14:58:35 +0200523static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200524 psa_key_usage_t usage,
525 psa_algorithm_t alg )
526{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200527 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
528 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100529 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200530 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100531 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
532
533 /* If the policy allows signing with any hash, just pick one. */
534 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
535 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100536#if defined(KNOWN_SUPPORTED_HASH_ALG)
537 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
538 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100539#else
Chris Jones9634bb12021-01-20 15:56:42 +0000540 mbedtls_test_fail( "No hash algorithm for hash-and-sign testing",
541 __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100542 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100543#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100544 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200545
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100546 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200547 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200548 /* Some algorithms require the payload to have the size of
549 * the hash encoded in the algorithm. Use this input size
550 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200551 if( hash_alg != 0 )
552 payload_length = PSA_HASH_SIZE( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200553 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100554 payload, payload_length,
555 signature, sizeof( signature ),
556 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200557 }
558
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100559 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200560 {
561 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100562 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200563 PSA_SUCCESS :
564 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200565 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100566 payload, payload_length,
567 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100568 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200569 }
570
571 return( 1 );
572
573exit:
574 return( 0 );
575}
576
Ronald Cron5425a212020-08-04 14:58:35 +0200577static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200578 psa_key_usage_t usage,
579 psa_algorithm_t alg )
580{
581 unsigned char plaintext[256] = "Hello, world...";
582 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
583 size_t ciphertext_length = sizeof( ciphertext );
584 size_t plaintext_length = 16;
585
586 if( usage & PSA_KEY_USAGE_ENCRYPT )
587 {
Ronald Cron5425a212020-08-04 14:58:35 +0200588 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100589 plaintext, plaintext_length,
590 NULL, 0,
591 ciphertext, sizeof( ciphertext ),
592 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200593 }
594
595 if( usage & PSA_KEY_USAGE_DECRYPT )
596 {
597 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200598 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200599 ciphertext, ciphertext_length,
600 NULL, 0,
601 plaintext, sizeof( plaintext ),
602 &plaintext_length );
603 TEST_ASSERT( status == PSA_SUCCESS ||
604 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
605 ( status == PSA_ERROR_INVALID_ARGUMENT ||
606 status == PSA_ERROR_INVALID_PADDING ) ) );
607 }
608
609 return( 1 );
610
611exit:
612 return( 0 );
613}
Gilles Peskine02b75072018-07-01 22:31:34 +0200614
Janos Follathf2815ea2019-07-03 12:41:36 +0100615static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200616 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100617 psa_algorithm_t alg,
618 unsigned char* input1, size_t input1_length,
619 unsigned char* input2, size_t input2_length,
620 size_t capacity )
621{
622 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
623 if( PSA_ALG_IS_HKDF( alg ) )
624 {
625 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
626 PSA_KEY_DERIVATION_INPUT_SALT,
627 input1, input1_length ) );
628 PSA_ASSERT( psa_key_derivation_input_key( operation,
629 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200630 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100631 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
632 PSA_KEY_DERIVATION_INPUT_INFO,
633 input2,
634 input2_length ) );
635 }
636 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
637 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
638 {
639 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
640 PSA_KEY_DERIVATION_INPUT_SEED,
641 input1, input1_length ) );
642 PSA_ASSERT( psa_key_derivation_input_key( operation,
643 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200644 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100645 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
646 PSA_KEY_DERIVATION_INPUT_LABEL,
647 input2, input2_length ) );
648 }
649 else
650 {
651 TEST_ASSERT( ! "Key derivation algorithm not supported" );
652 }
653
Gilles Peskinec744d992019-07-30 17:26:54 +0200654 if( capacity != SIZE_MAX )
655 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100656
657 return( 1 );
658
659exit:
660 return( 0 );
661}
662
663
Ronald Cron5425a212020-08-04 14:58:35 +0200664static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200665 psa_key_usage_t usage,
666 psa_algorithm_t alg )
667{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200668 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100669 unsigned char input1[] = "Input 1";
670 size_t input1_length = sizeof( input1 );
671 unsigned char input2[] = "Input 2";
672 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200673 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100674 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200675
676 if( usage & PSA_KEY_USAGE_DERIVE )
677 {
Ronald Cron5425a212020-08-04 14:58:35 +0200678 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100679 input1, input1_length,
680 input2, input2_length, capacity ) )
681 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200682
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200683 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200684 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100685 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200686 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200687 }
688
689 return( 1 );
690
691exit:
692 return( 0 );
693}
694
Gilles Peskinec7998b72018-11-07 18:45:02 +0100695/* We need two keys to exercise key agreement. Exercise the
696 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200697static psa_status_t key_agreement_with_self(
698 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200699 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100700{
701 psa_key_type_t private_key_type;
702 psa_key_type_t public_key_type;
703 size_t key_bits;
704 uint8_t *public_key = NULL;
705 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200706 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200707 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
708 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200709 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200710 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100711
Ronald Cron5425a212020-08-04 14:58:35 +0200712 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200713 private_key_type = psa_get_key_type( &attributes );
714 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200715 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100716 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
717 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200718 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100719 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100720
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200721 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200722 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200723 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100724exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100725 /*
726 * Key attributes may have been returned by psa_get_key_attributes()
727 * thus reset them as required.
728 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200729 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100730
731 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100732 return( status );
733}
734
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200735/* We need two keys to exercise key agreement. Exercise the
736 * private key against its own public key. */
737static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200738 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200739{
740 psa_key_type_t private_key_type;
741 psa_key_type_t public_key_type;
742 size_t key_bits;
743 uint8_t *public_key = NULL;
744 size_t public_key_length;
745 uint8_t output[1024];
746 size_t output_length;
747 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200748 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
749 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200750 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200751 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200752
Ronald Cron5425a212020-08-04 14:58:35 +0200753 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200754 private_key_type = psa_get_key_type( &attributes );
755 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200756 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200757 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
758 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200759 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200760 public_key, public_key_length,
761 &public_key_length ) );
762
Ronald Cron5425a212020-08-04 14:58:35 +0200763 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200764 public_key, public_key_length,
765 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200766exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100767 /*
768 * Key attributes may have been returned by psa_get_key_attributes()
769 * thus reset them as required.
770 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200771 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100772
773 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200774 return( status );
775}
776
Ronald Cron5425a212020-08-04 14:58:35 +0200777static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200778 psa_key_usage_t usage,
779 psa_algorithm_t alg )
780{
781 int ok = 0;
782
783 if( usage & PSA_KEY_USAGE_DERIVE )
784 {
785 /* We need two keys to exercise key agreement. Exercise the
786 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200787 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200788 }
789 ok = 1;
790
791exit:
792 return( ok );
793}
794
Ronald Cron5425a212020-08-04 14:58:35 +0200795static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200796 psa_key_usage_t usage,
797 psa_algorithm_t alg )
798{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200799 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200800 unsigned char output[1];
801 int ok = 0;
802
803 if( usage & PSA_KEY_USAGE_DERIVE )
804 {
805 /* We need two keys to exercise key agreement. Exercise the
806 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200807 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200808 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200809 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200810 output,
811 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200812 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200813 }
814 ok = 1;
815
816exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200817 return( ok );
818}
819
Jaeden Amerof7dca862019-06-27 17:31:33 +0100820int asn1_skip_integer( unsigned char **p, const unsigned char *end,
821 size_t min_bits, size_t max_bits,
822 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200823{
824 size_t len;
825 size_t actual_bits;
826 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100827 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100828 MBEDTLS_ASN1_INTEGER ),
829 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200830
831 /* Check if the retrieved length doesn't extend the actual buffer's size.
832 * It is assumed here, that end >= p, which validates casting to size_t. */
833 TEST_ASSERT( len <= (size_t)( end - *p) );
834
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200835 /* Tolerate a slight departure from DER encoding:
836 * - 0 may be represented by an empty string or a 1-byte string.
837 * - The sign bit may be used as a value bit. */
838 if( ( len == 1 && ( *p )[0] == 0 ) ||
839 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
840 {
841 ++( *p );
842 --len;
843 }
844 if( min_bits == 0 && len == 0 )
845 return( 1 );
846 msb = ( *p )[0];
847 TEST_ASSERT( msb != 0 );
848 actual_bits = 8 * ( len - 1 );
849 while( msb != 0 )
850 {
851 msb >>= 1;
852 ++actual_bits;
853 }
854 TEST_ASSERT( actual_bits >= min_bits );
855 TEST_ASSERT( actual_bits <= max_bits );
856 if( must_be_odd )
857 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
858 *p += len;
859 return( 1 );
860exit:
861 return( 0 );
862}
863
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200864static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
865 uint8_t *exported, size_t exported_length )
866{
867 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100868 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200869 else
870 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200871
872#if defined(MBEDTLS_DES_C)
873 if( type == PSA_KEY_TYPE_DES )
874 {
875 /* Check the parity bits. */
876 unsigned i;
877 for( i = 0; i < bits / 8; i++ )
878 {
879 unsigned bit_count = 0;
880 unsigned m;
881 for( m = 1; m <= 0x100; m <<= 1 )
882 {
883 if( exported[i] & m )
884 ++bit_count;
885 }
886 TEST_ASSERT( bit_count % 2 != 0 );
887 }
888 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200889 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200890#endif
891
892#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200893 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200894 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200895 uint8_t *p = exported;
896 uint8_t *end = exported + exported_length;
897 size_t len;
898 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200899 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200900 * modulus INTEGER, -- n
901 * publicExponent INTEGER, -- e
902 * privateExponent INTEGER, -- d
903 * prime1 INTEGER, -- p
904 * prime2 INTEGER, -- q
905 * exponent1 INTEGER, -- d mod (p-1)
906 * exponent2 INTEGER, -- d mod (q-1)
907 * coefficient INTEGER, -- (inverse of q) mod p
908 * }
909 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100910 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
911 MBEDTLS_ASN1_SEQUENCE |
912 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
913 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200914 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
915 goto exit;
916 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
917 goto exit;
918 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
919 goto exit;
920 /* Require d to be at least half the size of n. */
921 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
922 goto exit;
923 /* Require p and q to be at most half the size of n, rounded up. */
924 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
925 goto exit;
926 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
927 goto exit;
928 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
929 goto exit;
930 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
931 goto exit;
932 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
933 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100934 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100935 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200936 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200937#endif /* MBEDTLS_RSA_C */
938
939#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200940 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200941 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100942 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100943 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100944 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200945 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200946#endif /* MBEDTLS_ECP_C */
947
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200948 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
949 {
950 uint8_t *p = exported;
951 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200952#if defined(MBEDTLS_RSA_C)
953 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
954 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100955 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200956 /* RSAPublicKey ::= SEQUENCE {
957 * modulus INTEGER, -- n
958 * publicExponent INTEGER } -- e
959 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100960 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
961 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100962 MBEDTLS_ASN1_CONSTRUCTED ),
963 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100964 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200965 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
966 goto exit;
967 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
968 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100969 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200970 }
971 else
972#endif /* MBEDTLS_RSA_C */
973#if defined(MBEDTLS_ECP_C)
974 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
975 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200976 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
977 {
978 /* The representation of an ECC Montgomery public key is
979 * the raw compressed point */
980 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
981 }
982 else
983 {
984 /* The representation of an ECC Weierstrass public key is:
985 * - The byte 0x04;
986 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
987 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
988 * - where m is the bit size associated with the curve.
989 */
990 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
991 TEST_EQUAL( p[0], 4 );
992 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200993 }
994 else
995#endif /* MBEDTLS_ECP_C */
996 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100997 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200998 mbedtls_snprintf( message, sizeof( message ),
999 "No sanity check for public key type=0x%08lx",
1000 (unsigned long) type );
Chris Jones9634bb12021-01-20 15:56:42 +00001001 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +02001002 (void) p;
1003 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001004 return( 0 );
1005 }
1006 }
1007 else
1008
1009 {
1010 /* No sanity checks for other types */
1011 }
1012
1013 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001014
1015exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001016 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001017}
1018
Ronald Cron5425a212020-08-04 14:58:35 +02001019static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001020 psa_key_usage_t usage )
1021{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001022 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001023 uint8_t *exported = NULL;
1024 size_t exported_size = 0;
1025 size_t exported_length = 0;
1026 int ok = 0;
1027
Ronald Cron5425a212020-08-04 14:58:35 +02001028 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001029
1030 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001031 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001032 {
Ronald Cron5425a212020-08-04 14:58:35 +02001033 TEST_EQUAL( psa_export_key( key, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001034 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001035 ok = 1;
1036 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001037 }
1038
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001039 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
1040 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001041 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001042
Ronald Cron5425a212020-08-04 14:58:35 +02001043 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001044 exported, exported_size,
1045 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001046 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1047 psa_get_key_bits( &attributes ),
1048 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001049
1050exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001051 /*
1052 * Key attributes may have been returned by psa_get_key_attributes()
1053 * thus reset them as required.
1054 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001055 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001056
1057 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001058 return( ok );
1059}
1060
Ronald Cron5425a212020-08-04 14:58:35 +02001061static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001062{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001063 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001064 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001065 uint8_t *exported = NULL;
1066 size_t exported_size = 0;
1067 size_t exported_length = 0;
1068 int ok = 0;
1069
Ronald Cron5425a212020-08-04 14:58:35 +02001070 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001071 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001072 {
Ronald Cron5425a212020-08-04 14:58:35 +02001073 TEST_EQUAL( psa_export_public_key( key, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001074 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +02001075 return( 1 );
1076 }
1077
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001078 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001079 psa_get_key_type( &attributes ) );
1080 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
1081 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001082 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001083
Ronald Cron5425a212020-08-04 14:58:35 +02001084 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001085 exported, exported_size,
1086 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001087 ok = exported_key_sanity_check( public_type,
1088 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001089 exported, exported_length );
1090
1091exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001092 /*
1093 * Key attributes may have been returned by psa_get_key_attributes()
1094 * thus reset them as required.
1095 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001096 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001097
1098 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001099 return( ok );
1100}
1101
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001102/** Do smoke tests on a key.
1103 *
1104 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1105 * sign/verify, or derivation) that is permitted according to \p usage.
1106 * \p usage and \p alg should correspond to the expected policy on the
1107 * key.
1108 *
1109 * Export the key if permitted by \p usage, and check that the output
1110 * looks sensible. If \p usage forbids export, check that
1111 * \p psa_export_key correctly rejects the attempt. If the key is
1112 * asymmetric, also check \p psa_export_public_key.
1113 *
1114 * If the key fails the tests, this function calls the test framework's
Chris Jones9634bb12021-01-20 15:56:42 +00001115 * `mbedtls_test_fail` function and returns false. Otherwise this function
1116 * returns true. Therefore it should be used as follows:
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001117 * ```
1118 * if( ! exercise_key( ... ) ) goto exit;
1119 * ```
1120 *
Ronald Cron5425a212020-08-04 14:58:35 +02001121 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001122 * \p alg.
1123 * \param usage The usage flags to assume.
1124 * \param alg The algorithm to exercise.
1125 *
1126 * \retval 0 The key failed the smoke tests.
1127 * \retval 1 The key passed the smoke tests.
1128 */
Ronald Cron5425a212020-08-04 14:58:35 +02001129static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001130 psa_key_usage_t usage,
1131 psa_algorithm_t alg )
1132{
1133 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001134
Ronald Cron5425a212020-08-04 14:58:35 +02001135 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001136 return( 0 );
1137
Gilles Peskine02b75072018-07-01 22:31:34 +02001138 if( alg == 0 )
1139 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1140 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001141 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001142 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001143 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001144 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001145 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001146 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001147 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001148 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001149 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001150 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001151 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001152 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001153 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001154 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001155 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001156 else
1157 {
1158 char message[40];
1159 mbedtls_snprintf( message, sizeof( message ),
1160 "No code to exercise alg=0x%08lx",
1161 (unsigned long) alg );
Chris Jones9634bb12021-01-20 15:56:42 +00001162 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskine02b75072018-07-01 22:31:34 +02001163 ok = 0;
1164 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001165
Ronald Cron5425a212020-08-04 14:58:35 +02001166 ok = ok && exercise_export_key( key, usage );
1167 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001168
Gilles Peskine02b75072018-07-01 22:31:34 +02001169 return( ok );
1170}
1171
Gilles Peskine10df3412018-10-25 22:35:43 +02001172static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1173 psa_algorithm_t alg )
1174{
1175 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1176 {
1177 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001178 PSA_KEY_USAGE_VERIFY_HASH :
1179 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001180 }
1181 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1182 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1183 {
1184 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1185 PSA_KEY_USAGE_ENCRYPT :
1186 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1187 }
1188 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1189 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1190 {
1191 return( PSA_KEY_USAGE_DERIVE );
1192 }
1193 else
1194 {
1195 return( 0 );
1196 }
1197
1198}
Darryl Green0c6575a2018-11-07 16:05:30 +00001199
Ronald Cron5425a212020-08-04 14:58:35 +02001200static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001201{
1202 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001203 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001204 uint8_t buffer[1];
1205 size_t length;
1206 int ok = 0;
1207
Ronald Cronecfb2372020-07-23 17:13:42 +02001208 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001209 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1210 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1211 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001212 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001213 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001214 TEST_EQUAL(
1215 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1216 TEST_EQUAL(
1217 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001218 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001219 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1220 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1221 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1222 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1223
Ronald Cron5425a212020-08-04 14:58:35 +02001224 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001225 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001226 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001227 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001228 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001229
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001230 ok = 1;
1231
1232exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001233 /*
1234 * Key attributes may have been returned by psa_get_key_attributes()
1235 * thus reset them as required.
1236 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001237 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001238
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001239 return( ok );
1240}
1241
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001242/* Assert that a key isn't reported as having a slot number. */
1243#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1244#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1245 do \
1246 { \
1247 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1248 TEST_EQUAL( psa_get_key_slot_number( \
1249 attributes, \
1250 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1251 PSA_ERROR_INVALID_ARGUMENT ); \
1252 } \
1253 while( 0 )
1254#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1255#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1256 ( (void) 0 )
1257#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1258
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001259/* An overapproximation of the amount of storage needed for a key of the
1260 * given type and with the given content. The API doesn't make it easy
1261 * to find a good value for the size. The current implementation doesn't
1262 * care about the value anyway. */
1263#define KEY_BITS_FROM_DATA( type, data ) \
1264 ( data )->len
1265
Darryl Green0c6575a2018-11-07 16:05:30 +00001266typedef enum {
1267 IMPORT_KEY = 0,
1268 GENERATE_KEY = 1,
1269 DERIVE_KEY = 2
1270} generate_method;
1271
Gilles Peskinee59236f2018-01-27 23:32:46 +01001272/* END_HEADER */
1273
1274/* BEGIN_DEPENDENCIES
1275 * depends_on:MBEDTLS_PSA_CRYPTO_C
1276 * END_DEPENDENCIES
1277 */
1278
1279/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001280void static_checks( )
1281{
1282 size_t max_truncated_mac_size =
1283 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1284
1285 /* Check that the length for a truncated MAC always fits in the algorithm
1286 * encoding. The shifted mask is the maximum truncated value. The
1287 * untruncated algorithm may be one byte larger. */
1288 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001289
1290#if defined(MBEDTLS_TEST_DEPRECATED)
1291 /* Check deprecated constants. */
1292 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1293 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1294 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1295 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1296 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1297 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1298 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1299 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001300
Paul Elliott8ff510a2020-06-02 17:19:28 +01001301 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1302 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1303 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1304 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1305 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1306 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1308 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1309 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1310 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1311 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1312 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1314 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1326 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1328 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1329 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1330 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1331
1332 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1333 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1334 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1335 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1336 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1337 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1338 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1339 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001340
Paul Elliott75e27032020-06-03 15:17:39 +01001341 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1342 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1343 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1344 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1345 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1346
1347 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1348 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001349#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001350}
1351/* END_CASE */
1352
1353/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001354void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001355 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001356 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001357{
Gilles Peskine4747d192019-04-17 15:05:45 +02001358 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001359 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001360 psa_key_lifetime_t lifetime = lifetime_arg;
1361 psa_key_usage_t usage_flags = usage_flags_arg;
1362 psa_algorithm_t alg = alg_arg;
1363 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001364 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001365
Ronald Cronecfb2372020-07-23 17:13:42 +02001366 TEST_EQUAL(
1367 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1368 TEST_EQUAL(
1369 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001370 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1371 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1372 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1373 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001374 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001375
Gilles Peskinec87af662019-05-15 16:12:22 +02001376 psa_set_key_id( &attributes, id );
1377 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001378 psa_set_key_usage_flags( &attributes, usage_flags );
1379 psa_set_key_algorithm( &attributes, alg );
1380 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001381 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001382
Ronald Cronecfb2372020-07-23 17:13:42 +02001383 TEST_ASSERT( mbedtls_svc_key_id_equal(
1384 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001385 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1386 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1387 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1388 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001389 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001390
1391 psa_reset_key_attributes( &attributes );
1392
Ronald Cronecfb2372020-07-23 17:13:42 +02001393 TEST_EQUAL(
1394 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1395 TEST_EQUAL(
1396 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001397 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1398 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1399 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1400 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001401 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001402}
1403/* END_CASE */
1404
1405/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001406void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1407 int id2_arg, int owner_id2_arg,
1408 int expected_id_arg, int expected_owner_id_arg,
1409 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001410{
1411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001412 mbedtls_svc_key_id_t id1 =
1413 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001414 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001415 mbedtls_svc_key_id_t id2 =
1416 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001417 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001418 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001419 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1420
1421 if( id1_arg != -1 )
1422 psa_set_key_id( &attributes, id1 );
1423 if( lifetime_arg != -1 )
1424 psa_set_key_lifetime( &attributes, lifetime );
1425 if( id2_arg != -1 )
1426 psa_set_key_id( &attributes, id2 );
1427
Ronald Cronecfb2372020-07-23 17:13:42 +02001428 TEST_ASSERT( mbedtls_svc_key_id_equal(
1429 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001430 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1431}
1432/* END_CASE */
1433
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001434/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1435void slot_number_attribute( )
1436{
1437 psa_key_slot_number_t slot_number = 0xdeadbeef;
1438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1439
1440 /* Initially, there is no slot number. */
1441 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1442 PSA_ERROR_INVALID_ARGUMENT );
1443
1444 /* Test setting a slot number. */
1445 psa_set_key_slot_number( &attributes, 0 );
1446 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1447 TEST_EQUAL( slot_number, 0 );
1448
1449 /* Test changing the slot number. */
1450 psa_set_key_slot_number( &attributes, 42 );
1451 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1452 TEST_EQUAL( slot_number, 42 );
1453
1454 /* Test clearing the slot number. */
1455 psa_clear_key_slot_number( &attributes );
1456 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1457 PSA_ERROR_INVALID_ARGUMENT );
1458
1459 /* Clearing again should have no effect. */
1460 psa_clear_key_slot_number( &attributes );
1461 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1462 PSA_ERROR_INVALID_ARGUMENT );
1463
1464 /* Test that reset clears the slot number. */
1465 psa_set_key_slot_number( &attributes, 42 );
1466 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1467 TEST_EQUAL( slot_number, 42 );
1468 psa_reset_key_attributes( &attributes );
1469 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1470 PSA_ERROR_INVALID_ARGUMENT );
1471}
1472/* END_CASE */
1473
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001474/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001475void import_with_policy( int type_arg,
1476 int usage_arg, int alg_arg,
1477 int expected_status_arg )
1478{
1479 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1480 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001481 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001482 psa_key_type_t type = type_arg;
1483 psa_key_usage_t usage = usage_arg;
1484 psa_algorithm_t alg = alg_arg;
1485 psa_status_t expected_status = expected_status_arg;
1486 const uint8_t key_material[16] = {0};
1487 psa_status_t status;
1488
1489 PSA_ASSERT( psa_crypto_init( ) );
1490
1491 psa_set_key_type( &attributes, type );
1492 psa_set_key_usage_flags( &attributes, usage );
1493 psa_set_key_algorithm( &attributes, alg );
1494
1495 status = psa_import_key( &attributes,
1496 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001497 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001498 TEST_EQUAL( status, expected_status );
1499 if( status != PSA_SUCCESS )
1500 goto exit;
1501
Ronald Cron5425a212020-08-04 14:58:35 +02001502 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001503 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1504 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1505 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001506 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001507
Ronald Cron5425a212020-08-04 14:58:35 +02001508 PSA_ASSERT( psa_destroy_key( key ) );
1509 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001510
1511exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001512 /*
1513 * Key attributes may have been returned by psa_get_key_attributes()
1514 * thus reset them as required.
1515 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001516 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001517
1518 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001519 PSA_DONE( );
1520}
1521/* END_CASE */
1522
1523/* BEGIN_CASE */
1524void import_with_data( data_t *data, int type_arg,
1525 int attr_bits_arg,
1526 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001527{
1528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1529 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001530 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001531 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001532 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001533 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001534 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001535
Gilles Peskine8817f612018-12-18 00:18:46 +01001536 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001537
Gilles Peskine4747d192019-04-17 15:05:45 +02001538 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001539 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001540
Ronald Cron5425a212020-08-04 14:58:35 +02001541 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001542 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001543 if( status != PSA_SUCCESS )
1544 goto exit;
1545
Ronald Cron5425a212020-08-04 14:58:35 +02001546 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001547 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001548 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001549 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001550 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001551
Ronald Cron5425a212020-08-04 14:58:35 +02001552 PSA_ASSERT( psa_destroy_key( key ) );
1553 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001554
1555exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001556 /*
1557 * Key attributes may have been returned by psa_get_key_attributes()
1558 * thus reset them as required.
1559 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001560 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001561
1562 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001563 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564}
1565/* END_CASE */
1566
1567/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001568void import_large_key( int type_arg, int byte_size_arg,
1569 int expected_status_arg )
1570{
1571 psa_key_type_t type = type_arg;
1572 size_t byte_size = byte_size_arg;
1573 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1574 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001575 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001576 psa_status_t status;
1577 uint8_t *buffer = NULL;
1578 size_t buffer_size = byte_size + 1;
1579 size_t n;
1580
1581 /* It would be better to skip the test than fail it if the allocation
1582 * fails, but the test framework doesn't support this yet. */
1583 ASSERT_ALLOC( buffer, buffer_size );
1584 memset( buffer, 'K', byte_size );
1585
1586 PSA_ASSERT( psa_crypto_init( ) );
1587
1588 /* Try importing the key */
1589 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1590 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001591 status = psa_import_key( &attributes, buffer, byte_size, &key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001592 TEST_EQUAL( status, expected_status );
1593
1594 if( status == PSA_SUCCESS )
1595 {
Ronald Cron5425a212020-08-04 14:58:35 +02001596 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001597 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1598 TEST_EQUAL( psa_get_key_bits( &attributes ),
1599 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001600 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001601 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001602 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001603 for( n = 0; n < byte_size; n++ )
1604 TEST_EQUAL( buffer[n], 'K' );
1605 for( n = byte_size; n < buffer_size; n++ )
1606 TEST_EQUAL( buffer[n], 0 );
1607 }
1608
1609exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001610 /*
1611 * Key attributes may have been returned by psa_get_key_attributes()
1612 * thus reset them as required.
1613 */
1614 psa_reset_key_attributes( &attributes );
1615
Ronald Cron5425a212020-08-04 14:58:35 +02001616 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001617 PSA_DONE( );
1618 mbedtls_free( buffer );
1619}
1620/* END_CASE */
1621
1622/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001623void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1624{
Ronald Cron5425a212020-08-04 14:58:35 +02001625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001626 size_t bits = bits_arg;
1627 psa_status_t expected_status = expected_status_arg;
1628 psa_status_t status;
1629 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001630 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001631 size_t buffer_size = /* Slight overapproximations */
1632 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001633 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001634 unsigned char *p;
1635 int ret;
1636 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001637 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001638
Gilles Peskine8817f612018-12-18 00:18:46 +01001639 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001640 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001641
1642 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1643 bits, keypair ) ) >= 0 );
1644 length = ret;
1645
1646 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001647 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001648 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001649 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001650
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001651 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001652 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001653
1654exit:
1655 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001656 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001657}
1658/* END_CASE */
1659
1660/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001661void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001662 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001663 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001664 int expected_bits,
1665 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001666 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001667 int canonical_input )
1668{
Ronald Cron5425a212020-08-04 14:58:35 +02001669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001670 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001671 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001672 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001673 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001674 unsigned char *exported = NULL;
1675 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001676 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001677 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001678 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001679 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001680 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001681
Moran Pekercb088e72018-07-17 17:36:59 +03001682 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001683 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001684 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001685 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001686 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001687
Gilles Peskine4747d192019-04-17 15:05:45 +02001688 psa_set_key_usage_flags( &attributes, usage_arg );
1689 psa_set_key_algorithm( &attributes, alg );
1690 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001691
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001692 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001693 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001694
1695 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001696 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001697 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1698 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001699 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001700
1701 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001702 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001703 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001704
1705 /* The exported length must be set by psa_export_key() to a value between 0
1706 * and export_size. On errors, the exported length must be 0. */
1707 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1708 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1709 TEST_ASSERT( exported_length <= export_size );
1710
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001711 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001712 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001713 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001714 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001715 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001716 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001717 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001718
Ronald Cron5425a212020-08-04 14:58:35 +02001719 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001720 goto exit;
1721
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001722 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001723 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001724 else
1725 {
Ronald Cron5425a212020-08-04 14:58:35 +02001726 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001727 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001728 &key2 ) );
1729 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001730 reexported,
1731 export_size,
1732 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001733 ASSERT_COMPARE( exported, exported_length,
1734 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001735 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001736 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001737 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001738
1739destroy:
1740 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001741 PSA_ASSERT( psa_destroy_key( key ) );
1742 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001743
1744exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001745 /*
1746 * Key attributes may have been returned by psa_get_key_attributes()
1747 * thus reset them as required.
1748 */
1749 psa_reset_key_attributes( &got_attributes );
1750
itayzafrir3e02b3b2018-06-12 17:06:52 +03001751 mbedtls_free( exported );
1752 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001753 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001754}
1755/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001756
Moran Pekerf709f4a2018-06-06 17:26:04 +03001757/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001758void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001759 int type_arg,
1760 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001761 int export_size_delta,
1762 int expected_export_status_arg,
1763 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001764{
Ronald Cron5425a212020-08-04 14:58:35 +02001765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001766 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001767 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001768 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001769 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001770 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001771 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001772 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001774
Gilles Peskine8817f612018-12-18 00:18:46 +01001775 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001776
Gilles Peskine4747d192019-04-17 15:05:45 +02001777 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1778 psa_set_key_algorithm( &attributes, alg );
1779 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001780
1781 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001782 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001783
Gilles Peskine49c25912018-10-29 15:15:31 +01001784 /* Export the public key */
1785 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001786 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001787 exported, export_size,
1788 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001789 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001790 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001791 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001792 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001793 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001794 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001795 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001796 TEST_ASSERT( expected_public_key->len <=
1797 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001798 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1799 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001800 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001801
1802exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001803 /*
1804 * Key attributes may have been returned by psa_get_key_attributes()
1805 * thus reset them as required.
1806 */
1807 psa_reset_key_attributes( &attributes );
1808
itayzafrir3e02b3b2018-06-12 17:06:52 +03001809 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001810 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001811 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001812}
1813/* END_CASE */
1814
Gilles Peskine20035e32018-02-03 22:44:14 +01001815/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001816void import_and_exercise_key( data_t *data,
1817 int type_arg,
1818 int bits_arg,
1819 int alg_arg )
1820{
Ronald Cron5425a212020-08-04 14:58:35 +02001821 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001822 psa_key_type_t type = type_arg;
1823 size_t bits = bits_arg;
1824 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001825 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001826 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001827 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001828
Gilles Peskine8817f612018-12-18 00:18:46 +01001829 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001830
Gilles Peskine4747d192019-04-17 15:05:45 +02001831 psa_set_key_usage_flags( &attributes, usage );
1832 psa_set_key_algorithm( &attributes, alg );
1833 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001834
1835 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001836 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001837
1838 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001839 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001840 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1841 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001842
1843 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001844 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001845 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001846
Ronald Cron5425a212020-08-04 14:58:35 +02001847 PSA_ASSERT( psa_destroy_key( key ) );
1848 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001849
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001850exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001851 /*
1852 * Key attributes may have been returned by psa_get_key_attributes()
1853 * thus reset them as required.
1854 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001855 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001856
1857 psa_reset_key_attributes( &attributes );
1858 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001859 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001860}
1861/* END_CASE */
1862
1863/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001864void effective_key_attributes( int type_arg, int expected_type_arg,
1865 int bits_arg, int expected_bits_arg,
1866 int usage_arg, int expected_usage_arg,
1867 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001868{
Ronald Cron5425a212020-08-04 14:58:35 +02001869 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001870 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001871 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001872 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001873 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001874 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001875 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001876 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001877 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001879
Gilles Peskine8817f612018-12-18 00:18:46 +01001880 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001881
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001882 psa_set_key_usage_flags( &attributes, usage );
1883 psa_set_key_algorithm( &attributes, alg );
1884 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001885 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001886
Ronald Cron5425a212020-08-04 14:58:35 +02001887 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001888 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001889
Ronald Cron5425a212020-08-04 14:58:35 +02001890 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001891 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1892 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1893 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1894 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001895
1896exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001897 /*
1898 * Key attributes may have been returned by psa_get_key_attributes()
1899 * thus reset them as required.
1900 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001901 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001902
1903 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001904 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001905}
1906/* END_CASE */
1907
1908/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001909void check_key_policy( int type_arg, int bits_arg,
1910 int usage_arg, int alg_arg )
1911{
1912 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1913 usage_arg, usage_arg, alg_arg, alg_arg );
1914 goto exit;
1915}
1916/* END_CASE */
1917
1918/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001919void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001920{
1921 /* Test each valid way of initializing the object, except for `= {0}`, as
1922 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1923 * though it's OK by the C standard. We could test for this, but we'd need
1924 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001925 psa_key_attributes_t func = psa_key_attributes_init( );
1926 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1927 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001928
1929 memset( &zero, 0, sizeof( zero ) );
1930
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001931 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1932 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1933 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001934
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001935 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1936 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1937 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1938
1939 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1940 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1941 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1942
1943 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1944 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1945 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1946
1947 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1948 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1949 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001950}
1951/* END_CASE */
1952
1953/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001954void mac_key_policy( int policy_usage,
1955 int policy_alg,
1956 int key_type,
1957 data_t *key_data,
1958 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001959{
Ronald Cron5425a212020-08-04 14:58:35 +02001960 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001962 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001963 psa_status_t status;
1964 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001965
Gilles Peskine8817f612018-12-18 00:18:46 +01001966 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001967
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001968 psa_set_key_usage_flags( &attributes, policy_usage );
1969 psa_set_key_algorithm( &attributes, policy_alg );
1970 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001971
Gilles Peskine049c7532019-05-15 20:22:09 +02001972 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001973 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001974
Ronald Cron5425a212020-08-04 14:58:35 +02001975 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001976 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001977 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001978 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001979 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001980 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001981 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001982
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001984 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001986 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001987 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001988 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001989 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990
1991exit:
1992 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001993 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001994 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995}
1996/* END_CASE */
1997
1998/* BEGIN_CASE */
1999void cipher_key_policy( int policy_usage,
2000 int policy_alg,
2001 int key_type,
2002 data_t *key_data,
2003 int exercise_alg )
2004{
Ronald Cron5425a212020-08-04 14:58:35 +02002005 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002007 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002008 psa_status_t status;
2009
Gilles Peskine8817f612018-12-18 00:18:46 +01002010 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002011
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002012 psa_set_key_usage_flags( &attributes, policy_usage );
2013 psa_set_key_algorithm( &attributes, policy_alg );
2014 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002015
Gilles Peskine049c7532019-05-15 20:22:09 +02002016 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002017 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002018
Ronald Cron5425a212020-08-04 14:58:35 +02002019 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020 if( policy_alg == exercise_alg &&
2021 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002022 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002024 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025 psa_cipher_abort( &operation );
2026
Ronald Cron5425a212020-08-04 14:58:35 +02002027 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028 if( policy_alg == exercise_alg &&
2029 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002030 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002031 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002032 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033
2034exit:
2035 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002036 psa_destroy_key( key );
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 aead_key_policy( int policy_usage,
2043 int policy_alg,
2044 int key_type,
2045 data_t *key_data,
2046 int nonce_length_arg,
2047 int tag_length_arg,
2048 int exercise_alg )
2049{
Ronald Cron5425a212020-08-04 14:58:35 +02002050 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002051 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002052 psa_status_t status;
2053 unsigned char nonce[16] = {0};
2054 size_t nonce_length = nonce_length_arg;
2055 unsigned char tag[16];
2056 size_t tag_length = tag_length_arg;
2057 size_t output_length;
2058
2059 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
2060 TEST_ASSERT( tag_length <= sizeof( tag ) );
2061
Gilles Peskine8817f612018-12-18 00:18:46 +01002062 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002063
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002064 psa_set_key_usage_flags( &attributes, policy_usage );
2065 psa_set_key_algorithm( &attributes, policy_alg );
2066 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002067
Gilles Peskine049c7532019-05-15 20:22:09 +02002068 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002069 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070
Ronald Cron5425a212020-08-04 14:58:35 +02002071 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002072 nonce, nonce_length,
2073 NULL, 0,
2074 NULL, 0,
2075 tag, tag_length,
2076 &output_length );
2077 if( policy_alg == exercise_alg &&
2078 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002079 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002080 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002081 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002082
2083 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002084 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002085 nonce, nonce_length,
2086 NULL, 0,
2087 tag, tag_length,
2088 NULL, 0,
2089 &output_length );
2090 if( policy_alg == exercise_alg &&
2091 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002092 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002093 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002094 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002095
2096exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002097 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002098 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099}
2100/* END_CASE */
2101
2102/* BEGIN_CASE */
2103void asymmetric_encryption_key_policy( int policy_usage,
2104 int policy_alg,
2105 int key_type,
2106 data_t *key_data,
2107 int exercise_alg )
2108{
Ronald Cron5425a212020-08-04 14:58:35 +02002109 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002111 psa_status_t status;
2112 size_t key_bits;
2113 size_t buffer_length;
2114 unsigned char *buffer = NULL;
2115 size_t output_length;
2116
Gilles Peskine8817f612018-12-18 00:18:46 +01002117 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002119 psa_set_key_usage_flags( &attributes, policy_usage );
2120 psa_set_key_algorithm( &attributes, policy_alg );
2121 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122
Gilles Peskine049c7532019-05-15 20:22:09 +02002123 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002124 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
Ronald Cron5425a212020-08-04 14:58:35 +02002126 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002127 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2129 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002130 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002131
Ronald Cron5425a212020-08-04 14:58:35 +02002132 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002133 NULL, 0,
2134 NULL, 0,
2135 buffer, buffer_length,
2136 &output_length );
2137 if( policy_alg == exercise_alg &&
2138 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002139 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002140 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002141 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002142
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002143 if( buffer_length != 0 )
2144 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002145 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002146 buffer, buffer_length,
2147 NULL, 0,
2148 buffer, buffer_length,
2149 &output_length );
2150 if( policy_alg == exercise_alg &&
2151 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002152 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002153 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002154 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002155
2156exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002157 /*
2158 * Key attributes may have been returned by psa_get_key_attributes()
2159 * thus reset them as required.
2160 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002161 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002162
2163 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002164 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002165 mbedtls_free( buffer );
2166}
2167/* END_CASE */
2168
2169/* BEGIN_CASE */
2170void asymmetric_signature_key_policy( int policy_usage,
2171 int policy_alg,
2172 int key_type,
2173 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002174 int exercise_alg,
2175 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002176{
Ronald Cron5425a212020-08-04 14:58:35 +02002177 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002180 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2181 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2182 * compatible with the policy and `payload_length_arg` is supposed to be
2183 * a valid input length to sign. If `payload_length_arg <= 0`,
2184 * `exercise_alg` is supposed to be forbidden by the policy. */
2185 int compatible_alg = payload_length_arg > 0;
2186 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002187 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002188 size_t signature_length;
2189
Gilles Peskine8817f612018-12-18 00:18:46 +01002190 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002191
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002192 psa_set_key_usage_flags( &attributes, policy_usage );
2193 psa_set_key_algorithm( &attributes, policy_alg );
2194 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002195
Gilles Peskine049c7532019-05-15 20:22:09 +02002196 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002197 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002198
Ronald Cron5425a212020-08-04 14:58:35 +02002199 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002200 payload, payload_length,
2201 signature, sizeof( signature ),
2202 &signature_length );
2203 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002204 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002205 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002206 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002207
2208 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002209 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002210 payload, payload_length,
2211 signature, sizeof( signature ) );
2212 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002213 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002214 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002215 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002216
2217exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002218 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002219 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002220}
2221/* END_CASE */
2222
Janos Follathba3fab92019-06-11 14:50:16 +01002223/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002224void derive_key_policy( int policy_usage,
2225 int policy_alg,
2226 int key_type,
2227 data_t *key_data,
2228 int exercise_alg )
2229{
Ronald Cron5425a212020-08-04 14:58:35 +02002230 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002232 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002233 psa_status_t status;
2234
Gilles Peskine8817f612018-12-18 00:18:46 +01002235 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002236
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002237 psa_set_key_usage_flags( &attributes, policy_usage );
2238 psa_set_key_algorithm( &attributes, policy_alg );
2239 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002240
Gilles Peskine049c7532019-05-15 20:22:09 +02002241 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002242 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002243
Janos Follathba3fab92019-06-11 14:50:16 +01002244 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2245
2246 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2247 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002248 {
Janos Follathba3fab92019-06-11 14:50:16 +01002249 PSA_ASSERT( psa_key_derivation_input_bytes(
2250 &operation,
2251 PSA_KEY_DERIVATION_INPUT_SEED,
2252 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002253 }
Janos Follathba3fab92019-06-11 14:50:16 +01002254
2255 status = psa_key_derivation_input_key( &operation,
2256 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002257 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002258
Gilles Peskineea0fb492018-07-12 17:17:20 +02002259 if( policy_alg == exercise_alg &&
2260 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002261 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002262 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002263 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002264
2265exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002266 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002267 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002268 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002269}
2270/* END_CASE */
2271
2272/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002273void agreement_key_policy( int policy_usage,
2274 int policy_alg,
2275 int key_type_arg,
2276 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002277 int exercise_alg,
2278 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002279{
Ronald Cron5425a212020-08-04 14:58:35 +02002280 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002282 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002283 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002284 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002285 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002286
Gilles Peskine8817f612018-12-18 00:18:46 +01002287 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002288
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_set_key_usage_flags( &attributes, policy_usage );
2290 psa_set_key_algorithm( &attributes, policy_alg );
2291 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002292
Gilles Peskine049c7532019-05-15 20:22:09 +02002293 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002294 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002295
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002296 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002297 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002298
Steven Cooremance48e852020-10-05 16:02:45 +02002299 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002300
2301exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002302 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002303 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002304 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002305}
2306/* END_CASE */
2307
2308/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002309void key_policy_alg2( int key_type_arg, data_t *key_data,
2310 int usage_arg, int alg_arg, int alg2_arg )
2311{
Ronald Cron5425a212020-08-04 14:58:35 +02002312 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002313 psa_key_type_t key_type = key_type_arg;
2314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2315 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2316 psa_key_usage_t usage = usage_arg;
2317 psa_algorithm_t alg = alg_arg;
2318 psa_algorithm_t alg2 = alg2_arg;
2319
2320 PSA_ASSERT( psa_crypto_init( ) );
2321
2322 psa_set_key_usage_flags( &attributes, usage );
2323 psa_set_key_algorithm( &attributes, alg );
2324 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2325 psa_set_key_type( &attributes, key_type );
2326 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002327 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002328
Ronald Cron5425a212020-08-04 14:58:35 +02002329 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002330 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2331 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2332 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2333
Ronald Cron5425a212020-08-04 14:58:35 +02002334 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002335 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002336 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002337 goto exit;
2338
2339exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002340 /*
2341 * Key attributes may have been returned by psa_get_key_attributes()
2342 * thus reset them as required.
2343 */
2344 psa_reset_key_attributes( &got_attributes );
2345
Ronald Cron5425a212020-08-04 14:58:35 +02002346 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002347 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002348}
2349/* END_CASE */
2350
2351/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002352void raw_agreement_key_policy( int policy_usage,
2353 int policy_alg,
2354 int key_type_arg,
2355 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002356 int exercise_alg,
2357 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002358{
Ronald Cron5425a212020-08-04 14:58:35 +02002359 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002361 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002362 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002363 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002364 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002365
2366 PSA_ASSERT( psa_crypto_init( ) );
2367
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002368 psa_set_key_usage_flags( &attributes, policy_usage );
2369 psa_set_key_algorithm( &attributes, policy_alg );
2370 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002371
Gilles Peskine049c7532019-05-15 20:22:09 +02002372 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002373 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002374
Ronald Cron5425a212020-08-04 14:58:35 +02002375 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002376
Steven Cooremance48e852020-10-05 16:02:45 +02002377 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002378
2379exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002380 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002381 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002382 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002383}
2384/* END_CASE */
2385
2386/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002387void copy_success( int source_usage_arg,
2388 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002389 int type_arg, data_t *material,
2390 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002391 int target_usage_arg,
2392 int target_alg_arg, int target_alg2_arg,
2393 int expected_usage_arg,
2394 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002395{
Gilles Peskineca25db92019-04-19 11:43:08 +02002396 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2397 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002398 psa_key_usage_t expected_usage = expected_usage_arg;
2399 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002400 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002401 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2402 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002403 uint8_t *export_buffer = NULL;
2404
Gilles Peskine57ab7212019-01-28 13:03:09 +01002405 PSA_ASSERT( psa_crypto_init( ) );
2406
Gilles Peskineca25db92019-04-19 11:43:08 +02002407 /* Prepare the source key. */
2408 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2409 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002410 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002411 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002412 PSA_ASSERT( psa_import_key( &source_attributes,
2413 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002414 &source_key ) );
2415 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002416
Gilles Peskineca25db92019-04-19 11:43:08 +02002417 /* Prepare the target attributes. */
2418 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002419 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002420 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002421 /* Set volatile lifetime to reset the key identifier to 0. */
2422 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2423 }
2424
Gilles Peskineca25db92019-04-19 11:43:08 +02002425 if( target_usage_arg != -1 )
2426 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2427 if( target_alg_arg != -1 )
2428 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002429 if( target_alg2_arg != -1 )
2430 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002431
2432 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002433 PSA_ASSERT( psa_copy_key( source_key,
2434 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002435
2436 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002437 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002438
2439 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002440 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002441 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2442 psa_get_key_type( &target_attributes ) );
2443 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2444 psa_get_key_bits( &target_attributes ) );
2445 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2446 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002447 TEST_EQUAL( expected_alg2,
2448 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002449 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2450 {
2451 size_t length;
2452 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002453 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002454 material->len, &length ) );
2455 ASSERT_COMPARE( material->x, material->len,
2456 export_buffer, length );
2457 }
Ronald Cron5425a212020-08-04 14:58:35 +02002458 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002459 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002460 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002461 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002462
Ronald Cron5425a212020-08-04 14:58:35 +02002463 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002464
2465exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002466 /*
2467 * Source and target key attributes may have been returned by
2468 * psa_get_key_attributes() thus reset them as required.
2469 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002470 psa_reset_key_attributes( &source_attributes );
2471 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002472
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002473 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002474 mbedtls_free( export_buffer );
2475}
2476/* END_CASE */
2477
2478/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002479void copy_fail( int source_usage_arg,
2480 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002481 int type_arg, data_t *material,
2482 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002483 int target_usage_arg,
2484 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002485 int expected_status_arg )
2486{
2487 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2488 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002489 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2490 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002491
2492 PSA_ASSERT( psa_crypto_init( ) );
2493
2494 /* Prepare the source key. */
2495 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2496 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002497 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002498 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002499 PSA_ASSERT( psa_import_key( &source_attributes,
2500 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002501 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002502
2503 /* Prepare the target attributes. */
2504 psa_set_key_type( &target_attributes, target_type_arg );
2505 psa_set_key_bits( &target_attributes, target_bits_arg );
2506 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2507 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002508 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002509
2510 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002511 TEST_EQUAL( psa_copy_key( source_key,
2512 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002513 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002514
Ronald Cron5425a212020-08-04 14:58:35 +02002515 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002516
Gilles Peskine4a644642019-05-03 17:14:08 +02002517exit:
2518 psa_reset_key_attributes( &source_attributes );
2519 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002520 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002521}
2522/* END_CASE */
2523
2524/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002525void hash_operation_init( )
2526{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002527 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002528 /* Test each valid way of initializing the object, except for `= {0}`, as
2529 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2530 * though it's OK by the C standard. We could test for this, but we'd need
2531 * to supress the Clang warning for the test. */
2532 psa_hash_operation_t func = psa_hash_operation_init( );
2533 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2534 psa_hash_operation_t zero;
2535
2536 memset( &zero, 0, sizeof( zero ) );
2537
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002538 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002539 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2540 PSA_ERROR_BAD_STATE );
2541 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2542 PSA_ERROR_BAD_STATE );
2543 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2544 PSA_ERROR_BAD_STATE );
2545
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002546 /* A default hash operation should be abortable without error. */
2547 PSA_ASSERT( psa_hash_abort( &func ) );
2548 PSA_ASSERT( psa_hash_abort( &init ) );
2549 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002550}
2551/* END_CASE */
2552
2553/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002554void hash_setup( int alg_arg,
2555 int expected_status_arg )
2556{
2557 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002558 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002559 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002560 psa_status_t status;
2561
Gilles Peskine8817f612018-12-18 00:18:46 +01002562 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002564 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002565 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002566
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002567 /* Whether setup succeeded or failed, abort must succeed. */
2568 PSA_ASSERT( psa_hash_abort( &operation ) );
2569
2570 /* If setup failed, reproduce the failure, so as to
2571 * test the resulting state of the operation object. */
2572 if( status != PSA_SUCCESS )
2573 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2574
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002575 /* Now the operation object should be reusable. */
2576#if defined(KNOWN_SUPPORTED_HASH_ALG)
2577 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2578 PSA_ASSERT( psa_hash_abort( &operation ) );
2579#endif
2580
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002581exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002582 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002583}
2584/* END_CASE */
2585
2586/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002587void hash_compute_fail( int alg_arg, data_t *input,
2588 int output_size_arg, int expected_status_arg )
2589{
2590 psa_algorithm_t alg = alg_arg;
2591 uint8_t *output = NULL;
2592 size_t output_size = output_size_arg;
2593 size_t output_length = INVALID_EXPORT_LENGTH;
2594 psa_status_t expected_status = expected_status_arg;
2595 psa_status_t status;
2596
2597 ASSERT_ALLOC( output, output_size );
2598
2599 PSA_ASSERT( psa_crypto_init( ) );
2600
2601 status = psa_hash_compute( alg, input->x, input->len,
2602 output, output_size, &output_length );
2603 TEST_EQUAL( status, expected_status );
2604 TEST_ASSERT( output_length <= output_size );
2605
2606exit:
2607 mbedtls_free( output );
2608 PSA_DONE( );
2609}
2610/* END_CASE */
2611
2612/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002613void hash_compare_fail( int alg_arg, data_t *input,
2614 data_t *reference_hash,
2615 int expected_status_arg )
2616{
2617 psa_algorithm_t alg = alg_arg;
2618 psa_status_t expected_status = expected_status_arg;
2619 psa_status_t status;
2620
2621 PSA_ASSERT( psa_crypto_init( ) );
2622
2623 status = psa_hash_compare( alg, input->x, input->len,
2624 reference_hash->x, reference_hash->len );
2625 TEST_EQUAL( status, expected_status );
2626
2627exit:
2628 PSA_DONE( );
2629}
2630/* END_CASE */
2631
2632/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002633void hash_compute_compare( int alg_arg, data_t *input,
2634 data_t *expected_output )
2635{
2636 psa_algorithm_t alg = alg_arg;
2637 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2638 size_t output_length = INVALID_EXPORT_LENGTH;
2639 size_t i;
2640
2641 PSA_ASSERT( psa_crypto_init( ) );
2642
2643 /* Compute with tight buffer */
2644 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2645 output, PSA_HASH_SIZE( alg ),
2646 &output_length ) );
2647 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2648 ASSERT_COMPARE( output, output_length,
2649 expected_output->x, expected_output->len );
2650
2651 /* Compute with larger buffer */
2652 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2653 output, sizeof( output ),
2654 &output_length ) );
2655 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2656 ASSERT_COMPARE( output, output_length,
2657 expected_output->x, expected_output->len );
2658
2659 /* Compare with correct hash */
2660 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2661 output, output_length ) );
2662
2663 /* Compare with trailing garbage */
2664 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2665 output, output_length + 1 ),
2666 PSA_ERROR_INVALID_SIGNATURE );
2667
2668 /* Compare with truncated hash */
2669 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2670 output, output_length - 1 ),
2671 PSA_ERROR_INVALID_SIGNATURE );
2672
2673 /* Compare with corrupted value */
2674 for( i = 0; i < output_length; i++ )
2675 {
Chris Jones9634bb12021-01-20 15:56:42 +00002676 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002677 output[i] ^= 1;
2678 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2679 output, output_length ),
2680 PSA_ERROR_INVALID_SIGNATURE );
2681 output[i] ^= 1;
2682 }
2683
2684exit:
2685 PSA_DONE( );
2686}
2687/* END_CASE */
2688
2689/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002690void hash_bad_order( )
2691{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002692 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002693 unsigned char input[] = "";
2694 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002695 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002696 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2697 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2698 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002699 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002700 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002701 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002702
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002704
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002705 /* Call setup twice in a row. */
2706 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2707 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2708 PSA_ERROR_BAD_STATE );
2709 PSA_ASSERT( psa_hash_abort( &operation ) );
2710
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002711 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002712 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002713 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002714 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002715
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002716 /* Call update after finish. */
2717 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2718 PSA_ASSERT( psa_hash_finish( &operation,
2719 hash, sizeof( hash ), &hash_len ) );
2720 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002721 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002722 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002723
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002724 /* Call verify without calling setup beforehand. */
2725 TEST_EQUAL( psa_hash_verify( &operation,
2726 valid_hash, sizeof( valid_hash ) ),
2727 PSA_ERROR_BAD_STATE );
2728 PSA_ASSERT( psa_hash_abort( &operation ) );
2729
2730 /* Call verify after finish. */
2731 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2732 PSA_ASSERT( psa_hash_finish( &operation,
2733 hash, sizeof( hash ), &hash_len ) );
2734 TEST_EQUAL( psa_hash_verify( &operation,
2735 valid_hash, sizeof( valid_hash ) ),
2736 PSA_ERROR_BAD_STATE );
2737 PSA_ASSERT( psa_hash_abort( &operation ) );
2738
2739 /* Call verify twice in a row. */
2740 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2741 PSA_ASSERT( psa_hash_verify( &operation,
2742 valid_hash, sizeof( valid_hash ) ) );
2743 TEST_EQUAL( psa_hash_verify( &operation,
2744 valid_hash, sizeof( valid_hash ) ),
2745 PSA_ERROR_BAD_STATE );
2746 PSA_ASSERT( psa_hash_abort( &operation ) );
2747
2748 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002749 TEST_EQUAL( psa_hash_finish( &operation,
2750 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002751 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002752 PSA_ASSERT( psa_hash_abort( &operation ) );
2753
2754 /* Call finish twice in a row. */
2755 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2756 PSA_ASSERT( psa_hash_finish( &operation,
2757 hash, sizeof( hash ), &hash_len ) );
2758 TEST_EQUAL( psa_hash_finish( &operation,
2759 hash, sizeof( hash ), &hash_len ),
2760 PSA_ERROR_BAD_STATE );
2761 PSA_ASSERT( psa_hash_abort( &operation ) );
2762
2763 /* Call finish after calling verify. */
2764 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2765 PSA_ASSERT( psa_hash_verify( &operation,
2766 valid_hash, sizeof( valid_hash ) ) );
2767 TEST_EQUAL( psa_hash_finish( &operation,
2768 hash, sizeof( hash ), &hash_len ),
2769 PSA_ERROR_BAD_STATE );
2770 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002771
2772exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002773 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002774}
2775/* END_CASE */
2776
itayzafrir27e69452018-11-01 14:26:34 +02002777/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2778void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002779{
2780 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002781 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2782 * appended to it */
2783 unsigned char hash[] = {
2784 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2785 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2786 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002787 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002788 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002789
Gilles Peskine8817f612018-12-18 00:18:46 +01002790 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002791
itayzafrir27e69452018-11-01 14:26:34 +02002792 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002793 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002794 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002795 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002796
itayzafrir27e69452018-11-01 14:26:34 +02002797 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002799 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002800 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002801
itayzafrir27e69452018-11-01 14:26:34 +02002802 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002804 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002805 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002806
itayzafrirec93d302018-10-18 18:01:10 +03002807exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002808 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002809}
2810/* END_CASE */
2811
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002812/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2813void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002814{
2815 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002816 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002817 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002818 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002819 size_t hash_len;
2820
Gilles Peskine8817f612018-12-18 00:18:46 +01002821 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002822
itayzafrir58028322018-10-25 10:22:01 +03002823 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002824 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002825 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002826 hash, expected_size - 1, &hash_len ),
2827 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002828
2829exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002830 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002831}
2832/* END_CASE */
2833
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002834/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2835void hash_clone_source_state( )
2836{
2837 psa_algorithm_t alg = PSA_ALG_SHA_256;
2838 unsigned char hash[PSA_HASH_MAX_SIZE];
2839 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2840 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2841 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2842 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2843 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2844 size_t hash_len;
2845
2846 PSA_ASSERT( psa_crypto_init( ) );
2847 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2848
2849 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2850 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2851 PSA_ASSERT( psa_hash_finish( &op_finished,
2852 hash, sizeof( hash ), &hash_len ) );
2853 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2854 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2855
2856 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2857 PSA_ERROR_BAD_STATE );
2858
2859 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2860 PSA_ASSERT( psa_hash_finish( &op_init,
2861 hash, sizeof( hash ), &hash_len ) );
2862 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2863 PSA_ASSERT( psa_hash_finish( &op_finished,
2864 hash, sizeof( hash ), &hash_len ) );
2865 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2866 PSA_ASSERT( psa_hash_finish( &op_aborted,
2867 hash, sizeof( hash ), &hash_len ) );
2868
2869exit:
2870 psa_hash_abort( &op_source );
2871 psa_hash_abort( &op_init );
2872 psa_hash_abort( &op_setup );
2873 psa_hash_abort( &op_finished );
2874 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002875 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002876}
2877/* END_CASE */
2878
2879/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2880void hash_clone_target_state( )
2881{
2882 psa_algorithm_t alg = PSA_ALG_SHA_256;
2883 unsigned char hash[PSA_HASH_MAX_SIZE];
2884 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2885 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2886 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2887 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2888 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2889 size_t hash_len;
2890
2891 PSA_ASSERT( psa_crypto_init( ) );
2892
2893 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2894 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2895 PSA_ASSERT( psa_hash_finish( &op_finished,
2896 hash, sizeof( hash ), &hash_len ) );
2897 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2898 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2899
2900 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2901 PSA_ASSERT( psa_hash_finish( &op_target,
2902 hash, sizeof( hash ), &hash_len ) );
2903
2904 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2905 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2906 PSA_ERROR_BAD_STATE );
2907 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2908 PSA_ERROR_BAD_STATE );
2909
2910exit:
2911 psa_hash_abort( &op_target );
2912 psa_hash_abort( &op_init );
2913 psa_hash_abort( &op_setup );
2914 psa_hash_abort( &op_finished );
2915 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002916 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002917}
2918/* END_CASE */
2919
itayzafrir58028322018-10-25 10:22:01 +03002920/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002921void mac_operation_init( )
2922{
Jaeden Amero252ef282019-02-15 14:05:35 +00002923 const uint8_t input[1] = { 0 };
2924
Jaeden Amero769ce272019-01-04 11:48:03 +00002925 /* Test each valid way of initializing the object, except for `= {0}`, as
2926 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2927 * though it's OK by the C standard. We could test for this, but we'd need
2928 * to supress the Clang warning for the test. */
2929 psa_mac_operation_t func = psa_mac_operation_init( );
2930 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2931 psa_mac_operation_t zero;
2932
2933 memset( &zero, 0, sizeof( zero ) );
2934
Jaeden Amero252ef282019-02-15 14:05:35 +00002935 /* A freshly-initialized MAC operation should not be usable. */
2936 TEST_EQUAL( psa_mac_update( &func,
2937 input, sizeof( input ) ),
2938 PSA_ERROR_BAD_STATE );
2939 TEST_EQUAL( psa_mac_update( &init,
2940 input, sizeof( input ) ),
2941 PSA_ERROR_BAD_STATE );
2942 TEST_EQUAL( psa_mac_update( &zero,
2943 input, sizeof( input ) ),
2944 PSA_ERROR_BAD_STATE );
2945
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002946 /* A default MAC operation should be abortable without error. */
2947 PSA_ASSERT( psa_mac_abort( &func ) );
2948 PSA_ASSERT( psa_mac_abort( &init ) );
2949 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002950}
2951/* END_CASE */
2952
2953/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002954void mac_setup( int key_type_arg,
2955 data_t *key,
2956 int alg_arg,
2957 int expected_status_arg )
2958{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002959 psa_key_type_t key_type = key_type_arg;
2960 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002961 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002962 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002963 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2964#if defined(KNOWN_SUPPORTED_MAC_ALG)
2965 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2966#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002967
Gilles Peskine8817f612018-12-18 00:18:46 +01002968 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002969
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002970 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2971 &operation, &status ) )
2972 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002973 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002974
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002975 /* The operation object should be reusable. */
2976#if defined(KNOWN_SUPPORTED_MAC_ALG)
2977 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2978 smoke_test_key_data,
2979 sizeof( smoke_test_key_data ),
2980 KNOWN_SUPPORTED_MAC_ALG,
2981 &operation, &status ) )
2982 goto exit;
2983 TEST_EQUAL( status, PSA_SUCCESS );
2984#endif
2985
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002986exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002987 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002988}
2989/* END_CASE */
2990
2991/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002992void mac_bad_order( )
2993{
Ronald Cron5425a212020-08-04 14:58:35 +02002994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002995 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2996 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002997 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002998 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2999 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3000 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003002 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3003 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3004 size_t sign_mac_length = 0;
3005 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3006 const uint8_t verify_mac[] = {
3007 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3008 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3009 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3010
3011 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003012 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003013 psa_set_key_algorithm( &attributes, alg );
3014 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003015
Ronald Cron5425a212020-08-04 14:58:35 +02003016 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3017 &key ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003018
Jaeden Amero252ef282019-02-15 14:05:35 +00003019 /* Call update without calling setup beforehand. */
3020 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3021 PSA_ERROR_BAD_STATE );
3022 PSA_ASSERT( psa_mac_abort( &operation ) );
3023
3024 /* Call sign finish without calling setup beforehand. */
3025 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3026 &sign_mac_length),
3027 PSA_ERROR_BAD_STATE );
3028 PSA_ASSERT( psa_mac_abort( &operation ) );
3029
3030 /* Call verify finish without calling setup beforehand. */
3031 TEST_EQUAL( psa_mac_verify_finish( &operation,
3032 verify_mac, sizeof( verify_mac ) ),
3033 PSA_ERROR_BAD_STATE );
3034 PSA_ASSERT( psa_mac_abort( &operation ) );
3035
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003036 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003037 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
3038 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003039 PSA_ERROR_BAD_STATE );
3040 PSA_ASSERT( psa_mac_abort( &operation ) );
3041
Jaeden Amero252ef282019-02-15 14:05:35 +00003042 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003043 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003044 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3045 PSA_ASSERT( psa_mac_sign_finish( &operation,
3046 sign_mac, sizeof( sign_mac ),
3047 &sign_mac_length ) );
3048 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3049 PSA_ERROR_BAD_STATE );
3050 PSA_ASSERT( psa_mac_abort( &operation ) );
3051
3052 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003053 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003054 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3055 PSA_ASSERT( psa_mac_verify_finish( &operation,
3056 verify_mac, sizeof( verify_mac ) ) );
3057 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3058 PSA_ERROR_BAD_STATE );
3059 PSA_ASSERT( psa_mac_abort( &operation ) );
3060
3061 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003062 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003063 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3064 PSA_ASSERT( psa_mac_sign_finish( &operation,
3065 sign_mac, sizeof( sign_mac ),
3066 &sign_mac_length ) );
3067 TEST_EQUAL( psa_mac_sign_finish( &operation,
3068 sign_mac, sizeof( sign_mac ),
3069 &sign_mac_length ),
3070 PSA_ERROR_BAD_STATE );
3071 PSA_ASSERT( psa_mac_abort( &operation ) );
3072
3073 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003074 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003075 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3076 PSA_ASSERT( psa_mac_verify_finish( &operation,
3077 verify_mac, sizeof( verify_mac ) ) );
3078 TEST_EQUAL( psa_mac_verify_finish( &operation,
3079 verify_mac, sizeof( verify_mac ) ),
3080 PSA_ERROR_BAD_STATE );
3081 PSA_ASSERT( psa_mac_abort( &operation ) );
3082
3083 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003084 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003085 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3086 TEST_EQUAL( psa_mac_verify_finish( &operation,
3087 verify_mac, sizeof( verify_mac ) ),
3088 PSA_ERROR_BAD_STATE );
3089 PSA_ASSERT( psa_mac_abort( &operation ) );
3090
3091 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003092 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003093 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3094 TEST_EQUAL( psa_mac_sign_finish( &operation,
3095 sign_mac, sizeof( sign_mac ),
3096 &sign_mac_length ),
3097 PSA_ERROR_BAD_STATE );
3098 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003099
Ronald Cron5425a212020-08-04 14:58:35 +02003100 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003101
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003102exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003103 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003104}
3105/* END_CASE */
3106
3107/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003108void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003109 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003110 int alg_arg,
3111 data_t *input,
3112 data_t *expected_mac )
3113{
Ronald Cron5425a212020-08-04 14:58:35 +02003114 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003115 psa_key_type_t key_type = key_type_arg;
3116 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003117 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003119 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003120 size_t mac_buffer_size =
Ronald Cron5425a212020-08-04 14:58:35 +02003121 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003122 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003123 const size_t output_sizes_to_test[] = {
3124 0,
3125 1,
3126 expected_mac->len - 1,
3127 expected_mac->len,
3128 expected_mac->len + 1,
3129 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003130
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003131 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
Gilles Peskine3d404d62020-08-25 23:47:36 +02003132 /* We expect PSA_MAC_FINAL_SIZE to be exact. */
3133 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003134
Gilles Peskine8817f612018-12-18 00:18:46 +01003135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003136
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003138 psa_set_key_algorithm( &attributes, alg );
3139 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003140
Ronald Cron5425a212020-08-04 14:58:35 +02003141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3142 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003143
Gilles Peskine8b356b52020-08-25 23:44:59 +02003144 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3145 {
3146 const size_t output_size = output_sizes_to_test[i];
3147 psa_status_t expected_status =
3148 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3149 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003150
Chris Jones9634bb12021-01-20 15:56:42 +00003151 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003152 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003153
Gilles Peskine8b356b52020-08-25 23:44:59 +02003154 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003155 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003156 PSA_ASSERT( psa_mac_update( &operation,
3157 input->x, input->len ) );
3158 TEST_EQUAL( psa_mac_sign_finish( &operation,
3159 actual_mac, output_size,
3160 &mac_length ),
3161 expected_status );
3162 PSA_ASSERT( psa_mac_abort( &operation ) );
3163
3164 if( expected_status == PSA_SUCCESS )
3165 {
3166 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3167 actual_mac, mac_length );
3168 }
3169 mbedtls_free( actual_mac );
3170 actual_mac = NULL;
3171 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003172
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003173exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003174 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003176 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003177 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003178}
3179/* END_CASE */
3180
3181/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003182void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003183 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003184 int alg_arg,
3185 data_t *input,
3186 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003187{
Ronald Cron5425a212020-08-04 14:58:35 +02003188 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003189 psa_key_type_t key_type = key_type_arg;
3190 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003191 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003193 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003194
Gilles Peskine69c12672018-06-28 00:07:19 +02003195 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3196
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003198
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003199 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003200 psa_set_key_algorithm( &attributes, alg );
3201 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003202
Ronald Cron5425a212020-08-04 14:58:35 +02003203 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3204 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003205
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003206 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003207 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003208 PSA_ASSERT( psa_mac_update( &operation,
3209 input->x, input->len ) );
3210 PSA_ASSERT( psa_mac_verify_finish( &operation,
3211 expected_mac->x,
3212 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003213
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003214 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003215 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003216 PSA_ASSERT( psa_mac_update( &operation,
3217 input->x, input->len ) );
3218 TEST_EQUAL( psa_mac_verify_finish( &operation,
3219 expected_mac->x,
3220 expected_mac->len - 1 ),
3221 PSA_ERROR_INVALID_SIGNATURE );
3222
3223 /* Test a MAC that's too long. */
3224 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3225 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003226 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003227 PSA_ASSERT( psa_mac_update( &operation,
3228 input->x, input->len ) );
3229 TEST_EQUAL( psa_mac_verify_finish( &operation,
3230 perturbed_mac,
3231 expected_mac->len + 1 ),
3232 PSA_ERROR_INVALID_SIGNATURE );
3233
3234 /* Test changing one byte. */
3235 for( size_t i = 0; i < expected_mac->len; i++ )
3236 {
Chris Jones9634bb12021-01-20 15:56:42 +00003237 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003238 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003239 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003240 PSA_ASSERT( psa_mac_update( &operation,
3241 input->x, input->len ) );
3242 TEST_EQUAL( psa_mac_verify_finish( &operation,
3243 perturbed_mac,
3244 expected_mac->len ),
3245 PSA_ERROR_INVALID_SIGNATURE );
3246 perturbed_mac[i] ^= 1;
3247 }
3248
Gilles Peskine8c9def32018-02-08 10:02:12 +01003249exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003250 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003251 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003252 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003253 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003254}
3255/* END_CASE */
3256
3257/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003258void cipher_operation_init( )
3259{
Jaeden Ameroab439972019-02-15 14:12:05 +00003260 const uint8_t input[1] = { 0 };
3261 unsigned char output[1] = { 0 };
3262 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003263 /* Test each valid way of initializing the object, except for `= {0}`, as
3264 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3265 * though it's OK by the C standard. We could test for this, but we'd need
3266 * to supress the Clang warning for the test. */
3267 psa_cipher_operation_t func = psa_cipher_operation_init( );
3268 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3269 psa_cipher_operation_t zero;
3270
3271 memset( &zero, 0, sizeof( zero ) );
3272
Jaeden Ameroab439972019-02-15 14:12:05 +00003273 /* A freshly-initialized cipher operation should not be usable. */
3274 TEST_EQUAL( psa_cipher_update( &func,
3275 input, sizeof( input ),
3276 output, sizeof( output ),
3277 &output_length ),
3278 PSA_ERROR_BAD_STATE );
3279 TEST_EQUAL( psa_cipher_update( &init,
3280 input, sizeof( input ),
3281 output, sizeof( output ),
3282 &output_length ),
3283 PSA_ERROR_BAD_STATE );
3284 TEST_EQUAL( psa_cipher_update( &zero,
3285 input, sizeof( input ),
3286 output, sizeof( output ),
3287 &output_length ),
3288 PSA_ERROR_BAD_STATE );
3289
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003290 /* A default cipher operation should be abortable without error. */
3291 PSA_ASSERT( psa_cipher_abort( &func ) );
3292 PSA_ASSERT( psa_cipher_abort( &init ) );
3293 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003294}
3295/* END_CASE */
3296
3297/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003298void cipher_setup( int key_type_arg,
3299 data_t *key,
3300 int alg_arg,
3301 int expected_status_arg )
3302{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003303 psa_key_type_t key_type = key_type_arg;
3304 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003305 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003306 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003307 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003308#if defined(KNOWN_SUPPORTED_MAC_ALG)
3309 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3310#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003311
Gilles Peskine8817f612018-12-18 00:18:46 +01003312 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003313
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003314 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3315 &operation, &status ) )
3316 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003317 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003318
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003319 /* The operation object should be reusable. */
3320#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3321 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3322 smoke_test_key_data,
3323 sizeof( smoke_test_key_data ),
3324 KNOWN_SUPPORTED_CIPHER_ALG,
3325 &operation, &status ) )
3326 goto exit;
3327 TEST_EQUAL( status, PSA_SUCCESS );
3328#endif
3329
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003330exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003331 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003332 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003333}
3334/* END_CASE */
3335
3336/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003337void cipher_bad_order( )
3338{
Ronald Cron5425a212020-08-04 14:58:35 +02003339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003340 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3341 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003343 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3344 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003345 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003346 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3347 0xaa, 0xaa, 0xaa, 0xaa };
3348 const uint8_t text[] = {
3349 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3350 0xbb, 0xbb, 0xbb, 0xbb };
3351 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3352 size_t length = 0;
3353
3354 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003355 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3356 psa_set_key_algorithm( &attributes, alg );
3357 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003358 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3359 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003360
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003361 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003362 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3363 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003364 PSA_ERROR_BAD_STATE );
3365 PSA_ASSERT( psa_cipher_abort( &operation ) );
3366
3367 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003368 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3369 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003370 PSA_ERROR_BAD_STATE );
3371 PSA_ASSERT( psa_cipher_abort( &operation ) );
3372
Jaeden Ameroab439972019-02-15 14:12:05 +00003373 /* Generate an IV without calling setup beforehand. */
3374 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3375 buffer, sizeof( buffer ),
3376 &length ),
3377 PSA_ERROR_BAD_STATE );
3378 PSA_ASSERT( psa_cipher_abort( &operation ) );
3379
3380 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003381 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003382 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3383 buffer, sizeof( buffer ),
3384 &length ) );
3385 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3386 buffer, sizeof( buffer ),
3387 &length ),
3388 PSA_ERROR_BAD_STATE );
3389 PSA_ASSERT( psa_cipher_abort( &operation ) );
3390
3391 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003392 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003393 PSA_ASSERT( psa_cipher_set_iv( &operation,
3394 iv, sizeof( iv ) ) );
3395 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3396 buffer, sizeof( buffer ),
3397 &length ),
3398 PSA_ERROR_BAD_STATE );
3399 PSA_ASSERT( psa_cipher_abort( &operation ) );
3400
3401 /* Set an IV without calling setup beforehand. */
3402 TEST_EQUAL( psa_cipher_set_iv( &operation,
3403 iv, sizeof( iv ) ),
3404 PSA_ERROR_BAD_STATE );
3405 PSA_ASSERT( psa_cipher_abort( &operation ) );
3406
3407 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003408 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003409 PSA_ASSERT( psa_cipher_set_iv( &operation,
3410 iv, sizeof( iv ) ) );
3411 TEST_EQUAL( psa_cipher_set_iv( &operation,
3412 iv, sizeof( iv ) ),
3413 PSA_ERROR_BAD_STATE );
3414 PSA_ASSERT( psa_cipher_abort( &operation ) );
3415
3416 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003417 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003418 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3419 buffer, sizeof( buffer ),
3420 &length ) );
3421 TEST_EQUAL( psa_cipher_set_iv( &operation,
3422 iv, sizeof( iv ) ),
3423 PSA_ERROR_BAD_STATE );
3424 PSA_ASSERT( psa_cipher_abort( &operation ) );
3425
3426 /* Call update without calling setup beforehand. */
3427 TEST_EQUAL( psa_cipher_update( &operation,
3428 text, sizeof( text ),
3429 buffer, sizeof( buffer ),
3430 &length ),
3431 PSA_ERROR_BAD_STATE );
3432 PSA_ASSERT( psa_cipher_abort( &operation ) );
3433
3434 /* Call update without an IV where an IV is required. */
3435 TEST_EQUAL( psa_cipher_update( &operation,
3436 text, sizeof( text ),
3437 buffer, sizeof( buffer ),
3438 &length ),
3439 PSA_ERROR_BAD_STATE );
3440 PSA_ASSERT( psa_cipher_abort( &operation ) );
3441
3442 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003443 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003444 PSA_ASSERT( psa_cipher_set_iv( &operation,
3445 iv, sizeof( iv ) ) );
3446 PSA_ASSERT( psa_cipher_finish( &operation,
3447 buffer, sizeof( buffer ), &length ) );
3448 TEST_EQUAL( psa_cipher_update( &operation,
3449 text, sizeof( text ),
3450 buffer, sizeof( buffer ),
3451 &length ),
3452 PSA_ERROR_BAD_STATE );
3453 PSA_ASSERT( psa_cipher_abort( &operation ) );
3454
3455 /* Call finish without calling setup beforehand. */
3456 TEST_EQUAL( psa_cipher_finish( &operation,
3457 buffer, sizeof( buffer ), &length ),
3458 PSA_ERROR_BAD_STATE );
3459 PSA_ASSERT( psa_cipher_abort( &operation ) );
3460
3461 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003462 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003463 /* Not calling update means we are encrypting an empty buffer, which is OK
3464 * for cipher modes with padding. */
3465 TEST_EQUAL( psa_cipher_finish( &operation,
3466 buffer, sizeof( buffer ), &length ),
3467 PSA_ERROR_BAD_STATE );
3468 PSA_ASSERT( psa_cipher_abort( &operation ) );
3469
3470 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003471 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003472 PSA_ASSERT( psa_cipher_set_iv( &operation,
3473 iv, sizeof( iv ) ) );
3474 PSA_ASSERT( psa_cipher_finish( &operation,
3475 buffer, sizeof( buffer ), &length ) );
3476 TEST_EQUAL( psa_cipher_finish( &operation,
3477 buffer, sizeof( buffer ), &length ),
3478 PSA_ERROR_BAD_STATE );
3479 PSA_ASSERT( psa_cipher_abort( &operation ) );
3480
Ronald Cron5425a212020-08-04 14:58:35 +02003481 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003482
Jaeden Ameroab439972019-02-15 14:12:05 +00003483exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003484 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003485 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003486}
3487/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003488
Gilles Peskine50e586b2018-06-08 14:28:46 +02003489/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003490void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003491 data_t *key_data, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003492 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003493 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003494{
Ronald Cron5425a212020-08-04 14:58:35 +02003495 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003496 psa_status_t status;
3497 psa_key_type_t key_type = key_type_arg;
3498 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003499 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003500 unsigned char *output = NULL;
3501 size_t output_buffer_size = 0;
3502 size_t function_output_length = 0;
3503 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003504 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003505 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003506
Gilles Peskine8817f612018-12-18 00:18:46 +01003507 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003508
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003509 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3510 psa_set_key_algorithm( &attributes, alg );
3511 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003512
Ronald Cron5425a212020-08-04 14:58:35 +02003513 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3514 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003515
Ronald Cron5425a212020-08-04 14:58:35 +02003516 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003517
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003518 if( iv->len > 0 )
3519 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003520 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003521 }
3522
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003523 output_buffer_size = ( (size_t) input->len +
3524 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003525 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003526
Gilles Peskine8817f612018-12-18 00:18:46 +01003527 PSA_ASSERT( psa_cipher_update( &operation,
3528 input->x, input->len,
3529 output, output_buffer_size,
3530 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003531 total_output_length += function_output_length;
3532 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003533 output + total_output_length,
3534 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003535 &function_output_length );
3536 total_output_length += function_output_length;
3537
Gilles Peskinefe11b722018-12-18 00:24:04 +01003538 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003539 if( expected_status == PSA_SUCCESS )
3540 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003541 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003542 ASSERT_COMPARE( expected_output->x, expected_output->len,
3543 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003544 }
3545
3546exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003547 psa_cipher_abort( &operation );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003548 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003549 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003550 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003551}
3552/* END_CASE */
3553
3554/* BEGIN_CASE */
3555void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003556 data_t *key_data, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003557 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003558 int first_part_size_arg,
3559 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003560 data_t *expected_output )
3561{
Ronald Cron5425a212020-08-04 14:58:35 +02003562 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003563 psa_key_type_t key_type = key_type_arg;
3564 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003565 size_t first_part_size = first_part_size_arg;
3566 size_t output1_length = output1_length_arg;
3567 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003568 unsigned char *output = NULL;
3569 size_t output_buffer_size = 0;
3570 size_t function_output_length = 0;
3571 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003572 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003573 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003574
Gilles Peskine8817f612018-12-18 00:18:46 +01003575 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003576
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003577 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3578 psa_set_key_algorithm( &attributes, alg );
3579 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003580
Ronald Cron5425a212020-08-04 14:58:35 +02003581 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3582 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003583
Ronald Cron5425a212020-08-04 14:58:35 +02003584 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003585
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003586 if( iv->len > 0 )
3587 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003588 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003589 }
3590
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003591 output_buffer_size = ( (size_t) input->len +
3592 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003593 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003594
Gilles Peskinee0866522019-02-19 19:44:00 +01003595 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003596 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3597 output, output_buffer_size,
3598 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003599 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003600 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003601 PSA_ASSERT( psa_cipher_update( &operation,
3602 input->x + first_part_size,
3603 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003604 output + total_output_length,
3605 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003606 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003607 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003608 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003609 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003610 output + total_output_length,
3611 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003612 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003613 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003614 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003615
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003616 ASSERT_COMPARE( expected_output->x, expected_output->len,
3617 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003618
3619exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003620 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003621 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003622 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003623 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003624}
3625/* END_CASE */
3626
3627/* BEGIN_CASE */
3628void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003629 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003630 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003631 int first_part_size_arg,
3632 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003633 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003634{
Ronald Cron5425a212020-08-04 14:58:35 +02003635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003636 psa_key_type_t key_type = key_type_arg;
3637 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003638 size_t first_part_size = first_part_size_arg;
3639 size_t output1_length = output1_length_arg;
3640 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003641 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003642 size_t output_buffer_size = 0;
3643 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003644 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003645 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003647
Gilles Peskine8817f612018-12-18 00:18:46 +01003648 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003649
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003650 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3651 psa_set_key_algorithm( &attributes, alg );
3652 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003653
Ronald Cron5425a212020-08-04 14:58:35 +02003654 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3655 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003656
Ronald Cron5425a212020-08-04 14:58:35 +02003657 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003658
Steven Cooreman177deba2020-09-07 17:14:14 +02003659 if( iv->len > 0 )
3660 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003661 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003662 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003663
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003664 output_buffer_size = ( (size_t) input->len +
3665 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003666 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003667
Gilles Peskinee0866522019-02-19 19:44:00 +01003668 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003669 PSA_ASSERT( psa_cipher_update( &operation,
3670 input->x, first_part_size,
3671 output, output_buffer_size,
3672 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003673 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003674 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003675 PSA_ASSERT( psa_cipher_update( &operation,
3676 input->x + first_part_size,
3677 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003678 output + total_output_length,
3679 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003680 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003681 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003682 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003683 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003684 output + total_output_length,
3685 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003686 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003687 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003688 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003689
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003690 ASSERT_COMPARE( expected_output->x, expected_output->len,
3691 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003692
3693exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003694 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003695 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003696 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003697 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003698}
3699/* END_CASE */
3700
Gilles Peskine50e586b2018-06-08 14:28:46 +02003701/* BEGIN_CASE */
3702void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003703 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003704 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003705 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003706{
Ronald Cron5425a212020-08-04 14:58:35 +02003707 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003708 psa_status_t status;
3709 psa_key_type_t key_type = key_type_arg;
3710 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003711 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003712 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003713 size_t output_buffer_size = 0;
3714 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003715 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003716 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003718
Gilles Peskine8817f612018-12-18 00:18:46 +01003719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003720
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003721 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3722 psa_set_key_algorithm( &attributes, alg );
3723 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003724
Ronald Cron5425a212020-08-04 14:58:35 +02003725 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3726 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003727
Ronald Cron5425a212020-08-04 14:58:35 +02003728 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003729
Steven Cooreman177deba2020-09-07 17:14:14 +02003730 if( iv->len > 0 )
3731 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003732 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003733 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003734
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003735 output_buffer_size = ( (size_t) input->len +
3736 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003737 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003738
Gilles Peskine8817f612018-12-18 00:18:46 +01003739 PSA_ASSERT( psa_cipher_update( &operation,
3740 input->x, input->len,
3741 output, output_buffer_size,
3742 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003743 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003744 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003745 output + total_output_length,
3746 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003747 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003748 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003749 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003750
3751 if( expected_status == PSA_SUCCESS )
3752 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003753 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003754 ASSERT_COMPARE( expected_output->x, expected_output->len,
3755 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003756 }
3757
Gilles Peskine50e586b2018-06-08 14:28:46 +02003758exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003759 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003760 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003761 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003762 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003763}
3764/* END_CASE */
3765
Gilles Peskine50e586b2018-06-08 14:28:46 +02003766/* BEGIN_CASE */
3767void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003768 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003769 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003770{
Ronald Cron5425a212020-08-04 14:58:35 +02003771 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003772 psa_key_type_t key_type = key_type_arg;
3773 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003774 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003775 size_t iv_size = 16;
3776 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003777 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003778 size_t output1_size = 0;
3779 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003780 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003781 size_t output2_size = 0;
3782 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003783 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003784 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3785 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003786 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003787
Gilles Peskine8817f612018-12-18 00:18:46 +01003788 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003789
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003790 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3791 psa_set_key_algorithm( &attributes, alg );
3792 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003793
Ronald Cron5425a212020-08-04 14:58:35 +02003794 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3795 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003796
Ronald Cron5425a212020-08-04 14:58:35 +02003797 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3798 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003799
Steven Cooreman177deba2020-09-07 17:14:14 +02003800 if( alg != PSA_ALG_ECB_NO_PADDING )
3801 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003802 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3803 iv, iv_size,
3804 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003805 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003806 output1_size = ( (size_t) input->len +
3807 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003808 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003809
Gilles Peskine8817f612018-12-18 00:18:46 +01003810 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3811 output1, output1_size,
3812 &output1_length ) );
3813 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003814 output1 + output1_length,
3815 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003816 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003817
Gilles Peskine048b7f02018-06-08 14:20:49 +02003818 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003819
Gilles Peskine8817f612018-12-18 00:18:46 +01003820 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003821
3822 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003823 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003824
Steven Cooreman177deba2020-09-07 17:14:14 +02003825 if( iv_length > 0 )
3826 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003827 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3828 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003829 }
3830
Gilles Peskine8817f612018-12-18 00:18:46 +01003831 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3832 output2, output2_size,
3833 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003834 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003835 PSA_ASSERT( psa_cipher_finish( &operation2,
3836 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003837 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003838 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003839
Gilles Peskine048b7f02018-06-08 14:20:49 +02003840 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003841
Gilles Peskine8817f612018-12-18 00:18:46 +01003842 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003843
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003844 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003845
3846exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003847 psa_cipher_abort( &operation1 );
3848 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003849 mbedtls_free( output1 );
3850 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003851 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003852 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003853}
3854/* END_CASE */
3855
3856/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003857void cipher_verify_output_multipart( int alg_arg,
3858 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003859 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003860 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003861 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003862{
Ronald Cron5425a212020-08-04 14:58:35 +02003863 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003864 psa_key_type_t key_type = key_type_arg;
3865 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003866 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003867 unsigned char iv[16] = {0};
3868 size_t iv_size = 16;
3869 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003870 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003871 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003872 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003873 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003874 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003875 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003876 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003877 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3878 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003880
Gilles Peskine8817f612018-12-18 00:18:46 +01003881 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003882
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3884 psa_set_key_algorithm( &attributes, alg );
3885 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003886
Ronald Cron5425a212020-08-04 14:58:35 +02003887 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3888 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003889
Ronald Cron5425a212020-08-04 14:58:35 +02003890 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3891 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003892
Steven Cooreman177deba2020-09-07 17:14:14 +02003893 if( alg != PSA_ALG_ECB_NO_PADDING )
3894 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003895 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3896 iv, iv_size,
3897 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003898 }
3899
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003900 output1_buffer_size = ( (size_t) input->len +
3901 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003902 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003903
Gilles Peskinee0866522019-02-19 19:44:00 +01003904 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003905
Gilles Peskine8817f612018-12-18 00:18:46 +01003906 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3907 output1, output1_buffer_size,
3908 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003909 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003910
Gilles Peskine8817f612018-12-18 00:18:46 +01003911 PSA_ASSERT( psa_cipher_update( &operation1,
3912 input->x + first_part_size,
3913 input->len - first_part_size,
3914 output1, output1_buffer_size,
3915 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003916 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003917
Gilles Peskine8817f612018-12-18 00:18:46 +01003918 PSA_ASSERT( psa_cipher_finish( &operation1,
3919 output1 + output1_length,
3920 output1_buffer_size - output1_length,
3921 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003922 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003923
Gilles Peskine8817f612018-12-18 00:18:46 +01003924 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003925
Gilles Peskine048b7f02018-06-08 14:20:49 +02003926 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003927 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003928
Steven Cooreman177deba2020-09-07 17:14:14 +02003929 if( iv_length > 0 )
3930 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003931 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3932 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003933 }
Moran Pekerded84402018-06-06 16:36:50 +03003934
Gilles Peskine8817f612018-12-18 00:18:46 +01003935 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3936 output2, output2_buffer_size,
3937 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003938 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003939
Gilles Peskine8817f612018-12-18 00:18:46 +01003940 PSA_ASSERT( psa_cipher_update( &operation2,
3941 output1 + first_part_size,
3942 output1_length - first_part_size,
3943 output2, output2_buffer_size,
3944 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003945 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003946
Gilles Peskine8817f612018-12-18 00:18:46 +01003947 PSA_ASSERT( psa_cipher_finish( &operation2,
3948 output2 + output2_length,
3949 output2_buffer_size - output2_length,
3950 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003951 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003952
Gilles Peskine8817f612018-12-18 00:18:46 +01003953 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003954
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003955 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003956
3957exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003958 psa_cipher_abort( &operation1 );
3959 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003960 mbedtls_free( output1 );
3961 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003962 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003963 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003964}
3965/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003966
Gilles Peskine20035e32018-02-03 22:44:14 +01003967/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003968void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003969 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003970 data_t *nonce,
3971 data_t *additional_data,
3972 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003973 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003974{
Ronald Cron5425a212020-08-04 14:58:35 +02003975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003976 psa_key_type_t key_type = key_type_arg;
3977 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003978 unsigned char *output_data = NULL;
3979 size_t output_size = 0;
3980 size_t output_length = 0;
3981 unsigned char *output_data2 = NULL;
3982 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003983 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003984 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003986
Gilles Peskine4abf7412018-06-18 16:35:34 +02003987 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003988 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3989 * should be exact. */
3990 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3991 TEST_EQUAL( output_size,
3992 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003993 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003994
Gilles Peskine8817f612018-12-18 00:18:46 +01003995 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003996
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003997 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3998 psa_set_key_algorithm( &attributes, alg );
3999 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004000
Gilles Peskine049c7532019-05-15 20:22:09 +02004001 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004002 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004003
Ronald Cron5425a212020-08-04 14:58:35 +02004004 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004005 nonce->x, nonce->len,
4006 additional_data->x,
4007 additional_data->len,
4008 input_data->x, input_data->len,
4009 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004010 &output_length ),
4011 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004012
4013 if( PSA_SUCCESS == expected_result )
4014 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004015 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004016
Gilles Peskine003a4a92019-05-14 16:09:40 +02004017 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4018 * should be exact. */
4019 TEST_EQUAL( input_data->len,
4020 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
4021
Ronald Cron5425a212020-08-04 14:58:35 +02004022 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004023 nonce->x, nonce->len,
4024 additional_data->x,
4025 additional_data->len,
4026 output_data, output_length,
4027 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004028 &output_length2 ),
4029 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004030
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004031 ASSERT_COMPARE( input_data->x, input_data->len,
4032 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004033 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004034
Gilles Peskinea1cac842018-06-11 19:33:02 +02004035exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004036 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004037 mbedtls_free( output_data );
4038 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004039 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004040}
4041/* END_CASE */
4042
4043/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004044void aead_encrypt( int key_type_arg, data_t *key_data,
4045 int alg_arg,
4046 data_t *nonce,
4047 data_t *additional_data,
4048 data_t *input_data,
4049 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004050{
Ronald Cron5425a212020-08-04 14:58:35 +02004051 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004052 psa_key_type_t key_type = key_type_arg;
4053 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004054 unsigned char *output_data = NULL;
4055 size_t output_size = 0;
4056 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004057 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004058 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004059
Gilles Peskine4abf7412018-06-18 16:35:34 +02004060 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004061 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4062 * should be exact. */
4063 TEST_EQUAL( output_size,
4064 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004065 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004066
Gilles Peskine8817f612018-12-18 00:18:46 +01004067 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004068
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004069 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4070 psa_set_key_algorithm( &attributes, alg );
4071 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004072
Gilles Peskine049c7532019-05-15 20:22:09 +02004073 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004074 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004075
Ronald Cron5425a212020-08-04 14:58:35 +02004076 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004077 nonce->x, nonce->len,
4078 additional_data->x, additional_data->len,
4079 input_data->x, input_data->len,
4080 output_data, output_size,
4081 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004082
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004083 ASSERT_COMPARE( expected_result->x, expected_result->len,
4084 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004085
Gilles Peskinea1cac842018-06-11 19:33:02 +02004086exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004087 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004088 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004089 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004090}
4091/* END_CASE */
4092
4093/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004094void aead_decrypt( int key_type_arg, data_t *key_data,
4095 int alg_arg,
4096 data_t *nonce,
4097 data_t *additional_data,
4098 data_t *input_data,
4099 data_t *expected_data,
4100 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004101{
Ronald Cron5425a212020-08-04 14:58:35 +02004102 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004103 psa_key_type_t key_type = key_type_arg;
4104 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004105 unsigned char *output_data = NULL;
4106 size_t output_size = 0;
4107 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004108 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004110 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004111
Gilles Peskine003a4a92019-05-14 16:09:40 +02004112 output_size = input_data->len - tag_length;
4113 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4114 * should be exact. */
4115 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4116 TEST_EQUAL( output_size,
4117 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004118 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004119
Gilles Peskine8817f612018-12-18 00:18:46 +01004120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004122 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4123 psa_set_key_algorithm( &attributes, alg );
4124 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004125
Gilles Peskine049c7532019-05-15 20:22:09 +02004126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004127 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004128
Ronald Cron5425a212020-08-04 14:58:35 +02004129 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004130 nonce->x, nonce->len,
4131 additional_data->x,
4132 additional_data->len,
4133 input_data->x, input_data->len,
4134 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004135 &output_length ),
4136 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004137
Gilles Peskine2d277862018-06-18 15:41:12 +02004138 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004139 ASSERT_COMPARE( expected_data->x, expected_data->len,
4140 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004141
Gilles Peskinea1cac842018-06-11 19:33:02 +02004142exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004143 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004144 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004145 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004146}
4147/* END_CASE */
4148
4149/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004150void signature_size( int type_arg,
4151 int bits,
4152 int alg_arg,
4153 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004154{
4155 psa_key_type_t type = type_arg;
4156 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004157 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004158
Gilles Peskinefe11b722018-12-18 00:24:04 +01004159 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004160#if defined(MBEDTLS_TEST_DEPRECATED)
4161 TEST_EQUAL( actual_size,
4162 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4163#endif /* MBEDTLS_TEST_DEPRECATED */
4164
Gilles Peskinee59236f2018-01-27 23:32:46 +01004165exit:
4166 ;
4167}
4168/* END_CASE */
4169
4170/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004171void sign_deterministic( int key_type_arg, data_t *key_data,
4172 int alg_arg, data_t *input_data,
4173 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004174{
Ronald Cron5425a212020-08-04 14:58:35 +02004175 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004176 psa_key_type_t key_type = key_type_arg;
4177 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004178 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004179 unsigned char *signature = NULL;
4180 size_t signature_size;
4181 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004183
Gilles Peskine8817f612018-12-18 00:18:46 +01004184 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004185
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004187 psa_set_key_algorithm( &attributes, alg );
4188 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004189
Gilles Peskine049c7532019-05-15 20:22:09 +02004190 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004191 &key ) );
4192 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004193 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004194
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004195 /* Allocate a buffer which has the size advertized by the
4196 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004197 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004198 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004199 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004200 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004201 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004202
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004203 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004204 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004205 input_data->x, input_data->len,
4206 signature, signature_size,
4207 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004208 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004209 ASSERT_COMPARE( output_data->x, output_data->len,
4210 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004211
Gilles Peskine0627f982019-11-26 19:12:16 +01004212#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004213 memset( signature, 0, signature_size );
4214 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004215 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004216 input_data->x, input_data->len,
4217 signature, signature_size,
4218 &signature_length ) );
4219 ASSERT_COMPARE( output_data->x, output_data->len,
4220 signature, signature_length );
4221#endif /* MBEDTLS_TEST_DEPRECATED */
4222
Gilles Peskine20035e32018-02-03 22:44:14 +01004223exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004224 /*
4225 * Key attributes may have been returned by psa_get_key_attributes()
4226 * thus reset them as required.
4227 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004228 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004229
Ronald Cron5425a212020-08-04 14:58:35 +02004230 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004231 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004232 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004233}
4234/* END_CASE */
4235
4236/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004237void sign_fail( int key_type_arg, data_t *key_data,
4238 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004239 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004240{
Ronald Cron5425a212020-08-04 14:58:35 +02004241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004242 psa_key_type_t key_type = key_type_arg;
4243 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004244 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004245 psa_status_t actual_status;
4246 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004247 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004248 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004250
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004251 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004252
Gilles Peskine8817f612018-12-18 00:18:46 +01004253 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004254
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004255 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004256 psa_set_key_algorithm( &attributes, alg );
4257 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004258
Gilles Peskine049c7532019-05-15 20:22:09 +02004259 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004260 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004261
Ronald Cron5425a212020-08-04 14:58:35 +02004262 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004263 input_data->x, input_data->len,
4264 signature, signature_size,
4265 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004266 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004267 /* The value of *signature_length is unspecified on error, but
4268 * whatever it is, it should be less than signature_size, so that
4269 * if the caller tries to read *signature_length bytes without
4270 * checking the error code then they don't overflow a buffer. */
4271 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004272
Gilles Peskine895242b2019-11-29 12:15:40 +01004273#if defined(MBEDTLS_TEST_DEPRECATED)
4274 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004275 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004276 input_data->x, input_data->len,
4277 signature, signature_size,
4278 &signature_length ),
4279 expected_status );
4280 TEST_ASSERT( signature_length <= signature_size );
4281#endif /* MBEDTLS_TEST_DEPRECATED */
4282
Gilles Peskine20035e32018-02-03 22:44:14 +01004283exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004284 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004285 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004286 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004287 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004288}
4289/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004290
4291/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004292void sign_verify( int key_type_arg, data_t *key_data,
4293 int alg_arg, data_t *input_data )
4294{
Ronald Cron5425a212020-08-04 14:58:35 +02004295 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004296 psa_key_type_t key_type = key_type_arg;
4297 psa_algorithm_t alg = alg_arg;
4298 size_t key_bits;
4299 unsigned char *signature = NULL;
4300 size_t signature_size;
4301 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004302 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004303
Gilles Peskine8817f612018-12-18 00:18:46 +01004304 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004305
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004306 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004307 psa_set_key_algorithm( &attributes, alg );
4308 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004309
Gilles Peskine049c7532019-05-15 20:22:09 +02004310 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004311 &key ) );
4312 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004313 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004314
4315 /* Allocate a buffer which has the size advertized by the
4316 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004317 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004318 key_bits, alg );
4319 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004320 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004321 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004322
4323 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004324 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004325 input_data->x, input_data->len,
4326 signature, signature_size,
4327 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004328 /* Check that the signature length looks sensible. */
4329 TEST_ASSERT( signature_length <= signature_size );
4330 TEST_ASSERT( signature_length > 0 );
4331
4332 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004333 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004334 input_data->x, input_data->len,
4335 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004336
4337 if( input_data->len != 0 )
4338 {
4339 /* Flip a bit in the input and verify that the signature is now
4340 * detected as invalid. Flip a bit at the beginning, not at the end,
4341 * because ECDSA may ignore the last few bits of the input. */
4342 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004343 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004344 input_data->x, input_data->len,
4345 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004346 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004347 }
4348
4349exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004350 /*
4351 * Key attributes may have been returned by psa_get_key_attributes()
4352 * thus reset them as required.
4353 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004354 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004355
Ronald Cron5425a212020-08-04 14:58:35 +02004356 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004357 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004358 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004359}
4360/* END_CASE */
4361
4362/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004363void asymmetric_verify( int key_type_arg, data_t *key_data,
4364 int alg_arg, data_t *hash_data,
4365 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004366{
Ronald Cron5425a212020-08-04 14:58:35 +02004367 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004368 psa_key_type_t key_type = key_type_arg;
4369 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004371
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004372 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004373
Gilles Peskine8817f612018-12-18 00:18:46 +01004374 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004375
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004376 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004377 psa_set_key_algorithm( &attributes, alg );
4378 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004379
Gilles Peskine049c7532019-05-15 20:22:09 +02004380 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004381 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004382
Ronald Cron5425a212020-08-04 14:58:35 +02004383 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004384 hash_data->x, hash_data->len,
4385 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004386
4387#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004388 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004389 hash_data->x, hash_data->len,
4390 signature_data->x,
4391 signature_data->len ) );
4392
4393#endif /* MBEDTLS_TEST_DEPRECATED */
4394
itayzafrir5c753392018-05-08 11:18:38 +03004395exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004396 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004397 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004398 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004399}
4400/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004401
4402/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004403void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4404 int alg_arg, data_t *hash_data,
4405 data_t *signature_data,
4406 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004407{
Ronald Cron5425a212020-08-04 14:58:35 +02004408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004409 psa_key_type_t key_type = key_type_arg;
4410 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004411 psa_status_t actual_status;
4412 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004413 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004414
Gilles Peskine8817f612018-12-18 00:18:46 +01004415 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004416
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004417 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004418 psa_set_key_algorithm( &attributes, alg );
4419 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004420
Gilles Peskine049c7532019-05-15 20:22:09 +02004421 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004422 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004423
Ronald Cron5425a212020-08-04 14:58:35 +02004424 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004425 hash_data->x, hash_data->len,
4426 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004427 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004428
Gilles Peskine895242b2019-11-29 12:15:40 +01004429#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004430 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004431 hash_data->x, hash_data->len,
4432 signature_data->x, signature_data->len ),
4433 expected_status );
4434#endif /* MBEDTLS_TEST_DEPRECATED */
4435
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004436exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004437 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004438 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004439 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004440}
4441/* END_CASE */
4442
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004443/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004444void asymmetric_encrypt( int key_type_arg,
4445 data_t *key_data,
4446 int alg_arg,
4447 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004448 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004449 int expected_output_length_arg,
4450 int expected_status_arg )
4451{
Ronald Cron5425a212020-08-04 14:58:35 +02004452 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004453 psa_key_type_t key_type = key_type_arg;
4454 psa_algorithm_t alg = alg_arg;
4455 size_t expected_output_length = expected_output_length_arg;
4456 size_t key_bits;
4457 unsigned char *output = NULL;
4458 size_t output_size;
4459 size_t output_length = ~0;
4460 psa_status_t actual_status;
4461 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004463
Gilles Peskine8817f612018-12-18 00:18:46 +01004464 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004465
Gilles Peskine656896e2018-06-29 19:12:28 +02004466 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4468 psa_set_key_algorithm( &attributes, alg );
4469 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004470 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004471 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004472
4473 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004474 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004475 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004476 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004477 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004478
4479 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004480 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004481 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004482 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004483 output, output_size,
4484 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004485 TEST_EQUAL( actual_status, expected_status );
4486 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004487
Gilles Peskine68428122018-06-30 18:42:41 +02004488 /* If the label is empty, the test framework puts a non-null pointer
4489 * in label->x. Test that a null pointer works as well. */
4490 if( label->len == 0 )
4491 {
4492 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004493 if( output_size != 0 )
4494 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004495 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004496 input_data->x, input_data->len,
4497 NULL, label->len,
4498 output, output_size,
4499 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004500 TEST_EQUAL( actual_status, expected_status );
4501 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004502 }
4503
Gilles Peskine656896e2018-06-29 19:12:28 +02004504exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004505 /*
4506 * Key attributes may have been returned by psa_get_key_attributes()
4507 * thus reset them as required.
4508 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004509 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004510
Ronald Cron5425a212020-08-04 14:58:35 +02004511 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004512 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004513 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004514}
4515/* END_CASE */
4516
4517/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004518void asymmetric_encrypt_decrypt( int key_type_arg,
4519 data_t *key_data,
4520 int alg_arg,
4521 data_t *input_data,
4522 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004523{
Ronald Cron5425a212020-08-04 14:58:35 +02004524 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004525 psa_key_type_t key_type = key_type_arg;
4526 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004527 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004528 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004529 size_t output_size;
4530 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004531 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004532 size_t output2_size;
4533 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004534 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004535
Gilles Peskine8817f612018-12-18 00:18:46 +01004536 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004537
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004538 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4539 psa_set_key_algorithm( &attributes, alg );
4540 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004541
Gilles Peskine049c7532019-05-15 20:22:09 +02004542 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004543 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004544
4545 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004546 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004547 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004548 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004549 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004550 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004551 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004552
Gilles Peskineeebd7382018-06-08 18:11:54 +02004553 /* We test encryption by checking that encrypt-then-decrypt gives back
4554 * the original plaintext because of the non-optional random
4555 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004556 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004557 input_data->x, input_data->len,
4558 label->x, label->len,
4559 output, output_size,
4560 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004561 /* We don't know what ciphertext length to expect, but check that
4562 * it looks sensible. */
4563 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004564
Ronald Cron5425a212020-08-04 14:58:35 +02004565 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004566 output, output_length,
4567 label->x, label->len,
4568 output2, output2_size,
4569 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004570 ASSERT_COMPARE( input_data->x, input_data->len,
4571 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004572
4573exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004574 /*
4575 * Key attributes may have been returned by psa_get_key_attributes()
4576 * thus reset them as required.
4577 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004578 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004579
Ronald Cron5425a212020-08-04 14:58:35 +02004580 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004581 mbedtls_free( output );
4582 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004583 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004584}
4585/* END_CASE */
4586
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004587/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004588void asymmetric_decrypt( int key_type_arg,
4589 data_t *key_data,
4590 int alg_arg,
4591 data_t *input_data,
4592 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004593 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004594{
Ronald Cron5425a212020-08-04 14:58:35 +02004595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004596 psa_key_type_t key_type = key_type_arg;
4597 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004598 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004599 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004600 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004601 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004602
Jaeden Amero412654a2019-02-06 12:57:46 +00004603 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004604 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004605
Gilles Peskine8817f612018-12-18 00:18:46 +01004606 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004607
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004608 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4609 psa_set_key_algorithm( &attributes, alg );
4610 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004611
Gilles Peskine049c7532019-05-15 20:22:09 +02004612 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004613 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004614
Ronald Cron5425a212020-08-04 14:58:35 +02004615 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004616 input_data->x, input_data->len,
4617 label->x, label->len,
4618 output,
4619 output_size,
4620 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004621 ASSERT_COMPARE( expected_data->x, expected_data->len,
4622 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004623
Gilles Peskine68428122018-06-30 18:42:41 +02004624 /* If the label is empty, the test framework puts a non-null pointer
4625 * in label->x. Test that a null pointer works as well. */
4626 if( label->len == 0 )
4627 {
4628 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004629 if( output_size != 0 )
4630 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004631 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004632 input_data->x, input_data->len,
4633 NULL, label->len,
4634 output,
4635 output_size,
4636 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004637 ASSERT_COMPARE( expected_data->x, expected_data->len,
4638 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004639 }
4640
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004641exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004642 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004643 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004644 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004645 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004646}
4647/* END_CASE */
4648
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004649/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004650void asymmetric_decrypt_fail( int key_type_arg,
4651 data_t *key_data,
4652 int alg_arg,
4653 data_t *input_data,
4654 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004655 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004656 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004657{
Ronald Cron5425a212020-08-04 14:58:35 +02004658 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004659 psa_key_type_t key_type = key_type_arg;
4660 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004661 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004662 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004663 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004664 psa_status_t actual_status;
4665 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004667
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004668 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004669
Gilles Peskine8817f612018-12-18 00:18:46 +01004670 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004671
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004672 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4673 psa_set_key_algorithm( &attributes, alg );
4674 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004675
Gilles Peskine049c7532019-05-15 20:22:09 +02004676 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004677 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004678
Ronald Cron5425a212020-08-04 14:58:35 +02004679 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004680 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004681 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004682 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004683 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004684 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004685 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004686
Gilles Peskine68428122018-06-30 18:42:41 +02004687 /* If the label is empty, the test framework puts a non-null pointer
4688 * in label->x. Test that a null pointer works as well. */
4689 if( label->len == 0 )
4690 {
4691 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004692 if( output_size != 0 )
4693 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004694 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004695 input_data->x, input_data->len,
4696 NULL, label->len,
4697 output, output_size,
4698 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004699 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004700 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004701 }
4702
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004703exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004704 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004705 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004706 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004707 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004708}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004709/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004710
4711/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004712void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004713{
4714 /* Test each valid way of initializing the object, except for `= {0}`, as
4715 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4716 * though it's OK by the C standard. We could test for this, but we'd need
4717 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004718 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004719 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4720 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4721 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004722
4723 memset( &zero, 0, sizeof( zero ) );
4724
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004726 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004727 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004728 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004729 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004730 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004731 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004732
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004733 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004734 PSA_ASSERT( psa_key_derivation_abort(&func) );
4735 PSA_ASSERT( psa_key_derivation_abort(&init) );
4736 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004737}
4738/* END_CASE */
4739
Janos Follath16de4a42019-06-13 16:32:24 +01004740/* BEGIN_CASE */
4741void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004742{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004743 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004744 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004746
Gilles Peskine8817f612018-12-18 00:18:46 +01004747 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004748
Janos Follath16de4a42019-06-13 16:32:24 +01004749 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004750 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004751
4752exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004753 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004754 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004755}
4756/* END_CASE */
4757
Janos Follathaf3c2a02019-06-12 12:34:34 +01004758/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004759void derive_set_capacity( int alg_arg, int capacity_arg,
4760 int expected_status_arg )
4761{
4762 psa_algorithm_t alg = alg_arg;
4763 size_t capacity = capacity_arg;
4764 psa_status_t expected_status = expected_status_arg;
4765 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4766
4767 PSA_ASSERT( psa_crypto_init( ) );
4768
4769 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4770
4771 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4772 expected_status );
4773
4774exit:
4775 psa_key_derivation_abort( &operation );
4776 PSA_DONE( );
4777}
4778/* END_CASE */
4779
4780/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004781void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004782 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004783 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004784 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004785 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004786 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004787 int expected_status_arg3,
4788 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004789{
4790 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004791 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4792 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004793 psa_status_t expected_statuses[] = {expected_status_arg1,
4794 expected_status_arg2,
4795 expected_status_arg3};
4796 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004797 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4798 MBEDTLS_SVC_KEY_ID_INIT,
4799 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004800 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4801 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4802 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004803 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004804 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004805 psa_status_t expected_output_status = expected_output_status_arg;
4806 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004807
4808 PSA_ASSERT( psa_crypto_init( ) );
4809
4810 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4811 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004812
4813 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4814
4815 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4816 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004817 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004818 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004819 psa_set_key_type( &attributes, key_types[i] );
4820 PSA_ASSERT( psa_import_key( &attributes,
4821 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004822 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004823 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4824 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4825 {
4826 // When taking a private key as secret input, use key agreement
4827 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004828 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004829 expected_statuses[i] );
4830 }
4831 else
4832 {
4833 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004834 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004835 expected_statuses[i] );
4836 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004837 }
4838 else
4839 {
4840 TEST_EQUAL( psa_key_derivation_input_bytes(
4841 &operation, steps[i],
4842 inputs[i]->x, inputs[i]->len ),
4843 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004844 }
4845 }
4846
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004847 if( output_key_type != PSA_KEY_TYPE_NONE )
4848 {
4849 psa_reset_key_attributes( &attributes );
4850 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4851 psa_set_key_bits( &attributes, 8 );
4852 actual_output_status =
4853 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004854 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004855 }
4856 else
4857 {
4858 uint8_t buffer[1];
4859 actual_output_status =
4860 psa_key_derivation_output_bytes( &operation,
4861 buffer, sizeof( buffer ) );
4862 }
4863 TEST_EQUAL( actual_output_status, expected_output_status );
4864
Janos Follathaf3c2a02019-06-12 12:34:34 +01004865exit:
4866 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004867 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4868 psa_destroy_key( keys[i] );
4869 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004870 PSA_DONE( );
4871}
4872/* END_CASE */
4873
Janos Follathd958bb72019-07-03 15:02:16 +01004874/* BEGIN_CASE */
4875void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004876{
Janos Follathd958bb72019-07-03 15:02:16 +01004877 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004878 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004879 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004880 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004881 unsigned char input1[] = "Input 1";
4882 size_t input1_length = sizeof( input1 );
4883 unsigned char input2[] = "Input 2";
4884 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004885 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004886 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004887 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4888 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4889 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004890 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004891
Gilles Peskine8817f612018-12-18 00:18:46 +01004892 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004893
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004894 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4895 psa_set_key_algorithm( &attributes, alg );
4896 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004897
Gilles Peskine73676cb2019-05-15 20:15:10 +02004898 PSA_ASSERT( psa_import_key( &attributes,
4899 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004900 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004901
4902 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004903 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004904 input1, input1_length,
4905 input2, input2_length,
4906 capacity ) )
4907 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004908
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004909 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004910 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004911 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004912
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004914
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004916 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004917
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004918exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004920 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004921 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004922}
4923/* END_CASE */
4924
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004925/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004926void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004927{
4928 uint8_t output_buffer[16];
4929 size_t buffer_size = 16;
4930 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004932
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004933 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4934 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004935 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004936
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004937 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004938 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004939
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004940 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004941
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004942 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4943 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004944 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004945
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004946 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004947 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004948
4949exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004950 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004951}
4952/* END_CASE */
4953
4954/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004955void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004956 int step1_arg, data_t *input1,
4957 int step2_arg, data_t *input2,
4958 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004959 int requested_capacity_arg,
4960 data_t *expected_output1,
4961 data_t *expected_output2 )
4962{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004963 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004964 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4965 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004966 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4967 MBEDTLS_SVC_KEY_ID_INIT,
4968 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004969 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004971 uint8_t *expected_outputs[2] =
4972 {expected_output1->x, expected_output2->x};
4973 size_t output_sizes[2] =
4974 {expected_output1->len, expected_output2->len};
4975 size_t output_buffer_size = 0;
4976 uint8_t *output_buffer = NULL;
4977 size_t expected_capacity;
4978 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004980 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004981 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004982
4983 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4984 {
4985 if( output_sizes[i] > output_buffer_size )
4986 output_buffer_size = output_sizes[i];
4987 if( output_sizes[i] == 0 )
4988 expected_outputs[i] = NULL;
4989 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004990 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004991 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004992
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004993 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4994 psa_set_key_algorithm( &attributes, alg );
4995 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004996
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004997 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004998 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4999 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5000 requested_capacity ) );
5001 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005002 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005003 switch( steps[i] )
5004 {
5005 case 0:
5006 break;
5007 case PSA_KEY_DERIVATION_INPUT_SECRET:
5008 PSA_ASSERT( psa_import_key( &attributes,
5009 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005010 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005011 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005012 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005013 break;
5014 default:
5015 PSA_ASSERT( psa_key_derivation_input_bytes(
5016 &operation, steps[i],
5017 inputs[i]->x, inputs[i]->len ) );
5018 break;
5019 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005020 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005021
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005023 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005024 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005025 expected_capacity = requested_capacity;
5026
5027 /* Expansion phase. */
5028 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5029 {
5030 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005031 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005032 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005033 if( expected_capacity == 0 && output_sizes[i] == 0 )
5034 {
5035 /* Reading 0 bytes when 0 bytes are available can go either way. */
5036 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005037 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005038 continue;
5039 }
5040 else if( expected_capacity == 0 ||
5041 output_sizes[i] > expected_capacity )
5042 {
5043 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005044 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005045 expected_capacity = 0;
5046 continue;
5047 }
5048 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005049 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005050 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005051 ASSERT_COMPARE( output_buffer, output_sizes[i],
5052 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005054 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005055 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005056 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005057 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005058 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005059 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005060
5061exit:
5062 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005064 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5065 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005066 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005067}
5068/* END_CASE */
5069
5070/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005071void derive_full( int alg_arg,
5072 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005073 data_t *input1,
5074 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005075 int requested_capacity_arg )
5076{
Ronald Cron5425a212020-08-04 14:58:35 +02005077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005078 psa_algorithm_t alg = alg_arg;
5079 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005080 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005081 unsigned char output_buffer[16];
5082 size_t expected_capacity = requested_capacity;
5083 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005084 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005085
Gilles Peskine8817f612018-12-18 00:18:46 +01005086 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005087
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005088 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5089 psa_set_key_algorithm( &attributes, alg );
5090 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005091
Gilles Peskine049c7532019-05-15 20:22:09 +02005092 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005093 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005094
Ronald Cron5425a212020-08-04 14:58:35 +02005095 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005096 input1->x, input1->len,
5097 input2->x, input2->len,
5098 requested_capacity ) )
5099 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005100
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005101 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005102 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005103 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005104
5105 /* Expansion phase. */
5106 while( current_capacity > 0 )
5107 {
5108 size_t read_size = sizeof( output_buffer );
5109 if( read_size > current_capacity )
5110 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005111 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005112 output_buffer,
5113 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005114 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005116 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005117 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005118 }
5119
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005120 /* Check that the operation refuses to go over capacity. */
5121 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005122 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005123
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005124 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005125
5126exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005128 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005129 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005130}
5131/* END_CASE */
5132
Janos Follathe60c9052019-07-03 13:51:30 +01005133/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005134void derive_key_exercise( int alg_arg,
5135 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005136 data_t *input1,
5137 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005138 int derived_type_arg,
5139 int derived_bits_arg,
5140 int derived_usage_arg,
5141 int derived_alg_arg )
5142{
Ronald Cron5425a212020-08-04 14:58:35 +02005143 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5144 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005145 psa_algorithm_t alg = alg_arg;
5146 psa_key_type_t derived_type = derived_type_arg;
5147 size_t derived_bits = derived_bits_arg;
5148 psa_key_usage_t derived_usage = derived_usage_arg;
5149 psa_algorithm_t derived_alg = derived_alg_arg;
5150 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005153 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005154
Gilles Peskine8817f612018-12-18 00:18:46 +01005155 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005156
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5158 psa_set_key_algorithm( &attributes, alg );
5159 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005160 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005161 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005162
5163 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005164 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005165 input1->x, input1->len,
5166 input2->x, input2->len, capacity ) )
5167 goto exit;
5168
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005169 psa_set_key_usage_flags( &attributes, derived_usage );
5170 psa_set_key_algorithm( &attributes, derived_alg );
5171 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005172 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005173 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005174 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005175
5176 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005177 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005178 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5179 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005180
5181 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005182 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005183 goto exit;
5184
5185exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005186 /*
5187 * Key attributes may have been returned by psa_get_key_attributes()
5188 * thus reset them as required.
5189 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005190 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005191
5192 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005193 psa_destroy_key( base_key );
5194 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005195 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005196}
5197/* END_CASE */
5198
Janos Follath42fd8882019-07-03 14:17:09 +01005199/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005200void derive_key_export( int alg_arg,
5201 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005202 data_t *input1,
5203 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005204 int bytes1_arg,
5205 int bytes2_arg )
5206{
Ronald Cron5425a212020-08-04 14:58:35 +02005207 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5208 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005209 psa_algorithm_t alg = alg_arg;
5210 size_t bytes1 = bytes1_arg;
5211 size_t bytes2 = bytes2_arg;
5212 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005214 uint8_t *output_buffer = NULL;
5215 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005216 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5217 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005218 size_t length;
5219
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005220 ASSERT_ALLOC( output_buffer, capacity );
5221 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005222 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005223
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005224 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5225 psa_set_key_algorithm( &base_attributes, alg );
5226 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005227 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005228 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005229
5230 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005231 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005232 input1->x, input1->len,
5233 input2->x, input2->len, capacity ) )
5234 goto exit;
5235
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005236 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005237 output_buffer,
5238 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005240
5241 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005242 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005243 input1->x, input1->len,
5244 input2->x, input2->len, capacity ) )
5245 goto exit;
5246
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005247 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5248 psa_set_key_algorithm( &derived_attributes, 0 );
5249 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005250 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005252 &derived_key ) );
5253 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005254 export_buffer, bytes1,
5255 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005256 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005257 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005258 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005259 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005260 &derived_key ) );
5261 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005262 export_buffer + bytes1, bytes2,
5263 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005264 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005265
5266 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005267 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5268 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005269
5270exit:
5271 mbedtls_free( output_buffer );
5272 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005273 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005274 psa_destroy_key( base_key );
5275 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005276 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005277}
5278/* END_CASE */
5279
5280/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005281void derive_key( int alg_arg,
5282 data_t *key_data, data_t *input1, data_t *input2,
5283 int type_arg, int bits_arg,
5284 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005285{
Ronald Cron5425a212020-08-04 14:58:35 +02005286 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5287 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005288 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005289 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005290 size_t bits = bits_arg;
5291 psa_status_t expected_status = expected_status_arg;
5292 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5293 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5294 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5295
5296 PSA_ASSERT( psa_crypto_init( ) );
5297
5298 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5299 psa_set_key_algorithm( &base_attributes, alg );
5300 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5301 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005302 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005303
Ronald Cron5425a212020-08-04 14:58:35 +02005304 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005305 input1->x, input1->len,
5306 input2->x, input2->len, SIZE_MAX ) )
5307 goto exit;
5308
5309 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5310 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005311 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005312 psa_set_key_bits( &derived_attributes, bits );
5313 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005314 &derived_key ),
Gilles Peskinec744d992019-07-30 17:26:54 +02005315 expected_status );
5316
5317exit:
5318 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005319 psa_destroy_key( base_key );
5320 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005321 PSA_DONE( );
5322}
5323/* END_CASE */
5324
5325/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005326void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005327 int our_key_type_arg, int our_key_alg_arg,
5328 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005329 int expected_status_arg )
5330{
Ronald Cron5425a212020-08-04 14:58:35 +02005331 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005332 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005333 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005334 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005335 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005336 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005337 psa_status_t expected_status = expected_status_arg;
5338 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005339
Gilles Peskine8817f612018-12-18 00:18:46 +01005340 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005341
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005343 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005344 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005345 PSA_ASSERT( psa_import_key( &attributes,
5346 our_key_data->x, our_key_data->len,
5347 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005348
Gilles Peskine77f40d82019-04-11 21:27:06 +02005349 /* The tests currently include inputs that should fail at either step.
5350 * Test cases that fail at the setup step should be changed to call
5351 * key_derivation_setup instead, and this function should be renamed
5352 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005353 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005354 if( status == PSA_SUCCESS )
5355 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005356 TEST_EQUAL( psa_key_derivation_key_agreement(
5357 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5358 our_key,
5359 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005360 expected_status );
5361 }
5362 else
5363 {
5364 TEST_ASSERT( status == expected_status );
5365 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005366
5367exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005368 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005369 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005370 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005371}
5372/* END_CASE */
5373
5374/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005375void raw_key_agreement( int alg_arg,
5376 int our_key_type_arg, data_t *our_key_data,
5377 data_t *peer_key_data,
5378 data_t *expected_output )
5379{
Ronald Cron5425a212020-08-04 14:58:35 +02005380 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005381 psa_algorithm_t alg = alg_arg;
5382 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005384 unsigned char *output = NULL;
5385 size_t output_length = ~0;
5386
5387 ASSERT_ALLOC( output, expected_output->len );
5388 PSA_ASSERT( psa_crypto_init( ) );
5389
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005390 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5391 psa_set_key_algorithm( &attributes, alg );
5392 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005393 PSA_ASSERT( psa_import_key( &attributes,
5394 our_key_data->x, our_key_data->len,
5395 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005396
Gilles Peskinebe697d82019-05-16 18:00:41 +02005397 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5398 peer_key_data->x, peer_key_data->len,
5399 output, expected_output->len,
5400 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005401 ASSERT_COMPARE( output, output_length,
5402 expected_output->x, expected_output->len );
5403
5404exit:
5405 mbedtls_free( output );
5406 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005407 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005408}
5409/* END_CASE */
5410
5411/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005412void key_agreement_capacity( int alg_arg,
5413 int our_key_type_arg, data_t *our_key_data,
5414 data_t *peer_key_data,
5415 int expected_capacity_arg )
5416{
Ronald Cron5425a212020-08-04 14:58:35 +02005417 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005418 psa_algorithm_t alg = alg_arg;
5419 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005420 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005422 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005423 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005424
Gilles Peskine8817f612018-12-18 00:18:46 +01005425 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005426
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005427 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5428 psa_set_key_algorithm( &attributes, alg );
5429 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005430 PSA_ASSERT( psa_import_key( &attributes,
5431 our_key_data->x, our_key_data->len,
5432 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005433
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005434 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005435 PSA_ASSERT( psa_key_derivation_key_agreement(
5436 &operation,
5437 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5438 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005439 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5440 {
5441 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005443 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005444 NULL, 0 ) );
5445 }
Gilles Peskine59685592018-09-18 12:11:34 +02005446
Gilles Peskinebf491972018-10-25 22:36:12 +02005447 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005448 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005449 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005450 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005451
Gilles Peskinebf491972018-10-25 22:36:12 +02005452 /* Test the actual capacity by reading the output. */
5453 while( actual_capacity > sizeof( output ) )
5454 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005455 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005456 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005457 actual_capacity -= sizeof( output );
5458 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005459 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005460 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005461 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005462 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005463
Gilles Peskine59685592018-09-18 12:11:34 +02005464exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005465 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005466 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005467 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005468}
5469/* END_CASE */
5470
5471/* BEGIN_CASE */
5472void key_agreement_output( int alg_arg,
5473 int our_key_type_arg, data_t *our_key_data,
5474 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005475 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005476{
Ronald Cron5425a212020-08-04 14:58:35 +02005477 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005478 psa_algorithm_t alg = alg_arg;
5479 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005480 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005482 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005483
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005484 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5485 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005486
Gilles Peskine8817f612018-12-18 00:18:46 +01005487 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005488
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005489 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5490 psa_set_key_algorithm( &attributes, alg );
5491 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005492 PSA_ASSERT( psa_import_key( &attributes,
5493 our_key_data->x, our_key_data->len,
5494 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005495
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005496 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005497 PSA_ASSERT( psa_key_derivation_key_agreement(
5498 &operation,
5499 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5500 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005501 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5502 {
5503 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005504 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005505 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005506 NULL, 0 ) );
5507 }
Gilles Peskine59685592018-09-18 12:11:34 +02005508
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005509 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005510 actual_output,
5511 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005512 ASSERT_COMPARE( actual_output, expected_output1->len,
5513 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005514 if( expected_output2->len != 0 )
5515 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005516 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005517 actual_output,
5518 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005519 ASSERT_COMPARE( actual_output, expected_output2->len,
5520 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005521 }
Gilles Peskine59685592018-09-18 12:11:34 +02005522
5523exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005524 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005525 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005526 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005527 mbedtls_free( actual_output );
5528}
5529/* END_CASE */
5530
5531/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005532void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005533{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005534 size_t bytes = bytes_arg;
5535 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005536 unsigned char *output = NULL;
5537 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005538 size_t i;
5539 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005540
Simon Butcher49f8e312020-03-03 15:51:50 +00005541 TEST_ASSERT( bytes_arg >= 0 );
5542
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005543 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5544 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005545 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005546
Gilles Peskine8817f612018-12-18 00:18:46 +01005547 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005548
Gilles Peskinea50d7392018-06-21 10:22:13 +02005549 /* Run several times, to ensure that every output byte will be
5550 * nonzero at least once with overwhelming probability
5551 * (2^(-8*number_of_runs)). */
5552 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005553 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005554 if( bytes != 0 )
5555 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005556 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005557
5558 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005559 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5560 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005561
5562 for( i = 0; i < bytes; i++ )
5563 {
5564 if( output[i] != 0 )
5565 ++changed[i];
5566 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005567 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005568
5569 /* Check that every byte was changed to nonzero at least once. This
5570 * validates that psa_generate_random is overwriting every byte of
5571 * the output buffer. */
5572 for( i = 0; i < bytes; i++ )
5573 {
5574 TEST_ASSERT( changed[i] != 0 );
5575 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005576
5577exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005578 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005579 mbedtls_free( output );
5580 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005581}
5582/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005583
5584/* BEGIN_CASE */
5585void generate_key( int type_arg,
5586 int bits_arg,
5587 int usage_arg,
5588 int alg_arg,
5589 int expected_status_arg )
5590{
Ronald Cron5425a212020-08-04 14:58:35 +02005591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005592 psa_key_type_t type = type_arg;
5593 psa_key_usage_t usage = usage_arg;
5594 size_t bits = bits_arg;
5595 psa_algorithm_t alg = alg_arg;
5596 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005597 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005598 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005599
Gilles Peskine8817f612018-12-18 00:18:46 +01005600 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005601
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005602 psa_set_key_usage_flags( &attributes, usage );
5603 psa_set_key_algorithm( &attributes, alg );
5604 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005605 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005606
5607 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005608 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005609 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005610 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005611
5612 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005613 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005614 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5615 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005616
Gilles Peskine818ca122018-06-20 18:16:48 +02005617 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005618 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005619 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005620
5621exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005622 /*
5623 * Key attributes may have been returned by psa_get_key_attributes()
5624 * thus reset them as required.
5625 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005626 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005627
Ronald Cron5425a212020-08-04 14:58:35 +02005628 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005629 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005630}
5631/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005632
Gilles Peskinee56e8782019-04-26 17:34:02 +02005633/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5634void generate_key_rsa( int bits_arg,
5635 data_t *e_arg,
5636 int expected_status_arg )
5637{
Ronald Cron5425a212020-08-04 14:58:35 +02005638 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005639 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005640 size_t bits = bits_arg;
5641 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5642 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5643 psa_status_t expected_status = expected_status_arg;
5644 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5645 uint8_t *exported = NULL;
5646 size_t exported_size =
5647 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5648 size_t exported_length = SIZE_MAX;
5649 uint8_t *e_read_buffer = NULL;
5650 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005651 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005652 size_t e_read_length = SIZE_MAX;
5653
5654 if( e_arg->len == 0 ||
5655 ( e_arg->len == 3 &&
5656 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5657 {
5658 is_default_public_exponent = 1;
5659 e_read_size = 0;
5660 }
5661 ASSERT_ALLOC( e_read_buffer, e_read_size );
5662 ASSERT_ALLOC( exported, exported_size );
5663
5664 PSA_ASSERT( psa_crypto_init( ) );
5665
5666 psa_set_key_usage_flags( &attributes, usage );
5667 psa_set_key_algorithm( &attributes, alg );
5668 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5669 e_arg->x, e_arg->len ) );
5670 psa_set_key_bits( &attributes, bits );
5671
5672 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005673 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005674 if( expected_status != PSA_SUCCESS )
5675 goto exit;
5676
5677 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005678 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005679 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5680 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5681 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5682 e_read_buffer, e_read_size,
5683 &e_read_length ) );
5684 if( is_default_public_exponent )
5685 TEST_EQUAL( e_read_length, 0 );
5686 else
5687 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5688
5689 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005690 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005691 goto exit;
5692
5693 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005694 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005695 exported, exported_size,
5696 &exported_length ) );
5697 {
5698 uint8_t *p = exported;
5699 uint8_t *end = exported + exported_length;
5700 size_t len;
5701 /* RSAPublicKey ::= SEQUENCE {
5702 * modulus INTEGER, -- n
5703 * publicExponent INTEGER } -- e
5704 */
5705 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005706 MBEDTLS_ASN1_SEQUENCE |
5707 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005708 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5709 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5710 MBEDTLS_ASN1_INTEGER ) );
5711 if( len >= 1 && p[0] == 0 )
5712 {
5713 ++p;
5714 --len;
5715 }
5716 if( e_arg->len == 0 )
5717 {
5718 TEST_EQUAL( len, 3 );
5719 TEST_EQUAL( p[0], 1 );
5720 TEST_EQUAL( p[1], 0 );
5721 TEST_EQUAL( p[2], 1 );
5722 }
5723 else
5724 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5725 }
5726
5727exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005728 /*
5729 * Key attributes may have been returned by psa_get_key_attributes() or
5730 * set by psa_set_key_domain_parameters() thus reset them as required.
5731 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005732 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005733
Ronald Cron5425a212020-08-04 14:58:35 +02005734 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005735 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005736 mbedtls_free( e_read_buffer );
5737 mbedtls_free( exported );
5738}
5739/* END_CASE */
5740
Darryl Greend49a4992018-06-18 17:27:26 +01005741/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005742void persistent_key_load_key_from_storage( data_t *data,
5743 int type_arg, int bits_arg,
5744 int usage_flags_arg, int alg_arg,
5745 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005746{
Ronald Cron71016a92020-08-28 19:01:50 +02005747 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005749 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5750 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005751 psa_key_type_t type = type_arg;
5752 size_t bits = bits_arg;
5753 psa_key_usage_t usage_flags = usage_flags_arg;
5754 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005756 unsigned char *first_export = NULL;
5757 unsigned char *second_export = NULL;
5758 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5759 size_t first_exported_length;
5760 size_t second_exported_length;
5761
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005762 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5763 {
5764 ASSERT_ALLOC( first_export, export_size );
5765 ASSERT_ALLOC( second_export, export_size );
5766 }
Darryl Greend49a4992018-06-18 17:27:26 +01005767
Gilles Peskine8817f612018-12-18 00:18:46 +01005768 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005769
Gilles Peskinec87af662019-05-15 16:12:22 +02005770 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005771 psa_set_key_usage_flags( &attributes, usage_flags );
5772 psa_set_key_algorithm( &attributes, alg );
5773 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005774 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005775
Darryl Green0c6575a2018-11-07 16:05:30 +00005776 switch( generation_method )
5777 {
5778 case IMPORT_KEY:
5779 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005780 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005781 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005782 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005783
Darryl Green0c6575a2018-11-07 16:05:30 +00005784 case GENERATE_KEY:
5785 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005786 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005787 break;
5788
5789 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005790 {
5791 /* Create base key */
5792 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5793 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5794 psa_set_key_usage_flags( &base_attributes,
5795 PSA_KEY_USAGE_DERIVE );
5796 psa_set_key_algorithm( &base_attributes, derive_alg );
5797 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005798 PSA_ASSERT( psa_import_key( &base_attributes,
5799 data->x, data->len,
5800 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005801 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005802 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005803 PSA_ASSERT( psa_key_derivation_input_key(
5804 &operation,
5805 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005806 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005807 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005808 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005809 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5810 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005811 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005812 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005813 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005814 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005815 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005816 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005817 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005818 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005819
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005820 /* Export the key if permitted by the key policy. */
5821 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5822 {
Ronald Cron5425a212020-08-04 14:58:35 +02005823 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005824 first_export, export_size,
5825 &first_exported_length ) );
5826 if( generation_method == IMPORT_KEY )
5827 ASSERT_COMPARE( data->x, data->len,
5828 first_export, first_exported_length );
5829 }
Darryl Greend49a4992018-06-18 17:27:26 +01005830
5831 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005832 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005833 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005834 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005835
Darryl Greend49a4992018-06-18 17:27:26 +01005836 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005837 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005838 TEST_ASSERT( mbedtls_svc_key_id_equal(
5839 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005840 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5841 PSA_KEY_LIFETIME_PERSISTENT );
5842 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5843 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5844 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5845 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005846
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005847 /* Export the key again if permitted by the key policy. */
5848 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005849 {
Ronald Cron5425a212020-08-04 14:58:35 +02005850 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005851 second_export, export_size,
5852 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005853 ASSERT_COMPARE( first_export, first_exported_length,
5854 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005855 }
5856
5857 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005858 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005859 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005860
5861exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005862 /*
5863 * Key attributes may have been returned by psa_get_key_attributes()
5864 * thus reset them as required.
5865 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005866 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005867
Darryl Greend49a4992018-06-18 17:27:26 +01005868 mbedtls_free( first_export );
5869 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005870 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005871 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005872 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005873 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005874}
5875/* END_CASE */