blob: 7867892877851181c2c797812d89561ef8c67e4c [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 Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Jaeden Amerof24c7f82018-06-27 17:20:43 +010015/** An invalid export length that will never be set by psa_export_key(). */
16static const size_t INVALID_EXPORT_LENGTH = ~0U;
17
Gilles Peskinef426e0f2019-02-25 17:42:03 +010018/* A hash algorithm that is known to be supported.
19 *
20 * This is used in some smoke tests.
21 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010022#if defined(PSA_WANT_ALG_MD2)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010023#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
Gilles Peskined6dc40c2021-01-12 12:55:31 +010024#elif defined(PSA_WANT_ALG_MD4)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010025#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
Gilles Peskined6dc40c2021-01-12 12:55:31 +010026#elif defined(PSA_WANT_ALG_MD5)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010027#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
28/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
29 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
30 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
31 * implausible anyway. */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010032#elif defined(PSA_WANT_ALG_SHA_1)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010033#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
Gilles Peskined6dc40c2021-01-12 12:55:31 +010034#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010035#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
Gilles Peskined6dc40c2021-01-12 12:55:31 +010036#elif defined(PSA_WANT_ALG_SHA_384)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010037#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
Gilles Peskined6dc40c2021-01-12 12:55:31 +010038#elif defined(PSA_WANT_ALG_SHA_512)
39#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
40#elif defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010041#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
42#else
43#undef KNOWN_SUPPORTED_HASH_ALG
44#endif
45
46/* A block cipher that is known to be supported.
47 *
48 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
49 */
50#if defined(MBEDTLS_AES_C)
51#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
52#elif defined(MBEDTLS_ARIA_C)
53#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
54#elif defined(MBEDTLS_CAMELLIA_C)
55#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
56#undef KNOWN_SUPPORTED_BLOCK_CIPHER
57#endif
58
59/* A MAC mode that is known to be supported.
60 *
61 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
62 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
63 *
64 * This is used in some smoke tests.
65 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010066#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010067#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
68#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
69#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
70#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
71#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
72#else
73#undef KNOWN_SUPPORTED_MAC_ALG
74#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
75#endif
76
77/* A cipher algorithm and key type that are known to be supported.
78 *
79 * This is used in some smoke tests.
80 */
81#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
82#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
83#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
84#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
85#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
86#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
87#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
89#else
90#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91#endif
92#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
93#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
94#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
95#elif defined(MBEDTLS_RC4_C)
96#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
97#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
98#else
99#undef KNOWN_SUPPORTED_CIPHER_ALG
100#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
101#endif
102
Gilles Peskine667c1112019-12-03 19:03:20 +0100103#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100104int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
Gilles Peskine667c1112019-12-03 19:03:20 +0100105{
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100106 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
107 PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100108}
109#else
110int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
111{
112 (void) lifetime;
113 return( 0 );
114}
115#endif
116
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200117/** Test if a buffer contains a constant byte value.
118 *
119 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200120 *
121 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200122 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200123 * \param size Size of the buffer in bytes.
124 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200125 * \return 1 if the buffer is all-bits-zero.
126 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200127 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200128static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200129{
130 size_t i;
131 for( i = 0; i < size; i++ )
132 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200133 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200134 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200135 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200136 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200137}
Gilles Peskine818ca122018-06-20 18:16:48 +0200138
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200139/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
140static int asn1_write_10x( unsigned char **p,
141 unsigned char *start,
142 size_t bits,
143 unsigned char x )
144{
145 int ret;
146 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200147 if( bits == 0 )
148 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
149 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200150 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300151 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200152 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
153 *p -= len;
154 ( *p )[len-1] = x;
155 if( bits % 8 == 0 )
156 ( *p )[1] |= 1;
157 else
158 ( *p )[0] |= 1 << ( bits % 8 );
159 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
160 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
161 MBEDTLS_ASN1_INTEGER ) );
162 return( len );
163}
164
165static int construct_fake_rsa_key( unsigned char *buffer,
166 size_t buffer_size,
167 unsigned char **p,
168 size_t bits,
169 int keypair )
170{
171 size_t half_bits = ( bits + 1 ) / 2;
172 int ret;
173 int len = 0;
174 /* Construct something that looks like a DER encoding of
175 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
176 * RSAPrivateKey ::= SEQUENCE {
177 * version Version,
178 * modulus INTEGER, -- n
179 * publicExponent INTEGER, -- e
180 * privateExponent INTEGER, -- d
181 * prime1 INTEGER, -- p
182 * prime2 INTEGER, -- q
183 * exponent1 INTEGER, -- d mod (p-1)
184 * exponent2 INTEGER, -- d mod (q-1)
185 * coefficient INTEGER, -- (inverse of q) mod p
186 * otherPrimeInfos OtherPrimeInfos OPTIONAL
187 * }
188 * Or, for a public key, the same structure with only
189 * version, modulus and publicExponent.
190 */
191 *p = buffer + buffer_size;
192 if( keypair )
193 {
194 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
195 asn1_write_10x( p, buffer, half_bits, 1 ) );
196 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
197 asn1_write_10x( p, buffer, half_bits, 1 ) );
198 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
199 asn1_write_10x( p, buffer, half_bits, 1 ) );
200 MBEDTLS_ASN1_CHK_ADD( len, /* q */
201 asn1_write_10x( p, buffer, half_bits, 1 ) );
202 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
203 asn1_write_10x( p, buffer, half_bits, 3 ) );
204 MBEDTLS_ASN1_CHK_ADD( len, /* d */
205 asn1_write_10x( p, buffer, bits, 1 ) );
206 }
207 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
208 asn1_write_10x( p, buffer, 17, 1 ) );
209 MBEDTLS_ASN1_CHK_ADD( len, /* n */
210 asn1_write_10x( p, buffer, bits, 1 ) );
211 if( keypair )
212 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
213 mbedtls_asn1_write_int( p, buffer, 0 ) );
214 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
215 {
216 const unsigned char tag =
217 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
218 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
219 }
220 return( len );
221}
222
Ronald Cron5425a212020-08-04 14:58:35 +0200223int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100224{
225 int ok = 0;
226 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
227 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200228 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100229 psa_key_type_t type;
230 psa_key_type_t bits;
231
232 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
233 lifetime = psa_get_key_lifetime( &attributes );
234 id = psa_get_key_id( &attributes );
235 type = psa_get_key_type( &attributes );
236 bits = psa_get_key_bits( &attributes );
237
238 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200239 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200240 {
241 TEST_ASSERT(
242 ( PSA_KEY_ID_VOLATILE_MIN <=
243 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
244 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
245 PSA_KEY_ID_VOLATILE_MAX ) );
246 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100247 else
248 {
249 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200250 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
251 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100252 }
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254 /* randomly-generated 64-bit constant, should never appear in test data */
255 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
256 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100257 if( lifetime_is_dynamic_secure_element( lifetime ) )
Gilles Peskine667c1112019-12-03 19:03:20 +0100258 {
259 /* Mbed Crypto currently always exposes the slot number to
260 * applications. This is not mandated by the PSA specification
261 * and may change in future versions. */
262 TEST_EQUAL( status, 0 );
263 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
264 }
265 else
266 {
267 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
268 }
269#endif
270
271 /* Type and size */
272 TEST_ASSERT( type != 0 );
273 TEST_ASSERT( bits != 0 );
274 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
275 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
276 TEST_ASSERT( bits % 8 == 0 );
277
278 /* MAX macros concerning specific key types */
279 if( PSA_KEY_TYPE_IS_ECC( type ) )
280 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
281 else if( PSA_KEY_TYPE_IS_RSA( type ) )
282 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100283 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100284
285 ok = 1;
286
287exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100288 /*
289 * Key attributes may have been returned by psa_get_key_attributes()
290 * thus reset them as required.
291 */
Gilles Peskine667c1112019-12-03 19:03:20 +0100292 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100293
Gilles Peskine667c1112019-12-03 19:03:20 +0100294 return( ok );
295}
296
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100297int exercise_mac_setup( psa_key_type_t key_type,
298 const unsigned char *key_bytes,
299 size_t key_length,
300 psa_algorithm_t alg,
301 psa_mac_operation_t *operation,
302 psa_status_t *status )
303{
Ronald Cron5425a212020-08-04 14:58:35 +0200304 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200305 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100306
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100307 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200308 psa_set_key_algorithm( &attributes, alg );
309 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200310 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100311
Ronald Cron5425a212020-08-04 14:58:35 +0200312 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100313 /* Whether setup succeeded or failed, abort must succeed. */
314 PSA_ASSERT( psa_mac_abort( operation ) );
315 /* If setup failed, reproduce the failure, so that the caller can
316 * test the resulting state of the operation object. */
317 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100318 {
Ronald Cron5425a212020-08-04 14:58:35 +0200319 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100320 }
321
Ronald Cron5425a212020-08-04 14:58:35 +0200322 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100323 return( 1 );
324
325exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200326 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100327 return( 0 );
328}
329
330int exercise_cipher_setup( psa_key_type_t key_type,
331 const unsigned char *key_bytes,
332 size_t key_length,
333 psa_algorithm_t alg,
334 psa_cipher_operation_t *operation,
335 psa_status_t *status )
336{
Ronald Cron5425a212020-08-04 14:58:35 +0200337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100339
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200340 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
341 psa_set_key_algorithm( &attributes, alg );
342 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200343 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100344
Ronald Cron5425a212020-08-04 14:58:35 +0200345 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100346 /* Whether setup succeeded or failed, abort must succeed. */
347 PSA_ASSERT( psa_cipher_abort( operation ) );
348 /* If setup failed, reproduce the failure, so that the caller can
349 * test the resulting state of the operation object. */
350 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100351 {
Ronald Cron5425a212020-08-04 14:58:35 +0200352 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100353 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100354 }
355
Ronald Cron5425a212020-08-04 14:58:35 +0200356 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100357 return( 1 );
358
359exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200360 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100361 return( 0 );
362}
363
Ronald Cron5425a212020-08-04 14:58:35 +0200364static int exercise_mac_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200365 psa_key_usage_t usage,
366 psa_algorithm_t alg )
367{
Jaeden Amero769ce272019-01-04 11:48:03 +0000368 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200369 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200370 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200371 size_t mac_length = sizeof( mac );
372
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100373 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 {
Ronald Cron5425a212020-08-04 14:58:35 +0200375 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100376 PSA_ASSERT( psa_mac_update( &operation,
377 input, sizeof( input ) ) );
378 PSA_ASSERT( psa_mac_sign_finish( &operation,
379 mac, sizeof( mac ),
380 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200381 }
382
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100383 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200384 {
385 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100386 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200387 PSA_SUCCESS :
388 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200389 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100390 PSA_ASSERT( psa_mac_update( &operation,
391 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100392 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
393 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200394 }
395
396 return( 1 );
397
398exit:
399 psa_mac_abort( &operation );
400 return( 0 );
401}
402
Ronald Cron5425a212020-08-04 14:58:35 +0200403static int exercise_cipher_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200404 psa_key_usage_t usage,
405 psa_algorithm_t alg )
406{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000407 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200408 unsigned char iv[16] = {0};
409 size_t iv_length = sizeof( iv );
410 const unsigned char plaintext[16] = "Hello, world...";
411 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
412 size_t ciphertext_length = sizeof( ciphertext );
413 unsigned char decrypted[sizeof( ciphertext )];
414 size_t part_length;
415
416 if( usage & PSA_KEY_USAGE_ENCRYPT )
417 {
Ronald Cron5425a212020-08-04 14:58:35 +0200418 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100419 PSA_ASSERT( psa_cipher_generate_iv( &operation,
420 iv, sizeof( iv ),
421 &iv_length ) );
422 PSA_ASSERT( psa_cipher_update( &operation,
423 plaintext, sizeof( plaintext ),
424 ciphertext, sizeof( ciphertext ),
425 &ciphertext_length ) );
426 PSA_ASSERT( psa_cipher_finish( &operation,
427 ciphertext + ciphertext_length,
428 sizeof( ciphertext ) - ciphertext_length,
429 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200430 ciphertext_length += part_length;
431 }
432
433 if( usage & PSA_KEY_USAGE_DECRYPT )
434 {
435 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200436 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200437 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
438 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200440 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200441 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
442 * have this macro yet. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100443 iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200444 psa_get_key_type( &attributes ) );
445 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100446 psa_reset_key_attributes( &attributes );
Gilles Peskine818ca122018-06-20 18:16:48 +0200447 }
Ronald Cron5425a212020-08-04 14:58:35 +0200448 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100449 PSA_ASSERT( psa_cipher_set_iv( &operation,
450 iv, iv_length ) );
451 PSA_ASSERT( psa_cipher_update( &operation,
452 ciphertext, ciphertext_length,
453 decrypted, sizeof( decrypted ),
454 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200455 status = psa_cipher_finish( &operation,
456 decrypted + part_length,
457 sizeof( decrypted ) - part_length,
458 &part_length );
459 /* For a stream cipher, all inputs are valid. For a block cipher,
460 * if the input is some aribtrary data rather than an actual
461 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200462 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200463 TEST_ASSERT( status == PSA_SUCCESS ||
464 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200465 else
466 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200467 }
468
469 return( 1 );
470
471exit:
472 psa_cipher_abort( &operation );
473 return( 0 );
474}
475
Ronald Cron5425a212020-08-04 14:58:35 +0200476static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200477 psa_key_usage_t usage,
478 psa_algorithm_t alg )
479{
480 unsigned char nonce[16] = {0};
481 size_t nonce_length = sizeof( nonce );
482 unsigned char plaintext[16] = "Hello, world...";
483 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
484 size_t ciphertext_length = sizeof( ciphertext );
485 size_t plaintext_length = sizeof( ciphertext );
486
487 if( usage & PSA_KEY_USAGE_ENCRYPT )
488 {
Ronald Cron5425a212020-08-04 14:58:35 +0200489 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100490 nonce, nonce_length,
491 NULL, 0,
492 plaintext, sizeof( plaintext ),
493 ciphertext, sizeof( ciphertext ),
494 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200495 }
496
497 if( usage & PSA_KEY_USAGE_DECRYPT )
498 {
499 psa_status_t verify_status =
500 ( usage & PSA_KEY_USAGE_ENCRYPT ?
501 PSA_SUCCESS :
502 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200503 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100504 nonce, nonce_length,
505 NULL, 0,
506 ciphertext, ciphertext_length,
507 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100508 &plaintext_length ),
509 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200510 }
511
512 return( 1 );
513
514exit:
515 return( 0 );
516}
517
Ronald Cron5425a212020-08-04 14:58:35 +0200518static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200519 psa_key_usage_t usage,
520 psa_algorithm_t alg )
521{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200522 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
523 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100524 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200525 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100526 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
527
528 /* If the policy allows signing with any hash, just pick one. */
529 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
530 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100531#if defined(KNOWN_SUPPORTED_HASH_ALG)
532 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
533 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100534#else
Chris Jones9634bb12021-01-20 15:56:42 +0000535 mbedtls_test_fail( "No hash algorithm for hash-and-sign testing",
536 __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100537 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100538#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100539 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200540
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100541 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200542 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200543 /* Some algorithms require the payload to have the size of
544 * the hash encoded in the algorithm. Use this input size
545 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200546 if( hash_alg != 0 )
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100547 payload_length = PSA_HASH_LENGTH( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200548 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100549 payload, payload_length,
550 signature, sizeof( signature ),
551 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200552 }
553
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100554 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200555 {
556 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100557 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200558 PSA_SUCCESS :
559 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200560 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100561 payload, payload_length,
562 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100563 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200564 }
565
566 return( 1 );
567
568exit:
569 return( 0 );
570}
571
Ronald Cron5425a212020-08-04 14:58:35 +0200572static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200573 psa_key_usage_t usage,
574 psa_algorithm_t alg )
575{
576 unsigned char plaintext[256] = "Hello, world...";
577 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
578 size_t ciphertext_length = sizeof( ciphertext );
579 size_t plaintext_length = 16;
580
581 if( usage & PSA_KEY_USAGE_ENCRYPT )
582 {
Ronald Cron5425a212020-08-04 14:58:35 +0200583 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100584 plaintext, plaintext_length,
585 NULL, 0,
586 ciphertext, sizeof( ciphertext ),
587 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200588 }
589
590 if( usage & PSA_KEY_USAGE_DECRYPT )
591 {
592 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200593 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200594 ciphertext, ciphertext_length,
595 NULL, 0,
596 plaintext, sizeof( plaintext ),
597 &plaintext_length );
598 TEST_ASSERT( status == PSA_SUCCESS ||
599 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
600 ( status == PSA_ERROR_INVALID_ARGUMENT ||
601 status == PSA_ERROR_INVALID_PADDING ) ) );
602 }
603
604 return( 1 );
605
606exit:
607 return( 0 );
608}
Gilles Peskine02b75072018-07-01 22:31:34 +0200609
Janos Follathf2815ea2019-07-03 12:41:36 +0100610static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200611 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100612 psa_algorithm_t alg,
613 unsigned char* input1, size_t input1_length,
614 unsigned char* input2, size_t input2_length,
615 size_t capacity )
616{
617 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
618 if( PSA_ALG_IS_HKDF( alg ) )
619 {
620 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
621 PSA_KEY_DERIVATION_INPUT_SALT,
622 input1, input1_length ) );
623 PSA_ASSERT( psa_key_derivation_input_key( operation,
624 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200625 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100626 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
627 PSA_KEY_DERIVATION_INPUT_INFO,
628 input2,
629 input2_length ) );
630 }
631 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
632 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
633 {
634 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
635 PSA_KEY_DERIVATION_INPUT_SEED,
636 input1, input1_length ) );
637 PSA_ASSERT( psa_key_derivation_input_key( operation,
638 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200639 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100640 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
641 PSA_KEY_DERIVATION_INPUT_LABEL,
642 input2, input2_length ) );
643 }
644 else
645 {
646 TEST_ASSERT( ! "Key derivation algorithm not supported" );
647 }
648
Gilles Peskinec744d992019-07-30 17:26:54 +0200649 if( capacity != SIZE_MAX )
650 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100651
652 return( 1 );
653
654exit:
655 return( 0 );
656}
657
658
Ronald Cron5425a212020-08-04 14:58:35 +0200659static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200660 psa_key_usage_t usage,
661 psa_algorithm_t alg )
662{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200663 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100664 unsigned char input1[] = "Input 1";
665 size_t input1_length = sizeof( input1 );
666 unsigned char input2[] = "Input 2";
667 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200668 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100669 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200670
671 if( usage & PSA_KEY_USAGE_DERIVE )
672 {
Ronald Cron5425a212020-08-04 14:58:35 +0200673 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100674 input1, input1_length,
675 input2, input2_length, capacity ) )
676 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200677
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200678 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200679 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100680 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200681 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200682 }
683
684 return( 1 );
685
686exit:
687 return( 0 );
688}
689
Gilles Peskinec7998b72018-11-07 18:45:02 +0100690/* We need two keys to exercise key agreement. Exercise the
691 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200692static psa_status_t key_agreement_with_self(
693 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200694 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100695{
696 psa_key_type_t private_key_type;
697 psa_key_type_t public_key_type;
698 size_t key_bits;
699 uint8_t *public_key = NULL;
700 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200701 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200702 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
703 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200704 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100706
Ronald Cron5425a212020-08-04 14:58:35 +0200707 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200708 private_key_type = psa_get_key_type( &attributes );
709 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200710 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100711 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100712 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200713 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100714 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100715
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200716 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200717 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200718 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100719exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100720 /*
721 * Key attributes may have been returned by psa_get_key_attributes()
722 * thus reset them as required.
723 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200724 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100725
726 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100727 return( status );
728}
729
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200730/* We need two keys to exercise key agreement. Exercise the
731 * private key against its own public key. */
732static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200733 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200734{
735 psa_key_type_t private_key_type;
736 psa_key_type_t public_key_type;
737 size_t key_bits;
738 uint8_t *public_key = NULL;
739 size_t public_key_length;
740 uint8_t output[1024];
741 size_t output_length;
742 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200743 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
744 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200745 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200747
Ronald Cron5425a212020-08-04 14:58:35 +0200748 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200749 private_key_type = psa_get_key_type( &attributes );
750 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200751 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100752 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200753 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200754 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200755 public_key, public_key_length,
756 &public_key_length ) );
757
Ronald Cron5425a212020-08-04 14:58:35 +0200758 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200759 public_key, public_key_length,
760 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200761exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100762 /*
763 * Key attributes may have been returned by psa_get_key_attributes()
764 * thus reset them as required.
765 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200766 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100767
768 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200769 return( status );
770}
771
Ronald Cron5425a212020-08-04 14:58:35 +0200772static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200773 psa_key_usage_t usage,
774 psa_algorithm_t alg )
775{
776 int ok = 0;
777
778 if( usage & PSA_KEY_USAGE_DERIVE )
779 {
780 /* We need two keys to exercise key agreement. Exercise the
781 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200782 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200783 }
784 ok = 1;
785
786exit:
787 return( ok );
788}
789
Ronald Cron5425a212020-08-04 14:58:35 +0200790static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200791 psa_key_usage_t usage,
792 psa_algorithm_t alg )
793{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200794 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200795 unsigned char output[1];
796 int ok = 0;
797
798 if( usage & PSA_KEY_USAGE_DERIVE )
799 {
800 /* We need two keys to exercise key agreement. Exercise the
801 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200802 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200803 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200804 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200805 output,
806 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200807 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200808 }
809 ok = 1;
810
811exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200812 return( ok );
813}
814
Jaeden Amerof7dca862019-06-27 17:31:33 +0100815int asn1_skip_integer( unsigned char **p, const unsigned char *end,
816 size_t min_bits, size_t max_bits,
817 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200818{
819 size_t len;
820 size_t actual_bits;
821 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100822 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100823 MBEDTLS_ASN1_INTEGER ),
824 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200825
826 /* Check if the retrieved length doesn't extend the actual buffer's size.
827 * It is assumed here, that end >= p, which validates casting to size_t. */
828 TEST_ASSERT( len <= (size_t)( end - *p) );
829
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200830 /* Tolerate a slight departure from DER encoding:
831 * - 0 may be represented by an empty string or a 1-byte string.
832 * - The sign bit may be used as a value bit. */
833 if( ( len == 1 && ( *p )[0] == 0 ) ||
834 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
835 {
836 ++( *p );
837 --len;
838 }
839 if( min_bits == 0 && len == 0 )
840 return( 1 );
841 msb = ( *p )[0];
842 TEST_ASSERT( msb != 0 );
843 actual_bits = 8 * ( len - 1 );
844 while( msb != 0 )
845 {
846 msb >>= 1;
847 ++actual_bits;
848 }
849 TEST_ASSERT( actual_bits >= min_bits );
850 TEST_ASSERT( actual_bits <= max_bits );
851 if( must_be_odd )
852 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
853 *p += len;
854 return( 1 );
855exit:
856 return( 0 );
857}
858
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200859static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
860 uint8_t *exported, size_t exported_length )
861{
862 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100863 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200864 else
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100865 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200866
867#if defined(MBEDTLS_DES_C)
868 if( type == PSA_KEY_TYPE_DES )
869 {
870 /* Check the parity bits. */
871 unsigned i;
872 for( i = 0; i < bits / 8; i++ )
873 {
874 unsigned bit_count = 0;
875 unsigned m;
876 for( m = 1; m <= 0x100; m <<= 1 )
877 {
878 if( exported[i] & m )
879 ++bit_count;
880 }
881 TEST_ASSERT( bit_count % 2 != 0 );
882 }
883 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200884 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200885#endif
886
887#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200888 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200889 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200890 uint8_t *p = exported;
891 uint8_t *end = exported + exported_length;
892 size_t len;
893 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200894 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200895 * modulus INTEGER, -- n
896 * publicExponent INTEGER, -- e
897 * privateExponent INTEGER, -- d
898 * prime1 INTEGER, -- p
899 * prime2 INTEGER, -- q
900 * exponent1 INTEGER, -- d mod (p-1)
901 * exponent2 INTEGER, -- d mod (q-1)
902 * coefficient INTEGER, -- (inverse of q) mod p
903 * }
904 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100905 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
906 MBEDTLS_ASN1_SEQUENCE |
907 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
908 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200909 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
910 goto exit;
911 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
912 goto exit;
913 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
914 goto exit;
915 /* Require d to be at least half the size of n. */
916 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
917 goto exit;
918 /* Require p and q to be at most half the size of n, rounded up. */
919 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
920 goto exit;
921 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
922 goto exit;
923 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
924 goto exit;
925 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
926 goto exit;
927 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
928 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100929 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100930 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200931 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200932#endif /* MBEDTLS_RSA_C */
933
934#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200935 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200936 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100937 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100938 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100939 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200940 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200941#endif /* MBEDTLS_ECP_C */
942
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200943 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
944 {
945 uint8_t *p = exported;
946 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200947#if defined(MBEDTLS_RSA_C)
948 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
949 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100950 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200951 /* RSAPublicKey ::= SEQUENCE {
952 * modulus INTEGER, -- n
953 * publicExponent INTEGER } -- e
954 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100955 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
956 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100957 MBEDTLS_ASN1_CONSTRUCTED ),
958 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100959 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200960 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
961 goto exit;
962 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
963 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100964 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200965 }
966 else
967#endif /* MBEDTLS_RSA_C */
968#if defined(MBEDTLS_ECP_C)
969 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
970 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200971 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
972 {
973 /* The representation of an ECC Montgomery public key is
974 * the raw compressed point */
975 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
976 }
977 else
978 {
979 /* The representation of an ECC Weierstrass public key is:
980 * - The byte 0x04;
981 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
982 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
983 * - where m is the bit size associated with the curve.
984 */
985 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
986 TEST_EQUAL( p[0], 4 );
987 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200988 }
989 else
990#endif /* MBEDTLS_ECP_C */
991 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100992 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200993 mbedtls_snprintf( message, sizeof( message ),
994 "No sanity check for public key type=0x%08lx",
995 (unsigned long) type );
Chris Jones9634bb12021-01-20 15:56:42 +0000996 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200997 (void) p;
998 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200999 return( 0 );
1000 }
1001 }
1002 else
1003
1004 {
1005 /* No sanity checks for other types */
1006 }
1007
1008 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001009
1010exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001011 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001012}
1013
Ronald Cron5425a212020-08-04 14:58:35 +02001014static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001015 psa_key_usage_t usage )
1016{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001018 uint8_t *exported = NULL;
1019 size_t exported_size = 0;
1020 size_t exported_length = 0;
1021 int ok = 0;
1022
Ronald Cron5425a212020-08-04 14:58:35 +02001023 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001024
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001025 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1026 psa_get_key_type( &attributes ),
1027 psa_get_key_bits( &attributes ) );
1028 ASSERT_ALLOC( exported, exported_size );
1029
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001030 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001031 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001032 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001033 TEST_EQUAL( psa_export_key( key, exported,
1034 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001035 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001036 ok = 1;
1037 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001038 }
1039
Ronald Cron5425a212020-08-04 14:58:35 +02001040 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001041 exported, exported_size,
1042 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001043 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1044 psa_get_key_bits( &attributes ),
1045 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001046
1047exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001048 /*
1049 * Key attributes may have been returned by psa_get_key_attributes()
1050 * thus reset them as required.
1051 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001052 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001053
1054 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001055 return( ok );
1056}
1057
Ronald Cron5425a212020-08-04 14:58:35 +02001058static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001059{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001061 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001062 uint8_t *exported = NULL;
1063 size_t exported_size = 0;
1064 size_t exported_length = 0;
1065 int ok = 0;
1066
Ronald Cron5425a212020-08-04 14:58:35 +02001067 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001068 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001069 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001070 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1071 psa_get_key_type( &attributes ),
1072 psa_get_key_bits( &attributes ) );
1073 ASSERT_ALLOC( exported, exported_size );
1074
1075 TEST_EQUAL( psa_export_public_key( key, exported,
1076 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001077 PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001078 ok = 1;
1079 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001080 }
1081
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001082 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001083 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001084 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1085 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001086 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001087
Ronald Cron5425a212020-08-04 14:58:35 +02001088 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001089 exported, exported_size,
1090 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001091 ok = exported_key_sanity_check( public_type,
1092 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001093 exported, exported_length );
1094
1095exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001096 /*
1097 * Key attributes may have been returned by psa_get_key_attributes()
1098 * thus reset them as required.
1099 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001100 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001101
1102 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001103 return( ok );
1104}
1105
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001106/** Do smoke tests on a key.
1107 *
1108 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1109 * sign/verify, or derivation) that is permitted according to \p usage.
1110 * \p usage and \p alg should correspond to the expected policy on the
1111 * key.
1112 *
1113 * Export the key if permitted by \p usage, and check that the output
1114 * looks sensible. If \p usage forbids export, check that
1115 * \p psa_export_key correctly rejects the attempt. If the key is
1116 * asymmetric, also check \p psa_export_public_key.
1117 *
1118 * If the key fails the tests, this function calls the test framework's
Chris Jones9634bb12021-01-20 15:56:42 +00001119 * `mbedtls_test_fail` function and returns false. Otherwise this function
1120 * returns true. Therefore it should be used as follows:
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001121 * ```
1122 * if( ! exercise_key( ... ) ) goto exit;
1123 * ```
1124 *
Ronald Cron5425a212020-08-04 14:58:35 +02001125 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001126 * \p alg.
1127 * \param usage The usage flags to assume.
1128 * \param alg The algorithm to exercise.
1129 *
1130 * \retval 0 The key failed the smoke tests.
1131 * \retval 1 The key passed the smoke tests.
1132 */
Ronald Cron5425a212020-08-04 14:58:35 +02001133static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001134 psa_key_usage_t usage,
1135 psa_algorithm_t alg )
1136{
1137 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001138
Ronald Cron5425a212020-08-04 14:58:35 +02001139 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001140 return( 0 );
1141
Gilles Peskine02b75072018-07-01 22:31:34 +02001142 if( alg == 0 )
1143 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1144 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001145 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001146 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001147 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001148 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001149 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001150 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001151 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001152 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001153 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001154 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001155 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001156 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001157 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001158 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001159 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001160 else
1161 {
1162 char message[40];
1163 mbedtls_snprintf( message, sizeof( message ),
1164 "No code to exercise alg=0x%08lx",
1165 (unsigned long) alg );
Chris Jones9634bb12021-01-20 15:56:42 +00001166 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskine02b75072018-07-01 22:31:34 +02001167 ok = 0;
1168 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001169
Ronald Cron5425a212020-08-04 14:58:35 +02001170 ok = ok && exercise_export_key( key, usage );
1171 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001172
Gilles Peskine02b75072018-07-01 22:31:34 +02001173 return( ok );
1174}
1175
Gilles Peskine10df3412018-10-25 22:35:43 +02001176static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1177 psa_algorithm_t alg )
1178{
1179 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1180 {
1181 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001182 PSA_KEY_USAGE_VERIFY_HASH :
1183 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001184 }
1185 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1186 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1187 {
1188 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1189 PSA_KEY_USAGE_ENCRYPT :
1190 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1191 }
1192 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1193 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1194 {
1195 return( PSA_KEY_USAGE_DERIVE );
1196 }
1197 else
1198 {
1199 return( 0 );
1200 }
1201
1202}
Darryl Green0c6575a2018-11-07 16:05:30 +00001203
Ronald Cron5425a212020-08-04 14:58:35 +02001204static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001205{
1206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001207 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001208 uint8_t buffer[1];
1209 size_t length;
1210 int ok = 0;
1211
Ronald Cronecfb2372020-07-23 17:13:42 +02001212 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001213 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1214 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1215 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001216 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001217 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001218 TEST_EQUAL(
1219 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1220 TEST_EQUAL(
1221 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001222 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001223 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1224 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1225 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1226 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1227
Ronald Cron5425a212020-08-04 14:58:35 +02001228 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001229 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001230 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001231 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001232 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001233
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001234 ok = 1;
1235
1236exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001237 /*
1238 * Key attributes may have been returned by psa_get_key_attributes()
1239 * thus reset them as required.
1240 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001241 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001242
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001243 return( ok );
1244}
1245
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001246/* Assert that a key isn't reported as having a slot number. */
1247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1248#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1249 do \
1250 { \
1251 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1252 TEST_EQUAL( psa_get_key_slot_number( \
1253 attributes, \
1254 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1255 PSA_ERROR_INVALID_ARGUMENT ); \
1256 } \
1257 while( 0 )
1258#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1259#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1260 ( (void) 0 )
1261#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1262
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001263/* An overapproximation of the amount of storage needed for a key of the
1264 * given type and with the given content. The API doesn't make it easy
1265 * to find a good value for the size. The current implementation doesn't
1266 * care about the value anyway. */
1267#define KEY_BITS_FROM_DATA( type, data ) \
1268 ( data )->len
1269
Darryl Green0c6575a2018-11-07 16:05:30 +00001270typedef enum {
1271 IMPORT_KEY = 0,
1272 GENERATE_KEY = 1,
1273 DERIVE_KEY = 2
1274} generate_method;
1275
Gilles Peskinee59236f2018-01-27 23:32:46 +01001276/* END_HEADER */
1277
1278/* BEGIN_DEPENDENCIES
1279 * depends_on:MBEDTLS_PSA_CRYPTO_C
1280 * END_DEPENDENCIES
1281 */
1282
1283/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001284void static_checks( )
1285{
1286 size_t max_truncated_mac_size =
1287 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1288
1289 /* Check that the length for a truncated MAC always fits in the algorithm
1290 * encoding. The shifted mask is the maximum truncated value. The
1291 * untruncated algorithm may be one byte larger. */
1292 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001293
1294#if defined(MBEDTLS_TEST_DEPRECATED)
1295 /* Check deprecated constants. */
1296 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1297 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1298 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1299 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1300 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1301 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1302 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1303 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001304
Paul Elliott8ff510a2020-06-02 17:19:28 +01001305 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1306 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1308 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1309 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1310 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1311 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1312 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1314 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1326 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1328 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1329 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1330 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1331 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1332 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1333 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1334 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1335
1336 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1337 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1338 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1339 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1340 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1341 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1342 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1343 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001344
Paul Elliott75e27032020-06-03 15:17:39 +01001345 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1346 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1347 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1348 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1349 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1350
1351 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1352 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001353#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001354}
1355/* END_CASE */
1356
1357/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001358void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001359 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001360 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001361{
Gilles Peskine4747d192019-04-17 15:05:45 +02001362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001363 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001364 psa_key_lifetime_t lifetime = lifetime_arg;
1365 psa_key_usage_t usage_flags = usage_flags_arg;
1366 psa_algorithm_t alg = alg_arg;
1367 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001368 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001369
Ronald Cronecfb2372020-07-23 17:13:42 +02001370 TEST_EQUAL(
1371 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1372 TEST_EQUAL(
1373 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001374 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1375 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1376 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1377 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001378 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001379
Gilles Peskinec87af662019-05-15 16:12:22 +02001380 psa_set_key_id( &attributes, id );
1381 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001382 psa_set_key_usage_flags( &attributes, usage_flags );
1383 psa_set_key_algorithm( &attributes, alg );
1384 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001385 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001386
Ronald Cronecfb2372020-07-23 17:13:42 +02001387 TEST_ASSERT( mbedtls_svc_key_id_equal(
1388 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001389 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1390 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1391 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1392 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001393 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001394
1395 psa_reset_key_attributes( &attributes );
1396
Ronald Cronecfb2372020-07-23 17:13:42 +02001397 TEST_EQUAL(
1398 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1399 TEST_EQUAL(
1400 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001401 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1402 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1403 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1404 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001405 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001406}
1407/* END_CASE */
1408
1409/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001410void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1411 int id2_arg, int owner_id2_arg,
1412 int expected_id_arg, int expected_owner_id_arg,
1413 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001414{
1415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001416 mbedtls_svc_key_id_t id1 =
1417 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001418 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001419 mbedtls_svc_key_id_t id2 =
1420 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001421 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001422 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001423 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1424
1425 if( id1_arg != -1 )
1426 psa_set_key_id( &attributes, id1 );
1427 if( lifetime_arg != -1 )
1428 psa_set_key_lifetime( &attributes, lifetime );
1429 if( id2_arg != -1 )
1430 psa_set_key_id( &attributes, id2 );
1431
Ronald Cronecfb2372020-07-23 17:13:42 +02001432 TEST_ASSERT( mbedtls_svc_key_id_equal(
1433 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001434 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1435}
1436/* END_CASE */
1437
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001438/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1439void slot_number_attribute( )
1440{
1441 psa_key_slot_number_t slot_number = 0xdeadbeef;
1442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1443
1444 /* Initially, there is no slot number. */
1445 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1446 PSA_ERROR_INVALID_ARGUMENT );
1447
1448 /* Test setting a slot number. */
1449 psa_set_key_slot_number( &attributes, 0 );
1450 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1451 TEST_EQUAL( slot_number, 0 );
1452
1453 /* Test changing the slot number. */
1454 psa_set_key_slot_number( &attributes, 42 );
1455 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1456 TEST_EQUAL( slot_number, 42 );
1457
1458 /* Test clearing the slot number. */
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 /* Clearing again should have no effect. */
1464 psa_clear_key_slot_number( &attributes );
1465 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1466 PSA_ERROR_INVALID_ARGUMENT );
1467
1468 /* Test that reset clears the slot number. */
1469 psa_set_key_slot_number( &attributes, 42 );
1470 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1471 TEST_EQUAL( slot_number, 42 );
1472 psa_reset_key_attributes( &attributes );
1473 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1474 PSA_ERROR_INVALID_ARGUMENT );
1475}
1476/* END_CASE */
1477
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001478/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001479void import_with_policy( int type_arg,
1480 int usage_arg, int alg_arg,
1481 int expected_status_arg )
1482{
1483 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1484 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001486 psa_key_type_t type = type_arg;
1487 psa_key_usage_t usage = usage_arg;
1488 psa_algorithm_t alg = alg_arg;
1489 psa_status_t expected_status = expected_status_arg;
1490 const uint8_t key_material[16] = {0};
1491 psa_status_t status;
1492
1493 PSA_ASSERT( psa_crypto_init( ) );
1494
1495 psa_set_key_type( &attributes, type );
1496 psa_set_key_usage_flags( &attributes, usage );
1497 psa_set_key_algorithm( &attributes, alg );
1498
1499 status = psa_import_key( &attributes,
1500 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001501 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001502 TEST_EQUAL( status, expected_status );
1503 if( status != PSA_SUCCESS )
1504 goto exit;
1505
Ronald Cron5425a212020-08-04 14:58:35 +02001506 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001507 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1508 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1509 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001510 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001511
Ronald Cron5425a212020-08-04 14:58:35 +02001512 PSA_ASSERT( psa_destroy_key( key ) );
1513 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001514
1515exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001516 /*
1517 * Key attributes may have been returned by psa_get_key_attributes()
1518 * thus reset them as required.
1519 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001520 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001521
1522 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001523 PSA_DONE( );
1524}
1525/* END_CASE */
1526
1527/* BEGIN_CASE */
1528void import_with_data( data_t *data, int type_arg,
1529 int attr_bits_arg,
1530 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001531{
1532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1533 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001534 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001535 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001536 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001537 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001539
Gilles Peskine8817f612018-12-18 00:18:46 +01001540 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001541
Gilles Peskine4747d192019-04-17 15:05:45 +02001542 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001543 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001544
Ronald Cron5425a212020-08-04 14:58:35 +02001545 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001546 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001547 if( status != PSA_SUCCESS )
1548 goto exit;
1549
Ronald Cron5425a212020-08-04 14:58:35 +02001550 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001551 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001552 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001553 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001554 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001555
Ronald Cron5425a212020-08-04 14:58:35 +02001556 PSA_ASSERT( psa_destroy_key( key ) );
1557 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001558
1559exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001560 /*
1561 * Key attributes may have been returned by psa_get_key_attributes()
1562 * thus reset them as required.
1563 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001564 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001565
1566 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001567 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568}
1569/* END_CASE */
1570
1571/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001572void import_large_key( int type_arg, int byte_size_arg,
1573 int expected_status_arg )
1574{
1575 psa_key_type_t type = type_arg;
1576 size_t byte_size = byte_size_arg;
1577 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1578 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001579 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001580 psa_status_t status;
1581 uint8_t *buffer = NULL;
1582 size_t buffer_size = byte_size + 1;
1583 size_t n;
1584
Steven Cooreman69967ce2021-01-18 18:01:08 +01001585 /* Skip the test case if the target running the test cannot
1586 * accomodate large keys due to heap size constraints */
1587 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001588 memset( buffer, 'K', byte_size );
1589
1590 PSA_ASSERT( psa_crypto_init( ) );
1591
1592 /* Try importing the key */
1593 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1594 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001595 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001596 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001597 TEST_EQUAL( status, expected_status );
1598
1599 if( status == PSA_SUCCESS )
1600 {
Ronald Cron5425a212020-08-04 14:58:35 +02001601 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001602 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1603 TEST_EQUAL( psa_get_key_bits( &attributes ),
1604 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001605 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001606 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001607 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001608 for( n = 0; n < byte_size; n++ )
1609 TEST_EQUAL( buffer[n], 'K' );
1610 for( n = byte_size; n < buffer_size; n++ )
1611 TEST_EQUAL( buffer[n], 0 );
1612 }
1613
1614exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001615 /*
1616 * Key attributes may have been returned by psa_get_key_attributes()
1617 * thus reset them as required.
1618 */
1619 psa_reset_key_attributes( &attributes );
1620
Ronald Cron5425a212020-08-04 14:58:35 +02001621 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001622 PSA_DONE( );
1623 mbedtls_free( buffer );
1624}
1625/* END_CASE */
1626
1627/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001628void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1629{
Ronald Cron5425a212020-08-04 14:58:35 +02001630 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001631 size_t bits = bits_arg;
1632 psa_status_t expected_status = expected_status_arg;
1633 psa_status_t status;
1634 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001635 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001636 size_t buffer_size = /* Slight overapproximations */
1637 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001638 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001639 unsigned char *p;
1640 int ret;
1641 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001643
Gilles Peskine8817f612018-12-18 00:18:46 +01001644 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001645 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001646
1647 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1648 bits, keypair ) ) >= 0 );
1649 length = ret;
1650
1651 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001652 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001653 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001654 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001655
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001656 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001657 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001658
1659exit:
1660 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001661 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001662}
1663/* END_CASE */
1664
1665/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001666void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001667 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001668 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001669 int expected_bits,
1670 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001671 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001672 int canonical_input )
1673{
Ronald Cron5425a212020-08-04 14:58:35 +02001674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001675 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001676 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001677 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001678 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001679 unsigned char *exported = NULL;
1680 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001681 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001682 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001683 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001685 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001686
Moran Pekercb088e72018-07-17 17:36:59 +03001687 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001688 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001689 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001690 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001691 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001692
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 psa_set_key_usage_flags( &attributes, usage_arg );
1694 psa_set_key_algorithm( &attributes, alg );
1695 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001696
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001697 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001698 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001699
1700 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001701 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001702 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1703 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001704 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001705
1706 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001707 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001708 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001709
1710 /* The exported length must be set by psa_export_key() to a value between 0
1711 * and export_size. On errors, the exported length must be 0. */
1712 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1713 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1714 TEST_ASSERT( exported_length <= export_size );
1715
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001716 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001717 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001718 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001719 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001720 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001721 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001722 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001723
Ronald Cron5425a212020-08-04 14:58:35 +02001724 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001725 goto exit;
1726
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001727 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001728 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001729 else
1730 {
Ronald Cron5425a212020-08-04 14:58:35 +02001731 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001732 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001733 &key2 ) );
1734 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001735 reexported,
1736 export_size,
1737 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001738 ASSERT_COMPARE( exported, exported_length,
1739 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001740 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001741 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001742 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001743
1744destroy:
1745 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001746 PSA_ASSERT( psa_destroy_key( key ) );
1747 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001748
1749exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001750 /*
1751 * Key attributes may have been returned by psa_get_key_attributes()
1752 * thus reset them as required.
1753 */
1754 psa_reset_key_attributes( &got_attributes );
1755
itayzafrir3e02b3b2018-06-12 17:06:52 +03001756 mbedtls_free( exported );
1757 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001758 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001759}
1760/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001761
Moran Pekerf709f4a2018-06-06 17:26:04 +03001762/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001763void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001764 int type_arg,
1765 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001766 int export_size_delta,
1767 int expected_export_status_arg,
1768 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001769{
Ronald Cron5425a212020-08-04 14:58:35 +02001770 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001771 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001772 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001773 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001774 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001775 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001776 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001777 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001779
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001781
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1783 psa_set_key_algorithm( &attributes, alg );
1784 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001785
1786 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001787 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001788
Gilles Peskine49c25912018-10-29 15:15:31 +01001789 /* Export the public key */
1790 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001791 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001792 exported, export_size,
1793 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001794 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001795 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001796 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001797 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001798 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001799 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001800 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001801 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001802 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001803 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1804 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001805 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001806
1807exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001808 /*
1809 * Key attributes may have been returned by psa_get_key_attributes()
1810 * thus reset them as required.
1811 */
1812 psa_reset_key_attributes( &attributes );
1813
itayzafrir3e02b3b2018-06-12 17:06:52 +03001814 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001815 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001816 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001817}
1818/* END_CASE */
1819
Gilles Peskine20035e32018-02-03 22:44:14 +01001820/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001821void import_and_exercise_key( data_t *data,
1822 int type_arg,
1823 int bits_arg,
1824 int alg_arg )
1825{
Ronald Cron5425a212020-08-04 14:58:35 +02001826 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001827 psa_key_type_t type = type_arg;
1828 size_t bits = bits_arg;
1829 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001830 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001831 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001832 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001833
Gilles Peskine8817f612018-12-18 00:18:46 +01001834 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001835
Gilles Peskine4747d192019-04-17 15:05:45 +02001836 psa_set_key_usage_flags( &attributes, usage );
1837 psa_set_key_algorithm( &attributes, alg );
1838 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001839
1840 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001841 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001842
1843 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001844 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001845 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1846 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001847
1848 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001849 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001850 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001851
Ronald Cron5425a212020-08-04 14:58:35 +02001852 PSA_ASSERT( psa_destroy_key( key ) );
1853 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001854
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001855exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001856 /*
1857 * Key attributes may have been returned by psa_get_key_attributes()
1858 * thus reset them as required.
1859 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001860 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001861
1862 psa_reset_key_attributes( &attributes );
1863 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001864 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001865}
1866/* END_CASE */
1867
1868/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001869void effective_key_attributes( int type_arg, int expected_type_arg,
1870 int bits_arg, int expected_bits_arg,
1871 int usage_arg, int expected_usage_arg,
1872 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001873{
Ronald Cron5425a212020-08-04 14:58:35 +02001874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001875 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001876 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001877 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001878 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001879 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001880 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001881 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001882 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001883 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001884
Gilles Peskine8817f612018-12-18 00:18:46 +01001885 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001886
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001887 psa_set_key_usage_flags( &attributes, usage );
1888 psa_set_key_algorithm( &attributes, alg );
1889 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001890 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001891
Ronald Cron5425a212020-08-04 14:58:35 +02001892 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001893 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001894
Ronald Cron5425a212020-08-04 14:58:35 +02001895 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001896 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1897 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1898 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1899 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001900
1901exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001902 /*
1903 * Key attributes may have been returned by psa_get_key_attributes()
1904 * thus reset them as required.
1905 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001906 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001907
1908 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001909 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001910}
1911/* END_CASE */
1912
1913/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001914void check_key_policy( int type_arg, int bits_arg,
1915 int usage_arg, int alg_arg )
1916{
1917 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1918 usage_arg, usage_arg, alg_arg, alg_arg );
1919 goto exit;
1920}
1921/* END_CASE */
1922
1923/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001924void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001925{
1926 /* Test each valid way of initializing the object, except for `= {0}`, as
1927 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1928 * though it's OK by the C standard. We could test for this, but we'd need
1929 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001930 psa_key_attributes_t func = psa_key_attributes_init( );
1931 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1932 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001933
1934 memset( &zero, 0, sizeof( zero ) );
1935
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001936 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1937 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1938 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001939
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001940 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1941 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1942 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1943
1944 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1945 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1946 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1947
1948 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1949 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1950 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1951
1952 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1953 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1954 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001955}
1956/* END_CASE */
1957
1958/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001959void mac_key_policy( int policy_usage,
1960 int policy_alg,
1961 int key_type,
1962 data_t *key_data,
1963 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001964{
Ronald Cron5425a212020-08-04 14:58:35 +02001965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001967 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001968 psa_status_t status;
1969 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001970
Gilles Peskine8817f612018-12-18 00:18:46 +01001971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001972
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001973 psa_set_key_usage_flags( &attributes, policy_usage );
1974 psa_set_key_algorithm( &attributes, policy_alg );
1975 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001976
Gilles Peskine049c7532019-05-15 20:22:09 +02001977 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001978 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001979
Ronald Cron5425a212020-08-04 14:58:35 +02001980 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001981 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001982 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001983 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001985 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001987
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001988 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001989 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001991 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001992 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001993 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001994 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995
1996exit:
1997 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001998 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001999 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002000}
2001/* END_CASE */
2002
2003/* BEGIN_CASE */
2004void cipher_key_policy( int policy_usage,
2005 int policy_alg,
2006 int key_type,
2007 data_t *key_data,
2008 int exercise_alg )
2009{
Ronald Cron5425a212020-08-04 14:58:35 +02002010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002012 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013 psa_status_t status;
2014
Gilles Peskine8817f612018-12-18 00:18:46 +01002015 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002016
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002017 psa_set_key_usage_flags( &attributes, policy_usage );
2018 psa_set_key_algorithm( &attributes, policy_alg );
2019 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020
Gilles Peskine049c7532019-05-15 20:22:09 +02002021 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002022 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023
Ronald Cron5425a212020-08-04 14:58:35 +02002024 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025 if( policy_alg == exercise_alg &&
2026 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002027 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002029 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030 psa_cipher_abort( &operation );
2031
Ronald Cron5425a212020-08-04 14:58:35 +02002032 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033 if( policy_alg == exercise_alg &&
2034 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002035 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002036 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002037 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038
2039exit:
2040 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002041 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002042 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043}
2044/* END_CASE */
2045
2046/* BEGIN_CASE */
2047void aead_key_policy( int policy_usage,
2048 int policy_alg,
2049 int key_type,
2050 data_t *key_data,
2051 int nonce_length_arg,
2052 int tag_length_arg,
2053 int exercise_alg )
2054{
Ronald Cron5425a212020-08-04 14:58:35 +02002055 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002057 psa_status_t status;
2058 unsigned char nonce[16] = {0};
2059 size_t nonce_length = nonce_length_arg;
2060 unsigned char tag[16];
2061 size_t tag_length = tag_length_arg;
2062 size_t output_length;
2063
2064 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
2065 TEST_ASSERT( tag_length <= sizeof( tag ) );
2066
Gilles Peskine8817f612018-12-18 00:18:46 +01002067 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002068
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002069 psa_set_key_usage_flags( &attributes, policy_usage );
2070 psa_set_key_algorithm( &attributes, policy_alg );
2071 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002072
Gilles Peskine049c7532019-05-15 20:22:09 +02002073 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002074 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075
Ronald Cron5425a212020-08-04 14:58:35 +02002076 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077 nonce, nonce_length,
2078 NULL, 0,
2079 NULL, 0,
2080 tag, tag_length,
2081 &output_length );
2082 if( policy_alg == exercise_alg &&
2083 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002084 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002085 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002086 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002087
2088 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002089 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090 nonce, nonce_length,
2091 NULL, 0,
2092 tag, tag_length,
2093 NULL, 0,
2094 &output_length );
2095 if( policy_alg == exercise_alg &&
2096 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002097 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002098 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002099 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100
2101exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002102 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002103 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002104}
2105/* END_CASE */
2106
2107/* BEGIN_CASE */
2108void asymmetric_encryption_key_policy( int policy_usage,
2109 int policy_alg,
2110 int key_type,
2111 data_t *key_data,
2112 int exercise_alg )
2113{
Ronald Cron5425a212020-08-04 14:58:35 +02002114 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002116 psa_status_t status;
2117 size_t key_bits;
2118 size_t buffer_length;
2119 unsigned char *buffer = NULL;
2120 size_t output_length;
2121
Gilles Peskine8817f612018-12-18 00:18:46 +01002122 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002124 psa_set_key_usage_flags( &attributes, policy_usage );
2125 psa_set_key_algorithm( &attributes, policy_alg );
2126 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002127
Gilles Peskine049c7532019-05-15 20:22:09 +02002128 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002129 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002130
Ronald Cron5425a212020-08-04 14:58:35 +02002131 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002132 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002133 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2134 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002135 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002136
Ronald Cron5425a212020-08-04 14:58:35 +02002137 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002138 NULL, 0,
2139 NULL, 0,
2140 buffer, buffer_length,
2141 &output_length );
2142 if( policy_alg == exercise_alg &&
2143 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002144 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002146 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002147
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002148 if( buffer_length != 0 )
2149 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002150 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002151 buffer, buffer_length,
2152 NULL, 0,
2153 buffer, buffer_length,
2154 &output_length );
2155 if( policy_alg == exercise_alg &&
2156 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002157 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002158 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002159 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002160
2161exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002162 /*
2163 * Key attributes may have been returned by psa_get_key_attributes()
2164 * thus reset them as required.
2165 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002166 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002167
2168 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002169 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002170 mbedtls_free( buffer );
2171}
2172/* END_CASE */
2173
2174/* BEGIN_CASE */
2175void asymmetric_signature_key_policy( int policy_usage,
2176 int policy_alg,
2177 int key_type,
2178 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002179 int exercise_alg,
2180 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002181{
Ronald Cron5425a212020-08-04 14:58:35 +02002182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002184 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002185 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2186 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2187 * compatible with the policy and `payload_length_arg` is supposed to be
2188 * a valid input length to sign. If `payload_length_arg <= 0`,
2189 * `exercise_alg` is supposed to be forbidden by the policy. */
2190 int compatible_alg = payload_length_arg > 0;
2191 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002192 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002193 size_t signature_length;
2194
Gilles Peskine8817f612018-12-18 00:18:46 +01002195 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002196
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002197 psa_set_key_usage_flags( &attributes, policy_usage );
2198 psa_set_key_algorithm( &attributes, policy_alg );
2199 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002200
Gilles Peskine049c7532019-05-15 20:22:09 +02002201 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002202 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002203
Ronald Cron5425a212020-08-04 14:58:35 +02002204 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002205 payload, payload_length,
2206 signature, sizeof( signature ),
2207 &signature_length );
2208 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002209 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002210 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002211 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002212
2213 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002214 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002215 payload, payload_length,
2216 signature, sizeof( signature ) );
2217 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002218 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002219 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002220 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002221
2222exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002223 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002224 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002225}
2226/* END_CASE */
2227
Janos Follathba3fab92019-06-11 14:50:16 +01002228/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002229void derive_key_policy( int policy_usage,
2230 int policy_alg,
2231 int key_type,
2232 data_t *key_data,
2233 int exercise_alg )
2234{
Ronald Cron5425a212020-08-04 14:58:35 +02002235 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002236 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002237 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002238 psa_status_t status;
2239
Gilles Peskine8817f612018-12-18 00:18:46 +01002240 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002241
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002242 psa_set_key_usage_flags( &attributes, policy_usage );
2243 psa_set_key_algorithm( &attributes, policy_alg );
2244 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002245
Gilles Peskine049c7532019-05-15 20:22:09 +02002246 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002247 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002248
Janos Follathba3fab92019-06-11 14:50:16 +01002249 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2250
2251 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2252 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002253 {
Janos Follathba3fab92019-06-11 14:50:16 +01002254 PSA_ASSERT( psa_key_derivation_input_bytes(
2255 &operation,
2256 PSA_KEY_DERIVATION_INPUT_SEED,
2257 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002258 }
Janos Follathba3fab92019-06-11 14:50:16 +01002259
2260 status = psa_key_derivation_input_key( &operation,
2261 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002262 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002263
Gilles Peskineea0fb492018-07-12 17:17:20 +02002264 if( policy_alg == exercise_alg &&
2265 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002266 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002267 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002268 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002269
2270exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002271 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002272 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002273 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002274}
2275/* END_CASE */
2276
2277/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002278void agreement_key_policy( int policy_usage,
2279 int policy_alg,
2280 int key_type_arg,
2281 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002282 int exercise_alg,
2283 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002284{
Ronald Cron5425a212020-08-04 14:58:35 +02002285 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002287 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002288 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002289 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002290 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002291
Gilles Peskine8817f612018-12-18 00:18:46 +01002292 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002293
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002294 psa_set_key_usage_flags( &attributes, policy_usage );
2295 psa_set_key_algorithm( &attributes, policy_alg );
2296 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002297
Gilles Peskine049c7532019-05-15 20:22:09 +02002298 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002299 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002300
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002301 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002302 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002303
Steven Cooremance48e852020-10-05 16:02:45 +02002304 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002305
2306exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002307 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002308 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002309 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002310}
2311/* END_CASE */
2312
2313/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002314void key_policy_alg2( int key_type_arg, data_t *key_data,
2315 int usage_arg, int alg_arg, int alg2_arg )
2316{
Ronald Cron5425a212020-08-04 14:58:35 +02002317 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002318 psa_key_type_t key_type = key_type_arg;
2319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2320 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2321 psa_key_usage_t usage = usage_arg;
2322 psa_algorithm_t alg = alg_arg;
2323 psa_algorithm_t alg2 = alg2_arg;
2324
2325 PSA_ASSERT( psa_crypto_init( ) );
2326
2327 psa_set_key_usage_flags( &attributes, usage );
2328 psa_set_key_algorithm( &attributes, alg );
2329 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2330 psa_set_key_type( &attributes, key_type );
2331 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002332 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002333
Ronald Cron5425a212020-08-04 14:58:35 +02002334 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002335 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2336 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2337 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2338
Ronald Cron5425a212020-08-04 14:58:35 +02002339 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002340 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002341 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002342 goto exit;
2343
2344exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002345 /*
2346 * Key attributes may have been returned by psa_get_key_attributes()
2347 * thus reset them as required.
2348 */
2349 psa_reset_key_attributes( &got_attributes );
2350
Ronald Cron5425a212020-08-04 14:58:35 +02002351 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002352 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002353}
2354/* END_CASE */
2355
2356/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002357void raw_agreement_key_policy( int policy_usage,
2358 int policy_alg,
2359 int key_type_arg,
2360 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002361 int exercise_alg,
2362 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002363{
Ronald Cron5425a212020-08-04 14:58:35 +02002364 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002366 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002367 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002368 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002369 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002370
2371 PSA_ASSERT( psa_crypto_init( ) );
2372
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002373 psa_set_key_usage_flags( &attributes, policy_usage );
2374 psa_set_key_algorithm( &attributes, policy_alg );
2375 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002376
Gilles Peskine049c7532019-05-15 20:22:09 +02002377 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002378 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002379
Ronald Cron5425a212020-08-04 14:58:35 +02002380 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002381
Steven Cooremance48e852020-10-05 16:02:45 +02002382 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002383
2384exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002385 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002386 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002387 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002388}
2389/* END_CASE */
2390
2391/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002392void copy_success( int source_usage_arg,
2393 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002394 int type_arg, data_t *material,
2395 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002396 int target_usage_arg,
2397 int target_alg_arg, int target_alg2_arg,
2398 int expected_usage_arg,
2399 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002400{
Gilles Peskineca25db92019-04-19 11:43:08 +02002401 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2402 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002403 psa_key_usage_t expected_usage = expected_usage_arg;
2404 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002405 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002406 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2407 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002408 uint8_t *export_buffer = NULL;
2409
Gilles Peskine57ab7212019-01-28 13:03:09 +01002410 PSA_ASSERT( psa_crypto_init( ) );
2411
Gilles Peskineca25db92019-04-19 11:43:08 +02002412 /* Prepare the source key. */
2413 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2414 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002415 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002416 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002417 PSA_ASSERT( psa_import_key( &source_attributes,
2418 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002419 &source_key ) );
2420 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002421
Gilles Peskineca25db92019-04-19 11:43:08 +02002422 /* Prepare the target attributes. */
2423 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002424 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002425 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002426 /* Set volatile lifetime to reset the key identifier to 0. */
2427 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2428 }
2429
Gilles Peskineca25db92019-04-19 11:43:08 +02002430 if( target_usage_arg != -1 )
2431 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2432 if( target_alg_arg != -1 )
2433 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002434 if( target_alg2_arg != -1 )
2435 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002436
2437 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002438 PSA_ASSERT( psa_copy_key( source_key,
2439 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002440
2441 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002442 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002443
2444 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002445 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002446 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2447 psa_get_key_type( &target_attributes ) );
2448 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2449 psa_get_key_bits( &target_attributes ) );
2450 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2451 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002452 TEST_EQUAL( expected_alg2,
2453 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002454 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2455 {
2456 size_t length;
2457 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002458 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002459 material->len, &length ) );
2460 ASSERT_COMPARE( material->x, material->len,
2461 export_buffer, length );
2462 }
Ronald Cron5425a212020-08-04 14:58:35 +02002463 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002464 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002465 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002466 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002467
Ronald Cron5425a212020-08-04 14:58:35 +02002468 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002469
2470exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002471 /*
2472 * Source and target key attributes may have been returned by
2473 * psa_get_key_attributes() thus reset them as required.
2474 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002475 psa_reset_key_attributes( &source_attributes );
2476 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002477
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002478 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002479 mbedtls_free( export_buffer );
2480}
2481/* END_CASE */
2482
2483/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002484void copy_fail( int source_usage_arg,
2485 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002486 int type_arg, data_t *material,
2487 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002488 int target_usage_arg,
2489 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002490 int expected_status_arg )
2491{
2492 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2493 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002494 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2495 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002496
2497 PSA_ASSERT( psa_crypto_init( ) );
2498
2499 /* Prepare the source key. */
2500 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2501 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002502 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002503 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002504 PSA_ASSERT( psa_import_key( &source_attributes,
2505 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002506 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002507
2508 /* Prepare the target attributes. */
2509 psa_set_key_type( &target_attributes, target_type_arg );
2510 psa_set_key_bits( &target_attributes, target_bits_arg );
2511 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2512 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002513 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002514
2515 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002516 TEST_EQUAL( psa_copy_key( source_key,
2517 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002518 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002519
Ronald Cron5425a212020-08-04 14:58:35 +02002520 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002521
Gilles Peskine4a644642019-05-03 17:14:08 +02002522exit:
2523 psa_reset_key_attributes( &source_attributes );
2524 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002525 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002526}
2527/* END_CASE */
2528
2529/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002530void hash_operation_init( )
2531{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002532 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002533 /* Test each valid way of initializing the object, except for `= {0}`, as
2534 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2535 * though it's OK by the C standard. We could test for this, but we'd need
2536 * to supress the Clang warning for the test. */
2537 psa_hash_operation_t func = psa_hash_operation_init( );
2538 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2539 psa_hash_operation_t zero;
2540
2541 memset( &zero, 0, sizeof( zero ) );
2542
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002543 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002544 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2545 PSA_ERROR_BAD_STATE );
2546 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2547 PSA_ERROR_BAD_STATE );
2548 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2549 PSA_ERROR_BAD_STATE );
2550
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002551 /* A default hash operation should be abortable without error. */
2552 PSA_ASSERT( psa_hash_abort( &func ) );
2553 PSA_ASSERT( psa_hash_abort( &init ) );
2554 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002555}
2556/* END_CASE */
2557
2558/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002559void hash_setup( int alg_arg,
2560 int expected_status_arg )
2561{
2562 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002563 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002564 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002565 psa_status_t status;
2566
Gilles Peskine8817f612018-12-18 00:18:46 +01002567 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002568
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002569 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002570 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002571
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002572 /* Whether setup succeeded or failed, abort must succeed. */
2573 PSA_ASSERT( psa_hash_abort( &operation ) );
2574
2575 /* If setup failed, reproduce the failure, so as to
2576 * test the resulting state of the operation object. */
2577 if( status != PSA_SUCCESS )
2578 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2579
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002580 /* Now the operation object should be reusable. */
2581#if defined(KNOWN_SUPPORTED_HASH_ALG)
2582 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2583 PSA_ASSERT( psa_hash_abort( &operation ) );
2584#endif
2585
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002586exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002587 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002588}
2589/* END_CASE */
2590
2591/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002592void hash_compute_fail( int alg_arg, data_t *input,
2593 int output_size_arg, int expected_status_arg )
2594{
2595 psa_algorithm_t alg = alg_arg;
2596 uint8_t *output = NULL;
2597 size_t output_size = output_size_arg;
2598 size_t output_length = INVALID_EXPORT_LENGTH;
2599 psa_status_t expected_status = expected_status_arg;
2600 psa_status_t status;
2601
2602 ASSERT_ALLOC( output, output_size );
2603
2604 PSA_ASSERT( psa_crypto_init( ) );
2605
2606 status = psa_hash_compute( alg, input->x, input->len,
2607 output, output_size, &output_length );
2608 TEST_EQUAL( status, expected_status );
2609 TEST_ASSERT( output_length <= output_size );
2610
2611exit:
2612 mbedtls_free( output );
2613 PSA_DONE( );
2614}
2615/* END_CASE */
2616
2617/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002618void hash_compare_fail( int alg_arg, data_t *input,
2619 data_t *reference_hash,
2620 int expected_status_arg )
2621{
2622 psa_algorithm_t alg = alg_arg;
2623 psa_status_t expected_status = expected_status_arg;
2624 psa_status_t status;
2625
2626 PSA_ASSERT( psa_crypto_init( ) );
2627
2628 status = psa_hash_compare( alg, input->x, input->len,
2629 reference_hash->x, reference_hash->len );
2630 TEST_EQUAL( status, expected_status );
2631
2632exit:
2633 PSA_DONE( );
2634}
2635/* END_CASE */
2636
2637/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002638void hash_compute_compare( int alg_arg, data_t *input,
2639 data_t *expected_output )
2640{
2641 psa_algorithm_t alg = alg_arg;
2642 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2643 size_t output_length = INVALID_EXPORT_LENGTH;
2644 size_t i;
2645
2646 PSA_ASSERT( psa_crypto_init( ) );
2647
2648 /* Compute with tight buffer */
2649 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002650 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002651 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002652 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002653 ASSERT_COMPARE( output, output_length,
2654 expected_output->x, expected_output->len );
2655
2656 /* Compute with larger buffer */
2657 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2658 output, sizeof( output ),
2659 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002660 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002661 ASSERT_COMPARE( output, output_length,
2662 expected_output->x, expected_output->len );
2663
2664 /* Compare with correct hash */
2665 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2666 output, output_length ) );
2667
2668 /* Compare with trailing garbage */
2669 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2670 output, output_length + 1 ),
2671 PSA_ERROR_INVALID_SIGNATURE );
2672
2673 /* Compare with truncated hash */
2674 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2675 output, output_length - 1 ),
2676 PSA_ERROR_INVALID_SIGNATURE );
2677
2678 /* Compare with corrupted value */
2679 for( i = 0; i < output_length; i++ )
2680 {
Chris Jones9634bb12021-01-20 15:56:42 +00002681 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002682 output[i] ^= 1;
2683 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2684 output, output_length ),
2685 PSA_ERROR_INVALID_SIGNATURE );
2686 output[i] ^= 1;
2687 }
2688
2689exit:
2690 PSA_DONE( );
2691}
2692/* END_CASE */
2693
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002694/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002695void hash_bad_order( )
2696{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002697 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002698 unsigned char input[] = "";
2699 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002700 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002701 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2702 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2703 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002704 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002705 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002706 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002707
Gilles Peskine8817f612018-12-18 00:18:46 +01002708 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002709
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002710 /* Call setup twice in a row. */
2711 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2712 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2713 PSA_ERROR_BAD_STATE );
2714 PSA_ASSERT( psa_hash_abort( &operation ) );
2715
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002716 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002717 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002718 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002719 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002720
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002721 /* Call update after finish. */
2722 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2723 PSA_ASSERT( psa_hash_finish( &operation,
2724 hash, sizeof( hash ), &hash_len ) );
2725 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002726 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002727 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002728
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002729 /* Call verify without calling setup beforehand. */
2730 TEST_EQUAL( psa_hash_verify( &operation,
2731 valid_hash, sizeof( valid_hash ) ),
2732 PSA_ERROR_BAD_STATE );
2733 PSA_ASSERT( psa_hash_abort( &operation ) );
2734
2735 /* Call verify after finish. */
2736 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2737 PSA_ASSERT( psa_hash_finish( &operation,
2738 hash, sizeof( hash ), &hash_len ) );
2739 TEST_EQUAL( psa_hash_verify( &operation,
2740 valid_hash, sizeof( valid_hash ) ),
2741 PSA_ERROR_BAD_STATE );
2742 PSA_ASSERT( psa_hash_abort( &operation ) );
2743
2744 /* Call verify twice in a row. */
2745 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2746 PSA_ASSERT( psa_hash_verify( &operation,
2747 valid_hash, sizeof( valid_hash ) ) );
2748 TEST_EQUAL( psa_hash_verify( &operation,
2749 valid_hash, sizeof( valid_hash ) ),
2750 PSA_ERROR_BAD_STATE );
2751 PSA_ASSERT( psa_hash_abort( &operation ) );
2752
2753 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002754 TEST_EQUAL( psa_hash_finish( &operation,
2755 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002756 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002757 PSA_ASSERT( psa_hash_abort( &operation ) );
2758
2759 /* Call finish twice in a row. */
2760 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2761 PSA_ASSERT( psa_hash_finish( &operation,
2762 hash, sizeof( hash ), &hash_len ) );
2763 TEST_EQUAL( psa_hash_finish( &operation,
2764 hash, sizeof( hash ), &hash_len ),
2765 PSA_ERROR_BAD_STATE );
2766 PSA_ASSERT( psa_hash_abort( &operation ) );
2767
2768 /* Call finish after calling verify. */
2769 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2770 PSA_ASSERT( psa_hash_verify( &operation,
2771 valid_hash, sizeof( valid_hash ) ) );
2772 TEST_EQUAL( psa_hash_finish( &operation,
2773 hash, sizeof( hash ), &hash_len ),
2774 PSA_ERROR_BAD_STATE );
2775 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002776
2777exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002778 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002779}
2780/* END_CASE */
2781
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002782/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002783void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002784{
2785 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002786 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2787 * appended to it */
2788 unsigned char hash[] = {
2789 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2790 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2791 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002792 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002793 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002794
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002796
itayzafrir27e69452018-11-01 14:26:34 +02002797 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002799 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002800 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002801
itayzafrir27e69452018-11-01 14:26:34 +02002802 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002804 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002805 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002806
itayzafrir27e69452018-11-01 14:26:34 +02002807 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002809 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002810 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002811
itayzafrirec93d302018-10-18 18:01:10 +03002812exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002813 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002814}
2815/* END_CASE */
2816
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002817/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2818void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002819{
2820 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002821 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002822 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002823 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002824 size_t hash_len;
2825
Gilles Peskine8817f612018-12-18 00:18:46 +01002826 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002827
itayzafrir58028322018-10-25 10:22:01 +03002828 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002829 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002830 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002831 hash, expected_size - 1, &hash_len ),
2832 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002833
2834exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002835 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002836}
2837/* END_CASE */
2838
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002839/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2840void hash_clone_source_state( )
2841{
2842 psa_algorithm_t alg = PSA_ALG_SHA_256;
2843 unsigned char hash[PSA_HASH_MAX_SIZE];
2844 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2845 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2846 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2847 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2848 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2849 size_t hash_len;
2850
2851 PSA_ASSERT( psa_crypto_init( ) );
2852 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2853
2854 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2855 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2856 PSA_ASSERT( psa_hash_finish( &op_finished,
2857 hash, sizeof( hash ), &hash_len ) );
2858 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2859 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2860
2861 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2862 PSA_ERROR_BAD_STATE );
2863
2864 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2865 PSA_ASSERT( psa_hash_finish( &op_init,
2866 hash, sizeof( hash ), &hash_len ) );
2867 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2868 PSA_ASSERT( psa_hash_finish( &op_finished,
2869 hash, sizeof( hash ), &hash_len ) );
2870 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2871 PSA_ASSERT( psa_hash_finish( &op_aborted,
2872 hash, sizeof( hash ), &hash_len ) );
2873
2874exit:
2875 psa_hash_abort( &op_source );
2876 psa_hash_abort( &op_init );
2877 psa_hash_abort( &op_setup );
2878 psa_hash_abort( &op_finished );
2879 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002880 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002881}
2882/* END_CASE */
2883
2884/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2885void hash_clone_target_state( )
2886{
2887 psa_algorithm_t alg = PSA_ALG_SHA_256;
2888 unsigned char hash[PSA_HASH_MAX_SIZE];
2889 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2890 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2891 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2892 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2893 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2894 size_t hash_len;
2895
2896 PSA_ASSERT( psa_crypto_init( ) );
2897
2898 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2899 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2900 PSA_ASSERT( psa_hash_finish( &op_finished,
2901 hash, sizeof( hash ), &hash_len ) );
2902 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2903 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2904
2905 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2906 PSA_ASSERT( psa_hash_finish( &op_target,
2907 hash, sizeof( hash ), &hash_len ) );
2908
2909 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2910 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2911 PSA_ERROR_BAD_STATE );
2912 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2913 PSA_ERROR_BAD_STATE );
2914
2915exit:
2916 psa_hash_abort( &op_target );
2917 psa_hash_abort( &op_init );
2918 psa_hash_abort( &op_setup );
2919 psa_hash_abort( &op_finished );
2920 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002921 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002922}
2923/* END_CASE */
2924
itayzafrir58028322018-10-25 10:22:01 +03002925/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002926void mac_operation_init( )
2927{
Jaeden Amero252ef282019-02-15 14:05:35 +00002928 const uint8_t input[1] = { 0 };
2929
Jaeden Amero769ce272019-01-04 11:48:03 +00002930 /* Test each valid way of initializing the object, except for `= {0}`, as
2931 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2932 * though it's OK by the C standard. We could test for this, but we'd need
2933 * to supress the Clang warning for the test. */
2934 psa_mac_operation_t func = psa_mac_operation_init( );
2935 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2936 psa_mac_operation_t zero;
2937
2938 memset( &zero, 0, sizeof( zero ) );
2939
Jaeden Amero252ef282019-02-15 14:05:35 +00002940 /* A freshly-initialized MAC operation should not be usable. */
2941 TEST_EQUAL( psa_mac_update( &func,
2942 input, sizeof( input ) ),
2943 PSA_ERROR_BAD_STATE );
2944 TEST_EQUAL( psa_mac_update( &init,
2945 input, sizeof( input ) ),
2946 PSA_ERROR_BAD_STATE );
2947 TEST_EQUAL( psa_mac_update( &zero,
2948 input, sizeof( input ) ),
2949 PSA_ERROR_BAD_STATE );
2950
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002951 /* A default MAC operation should be abortable without error. */
2952 PSA_ASSERT( psa_mac_abort( &func ) );
2953 PSA_ASSERT( psa_mac_abort( &init ) );
2954 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002955}
2956/* END_CASE */
2957
2958/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002959void mac_setup( int key_type_arg,
2960 data_t *key,
2961 int alg_arg,
2962 int expected_status_arg )
2963{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002964 psa_key_type_t key_type = key_type_arg;
2965 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002966 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002967 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002968 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2969#if defined(KNOWN_SUPPORTED_MAC_ALG)
2970 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2971#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002972
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002974
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002975 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2976 &operation, &status ) )
2977 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002978 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002979
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002980 /* The operation object should be reusable. */
2981#if defined(KNOWN_SUPPORTED_MAC_ALG)
2982 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2983 smoke_test_key_data,
2984 sizeof( smoke_test_key_data ),
2985 KNOWN_SUPPORTED_MAC_ALG,
2986 &operation, &status ) )
2987 goto exit;
2988 TEST_EQUAL( status, PSA_SUCCESS );
2989#endif
2990
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002991exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002992 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002993}
2994/* END_CASE */
2995
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002996/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00002997void mac_bad_order( )
2998{
Ronald Cron5425a212020-08-04 14:58:35 +02002999 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003000 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3001 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003002 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003003 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3004 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3005 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003007 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3008 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3009 size_t sign_mac_length = 0;
3010 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3011 const uint8_t verify_mac[] = {
3012 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3013 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3014 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3015
3016 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003017 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003018 psa_set_key_algorithm( &attributes, alg );
3019 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003020
Ronald Cron5425a212020-08-04 14:58:35 +02003021 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3022 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003023
Jaeden Amero252ef282019-02-15 14:05:35 +00003024 /* Call update without calling setup beforehand. */
3025 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3026 PSA_ERROR_BAD_STATE );
3027 PSA_ASSERT( psa_mac_abort( &operation ) );
3028
3029 /* Call sign finish without calling setup beforehand. */
3030 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3031 &sign_mac_length),
3032 PSA_ERROR_BAD_STATE );
3033 PSA_ASSERT( psa_mac_abort( &operation ) );
3034
3035 /* Call verify finish without calling setup beforehand. */
3036 TEST_EQUAL( psa_mac_verify_finish( &operation,
3037 verify_mac, sizeof( verify_mac ) ),
3038 PSA_ERROR_BAD_STATE );
3039 PSA_ASSERT( psa_mac_abort( &operation ) );
3040
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003041 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003042 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
3043 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003044 PSA_ERROR_BAD_STATE );
3045 PSA_ASSERT( psa_mac_abort( &operation ) );
3046
Jaeden Amero252ef282019-02-15 14:05:35 +00003047 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003048 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003049 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3050 PSA_ASSERT( psa_mac_sign_finish( &operation,
3051 sign_mac, sizeof( sign_mac ),
3052 &sign_mac_length ) );
3053 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3054 PSA_ERROR_BAD_STATE );
3055 PSA_ASSERT( psa_mac_abort( &operation ) );
3056
3057 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003058 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003059 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3060 PSA_ASSERT( psa_mac_verify_finish( &operation,
3061 verify_mac, sizeof( verify_mac ) ) );
3062 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3063 PSA_ERROR_BAD_STATE );
3064 PSA_ASSERT( psa_mac_abort( &operation ) );
3065
3066 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003067 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003068 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3069 PSA_ASSERT( psa_mac_sign_finish( &operation,
3070 sign_mac, sizeof( sign_mac ),
3071 &sign_mac_length ) );
3072 TEST_EQUAL( psa_mac_sign_finish( &operation,
3073 sign_mac, sizeof( sign_mac ),
3074 &sign_mac_length ),
3075 PSA_ERROR_BAD_STATE );
3076 PSA_ASSERT( psa_mac_abort( &operation ) );
3077
3078 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003079 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003080 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3081 PSA_ASSERT( psa_mac_verify_finish( &operation,
3082 verify_mac, sizeof( verify_mac ) ) );
3083 TEST_EQUAL( psa_mac_verify_finish( &operation,
3084 verify_mac, sizeof( verify_mac ) ),
3085 PSA_ERROR_BAD_STATE );
3086 PSA_ASSERT( psa_mac_abort( &operation ) );
3087
3088 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003089 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003090 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3091 TEST_EQUAL( psa_mac_verify_finish( &operation,
3092 verify_mac, sizeof( verify_mac ) ),
3093 PSA_ERROR_BAD_STATE );
3094 PSA_ASSERT( psa_mac_abort( &operation ) );
3095
3096 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003097 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003098 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3099 TEST_EQUAL( psa_mac_sign_finish( &operation,
3100 sign_mac, sizeof( sign_mac ),
3101 &sign_mac_length ),
3102 PSA_ERROR_BAD_STATE );
3103 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003104
Ronald Cron5425a212020-08-04 14:58:35 +02003105 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003106
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003107exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003108 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003109}
3110/* END_CASE */
3111
3112/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003113void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003114 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003115 int alg_arg,
3116 data_t *input,
3117 data_t *expected_mac )
3118{
Ronald Cron5425a212020-08-04 14:58:35 +02003119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003120 psa_key_type_t key_type = key_type_arg;
3121 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003122 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003124 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003125 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003126 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003127 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003128 const size_t output_sizes_to_test[] = {
3129 0,
3130 1,
3131 expected_mac->len - 1,
3132 expected_mac->len,
3133 expected_mac->len + 1,
3134 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003135
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003136 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003137 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003138 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003139
Gilles Peskine8817f612018-12-18 00:18:46 +01003140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003141
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003142 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003143 psa_set_key_algorithm( &attributes, alg );
3144 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003145
Ronald Cron5425a212020-08-04 14:58:35 +02003146 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3147 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003148
Gilles Peskine8b356b52020-08-25 23:44:59 +02003149 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3150 {
3151 const size_t output_size = output_sizes_to_test[i];
3152 psa_status_t expected_status =
3153 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3154 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003155
Chris Jones9634bb12021-01-20 15:56:42 +00003156 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003157 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003158
Gilles Peskine8b356b52020-08-25 23:44:59 +02003159 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003160 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003161 PSA_ASSERT( psa_mac_update( &operation,
3162 input->x, input->len ) );
3163 TEST_EQUAL( psa_mac_sign_finish( &operation,
3164 actual_mac, output_size,
3165 &mac_length ),
3166 expected_status );
3167 PSA_ASSERT( psa_mac_abort( &operation ) );
3168
3169 if( expected_status == PSA_SUCCESS )
3170 {
3171 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3172 actual_mac, mac_length );
3173 }
3174 mbedtls_free( actual_mac );
3175 actual_mac = NULL;
3176 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003177
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003178exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003179 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003180 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003181 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003182 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003183}
3184/* END_CASE */
3185
3186/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003187void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003188 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003189 int alg_arg,
3190 data_t *input,
3191 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003192{
Ronald Cron5425a212020-08-04 14:58:35 +02003193 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003194 psa_key_type_t key_type = key_type_arg;
3195 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003196 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003198 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003199
Gilles Peskine69c12672018-06-28 00:07:19 +02003200 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3201
Gilles Peskine8817f612018-12-18 00:18:46 +01003202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003203
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003205 psa_set_key_algorithm( &attributes, alg );
3206 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003207
Ronald Cron5425a212020-08-04 14:58:35 +02003208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3209 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003210
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003211 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003212 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003213 PSA_ASSERT( psa_mac_update( &operation,
3214 input->x, input->len ) );
3215 PSA_ASSERT( psa_mac_verify_finish( &operation,
3216 expected_mac->x,
3217 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003218
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003219 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003220 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003221 PSA_ASSERT( psa_mac_update( &operation,
3222 input->x, input->len ) );
3223 TEST_EQUAL( psa_mac_verify_finish( &operation,
3224 expected_mac->x,
3225 expected_mac->len - 1 ),
3226 PSA_ERROR_INVALID_SIGNATURE );
3227
3228 /* Test a MAC that's too long. */
3229 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3230 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003231 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003232 PSA_ASSERT( psa_mac_update( &operation,
3233 input->x, input->len ) );
3234 TEST_EQUAL( psa_mac_verify_finish( &operation,
3235 perturbed_mac,
3236 expected_mac->len + 1 ),
3237 PSA_ERROR_INVALID_SIGNATURE );
3238
3239 /* Test changing one byte. */
3240 for( size_t i = 0; i < expected_mac->len; i++ )
3241 {
Chris Jones9634bb12021-01-20 15:56:42 +00003242 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003243 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003244 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003245 PSA_ASSERT( psa_mac_update( &operation,
3246 input->x, input->len ) );
3247 TEST_EQUAL( psa_mac_verify_finish( &operation,
3248 perturbed_mac,
3249 expected_mac->len ),
3250 PSA_ERROR_INVALID_SIGNATURE );
3251 perturbed_mac[i] ^= 1;
3252 }
3253
Gilles Peskine8c9def32018-02-08 10:02:12 +01003254exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003255 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003256 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003257 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003258 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003259}
3260/* END_CASE */
3261
3262/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003263void cipher_operation_init( )
3264{
Jaeden Ameroab439972019-02-15 14:12:05 +00003265 const uint8_t input[1] = { 0 };
3266 unsigned char output[1] = { 0 };
3267 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003268 /* Test each valid way of initializing the object, except for `= {0}`, as
3269 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3270 * though it's OK by the C standard. We could test for this, but we'd need
3271 * to supress the Clang warning for the test. */
3272 psa_cipher_operation_t func = psa_cipher_operation_init( );
3273 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3274 psa_cipher_operation_t zero;
3275
3276 memset( &zero, 0, sizeof( zero ) );
3277
Jaeden Ameroab439972019-02-15 14:12:05 +00003278 /* A freshly-initialized cipher operation should not be usable. */
3279 TEST_EQUAL( psa_cipher_update( &func,
3280 input, sizeof( input ),
3281 output, sizeof( output ),
3282 &output_length ),
3283 PSA_ERROR_BAD_STATE );
3284 TEST_EQUAL( psa_cipher_update( &init,
3285 input, sizeof( input ),
3286 output, sizeof( output ),
3287 &output_length ),
3288 PSA_ERROR_BAD_STATE );
3289 TEST_EQUAL( psa_cipher_update( &zero,
3290 input, sizeof( input ),
3291 output, sizeof( output ),
3292 &output_length ),
3293 PSA_ERROR_BAD_STATE );
3294
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003295 /* A default cipher operation should be abortable without error. */
3296 PSA_ASSERT( psa_cipher_abort( &func ) );
3297 PSA_ASSERT( psa_cipher_abort( &init ) );
3298 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003299}
3300/* END_CASE */
3301
3302/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003303void cipher_setup( int key_type_arg,
3304 data_t *key,
3305 int alg_arg,
3306 int expected_status_arg )
3307{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003308 psa_key_type_t key_type = key_type_arg;
3309 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003310 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003311 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003312 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003313#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003314 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3315#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003316
Gilles Peskine8817f612018-12-18 00:18:46 +01003317 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003318
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003319 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3320 &operation, &status ) )
3321 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003322 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003323
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003324 /* The operation object should be reusable. */
3325#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3326 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3327 smoke_test_key_data,
3328 sizeof( smoke_test_key_data ),
3329 KNOWN_SUPPORTED_CIPHER_ALG,
3330 &operation, &status ) )
3331 goto exit;
3332 TEST_EQUAL( status, PSA_SUCCESS );
3333#endif
3334
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003335exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003336 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003337 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003338}
3339/* END_CASE */
3340
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003341/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003342void cipher_bad_order( )
3343{
Ronald Cron5425a212020-08-04 14:58:35 +02003344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003345 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3346 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003348 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003349 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003350 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003351 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3352 0xaa, 0xaa, 0xaa, 0xaa };
3353 const uint8_t text[] = {
3354 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3355 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003356 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003357 size_t length = 0;
3358
3359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003360 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3361 psa_set_key_algorithm( &attributes, alg );
3362 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003363 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3364 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003365
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003366 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003367 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3368 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003369 PSA_ERROR_BAD_STATE );
3370 PSA_ASSERT( psa_cipher_abort( &operation ) );
3371
3372 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003373 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3374 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003375 PSA_ERROR_BAD_STATE );
3376 PSA_ASSERT( psa_cipher_abort( &operation ) );
3377
Jaeden Ameroab439972019-02-15 14:12:05 +00003378 /* Generate an IV without calling setup beforehand. */
3379 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3380 buffer, sizeof( buffer ),
3381 &length ),
3382 PSA_ERROR_BAD_STATE );
3383 PSA_ASSERT( psa_cipher_abort( &operation ) );
3384
3385 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003386 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003387 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3388 buffer, sizeof( buffer ),
3389 &length ) );
3390 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3391 buffer, sizeof( buffer ),
3392 &length ),
3393 PSA_ERROR_BAD_STATE );
3394 PSA_ASSERT( psa_cipher_abort( &operation ) );
3395
3396 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003397 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003398 PSA_ASSERT( psa_cipher_set_iv( &operation,
3399 iv, sizeof( iv ) ) );
3400 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3401 buffer, sizeof( buffer ),
3402 &length ),
3403 PSA_ERROR_BAD_STATE );
3404 PSA_ASSERT( psa_cipher_abort( &operation ) );
3405
3406 /* Set an IV without calling setup beforehand. */
3407 TEST_EQUAL( psa_cipher_set_iv( &operation,
3408 iv, sizeof( iv ) ),
3409 PSA_ERROR_BAD_STATE );
3410 PSA_ASSERT( psa_cipher_abort( &operation ) );
3411
3412 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003413 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003414 PSA_ASSERT( psa_cipher_set_iv( &operation,
3415 iv, sizeof( iv ) ) );
3416 TEST_EQUAL( psa_cipher_set_iv( &operation,
3417 iv, sizeof( iv ) ),
3418 PSA_ERROR_BAD_STATE );
3419 PSA_ASSERT( psa_cipher_abort( &operation ) );
3420
3421 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003422 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003423 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3424 buffer, sizeof( buffer ),
3425 &length ) );
3426 TEST_EQUAL( psa_cipher_set_iv( &operation,
3427 iv, sizeof( iv ) ),
3428 PSA_ERROR_BAD_STATE );
3429 PSA_ASSERT( psa_cipher_abort( &operation ) );
3430
3431 /* Call update without calling setup beforehand. */
3432 TEST_EQUAL( psa_cipher_update( &operation,
3433 text, sizeof( text ),
3434 buffer, sizeof( buffer ),
3435 &length ),
3436 PSA_ERROR_BAD_STATE );
3437 PSA_ASSERT( psa_cipher_abort( &operation ) );
3438
3439 /* Call update without an IV where an IV is required. */
3440 TEST_EQUAL( psa_cipher_update( &operation,
3441 text, sizeof( text ),
3442 buffer, sizeof( buffer ),
3443 &length ),
3444 PSA_ERROR_BAD_STATE );
3445 PSA_ASSERT( psa_cipher_abort( &operation ) );
3446
3447 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003448 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003449 PSA_ASSERT( psa_cipher_set_iv( &operation,
3450 iv, sizeof( iv ) ) );
3451 PSA_ASSERT( psa_cipher_finish( &operation,
3452 buffer, sizeof( buffer ), &length ) );
3453 TEST_EQUAL( psa_cipher_update( &operation,
3454 text, sizeof( text ),
3455 buffer, sizeof( buffer ),
3456 &length ),
3457 PSA_ERROR_BAD_STATE );
3458 PSA_ASSERT( psa_cipher_abort( &operation ) );
3459
3460 /* Call finish without calling setup beforehand. */
3461 TEST_EQUAL( psa_cipher_finish( &operation,
3462 buffer, sizeof( buffer ), &length ),
3463 PSA_ERROR_BAD_STATE );
3464 PSA_ASSERT( psa_cipher_abort( &operation ) );
3465
3466 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003467 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003468 /* Not calling update means we are encrypting an empty buffer, which is OK
3469 * for cipher modes with padding. */
3470 TEST_EQUAL( psa_cipher_finish( &operation,
3471 buffer, sizeof( buffer ), &length ),
3472 PSA_ERROR_BAD_STATE );
3473 PSA_ASSERT( psa_cipher_abort( &operation ) );
3474
3475 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003476 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003477 PSA_ASSERT( psa_cipher_set_iv( &operation,
3478 iv, sizeof( iv ) ) );
3479 PSA_ASSERT( psa_cipher_finish( &operation,
3480 buffer, sizeof( buffer ), &length ) );
3481 TEST_EQUAL( psa_cipher_finish( &operation,
3482 buffer, sizeof( buffer ), &length ),
3483 PSA_ERROR_BAD_STATE );
3484 PSA_ASSERT( psa_cipher_abort( &operation ) );
3485
Ronald Cron5425a212020-08-04 14:58:35 +02003486 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003487
Jaeden Ameroab439972019-02-15 14:12:05 +00003488exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003489 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003490 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003491}
3492/* END_CASE */
3493
3494/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003495void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003496 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003497 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003498 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003499{
Ronald Cron5425a212020-08-04 14:58:35 +02003500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003501 psa_status_t status;
3502 psa_key_type_t key_type = key_type_arg;
3503 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003504 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003505 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003506 size_t output_buffer_size = 0;
3507 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003508 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003509 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003511
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003513
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003514 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3515 psa_set_key_algorithm( &attributes, alg );
3516 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003517
Ronald Cron5425a212020-08-04 14:58:35 +02003518 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3519 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003520
Ronald Cron5425a212020-08-04 14:58:35 +02003521 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003522
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003523 if( iv->len > 0 )
3524 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003525 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003526 }
3527
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003528 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003529 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003530 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003531
Gilles Peskine8817f612018-12-18 00:18:46 +01003532 PSA_ASSERT( psa_cipher_update( &operation,
3533 input->x, input->len,
3534 output, output_buffer_size,
3535 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003536 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003537 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003538 output + total_output_length,
3539 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003540 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003541 total_output_length += function_output_length;
3542
Gilles Peskinefe11b722018-12-18 00:24:04 +01003543 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003544 if( expected_status == PSA_SUCCESS )
3545 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003547 ASSERT_COMPARE( expected_output->x, expected_output->len,
3548 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003549 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003550
Gilles Peskine50e586b2018-06-08 14:28:46 +02003551exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003552 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003553 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003554 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003555 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003556}
3557/* END_CASE */
3558
3559/* BEGIN_CASE */
3560void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003561 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003562 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003563 int first_part_size_arg,
3564 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003565 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003566{
Ronald Cron5425a212020-08-04 14:58:35 +02003567 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003568 psa_key_type_t key_type = key_type_arg;
3569 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003570 size_t first_part_size = first_part_size_arg;
3571 size_t output1_length = output1_length_arg;
3572 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003573 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003574 size_t output_buffer_size = 0;
3575 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003576 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003577 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003578 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003579
Gilles Peskine8817f612018-12-18 00:18:46 +01003580 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003581
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003582 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3583 psa_set_key_algorithm( &attributes, alg );
3584 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003585
Ronald Cron5425a212020-08-04 14:58:35 +02003586 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3587 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003588
Ronald Cron5425a212020-08-04 14:58:35 +02003589 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003590
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003591 if( iv->len > 0 )
3592 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003593 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003594 }
3595
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003596 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003597 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003598 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003599
Gilles Peskinee0866522019-02-19 19:44:00 +01003600 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003601 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3602 output, output_buffer_size,
3603 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003604 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003605 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003606 PSA_ASSERT( psa_cipher_update( &operation,
3607 input->x + first_part_size,
3608 input->len - first_part_size,
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 Peskinee0866522019-02-19 19:44:00 +01003612 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003613 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003614 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003615 output + total_output_length,
3616 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003617 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003618 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003619 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003620
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003621 ASSERT_COMPARE( expected_output->x, expected_output->len,
3622 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003623
3624exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003625 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003626 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003627 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003628 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003629}
3630/* END_CASE */
3631
3632/* BEGIN_CASE */
3633void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003634 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003635 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003636 int first_part_size_arg,
3637 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003638 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003639{
Ronald Cron5425a212020-08-04 14:58:35 +02003640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003641 psa_key_type_t key_type = key_type_arg;
3642 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003643 size_t first_part_size = first_part_size_arg;
3644 size_t output1_length = output1_length_arg;
3645 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003646 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003647 size_t output_buffer_size = 0;
3648 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003649 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003650 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003652
Gilles Peskine8817f612018-12-18 00:18:46 +01003653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003654
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003655 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3656 psa_set_key_algorithm( &attributes, alg );
3657 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003658
Ronald Cron5425a212020-08-04 14:58:35 +02003659 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3660 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003661
Ronald Cron5425a212020-08-04 14:58:35 +02003662 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003663
Steven Cooreman177deba2020-09-07 17:14:14 +02003664 if( iv->len > 0 )
3665 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003666 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003667 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003668
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003669 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003670 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003671 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003672
Gilles Peskinee0866522019-02-19 19:44:00 +01003673 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003674 PSA_ASSERT( psa_cipher_update( &operation,
3675 input->x, first_part_size,
3676 output, output_buffer_size,
3677 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003678 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003679 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003680 PSA_ASSERT( psa_cipher_update( &operation,
3681 input->x + first_part_size,
3682 input->len - first_part_size,
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 Peskinee0866522019-02-19 19:44:00 +01003686 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003687 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003688 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003689 output + total_output_length,
3690 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003691 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003692 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003693 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003694
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003695 ASSERT_COMPARE( expected_output->x, expected_output->len,
3696 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003697
3698exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003699 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003700 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003702 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003703}
3704/* END_CASE */
3705
Gilles Peskine50e586b2018-06-08 14:28:46 +02003706/* BEGIN_CASE */
3707void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003708 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003709 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003710 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003711{
Ronald Cron5425a212020-08-04 14:58:35 +02003712 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003713 psa_status_t status;
3714 psa_key_type_t key_type = key_type_arg;
3715 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003716 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003717 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003718 size_t output_buffer_size = 0;
3719 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003720 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003721 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003723
Gilles Peskine8817f612018-12-18 00:18:46 +01003724 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003725
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003726 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3727 psa_set_key_algorithm( &attributes, alg );
3728 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003729
Ronald Cron5425a212020-08-04 14:58:35 +02003730 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3731 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003732
Ronald Cron5425a212020-08-04 14:58:35 +02003733 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003734
Steven Cooreman177deba2020-09-07 17:14:14 +02003735 if( iv->len > 0 )
3736 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003737 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003738 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003739
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003740 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003741 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003742 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003743
Gilles Peskine8817f612018-12-18 00:18:46 +01003744 PSA_ASSERT( psa_cipher_update( &operation,
3745 input->x, input->len,
3746 output, output_buffer_size,
3747 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003748 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003749 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003750 output + total_output_length,
3751 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003752 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003753 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003754 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003755
3756 if( expected_status == PSA_SUCCESS )
3757 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003758 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003759 ASSERT_COMPARE( expected_output->x, expected_output->len,
3760 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003761 }
3762
Gilles Peskine50e586b2018-06-08 14:28:46 +02003763exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003764 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003765 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003766 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003767 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003768}
3769/* END_CASE */
3770
Gilles Peskine50e586b2018-06-08 14:28:46 +02003771/* BEGIN_CASE */
3772void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003773 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003774 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003775{
Ronald Cron5425a212020-08-04 14:58:35 +02003776 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003777 psa_key_type_t key_type = key_type_arg;
3778 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003779 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003780 size_t iv_size = 16;
3781 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003782 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003783 size_t output1_size = 0;
3784 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003785 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003786 size_t output2_size = 0;
3787 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003788 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003789 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3790 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003791 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003792
Gilles Peskine8817f612018-12-18 00:18:46 +01003793 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003794
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003795 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3796 psa_set_key_algorithm( &attributes, alg );
3797 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003798
Ronald Cron5425a212020-08-04 14:58:35 +02003799 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3800 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003801
Ronald Cron5425a212020-08-04 14:58:35 +02003802 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3803 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003804
Steven Cooreman177deba2020-09-07 17:14:14 +02003805 if( alg != PSA_ALG_ECB_NO_PADDING )
3806 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003807 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3808 iv, iv_size,
3809 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003810 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003811 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003812 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003813 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003814
Gilles Peskine8817f612018-12-18 00:18:46 +01003815 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3816 output1, output1_size,
3817 &output1_length ) );
3818 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003819 output1 + output1_length,
3820 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003821 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003822
Gilles Peskine048b7f02018-06-08 14:20:49 +02003823 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003824
Gilles Peskine8817f612018-12-18 00:18:46 +01003825 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003826
3827 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003828 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003829
Steven Cooreman177deba2020-09-07 17:14:14 +02003830 if( iv_length > 0 )
3831 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003832 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3833 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003834 }
3835
Gilles Peskine8817f612018-12-18 00:18:46 +01003836 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3837 output2, output2_size,
3838 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003839 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003840 PSA_ASSERT( psa_cipher_finish( &operation2,
3841 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003842 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003843 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003844
Gilles Peskine048b7f02018-06-08 14:20:49 +02003845 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003846
Gilles Peskine8817f612018-12-18 00:18:46 +01003847 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003848
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003849 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003850
3851exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003852 psa_cipher_abort( &operation1 );
3853 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003854 mbedtls_free( output1 );
3855 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003856 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003857 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003858}
3859/* END_CASE */
3860
3861/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003862void cipher_verify_output_multipart( int alg_arg,
3863 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003864 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003865 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003866 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003867{
Ronald Cron5425a212020-08-04 14:58:35 +02003868 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003869 psa_key_type_t key_type = key_type_arg;
3870 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003871 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003872 unsigned char iv[16] = {0};
3873 size_t iv_size = 16;
3874 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003875 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003876 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003877 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003878 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003879 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003880 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003881 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003882 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3883 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003884 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003885
Gilles Peskine8817f612018-12-18 00:18:46 +01003886 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003887
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003888 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3889 psa_set_key_algorithm( &attributes, alg );
3890 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003891
Ronald Cron5425a212020-08-04 14:58:35 +02003892 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3893 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003894
Ronald Cron5425a212020-08-04 14:58:35 +02003895 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3896 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003897
Steven Cooreman177deba2020-09-07 17:14:14 +02003898 if( alg != PSA_ALG_ECB_NO_PADDING )
3899 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003900 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3901 iv, iv_size,
3902 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003903 }
3904
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003905 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003906 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003907 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003908
Gilles Peskinee0866522019-02-19 19:44:00 +01003909 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003910
Gilles Peskine8817f612018-12-18 00:18:46 +01003911 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3912 output1, output1_buffer_size,
3913 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003914 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003915
Gilles Peskine8817f612018-12-18 00:18:46 +01003916 PSA_ASSERT( psa_cipher_update( &operation1,
3917 input->x + first_part_size,
3918 input->len - first_part_size,
3919 output1, output1_buffer_size,
3920 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003921 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003922
Gilles Peskine8817f612018-12-18 00:18:46 +01003923 PSA_ASSERT( psa_cipher_finish( &operation1,
3924 output1 + output1_length,
3925 output1_buffer_size - output1_length,
3926 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003927 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003928
Gilles Peskine8817f612018-12-18 00:18:46 +01003929 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003930
Gilles Peskine048b7f02018-06-08 14:20:49 +02003931 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003932 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003933
Steven Cooreman177deba2020-09-07 17:14:14 +02003934 if( iv_length > 0 )
3935 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003936 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3937 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003938 }
Moran Pekerded84402018-06-06 16:36:50 +03003939
Gilles Peskine8817f612018-12-18 00:18:46 +01003940 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3941 output2, output2_buffer_size,
3942 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003943 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003944
Gilles Peskine8817f612018-12-18 00:18:46 +01003945 PSA_ASSERT( psa_cipher_update( &operation2,
3946 output1 + first_part_size,
3947 output1_length - first_part_size,
3948 output2, output2_buffer_size,
3949 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003950 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003951
Gilles Peskine8817f612018-12-18 00:18:46 +01003952 PSA_ASSERT( psa_cipher_finish( &operation2,
3953 output2 + output2_length,
3954 output2_buffer_size - output2_length,
3955 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003956 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003957
Gilles Peskine8817f612018-12-18 00:18:46 +01003958 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003959
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003960 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003961
3962exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003963 psa_cipher_abort( &operation1 );
3964 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003965 mbedtls_free( output1 );
3966 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003967 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003968 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003969}
3970/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003971
Gilles Peskine20035e32018-02-03 22:44:14 +01003972/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003973void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003974 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003975 data_t *nonce,
3976 data_t *additional_data,
3977 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003978 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003979{
Ronald Cron5425a212020-08-04 14:58:35 +02003980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003981 psa_key_type_t key_type = key_type_arg;
3982 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003983 unsigned char *output_data = NULL;
3984 size_t output_size = 0;
3985 size_t output_length = 0;
3986 unsigned char *output_data2 = NULL;
3987 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003988 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003989 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003991
Gilles Peskine4abf7412018-06-18 16:35:34 +02003992 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003993 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3994 * should be exact. */
3995 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3996 TEST_EQUAL( output_size,
3997 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003998 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003999
Gilles Peskine8817f612018-12-18 00:18:46 +01004000 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004001
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004002 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4003 psa_set_key_algorithm( &attributes, alg );
4004 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004005
Gilles Peskine049c7532019-05-15 20:22:09 +02004006 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004007 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004008
Ronald Cron5425a212020-08-04 14:58:35 +02004009 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004010 nonce->x, nonce->len,
4011 additional_data->x,
4012 additional_data->len,
4013 input_data->x, input_data->len,
4014 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004015 &output_length ),
4016 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004017
4018 if( PSA_SUCCESS == expected_result )
4019 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004020 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004021
Gilles Peskine003a4a92019-05-14 16:09:40 +02004022 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4023 * should be exact. */
4024 TEST_EQUAL( input_data->len,
4025 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
4026
Ronald Cron5425a212020-08-04 14:58:35 +02004027 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004028 nonce->x, nonce->len,
4029 additional_data->x,
4030 additional_data->len,
4031 output_data, output_length,
4032 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004033 &output_length2 ),
4034 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004035
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004036 ASSERT_COMPARE( input_data->x, input_data->len,
4037 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004038 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004039
Gilles Peskinea1cac842018-06-11 19:33:02 +02004040exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004041 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004042 mbedtls_free( output_data );
4043 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004044 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004045}
4046/* END_CASE */
4047
4048/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004049void aead_encrypt( int key_type_arg, data_t *key_data,
4050 int alg_arg,
4051 data_t *nonce,
4052 data_t *additional_data,
4053 data_t *input_data,
4054 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004055{
Ronald Cron5425a212020-08-04 14:58:35 +02004056 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004057 psa_key_type_t key_type = key_type_arg;
4058 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004059 unsigned char *output_data = NULL;
4060 size_t output_size = 0;
4061 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004062 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004063 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004064 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004065
Gilles Peskine4abf7412018-06-18 16:35:34 +02004066 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004067 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4068 * should be exact. */
4069 TEST_EQUAL( output_size,
4070 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004071 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004072
Gilles Peskine8817f612018-12-18 00:18:46 +01004073 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004074
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004075 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4076 psa_set_key_algorithm( &attributes, alg );
4077 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004078
Gilles Peskine049c7532019-05-15 20:22:09 +02004079 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004080 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004081
Steven Cooremand588ea12021-01-11 19:36:04 +01004082 status = psa_aead_encrypt( key, alg,
4083 nonce->x, nonce->len,
4084 additional_data->x, additional_data->len,
4085 input_data->x, input_data->len,
4086 output_data, output_size,
4087 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004088
Steven Cooremand588ea12021-01-11 19:36:04 +01004089#if defined(MBEDTLS_AES_ALT) || \
4090 defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
4091 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
4092 if( status == PSA_ERROR_NOT_SUPPORTED &&
4093 key_type == PSA_KEY_TYPE_AES &&
4094 key_data->len == 24 )
4095 {
4096 test_skip( "AES-192 not supported", __LINE__, __FILE__ );
4097 goto exit;
4098 }
4099#endif /* AES could be alternatively implemented */
4100
4101 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004102 ASSERT_COMPARE( expected_result->x, expected_result->len,
4103 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004104
Gilles Peskinea1cac842018-06-11 19:33:02 +02004105exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004106 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004107 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004108 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004109}
4110/* END_CASE */
4111
4112/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004113void aead_decrypt( int key_type_arg, data_t *key_data,
4114 int alg_arg,
4115 data_t *nonce,
4116 data_t *additional_data,
4117 data_t *input_data,
4118 data_t *expected_data,
4119 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004120{
Ronald Cron5425a212020-08-04 14:58:35 +02004121 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004122 psa_key_type_t key_type = key_type_arg;
4123 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004124 unsigned char *output_data = NULL;
4125 size_t output_size = 0;
4126 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004127 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004129 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004130 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004131
Gilles Peskine003a4a92019-05-14 16:09:40 +02004132 output_size = input_data->len - tag_length;
4133 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4134 * should be exact. */
4135 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4136 TEST_EQUAL( output_size,
4137 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004138 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004139
Gilles Peskine8817f612018-12-18 00:18:46 +01004140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004141
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004142 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4143 psa_set_key_algorithm( &attributes, alg );
4144 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004145
Gilles Peskine049c7532019-05-15 20:22:09 +02004146 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004147 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004148
Steven Cooremand588ea12021-01-11 19:36:04 +01004149 status = psa_aead_decrypt( key, alg,
4150 nonce->x, nonce->len,
4151 additional_data->x,
4152 additional_data->len,
4153 input_data->x, input_data->len,
4154 output_data, output_size,
4155 &output_length );
4156
4157#if defined(MBEDTLS_AES_ALT) || \
4158 defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
4159 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
4160 if( status == PSA_ERROR_NOT_SUPPORTED &&
4161 key_type == PSA_KEY_TYPE_AES &&
4162 key_data->len == 24 )
4163 {
4164 test_skip( "AES-192 not supported", __LINE__, __FILE__ );
4165 goto exit;
4166 }
4167#endif /* AES could be alternatively implemented */
4168
4169 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004170
Gilles Peskine2d277862018-06-18 15:41:12 +02004171 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004172 ASSERT_COMPARE( expected_data->x, expected_data->len,
4173 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004174
Gilles Peskinea1cac842018-06-11 19:33:02 +02004175exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004176 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004177 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004178 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004179}
4180/* END_CASE */
4181
4182/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004183void signature_size( int type_arg,
4184 int bits,
4185 int alg_arg,
4186 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004187{
4188 psa_key_type_t type = type_arg;
4189 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004190 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004191
Gilles Peskinefe11b722018-12-18 00:24:04 +01004192 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004193#if defined(MBEDTLS_TEST_DEPRECATED)
4194 TEST_EQUAL( actual_size,
4195 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4196#endif /* MBEDTLS_TEST_DEPRECATED */
4197
Gilles Peskinee59236f2018-01-27 23:32:46 +01004198exit:
4199 ;
4200}
4201/* END_CASE */
4202
4203/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004204void sign_deterministic( int key_type_arg, data_t *key_data,
4205 int alg_arg, data_t *input_data,
4206 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004207{
Ronald Cron5425a212020-08-04 14:58:35 +02004208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004209 psa_key_type_t key_type = key_type_arg;
4210 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004211 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004212 unsigned char *signature = NULL;
4213 size_t signature_size;
4214 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004216
Gilles Peskine8817f612018-12-18 00:18:46 +01004217 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004218
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004220 psa_set_key_algorithm( &attributes, alg );
4221 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004222
Gilles Peskine049c7532019-05-15 20:22:09 +02004223 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004224 &key ) );
4225 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004226 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004227
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004228 /* Allocate a buffer which has the size advertized by the
4229 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004230 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004231 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004232 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004233 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004234 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004235
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004236 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004237 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004238 input_data->x, input_data->len,
4239 signature, signature_size,
4240 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004241 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004242 ASSERT_COMPARE( output_data->x, output_data->len,
4243 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004244
Gilles Peskine0627f982019-11-26 19:12:16 +01004245#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004246 memset( signature, 0, signature_size );
4247 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004248 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004249 input_data->x, input_data->len,
4250 signature, signature_size,
4251 &signature_length ) );
4252 ASSERT_COMPARE( output_data->x, output_data->len,
4253 signature, signature_length );
4254#endif /* MBEDTLS_TEST_DEPRECATED */
4255
Gilles Peskine20035e32018-02-03 22:44:14 +01004256exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004257 /*
4258 * Key attributes may have been returned by psa_get_key_attributes()
4259 * thus reset them as required.
4260 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004261 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004262
Ronald Cron5425a212020-08-04 14:58:35 +02004263 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004264 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004265 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004266}
4267/* END_CASE */
4268
4269/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004270void sign_fail( int key_type_arg, data_t *key_data,
4271 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004272 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004273{
Ronald Cron5425a212020-08-04 14:58:35 +02004274 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004275 psa_key_type_t key_type = key_type_arg;
4276 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004277 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004278 psa_status_t actual_status;
4279 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004280 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004281 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004283
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004284 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004285
Gilles Peskine8817f612018-12-18 00:18:46 +01004286 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004287
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004288 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004289 psa_set_key_algorithm( &attributes, alg );
4290 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004291
Gilles Peskine049c7532019-05-15 20:22:09 +02004292 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004293 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004294
Ronald Cron5425a212020-08-04 14:58:35 +02004295 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004296 input_data->x, input_data->len,
4297 signature, signature_size,
4298 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004299 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004300 /* The value of *signature_length is unspecified on error, but
4301 * whatever it is, it should be less than signature_size, so that
4302 * if the caller tries to read *signature_length bytes without
4303 * checking the error code then they don't overflow a buffer. */
4304 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004305
Gilles Peskine895242b2019-11-29 12:15:40 +01004306#if defined(MBEDTLS_TEST_DEPRECATED)
4307 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004308 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004309 input_data->x, input_data->len,
4310 signature, signature_size,
4311 &signature_length ),
4312 expected_status );
4313 TEST_ASSERT( signature_length <= signature_size );
4314#endif /* MBEDTLS_TEST_DEPRECATED */
4315
Gilles Peskine20035e32018-02-03 22:44:14 +01004316exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004317 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004318 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004319 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004320 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004321}
4322/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004323
4324/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004325void sign_verify( int key_type_arg, data_t *key_data,
4326 int alg_arg, data_t *input_data )
4327{
Ronald Cron5425a212020-08-04 14:58:35 +02004328 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004329 psa_key_type_t key_type = key_type_arg;
4330 psa_algorithm_t alg = alg_arg;
4331 size_t key_bits;
4332 unsigned char *signature = NULL;
4333 size_t signature_size;
4334 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004336
Gilles Peskine8817f612018-12-18 00:18:46 +01004337 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004338
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004339 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004340 psa_set_key_algorithm( &attributes, alg );
4341 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004342
Gilles Peskine049c7532019-05-15 20:22:09 +02004343 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004344 &key ) );
4345 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004346 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004347
4348 /* Allocate a buffer which has the size advertized by the
4349 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004350 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004351 key_bits, alg );
4352 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004353 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004354 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004355
4356 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004357 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004358 input_data->x, input_data->len,
4359 signature, signature_size,
4360 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004361 /* Check that the signature length looks sensible. */
4362 TEST_ASSERT( signature_length <= signature_size );
4363 TEST_ASSERT( signature_length > 0 );
4364
4365 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004366 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004367 input_data->x, input_data->len,
4368 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004369
4370 if( input_data->len != 0 )
4371 {
4372 /* Flip a bit in the input and verify that the signature is now
4373 * detected as invalid. Flip a bit at the beginning, not at the end,
4374 * because ECDSA may ignore the last few bits of the input. */
4375 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004376 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004377 input_data->x, input_data->len,
4378 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004379 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004380 }
4381
4382exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004383 /*
4384 * Key attributes may have been returned by psa_get_key_attributes()
4385 * thus reset them as required.
4386 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004387 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004388
Ronald Cron5425a212020-08-04 14:58:35 +02004389 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004390 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004391 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004392}
4393/* END_CASE */
4394
4395/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004396void asymmetric_verify( int key_type_arg, data_t *key_data,
4397 int alg_arg, data_t *hash_data,
4398 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004399{
Ronald Cron5425a212020-08-04 14:58:35 +02004400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004401 psa_key_type_t key_type = key_type_arg;
4402 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004403 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004404
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004405 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004406
Gilles Peskine8817f612018-12-18 00:18:46 +01004407 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004408
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004409 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004410 psa_set_key_algorithm( &attributes, alg );
4411 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004412
Gilles Peskine049c7532019-05-15 20:22:09 +02004413 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004414 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004415
Ronald Cron5425a212020-08-04 14:58:35 +02004416 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004417 hash_data->x, hash_data->len,
4418 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004419
4420#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004421 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004422 hash_data->x, hash_data->len,
4423 signature_data->x,
4424 signature_data->len ) );
4425
4426#endif /* MBEDTLS_TEST_DEPRECATED */
4427
itayzafrir5c753392018-05-08 11:18:38 +03004428exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004429 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004430 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004431 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004432}
4433/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004434
4435/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004436void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4437 int alg_arg, data_t *hash_data,
4438 data_t *signature_data,
4439 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004440{
Ronald Cron5425a212020-08-04 14:58:35 +02004441 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004442 psa_key_type_t key_type = key_type_arg;
4443 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004444 psa_status_t actual_status;
4445 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004447
Gilles Peskine8817f612018-12-18 00:18:46 +01004448 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004449
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004450 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004451 psa_set_key_algorithm( &attributes, alg );
4452 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004453
Gilles Peskine049c7532019-05-15 20:22:09 +02004454 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004455 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004456
Ronald Cron5425a212020-08-04 14:58:35 +02004457 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004458 hash_data->x, hash_data->len,
4459 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004460 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004461
Gilles Peskine895242b2019-11-29 12:15:40 +01004462#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004463 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004464 hash_data->x, hash_data->len,
4465 signature_data->x, signature_data->len ),
4466 expected_status );
4467#endif /* MBEDTLS_TEST_DEPRECATED */
4468
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004469exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004470 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004471 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004472 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004473}
4474/* END_CASE */
4475
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004476/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004477void asymmetric_encrypt( int key_type_arg,
4478 data_t *key_data,
4479 int alg_arg,
4480 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004481 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004482 int expected_output_length_arg,
4483 int expected_status_arg )
4484{
Ronald Cron5425a212020-08-04 14:58:35 +02004485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004486 psa_key_type_t key_type = key_type_arg;
4487 psa_algorithm_t alg = alg_arg;
4488 size_t expected_output_length = expected_output_length_arg;
4489 size_t key_bits;
4490 unsigned char *output = NULL;
4491 size_t output_size;
4492 size_t output_length = ~0;
4493 psa_status_t actual_status;
4494 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004496
Gilles Peskine8817f612018-12-18 00:18:46 +01004497 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004498
Gilles Peskine656896e2018-06-29 19:12:28 +02004499 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004500 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4501 psa_set_key_algorithm( &attributes, alg );
4502 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004503 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004504 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004505
4506 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004507 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004508 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004509 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004510 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004511
4512 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004513 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004514 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004515 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004516 output, output_size,
4517 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004518 TEST_EQUAL( actual_status, expected_status );
4519 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004520
Gilles Peskine68428122018-06-30 18:42:41 +02004521 /* If the label is empty, the test framework puts a non-null pointer
4522 * in label->x. Test that a null pointer works as well. */
4523 if( label->len == 0 )
4524 {
4525 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004526 if( output_size != 0 )
4527 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004528 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004529 input_data->x, input_data->len,
4530 NULL, label->len,
4531 output, output_size,
4532 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004533 TEST_EQUAL( actual_status, expected_status );
4534 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004535 }
4536
Gilles Peskine656896e2018-06-29 19:12:28 +02004537exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004538 /*
4539 * Key attributes may have been returned by psa_get_key_attributes()
4540 * thus reset them as required.
4541 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004542 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004543
Ronald Cron5425a212020-08-04 14:58:35 +02004544 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004545 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004546 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004547}
4548/* END_CASE */
4549
4550/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004551void asymmetric_encrypt_decrypt( int key_type_arg,
4552 data_t *key_data,
4553 int alg_arg,
4554 data_t *input_data,
4555 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004556{
Ronald Cron5425a212020-08-04 14:58:35 +02004557 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004558 psa_key_type_t key_type = key_type_arg;
4559 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004560 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004561 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004562 size_t output_size;
4563 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004564 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004565 size_t output2_size;
4566 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004568
Gilles Peskine8817f612018-12-18 00:18:46 +01004569 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004570
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4572 psa_set_key_algorithm( &attributes, alg );
4573 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004574
Gilles Peskine049c7532019-05-15 20:22:09 +02004575 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004576 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004577
4578 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004579 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004580 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004581 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004582 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004583 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004584 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004585
Gilles Peskineeebd7382018-06-08 18:11:54 +02004586 /* We test encryption by checking that encrypt-then-decrypt gives back
4587 * the original plaintext because of the non-optional random
4588 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004589 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004590 input_data->x, input_data->len,
4591 label->x, label->len,
4592 output, output_size,
4593 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004594 /* We don't know what ciphertext length to expect, but check that
4595 * it looks sensible. */
4596 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004597
Ronald Cron5425a212020-08-04 14:58:35 +02004598 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004599 output, output_length,
4600 label->x, label->len,
4601 output2, output2_size,
4602 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004603 ASSERT_COMPARE( input_data->x, input_data->len,
4604 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004605
4606exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004607 /*
4608 * Key attributes may have been returned by psa_get_key_attributes()
4609 * thus reset them as required.
4610 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004611 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004612
Ronald Cron5425a212020-08-04 14:58:35 +02004613 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004614 mbedtls_free( output );
4615 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004616 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004617}
4618/* END_CASE */
4619
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004620/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004621void asymmetric_decrypt( int key_type_arg,
4622 data_t *key_data,
4623 int alg_arg,
4624 data_t *input_data,
4625 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004626 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004627{
Ronald Cron5425a212020-08-04 14:58:35 +02004628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004629 psa_key_type_t key_type = key_type_arg;
4630 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004631 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004632 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004633 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004635
Jaeden Amero412654a2019-02-06 12:57:46 +00004636 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004637 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004638
Gilles Peskine8817f612018-12-18 00:18:46 +01004639 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004640
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004641 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4642 psa_set_key_algorithm( &attributes, alg );
4643 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004644
Gilles Peskine049c7532019-05-15 20:22:09 +02004645 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004646 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004647
Ronald Cron5425a212020-08-04 14:58:35 +02004648 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004649 input_data->x, input_data->len,
4650 label->x, label->len,
4651 output,
4652 output_size,
4653 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004654 ASSERT_COMPARE( expected_data->x, expected_data->len,
4655 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004656
Gilles Peskine68428122018-06-30 18:42:41 +02004657 /* If the label is empty, the test framework puts a non-null pointer
4658 * in label->x. Test that a null pointer works as well. */
4659 if( label->len == 0 )
4660 {
4661 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004662 if( output_size != 0 )
4663 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004664 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004665 input_data->x, input_data->len,
4666 NULL, label->len,
4667 output,
4668 output_size,
4669 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004670 ASSERT_COMPARE( expected_data->x, expected_data->len,
4671 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004672 }
4673
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004674exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004675 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004676 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004677 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004678 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004679}
4680/* END_CASE */
4681
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004682/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004683void asymmetric_decrypt_fail( int key_type_arg,
4684 data_t *key_data,
4685 int alg_arg,
4686 data_t *input_data,
4687 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004688 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004689 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004690{
Ronald Cron5425a212020-08-04 14:58:35 +02004691 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004692 psa_key_type_t key_type = key_type_arg;
4693 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004694 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004695 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004696 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004697 psa_status_t actual_status;
4698 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004700
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004701 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004702
Gilles Peskine8817f612018-12-18 00:18:46 +01004703 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004704
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004705 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4706 psa_set_key_algorithm( &attributes, alg );
4707 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004708
Gilles Peskine049c7532019-05-15 20:22:09 +02004709 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004710 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004711
Ronald Cron5425a212020-08-04 14:58:35 +02004712 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004713 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004714 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004715 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004716 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004717 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004718 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004719
Gilles Peskine68428122018-06-30 18:42:41 +02004720 /* If the label is empty, the test framework puts a non-null pointer
4721 * in label->x. Test that a null pointer works as well. */
4722 if( label->len == 0 )
4723 {
4724 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004725 if( output_size != 0 )
4726 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004727 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004728 input_data->x, input_data->len,
4729 NULL, label->len,
4730 output, output_size,
4731 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004732 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004733 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004734 }
4735
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004736exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004737 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004738 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004739 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004740 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004741}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004742/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004743
4744/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004745void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004746{
4747 /* Test each valid way of initializing the object, except for `= {0}`, as
4748 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4749 * though it's OK by the C standard. We could test for this, but we'd need
4750 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004751 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004752 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4753 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4754 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004755
4756 memset( &zero, 0, sizeof( zero ) );
4757
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004758 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004759 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004760 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004761 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004762 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004763 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004764 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004765
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004767 PSA_ASSERT( psa_key_derivation_abort(&func) );
4768 PSA_ASSERT( psa_key_derivation_abort(&init) );
4769 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004770}
4771/* END_CASE */
4772
Janos Follath16de4a42019-06-13 16:32:24 +01004773/* BEGIN_CASE */
4774void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004775{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004776 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004777 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004779
Gilles Peskine8817f612018-12-18 00:18:46 +01004780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004781
Janos Follath16de4a42019-06-13 16:32:24 +01004782 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004783 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004784
4785exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004786 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004787 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004788}
4789/* END_CASE */
4790
Janos Follathaf3c2a02019-06-12 12:34:34 +01004791/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004792void derive_set_capacity( int alg_arg, int capacity_arg,
4793 int expected_status_arg )
4794{
4795 psa_algorithm_t alg = alg_arg;
4796 size_t capacity = capacity_arg;
4797 psa_status_t expected_status = expected_status_arg;
4798 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4799
4800 PSA_ASSERT( psa_crypto_init( ) );
4801
4802 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4803
4804 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4805 expected_status );
4806
4807exit:
4808 psa_key_derivation_abort( &operation );
4809 PSA_DONE( );
4810}
4811/* END_CASE */
4812
4813/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004814void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004815 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004816 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004817 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004818 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004819 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004820 int expected_status_arg3,
4821 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004822{
4823 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004824 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4825 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004826 psa_status_t expected_statuses[] = {expected_status_arg1,
4827 expected_status_arg2,
4828 expected_status_arg3};
4829 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004830 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4831 MBEDTLS_SVC_KEY_ID_INIT,
4832 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004833 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4834 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4835 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004836 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004837 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004838 psa_status_t expected_output_status = expected_output_status_arg;
4839 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004840
4841 PSA_ASSERT( psa_crypto_init( ) );
4842
4843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4844 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004845
4846 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4847
4848 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4849 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004850 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004851 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004852 psa_set_key_type( &attributes, key_types[i] );
4853 PSA_ASSERT( psa_import_key( &attributes,
4854 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004855 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004856 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4857 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4858 {
4859 // When taking a private key as secret input, use key agreement
4860 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004861 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004862 expected_statuses[i] );
4863 }
4864 else
4865 {
4866 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004867 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004868 expected_statuses[i] );
4869 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004870 }
4871 else
4872 {
4873 TEST_EQUAL( psa_key_derivation_input_bytes(
4874 &operation, steps[i],
4875 inputs[i]->x, inputs[i]->len ),
4876 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004877 }
4878 }
4879
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004880 if( output_key_type != PSA_KEY_TYPE_NONE )
4881 {
4882 psa_reset_key_attributes( &attributes );
4883 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4884 psa_set_key_bits( &attributes, 8 );
4885 actual_output_status =
4886 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004887 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004888 }
4889 else
4890 {
4891 uint8_t buffer[1];
4892 actual_output_status =
4893 psa_key_derivation_output_bytes( &operation,
4894 buffer, sizeof( buffer ) );
4895 }
4896 TEST_EQUAL( actual_output_status, expected_output_status );
4897
Janos Follathaf3c2a02019-06-12 12:34:34 +01004898exit:
4899 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004900 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4901 psa_destroy_key( keys[i] );
4902 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004903 PSA_DONE( );
4904}
4905/* END_CASE */
4906
Janos Follathd958bb72019-07-03 15:02:16 +01004907/* BEGIN_CASE */
4908void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004909{
Janos Follathd958bb72019-07-03 15:02:16 +01004910 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004911 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004912 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004914 unsigned char input1[] = "Input 1";
4915 size_t input1_length = sizeof( input1 );
4916 unsigned char input2[] = "Input 2";
4917 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004918 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004919 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004920 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4921 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4922 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004923 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004924
Gilles Peskine8817f612018-12-18 00:18:46 +01004925 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004926
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004927 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4928 psa_set_key_algorithm( &attributes, alg );
4929 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004930
Gilles Peskine73676cb2019-05-15 20:15:10 +02004931 PSA_ASSERT( psa_import_key( &attributes,
4932 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004933 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004934
4935 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004936 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004937 input1, input1_length,
4938 input2, input2_length,
4939 capacity ) )
4940 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004941
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004942 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004943 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004944 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004945
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004946 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004947
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004948 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004949 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004950
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004951exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004952 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004953 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004954 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004955}
4956/* END_CASE */
4957
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004958/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004959void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004960{
4961 uint8_t output_buffer[16];
4962 size_t buffer_size = 16;
4963 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004965
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004966 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4967 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004968 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004969
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004971 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004972
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004974
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004975 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4976 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004977 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004978
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004980 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004981
4982exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004983 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004984}
4985/* END_CASE */
4986
4987/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004988void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004989 int step1_arg, data_t *input1,
4990 int step2_arg, data_t *input2,
4991 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004992 int requested_capacity_arg,
4993 data_t *expected_output1,
4994 data_t *expected_output2 )
4995{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004996 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004997 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4998 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004999 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5000 MBEDTLS_SVC_KEY_ID_INIT,
5001 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005002 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005003 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005004 uint8_t *expected_outputs[2] =
5005 {expected_output1->x, expected_output2->x};
5006 size_t output_sizes[2] =
5007 {expected_output1->len, expected_output2->len};
5008 size_t output_buffer_size = 0;
5009 uint8_t *output_buffer = NULL;
5010 size_t expected_capacity;
5011 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005012 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005013 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005014 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005015
5016 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5017 {
5018 if( output_sizes[i] > output_buffer_size )
5019 output_buffer_size = output_sizes[i];
5020 if( output_sizes[i] == 0 )
5021 expected_outputs[i] = NULL;
5022 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005023 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005024 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005025
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005026 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5027 psa_set_key_algorithm( &attributes, alg );
5028 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005029
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005030 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005031 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5032 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5033 requested_capacity ) );
5034 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005035 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005036 switch( steps[i] )
5037 {
5038 case 0:
5039 break;
5040 case PSA_KEY_DERIVATION_INPUT_SECRET:
5041 PSA_ASSERT( psa_import_key( &attributes,
5042 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005043 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005044 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005045 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005046 break;
5047 default:
5048 PSA_ASSERT( psa_key_derivation_input_bytes(
5049 &operation, steps[i],
5050 inputs[i]->x, inputs[i]->len ) );
5051 break;
5052 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005053 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005054
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005055 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005056 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005057 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005058 expected_capacity = requested_capacity;
5059
5060 /* Expansion phase. */
5061 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5062 {
5063 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005065 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005066 if( expected_capacity == 0 && output_sizes[i] == 0 )
5067 {
5068 /* Reading 0 bytes when 0 bytes are available can go either way. */
5069 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005070 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005071 continue;
5072 }
5073 else if( expected_capacity == 0 ||
5074 output_sizes[i] > expected_capacity )
5075 {
5076 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005077 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005078 expected_capacity = 0;
5079 continue;
5080 }
5081 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005082 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005083 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005084 ASSERT_COMPARE( output_buffer, output_sizes[i],
5085 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005087 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005088 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005089 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005090 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005091 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005092 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005093
5094exit:
5095 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005096 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005097 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5098 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005099 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005100}
5101/* END_CASE */
5102
5103/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005104void derive_full( int alg_arg,
5105 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005106 data_t *input1,
5107 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005108 int requested_capacity_arg )
5109{
Ronald Cron5425a212020-08-04 14:58:35 +02005110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005111 psa_algorithm_t alg = alg_arg;
5112 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005113 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005114 unsigned char output_buffer[16];
5115 size_t expected_capacity = requested_capacity;
5116 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005118
Gilles Peskine8817f612018-12-18 00:18:46 +01005119 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005120
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005121 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5122 psa_set_key_algorithm( &attributes, alg );
5123 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005124
Gilles Peskine049c7532019-05-15 20:22:09 +02005125 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005126 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005127
Ronald Cron5425a212020-08-04 14:58:35 +02005128 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005129 input1->x, input1->len,
5130 input2->x, input2->len,
5131 requested_capacity ) )
5132 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005133
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005135 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005136 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005137
5138 /* Expansion phase. */
5139 while( current_capacity > 0 )
5140 {
5141 size_t read_size = sizeof( output_buffer );
5142 if( read_size > current_capacity )
5143 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005145 output_buffer,
5146 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005147 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005149 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005150 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005151 }
5152
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005153 /* Check that the operation refuses to go over capacity. */
5154 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005155 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005156
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005157 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005158
5159exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005160 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005161 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005162 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005163}
5164/* END_CASE */
5165
Janos Follathe60c9052019-07-03 13:51:30 +01005166/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005167void derive_key_exercise( int alg_arg,
5168 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005169 data_t *input1,
5170 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005171 int derived_type_arg,
5172 int derived_bits_arg,
5173 int derived_usage_arg,
5174 int derived_alg_arg )
5175{
Ronald Cron5425a212020-08-04 14:58:35 +02005176 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5177 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005178 psa_algorithm_t alg = alg_arg;
5179 psa_key_type_t derived_type = derived_type_arg;
5180 size_t derived_bits = derived_bits_arg;
5181 psa_key_usage_t derived_usage = derived_usage_arg;
5182 psa_algorithm_t derived_alg = derived_alg_arg;
5183 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005184 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005186 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005187
Gilles Peskine8817f612018-12-18 00:18:46 +01005188 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005189
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005190 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5191 psa_set_key_algorithm( &attributes, alg );
5192 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005193 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005194 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005195
5196 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005197 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005198 input1->x, input1->len,
5199 input2->x, input2->len, capacity ) )
5200 goto exit;
5201
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005202 psa_set_key_usage_flags( &attributes, derived_usage );
5203 psa_set_key_algorithm( &attributes, derived_alg );
5204 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005205 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005207 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005208
5209 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005210 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005211 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5212 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005213
5214 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005215 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005216 goto exit;
5217
5218exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005219 /*
5220 * Key attributes may have been returned by psa_get_key_attributes()
5221 * thus reset them as required.
5222 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005223 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005224
5225 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005226 psa_destroy_key( base_key );
5227 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005228 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005229}
5230/* END_CASE */
5231
Janos Follath42fd8882019-07-03 14:17:09 +01005232/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005233void derive_key_export( int alg_arg,
5234 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005235 data_t *input1,
5236 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005237 int bytes1_arg,
5238 int bytes2_arg )
5239{
Ronald Cron5425a212020-08-04 14:58:35 +02005240 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5241 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005242 psa_algorithm_t alg = alg_arg;
5243 size_t bytes1 = bytes1_arg;
5244 size_t bytes2 = bytes2_arg;
5245 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005246 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005247 uint8_t *output_buffer = NULL;
5248 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005249 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5250 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005251 size_t length;
5252
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005253 ASSERT_ALLOC( output_buffer, capacity );
5254 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005255 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005256
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005257 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5258 psa_set_key_algorithm( &base_attributes, alg );
5259 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005260 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005261 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005262
5263 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005264 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005265 input1->x, input1->len,
5266 input2->x, input2->len, capacity ) )
5267 goto exit;
5268
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005270 output_buffer,
5271 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005273
5274 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005275 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005276 input1->x, input1->len,
5277 input2->x, input2->len, capacity ) )
5278 goto exit;
5279
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005280 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5281 psa_set_key_algorithm( &derived_attributes, 0 );
5282 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005283 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005285 &derived_key ) );
5286 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005287 export_buffer, bytes1,
5288 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005289 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005290 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005291 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005292 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005293 &derived_key ) );
5294 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005295 export_buffer + bytes1, bytes2,
5296 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005297 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005298
5299 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005300 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5301 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005302
5303exit:
5304 mbedtls_free( output_buffer );
5305 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005307 psa_destroy_key( base_key );
5308 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005309 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005310}
5311/* END_CASE */
5312
5313/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005314void derive_key( int alg_arg,
5315 data_t *key_data, data_t *input1, data_t *input2,
5316 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005317 int expected_status_arg,
5318 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005319{
Ronald Cron5425a212020-08-04 14:58:35 +02005320 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5321 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005322 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005323 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005324 size_t bits = bits_arg;
5325 psa_status_t expected_status = expected_status_arg;
5326 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5327 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5328 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5329
5330 PSA_ASSERT( psa_crypto_init( ) );
5331
5332 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5333 psa_set_key_algorithm( &base_attributes, alg );
5334 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5335 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005336 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005337
Ronald Cron5425a212020-08-04 14:58:35 +02005338 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005339 input1->x, input1->len,
5340 input2->x, input2->len, SIZE_MAX ) )
5341 goto exit;
5342
5343 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5344 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005345 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005346 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005347
5348 psa_status_t status =
5349 psa_key_derivation_output_key( &derived_attributes,
5350 &operation,
5351 &derived_key );
5352 if( is_large_output > 0 )
5353 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5354 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005355
5356exit:
5357 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005358 psa_destroy_key( base_key );
5359 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005360 PSA_DONE( );
5361}
5362/* END_CASE */
5363
5364/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005365void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005366 int our_key_type_arg, int our_key_alg_arg,
5367 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005368 int expected_status_arg )
5369{
Ronald Cron5425a212020-08-04 14:58:35 +02005370 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005371 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005372 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005373 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005374 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005376 psa_status_t expected_status = expected_status_arg;
5377 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005378
Gilles Peskine8817f612018-12-18 00:18:46 +01005379 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005380
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005381 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005382 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005383 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005384 PSA_ASSERT( psa_import_key( &attributes,
5385 our_key_data->x, our_key_data->len,
5386 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005387
Gilles Peskine77f40d82019-04-11 21:27:06 +02005388 /* The tests currently include inputs that should fail at either step.
5389 * Test cases that fail at the setup step should be changed to call
5390 * key_derivation_setup instead, and this function should be renamed
5391 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005392 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005393 if( status == PSA_SUCCESS )
5394 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005395 TEST_EQUAL( psa_key_derivation_key_agreement(
5396 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5397 our_key,
5398 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005399 expected_status );
5400 }
5401 else
5402 {
5403 TEST_ASSERT( status == expected_status );
5404 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005405
5406exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005407 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005408 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005409 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005410}
5411/* END_CASE */
5412
5413/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005414void raw_key_agreement( int alg_arg,
5415 int our_key_type_arg, data_t *our_key_data,
5416 data_t *peer_key_data,
5417 data_t *expected_output )
5418{
Ronald Cron5425a212020-08-04 14:58:35 +02005419 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005420 psa_algorithm_t alg = alg_arg;
5421 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005423 unsigned char *output = NULL;
5424 size_t output_length = ~0;
5425
5426 ASSERT_ALLOC( output, expected_output->len );
5427 PSA_ASSERT( psa_crypto_init( ) );
5428
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005429 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5430 psa_set_key_algorithm( &attributes, alg );
5431 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005432 PSA_ASSERT( psa_import_key( &attributes,
5433 our_key_data->x, our_key_data->len,
5434 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005435
Gilles Peskinebe697d82019-05-16 18:00:41 +02005436 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5437 peer_key_data->x, peer_key_data->len,
5438 output, expected_output->len,
5439 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005440 ASSERT_COMPARE( output, output_length,
5441 expected_output->x, expected_output->len );
5442
5443exit:
5444 mbedtls_free( output );
5445 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005446 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005447}
5448/* END_CASE */
5449
5450/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005451void key_agreement_capacity( int alg_arg,
5452 int our_key_type_arg, data_t *our_key_data,
5453 data_t *peer_key_data,
5454 int expected_capacity_arg )
5455{
Ronald Cron5425a212020-08-04 14:58:35 +02005456 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005457 psa_algorithm_t alg = alg_arg;
5458 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005459 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005461 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005462 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005463
Gilles Peskine8817f612018-12-18 00:18:46 +01005464 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005465
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005466 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5467 psa_set_key_algorithm( &attributes, alg );
5468 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005469 PSA_ASSERT( psa_import_key( &attributes,
5470 our_key_data->x, our_key_data->len,
5471 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005472
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005473 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005474 PSA_ASSERT( psa_key_derivation_key_agreement(
5475 &operation,
5476 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5477 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005478 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5479 {
5480 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005481 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005482 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005483 NULL, 0 ) );
5484 }
Gilles Peskine59685592018-09-18 12:11:34 +02005485
Gilles Peskinebf491972018-10-25 22:36:12 +02005486 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005487 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005489 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005490
Gilles Peskinebf491972018-10-25 22:36:12 +02005491 /* Test the actual capacity by reading the output. */
5492 while( actual_capacity > sizeof( output ) )
5493 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005494 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005495 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005496 actual_capacity -= sizeof( output );
5497 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005498 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005499 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005500 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005501 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005502
Gilles Peskine59685592018-09-18 12:11:34 +02005503exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005504 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005505 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005506 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005507}
5508/* END_CASE */
5509
5510/* BEGIN_CASE */
5511void key_agreement_output( int alg_arg,
5512 int our_key_type_arg, data_t *our_key_data,
5513 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005514 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005515{
Ronald Cron5425a212020-08-04 14:58:35 +02005516 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005517 psa_algorithm_t alg = alg_arg;
5518 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005519 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005521 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005522
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005523 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5524 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005525
Gilles Peskine8817f612018-12-18 00:18:46 +01005526 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005527
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005528 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5529 psa_set_key_algorithm( &attributes, alg );
5530 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005531 PSA_ASSERT( psa_import_key( &attributes,
5532 our_key_data->x, our_key_data->len,
5533 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005534
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005535 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005536 PSA_ASSERT( psa_key_derivation_key_agreement(
5537 &operation,
5538 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5539 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005540 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5541 {
5542 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005543 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005544 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005545 NULL, 0 ) );
5546 }
Gilles Peskine59685592018-09-18 12:11:34 +02005547
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005548 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005549 actual_output,
5550 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005551 ASSERT_COMPARE( actual_output, expected_output1->len,
5552 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005553 if( expected_output2->len != 0 )
5554 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005556 actual_output,
5557 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005558 ASSERT_COMPARE( actual_output, expected_output2->len,
5559 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005560 }
Gilles Peskine59685592018-09-18 12:11:34 +02005561
5562exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005563 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005564 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005565 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005566 mbedtls_free( actual_output );
5567}
5568/* END_CASE */
5569
5570/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005571void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005572{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005573 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005574 unsigned char *output = NULL;
5575 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005576 size_t i;
5577 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005578
Simon Butcher49f8e312020-03-03 15:51:50 +00005579 TEST_ASSERT( bytes_arg >= 0 );
5580
Gilles Peskine91892022021-02-08 19:50:26 +01005581 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005582 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005583
Gilles Peskine8817f612018-12-18 00:18:46 +01005584 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005585
Gilles Peskinea50d7392018-06-21 10:22:13 +02005586 /* Run several times, to ensure that every output byte will be
5587 * nonzero at least once with overwhelming probability
5588 * (2^(-8*number_of_runs)). */
5589 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005590 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005591 if( bytes != 0 )
5592 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005593 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005594
Gilles Peskinea50d7392018-06-21 10:22:13 +02005595 for( i = 0; i < bytes; i++ )
5596 {
5597 if( output[i] != 0 )
5598 ++changed[i];
5599 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005600 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005601
5602 /* Check that every byte was changed to nonzero at least once. This
5603 * validates that psa_generate_random is overwriting every byte of
5604 * the output buffer. */
5605 for( i = 0; i < bytes; i++ )
5606 {
5607 TEST_ASSERT( changed[i] != 0 );
5608 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005609
5610exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005611 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005612 mbedtls_free( output );
5613 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005614}
5615/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005616
5617/* BEGIN_CASE */
5618void generate_key( int type_arg,
5619 int bits_arg,
5620 int usage_arg,
5621 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005622 int expected_status_arg,
5623 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005624{
Ronald Cron5425a212020-08-04 14:58:35 +02005625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005626 psa_key_type_t type = type_arg;
5627 psa_key_usage_t usage = usage_arg;
5628 size_t bits = bits_arg;
5629 psa_algorithm_t alg = alg_arg;
5630 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005632 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005633
Gilles Peskine8817f612018-12-18 00:18:46 +01005634 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005635
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005636 psa_set_key_usage_flags( &attributes, usage );
5637 psa_set_key_algorithm( &attributes, alg );
5638 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005639 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005640
5641 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005642 psa_status_t status = psa_generate_key( &attributes, &key );
5643
5644 if( is_large_key > 0 )
5645 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5646 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005647 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005648 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005649
5650 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005651 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005652 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5653 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005654
Gilles Peskine818ca122018-06-20 18:16:48 +02005655 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005656 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005657 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005658
5659exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005660 /*
5661 * Key attributes may have been returned by psa_get_key_attributes()
5662 * thus reset them as required.
5663 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005664 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005665
Ronald Cron5425a212020-08-04 14:58:35 +02005666 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005667 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005668}
5669/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005670
Gilles Peskinee56e8782019-04-26 17:34:02 +02005671/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5672void generate_key_rsa( int bits_arg,
5673 data_t *e_arg,
5674 int expected_status_arg )
5675{
Ronald Cron5425a212020-08-04 14:58:35 +02005676 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005677 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005678 size_t bits = bits_arg;
5679 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5680 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5681 psa_status_t expected_status = expected_status_arg;
5682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5683 uint8_t *exported = NULL;
5684 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005685 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005686 size_t exported_length = SIZE_MAX;
5687 uint8_t *e_read_buffer = NULL;
5688 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005689 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005690 size_t e_read_length = SIZE_MAX;
5691
5692 if( e_arg->len == 0 ||
5693 ( e_arg->len == 3 &&
5694 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5695 {
5696 is_default_public_exponent = 1;
5697 e_read_size = 0;
5698 }
5699 ASSERT_ALLOC( e_read_buffer, e_read_size );
5700 ASSERT_ALLOC( exported, exported_size );
5701
5702 PSA_ASSERT( psa_crypto_init( ) );
5703
5704 psa_set_key_usage_flags( &attributes, usage );
5705 psa_set_key_algorithm( &attributes, alg );
5706 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5707 e_arg->x, e_arg->len ) );
5708 psa_set_key_bits( &attributes, bits );
5709
5710 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005711 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005712 if( expected_status != PSA_SUCCESS )
5713 goto exit;
5714
5715 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005716 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005717 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5718 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5719 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5720 e_read_buffer, e_read_size,
5721 &e_read_length ) );
5722 if( is_default_public_exponent )
5723 TEST_EQUAL( e_read_length, 0 );
5724 else
5725 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5726
5727 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005728 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005729 goto exit;
5730
5731 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005732 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005733 exported, exported_size,
5734 &exported_length ) );
5735 {
5736 uint8_t *p = exported;
5737 uint8_t *end = exported + exported_length;
5738 size_t len;
5739 /* RSAPublicKey ::= SEQUENCE {
5740 * modulus INTEGER, -- n
5741 * publicExponent INTEGER } -- e
5742 */
5743 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005744 MBEDTLS_ASN1_SEQUENCE |
5745 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005746 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5747 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5748 MBEDTLS_ASN1_INTEGER ) );
5749 if( len >= 1 && p[0] == 0 )
5750 {
5751 ++p;
5752 --len;
5753 }
5754 if( e_arg->len == 0 )
5755 {
5756 TEST_EQUAL( len, 3 );
5757 TEST_EQUAL( p[0], 1 );
5758 TEST_EQUAL( p[1], 0 );
5759 TEST_EQUAL( p[2], 1 );
5760 }
5761 else
5762 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5763 }
5764
5765exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005766 /*
5767 * Key attributes may have been returned by psa_get_key_attributes() or
5768 * set by psa_set_key_domain_parameters() thus reset them as required.
5769 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005770 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005771
Ronald Cron5425a212020-08-04 14:58:35 +02005772 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005773 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005774 mbedtls_free( e_read_buffer );
5775 mbedtls_free( exported );
5776}
5777/* END_CASE */
5778
Darryl Greend49a4992018-06-18 17:27:26 +01005779/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005780void persistent_key_load_key_from_storage( data_t *data,
5781 int type_arg, int bits_arg,
5782 int usage_flags_arg, int alg_arg,
5783 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005784{
Ronald Cron71016a92020-08-28 19:01:50 +02005785 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005786 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5788 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005789 psa_key_type_t type = type_arg;
5790 size_t bits = bits_arg;
5791 psa_key_usage_t usage_flags = usage_flags_arg;
5792 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005793 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005794 unsigned char *first_export = NULL;
5795 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005796 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005797 size_t first_exported_length;
5798 size_t second_exported_length;
5799
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005800 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5801 {
5802 ASSERT_ALLOC( first_export, export_size );
5803 ASSERT_ALLOC( second_export, export_size );
5804 }
Darryl Greend49a4992018-06-18 17:27:26 +01005805
Gilles Peskine8817f612018-12-18 00:18:46 +01005806 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005807
Gilles Peskinec87af662019-05-15 16:12:22 +02005808 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005809 psa_set_key_usage_flags( &attributes, usage_flags );
5810 psa_set_key_algorithm( &attributes, alg );
5811 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005812 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005813
Darryl Green0c6575a2018-11-07 16:05:30 +00005814 switch( generation_method )
5815 {
5816 case IMPORT_KEY:
5817 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005818 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005819 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005820 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005821
Darryl Green0c6575a2018-11-07 16:05:30 +00005822 case GENERATE_KEY:
5823 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005824 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005825 break;
5826
5827 case DERIVE_KEY:
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005828#if PSA_WANT_ALG_HKDF && PSA_WANT_ALG_SHA_256
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005829 {
5830 /* Create base key */
5831 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5832 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5833 psa_set_key_usage_flags( &base_attributes,
5834 PSA_KEY_USAGE_DERIVE );
5835 psa_set_key_algorithm( &base_attributes, derive_alg );
5836 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005837 PSA_ASSERT( psa_import_key( &base_attributes,
5838 data->x, data->len,
5839 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005840 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005841 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005842 PSA_ASSERT( psa_key_derivation_input_key(
5843 &operation,
5844 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005845 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005846 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005847 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005848 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5849 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005850 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005851 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005852 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005853 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005854 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005855#else
5856 TEST_ASSUME( ! "KDF not supported in this configuration" );
5857#endif
5858 break;
5859
5860 default:
5861 TEST_ASSERT( ! "generation_method not implemented in test" );
5862 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005863 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005864 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005865
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005866 /* Export the key if permitted by the key policy. */
5867 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5868 {
Ronald Cron5425a212020-08-04 14:58:35 +02005869 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005870 first_export, export_size,
5871 &first_exported_length ) );
5872 if( generation_method == IMPORT_KEY )
5873 ASSERT_COMPARE( data->x, data->len,
5874 first_export, first_exported_length );
5875 }
Darryl Greend49a4992018-06-18 17:27:26 +01005876
5877 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005878 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005879 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005880 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005881
Darryl Greend49a4992018-06-18 17:27:26 +01005882 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005883 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005884 TEST_ASSERT( mbedtls_svc_key_id_equal(
5885 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005886 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5887 PSA_KEY_LIFETIME_PERSISTENT );
5888 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5889 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5890 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5891 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005892
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005893 /* Export the key again if permitted by the key policy. */
5894 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005895 {
Ronald Cron5425a212020-08-04 14:58:35 +02005896 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005897 second_export, export_size,
5898 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005899 ASSERT_COMPARE( first_export, first_exported_length,
5900 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005901 }
5902
5903 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005904 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005905 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005906
5907exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005908 /*
5909 * Key attributes may have been returned by psa_get_key_attributes()
5910 * thus reset them as required.
5911 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005912 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005913
Darryl Greend49a4992018-06-18 17:27:26 +01005914 mbedtls_free( first_export );
5915 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005916 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005917 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005918 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005919 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005920}
5921/* END_CASE */