blob: d486dd19cc997c4851d65a390dedd4f5e4511bfa [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
540 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100541 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100542#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100543 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200544
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100545 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200546 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200547 /* Some algorithms require the payload to have the size of
548 * the hash encoded in the algorithm. Use this input size
549 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200550 if( hash_alg != 0 )
551 payload_length = PSA_HASH_SIZE( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200552 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100553 payload, payload_length,
554 signature, sizeof( signature ),
555 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200556 }
557
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100558 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200559 {
560 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100561 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200562 PSA_SUCCESS :
563 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200564 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100565 payload, payload_length,
566 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100567 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200568 }
569
570 return( 1 );
571
572exit:
573 return( 0 );
574}
575
Ronald Cron5425a212020-08-04 14:58:35 +0200576static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200577 psa_key_usage_t usage,
578 psa_algorithm_t alg )
579{
580 unsigned char plaintext[256] = "Hello, world...";
581 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
582 size_t ciphertext_length = sizeof( ciphertext );
583 size_t plaintext_length = 16;
584
585 if( usage & PSA_KEY_USAGE_ENCRYPT )
586 {
Ronald Cron5425a212020-08-04 14:58:35 +0200587 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100588 plaintext, plaintext_length,
589 NULL, 0,
590 ciphertext, sizeof( ciphertext ),
591 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200592 }
593
594 if( usage & PSA_KEY_USAGE_DECRYPT )
595 {
596 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200597 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200598 ciphertext, ciphertext_length,
599 NULL, 0,
600 plaintext, sizeof( plaintext ),
601 &plaintext_length );
602 TEST_ASSERT( status == PSA_SUCCESS ||
603 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
604 ( status == PSA_ERROR_INVALID_ARGUMENT ||
605 status == PSA_ERROR_INVALID_PADDING ) ) );
606 }
607
608 return( 1 );
609
610exit:
611 return( 0 );
612}
Gilles Peskine02b75072018-07-01 22:31:34 +0200613
Janos Follathf2815ea2019-07-03 12:41:36 +0100614static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200615 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100616 psa_algorithm_t alg,
617 unsigned char* input1, size_t input1_length,
618 unsigned char* input2, size_t input2_length,
619 size_t capacity )
620{
621 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
622 if( PSA_ALG_IS_HKDF( alg ) )
623 {
624 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
625 PSA_KEY_DERIVATION_INPUT_SALT,
626 input1, input1_length ) );
627 PSA_ASSERT( psa_key_derivation_input_key( operation,
628 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200629 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100630 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
631 PSA_KEY_DERIVATION_INPUT_INFO,
632 input2,
633 input2_length ) );
634 }
635 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
636 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
637 {
638 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
639 PSA_KEY_DERIVATION_INPUT_SEED,
640 input1, input1_length ) );
641 PSA_ASSERT( psa_key_derivation_input_key( operation,
642 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200643 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100644 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
645 PSA_KEY_DERIVATION_INPUT_LABEL,
646 input2, input2_length ) );
647 }
648 else
649 {
650 TEST_ASSERT( ! "Key derivation algorithm not supported" );
651 }
652
Gilles Peskinec744d992019-07-30 17:26:54 +0200653 if( capacity != SIZE_MAX )
654 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100655
656 return( 1 );
657
658exit:
659 return( 0 );
660}
661
662
Ronald Cron5425a212020-08-04 14:58:35 +0200663static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200664 psa_key_usage_t usage,
665 psa_algorithm_t alg )
666{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200667 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100668 unsigned char input1[] = "Input 1";
669 size_t input1_length = sizeof( input1 );
670 unsigned char input2[] = "Input 2";
671 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200672 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100673 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200674
675 if( usage & PSA_KEY_USAGE_DERIVE )
676 {
Ronald Cron5425a212020-08-04 14:58:35 +0200677 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100678 input1, input1_length,
679 input2, input2_length, capacity ) )
680 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200681
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200682 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200683 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100684 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200685 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200686 }
687
688 return( 1 );
689
690exit:
691 return( 0 );
692}
693
Gilles Peskinec7998b72018-11-07 18:45:02 +0100694/* We need two keys to exercise key agreement. Exercise the
695 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200696static psa_status_t key_agreement_with_self(
697 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200698 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100699{
700 psa_key_type_t private_key_type;
701 psa_key_type_t public_key_type;
702 size_t key_bits;
703 uint8_t *public_key = NULL;
704 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200705 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200706 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
707 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200708 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100710
Ronald Cron5425a212020-08-04 14:58:35 +0200711 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200712 private_key_type = psa_get_key_type( &attributes );
713 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200714 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100715 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
716 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200717 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100718 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100719
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200720 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200721 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200722 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100723exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100724 /*
725 * Key attributes may have been returned by psa_get_key_attributes()
726 * thus reset them as required.
727 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200728 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100729
730 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100731 return( status );
732}
733
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200734/* We need two keys to exercise key agreement. Exercise the
735 * private key against its own public key. */
736static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200737 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200738{
739 psa_key_type_t private_key_type;
740 psa_key_type_t public_key_type;
741 size_t key_bits;
742 uint8_t *public_key = NULL;
743 size_t public_key_length;
744 uint8_t output[1024];
745 size_t output_length;
746 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200747 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
748 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200749 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200750 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200751
Ronald Cron5425a212020-08-04 14:58:35 +0200752 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200753 private_key_type = psa_get_key_type( &attributes );
754 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200755 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200756 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
757 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200758 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200759 public_key, public_key_length,
760 &public_key_length ) );
761
Ronald Cron5425a212020-08-04 14:58:35 +0200762 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200763 public_key, public_key_length,
764 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200765exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100766 /*
767 * Key attributes may have been returned by psa_get_key_attributes()
768 * thus reset them as required.
769 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200770 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100771
772 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200773 return( status );
774}
775
Ronald Cron5425a212020-08-04 14:58:35 +0200776static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200777 psa_key_usage_t usage,
778 psa_algorithm_t alg )
779{
780 int ok = 0;
781
782 if( usage & PSA_KEY_USAGE_DERIVE )
783 {
784 /* We need two keys to exercise key agreement. Exercise the
785 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200786 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200787 }
788 ok = 1;
789
790exit:
791 return( ok );
792}
793
Ronald Cron5425a212020-08-04 14:58:35 +0200794static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200795 psa_key_usage_t usage,
796 psa_algorithm_t alg )
797{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200798 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200799 unsigned char output[1];
800 int ok = 0;
801
802 if( usage & PSA_KEY_USAGE_DERIVE )
803 {
804 /* We need two keys to exercise key agreement. Exercise the
805 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200806 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200807 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200808 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200809 output,
810 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200811 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200812 }
813 ok = 1;
814
815exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200816 return( ok );
817}
818
Jaeden Amerof7dca862019-06-27 17:31:33 +0100819int asn1_skip_integer( unsigned char **p, const unsigned char *end,
820 size_t min_bits, size_t max_bits,
821 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200822{
823 size_t len;
824 size_t actual_bits;
825 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100826 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100827 MBEDTLS_ASN1_INTEGER ),
828 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200829
830 /* Check if the retrieved length doesn't extend the actual buffer's size.
831 * It is assumed here, that end >= p, which validates casting to size_t. */
832 TEST_ASSERT( len <= (size_t)( end - *p) );
833
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200834 /* Tolerate a slight departure from DER encoding:
835 * - 0 may be represented by an empty string or a 1-byte string.
836 * - The sign bit may be used as a value bit. */
837 if( ( len == 1 && ( *p )[0] == 0 ) ||
838 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
839 {
840 ++( *p );
841 --len;
842 }
843 if( min_bits == 0 && len == 0 )
844 return( 1 );
845 msb = ( *p )[0];
846 TEST_ASSERT( msb != 0 );
847 actual_bits = 8 * ( len - 1 );
848 while( msb != 0 )
849 {
850 msb >>= 1;
851 ++actual_bits;
852 }
853 TEST_ASSERT( actual_bits >= min_bits );
854 TEST_ASSERT( actual_bits <= max_bits );
855 if( must_be_odd )
856 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
857 *p += len;
858 return( 1 );
859exit:
860 return( 0 );
861}
862
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200863static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
864 uint8_t *exported, size_t exported_length )
865{
866 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100867 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200868 else
869 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200870
871#if defined(MBEDTLS_DES_C)
872 if( type == PSA_KEY_TYPE_DES )
873 {
874 /* Check the parity bits. */
875 unsigned i;
876 for( i = 0; i < bits / 8; i++ )
877 {
878 unsigned bit_count = 0;
879 unsigned m;
880 for( m = 1; m <= 0x100; m <<= 1 )
881 {
882 if( exported[i] & m )
883 ++bit_count;
884 }
885 TEST_ASSERT( bit_count % 2 != 0 );
886 }
887 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200888 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200889#endif
890
891#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200892 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200893 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200894 uint8_t *p = exported;
895 uint8_t *end = exported + exported_length;
896 size_t len;
897 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200898 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200899 * modulus INTEGER, -- n
900 * publicExponent INTEGER, -- e
901 * privateExponent INTEGER, -- d
902 * prime1 INTEGER, -- p
903 * prime2 INTEGER, -- q
904 * exponent1 INTEGER, -- d mod (p-1)
905 * exponent2 INTEGER, -- d mod (q-1)
906 * coefficient INTEGER, -- (inverse of q) mod p
907 * }
908 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100909 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
910 MBEDTLS_ASN1_SEQUENCE |
911 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
912 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200913 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
914 goto exit;
915 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
916 goto exit;
917 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
918 goto exit;
919 /* Require d to be at least half the size of n. */
920 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
921 goto exit;
922 /* Require p and q to be at most half the size of n, rounded up. */
923 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
924 goto exit;
925 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
926 goto exit;
927 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
928 goto exit;
929 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
930 goto exit;
931 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
932 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100933 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100934 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200935 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200936#endif /* MBEDTLS_RSA_C */
937
938#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200939 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200940 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100941 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100942 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100943 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200944 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200945#endif /* MBEDTLS_ECP_C */
946
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200947 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
948 {
949 uint8_t *p = exported;
950 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200951#if defined(MBEDTLS_RSA_C)
952 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
953 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100954 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200955 /* RSAPublicKey ::= SEQUENCE {
956 * modulus INTEGER, -- n
957 * publicExponent INTEGER } -- e
958 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100959 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
960 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100961 MBEDTLS_ASN1_CONSTRUCTED ),
962 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100963 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200964 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
965 goto exit;
966 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
967 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100968 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200969 }
970 else
971#endif /* MBEDTLS_RSA_C */
972#if defined(MBEDTLS_ECP_C)
973 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
974 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200975 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
976 {
977 /* The representation of an ECC Montgomery public key is
978 * the raw compressed point */
979 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
980 }
981 else
982 {
983 /* The representation of an ECC Weierstrass public key is:
984 * - The byte 0x04;
985 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
986 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
987 * - where m is the bit size associated with the curve.
988 */
989 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
990 TEST_EQUAL( p[0], 4 );
991 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200992 }
993 else
994#endif /* MBEDTLS_ECP_C */
995 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100996 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200997 mbedtls_snprintf( message, sizeof( message ),
998 "No sanity check for public key type=0x%08lx",
999 (unsigned long) type );
1000 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +02001001 (void) p;
1002 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001003 return( 0 );
1004 }
1005 }
1006 else
1007
1008 {
1009 /* No sanity checks for other types */
1010 }
1011
1012 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001013
1014exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001015 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001016}
1017
Ronald Cron5425a212020-08-04 14:58:35 +02001018static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001019 psa_key_usage_t usage )
1020{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001022 uint8_t *exported = NULL;
1023 size_t exported_size = 0;
1024 size_t exported_length = 0;
1025 int ok = 0;
1026
Ronald Cron5425a212020-08-04 14:58:35 +02001027 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001028
1029 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001030 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001031 {
Ronald Cron5425a212020-08-04 14:58:35 +02001032 TEST_EQUAL( psa_export_key( key, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001034 ok = 1;
1035 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001036 }
1037
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001038 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
1039 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001040 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001041
Ronald Cron5425a212020-08-04 14:58:35 +02001042 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001043 exported, exported_size,
1044 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001045 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1046 psa_get_key_bits( &attributes ),
1047 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001048
1049exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001050 /*
1051 * Key attributes may have been returned by psa_get_key_attributes()
1052 * thus reset them as required.
1053 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001054 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001055
1056 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001057 return( ok );
1058}
1059
Ronald Cron5425a212020-08-04 14:58:35 +02001060static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001061{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001063 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001064 uint8_t *exported = NULL;
1065 size_t exported_size = 0;
1066 size_t exported_length = 0;
1067 int ok = 0;
1068
Ronald Cron5425a212020-08-04 14:58:35 +02001069 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001070 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001071 {
Ronald Cron5425a212020-08-04 14:58:35 +02001072 TEST_EQUAL( psa_export_public_key( key, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001073 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +02001074 return( 1 );
1075 }
1076
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001077 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001078 psa_get_key_type( &attributes ) );
1079 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
1080 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001081 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001082
Ronald Cron5425a212020-08-04 14:58:35 +02001083 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001084 exported, exported_size,
1085 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001086 ok = exported_key_sanity_check( public_type,
1087 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001088 exported, exported_length );
1089
1090exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001091 /*
1092 * Key attributes may have been returned by psa_get_key_attributes()
1093 * thus reset them as required.
1094 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001095 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001096
1097 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001098 return( ok );
1099}
1100
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001101/** Do smoke tests on a key.
1102 *
1103 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1104 * sign/verify, or derivation) that is permitted according to \p usage.
1105 * \p usage and \p alg should correspond to the expected policy on the
1106 * key.
1107 *
1108 * Export the key if permitted by \p usage, and check that the output
1109 * looks sensible. If \p usage forbids export, check that
1110 * \p psa_export_key correctly rejects the attempt. If the key is
1111 * asymmetric, also check \p psa_export_public_key.
1112 *
1113 * If the key fails the tests, this function calls the test framework's
1114 * `test_fail` function and returns false. Otherwise this function returns
1115 * true. Therefore it should be used as follows:
1116 * ```
1117 * if( ! exercise_key( ... ) ) goto exit;
1118 * ```
1119 *
Ronald Cron5425a212020-08-04 14:58:35 +02001120 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001121 * \p alg.
1122 * \param usage The usage flags to assume.
1123 * \param alg The algorithm to exercise.
1124 *
1125 * \retval 0 The key failed the smoke tests.
1126 * \retval 1 The key passed the smoke tests.
1127 */
Ronald Cron5425a212020-08-04 14:58:35 +02001128static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001129 psa_key_usage_t usage,
1130 psa_algorithm_t alg )
1131{
1132 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001133
Ronald Cron5425a212020-08-04 14:58:35 +02001134 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001135 return( 0 );
1136
Gilles Peskine02b75072018-07-01 22:31:34 +02001137 if( alg == 0 )
1138 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1139 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001140 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001141 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001142 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001143 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001144 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001145 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001146 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001147 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001148 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001149 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001150 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001151 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001152 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001153 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001154 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001155 else
1156 {
1157 char message[40];
1158 mbedtls_snprintf( message, sizeof( message ),
1159 "No code to exercise alg=0x%08lx",
1160 (unsigned long) alg );
1161 test_fail( message, __LINE__, __FILE__ );
1162 ok = 0;
1163 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001164
Ronald Cron5425a212020-08-04 14:58:35 +02001165 ok = ok && exercise_export_key( key, usage );
1166 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001167
Gilles Peskine02b75072018-07-01 22:31:34 +02001168 return( ok );
1169}
1170
Gilles Peskine10df3412018-10-25 22:35:43 +02001171static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1172 psa_algorithm_t alg )
1173{
1174 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1175 {
1176 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001177 PSA_KEY_USAGE_VERIFY_HASH :
1178 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001179 }
1180 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1181 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1182 {
1183 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1184 PSA_KEY_USAGE_ENCRYPT :
1185 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1186 }
1187 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1188 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1189 {
1190 return( PSA_KEY_USAGE_DERIVE );
1191 }
1192 else
1193 {
1194 return( 0 );
1195 }
1196
1197}
Darryl Green0c6575a2018-11-07 16:05:30 +00001198
Ronald Cron5425a212020-08-04 14:58:35 +02001199static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001200{
1201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001202 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001203 uint8_t buffer[1];
1204 size_t length;
1205 int ok = 0;
1206
Ronald Cronecfb2372020-07-23 17:13:42 +02001207 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001208 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1209 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1210 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001211 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001212 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001213 TEST_EQUAL(
1214 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1215 TEST_EQUAL(
1216 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001217 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001218 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1219 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1220 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1221 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1222
Ronald Cron5425a212020-08-04 14:58:35 +02001223 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001224 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001225 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001226 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001227 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001228
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001229 ok = 1;
1230
1231exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001232 /*
1233 * Key attributes may have been returned by psa_get_key_attributes()
1234 * thus reset them as required.
1235 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001236 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001237
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001238 return( ok );
1239}
1240
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001241/* Assert that a key isn't reported as having a slot number. */
1242#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1243#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1244 do \
1245 { \
1246 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1247 TEST_EQUAL( psa_get_key_slot_number( \
1248 attributes, \
1249 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1250 PSA_ERROR_INVALID_ARGUMENT ); \
1251 } \
1252 while( 0 )
1253#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1255 ( (void) 0 )
1256#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1257
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001258/* An overapproximation of the amount of storage needed for a key of the
1259 * given type and with the given content. The API doesn't make it easy
1260 * to find a good value for the size. The current implementation doesn't
1261 * care about the value anyway. */
1262#define KEY_BITS_FROM_DATA( type, data ) \
1263 ( data )->len
1264
Darryl Green0c6575a2018-11-07 16:05:30 +00001265typedef enum {
1266 IMPORT_KEY = 0,
1267 GENERATE_KEY = 1,
1268 DERIVE_KEY = 2
1269} generate_method;
1270
Gilles Peskinee59236f2018-01-27 23:32:46 +01001271/* END_HEADER */
1272
1273/* BEGIN_DEPENDENCIES
1274 * depends_on:MBEDTLS_PSA_CRYPTO_C
1275 * END_DEPENDENCIES
1276 */
1277
1278/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001279void static_checks( )
1280{
1281 size_t max_truncated_mac_size =
1282 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1283
1284 /* Check that the length for a truncated MAC always fits in the algorithm
1285 * encoding. The shifted mask is the maximum truncated value. The
1286 * untruncated algorithm may be one byte larger. */
1287 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001288
1289#if defined(MBEDTLS_TEST_DEPRECATED)
1290 /* Check deprecated constants. */
1291 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1292 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1293 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1294 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1295 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1296 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1297 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1298 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001299
Paul Elliott8ff510a2020-06-02 17:19:28 +01001300 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1301 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1302 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1303 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1304 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1305 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1306 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1308 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1309 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1310 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1311 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1312 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1314 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1325 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1326 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1328 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1329 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1330
1331 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1332 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1333 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1334 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1335 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1336 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1337 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1338 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001339
Paul Elliott75e27032020-06-03 15:17:39 +01001340 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1341 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1342 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1343 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1344 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1345
1346 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1347 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001348#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001349}
1350/* END_CASE */
1351
1352/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001353void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001354 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001355 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001356{
Gilles Peskine4747d192019-04-17 15:05:45 +02001357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001358 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001359 psa_key_lifetime_t lifetime = lifetime_arg;
1360 psa_key_usage_t usage_flags = usage_flags_arg;
1361 psa_algorithm_t alg = alg_arg;
1362 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001363 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001364
Ronald Cronecfb2372020-07-23 17:13:42 +02001365 TEST_EQUAL(
1366 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1367 TEST_EQUAL(
1368 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001369 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1370 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1371 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1372 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001373 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001374
Gilles Peskinec87af662019-05-15 16:12:22 +02001375 psa_set_key_id( &attributes, id );
1376 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001377 psa_set_key_usage_flags( &attributes, usage_flags );
1378 psa_set_key_algorithm( &attributes, alg );
1379 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001380 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001381
Ronald Cronecfb2372020-07-23 17:13:42 +02001382 TEST_ASSERT( mbedtls_svc_key_id_equal(
1383 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001384 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1385 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1386 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1387 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001388 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001389
1390 psa_reset_key_attributes( &attributes );
1391
Ronald Cronecfb2372020-07-23 17:13:42 +02001392 TEST_EQUAL(
1393 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1394 TEST_EQUAL(
1395 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001396 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1397 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1398 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1399 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001400 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001401}
1402/* END_CASE */
1403
1404/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001405void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1406 int id2_arg, int owner_id2_arg,
1407 int expected_id_arg, int expected_owner_id_arg,
1408 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001409{
1410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001411 mbedtls_svc_key_id_t id1 =
1412 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001413 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001414 mbedtls_svc_key_id_t id2 =
1415 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001416 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001417 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001418 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1419
1420 if( id1_arg != -1 )
1421 psa_set_key_id( &attributes, id1 );
1422 if( lifetime_arg != -1 )
1423 psa_set_key_lifetime( &attributes, lifetime );
1424 if( id2_arg != -1 )
1425 psa_set_key_id( &attributes, id2 );
1426
Ronald Cronecfb2372020-07-23 17:13:42 +02001427 TEST_ASSERT( mbedtls_svc_key_id_equal(
1428 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001429 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1430}
1431/* END_CASE */
1432
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001433/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1434void slot_number_attribute( )
1435{
1436 psa_key_slot_number_t slot_number = 0xdeadbeef;
1437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1438
1439 /* Initially, there is no slot number. */
1440 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1441 PSA_ERROR_INVALID_ARGUMENT );
1442
1443 /* Test setting a slot number. */
1444 psa_set_key_slot_number( &attributes, 0 );
1445 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1446 TEST_EQUAL( slot_number, 0 );
1447
1448 /* Test changing the slot number. */
1449 psa_set_key_slot_number( &attributes, 42 );
1450 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1451 TEST_EQUAL( slot_number, 42 );
1452
1453 /* Test clearing the slot number. */
1454 psa_clear_key_slot_number( &attributes );
1455 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1456 PSA_ERROR_INVALID_ARGUMENT );
1457
1458 /* Clearing again should have no effect. */
1459 psa_clear_key_slot_number( &attributes );
1460 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1461 PSA_ERROR_INVALID_ARGUMENT );
1462
1463 /* Test that reset clears the slot number. */
1464 psa_set_key_slot_number( &attributes, 42 );
1465 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1466 TEST_EQUAL( slot_number, 42 );
1467 psa_reset_key_attributes( &attributes );
1468 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1469 PSA_ERROR_INVALID_ARGUMENT );
1470}
1471/* END_CASE */
1472
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001473/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001474void import_with_policy( int type_arg,
1475 int usage_arg, int alg_arg,
1476 int expected_status_arg )
1477{
1478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1479 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001480 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001481 psa_key_type_t type = type_arg;
1482 psa_key_usage_t usage = usage_arg;
1483 psa_algorithm_t alg = alg_arg;
1484 psa_status_t expected_status = expected_status_arg;
1485 const uint8_t key_material[16] = {0};
1486 psa_status_t status;
1487
1488 PSA_ASSERT( psa_crypto_init( ) );
1489
1490 psa_set_key_type( &attributes, type );
1491 psa_set_key_usage_flags( &attributes, usage );
1492 psa_set_key_algorithm( &attributes, alg );
1493
1494 status = psa_import_key( &attributes,
1495 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001496 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001497 TEST_EQUAL( status, expected_status );
1498 if( status != PSA_SUCCESS )
1499 goto exit;
1500
Ronald Cron5425a212020-08-04 14:58:35 +02001501 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001502 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1503 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1504 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001505 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001506
Ronald Cron5425a212020-08-04 14:58:35 +02001507 PSA_ASSERT( psa_destroy_key( key ) );
1508 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001509
1510exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001511 /*
1512 * Key attributes may have been returned by psa_get_key_attributes()
1513 * thus reset them as required.
1514 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001515 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001516
1517 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001518 PSA_DONE( );
1519}
1520/* END_CASE */
1521
1522/* BEGIN_CASE */
1523void import_with_data( data_t *data, int type_arg,
1524 int attr_bits_arg,
1525 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001526{
1527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1528 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001529 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001530 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001531 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001532 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001533 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001534
Gilles Peskine8817f612018-12-18 00:18:46 +01001535 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001536
Gilles Peskine4747d192019-04-17 15:05:45 +02001537 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001538 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001539
Ronald Cron5425a212020-08-04 14:58:35 +02001540 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001541 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001542 if( status != PSA_SUCCESS )
1543 goto exit;
1544
Ronald Cron5425a212020-08-04 14:58:35 +02001545 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001546 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001547 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001548 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001549 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001550
Ronald Cron5425a212020-08-04 14:58:35 +02001551 PSA_ASSERT( psa_destroy_key( key ) );
1552 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553
1554exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001555 /*
1556 * Key attributes may have been returned by psa_get_key_attributes()
1557 * thus reset them as required.
1558 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001559 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001560
1561 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001562 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001563}
1564/* END_CASE */
1565
1566/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001567void import_large_key( int type_arg, int byte_size_arg,
1568 int expected_status_arg )
1569{
1570 psa_key_type_t type = type_arg;
1571 size_t byte_size = byte_size_arg;
1572 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1573 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001574 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001575 psa_status_t status;
1576 uint8_t *buffer = NULL;
1577 size_t buffer_size = byte_size + 1;
1578 size_t n;
1579
1580 /* It would be better to skip the test than fail it if the allocation
1581 * fails, but the test framework doesn't support this yet. */
1582 ASSERT_ALLOC( buffer, buffer_size );
1583 memset( buffer, 'K', byte_size );
1584
1585 PSA_ASSERT( psa_crypto_init( ) );
1586
1587 /* Try importing the key */
1588 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1589 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001590 status = psa_import_key( &attributes, buffer, byte_size, &key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001591 TEST_EQUAL( status, expected_status );
1592
1593 if( status == PSA_SUCCESS )
1594 {
Ronald Cron5425a212020-08-04 14:58:35 +02001595 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001596 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1597 TEST_EQUAL( psa_get_key_bits( &attributes ),
1598 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001599 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001600 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001601 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001602 for( n = 0; n < byte_size; n++ )
1603 TEST_EQUAL( buffer[n], 'K' );
1604 for( n = byte_size; n < buffer_size; n++ )
1605 TEST_EQUAL( buffer[n], 0 );
1606 }
1607
1608exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001609 /*
1610 * Key attributes may have been returned by psa_get_key_attributes()
1611 * thus reset them as required.
1612 */
1613 psa_reset_key_attributes( &attributes );
1614
Ronald Cron5425a212020-08-04 14:58:35 +02001615 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001616 PSA_DONE( );
1617 mbedtls_free( buffer );
1618}
1619/* END_CASE */
1620
1621/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001622void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1623{
Ronald Cron5425a212020-08-04 14:58:35 +02001624 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001625 size_t bits = bits_arg;
1626 psa_status_t expected_status = expected_status_arg;
1627 psa_status_t status;
1628 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001629 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001630 size_t buffer_size = /* Slight overapproximations */
1631 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001632 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001633 unsigned char *p;
1634 int ret;
1635 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001636 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001637
Gilles Peskine8817f612018-12-18 00:18:46 +01001638 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001639 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001640
1641 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1642 bits, keypair ) ) >= 0 );
1643 length = ret;
1644
1645 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001646 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001647 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001648 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001649
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001650 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001651 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001652
1653exit:
1654 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001655 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001656}
1657/* END_CASE */
1658
1659/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001660void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001661 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001662 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001663 int expected_bits,
1664 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001665 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001666 int canonical_input )
1667{
Ronald Cron5425a212020-08-04 14:58:35 +02001668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001669 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001670 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001671 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001672 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001673 unsigned char *exported = NULL;
1674 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001675 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001676 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001677 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001679 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001680
Moran Pekercb088e72018-07-17 17:36:59 +03001681 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001682 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001683 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001684 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001685 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001686
Gilles Peskine4747d192019-04-17 15:05:45 +02001687 psa_set_key_usage_flags( &attributes, usage_arg );
1688 psa_set_key_algorithm( &attributes, alg );
1689 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001690
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001691 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001692 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001693
1694 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001695 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001696 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1697 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001698 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001699
1700 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001701 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001703
1704 /* The exported length must be set by psa_export_key() to a value between 0
1705 * and export_size. On errors, the exported length must be 0. */
1706 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1707 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1708 TEST_ASSERT( exported_length <= export_size );
1709
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001710 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001711 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001712 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001713 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001714 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001715 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001716 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001717
Ronald Cron5425a212020-08-04 14:58:35 +02001718 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001719 goto exit;
1720
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001721 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001722 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001723 else
1724 {
Ronald Cron5425a212020-08-04 14:58:35 +02001725 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001726 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001727 &key2 ) );
1728 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001729 reexported,
1730 export_size,
1731 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001732 ASSERT_COMPARE( exported, exported_length,
1733 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001734 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001735 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001736 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001737
1738destroy:
1739 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001740 PSA_ASSERT( psa_destroy_key( key ) );
1741 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001742
1743exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001744 /*
1745 * Key attributes may have been returned by psa_get_key_attributes()
1746 * thus reset them as required.
1747 */
1748 psa_reset_key_attributes( &got_attributes );
1749
itayzafrir3e02b3b2018-06-12 17:06:52 +03001750 mbedtls_free( exported );
1751 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001752 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001753}
1754/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001755
Moran Pekerf709f4a2018-06-06 17:26:04 +03001756/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001757void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001758 int type_arg,
1759 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001760 int export_size_delta,
1761 int expected_export_status_arg,
1762 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001763{
Ronald Cron5425a212020-08-04 14:58:35 +02001764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001765 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001766 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001767 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001768 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001769 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001770 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001771 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001773
Gilles Peskine8817f612018-12-18 00:18:46 +01001774 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001775
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1777 psa_set_key_algorithm( &attributes, alg );
1778 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001779
1780 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001781 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001782
Gilles Peskine49c25912018-10-29 15:15:31 +01001783 /* Export the public key */
1784 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001785 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001786 exported, export_size,
1787 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001788 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001789 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001790 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001791 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001792 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001793 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001794 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001795 TEST_ASSERT( expected_public_key->len <=
1796 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001797 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1798 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001799 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001800
1801exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001802 /*
1803 * Key attributes may have been returned by psa_get_key_attributes()
1804 * thus reset them as required.
1805 */
1806 psa_reset_key_attributes( &attributes );
1807
itayzafrir3e02b3b2018-06-12 17:06:52 +03001808 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001809 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001810 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001811}
1812/* END_CASE */
1813
Gilles Peskine20035e32018-02-03 22:44:14 +01001814/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001815void import_and_exercise_key( data_t *data,
1816 int type_arg,
1817 int bits_arg,
1818 int alg_arg )
1819{
Ronald Cron5425a212020-08-04 14:58:35 +02001820 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001821 psa_key_type_t type = type_arg;
1822 size_t bits = bits_arg;
1823 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001824 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001826 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001827
Gilles Peskine8817f612018-12-18 00:18:46 +01001828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001829
Gilles Peskine4747d192019-04-17 15:05:45 +02001830 psa_set_key_usage_flags( &attributes, usage );
1831 psa_set_key_algorithm( &attributes, alg );
1832 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001833
1834 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001835 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001836
1837 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001838 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001839 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1840 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001841
1842 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001843 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001844 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001845
Ronald Cron5425a212020-08-04 14:58:35 +02001846 PSA_ASSERT( psa_destroy_key( key ) );
1847 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001848
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001849exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001850 /*
1851 * Key attributes may have been returned by psa_get_key_attributes()
1852 * thus reset them as required.
1853 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001854 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001855
1856 psa_reset_key_attributes( &attributes );
1857 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001858 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001859}
1860/* END_CASE */
1861
1862/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001863void effective_key_attributes( int type_arg, int expected_type_arg,
1864 int bits_arg, int expected_bits_arg,
1865 int usage_arg, int expected_usage_arg,
1866 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001867{
Ronald Cron5425a212020-08-04 14:58:35 +02001868 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001869 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001870 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001871 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001872 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001873 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001874 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001875 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001876 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001878
Gilles Peskine8817f612018-12-18 00:18:46 +01001879 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001880
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001881 psa_set_key_usage_flags( &attributes, usage );
1882 psa_set_key_algorithm( &attributes, alg );
1883 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001884 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001885
Ronald Cron5425a212020-08-04 14:58:35 +02001886 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001887 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001888
Ronald Cron5425a212020-08-04 14:58:35 +02001889 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001890 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1891 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1892 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1893 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001894
1895exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001896 /*
1897 * Key attributes may have been returned by psa_get_key_attributes()
1898 * thus reset them as required.
1899 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001900 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001901
1902 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001903 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001904}
1905/* END_CASE */
1906
1907/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001908void check_key_policy( int type_arg, int bits_arg,
1909 int usage_arg, int alg_arg )
1910{
1911 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1912 usage_arg, usage_arg, alg_arg, alg_arg );
1913 goto exit;
1914}
1915/* END_CASE */
1916
1917/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001918void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001919{
1920 /* Test each valid way of initializing the object, except for `= {0}`, as
1921 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1922 * though it's OK by the C standard. We could test for this, but we'd need
1923 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001924 psa_key_attributes_t func = psa_key_attributes_init( );
1925 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1926 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001927
1928 memset( &zero, 0, sizeof( zero ) );
1929
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001930 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1931 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1932 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001933
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001934 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1935 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1936 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1937
1938 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1939 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1940 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1941
1942 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1943 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1944 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1945
1946 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1947 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1948 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001949}
1950/* END_CASE */
1951
1952/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953void mac_key_policy( int policy_usage,
1954 int policy_alg,
1955 int key_type,
1956 data_t *key_data,
1957 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001958{
Ronald Cron5425a212020-08-04 14:58:35 +02001959 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001961 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001962 psa_status_t status;
1963 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001964
Gilles Peskine8817f612018-12-18 00:18:46 +01001965 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001966
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001967 psa_set_key_usage_flags( &attributes, policy_usage );
1968 psa_set_key_algorithm( &attributes, policy_alg );
1969 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001970
Gilles Peskine049c7532019-05-15 20:22:09 +02001971 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001972 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001973
Ronald Cron5425a212020-08-04 14:58:35 +02001974 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001975 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001976 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001977 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001978 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001979 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001980 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001981
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001982 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001983 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001985 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001986 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001987 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001988 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001989
1990exit:
1991 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001992 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001993 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994}
1995/* END_CASE */
1996
1997/* BEGIN_CASE */
1998void cipher_key_policy( int policy_usage,
1999 int policy_alg,
2000 int key_type,
2001 data_t *key_data,
2002 int exercise_alg )
2003{
Ronald Cron5425a212020-08-04 14:58:35 +02002004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002005 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002006 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002007 psa_status_t status;
2008
Gilles Peskine8817f612018-12-18 00:18:46 +01002009 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002010
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002011 psa_set_key_usage_flags( &attributes, policy_usage );
2012 psa_set_key_algorithm( &attributes, policy_alg );
2013 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002014
Gilles Peskine049c7532019-05-15 20:22:09 +02002015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002016 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017
Ronald Cron5425a212020-08-04 14:58:35 +02002018 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019 if( policy_alg == exercise_alg &&
2020 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002021 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002023 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 psa_cipher_abort( &operation );
2025
Ronald Cron5425a212020-08-04 14:58:35 +02002026 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002027 if( policy_alg == exercise_alg &&
2028 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002029 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002031 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032
2033exit:
2034 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002035 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002036 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037}
2038/* END_CASE */
2039
2040/* BEGIN_CASE */
2041void aead_key_policy( int policy_usage,
2042 int policy_alg,
2043 int key_type,
2044 data_t *key_data,
2045 int nonce_length_arg,
2046 int tag_length_arg,
2047 int exercise_alg )
2048{
Ronald Cron5425a212020-08-04 14:58:35 +02002049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002050 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002051 psa_status_t status;
2052 unsigned char nonce[16] = {0};
2053 size_t nonce_length = nonce_length_arg;
2054 unsigned char tag[16];
2055 size_t tag_length = tag_length_arg;
2056 size_t output_length;
2057
2058 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
2059 TEST_ASSERT( tag_length <= sizeof( tag ) );
2060
Gilles Peskine8817f612018-12-18 00:18:46 +01002061 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002062
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002063 psa_set_key_usage_flags( &attributes, policy_usage );
2064 psa_set_key_algorithm( &attributes, policy_alg );
2065 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066
Gilles Peskine049c7532019-05-15 20:22:09 +02002067 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002068 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002069
Ronald Cron5425a212020-08-04 14:58:35 +02002070 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002071 nonce, nonce_length,
2072 NULL, 0,
2073 NULL, 0,
2074 tag, tag_length,
2075 &output_length );
2076 if( policy_alg == exercise_alg &&
2077 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002078 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002080 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081
2082 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002083 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084 nonce, nonce_length,
2085 NULL, 0,
2086 tag, tag_length,
2087 NULL, 0,
2088 &output_length );
2089 if( policy_alg == exercise_alg &&
2090 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002091 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002092 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002093 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094
2095exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002096 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002097 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002098}
2099/* END_CASE */
2100
2101/* BEGIN_CASE */
2102void asymmetric_encryption_key_policy( int policy_usage,
2103 int policy_alg,
2104 int key_type,
2105 data_t *key_data,
2106 int exercise_alg )
2107{
Ronald Cron5425a212020-08-04 14:58:35 +02002108 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002110 psa_status_t status;
2111 size_t key_bits;
2112 size_t buffer_length;
2113 unsigned char *buffer = NULL;
2114 size_t output_length;
2115
Gilles Peskine8817f612018-12-18 00:18:46 +01002116 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002117
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002118 psa_set_key_usage_flags( &attributes, policy_usage );
2119 psa_set_key_algorithm( &attributes, policy_alg );
2120 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002121
Gilles Peskine049c7532019-05-15 20:22:09 +02002122 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002123 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002124
Ronald Cron5425a212020-08-04 14:58:35 +02002125 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002126 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002127 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2128 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002129 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002130
Ronald Cron5425a212020-08-04 14:58:35 +02002131 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132 NULL, 0,
2133 NULL, 0,
2134 buffer, buffer_length,
2135 &output_length );
2136 if( policy_alg == exercise_alg &&
2137 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002138 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002139 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002140 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002141
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002142 if( buffer_length != 0 )
2143 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002144 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145 buffer, buffer_length,
2146 NULL, 0,
2147 buffer, buffer_length,
2148 &output_length );
2149 if( policy_alg == exercise_alg &&
2150 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002151 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002152 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002153 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002154
2155exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002156 /*
2157 * Key attributes may have been returned by psa_get_key_attributes()
2158 * thus reset them as required.
2159 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002160 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002161
2162 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002163 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002164 mbedtls_free( buffer );
2165}
2166/* END_CASE */
2167
2168/* BEGIN_CASE */
2169void asymmetric_signature_key_policy( int policy_usage,
2170 int policy_alg,
2171 int key_type,
2172 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002173 int exercise_alg,
2174 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002175{
Ronald Cron5425a212020-08-04 14:58:35 +02002176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002178 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002179 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2180 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2181 * compatible with the policy and `payload_length_arg` is supposed to be
2182 * a valid input length to sign. If `payload_length_arg <= 0`,
2183 * `exercise_alg` is supposed to be forbidden by the policy. */
2184 int compatible_alg = payload_length_arg > 0;
2185 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002186 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002187 size_t signature_length;
2188
Gilles Peskine8817f612018-12-18 00:18:46 +01002189 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002190
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002191 psa_set_key_usage_flags( &attributes, policy_usage );
2192 psa_set_key_algorithm( &attributes, policy_alg );
2193 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002194
Gilles Peskine049c7532019-05-15 20:22:09 +02002195 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002196 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002197
Ronald Cron5425a212020-08-04 14:58:35 +02002198 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002199 payload, payload_length,
2200 signature, sizeof( signature ),
2201 &signature_length );
2202 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002203 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002204 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002205 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002206
2207 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002208 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002209 payload, payload_length,
2210 signature, sizeof( signature ) );
2211 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002212 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002213 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002214 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002215
2216exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002218 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002219}
2220/* END_CASE */
2221
Janos Follathba3fab92019-06-11 14:50:16 +01002222/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002223void derive_key_policy( int policy_usage,
2224 int policy_alg,
2225 int key_type,
2226 data_t *key_data,
2227 int exercise_alg )
2228{
Ronald Cron5425a212020-08-04 14:58:35 +02002229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002231 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002232 psa_status_t status;
2233
Gilles Peskine8817f612018-12-18 00:18:46 +01002234 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002235
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002236 psa_set_key_usage_flags( &attributes, policy_usage );
2237 psa_set_key_algorithm( &attributes, policy_alg );
2238 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002239
Gilles Peskine049c7532019-05-15 20:22:09 +02002240 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002241 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002242
Janos Follathba3fab92019-06-11 14:50:16 +01002243 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2244
2245 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2246 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002247 {
Janos Follathba3fab92019-06-11 14:50:16 +01002248 PSA_ASSERT( psa_key_derivation_input_bytes(
2249 &operation,
2250 PSA_KEY_DERIVATION_INPUT_SEED,
2251 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002252 }
Janos Follathba3fab92019-06-11 14:50:16 +01002253
2254 status = psa_key_derivation_input_key( &operation,
2255 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002256 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002257
Gilles Peskineea0fb492018-07-12 17:17:20 +02002258 if( policy_alg == exercise_alg &&
2259 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002260 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002261 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002262 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002263
2264exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002265 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002266 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002267 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002268}
2269/* END_CASE */
2270
2271/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002272void agreement_key_policy( int policy_usage,
2273 int policy_alg,
2274 int key_type_arg,
2275 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002276 int exercise_alg,
2277 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002278{
Ronald Cron5425a212020-08-04 14:58:35 +02002279 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002281 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002282 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002283 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002284 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002285
Gilles Peskine8817f612018-12-18 00:18:46 +01002286 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002287
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002288 psa_set_key_usage_flags( &attributes, policy_usage );
2289 psa_set_key_algorithm( &attributes, policy_alg );
2290 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002291
Gilles Peskine049c7532019-05-15 20:22:09 +02002292 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002293 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002294
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002295 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002296 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002297
Steven Cooremance48e852020-10-05 16:02:45 +02002298 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002299
2300exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002301 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002302 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002303 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002304}
2305/* END_CASE */
2306
2307/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002308void key_policy_alg2( int key_type_arg, data_t *key_data,
2309 int usage_arg, int alg_arg, int alg2_arg )
2310{
Ronald Cron5425a212020-08-04 14:58:35 +02002311 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002312 psa_key_type_t key_type = key_type_arg;
2313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2314 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2315 psa_key_usage_t usage = usage_arg;
2316 psa_algorithm_t alg = alg_arg;
2317 psa_algorithm_t alg2 = alg2_arg;
2318
2319 PSA_ASSERT( psa_crypto_init( ) );
2320
2321 psa_set_key_usage_flags( &attributes, usage );
2322 psa_set_key_algorithm( &attributes, alg );
2323 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2324 psa_set_key_type( &attributes, key_type );
2325 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002326 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002327
Ronald Cron5425a212020-08-04 14:58:35 +02002328 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002329 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2330 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2331 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2332
Ronald Cron5425a212020-08-04 14:58:35 +02002333 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002334 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002335 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002336 goto exit;
2337
2338exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002339 /*
2340 * Key attributes may have been returned by psa_get_key_attributes()
2341 * thus reset them as required.
2342 */
2343 psa_reset_key_attributes( &got_attributes );
2344
Ronald Cron5425a212020-08-04 14:58:35 +02002345 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002346 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002347}
2348/* END_CASE */
2349
2350/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002351void raw_agreement_key_policy( int policy_usage,
2352 int policy_alg,
2353 int key_type_arg,
2354 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002355 int exercise_alg,
2356 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002357{
Ronald Cron5425a212020-08-04 14:58:35 +02002358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002360 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002361 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002362 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002363 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002364
2365 PSA_ASSERT( psa_crypto_init( ) );
2366
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002367 psa_set_key_usage_flags( &attributes, policy_usage );
2368 psa_set_key_algorithm( &attributes, policy_alg );
2369 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002370
Gilles Peskine049c7532019-05-15 20:22:09 +02002371 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002372 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002373
Ronald Cron5425a212020-08-04 14:58:35 +02002374 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002375
Steven Cooremance48e852020-10-05 16:02:45 +02002376 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002377
2378exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002379 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002380 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002381 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002382}
2383/* END_CASE */
2384
2385/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002386void copy_success( int source_usage_arg,
2387 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002388 int type_arg, data_t *material,
2389 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002390 int target_usage_arg,
2391 int target_alg_arg, int target_alg2_arg,
2392 int expected_usage_arg,
2393 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002394{
Gilles Peskineca25db92019-04-19 11:43:08 +02002395 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2396 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002397 psa_key_usage_t expected_usage = expected_usage_arg;
2398 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002399 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002400 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2401 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002402 uint8_t *export_buffer = NULL;
2403
Gilles Peskine57ab7212019-01-28 13:03:09 +01002404 PSA_ASSERT( psa_crypto_init( ) );
2405
Gilles Peskineca25db92019-04-19 11:43:08 +02002406 /* Prepare the source key. */
2407 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2408 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002409 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002410 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002411 PSA_ASSERT( psa_import_key( &source_attributes,
2412 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002413 &source_key ) );
2414 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002415
Gilles Peskineca25db92019-04-19 11:43:08 +02002416 /* Prepare the target attributes. */
2417 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002418 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002419 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002420 /* Set volatile lifetime to reset the key identifier to 0. */
2421 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2422 }
2423
Gilles Peskineca25db92019-04-19 11:43:08 +02002424 if( target_usage_arg != -1 )
2425 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2426 if( target_alg_arg != -1 )
2427 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002428 if( target_alg2_arg != -1 )
2429 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002430
2431 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002432 PSA_ASSERT( psa_copy_key( source_key,
2433 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002434
2435 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002436 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002437
2438 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002439 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002440 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2441 psa_get_key_type( &target_attributes ) );
2442 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2443 psa_get_key_bits( &target_attributes ) );
2444 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2445 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002446 TEST_EQUAL( expected_alg2,
2447 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002448 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2449 {
2450 size_t length;
2451 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002452 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002453 material->len, &length ) );
2454 ASSERT_COMPARE( material->x, material->len,
2455 export_buffer, length );
2456 }
Ronald Cron5425a212020-08-04 14:58:35 +02002457 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002458 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002459 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002460 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002461
Ronald Cron5425a212020-08-04 14:58:35 +02002462 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002463
2464exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002465 /*
2466 * Source and target key attributes may have been returned by
2467 * psa_get_key_attributes() thus reset them as required.
2468 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002469 psa_reset_key_attributes( &source_attributes );
2470 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002471
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002472 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002473 mbedtls_free( export_buffer );
2474}
2475/* END_CASE */
2476
2477/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002478void copy_fail( int source_usage_arg,
2479 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002480 int type_arg, data_t *material,
2481 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002482 int target_usage_arg,
2483 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002484 int expected_status_arg )
2485{
2486 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2487 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002488 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2489 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002490
2491 PSA_ASSERT( psa_crypto_init( ) );
2492
2493 /* Prepare the source key. */
2494 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2495 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002496 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002497 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002498 PSA_ASSERT( psa_import_key( &source_attributes,
2499 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002500 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002501
2502 /* Prepare the target attributes. */
2503 psa_set_key_type( &target_attributes, target_type_arg );
2504 psa_set_key_bits( &target_attributes, target_bits_arg );
2505 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2506 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002507 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002508
2509 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002510 TEST_EQUAL( psa_copy_key( source_key,
2511 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002512 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002513
Ronald Cron5425a212020-08-04 14:58:35 +02002514 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002515
Gilles Peskine4a644642019-05-03 17:14:08 +02002516exit:
2517 psa_reset_key_attributes( &source_attributes );
2518 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002519 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002520}
2521/* END_CASE */
2522
2523/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002524void hash_operation_init( )
2525{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002526 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002527 /* Test each valid way of initializing the object, except for `= {0}`, as
2528 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2529 * though it's OK by the C standard. We could test for this, but we'd need
2530 * to supress the Clang warning for the test. */
2531 psa_hash_operation_t func = psa_hash_operation_init( );
2532 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2533 psa_hash_operation_t zero;
2534
2535 memset( &zero, 0, sizeof( zero ) );
2536
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002537 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002538 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2539 PSA_ERROR_BAD_STATE );
2540 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2541 PSA_ERROR_BAD_STATE );
2542 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2543 PSA_ERROR_BAD_STATE );
2544
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002545 /* A default hash operation should be abortable without error. */
2546 PSA_ASSERT( psa_hash_abort( &func ) );
2547 PSA_ASSERT( psa_hash_abort( &init ) );
2548 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002549}
2550/* END_CASE */
2551
2552/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002553void hash_setup( int alg_arg,
2554 int expected_status_arg )
2555{
2556 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002557 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002558 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002559 psa_status_t status;
2560
Gilles Peskine8817f612018-12-18 00:18:46 +01002561 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002562
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002563 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002564 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002565
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002566 /* Whether setup succeeded or failed, abort must succeed. */
2567 PSA_ASSERT( psa_hash_abort( &operation ) );
2568
2569 /* If setup failed, reproduce the failure, so as to
2570 * test the resulting state of the operation object. */
2571 if( status != PSA_SUCCESS )
2572 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2573
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002574 /* Now the operation object should be reusable. */
2575#if defined(KNOWN_SUPPORTED_HASH_ALG)
2576 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2577 PSA_ASSERT( psa_hash_abort( &operation ) );
2578#endif
2579
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002580exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002581 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002582}
2583/* END_CASE */
2584
2585/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002586void hash_compute_fail( int alg_arg, data_t *input,
2587 int output_size_arg, int expected_status_arg )
2588{
2589 psa_algorithm_t alg = alg_arg;
2590 uint8_t *output = NULL;
2591 size_t output_size = output_size_arg;
2592 size_t output_length = INVALID_EXPORT_LENGTH;
2593 psa_status_t expected_status = expected_status_arg;
2594 psa_status_t status;
2595
2596 ASSERT_ALLOC( output, output_size );
2597
2598 PSA_ASSERT( psa_crypto_init( ) );
2599
2600 status = psa_hash_compute( alg, input->x, input->len,
2601 output, output_size, &output_length );
2602 TEST_EQUAL( status, expected_status );
2603 TEST_ASSERT( output_length <= output_size );
2604
2605exit:
2606 mbedtls_free( output );
2607 PSA_DONE( );
2608}
2609/* END_CASE */
2610
2611/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002612void hash_compare_fail( int alg_arg, data_t *input,
2613 data_t *reference_hash,
2614 int expected_status_arg )
2615{
2616 psa_algorithm_t alg = alg_arg;
2617 psa_status_t expected_status = expected_status_arg;
2618 psa_status_t status;
2619
2620 PSA_ASSERT( psa_crypto_init( ) );
2621
2622 status = psa_hash_compare( alg, input->x, input->len,
2623 reference_hash->x, reference_hash->len );
2624 TEST_EQUAL( status, expected_status );
2625
2626exit:
2627 PSA_DONE( );
2628}
2629/* END_CASE */
2630
2631/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002632void hash_compute_compare( int alg_arg, data_t *input,
2633 data_t *expected_output )
2634{
2635 psa_algorithm_t alg = alg_arg;
2636 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2637 size_t output_length = INVALID_EXPORT_LENGTH;
2638 size_t i;
2639
2640 PSA_ASSERT( psa_crypto_init( ) );
2641
2642 /* Compute with tight buffer */
2643 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2644 output, PSA_HASH_SIZE( alg ),
2645 &output_length ) );
2646 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2647 ASSERT_COMPARE( output, output_length,
2648 expected_output->x, expected_output->len );
2649
2650 /* Compute with larger buffer */
2651 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2652 output, sizeof( output ),
2653 &output_length ) );
2654 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2655 ASSERT_COMPARE( output, output_length,
2656 expected_output->x, expected_output->len );
2657
2658 /* Compare with correct hash */
2659 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2660 output, output_length ) );
2661
2662 /* Compare with trailing garbage */
2663 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2664 output, output_length + 1 ),
2665 PSA_ERROR_INVALID_SIGNATURE );
2666
2667 /* Compare with truncated hash */
2668 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2669 output, output_length - 1 ),
2670 PSA_ERROR_INVALID_SIGNATURE );
2671
2672 /* Compare with corrupted value */
2673 for( i = 0; i < output_length; i++ )
2674 {
2675 test_set_step( i );
2676 output[i] ^= 1;
2677 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2678 output, output_length ),
2679 PSA_ERROR_INVALID_SIGNATURE );
2680 output[i] ^= 1;
2681 }
2682
2683exit:
2684 PSA_DONE( );
2685}
2686/* END_CASE */
2687
2688/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002689void hash_bad_order( )
2690{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002691 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002692 unsigned char input[] = "";
2693 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002694 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002695 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2696 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2697 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002698 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002699 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002700 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002701
Gilles Peskine8817f612018-12-18 00:18:46 +01002702 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002703
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002704 /* Call setup twice in a row. */
2705 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2706 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2707 PSA_ERROR_BAD_STATE );
2708 PSA_ASSERT( psa_hash_abort( &operation ) );
2709
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002710 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002711 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002712 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002713 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002714
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002715 /* Call update after finish. */
2716 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2717 PSA_ASSERT( psa_hash_finish( &operation,
2718 hash, sizeof( hash ), &hash_len ) );
2719 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002720 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002721 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002722
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002723 /* Call verify without calling setup beforehand. */
2724 TEST_EQUAL( psa_hash_verify( &operation,
2725 valid_hash, sizeof( valid_hash ) ),
2726 PSA_ERROR_BAD_STATE );
2727 PSA_ASSERT( psa_hash_abort( &operation ) );
2728
2729 /* Call verify after finish. */
2730 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2731 PSA_ASSERT( psa_hash_finish( &operation,
2732 hash, sizeof( hash ), &hash_len ) );
2733 TEST_EQUAL( psa_hash_verify( &operation,
2734 valid_hash, sizeof( valid_hash ) ),
2735 PSA_ERROR_BAD_STATE );
2736 PSA_ASSERT( psa_hash_abort( &operation ) );
2737
2738 /* Call verify twice in a row. */
2739 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2740 PSA_ASSERT( psa_hash_verify( &operation,
2741 valid_hash, sizeof( valid_hash ) ) );
2742 TEST_EQUAL( psa_hash_verify( &operation,
2743 valid_hash, sizeof( valid_hash ) ),
2744 PSA_ERROR_BAD_STATE );
2745 PSA_ASSERT( psa_hash_abort( &operation ) );
2746
2747 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002748 TEST_EQUAL( psa_hash_finish( &operation,
2749 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002750 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002751 PSA_ASSERT( psa_hash_abort( &operation ) );
2752
2753 /* Call finish twice in a row. */
2754 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2755 PSA_ASSERT( psa_hash_finish( &operation,
2756 hash, sizeof( hash ), &hash_len ) );
2757 TEST_EQUAL( psa_hash_finish( &operation,
2758 hash, sizeof( hash ), &hash_len ),
2759 PSA_ERROR_BAD_STATE );
2760 PSA_ASSERT( psa_hash_abort( &operation ) );
2761
2762 /* Call finish after calling verify. */
2763 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2764 PSA_ASSERT( psa_hash_verify( &operation,
2765 valid_hash, sizeof( valid_hash ) ) );
2766 TEST_EQUAL( psa_hash_finish( &operation,
2767 hash, sizeof( hash ), &hash_len ),
2768 PSA_ERROR_BAD_STATE );
2769 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002770
2771exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002772 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002773}
2774/* END_CASE */
2775
itayzafrir27e69452018-11-01 14:26:34 +02002776/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2777void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002778{
2779 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002780 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2781 * appended to it */
2782 unsigned char hash[] = {
2783 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2784 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2785 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002786 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002787 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002788
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002790
itayzafrir27e69452018-11-01 14:26:34 +02002791 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002792 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002793 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002794 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002795
itayzafrir27e69452018-11-01 14:26:34 +02002796 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002797 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002798 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002799 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002800
itayzafrir27e69452018-11-01 14:26:34 +02002801 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002803 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002804 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002805
itayzafrirec93d302018-10-18 18:01:10 +03002806exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002807 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002808}
2809/* END_CASE */
2810
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002811/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2812void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002813{
2814 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002815 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002816 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002817 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002818 size_t hash_len;
2819
Gilles Peskine8817f612018-12-18 00:18:46 +01002820 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002821
itayzafrir58028322018-10-25 10:22:01 +03002822 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002823 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002824 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002825 hash, expected_size - 1, &hash_len ),
2826 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002827
2828exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002829 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002830}
2831/* END_CASE */
2832
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002833/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2834void hash_clone_source_state( )
2835{
2836 psa_algorithm_t alg = PSA_ALG_SHA_256;
2837 unsigned char hash[PSA_HASH_MAX_SIZE];
2838 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2839 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2840 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2841 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2842 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2843 size_t hash_len;
2844
2845 PSA_ASSERT( psa_crypto_init( ) );
2846 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2847
2848 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2849 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2850 PSA_ASSERT( psa_hash_finish( &op_finished,
2851 hash, sizeof( hash ), &hash_len ) );
2852 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2853 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2854
2855 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2856 PSA_ERROR_BAD_STATE );
2857
2858 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2859 PSA_ASSERT( psa_hash_finish( &op_init,
2860 hash, sizeof( hash ), &hash_len ) );
2861 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2862 PSA_ASSERT( psa_hash_finish( &op_finished,
2863 hash, sizeof( hash ), &hash_len ) );
2864 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2865 PSA_ASSERT( psa_hash_finish( &op_aborted,
2866 hash, sizeof( hash ), &hash_len ) );
2867
2868exit:
2869 psa_hash_abort( &op_source );
2870 psa_hash_abort( &op_init );
2871 psa_hash_abort( &op_setup );
2872 psa_hash_abort( &op_finished );
2873 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002874 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002875}
2876/* END_CASE */
2877
2878/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2879void hash_clone_target_state( )
2880{
2881 psa_algorithm_t alg = PSA_ALG_SHA_256;
2882 unsigned char hash[PSA_HASH_MAX_SIZE];
2883 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2884 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2885 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2886 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2887 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2888 size_t hash_len;
2889
2890 PSA_ASSERT( psa_crypto_init( ) );
2891
2892 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2893 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2894 PSA_ASSERT( psa_hash_finish( &op_finished,
2895 hash, sizeof( hash ), &hash_len ) );
2896 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2897 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2898
2899 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2900 PSA_ASSERT( psa_hash_finish( &op_target,
2901 hash, sizeof( hash ), &hash_len ) );
2902
2903 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2904 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2905 PSA_ERROR_BAD_STATE );
2906 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2907 PSA_ERROR_BAD_STATE );
2908
2909exit:
2910 psa_hash_abort( &op_target );
2911 psa_hash_abort( &op_init );
2912 psa_hash_abort( &op_setup );
2913 psa_hash_abort( &op_finished );
2914 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002915 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002916}
2917/* END_CASE */
2918
itayzafrir58028322018-10-25 10:22:01 +03002919/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002920void mac_operation_init( )
2921{
Jaeden Amero252ef282019-02-15 14:05:35 +00002922 const uint8_t input[1] = { 0 };
2923
Jaeden Amero769ce272019-01-04 11:48:03 +00002924 /* Test each valid way of initializing the object, except for `= {0}`, as
2925 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2926 * though it's OK by the C standard. We could test for this, but we'd need
2927 * to supress the Clang warning for the test. */
2928 psa_mac_operation_t func = psa_mac_operation_init( );
2929 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2930 psa_mac_operation_t zero;
2931
2932 memset( &zero, 0, sizeof( zero ) );
2933
Jaeden Amero252ef282019-02-15 14:05:35 +00002934 /* A freshly-initialized MAC operation should not be usable. */
2935 TEST_EQUAL( psa_mac_update( &func,
2936 input, sizeof( input ) ),
2937 PSA_ERROR_BAD_STATE );
2938 TEST_EQUAL( psa_mac_update( &init,
2939 input, sizeof( input ) ),
2940 PSA_ERROR_BAD_STATE );
2941 TEST_EQUAL( psa_mac_update( &zero,
2942 input, sizeof( input ) ),
2943 PSA_ERROR_BAD_STATE );
2944
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002945 /* A default MAC operation should be abortable without error. */
2946 PSA_ASSERT( psa_mac_abort( &func ) );
2947 PSA_ASSERT( psa_mac_abort( &init ) );
2948 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002949}
2950/* END_CASE */
2951
2952/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002953void mac_setup( int key_type_arg,
2954 data_t *key,
2955 int alg_arg,
2956 int expected_status_arg )
2957{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002958 psa_key_type_t key_type = key_type_arg;
2959 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002960 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002961 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002962 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2963#if defined(KNOWN_SUPPORTED_MAC_ALG)
2964 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2965#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002966
Gilles Peskine8817f612018-12-18 00:18:46 +01002967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002968
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002969 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2970 &operation, &status ) )
2971 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002972 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002973
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002974 /* The operation object should be reusable. */
2975#if defined(KNOWN_SUPPORTED_MAC_ALG)
2976 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2977 smoke_test_key_data,
2978 sizeof( smoke_test_key_data ),
2979 KNOWN_SUPPORTED_MAC_ALG,
2980 &operation, &status ) )
2981 goto exit;
2982 TEST_EQUAL( status, PSA_SUCCESS );
2983#endif
2984
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002985exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002986 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002987}
2988/* END_CASE */
2989
2990/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002991void mac_bad_order( )
2992{
Ronald Cron5425a212020-08-04 14:58:35 +02002993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002994 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2995 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002996 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002997 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2998 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2999 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003000 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003001 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3002 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3003 size_t sign_mac_length = 0;
3004 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3005 const uint8_t verify_mac[] = {
3006 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3007 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3008 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3009
3010 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003011 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003012 psa_set_key_algorithm( &attributes, alg );
3013 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003014
Ronald Cron5425a212020-08-04 14:58:35 +02003015 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3016 &key ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003017
Jaeden Amero252ef282019-02-15 14:05:35 +00003018 /* Call update without calling setup beforehand. */
3019 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3020 PSA_ERROR_BAD_STATE );
3021 PSA_ASSERT( psa_mac_abort( &operation ) );
3022
3023 /* Call sign finish without calling setup beforehand. */
3024 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3025 &sign_mac_length),
3026 PSA_ERROR_BAD_STATE );
3027 PSA_ASSERT( psa_mac_abort( &operation ) );
3028
3029 /* Call verify finish without calling setup beforehand. */
3030 TEST_EQUAL( psa_mac_verify_finish( &operation,
3031 verify_mac, sizeof( verify_mac ) ),
3032 PSA_ERROR_BAD_STATE );
3033 PSA_ASSERT( psa_mac_abort( &operation ) );
3034
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003035 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003036 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
3037 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003038 PSA_ERROR_BAD_STATE );
3039 PSA_ASSERT( psa_mac_abort( &operation ) );
3040
Jaeden Amero252ef282019-02-15 14:05:35 +00003041 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003042 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003043 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3044 PSA_ASSERT( psa_mac_sign_finish( &operation,
3045 sign_mac, sizeof( sign_mac ),
3046 &sign_mac_length ) );
3047 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3048 PSA_ERROR_BAD_STATE );
3049 PSA_ASSERT( psa_mac_abort( &operation ) );
3050
3051 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003052 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003053 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3054 PSA_ASSERT( psa_mac_verify_finish( &operation,
3055 verify_mac, sizeof( verify_mac ) ) );
3056 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3057 PSA_ERROR_BAD_STATE );
3058 PSA_ASSERT( psa_mac_abort( &operation ) );
3059
3060 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003061 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003062 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3063 PSA_ASSERT( psa_mac_sign_finish( &operation,
3064 sign_mac, sizeof( sign_mac ),
3065 &sign_mac_length ) );
3066 TEST_EQUAL( psa_mac_sign_finish( &operation,
3067 sign_mac, sizeof( sign_mac ),
3068 &sign_mac_length ),
3069 PSA_ERROR_BAD_STATE );
3070 PSA_ASSERT( psa_mac_abort( &operation ) );
3071
3072 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003073 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003074 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3075 PSA_ASSERT( psa_mac_verify_finish( &operation,
3076 verify_mac, sizeof( verify_mac ) ) );
3077 TEST_EQUAL( psa_mac_verify_finish( &operation,
3078 verify_mac, sizeof( verify_mac ) ),
3079 PSA_ERROR_BAD_STATE );
3080 PSA_ASSERT( psa_mac_abort( &operation ) );
3081
3082 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003083 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003084 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3085 TEST_EQUAL( psa_mac_verify_finish( &operation,
3086 verify_mac, sizeof( verify_mac ) ),
3087 PSA_ERROR_BAD_STATE );
3088 PSA_ASSERT( psa_mac_abort( &operation ) );
3089
3090 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003091 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003092 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3093 TEST_EQUAL( psa_mac_sign_finish( &operation,
3094 sign_mac, sizeof( sign_mac ),
3095 &sign_mac_length ),
3096 PSA_ERROR_BAD_STATE );
3097 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003098
Ronald Cron5425a212020-08-04 14:58:35 +02003099 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003100
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003101exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003102 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003103}
3104/* END_CASE */
3105
3106/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003107void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003108 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003109 int alg_arg,
3110 data_t *input,
3111 data_t *expected_mac )
3112{
Ronald Cron5425a212020-08-04 14:58:35 +02003113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003114 psa_key_type_t key_type = key_type_arg;
3115 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003116 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003118 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003119 size_t mac_buffer_size =
Ronald Cron5425a212020-08-04 14:58:35 +02003120 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003121 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003122 const size_t output_sizes_to_test[] = {
3123 0,
3124 1,
3125 expected_mac->len - 1,
3126 expected_mac->len,
3127 expected_mac->len + 1,
3128 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003129
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003130 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
Gilles Peskine3d404d62020-08-25 23:47:36 +02003131 /* We expect PSA_MAC_FINAL_SIZE to be exact. */
3132 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003133
Gilles Peskine8817f612018-12-18 00:18:46 +01003134 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003135
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003136 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003137 psa_set_key_algorithm( &attributes, alg );
3138 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003139
Ronald Cron5425a212020-08-04 14:58:35 +02003140 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3141 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003142
Gilles Peskine8b356b52020-08-25 23:44:59 +02003143 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3144 {
3145 const size_t output_size = output_sizes_to_test[i];
3146 psa_status_t expected_status =
3147 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3148 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003149
Gilles Peskine8b356b52020-08-25 23:44:59 +02003150 test_set_step( output_size );
3151 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003152
Gilles Peskine8b356b52020-08-25 23:44:59 +02003153 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003154 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003155 PSA_ASSERT( psa_mac_update( &operation,
3156 input->x, input->len ) );
3157 TEST_EQUAL( psa_mac_sign_finish( &operation,
3158 actual_mac, output_size,
3159 &mac_length ),
3160 expected_status );
3161 PSA_ASSERT( psa_mac_abort( &operation ) );
3162
3163 if( expected_status == PSA_SUCCESS )
3164 {
3165 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3166 actual_mac, mac_length );
3167 }
3168 mbedtls_free( actual_mac );
3169 actual_mac = NULL;
3170 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003171
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003172exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003173 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003174 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003175 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003176 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003177}
3178/* END_CASE */
3179
3180/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003181void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003182 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003183 int alg_arg,
3184 data_t *input,
3185 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003186{
Ronald Cron5425a212020-08-04 14:58:35 +02003187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003188 psa_key_type_t key_type = key_type_arg;
3189 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003190 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003192 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003193
Gilles Peskine69c12672018-06-28 00:07:19 +02003194 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3195
Gilles Peskine8817f612018-12-18 00:18:46 +01003196 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003197
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003198 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003199 psa_set_key_algorithm( &attributes, alg );
3200 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003201
Ronald Cron5425a212020-08-04 14:58:35 +02003202 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3203 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003204
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003205 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003206 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003207 PSA_ASSERT( psa_mac_update( &operation,
3208 input->x, input->len ) );
3209 PSA_ASSERT( psa_mac_verify_finish( &operation,
3210 expected_mac->x,
3211 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003212
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003213 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003214 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003215 PSA_ASSERT( psa_mac_update( &operation,
3216 input->x, input->len ) );
3217 TEST_EQUAL( psa_mac_verify_finish( &operation,
3218 expected_mac->x,
3219 expected_mac->len - 1 ),
3220 PSA_ERROR_INVALID_SIGNATURE );
3221
3222 /* Test a MAC that's too long. */
3223 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3224 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003225 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003226 PSA_ASSERT( psa_mac_update( &operation,
3227 input->x, input->len ) );
3228 TEST_EQUAL( psa_mac_verify_finish( &operation,
3229 perturbed_mac,
3230 expected_mac->len + 1 ),
3231 PSA_ERROR_INVALID_SIGNATURE );
3232
3233 /* Test changing one byte. */
3234 for( size_t i = 0; i < expected_mac->len; i++ )
3235 {
3236 test_set_step( i );
3237 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003238 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003239 PSA_ASSERT( psa_mac_update( &operation,
3240 input->x, input->len ) );
3241 TEST_EQUAL( psa_mac_verify_finish( &operation,
3242 perturbed_mac,
3243 expected_mac->len ),
3244 PSA_ERROR_INVALID_SIGNATURE );
3245 perturbed_mac[i] ^= 1;
3246 }
3247
Gilles Peskine8c9def32018-02-08 10:02:12 +01003248exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003249 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003250 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003251 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003252 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003253}
3254/* END_CASE */
3255
3256/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003257void cipher_operation_init( )
3258{
Jaeden Ameroab439972019-02-15 14:12:05 +00003259 const uint8_t input[1] = { 0 };
3260 unsigned char output[1] = { 0 };
3261 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003262 /* Test each valid way of initializing the object, except for `= {0}`, as
3263 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3264 * though it's OK by the C standard. We could test for this, but we'd need
3265 * to supress the Clang warning for the test. */
3266 psa_cipher_operation_t func = psa_cipher_operation_init( );
3267 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3268 psa_cipher_operation_t zero;
3269
3270 memset( &zero, 0, sizeof( zero ) );
3271
Jaeden Ameroab439972019-02-15 14:12:05 +00003272 /* A freshly-initialized cipher operation should not be usable. */
3273 TEST_EQUAL( psa_cipher_update( &func,
3274 input, sizeof( input ),
3275 output, sizeof( output ),
3276 &output_length ),
3277 PSA_ERROR_BAD_STATE );
3278 TEST_EQUAL( psa_cipher_update( &init,
3279 input, sizeof( input ),
3280 output, sizeof( output ),
3281 &output_length ),
3282 PSA_ERROR_BAD_STATE );
3283 TEST_EQUAL( psa_cipher_update( &zero,
3284 input, sizeof( input ),
3285 output, sizeof( output ),
3286 &output_length ),
3287 PSA_ERROR_BAD_STATE );
3288
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003289 /* A default cipher operation should be abortable without error. */
3290 PSA_ASSERT( psa_cipher_abort( &func ) );
3291 PSA_ASSERT( psa_cipher_abort( &init ) );
3292 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003293}
3294/* END_CASE */
3295
3296/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003297void cipher_setup( int key_type_arg,
3298 data_t *key,
3299 int alg_arg,
3300 int expected_status_arg )
3301{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003302 psa_key_type_t key_type = key_type_arg;
3303 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003304 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003305 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003306 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003307#if defined(KNOWN_SUPPORTED_MAC_ALG)
3308 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3309#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003310
Gilles Peskine8817f612018-12-18 00:18:46 +01003311 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003312
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003313 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3314 &operation, &status ) )
3315 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003316 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003317
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003318 /* The operation object should be reusable. */
3319#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3320 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3321 smoke_test_key_data,
3322 sizeof( smoke_test_key_data ),
3323 KNOWN_SUPPORTED_CIPHER_ALG,
3324 &operation, &status ) )
3325 goto exit;
3326 TEST_EQUAL( status, PSA_SUCCESS );
3327#endif
3328
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003329exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003330 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003331 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003332}
3333/* END_CASE */
3334
3335/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003336void cipher_bad_order( )
3337{
Ronald Cron5425a212020-08-04 14:58:35 +02003338 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003339 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3340 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003342 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3343 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003344 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003345 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3346 0xaa, 0xaa, 0xaa, 0xaa };
3347 const uint8_t text[] = {
3348 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3349 0xbb, 0xbb, 0xbb, 0xbb };
3350 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3351 size_t length = 0;
3352
3353 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003354 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3355 psa_set_key_algorithm( &attributes, alg );
3356 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003357 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3358 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003359
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003360 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003361 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3362 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003363 PSA_ERROR_BAD_STATE );
3364 PSA_ASSERT( psa_cipher_abort( &operation ) );
3365
3366 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003367 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3368 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003369 PSA_ERROR_BAD_STATE );
3370 PSA_ASSERT( psa_cipher_abort( &operation ) );
3371
Jaeden Ameroab439972019-02-15 14:12:05 +00003372 /* Generate an IV without calling setup beforehand. */
3373 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3374 buffer, sizeof( buffer ),
3375 &length ),
3376 PSA_ERROR_BAD_STATE );
3377 PSA_ASSERT( psa_cipher_abort( &operation ) );
3378
3379 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003380 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003381 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3382 buffer, sizeof( buffer ),
3383 &length ) );
3384 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3385 buffer, sizeof( buffer ),
3386 &length ),
3387 PSA_ERROR_BAD_STATE );
3388 PSA_ASSERT( psa_cipher_abort( &operation ) );
3389
3390 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003391 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003392 PSA_ASSERT( psa_cipher_set_iv( &operation,
3393 iv, sizeof( iv ) ) );
3394 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3395 buffer, sizeof( buffer ),
3396 &length ),
3397 PSA_ERROR_BAD_STATE );
3398 PSA_ASSERT( psa_cipher_abort( &operation ) );
3399
3400 /* Set an IV without calling setup beforehand. */
3401 TEST_EQUAL( psa_cipher_set_iv( &operation,
3402 iv, sizeof( iv ) ),
3403 PSA_ERROR_BAD_STATE );
3404 PSA_ASSERT( psa_cipher_abort( &operation ) );
3405
3406 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003407 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003408 PSA_ASSERT( psa_cipher_set_iv( &operation,
3409 iv, sizeof( iv ) ) );
3410 TEST_EQUAL( psa_cipher_set_iv( &operation,
3411 iv, sizeof( iv ) ),
3412 PSA_ERROR_BAD_STATE );
3413 PSA_ASSERT( psa_cipher_abort( &operation ) );
3414
3415 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003416 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003417 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3418 buffer, sizeof( buffer ),
3419 &length ) );
3420 TEST_EQUAL( psa_cipher_set_iv( &operation,
3421 iv, sizeof( iv ) ),
3422 PSA_ERROR_BAD_STATE );
3423 PSA_ASSERT( psa_cipher_abort( &operation ) );
3424
3425 /* Call update without calling setup beforehand. */
3426 TEST_EQUAL( psa_cipher_update( &operation,
3427 text, sizeof( text ),
3428 buffer, sizeof( buffer ),
3429 &length ),
3430 PSA_ERROR_BAD_STATE );
3431 PSA_ASSERT( psa_cipher_abort( &operation ) );
3432
3433 /* Call update without an IV where an IV is required. */
3434 TEST_EQUAL( psa_cipher_update( &operation,
3435 text, sizeof( text ),
3436 buffer, sizeof( buffer ),
3437 &length ),
3438 PSA_ERROR_BAD_STATE );
3439 PSA_ASSERT( psa_cipher_abort( &operation ) );
3440
3441 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003442 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003443 PSA_ASSERT( psa_cipher_set_iv( &operation,
3444 iv, sizeof( iv ) ) );
3445 PSA_ASSERT( psa_cipher_finish( &operation,
3446 buffer, sizeof( buffer ), &length ) );
3447 TEST_EQUAL( psa_cipher_update( &operation,
3448 text, sizeof( text ),
3449 buffer, sizeof( buffer ),
3450 &length ),
3451 PSA_ERROR_BAD_STATE );
3452 PSA_ASSERT( psa_cipher_abort( &operation ) );
3453
3454 /* Call finish without calling setup beforehand. */
3455 TEST_EQUAL( psa_cipher_finish( &operation,
3456 buffer, sizeof( buffer ), &length ),
3457 PSA_ERROR_BAD_STATE );
3458 PSA_ASSERT( psa_cipher_abort( &operation ) );
3459
3460 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003461 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003462 /* Not calling update means we are encrypting an empty buffer, which is OK
3463 * for cipher modes with padding. */
3464 TEST_EQUAL( psa_cipher_finish( &operation,
3465 buffer, sizeof( buffer ), &length ),
3466 PSA_ERROR_BAD_STATE );
3467 PSA_ASSERT( psa_cipher_abort( &operation ) );
3468
3469 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003470 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003471 PSA_ASSERT( psa_cipher_set_iv( &operation,
3472 iv, sizeof( iv ) ) );
3473 PSA_ASSERT( psa_cipher_finish( &operation,
3474 buffer, sizeof( buffer ), &length ) );
3475 TEST_EQUAL( psa_cipher_finish( &operation,
3476 buffer, sizeof( buffer ), &length ),
3477 PSA_ERROR_BAD_STATE );
3478 PSA_ASSERT( psa_cipher_abort( &operation ) );
3479
Ronald Cron5425a212020-08-04 14:58:35 +02003480 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003481
Jaeden Ameroab439972019-02-15 14:12:05 +00003482exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003483 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003484 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003485}
3486/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003487
Gilles Peskine50e586b2018-06-08 14:28:46 +02003488/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003489void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003490 data_t *key_data, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003491 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003492 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003493{
Ronald Cron5425a212020-08-04 14:58:35 +02003494 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003495 psa_status_t status;
3496 psa_key_type_t key_type = key_type_arg;
3497 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003498 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003499 unsigned char *output = NULL;
3500 size_t output_buffer_size = 0;
3501 size_t function_output_length = 0;
3502 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003503 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003505
Gilles Peskine8817f612018-12-18 00:18:46 +01003506 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003507
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003508 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3509 psa_set_key_algorithm( &attributes, alg );
3510 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003511
Ronald Cron5425a212020-08-04 14:58:35 +02003512 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3513 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003514
Ronald Cron5425a212020-08-04 14:58:35 +02003515 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003516
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003517 if( iv->len > 0 )
3518 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003519 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003520 }
3521
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003522 output_buffer_size = ( (size_t) input->len +
3523 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003524 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003525
Gilles Peskine8817f612018-12-18 00:18:46 +01003526 PSA_ASSERT( psa_cipher_update( &operation,
3527 input->x, input->len,
3528 output, output_buffer_size,
3529 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003530 total_output_length += function_output_length;
3531 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003532 output + total_output_length,
3533 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003534 &function_output_length );
3535 total_output_length += function_output_length;
3536
Gilles Peskinefe11b722018-12-18 00:24:04 +01003537 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003538 if( expected_status == PSA_SUCCESS )
3539 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003541 ASSERT_COMPARE( expected_output->x, expected_output->len,
3542 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003543 }
3544
3545exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003546 psa_cipher_abort( &operation );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003547 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003548 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003549 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003550}
3551/* END_CASE */
3552
3553/* BEGIN_CASE */
3554void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003555 data_t *key_data, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003556 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003557 int first_part_size_arg,
3558 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003559 data_t *expected_output )
3560{
Ronald Cron5425a212020-08-04 14:58:35 +02003561 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003562 psa_key_type_t key_type = key_type_arg;
3563 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003564 size_t first_part_size = first_part_size_arg;
3565 size_t output1_length = output1_length_arg;
3566 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003567 unsigned char *output = NULL;
3568 size_t output_buffer_size = 0;
3569 size_t function_output_length = 0;
3570 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003571 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003572 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003573
Gilles Peskine8817f612018-12-18 00:18:46 +01003574 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003575
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003576 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3577 psa_set_key_algorithm( &attributes, alg );
3578 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003579
Ronald Cron5425a212020-08-04 14:58:35 +02003580 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3581 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003582
Ronald Cron5425a212020-08-04 14:58:35 +02003583 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003584
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003585 if( iv->len > 0 )
3586 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003587 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003588 }
3589
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003590 output_buffer_size = ( (size_t) input->len +
3591 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003592 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003593
Gilles Peskinee0866522019-02-19 19:44:00 +01003594 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003595 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3596 output, output_buffer_size,
3597 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003598 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003599 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003600 PSA_ASSERT( psa_cipher_update( &operation,
3601 input->x + first_part_size,
3602 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003603 output + total_output_length,
3604 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003606 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003607 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003608 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003609 output + total_output_length,
3610 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003611 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003612 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003614
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003615 ASSERT_COMPARE( expected_output->x, expected_output->len,
3616 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003617
3618exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003619 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003620 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003621 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003622 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003623}
3624/* END_CASE */
3625
3626/* BEGIN_CASE */
3627void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003628 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003629 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003630 int first_part_size_arg,
3631 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003632 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003633{
Ronald Cron5425a212020-08-04 14:58:35 +02003634 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003635 psa_key_type_t key_type = key_type_arg;
3636 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003637 size_t first_part_size = first_part_size_arg;
3638 size_t output1_length = output1_length_arg;
3639 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003640 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003641 size_t output_buffer_size = 0;
3642 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003643 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003644 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003646
Gilles Peskine8817f612018-12-18 00:18:46 +01003647 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003648
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003649 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3650 psa_set_key_algorithm( &attributes, alg );
3651 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003652
Ronald Cron5425a212020-08-04 14:58:35 +02003653 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3654 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003655
Ronald Cron5425a212020-08-04 14:58:35 +02003656 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003657
Steven Cooreman177deba2020-09-07 17:14:14 +02003658 if( iv->len > 0 )
3659 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003660 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003661 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003662
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003663 output_buffer_size = ( (size_t) input->len +
3664 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003665 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003666
Gilles Peskinee0866522019-02-19 19:44:00 +01003667 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003668 PSA_ASSERT( psa_cipher_update( &operation,
3669 input->x, first_part_size,
3670 output, output_buffer_size,
3671 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003672 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003673 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003674 PSA_ASSERT( psa_cipher_update( &operation,
3675 input->x + first_part_size,
3676 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003677 output + total_output_length,
3678 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003679 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003680 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003681 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003682 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003683 output + total_output_length,
3684 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003685 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003686 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003687 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003688
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003689 ASSERT_COMPARE( expected_output->x, expected_output->len,
3690 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003691
3692exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003693 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003694 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003695 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003696 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003697}
3698/* END_CASE */
3699
Gilles Peskine50e586b2018-06-08 14:28:46 +02003700/* BEGIN_CASE */
3701void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003702 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003703 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003704 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003705{
Ronald Cron5425a212020-08-04 14:58:35 +02003706 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003707 psa_status_t status;
3708 psa_key_type_t key_type = key_type_arg;
3709 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003710 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003711 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003712 size_t output_buffer_size = 0;
3713 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003714 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003715 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003716 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003717
Gilles Peskine8817f612018-12-18 00:18:46 +01003718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003719
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003720 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3721 psa_set_key_algorithm( &attributes, alg );
3722 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003723
Ronald Cron5425a212020-08-04 14:58:35 +02003724 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3725 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003726
Ronald Cron5425a212020-08-04 14:58:35 +02003727 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003728
Steven Cooreman177deba2020-09-07 17:14:14 +02003729 if( iv->len > 0 )
3730 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003731 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003732 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003733
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003734 output_buffer_size = ( (size_t) input->len +
3735 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003736 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003737
Gilles Peskine8817f612018-12-18 00:18:46 +01003738 PSA_ASSERT( psa_cipher_update( &operation,
3739 input->x, input->len,
3740 output, output_buffer_size,
3741 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003742 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003743 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003744 output + total_output_length,
3745 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003746 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003747 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003748 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003749
3750 if( expected_status == PSA_SUCCESS )
3751 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003752 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003753 ASSERT_COMPARE( expected_output->x, expected_output->len,
3754 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003755 }
3756
Gilles Peskine50e586b2018-06-08 14:28:46 +02003757exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003758 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003759 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003760 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003761 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003762}
3763/* END_CASE */
3764
Gilles Peskine50e586b2018-06-08 14:28:46 +02003765/* BEGIN_CASE */
3766void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003767 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003768 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003769{
Ronald Cron5425a212020-08-04 14:58:35 +02003770 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003771 psa_key_type_t key_type = key_type_arg;
3772 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003773 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003774 size_t iv_size = 16;
3775 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003776 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003777 size_t output1_size = 0;
3778 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003779 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003780 size_t output2_size = 0;
3781 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003782 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003783 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3784 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003786
Gilles Peskine8817f612018-12-18 00:18:46 +01003787 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003788
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003789 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3790 psa_set_key_algorithm( &attributes, alg );
3791 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003792
Ronald Cron5425a212020-08-04 14:58:35 +02003793 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3794 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003795
Ronald Cron5425a212020-08-04 14:58:35 +02003796 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3797 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003798
Steven Cooreman177deba2020-09-07 17:14:14 +02003799 if( alg != PSA_ALG_ECB_NO_PADDING )
3800 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003801 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3802 iv, iv_size,
3803 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003804 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003805 output1_size = ( (size_t) input->len +
3806 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003807 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003808
Gilles Peskine8817f612018-12-18 00:18:46 +01003809 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3810 output1, output1_size,
3811 &output1_length ) );
3812 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003813 output1 + output1_length,
3814 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003815 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003816
Gilles Peskine048b7f02018-06-08 14:20:49 +02003817 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003818
Gilles Peskine8817f612018-12-18 00:18:46 +01003819 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003820
3821 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003822 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003823
Steven Cooreman177deba2020-09-07 17:14:14 +02003824 if( iv_length > 0 )
3825 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003826 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3827 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003828 }
3829
Gilles Peskine8817f612018-12-18 00:18:46 +01003830 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3831 output2, output2_size,
3832 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003833 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003834 PSA_ASSERT( psa_cipher_finish( &operation2,
3835 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003836 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003837 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003838
Gilles Peskine048b7f02018-06-08 14:20:49 +02003839 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003840
Gilles Peskine8817f612018-12-18 00:18:46 +01003841 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003842
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003843 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003844
3845exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003846 psa_cipher_abort( &operation1 );
3847 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003848 mbedtls_free( output1 );
3849 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003850 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003851 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003852}
3853/* END_CASE */
3854
3855/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003856void cipher_verify_output_multipart( int alg_arg,
3857 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003858 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003859 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003860 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003861{
Ronald Cron5425a212020-08-04 14:58:35 +02003862 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003863 psa_key_type_t key_type = key_type_arg;
3864 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003865 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003866 unsigned char iv[16] = {0};
3867 size_t iv_size = 16;
3868 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003869 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003870 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003871 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003872 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003873 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003874 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003875 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003876 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3877 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003879
Gilles Peskine8817f612018-12-18 00:18:46 +01003880 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003881
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003882 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3883 psa_set_key_algorithm( &attributes, alg );
3884 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003885
Ronald Cron5425a212020-08-04 14:58:35 +02003886 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3887 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003888
Ronald Cron5425a212020-08-04 14:58:35 +02003889 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3890 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003891
Steven Cooreman177deba2020-09-07 17:14:14 +02003892 if( alg != PSA_ALG_ECB_NO_PADDING )
3893 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003894 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3895 iv, iv_size,
3896 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003897 }
3898
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003899 output1_buffer_size = ( (size_t) input->len +
3900 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003901 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003902
Gilles Peskinee0866522019-02-19 19:44:00 +01003903 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003904
Gilles Peskine8817f612018-12-18 00:18:46 +01003905 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3906 output1, output1_buffer_size,
3907 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003908 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003909
Gilles Peskine8817f612018-12-18 00:18:46 +01003910 PSA_ASSERT( psa_cipher_update( &operation1,
3911 input->x + first_part_size,
3912 input->len - first_part_size,
3913 output1, output1_buffer_size,
3914 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003915 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003916
Gilles Peskine8817f612018-12-18 00:18:46 +01003917 PSA_ASSERT( psa_cipher_finish( &operation1,
3918 output1 + output1_length,
3919 output1_buffer_size - output1_length,
3920 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003921 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003922
Gilles Peskine8817f612018-12-18 00:18:46 +01003923 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003924
Gilles Peskine048b7f02018-06-08 14:20:49 +02003925 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003926 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003927
Steven Cooreman177deba2020-09-07 17:14:14 +02003928 if( iv_length > 0 )
3929 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003930 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3931 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003932 }
Moran Pekerded84402018-06-06 16:36:50 +03003933
Gilles Peskine8817f612018-12-18 00:18:46 +01003934 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3935 output2, output2_buffer_size,
3936 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003937 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003938
Gilles Peskine8817f612018-12-18 00:18:46 +01003939 PSA_ASSERT( psa_cipher_update( &operation2,
3940 output1 + first_part_size,
3941 output1_length - first_part_size,
3942 output2, output2_buffer_size,
3943 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003944 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003945
Gilles Peskine8817f612018-12-18 00:18:46 +01003946 PSA_ASSERT( psa_cipher_finish( &operation2,
3947 output2 + output2_length,
3948 output2_buffer_size - output2_length,
3949 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003950 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003951
Gilles Peskine8817f612018-12-18 00:18:46 +01003952 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003953
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003954 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003955
3956exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003957 psa_cipher_abort( &operation1 );
3958 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003959 mbedtls_free( output1 );
3960 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003961 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003962 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003963}
3964/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003965
Gilles Peskine20035e32018-02-03 22:44:14 +01003966/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003967void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003968 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003969 data_t *nonce,
3970 data_t *additional_data,
3971 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003972 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003973{
Ronald Cron5425a212020-08-04 14:58:35 +02003974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003975 psa_key_type_t key_type = key_type_arg;
3976 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003977 unsigned char *output_data = NULL;
3978 size_t output_size = 0;
3979 size_t output_length = 0;
3980 unsigned char *output_data2 = NULL;
3981 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003982 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003983 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003984 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003985
Gilles Peskine4abf7412018-06-18 16:35:34 +02003986 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003987 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3988 * should be exact. */
3989 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3990 TEST_EQUAL( output_size,
3991 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003992 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003993
Gilles Peskine8817f612018-12-18 00:18:46 +01003994 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003995
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3997 psa_set_key_algorithm( &attributes, alg );
3998 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003999
Gilles Peskine049c7532019-05-15 20:22:09 +02004000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004001 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004002
Ronald Cron5425a212020-08-04 14:58:35 +02004003 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004004 nonce->x, nonce->len,
4005 additional_data->x,
4006 additional_data->len,
4007 input_data->x, input_data->len,
4008 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004009 &output_length ),
4010 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004011
4012 if( PSA_SUCCESS == expected_result )
4013 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004014 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004015
Gilles Peskine003a4a92019-05-14 16:09:40 +02004016 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4017 * should be exact. */
4018 TEST_EQUAL( input_data->len,
4019 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
4020
Ronald Cron5425a212020-08-04 14:58:35 +02004021 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004022 nonce->x, nonce->len,
4023 additional_data->x,
4024 additional_data->len,
4025 output_data, output_length,
4026 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004027 &output_length2 ),
4028 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004029
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004030 ASSERT_COMPARE( input_data->x, input_data->len,
4031 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004032 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004033
Gilles Peskinea1cac842018-06-11 19:33:02 +02004034exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004035 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004036 mbedtls_free( output_data );
4037 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004038 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004039}
4040/* END_CASE */
4041
4042/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004043void aead_encrypt( int key_type_arg, data_t *key_data,
4044 int alg_arg,
4045 data_t *nonce,
4046 data_t *additional_data,
4047 data_t *input_data,
4048 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004049{
Ronald Cron5425a212020-08-04 14:58:35 +02004050 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004051 psa_key_type_t key_type = key_type_arg;
4052 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004053 unsigned char *output_data = NULL;
4054 size_t output_size = 0;
4055 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004056 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004057 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004058
Gilles Peskine4abf7412018-06-18 16:35:34 +02004059 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004060 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4061 * should be exact. */
4062 TEST_EQUAL( output_size,
4063 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004064 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004065
Gilles Peskine8817f612018-12-18 00:18:46 +01004066 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004067
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004068 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4069 psa_set_key_algorithm( &attributes, alg );
4070 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004071
Gilles Peskine049c7532019-05-15 20:22:09 +02004072 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004073 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004074
Ronald Cron5425a212020-08-04 14:58:35 +02004075 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004076 nonce->x, nonce->len,
4077 additional_data->x, additional_data->len,
4078 input_data->x, input_data->len,
4079 output_data, output_size,
4080 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004081
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004082 ASSERT_COMPARE( expected_result->x, expected_result->len,
4083 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004084
Gilles Peskinea1cac842018-06-11 19:33:02 +02004085exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004086 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004087 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004088 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004089}
4090/* END_CASE */
4091
4092/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004093void aead_decrypt( int key_type_arg, data_t *key_data,
4094 int alg_arg,
4095 data_t *nonce,
4096 data_t *additional_data,
4097 data_t *input_data,
4098 data_t *expected_data,
4099 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004100{
Ronald Cron5425a212020-08-04 14:58:35 +02004101 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004102 psa_key_type_t key_type = key_type_arg;
4103 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004104 unsigned char *output_data = NULL;
4105 size_t output_size = 0;
4106 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004107 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004109 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004110
Gilles Peskine003a4a92019-05-14 16:09:40 +02004111 output_size = input_data->len - tag_length;
4112 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4113 * should be exact. */
4114 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4115 TEST_EQUAL( output_size,
4116 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004117 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004118
Gilles Peskine8817f612018-12-18 00:18:46 +01004119 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004120
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004121 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4122 psa_set_key_algorithm( &attributes, alg );
4123 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004124
Gilles Peskine049c7532019-05-15 20:22:09 +02004125 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004126 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004127
Ronald Cron5425a212020-08-04 14:58:35 +02004128 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004129 nonce->x, nonce->len,
4130 additional_data->x,
4131 additional_data->len,
4132 input_data->x, input_data->len,
4133 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004134 &output_length ),
4135 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004136
Gilles Peskine2d277862018-06-18 15:41:12 +02004137 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004138 ASSERT_COMPARE( expected_data->x, expected_data->len,
4139 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004140
Gilles Peskinea1cac842018-06-11 19:33:02 +02004141exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004142 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004143 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004144 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004145}
4146/* END_CASE */
4147
4148/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004149void signature_size( int type_arg,
4150 int bits,
4151 int alg_arg,
4152 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004153{
4154 psa_key_type_t type = type_arg;
4155 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004156 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004157
Gilles Peskinefe11b722018-12-18 00:24:04 +01004158 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004159#if defined(MBEDTLS_TEST_DEPRECATED)
4160 TEST_EQUAL( actual_size,
4161 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4162#endif /* MBEDTLS_TEST_DEPRECATED */
4163
Gilles Peskinee59236f2018-01-27 23:32:46 +01004164exit:
4165 ;
4166}
4167/* END_CASE */
4168
4169/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004170void sign_deterministic( int key_type_arg, data_t *key_data,
4171 int alg_arg, data_t *input_data,
4172 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004173{
Ronald Cron5425a212020-08-04 14:58:35 +02004174 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004175 psa_key_type_t key_type = key_type_arg;
4176 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004177 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004178 unsigned char *signature = NULL;
4179 size_t signature_size;
4180 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004181 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004182
Gilles Peskine8817f612018-12-18 00:18:46 +01004183 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004184
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004185 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004186 psa_set_key_algorithm( &attributes, alg );
4187 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004188
Gilles Peskine049c7532019-05-15 20:22:09 +02004189 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004190 &key ) );
4191 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004192 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004193
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004194 /* Allocate a buffer which has the size advertized by the
4195 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004196 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004197 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004198 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004199 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004200 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004201
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004202 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004203 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004204 input_data->x, input_data->len,
4205 signature, signature_size,
4206 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004207 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004208 ASSERT_COMPARE( output_data->x, output_data->len,
4209 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004210
Gilles Peskine0627f982019-11-26 19:12:16 +01004211#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004212 memset( signature, 0, signature_size );
4213 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004214 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004215 input_data->x, input_data->len,
4216 signature, signature_size,
4217 &signature_length ) );
4218 ASSERT_COMPARE( output_data->x, output_data->len,
4219 signature, signature_length );
4220#endif /* MBEDTLS_TEST_DEPRECATED */
4221
Gilles Peskine20035e32018-02-03 22:44:14 +01004222exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004223 /*
4224 * Key attributes may have been returned by psa_get_key_attributes()
4225 * thus reset them as required.
4226 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004227 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004228
Ronald Cron5425a212020-08-04 14:58:35 +02004229 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004230 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004231 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004232}
4233/* END_CASE */
4234
4235/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004236void sign_fail( int key_type_arg, data_t *key_data,
4237 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004238 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004239{
Ronald Cron5425a212020-08-04 14:58:35 +02004240 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004241 psa_key_type_t key_type = key_type_arg;
4242 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004243 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004244 psa_status_t actual_status;
4245 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004246 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004247 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004249
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004250 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004251
Gilles Peskine8817f612018-12-18 00:18:46 +01004252 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004253
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004254 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004255 psa_set_key_algorithm( &attributes, alg );
4256 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004257
Gilles Peskine049c7532019-05-15 20:22:09 +02004258 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004259 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004260
Ronald Cron5425a212020-08-04 14:58:35 +02004261 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004262 input_data->x, input_data->len,
4263 signature, signature_size,
4264 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004265 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004266 /* The value of *signature_length is unspecified on error, but
4267 * whatever it is, it should be less than signature_size, so that
4268 * if the caller tries to read *signature_length bytes without
4269 * checking the error code then they don't overflow a buffer. */
4270 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004271
Gilles Peskine895242b2019-11-29 12:15:40 +01004272#if defined(MBEDTLS_TEST_DEPRECATED)
4273 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004274 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004275 input_data->x, input_data->len,
4276 signature, signature_size,
4277 &signature_length ),
4278 expected_status );
4279 TEST_ASSERT( signature_length <= signature_size );
4280#endif /* MBEDTLS_TEST_DEPRECATED */
4281
Gilles Peskine20035e32018-02-03 22:44:14 +01004282exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004283 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004284 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004285 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004286 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004287}
4288/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004289
4290/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004291void sign_verify( int key_type_arg, data_t *key_data,
4292 int alg_arg, data_t *input_data )
4293{
Ronald Cron5425a212020-08-04 14:58:35 +02004294 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004295 psa_key_type_t key_type = key_type_arg;
4296 psa_algorithm_t alg = alg_arg;
4297 size_t key_bits;
4298 unsigned char *signature = NULL;
4299 size_t signature_size;
4300 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004301 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004302
Gilles Peskine8817f612018-12-18 00:18:46 +01004303 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004304
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004305 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004306 psa_set_key_algorithm( &attributes, alg );
4307 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004308
Gilles Peskine049c7532019-05-15 20:22:09 +02004309 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004310 &key ) );
4311 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004312 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004313
4314 /* Allocate a buffer which has the size advertized by the
4315 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004316 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004317 key_bits, alg );
4318 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004319 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004320 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004321
4322 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004323 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004324 input_data->x, input_data->len,
4325 signature, signature_size,
4326 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004327 /* Check that the signature length looks sensible. */
4328 TEST_ASSERT( signature_length <= signature_size );
4329 TEST_ASSERT( signature_length > 0 );
4330
4331 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004332 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004333 input_data->x, input_data->len,
4334 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004335
4336 if( input_data->len != 0 )
4337 {
4338 /* Flip a bit in the input and verify that the signature is now
4339 * detected as invalid. Flip a bit at the beginning, not at the end,
4340 * because ECDSA may ignore the last few bits of the input. */
4341 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004342 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004343 input_data->x, input_data->len,
4344 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004345 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004346 }
4347
4348exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004349 /*
4350 * Key attributes may have been returned by psa_get_key_attributes()
4351 * thus reset them as required.
4352 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004353 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004354
Ronald Cron5425a212020-08-04 14:58:35 +02004355 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004356 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004357 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004358}
4359/* END_CASE */
4360
4361/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004362void asymmetric_verify( int key_type_arg, data_t *key_data,
4363 int alg_arg, data_t *hash_data,
4364 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004365{
Ronald Cron5425a212020-08-04 14:58:35 +02004366 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004367 psa_key_type_t key_type = key_type_arg;
4368 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004370
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004371 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004372
Gilles Peskine8817f612018-12-18 00:18:46 +01004373 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004374
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004375 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004376 psa_set_key_algorithm( &attributes, alg );
4377 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004378
Gilles Peskine049c7532019-05-15 20:22:09 +02004379 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004380 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004381
Ronald Cron5425a212020-08-04 14:58:35 +02004382 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004383 hash_data->x, hash_data->len,
4384 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004385
4386#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004387 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004388 hash_data->x, hash_data->len,
4389 signature_data->x,
4390 signature_data->len ) );
4391
4392#endif /* MBEDTLS_TEST_DEPRECATED */
4393
itayzafrir5c753392018-05-08 11:18:38 +03004394exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004395 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004396 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004397 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004398}
4399/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004400
4401/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004402void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4403 int alg_arg, data_t *hash_data,
4404 data_t *signature_data,
4405 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004406{
Ronald Cron5425a212020-08-04 14:58:35 +02004407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004408 psa_key_type_t key_type = key_type_arg;
4409 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004410 psa_status_t actual_status;
4411 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004413
Gilles Peskine8817f612018-12-18 00:18:46 +01004414 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004415
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004416 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004417 psa_set_key_algorithm( &attributes, alg );
4418 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004419
Gilles Peskine049c7532019-05-15 20:22:09 +02004420 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004421 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004422
Ronald Cron5425a212020-08-04 14:58:35 +02004423 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004424 hash_data->x, hash_data->len,
4425 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004426 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004427
Gilles Peskine895242b2019-11-29 12:15:40 +01004428#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004429 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004430 hash_data->x, hash_data->len,
4431 signature_data->x, signature_data->len ),
4432 expected_status );
4433#endif /* MBEDTLS_TEST_DEPRECATED */
4434
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004435exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004436 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004437 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004438 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004439}
4440/* END_CASE */
4441
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004442/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004443void asymmetric_encrypt( int key_type_arg,
4444 data_t *key_data,
4445 int alg_arg,
4446 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004447 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004448 int expected_output_length_arg,
4449 int expected_status_arg )
4450{
Ronald Cron5425a212020-08-04 14:58:35 +02004451 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004452 psa_key_type_t key_type = key_type_arg;
4453 psa_algorithm_t alg = alg_arg;
4454 size_t expected_output_length = expected_output_length_arg;
4455 size_t key_bits;
4456 unsigned char *output = NULL;
4457 size_t output_size;
4458 size_t output_length = ~0;
4459 psa_status_t actual_status;
4460 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004462
Gilles Peskine8817f612018-12-18 00:18:46 +01004463 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004464
Gilles Peskine656896e2018-06-29 19:12:28 +02004465 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004466 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4467 psa_set_key_algorithm( &attributes, alg );
4468 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004469 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004470 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004471
4472 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004473 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004474 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004475 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004476 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004477
4478 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004479 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004480 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004481 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004482 output, output_size,
4483 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004484 TEST_EQUAL( actual_status, expected_status );
4485 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004486
Gilles Peskine68428122018-06-30 18:42:41 +02004487 /* If the label is empty, the test framework puts a non-null pointer
4488 * in label->x. Test that a null pointer works as well. */
4489 if( label->len == 0 )
4490 {
4491 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004492 if( output_size != 0 )
4493 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004494 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004495 input_data->x, input_data->len,
4496 NULL, label->len,
4497 output, output_size,
4498 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004499 TEST_EQUAL( actual_status, expected_status );
4500 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004501 }
4502
Gilles Peskine656896e2018-06-29 19:12:28 +02004503exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004504 /*
4505 * Key attributes may have been returned by psa_get_key_attributes()
4506 * thus reset them as required.
4507 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004508 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004509
Ronald Cron5425a212020-08-04 14:58:35 +02004510 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004511 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004512 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004513}
4514/* END_CASE */
4515
4516/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004517void asymmetric_encrypt_decrypt( int key_type_arg,
4518 data_t *key_data,
4519 int alg_arg,
4520 data_t *input_data,
4521 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004522{
Ronald Cron5425a212020-08-04 14:58:35 +02004523 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004524 psa_key_type_t key_type = key_type_arg;
4525 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004526 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004527 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004528 size_t output_size;
4529 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004530 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004531 size_t output2_size;
4532 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004534
Gilles Peskine8817f612018-12-18 00:18:46 +01004535 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004536
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004537 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4538 psa_set_key_algorithm( &attributes, alg );
4539 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004540
Gilles Peskine049c7532019-05-15 20:22:09 +02004541 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004542 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004543
4544 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004545 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004546 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004547 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004548 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004549 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004550 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004551
Gilles Peskineeebd7382018-06-08 18:11:54 +02004552 /* We test encryption by checking that encrypt-then-decrypt gives back
4553 * the original plaintext because of the non-optional random
4554 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004555 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004556 input_data->x, input_data->len,
4557 label->x, label->len,
4558 output, output_size,
4559 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004560 /* We don't know what ciphertext length to expect, but check that
4561 * it looks sensible. */
4562 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004563
Ronald Cron5425a212020-08-04 14:58:35 +02004564 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004565 output, output_length,
4566 label->x, label->len,
4567 output2, output2_size,
4568 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004569 ASSERT_COMPARE( input_data->x, input_data->len,
4570 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004571
4572exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004573 /*
4574 * Key attributes may have been returned by psa_get_key_attributes()
4575 * thus reset them as required.
4576 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004577 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004578
Ronald Cron5425a212020-08-04 14:58:35 +02004579 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004580 mbedtls_free( output );
4581 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004582 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004583}
4584/* END_CASE */
4585
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004586/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004587void asymmetric_decrypt( int key_type_arg,
4588 data_t *key_data,
4589 int alg_arg,
4590 data_t *input_data,
4591 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004592 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004593{
Ronald Cron5425a212020-08-04 14:58:35 +02004594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004595 psa_key_type_t key_type = key_type_arg;
4596 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004597 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004598 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004599 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004600 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004601
Jaeden Amero412654a2019-02-06 12:57:46 +00004602 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004603 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004604
Gilles Peskine8817f612018-12-18 00:18:46 +01004605 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004606
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4608 psa_set_key_algorithm( &attributes, alg );
4609 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004610
Gilles Peskine049c7532019-05-15 20:22:09 +02004611 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004612 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004613
Ronald Cron5425a212020-08-04 14:58:35 +02004614 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004615 input_data->x, input_data->len,
4616 label->x, label->len,
4617 output,
4618 output_size,
4619 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004620 ASSERT_COMPARE( expected_data->x, expected_data->len,
4621 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004622
Gilles Peskine68428122018-06-30 18:42:41 +02004623 /* If the label is empty, the test framework puts a non-null pointer
4624 * in label->x. Test that a null pointer works as well. */
4625 if( label->len == 0 )
4626 {
4627 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004628 if( output_size != 0 )
4629 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004630 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004631 input_data->x, input_data->len,
4632 NULL, label->len,
4633 output,
4634 output_size,
4635 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004636 ASSERT_COMPARE( expected_data->x, expected_data->len,
4637 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004638 }
4639
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004640exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004641 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004642 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004643 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004644 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004645}
4646/* END_CASE */
4647
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004648/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004649void asymmetric_decrypt_fail( int key_type_arg,
4650 data_t *key_data,
4651 int alg_arg,
4652 data_t *input_data,
4653 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004654 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004655 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004656{
Ronald Cron5425a212020-08-04 14:58:35 +02004657 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004658 psa_key_type_t key_type = key_type_arg;
4659 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004660 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004661 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004662 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004663 psa_status_t actual_status;
4664 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004666
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004667 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004668
Gilles Peskine8817f612018-12-18 00:18:46 +01004669 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004670
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004671 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4672 psa_set_key_algorithm( &attributes, alg );
4673 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004674
Gilles Peskine049c7532019-05-15 20:22:09 +02004675 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004676 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004677
Ronald Cron5425a212020-08-04 14:58:35 +02004678 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004679 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004680 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004681 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004682 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004683 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004684 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004685
Gilles Peskine68428122018-06-30 18:42:41 +02004686 /* If the label is empty, the test framework puts a non-null pointer
4687 * in label->x. Test that a null pointer works as well. */
4688 if( label->len == 0 )
4689 {
4690 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004691 if( output_size != 0 )
4692 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004693 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004694 input_data->x, input_data->len,
4695 NULL, label->len,
4696 output, output_size,
4697 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004698 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004699 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004700 }
4701
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004702exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004703 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004704 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004705 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004706 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004707}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004708/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004709
4710/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004711void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004712{
4713 /* Test each valid way of initializing the object, except for `= {0}`, as
4714 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4715 * though it's OK by the C standard. We could test for this, but we'd need
4716 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004717 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004718 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4719 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4720 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004721
4722 memset( &zero, 0, sizeof( zero ) );
4723
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004725 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004726 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004727 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004728 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004729 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004730 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004731
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004733 PSA_ASSERT( psa_key_derivation_abort(&func) );
4734 PSA_ASSERT( psa_key_derivation_abort(&init) );
4735 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004736}
4737/* END_CASE */
4738
Janos Follath16de4a42019-06-13 16:32:24 +01004739/* BEGIN_CASE */
4740void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004741{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004742 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004743 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004745
Gilles Peskine8817f612018-12-18 00:18:46 +01004746 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004747
Janos Follath16de4a42019-06-13 16:32:24 +01004748 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004749 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004750
4751exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004753 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004754}
4755/* END_CASE */
4756
Janos Follathaf3c2a02019-06-12 12:34:34 +01004757/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004758void derive_set_capacity( int alg_arg, int capacity_arg,
4759 int expected_status_arg )
4760{
4761 psa_algorithm_t alg = alg_arg;
4762 size_t capacity = capacity_arg;
4763 psa_status_t expected_status = expected_status_arg;
4764 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4765
4766 PSA_ASSERT( psa_crypto_init( ) );
4767
4768 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4769
4770 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4771 expected_status );
4772
4773exit:
4774 psa_key_derivation_abort( &operation );
4775 PSA_DONE( );
4776}
4777/* END_CASE */
4778
4779/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004780void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004781 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004782 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004783 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004784 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004785 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004786 int expected_status_arg3,
4787 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004788{
4789 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004790 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4791 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004792 psa_status_t expected_statuses[] = {expected_status_arg1,
4793 expected_status_arg2,
4794 expected_status_arg3};
4795 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004796 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4797 MBEDTLS_SVC_KEY_ID_INIT,
4798 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004799 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4801 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004802 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004803 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004804 psa_status_t expected_output_status = expected_output_status_arg;
4805 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004806
4807 PSA_ASSERT( psa_crypto_init( ) );
4808
4809 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4810 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004811
4812 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4813
4814 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4815 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004816 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004817 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004818 psa_set_key_type( &attributes, key_types[i] );
4819 PSA_ASSERT( psa_import_key( &attributes,
4820 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004821 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004822 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4823 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4824 {
4825 // When taking a private key as secret input, use key agreement
4826 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004827 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004828 expected_statuses[i] );
4829 }
4830 else
4831 {
4832 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004833 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004834 expected_statuses[i] );
4835 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004836 }
4837 else
4838 {
4839 TEST_EQUAL( psa_key_derivation_input_bytes(
4840 &operation, steps[i],
4841 inputs[i]->x, inputs[i]->len ),
4842 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004843 }
4844 }
4845
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004846 if( output_key_type != PSA_KEY_TYPE_NONE )
4847 {
4848 psa_reset_key_attributes( &attributes );
4849 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4850 psa_set_key_bits( &attributes, 8 );
4851 actual_output_status =
4852 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004853 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004854 }
4855 else
4856 {
4857 uint8_t buffer[1];
4858 actual_output_status =
4859 psa_key_derivation_output_bytes( &operation,
4860 buffer, sizeof( buffer ) );
4861 }
4862 TEST_EQUAL( actual_output_status, expected_output_status );
4863
Janos Follathaf3c2a02019-06-12 12:34:34 +01004864exit:
4865 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004866 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4867 psa_destroy_key( keys[i] );
4868 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004869 PSA_DONE( );
4870}
4871/* END_CASE */
4872
Janos Follathd958bb72019-07-03 15:02:16 +01004873/* BEGIN_CASE */
4874void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004875{
Janos Follathd958bb72019-07-03 15:02:16 +01004876 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004877 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004878 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004879 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004880 unsigned char input1[] = "Input 1";
4881 size_t input1_length = sizeof( input1 );
4882 unsigned char input2[] = "Input 2";
4883 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004884 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004885 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004886 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4887 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4888 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004889 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004890
Gilles Peskine8817f612018-12-18 00:18:46 +01004891 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004892
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004893 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4894 psa_set_key_algorithm( &attributes, alg );
4895 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004896
Gilles Peskine73676cb2019-05-15 20:15:10 +02004897 PSA_ASSERT( psa_import_key( &attributes,
4898 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004899 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004900
4901 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004902 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004903 input1, input1_length,
4904 input2, input2_length,
4905 capacity ) )
4906 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004907
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004908 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004909 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004910 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004911
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004913
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004915 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004916
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004917exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004919 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004920 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004921}
4922/* END_CASE */
4923
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004924/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004925void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004926{
4927 uint8_t output_buffer[16];
4928 size_t buffer_size = 16;
4929 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004930 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004931
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004932 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4933 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004934 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004935
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004937 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004938
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004939 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004940
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004941 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4942 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004943 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004944
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004946 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004947
4948exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004950}
4951/* END_CASE */
4952
4953/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004954void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004955 int step1_arg, data_t *input1,
4956 int step2_arg, data_t *input2,
4957 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004958 int requested_capacity_arg,
4959 data_t *expected_output1,
4960 data_t *expected_output2 )
4961{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004962 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004963 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4964 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004965 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4966 MBEDTLS_SVC_KEY_ID_INIT,
4967 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004968 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004969 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004970 uint8_t *expected_outputs[2] =
4971 {expected_output1->x, expected_output2->x};
4972 size_t output_sizes[2] =
4973 {expected_output1->len, expected_output2->len};
4974 size_t output_buffer_size = 0;
4975 uint8_t *output_buffer = NULL;
4976 size_t expected_capacity;
4977 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004978 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004979 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004980 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004981
4982 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4983 {
4984 if( output_sizes[i] > output_buffer_size )
4985 output_buffer_size = output_sizes[i];
4986 if( output_sizes[i] == 0 )
4987 expected_outputs[i] = NULL;
4988 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004989 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004990 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004991
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004992 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4993 psa_set_key_algorithm( &attributes, alg );
4994 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004995
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004996 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004997 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4998 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4999 requested_capacity ) );
5000 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005001 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005002 switch( steps[i] )
5003 {
5004 case 0:
5005 break;
5006 case PSA_KEY_DERIVATION_INPUT_SECRET:
5007 PSA_ASSERT( psa_import_key( &attributes,
5008 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005009 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005010 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005011 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005012 break;
5013 default:
5014 PSA_ASSERT( psa_key_derivation_input_bytes(
5015 &operation, steps[i],
5016 inputs[i]->x, inputs[i]->len ) );
5017 break;
5018 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005019 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005020
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005022 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005023 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005024 expected_capacity = requested_capacity;
5025
5026 /* Expansion phase. */
5027 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5028 {
5029 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005030 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005031 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005032 if( expected_capacity == 0 && output_sizes[i] == 0 )
5033 {
5034 /* Reading 0 bytes when 0 bytes are available can go either way. */
5035 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005036 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005037 continue;
5038 }
5039 else if( expected_capacity == 0 ||
5040 output_sizes[i] > expected_capacity )
5041 {
5042 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005043 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005044 expected_capacity = 0;
5045 continue;
5046 }
5047 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005048 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005049 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005050 ASSERT_COMPARE( output_buffer, output_sizes[i],
5051 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005052 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005053 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005054 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005055 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005056 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005057 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005058 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005059
5060exit:
5061 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005063 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5064 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005065 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005066}
5067/* END_CASE */
5068
5069/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005070void derive_full( int alg_arg,
5071 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005072 data_t *input1,
5073 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005074 int requested_capacity_arg )
5075{
Ronald Cron5425a212020-08-04 14:58:35 +02005076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005077 psa_algorithm_t alg = alg_arg;
5078 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005079 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005080 unsigned char output_buffer[16];
5081 size_t expected_capacity = requested_capacity;
5082 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005084
Gilles Peskine8817f612018-12-18 00:18:46 +01005085 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005086
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005087 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5088 psa_set_key_algorithm( &attributes, alg );
5089 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005090
Gilles Peskine049c7532019-05-15 20:22:09 +02005091 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005092 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005093
Ronald Cron5425a212020-08-04 14:58:35 +02005094 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005095 input1->x, input1->len,
5096 input2->x, input2->len,
5097 requested_capacity ) )
5098 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005099
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005100 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005101 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005102 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005103
5104 /* Expansion phase. */
5105 while( current_capacity > 0 )
5106 {
5107 size_t read_size = sizeof( output_buffer );
5108 if( read_size > current_capacity )
5109 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005111 output_buffer,
5112 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005113 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005114 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005115 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005116 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005117 }
5118
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005119 /* Check that the operation refuses to go over capacity. */
5120 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005121 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005122
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005123 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005124
5125exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005126 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005127 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005128 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005129}
5130/* END_CASE */
5131
Janos Follathe60c9052019-07-03 13:51:30 +01005132/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005133void derive_key_exercise( int alg_arg,
5134 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005135 data_t *input1,
5136 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005137 int derived_type_arg,
5138 int derived_bits_arg,
5139 int derived_usage_arg,
5140 int derived_alg_arg )
5141{
Ronald Cron5425a212020-08-04 14:58:35 +02005142 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5143 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005144 psa_algorithm_t alg = alg_arg;
5145 psa_key_type_t derived_type = derived_type_arg;
5146 size_t derived_bits = derived_bits_arg;
5147 psa_key_usage_t derived_usage = derived_usage_arg;
5148 psa_algorithm_t derived_alg = derived_alg_arg;
5149 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005150 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005152 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005153
Gilles Peskine8817f612018-12-18 00:18:46 +01005154 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005155
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005156 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5157 psa_set_key_algorithm( &attributes, alg );
5158 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005159 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005160 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005161
5162 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005163 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005164 input1->x, input1->len,
5165 input2->x, input2->len, capacity ) )
5166 goto exit;
5167
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005168 psa_set_key_usage_flags( &attributes, derived_usage );
5169 psa_set_key_algorithm( &attributes, derived_alg );
5170 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005171 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005172 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005173 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005174
5175 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005176 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005177 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5178 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005179
5180 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005181 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005182 goto exit;
5183
5184exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005185 /*
5186 * Key attributes may have been returned by psa_get_key_attributes()
5187 * thus reset them as required.
5188 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005189 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005190
5191 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005192 psa_destroy_key( base_key );
5193 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005194 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005195}
5196/* END_CASE */
5197
Janos Follath42fd8882019-07-03 14:17:09 +01005198/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005199void derive_key_export( int alg_arg,
5200 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005201 data_t *input1,
5202 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005203 int bytes1_arg,
5204 int bytes2_arg )
5205{
Ronald Cron5425a212020-08-04 14:58:35 +02005206 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5207 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005208 psa_algorithm_t alg = alg_arg;
5209 size_t bytes1 = bytes1_arg;
5210 size_t bytes2 = bytes2_arg;
5211 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005213 uint8_t *output_buffer = NULL;
5214 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005215 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5216 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005217 size_t length;
5218
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005219 ASSERT_ALLOC( output_buffer, capacity );
5220 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005221 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005222
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005223 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5224 psa_set_key_algorithm( &base_attributes, alg );
5225 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005226 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005227 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005228
5229 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005230 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005231 input1->x, input1->len,
5232 input2->x, input2->len, capacity ) )
5233 goto exit;
5234
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005235 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005236 output_buffer,
5237 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005238 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005239
5240 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005241 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005242 input1->x, input1->len,
5243 input2->x, input2->len, capacity ) )
5244 goto exit;
5245
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005246 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5247 psa_set_key_algorithm( &derived_attributes, 0 );
5248 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005249 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005250 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005251 &derived_key ) );
5252 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005253 export_buffer, bytes1,
5254 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005255 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005256 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005257 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005258 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005259 &derived_key ) );
5260 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005261 export_buffer + bytes1, bytes2,
5262 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005263 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005264
5265 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005266 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5267 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005268
5269exit:
5270 mbedtls_free( output_buffer );
5271 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005273 psa_destroy_key( base_key );
5274 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005275 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005276}
5277/* END_CASE */
5278
5279/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005280void derive_key( int alg_arg,
5281 data_t *key_data, data_t *input1, data_t *input2,
5282 int type_arg, int bits_arg,
5283 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005284{
Ronald Cron5425a212020-08-04 14:58:35 +02005285 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5286 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005287 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005288 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005289 size_t bits = bits_arg;
5290 psa_status_t expected_status = expected_status_arg;
5291 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5292 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5293 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5294
5295 PSA_ASSERT( psa_crypto_init( ) );
5296
5297 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5298 psa_set_key_algorithm( &base_attributes, alg );
5299 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5300 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005301 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005302
Ronald Cron5425a212020-08-04 14:58:35 +02005303 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005304 input1->x, input1->len,
5305 input2->x, input2->len, SIZE_MAX ) )
5306 goto exit;
5307
5308 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5309 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005310 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005311 psa_set_key_bits( &derived_attributes, bits );
5312 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005313 &derived_key ),
Gilles Peskinec744d992019-07-30 17:26:54 +02005314 expected_status );
5315
5316exit:
5317 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005318 psa_destroy_key( base_key );
5319 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005320 PSA_DONE( );
5321}
5322/* END_CASE */
5323
5324/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005325void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005326 int our_key_type_arg, int our_key_alg_arg,
5327 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005328 int expected_status_arg )
5329{
Ronald Cron5425a212020-08-04 14:58:35 +02005330 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005331 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005332 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005333 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005336 psa_status_t expected_status = expected_status_arg;
5337 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005338
Gilles Peskine8817f612018-12-18 00:18:46 +01005339 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005340
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005341 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005342 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005343 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005344 PSA_ASSERT( psa_import_key( &attributes,
5345 our_key_data->x, our_key_data->len,
5346 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005347
Gilles Peskine77f40d82019-04-11 21:27:06 +02005348 /* The tests currently include inputs that should fail at either step.
5349 * Test cases that fail at the setup step should be changed to call
5350 * key_derivation_setup instead, and this function should be renamed
5351 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005352 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005353 if( status == PSA_SUCCESS )
5354 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005355 TEST_EQUAL( psa_key_derivation_key_agreement(
5356 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5357 our_key,
5358 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005359 expected_status );
5360 }
5361 else
5362 {
5363 TEST_ASSERT( status == expected_status );
5364 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005365
5366exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005367 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005368 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005369 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005370}
5371/* END_CASE */
5372
5373/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005374void raw_key_agreement( int alg_arg,
5375 int our_key_type_arg, data_t *our_key_data,
5376 data_t *peer_key_data,
5377 data_t *expected_output )
5378{
Ronald Cron5425a212020-08-04 14:58:35 +02005379 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005380 psa_algorithm_t alg = alg_arg;
5381 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005383 unsigned char *output = NULL;
5384 size_t output_length = ~0;
5385
5386 ASSERT_ALLOC( output, expected_output->len );
5387 PSA_ASSERT( psa_crypto_init( ) );
5388
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005389 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5390 psa_set_key_algorithm( &attributes, alg );
5391 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005392 PSA_ASSERT( psa_import_key( &attributes,
5393 our_key_data->x, our_key_data->len,
5394 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005395
Gilles Peskinebe697d82019-05-16 18:00:41 +02005396 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5397 peer_key_data->x, peer_key_data->len,
5398 output, expected_output->len,
5399 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005400 ASSERT_COMPARE( output, output_length,
5401 expected_output->x, expected_output->len );
5402
5403exit:
5404 mbedtls_free( output );
5405 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005406 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005407}
5408/* END_CASE */
5409
5410/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005411void key_agreement_capacity( int alg_arg,
5412 int our_key_type_arg, data_t *our_key_data,
5413 data_t *peer_key_data,
5414 int expected_capacity_arg )
5415{
Ronald Cron5425a212020-08-04 14:58:35 +02005416 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005417 psa_algorithm_t alg = alg_arg;
5418 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005419 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005421 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005422 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005423
Gilles Peskine8817f612018-12-18 00:18:46 +01005424 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005425
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005426 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5427 psa_set_key_algorithm( &attributes, alg );
5428 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005429 PSA_ASSERT( psa_import_key( &attributes,
5430 our_key_data->x, our_key_data->len,
5431 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005432
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005434 PSA_ASSERT( psa_key_derivation_key_agreement(
5435 &operation,
5436 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5437 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005438 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5439 {
5440 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005441 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005442 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005443 NULL, 0 ) );
5444 }
Gilles Peskine59685592018-09-18 12:11:34 +02005445
Gilles Peskinebf491972018-10-25 22:36:12 +02005446 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005447 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005448 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005449 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005450
Gilles Peskinebf491972018-10-25 22:36:12 +02005451 /* Test the actual capacity by reading the output. */
5452 while( actual_capacity > sizeof( output ) )
5453 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005454 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005455 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005456 actual_capacity -= sizeof( output );
5457 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005458 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005459 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005460 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005461 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005462
Gilles Peskine59685592018-09-18 12:11:34 +02005463exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005464 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005465 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005466 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005467}
5468/* END_CASE */
5469
5470/* BEGIN_CASE */
5471void key_agreement_output( int alg_arg,
5472 int our_key_type_arg, data_t *our_key_data,
5473 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005474 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005475{
Ronald Cron5425a212020-08-04 14:58:35 +02005476 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005477 psa_algorithm_t alg = alg_arg;
5478 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005479 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005480 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005481 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005482
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005483 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5484 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005485
Gilles Peskine8817f612018-12-18 00:18:46 +01005486 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005487
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005488 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5489 psa_set_key_algorithm( &attributes, alg );
5490 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005491 PSA_ASSERT( psa_import_key( &attributes,
5492 our_key_data->x, our_key_data->len,
5493 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005495 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005496 PSA_ASSERT( psa_key_derivation_key_agreement(
5497 &operation,
5498 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5499 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005500 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5501 {
5502 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005503 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005504 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005505 NULL, 0 ) );
5506 }
Gilles Peskine59685592018-09-18 12:11:34 +02005507
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005508 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005509 actual_output,
5510 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005511 ASSERT_COMPARE( actual_output, expected_output1->len,
5512 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005513 if( expected_output2->len != 0 )
5514 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005516 actual_output,
5517 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005518 ASSERT_COMPARE( actual_output, expected_output2->len,
5519 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005520 }
Gilles Peskine59685592018-09-18 12:11:34 +02005521
5522exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005524 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005525 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005526 mbedtls_free( actual_output );
5527}
5528/* END_CASE */
5529
5530/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005531void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005532{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005533 size_t bytes = bytes_arg;
5534 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005535 unsigned char *output = NULL;
5536 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005537 size_t i;
5538 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005539
Simon Butcher49f8e312020-03-03 15:51:50 +00005540 TEST_ASSERT( bytes_arg >= 0 );
5541
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005542 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5543 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005544 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005545
Gilles Peskine8817f612018-12-18 00:18:46 +01005546 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005547
Gilles Peskinea50d7392018-06-21 10:22:13 +02005548 /* Run several times, to ensure that every output byte will be
5549 * nonzero at least once with overwhelming probability
5550 * (2^(-8*number_of_runs)). */
5551 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005552 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005553 if( bytes != 0 )
5554 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005555 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005556
5557 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005558 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5559 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005560
5561 for( i = 0; i < bytes; i++ )
5562 {
5563 if( output[i] != 0 )
5564 ++changed[i];
5565 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005566 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005567
5568 /* Check that every byte was changed to nonzero at least once. This
5569 * validates that psa_generate_random is overwriting every byte of
5570 * the output buffer. */
5571 for( i = 0; i < bytes; i++ )
5572 {
5573 TEST_ASSERT( changed[i] != 0 );
5574 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005575
5576exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005577 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005578 mbedtls_free( output );
5579 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005580}
5581/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005582
5583/* BEGIN_CASE */
5584void generate_key( int type_arg,
5585 int bits_arg,
5586 int usage_arg,
5587 int alg_arg,
5588 int expected_status_arg )
5589{
Ronald Cron5425a212020-08-04 14:58:35 +02005590 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005591 psa_key_type_t type = type_arg;
5592 psa_key_usage_t usage = usage_arg;
5593 size_t bits = bits_arg;
5594 psa_algorithm_t alg = alg_arg;
5595 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005596 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005597 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005598
Gilles Peskine8817f612018-12-18 00:18:46 +01005599 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005600
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005601 psa_set_key_usage_flags( &attributes, usage );
5602 psa_set_key_algorithm( &attributes, alg );
5603 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005604 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005605
5606 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005607 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005608 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005609 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005610
5611 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005612 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005613 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5614 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005615
Gilles Peskine818ca122018-06-20 18:16:48 +02005616 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005617 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005618 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005619
5620exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005621 /*
5622 * Key attributes may have been returned by psa_get_key_attributes()
5623 * thus reset them as required.
5624 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005625 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005626
Ronald Cron5425a212020-08-04 14:58:35 +02005627 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005628 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005629}
5630/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005631
Gilles Peskinee56e8782019-04-26 17:34:02 +02005632/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5633void generate_key_rsa( int bits_arg,
5634 data_t *e_arg,
5635 int expected_status_arg )
5636{
Ronald Cron5425a212020-08-04 14:58:35 +02005637 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005638 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005639 size_t bits = bits_arg;
5640 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5641 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5642 psa_status_t expected_status = expected_status_arg;
5643 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5644 uint8_t *exported = NULL;
5645 size_t exported_size =
5646 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5647 size_t exported_length = SIZE_MAX;
5648 uint8_t *e_read_buffer = NULL;
5649 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005650 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005651 size_t e_read_length = SIZE_MAX;
5652
5653 if( e_arg->len == 0 ||
5654 ( e_arg->len == 3 &&
5655 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5656 {
5657 is_default_public_exponent = 1;
5658 e_read_size = 0;
5659 }
5660 ASSERT_ALLOC( e_read_buffer, e_read_size );
5661 ASSERT_ALLOC( exported, exported_size );
5662
5663 PSA_ASSERT( psa_crypto_init( ) );
5664
5665 psa_set_key_usage_flags( &attributes, usage );
5666 psa_set_key_algorithm( &attributes, alg );
5667 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5668 e_arg->x, e_arg->len ) );
5669 psa_set_key_bits( &attributes, bits );
5670
5671 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005672 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005673 if( expected_status != PSA_SUCCESS )
5674 goto exit;
5675
5676 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005677 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005678 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5679 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5680 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5681 e_read_buffer, e_read_size,
5682 &e_read_length ) );
5683 if( is_default_public_exponent )
5684 TEST_EQUAL( e_read_length, 0 );
5685 else
5686 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5687
5688 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005689 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005690 goto exit;
5691
5692 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005693 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005694 exported, exported_size,
5695 &exported_length ) );
5696 {
5697 uint8_t *p = exported;
5698 uint8_t *end = exported + exported_length;
5699 size_t len;
5700 /* RSAPublicKey ::= SEQUENCE {
5701 * modulus INTEGER, -- n
5702 * publicExponent INTEGER } -- e
5703 */
5704 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005705 MBEDTLS_ASN1_SEQUENCE |
5706 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005707 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5708 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5709 MBEDTLS_ASN1_INTEGER ) );
5710 if( len >= 1 && p[0] == 0 )
5711 {
5712 ++p;
5713 --len;
5714 }
5715 if( e_arg->len == 0 )
5716 {
5717 TEST_EQUAL( len, 3 );
5718 TEST_EQUAL( p[0], 1 );
5719 TEST_EQUAL( p[1], 0 );
5720 TEST_EQUAL( p[2], 1 );
5721 }
5722 else
5723 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5724 }
5725
5726exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005727 /*
5728 * Key attributes may have been returned by psa_get_key_attributes() or
5729 * set by psa_set_key_domain_parameters() thus reset them as required.
5730 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005731 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005732
Ronald Cron5425a212020-08-04 14:58:35 +02005733 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005734 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005735 mbedtls_free( e_read_buffer );
5736 mbedtls_free( exported );
5737}
5738/* END_CASE */
5739
Darryl Greend49a4992018-06-18 17:27:26 +01005740/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005741void persistent_key_load_key_from_storage( data_t *data,
5742 int type_arg, int bits_arg,
5743 int usage_flags_arg, int alg_arg,
5744 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005745{
Ronald Cron71016a92020-08-28 19:01:50 +02005746 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005748 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5749 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005750 psa_key_type_t type = type_arg;
5751 size_t bits = bits_arg;
5752 psa_key_usage_t usage_flags = usage_flags_arg;
5753 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005754 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005755 unsigned char *first_export = NULL;
5756 unsigned char *second_export = NULL;
5757 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5758 size_t first_exported_length;
5759 size_t second_exported_length;
5760
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005761 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5762 {
5763 ASSERT_ALLOC( first_export, export_size );
5764 ASSERT_ALLOC( second_export, export_size );
5765 }
Darryl Greend49a4992018-06-18 17:27:26 +01005766
Gilles Peskine8817f612018-12-18 00:18:46 +01005767 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005768
Gilles Peskinec87af662019-05-15 16:12:22 +02005769 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005770 psa_set_key_usage_flags( &attributes, usage_flags );
5771 psa_set_key_algorithm( &attributes, alg );
5772 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005773 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005774
Darryl Green0c6575a2018-11-07 16:05:30 +00005775 switch( generation_method )
5776 {
5777 case IMPORT_KEY:
5778 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005779 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005780 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005781 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005782
Darryl Green0c6575a2018-11-07 16:05:30 +00005783 case GENERATE_KEY:
5784 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005785 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005786 break;
5787
5788 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005789 {
5790 /* Create base key */
5791 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5792 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5793 psa_set_key_usage_flags( &base_attributes,
5794 PSA_KEY_USAGE_DERIVE );
5795 psa_set_key_algorithm( &base_attributes, derive_alg );
5796 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005797 PSA_ASSERT( psa_import_key( &base_attributes,
5798 data->x, data->len,
5799 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005800 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005801 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005802 PSA_ASSERT( psa_key_derivation_input_key(
5803 &operation,
5804 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005805 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005806 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005807 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005808 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5809 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005810 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005811 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005812 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005813 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005814 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005815 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005816 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005817 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005818
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005819 /* Export the key if permitted by the key policy. */
5820 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5821 {
Ronald Cron5425a212020-08-04 14:58:35 +02005822 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005823 first_export, export_size,
5824 &first_exported_length ) );
5825 if( generation_method == IMPORT_KEY )
5826 ASSERT_COMPARE( data->x, data->len,
5827 first_export, first_exported_length );
5828 }
Darryl Greend49a4992018-06-18 17:27:26 +01005829
5830 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005831 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005832 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005833 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005834
Darryl Greend49a4992018-06-18 17:27:26 +01005835 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005836 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005837 TEST_ASSERT( mbedtls_svc_key_id_equal(
5838 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005839 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5840 PSA_KEY_LIFETIME_PERSISTENT );
5841 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5842 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5843 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5844 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005845
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005846 /* Export the key again if permitted by the key policy. */
5847 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005848 {
Ronald Cron5425a212020-08-04 14:58:35 +02005849 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005850 second_export, export_size,
5851 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005852 ASSERT_COMPARE( first_export, first_exported_length,
5853 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005854 }
5855
5856 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005857 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005858 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005859
5860exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005861 /*
5862 * Key attributes may have been returned by psa_get_key_attributes()
5863 * thus reset them as required.
5864 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005865 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005866
Darryl Greend49a4992018-06-18 17:27:26 +01005867 mbedtls_free( first_export );
5868 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005869 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005870 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005871 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005872 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005873}
5874/* END_CASE */