blob: 061c52e5e8f57f345a3ece627701baf527e77c2b [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
535 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100536 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100537#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100538 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200539
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100540 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200541 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200542 /* Some algorithms require the payload to have the size of
543 * the hash encoded in the algorithm. Use this input size
544 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200545 if( hash_alg != 0 )
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100546 payload_length = PSA_HASH_LENGTH( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200547 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100548 payload, payload_length,
549 signature, sizeof( signature ),
550 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200551 }
552
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100553 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200554 {
555 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100556 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200557 PSA_SUCCESS :
558 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200559 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100560 payload, payload_length,
561 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100562 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200563 }
564
565 return( 1 );
566
567exit:
568 return( 0 );
569}
570
Ronald Cron5425a212020-08-04 14:58:35 +0200571static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200572 psa_key_usage_t usage,
573 psa_algorithm_t alg )
574{
575 unsigned char plaintext[256] = "Hello, world...";
576 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
577 size_t ciphertext_length = sizeof( ciphertext );
578 size_t plaintext_length = 16;
579
580 if( usage & PSA_KEY_USAGE_ENCRYPT )
581 {
Ronald Cron5425a212020-08-04 14:58:35 +0200582 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100583 plaintext, plaintext_length,
584 NULL, 0,
585 ciphertext, sizeof( ciphertext ),
586 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200587 }
588
589 if( usage & PSA_KEY_USAGE_DECRYPT )
590 {
591 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200592 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200593 ciphertext, ciphertext_length,
594 NULL, 0,
595 plaintext, sizeof( plaintext ),
596 &plaintext_length );
597 TEST_ASSERT( status == PSA_SUCCESS ||
598 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
599 ( status == PSA_ERROR_INVALID_ARGUMENT ||
600 status == PSA_ERROR_INVALID_PADDING ) ) );
601 }
602
603 return( 1 );
604
605exit:
606 return( 0 );
607}
Gilles Peskine02b75072018-07-01 22:31:34 +0200608
Janos Follathf2815ea2019-07-03 12:41:36 +0100609static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200610 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100611 psa_algorithm_t alg,
612 unsigned char* input1, size_t input1_length,
613 unsigned char* input2, size_t input2_length,
614 size_t capacity )
615{
616 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
617 if( PSA_ALG_IS_HKDF( alg ) )
618 {
619 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
620 PSA_KEY_DERIVATION_INPUT_SALT,
621 input1, input1_length ) );
622 PSA_ASSERT( psa_key_derivation_input_key( operation,
623 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200624 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100625 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
626 PSA_KEY_DERIVATION_INPUT_INFO,
627 input2,
628 input2_length ) );
629 }
630 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
631 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
632 {
633 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
634 PSA_KEY_DERIVATION_INPUT_SEED,
635 input1, input1_length ) );
636 PSA_ASSERT( psa_key_derivation_input_key( operation,
637 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200638 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100639 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
640 PSA_KEY_DERIVATION_INPUT_LABEL,
641 input2, input2_length ) );
642 }
643 else
644 {
645 TEST_ASSERT( ! "Key derivation algorithm not supported" );
646 }
647
Gilles Peskinec744d992019-07-30 17:26:54 +0200648 if( capacity != SIZE_MAX )
649 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100650
651 return( 1 );
652
653exit:
654 return( 0 );
655}
656
657
Ronald Cron5425a212020-08-04 14:58:35 +0200658static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200659 psa_key_usage_t usage,
660 psa_algorithm_t alg )
661{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200662 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100663 unsigned char input1[] = "Input 1";
664 size_t input1_length = sizeof( input1 );
665 unsigned char input2[] = "Input 2";
666 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200667 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100668 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200669
670 if( usage & PSA_KEY_USAGE_DERIVE )
671 {
Ronald Cron5425a212020-08-04 14:58:35 +0200672 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100673 input1, input1_length,
674 input2, input2_length, capacity ) )
675 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200676
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200677 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200678 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100679 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200680 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200681 }
682
683 return( 1 );
684
685exit:
686 return( 0 );
687}
688
Gilles Peskinec7998b72018-11-07 18:45:02 +0100689/* We need two keys to exercise key agreement. Exercise the
690 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200691static psa_status_t key_agreement_with_self(
692 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200693 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100694{
695 psa_key_type_t private_key_type;
696 psa_key_type_t public_key_type;
697 size_t key_bits;
698 uint8_t *public_key = NULL;
699 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200700 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200701 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
702 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200703 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100705
Ronald Cron5425a212020-08-04 14:58:35 +0200706 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200707 private_key_type = psa_get_key_type( &attributes );
708 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200709 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100710 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100711 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200712 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100713 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100714
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200715 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200716 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200717 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100718exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100719 /*
720 * Key attributes may have been returned by psa_get_key_attributes()
721 * thus reset them as required.
722 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200723 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100724
725 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100726 return( status );
727}
728
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200729/* We need two keys to exercise key agreement. Exercise the
730 * private key against its own public key. */
731static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200732 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200733{
734 psa_key_type_t private_key_type;
735 psa_key_type_t public_key_type;
736 size_t key_bits;
737 uint8_t *public_key = NULL;
738 size_t public_key_length;
739 uint8_t output[1024];
740 size_t output_length;
741 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200742 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
743 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200744 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200746
Ronald Cron5425a212020-08-04 14:58:35 +0200747 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200748 private_key_type = psa_get_key_type( &attributes );
749 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200750 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100751 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200752 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200753 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200754 public_key, public_key_length,
755 &public_key_length ) );
756
Ronald Cron5425a212020-08-04 14:58:35 +0200757 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200758 public_key, public_key_length,
759 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200760exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100761 /*
762 * Key attributes may have been returned by psa_get_key_attributes()
763 * thus reset them as required.
764 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200765 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100766
767 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200768 return( status );
769}
770
Ronald Cron5425a212020-08-04 14:58:35 +0200771static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200772 psa_key_usage_t usage,
773 psa_algorithm_t alg )
774{
775 int ok = 0;
776
777 if( usage & PSA_KEY_USAGE_DERIVE )
778 {
779 /* We need two keys to exercise key agreement. Exercise the
780 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200781 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200782 }
783 ok = 1;
784
785exit:
786 return( ok );
787}
788
Ronald Cron5425a212020-08-04 14:58:35 +0200789static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200790 psa_key_usage_t usage,
791 psa_algorithm_t alg )
792{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200793 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200794 unsigned char output[1];
795 int ok = 0;
796
797 if( usage & PSA_KEY_USAGE_DERIVE )
798 {
799 /* We need two keys to exercise key agreement. Exercise the
800 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200801 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200802 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200803 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200804 output,
805 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200806 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200807 }
808 ok = 1;
809
810exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200811 return( ok );
812}
813
Jaeden Amerof7dca862019-06-27 17:31:33 +0100814int asn1_skip_integer( unsigned char **p, const unsigned char *end,
815 size_t min_bits, size_t max_bits,
816 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200817{
818 size_t len;
819 size_t actual_bits;
820 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100821 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100822 MBEDTLS_ASN1_INTEGER ),
823 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200824
825 /* Check if the retrieved length doesn't extend the actual buffer's size.
826 * It is assumed here, that end >= p, which validates casting to size_t. */
827 TEST_ASSERT( len <= (size_t)( end - *p) );
828
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200829 /* Tolerate a slight departure from DER encoding:
830 * - 0 may be represented by an empty string or a 1-byte string.
831 * - The sign bit may be used as a value bit. */
832 if( ( len == 1 && ( *p )[0] == 0 ) ||
833 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
834 {
835 ++( *p );
836 --len;
837 }
838 if( min_bits == 0 && len == 0 )
839 return( 1 );
840 msb = ( *p )[0];
841 TEST_ASSERT( msb != 0 );
842 actual_bits = 8 * ( len - 1 );
843 while( msb != 0 )
844 {
845 msb >>= 1;
846 ++actual_bits;
847 }
848 TEST_ASSERT( actual_bits >= min_bits );
849 TEST_ASSERT( actual_bits <= max_bits );
850 if( must_be_odd )
851 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
852 *p += len;
853 return( 1 );
854exit:
855 return( 0 );
856}
857
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200858static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
859 uint8_t *exported, size_t exported_length )
860{
861 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100862 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200863 else
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100864 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200865
866#if defined(MBEDTLS_DES_C)
867 if( type == PSA_KEY_TYPE_DES )
868 {
869 /* Check the parity bits. */
870 unsigned i;
871 for( i = 0; i < bits / 8; i++ )
872 {
873 unsigned bit_count = 0;
874 unsigned m;
875 for( m = 1; m <= 0x100; m <<= 1 )
876 {
877 if( exported[i] & m )
878 ++bit_count;
879 }
880 TEST_ASSERT( bit_count % 2 != 0 );
881 }
882 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200883 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200884#endif
885
886#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200887 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200888 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200889 uint8_t *p = exported;
890 uint8_t *end = exported + exported_length;
891 size_t len;
892 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200893 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200894 * modulus INTEGER, -- n
895 * publicExponent INTEGER, -- e
896 * privateExponent INTEGER, -- d
897 * prime1 INTEGER, -- p
898 * prime2 INTEGER, -- q
899 * exponent1 INTEGER, -- d mod (p-1)
900 * exponent2 INTEGER, -- d mod (q-1)
901 * coefficient INTEGER, -- (inverse of q) mod p
902 * }
903 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100904 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
905 MBEDTLS_ASN1_SEQUENCE |
906 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
907 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200908 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
909 goto exit;
910 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
911 goto exit;
912 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
913 goto exit;
914 /* Require d to be at least half the size of n. */
915 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
916 goto exit;
917 /* Require p and q to be at most half the size of n, rounded up. */
918 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
919 goto exit;
920 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
921 goto exit;
922 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
923 goto exit;
924 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
925 goto exit;
926 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
927 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100928 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100929 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200930 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200931#endif /* MBEDTLS_RSA_C */
932
933#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200934 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200935 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100936 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100937 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100938 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200939 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200940#endif /* MBEDTLS_ECP_C */
941
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200942 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
943 {
944 uint8_t *p = exported;
945 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200946#if defined(MBEDTLS_RSA_C)
947 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
948 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100949 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200950 /* RSAPublicKey ::= SEQUENCE {
951 * modulus INTEGER, -- n
952 * publicExponent INTEGER } -- e
953 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100954 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
955 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100956 MBEDTLS_ASN1_CONSTRUCTED ),
957 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100958 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200959 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
960 goto exit;
961 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
962 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100963 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200964 }
965 else
966#endif /* MBEDTLS_RSA_C */
967#if defined(MBEDTLS_ECP_C)
968 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
969 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200970 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
971 {
972 /* The representation of an ECC Montgomery public key is
973 * the raw compressed point */
974 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
975 }
976 else
977 {
978 /* The representation of an ECC Weierstrass public key is:
979 * - The byte 0x04;
980 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
981 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
982 * - where m is the bit size associated with the curve.
983 */
984 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
985 TEST_EQUAL( p[0], 4 );
986 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200987 }
988 else
989#endif /* MBEDTLS_ECP_C */
990 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100991 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200992 mbedtls_snprintf( message, sizeof( message ),
993 "No sanity check for public key type=0x%08lx",
994 (unsigned long) type );
995 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200996 (void) p;
997 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200998 return( 0 );
999 }
1000 }
1001 else
1002
1003 {
1004 /* No sanity checks for other types */
1005 }
1006
1007 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001008
1009exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001010 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001011}
1012
Ronald Cron5425a212020-08-04 14:58:35 +02001013static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001014 psa_key_usage_t usage )
1015{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001017 uint8_t *exported = NULL;
1018 size_t exported_size = 0;
1019 size_t exported_length = 0;
1020 int ok = 0;
1021
Ronald Cron5425a212020-08-04 14:58:35 +02001022 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001023
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001024 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1025 psa_get_key_type( &attributes ),
1026 psa_get_key_bits( &attributes ) );
1027 ASSERT_ALLOC( exported, exported_size );
1028
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001029 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001030 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001031 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001032 TEST_EQUAL( psa_export_key( key, exported,
1033 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001034 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001035 ok = 1;
1036 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001037 }
1038
Ronald Cron5425a212020-08-04 14:58:35 +02001039 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001040 exported, exported_size,
1041 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001042 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1043 psa_get_key_bits( &attributes ),
1044 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001045
1046exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001047 /*
1048 * Key attributes may have been returned by psa_get_key_attributes()
1049 * thus reset them as required.
1050 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001051 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001052
1053 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001054 return( ok );
1055}
1056
Ronald Cron5425a212020-08-04 14:58:35 +02001057static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001058{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001060 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001061 uint8_t *exported = NULL;
1062 size_t exported_size = 0;
1063 size_t exported_length = 0;
1064 int ok = 0;
1065
Ronald Cron5425a212020-08-04 14:58:35 +02001066 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001067 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001068 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001069 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1070 psa_get_key_type( &attributes ),
1071 psa_get_key_bits( &attributes ) );
1072 ASSERT_ALLOC( exported, exported_size );
1073
1074 TEST_EQUAL( psa_export_public_key( key, exported,
1075 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001076 PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001077 ok = 1;
1078 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001079 }
1080
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001081 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001082 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001083 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1084 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001085 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001086
Ronald Cron5425a212020-08-04 14:58:35 +02001087 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001088 exported, exported_size,
1089 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001090 ok = exported_key_sanity_check( public_type,
1091 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001092 exported, exported_length );
1093
1094exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001095 /*
1096 * Key attributes may have been returned by psa_get_key_attributes()
1097 * thus reset them as required.
1098 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001099 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001100
1101 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001102 return( ok );
1103}
1104
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001105/** Do smoke tests on a key.
1106 *
1107 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1108 * sign/verify, or derivation) that is permitted according to \p usage.
1109 * \p usage and \p alg should correspond to the expected policy on the
1110 * key.
1111 *
1112 * Export the key if permitted by \p usage, and check that the output
1113 * looks sensible. If \p usage forbids export, check that
1114 * \p psa_export_key correctly rejects the attempt. If the key is
1115 * asymmetric, also check \p psa_export_public_key.
1116 *
1117 * If the key fails the tests, this function calls the test framework's
1118 * `test_fail` function and returns false. Otherwise this function returns
1119 * true. Therefore it should be used as follows:
1120 * ```
1121 * if( ! exercise_key( ... ) ) goto exit;
1122 * ```
1123 *
Ronald Cron5425a212020-08-04 14:58:35 +02001124 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001125 * \p alg.
1126 * \param usage The usage flags to assume.
1127 * \param alg The algorithm to exercise.
1128 *
1129 * \retval 0 The key failed the smoke tests.
1130 * \retval 1 The key passed the smoke tests.
1131 */
Ronald Cron5425a212020-08-04 14:58:35 +02001132static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001133 psa_key_usage_t usage,
1134 psa_algorithm_t alg )
1135{
1136 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001137
Ronald Cron5425a212020-08-04 14:58:35 +02001138 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001139 return( 0 );
1140
Gilles Peskine02b75072018-07-01 22:31:34 +02001141 if( alg == 0 )
1142 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1143 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001144 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001145 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001146 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001147 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001148 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001149 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001150 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001151 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001152 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001153 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001154 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001155 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001156 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001157 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001158 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001159 else
1160 {
1161 char message[40];
1162 mbedtls_snprintf( message, sizeof( message ),
1163 "No code to exercise alg=0x%08lx",
1164 (unsigned long) alg );
1165 test_fail( message, __LINE__, __FILE__ );
1166 ok = 0;
1167 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001168
Ronald Cron5425a212020-08-04 14:58:35 +02001169 ok = ok && exercise_export_key( key, usage );
1170 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001171
Gilles Peskine02b75072018-07-01 22:31:34 +02001172 return( ok );
1173}
1174
Gilles Peskine10df3412018-10-25 22:35:43 +02001175static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1176 psa_algorithm_t alg )
1177{
1178 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1179 {
1180 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001181 PSA_KEY_USAGE_VERIFY_HASH :
1182 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001183 }
1184 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1185 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1186 {
1187 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1188 PSA_KEY_USAGE_ENCRYPT :
1189 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1190 }
1191 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1192 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1193 {
1194 return( PSA_KEY_USAGE_DERIVE );
1195 }
1196 else
1197 {
1198 return( 0 );
1199 }
1200
1201}
Darryl Green0c6575a2018-11-07 16:05:30 +00001202
Ronald Cron5425a212020-08-04 14:58:35 +02001203static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001204{
1205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001206 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001207 uint8_t buffer[1];
1208 size_t length;
1209 int ok = 0;
1210
Ronald Cronecfb2372020-07-23 17:13:42 +02001211 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1213 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1214 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001215 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001216 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001217 TEST_EQUAL(
1218 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1219 TEST_EQUAL(
1220 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001221 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001222 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1223 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1224 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1225 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1226
Ronald Cron5425a212020-08-04 14:58:35 +02001227 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001228 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001229 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001230 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001231 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001232
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001233 ok = 1;
1234
1235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001236 /*
1237 * Key attributes may have been returned by psa_get_key_attributes()
1238 * thus reset them as required.
1239 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001240 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001241
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001242 return( ok );
1243}
1244
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001245/* Assert that a key isn't reported as having a slot number. */
1246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1247#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1248 do \
1249 { \
1250 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1251 TEST_EQUAL( psa_get_key_slot_number( \
1252 attributes, \
1253 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1254 PSA_ERROR_INVALID_ARGUMENT ); \
1255 } \
1256 while( 0 )
1257#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1258#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1259 ( (void) 0 )
1260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1261
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001262/* An overapproximation of the amount of storage needed for a key of the
1263 * given type and with the given content. The API doesn't make it easy
1264 * to find a good value for the size. The current implementation doesn't
1265 * care about the value anyway. */
1266#define KEY_BITS_FROM_DATA( type, data ) \
1267 ( data )->len
1268
Darryl Green0c6575a2018-11-07 16:05:30 +00001269typedef enum {
1270 IMPORT_KEY = 0,
1271 GENERATE_KEY = 1,
1272 DERIVE_KEY = 2
1273} generate_method;
1274
Gilles Peskinee59236f2018-01-27 23:32:46 +01001275/* END_HEADER */
1276
1277/* BEGIN_DEPENDENCIES
1278 * depends_on:MBEDTLS_PSA_CRYPTO_C
1279 * END_DEPENDENCIES
1280 */
1281
1282/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001283void static_checks( )
1284{
1285 size_t max_truncated_mac_size =
1286 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1287
1288 /* Check that the length for a truncated MAC always fits in the algorithm
1289 * encoding. The shifted mask is the maximum truncated value. The
1290 * untruncated algorithm may be one byte larger. */
1291 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001292
1293#if defined(MBEDTLS_TEST_DEPRECATED)
1294 /* Check deprecated constants. */
1295 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1296 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1297 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1298 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1299 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1300 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1301 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1302 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001303
Paul Elliott8ff510a2020-06-02 17:19:28 +01001304 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1305 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1306 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1308 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1309 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1310 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1311 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1312 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1314 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1326 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1328 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1329 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1330 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1331 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1332 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1333 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1334
1335 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1336 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1337 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1338 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1339 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1340 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1341 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1342 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001343
Paul Elliott75e27032020-06-03 15:17:39 +01001344 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1345 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1346 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1347 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1348 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1349
1350 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1351 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001352#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001353}
1354/* END_CASE */
1355
1356/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001357void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001358 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001359 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001360{
Gilles Peskine4747d192019-04-17 15:05:45 +02001361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001362 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001363 psa_key_lifetime_t lifetime = lifetime_arg;
1364 psa_key_usage_t usage_flags = usage_flags_arg;
1365 psa_algorithm_t alg = alg_arg;
1366 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001367 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001368
Ronald Cronecfb2372020-07-23 17:13:42 +02001369 TEST_EQUAL(
1370 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1371 TEST_EQUAL(
1372 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001373 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1374 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1375 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1376 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001377 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001378
Gilles Peskinec87af662019-05-15 16:12:22 +02001379 psa_set_key_id( &attributes, id );
1380 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001381 psa_set_key_usage_flags( &attributes, usage_flags );
1382 psa_set_key_algorithm( &attributes, alg );
1383 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001384 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001385
Ronald Cronecfb2372020-07-23 17:13:42 +02001386 TEST_ASSERT( mbedtls_svc_key_id_equal(
1387 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001388 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1389 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1390 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1391 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001392 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001393
1394 psa_reset_key_attributes( &attributes );
1395
Ronald Cronecfb2372020-07-23 17:13:42 +02001396 TEST_EQUAL(
1397 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1398 TEST_EQUAL(
1399 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001400 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1401 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1402 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1403 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001404 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001405}
1406/* END_CASE */
1407
1408/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001409void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1410 int id2_arg, int owner_id2_arg,
1411 int expected_id_arg, int expected_owner_id_arg,
1412 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001413{
1414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001415 mbedtls_svc_key_id_t id1 =
1416 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001417 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001418 mbedtls_svc_key_id_t id2 =
1419 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001420 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001421 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001422 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1423
1424 if( id1_arg != -1 )
1425 psa_set_key_id( &attributes, id1 );
1426 if( lifetime_arg != -1 )
1427 psa_set_key_lifetime( &attributes, lifetime );
1428 if( id2_arg != -1 )
1429 psa_set_key_id( &attributes, id2 );
1430
Ronald Cronecfb2372020-07-23 17:13:42 +02001431 TEST_ASSERT( mbedtls_svc_key_id_equal(
1432 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001433 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1434}
1435/* END_CASE */
1436
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001437/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1438void slot_number_attribute( )
1439{
1440 psa_key_slot_number_t slot_number = 0xdeadbeef;
1441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1442
1443 /* Initially, there is no slot number. */
1444 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1445 PSA_ERROR_INVALID_ARGUMENT );
1446
1447 /* Test setting a slot number. */
1448 psa_set_key_slot_number( &attributes, 0 );
1449 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1450 TEST_EQUAL( slot_number, 0 );
1451
1452 /* Test changing the slot number. */
1453 psa_set_key_slot_number( &attributes, 42 );
1454 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1455 TEST_EQUAL( slot_number, 42 );
1456
1457 /* Test clearing the slot number. */
1458 psa_clear_key_slot_number( &attributes );
1459 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1460 PSA_ERROR_INVALID_ARGUMENT );
1461
1462 /* Clearing again should have no effect. */
1463 psa_clear_key_slot_number( &attributes );
1464 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1465 PSA_ERROR_INVALID_ARGUMENT );
1466
1467 /* Test that reset clears the slot number. */
1468 psa_set_key_slot_number( &attributes, 42 );
1469 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1470 TEST_EQUAL( slot_number, 42 );
1471 psa_reset_key_attributes( &attributes );
1472 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1473 PSA_ERROR_INVALID_ARGUMENT );
1474}
1475/* END_CASE */
1476
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001477/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001478void import_with_policy( int type_arg,
1479 int usage_arg, int alg_arg,
1480 int expected_status_arg )
1481{
1482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1483 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001484 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001485 psa_key_type_t type = type_arg;
1486 psa_key_usage_t usage = usage_arg;
1487 psa_algorithm_t alg = alg_arg;
1488 psa_status_t expected_status = expected_status_arg;
1489 const uint8_t key_material[16] = {0};
1490 psa_status_t status;
1491
1492 PSA_ASSERT( psa_crypto_init( ) );
1493
1494 psa_set_key_type( &attributes, type );
1495 psa_set_key_usage_flags( &attributes, usage );
1496 psa_set_key_algorithm( &attributes, alg );
1497
1498 status = psa_import_key( &attributes,
1499 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001500 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001501 TEST_EQUAL( status, expected_status );
1502 if( status != PSA_SUCCESS )
1503 goto exit;
1504
Ronald Cron5425a212020-08-04 14:58:35 +02001505 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001506 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1507 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1508 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001509 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001510
Ronald Cron5425a212020-08-04 14:58:35 +02001511 PSA_ASSERT( psa_destroy_key( key ) );
1512 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001513
1514exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001515 /*
1516 * Key attributes may have been returned by psa_get_key_attributes()
1517 * thus reset them as required.
1518 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001519 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001520
1521 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001522 PSA_DONE( );
1523}
1524/* END_CASE */
1525
1526/* BEGIN_CASE */
1527void import_with_data( data_t *data, int type_arg,
1528 int attr_bits_arg,
1529 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001530{
1531 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1532 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001533 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001534 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001535 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001536 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001537 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538
Gilles Peskine8817f612018-12-18 00:18:46 +01001539 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540
Gilles Peskine4747d192019-04-17 15:05:45 +02001541 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001542 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001543
Ronald Cron5425a212020-08-04 14:58:35 +02001544 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001545 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001546 if( status != PSA_SUCCESS )
1547 goto exit;
1548
Ronald Cron5425a212020-08-04 14:58:35 +02001549 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001550 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001551 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001552 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001553 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001554
Ronald Cron5425a212020-08-04 14:58:35 +02001555 PSA_ASSERT( psa_destroy_key( key ) );
1556 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001557
1558exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001559 /*
1560 * Key attributes may have been returned by psa_get_key_attributes()
1561 * thus reset them as required.
1562 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001563 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001564
1565 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001566 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567}
1568/* END_CASE */
1569
1570/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001571void import_large_key( int type_arg, int byte_size_arg,
1572 int expected_status_arg )
1573{
1574 psa_key_type_t type = type_arg;
1575 size_t byte_size = byte_size_arg;
1576 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1577 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001578 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001579 psa_status_t status;
1580 uint8_t *buffer = NULL;
1581 size_t buffer_size = byte_size + 1;
1582 size_t n;
1583
Steven Cooreman69967ce2021-01-18 18:01:08 +01001584 /* Skip the test case if the target running the test cannot
1585 * accomodate large keys due to heap size constraints */
1586 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001587 memset( buffer, 'K', byte_size );
1588
1589 PSA_ASSERT( psa_crypto_init( ) );
1590
1591 /* Try importing the key */
1592 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1593 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001594 status = psa_import_key( &attributes, buffer, byte_size, &key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001595 TEST_EQUAL( status, expected_status );
1596
1597 if( status == PSA_SUCCESS )
1598 {
Ronald Cron5425a212020-08-04 14:58:35 +02001599 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001600 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1601 TEST_EQUAL( psa_get_key_bits( &attributes ),
1602 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001603 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001604 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001605 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001606 for( n = 0; n < byte_size; n++ )
1607 TEST_EQUAL( buffer[n], 'K' );
1608 for( n = byte_size; n < buffer_size; n++ )
1609 TEST_EQUAL( buffer[n], 0 );
1610 }
1611
1612exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001613 /*
1614 * Key attributes may have been returned by psa_get_key_attributes()
1615 * thus reset them as required.
1616 */
1617 psa_reset_key_attributes( &attributes );
1618
Ronald Cron5425a212020-08-04 14:58:35 +02001619 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001620 PSA_DONE( );
1621 mbedtls_free( buffer );
1622}
1623/* END_CASE */
1624
1625/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001626void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1627{
Ronald Cron5425a212020-08-04 14:58:35 +02001628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001629 size_t bits = bits_arg;
1630 psa_status_t expected_status = expected_status_arg;
1631 psa_status_t status;
1632 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001633 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001634 size_t buffer_size = /* Slight overapproximations */
1635 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001636 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001637 unsigned char *p;
1638 int ret;
1639 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001640 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001641
Gilles Peskine8817f612018-12-18 00:18:46 +01001642 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001643 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001644
1645 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1646 bits, keypair ) ) >= 0 );
1647 length = ret;
1648
1649 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001650 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001651 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001652 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001653
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001654 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001655 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001656
1657exit:
1658 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001659 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001660}
1661/* END_CASE */
1662
1663/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001664void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001665 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001666 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001667 int expected_bits,
1668 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001669 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001670 int canonical_input )
1671{
Ronald Cron5425a212020-08-04 14:58:35 +02001672 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001673 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001674 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001675 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001676 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001677 unsigned char *exported = NULL;
1678 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001679 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001680 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001681 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001683 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001684
Moran Pekercb088e72018-07-17 17:36:59 +03001685 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001686 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001687 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001688 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001689 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001690
Gilles Peskine4747d192019-04-17 15:05:45 +02001691 psa_set_key_usage_flags( &attributes, usage_arg );
1692 psa_set_key_algorithm( &attributes, alg );
1693 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001694
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001695 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001696 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001697
1698 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001699 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001700 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1701 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001702 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001703
1704 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001705 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001706 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001707
1708 /* The exported length must be set by psa_export_key() to a value between 0
1709 * and export_size. On errors, the exported length must be 0. */
1710 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1711 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1712 TEST_ASSERT( exported_length <= export_size );
1713
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001714 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001715 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001716 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001717 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001718 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001719 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001720 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001721
Ronald Cron5425a212020-08-04 14:58:35 +02001722 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001723 goto exit;
1724
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001725 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001726 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001727 else
1728 {
Ronald Cron5425a212020-08-04 14:58:35 +02001729 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001730 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001731 &key2 ) );
1732 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001733 reexported,
1734 export_size,
1735 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001736 ASSERT_COMPARE( exported, exported_length,
1737 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001738 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001739 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001740 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001741
1742destroy:
1743 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001744 PSA_ASSERT( psa_destroy_key( key ) );
1745 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001746
1747exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001748 /*
1749 * Key attributes may have been returned by psa_get_key_attributes()
1750 * thus reset them as required.
1751 */
1752 psa_reset_key_attributes( &got_attributes );
1753
itayzafrir3e02b3b2018-06-12 17:06:52 +03001754 mbedtls_free( exported );
1755 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001756 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001757}
1758/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001759
Moran Pekerf709f4a2018-06-06 17:26:04 +03001760/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001761void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001762 int type_arg,
1763 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001764 int export_size_delta,
1765 int expected_export_status_arg,
1766 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001767{
Ronald Cron5425a212020-08-04 14:58:35 +02001768 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001769 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001770 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001771 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001772 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001773 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001774 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001775 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001777
Gilles Peskine8817f612018-12-18 00:18:46 +01001778 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001779
Gilles Peskine4747d192019-04-17 15:05:45 +02001780 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1781 psa_set_key_algorithm( &attributes, alg );
1782 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001783
1784 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001785 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001786
Gilles Peskine49c25912018-10-29 15:15:31 +01001787 /* Export the public key */
1788 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001789 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001790 exported, export_size,
1791 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001792 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001793 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001794 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001795 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001796 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001797 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001798 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001799 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001800 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001801 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1802 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001803 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001804
1805exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001806 /*
1807 * Key attributes may have been returned by psa_get_key_attributes()
1808 * thus reset them as required.
1809 */
1810 psa_reset_key_attributes( &attributes );
1811
itayzafrir3e02b3b2018-06-12 17:06:52 +03001812 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001813 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001814 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001815}
1816/* END_CASE */
1817
Gilles Peskine20035e32018-02-03 22:44:14 +01001818/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001819void import_and_exercise_key( data_t *data,
1820 int type_arg,
1821 int bits_arg,
1822 int alg_arg )
1823{
Ronald Cron5425a212020-08-04 14:58:35 +02001824 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001825 psa_key_type_t type = type_arg;
1826 size_t bits = bits_arg;
1827 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001828 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001829 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001830 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001831
Gilles Peskine8817f612018-12-18 00:18:46 +01001832 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001833
Gilles Peskine4747d192019-04-17 15:05:45 +02001834 psa_set_key_usage_flags( &attributes, usage );
1835 psa_set_key_algorithm( &attributes, alg );
1836 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001837
1838 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001839 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001840
1841 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001842 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001843 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1844 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001845
1846 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001847 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001848 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001849
Ronald Cron5425a212020-08-04 14:58:35 +02001850 PSA_ASSERT( psa_destroy_key( key ) );
1851 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001852
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001853exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001854 /*
1855 * Key attributes may have been returned by psa_get_key_attributes()
1856 * thus reset them as required.
1857 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001858 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001859
1860 psa_reset_key_attributes( &attributes );
1861 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001862 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001863}
1864/* END_CASE */
1865
1866/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001867void effective_key_attributes( int type_arg, int expected_type_arg,
1868 int bits_arg, int expected_bits_arg,
1869 int usage_arg, int expected_usage_arg,
1870 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001871{
Ronald Cron5425a212020-08-04 14:58:35 +02001872 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001873 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001874 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001875 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001876 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001877 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001878 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001879 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001880 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001881 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001882
Gilles Peskine8817f612018-12-18 00:18:46 +01001883 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001884
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001885 psa_set_key_usage_flags( &attributes, usage );
1886 psa_set_key_algorithm( &attributes, alg );
1887 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001888 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001889
Ronald Cron5425a212020-08-04 14:58:35 +02001890 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001891 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001892
Ronald Cron5425a212020-08-04 14:58:35 +02001893 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001894 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1895 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1896 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1897 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001898
1899exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001900 /*
1901 * Key attributes may have been returned by psa_get_key_attributes()
1902 * thus reset them as required.
1903 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001904 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001905
1906 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001907 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001908}
1909/* END_CASE */
1910
1911/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001912void check_key_policy( int type_arg, int bits_arg,
1913 int usage_arg, int alg_arg )
1914{
1915 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1916 usage_arg, usage_arg, alg_arg, alg_arg );
1917 goto exit;
1918}
1919/* END_CASE */
1920
1921/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001922void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001923{
1924 /* Test each valid way of initializing the object, except for `= {0}`, as
1925 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1926 * though it's OK by the C standard. We could test for this, but we'd need
1927 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001928 psa_key_attributes_t func = psa_key_attributes_init( );
1929 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1930 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001931
1932 memset( &zero, 0, sizeof( zero ) );
1933
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001934 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1935 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1936 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001937
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001938 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1939 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1940 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1941
1942 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1943 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1944 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1945
1946 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1947 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1948 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1949
1950 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1951 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1952 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001953}
1954/* END_CASE */
1955
1956/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001957void mac_key_policy( int policy_usage,
1958 int policy_alg,
1959 int key_type,
1960 data_t *key_data,
1961 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001962{
Ronald Cron5425a212020-08-04 14:58:35 +02001963 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001965 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001966 psa_status_t status;
1967 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001968
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001970
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001971 psa_set_key_usage_flags( &attributes, policy_usage );
1972 psa_set_key_algorithm( &attributes, policy_alg );
1973 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001974
Gilles Peskine049c7532019-05-15 20:22:09 +02001975 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001976 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001977
Ronald Cron5425a212020-08-04 14:58:35 +02001978 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001979 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001980 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001981 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001982 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001983 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001985
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001987 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001988 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001989 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001990 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001991 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001992 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001993
1994exit:
1995 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001996 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001997 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001998}
1999/* END_CASE */
2000
2001/* BEGIN_CASE */
2002void cipher_key_policy( int policy_usage,
2003 int policy_alg,
2004 int key_type,
2005 data_t *key_data,
2006 int exercise_alg )
2007{
Ronald Cron5425a212020-08-04 14:58:35 +02002008 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002009 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002010 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002011 psa_status_t status;
2012
Gilles Peskine8817f612018-12-18 00:18:46 +01002013 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002014
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002015 psa_set_key_usage_flags( &attributes, policy_usage );
2016 psa_set_key_algorithm( &attributes, policy_alg );
2017 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002018
Gilles Peskine049c7532019-05-15 20:22:09 +02002019 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002020 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021
Ronald Cron5425a212020-08-04 14:58:35 +02002022 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023 if( policy_alg == exercise_alg &&
2024 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002025 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002026 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002027 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028 psa_cipher_abort( &operation );
2029
Ronald Cron5425a212020-08-04 14:58:35 +02002030 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002031 if( policy_alg == exercise_alg &&
2032 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002033 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002035 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002036
2037exit:
2038 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002039 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002040 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041}
2042/* END_CASE */
2043
2044/* BEGIN_CASE */
2045void aead_key_policy( int policy_usage,
2046 int policy_alg,
2047 int key_type,
2048 data_t *key_data,
2049 int nonce_length_arg,
2050 int tag_length_arg,
2051 int exercise_alg )
2052{
Ronald Cron5425a212020-08-04 14:58:35 +02002053 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002054 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002055 psa_status_t status;
2056 unsigned char nonce[16] = {0};
2057 size_t nonce_length = nonce_length_arg;
2058 unsigned char tag[16];
2059 size_t tag_length = tag_length_arg;
2060 size_t output_length;
2061
2062 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
2063 TEST_ASSERT( tag_length <= sizeof( tag ) );
2064
Gilles Peskine8817f612018-12-18 00:18:46 +01002065 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002067 psa_set_key_usage_flags( &attributes, policy_usage );
2068 psa_set_key_algorithm( &attributes, policy_alg );
2069 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070
Gilles Peskine049c7532019-05-15 20:22:09 +02002071 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002072 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073
Ronald Cron5425a212020-08-04 14:58:35 +02002074 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075 nonce, nonce_length,
2076 NULL, 0,
2077 NULL, 0,
2078 tag, tag_length,
2079 &output_length );
2080 if( policy_alg == exercise_alg &&
2081 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002082 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002083 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002084 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002085
2086 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002087 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002088 nonce, nonce_length,
2089 NULL, 0,
2090 tag, tag_length,
2091 NULL, 0,
2092 &output_length );
2093 if( policy_alg == exercise_alg &&
2094 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002095 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002097 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002098
2099exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002100 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002101 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002102}
2103/* END_CASE */
2104
2105/* BEGIN_CASE */
2106void asymmetric_encryption_key_policy( int policy_usage,
2107 int policy_alg,
2108 int key_type,
2109 data_t *key_data,
2110 int exercise_alg )
2111{
Ronald Cron5425a212020-08-04 14:58:35 +02002112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002114 psa_status_t status;
2115 size_t key_bits;
2116 size_t buffer_length;
2117 unsigned char *buffer = NULL;
2118 size_t output_length;
2119
Gilles Peskine8817f612018-12-18 00:18:46 +01002120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002122 psa_set_key_usage_flags( &attributes, policy_usage );
2123 psa_set_key_algorithm( &attributes, policy_alg );
2124 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
Gilles Peskine049c7532019-05-15 20:22:09 +02002126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002127 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128
Ronald Cron5425a212020-08-04 14:58:35 +02002129 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002130 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002131 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2132 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002133 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002134
Ronald Cron5425a212020-08-04 14:58:35 +02002135 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002136 NULL, 0,
2137 NULL, 0,
2138 buffer, buffer_length,
2139 &output_length );
2140 if( policy_alg == exercise_alg &&
2141 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002142 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002143 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002144 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002146 if( buffer_length != 0 )
2147 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002148 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002149 buffer, buffer_length,
2150 NULL, 0,
2151 buffer, buffer_length,
2152 &output_length );
2153 if( policy_alg == exercise_alg &&
2154 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002155 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002156 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002157 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002158
2159exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002160 /*
2161 * Key attributes may have been returned by psa_get_key_attributes()
2162 * thus reset them as required.
2163 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002164 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002165
2166 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002167 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002168 mbedtls_free( buffer );
2169}
2170/* END_CASE */
2171
2172/* BEGIN_CASE */
2173void asymmetric_signature_key_policy( int policy_usage,
2174 int policy_alg,
2175 int key_type,
2176 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002177 int exercise_alg,
2178 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179{
Ronald Cron5425a212020-08-04 14:58:35 +02002180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002181 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002182 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002183 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2184 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2185 * compatible with the policy and `payload_length_arg` is supposed to be
2186 * a valid input length to sign. If `payload_length_arg <= 0`,
2187 * `exercise_alg` is supposed to be forbidden by the policy. */
2188 int compatible_alg = payload_length_arg > 0;
2189 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002190 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002191 size_t signature_length;
2192
Gilles Peskine8817f612018-12-18 00:18:46 +01002193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002194
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002195 psa_set_key_usage_flags( &attributes, policy_usage );
2196 psa_set_key_algorithm( &attributes, policy_alg );
2197 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002198
Gilles Peskine049c7532019-05-15 20:22:09 +02002199 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002200 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002201
Ronald Cron5425a212020-08-04 14:58:35 +02002202 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002203 payload, payload_length,
2204 signature, sizeof( signature ),
2205 &signature_length );
2206 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002207 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002208 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002209 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002210
2211 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002212 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002213 payload, payload_length,
2214 signature, sizeof( signature ) );
2215 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002216 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002217 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002218 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002219
2220exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002221 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002222 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002223}
2224/* END_CASE */
2225
Janos Follathba3fab92019-06-11 14:50:16 +01002226/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002227void derive_key_policy( int policy_usage,
2228 int policy_alg,
2229 int key_type,
2230 data_t *key_data,
2231 int exercise_alg )
2232{
Ronald Cron5425a212020-08-04 14:58:35 +02002233 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002235 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002236 psa_status_t status;
2237
Gilles Peskine8817f612018-12-18 00:18:46 +01002238 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002239
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002240 psa_set_key_usage_flags( &attributes, policy_usage );
2241 psa_set_key_algorithm( &attributes, policy_alg );
2242 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002243
Gilles Peskine049c7532019-05-15 20:22:09 +02002244 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002245 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002246
Janos Follathba3fab92019-06-11 14:50:16 +01002247 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2248
2249 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2250 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002251 {
Janos Follathba3fab92019-06-11 14:50:16 +01002252 PSA_ASSERT( psa_key_derivation_input_bytes(
2253 &operation,
2254 PSA_KEY_DERIVATION_INPUT_SEED,
2255 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002256 }
Janos Follathba3fab92019-06-11 14:50:16 +01002257
2258 status = psa_key_derivation_input_key( &operation,
2259 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002260 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002261
Gilles Peskineea0fb492018-07-12 17:17:20 +02002262 if( policy_alg == exercise_alg &&
2263 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002264 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002265 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002266 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002267
2268exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002269 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002270 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002271 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002272}
2273/* END_CASE */
2274
2275/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002276void agreement_key_policy( int policy_usage,
2277 int policy_alg,
2278 int key_type_arg,
2279 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002280 int exercise_alg,
2281 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002282{
Ronald Cron5425a212020-08-04 14:58:35 +02002283 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002284 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002285 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002286 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002287 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002288 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002289
Gilles Peskine8817f612018-12-18 00:18:46 +01002290 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002291
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002292 psa_set_key_usage_flags( &attributes, policy_usage );
2293 psa_set_key_algorithm( &attributes, policy_alg );
2294 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002295
Gilles Peskine049c7532019-05-15 20:22:09 +02002296 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002297 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002298
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002299 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002300 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002301
Steven Cooremance48e852020-10-05 16:02:45 +02002302 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002303
2304exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002305 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002306 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002307 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002308}
2309/* END_CASE */
2310
2311/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002312void key_policy_alg2( int key_type_arg, data_t *key_data,
2313 int usage_arg, int alg_arg, int alg2_arg )
2314{
Ronald Cron5425a212020-08-04 14:58:35 +02002315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002316 psa_key_type_t key_type = key_type_arg;
2317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2318 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2319 psa_key_usage_t usage = usage_arg;
2320 psa_algorithm_t alg = alg_arg;
2321 psa_algorithm_t alg2 = alg2_arg;
2322
2323 PSA_ASSERT( psa_crypto_init( ) );
2324
2325 psa_set_key_usage_flags( &attributes, usage );
2326 psa_set_key_algorithm( &attributes, alg );
2327 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2328 psa_set_key_type( &attributes, key_type );
2329 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002330 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002331
Ronald Cron5425a212020-08-04 14:58:35 +02002332 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002333 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2334 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2335 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2336
Ronald Cron5425a212020-08-04 14:58:35 +02002337 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002338 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002339 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002340 goto exit;
2341
2342exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002343 /*
2344 * Key attributes may have been returned by psa_get_key_attributes()
2345 * thus reset them as required.
2346 */
2347 psa_reset_key_attributes( &got_attributes );
2348
Ronald Cron5425a212020-08-04 14:58:35 +02002349 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002350 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002351}
2352/* END_CASE */
2353
2354/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002355void raw_agreement_key_policy( int policy_usage,
2356 int policy_alg,
2357 int key_type_arg,
2358 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002359 int exercise_alg,
2360 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002361{
Ronald Cron5425a212020-08-04 14:58:35 +02002362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002364 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002365 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002366 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002367 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002368
2369 PSA_ASSERT( psa_crypto_init( ) );
2370
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002371 psa_set_key_usage_flags( &attributes, policy_usage );
2372 psa_set_key_algorithm( &attributes, policy_alg );
2373 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002374
Gilles Peskine049c7532019-05-15 20:22:09 +02002375 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002376 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002377
Ronald Cron5425a212020-08-04 14:58:35 +02002378 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002379
Steven Cooremance48e852020-10-05 16:02:45 +02002380 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002381
2382exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002383 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002384 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002385 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002386}
2387/* END_CASE */
2388
2389/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002390void copy_success( int source_usage_arg,
2391 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002392 int type_arg, data_t *material,
2393 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002394 int target_usage_arg,
2395 int target_alg_arg, int target_alg2_arg,
2396 int expected_usage_arg,
2397 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002398{
Gilles Peskineca25db92019-04-19 11:43:08 +02002399 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2400 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002401 psa_key_usage_t expected_usage = expected_usage_arg;
2402 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002403 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002404 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2405 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002406 uint8_t *export_buffer = NULL;
2407
Gilles Peskine57ab7212019-01-28 13:03:09 +01002408 PSA_ASSERT( psa_crypto_init( ) );
2409
Gilles Peskineca25db92019-04-19 11:43:08 +02002410 /* Prepare the source key. */
2411 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2412 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002413 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002414 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002415 PSA_ASSERT( psa_import_key( &source_attributes,
2416 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002417 &source_key ) );
2418 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002419
Gilles Peskineca25db92019-04-19 11:43:08 +02002420 /* Prepare the target attributes. */
2421 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002422 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002423 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002424 /* Set volatile lifetime to reset the key identifier to 0. */
2425 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2426 }
2427
Gilles Peskineca25db92019-04-19 11:43:08 +02002428 if( target_usage_arg != -1 )
2429 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2430 if( target_alg_arg != -1 )
2431 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002432 if( target_alg2_arg != -1 )
2433 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002434
2435 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002436 PSA_ASSERT( psa_copy_key( source_key,
2437 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002438
2439 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002440 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002441
2442 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002443 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002444 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2445 psa_get_key_type( &target_attributes ) );
2446 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2447 psa_get_key_bits( &target_attributes ) );
2448 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2449 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002450 TEST_EQUAL( expected_alg2,
2451 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002452 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2453 {
2454 size_t length;
2455 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002456 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002457 material->len, &length ) );
2458 ASSERT_COMPARE( material->x, material->len,
2459 export_buffer, length );
2460 }
Ronald Cron5425a212020-08-04 14:58:35 +02002461 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002462 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002463 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002464 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002465
Ronald Cron5425a212020-08-04 14:58:35 +02002466 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002467
2468exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002469 /*
2470 * Source and target key attributes may have been returned by
2471 * psa_get_key_attributes() thus reset them as required.
2472 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002473 psa_reset_key_attributes( &source_attributes );
2474 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002475
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002476 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002477 mbedtls_free( export_buffer );
2478}
2479/* END_CASE */
2480
2481/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002482void copy_fail( int source_usage_arg,
2483 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002484 int type_arg, data_t *material,
2485 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002486 int target_usage_arg,
2487 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002488 int expected_status_arg )
2489{
2490 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2491 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002492 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2493 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002494
2495 PSA_ASSERT( psa_crypto_init( ) );
2496
2497 /* Prepare the source key. */
2498 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2499 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002500 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002501 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002502 PSA_ASSERT( psa_import_key( &source_attributes,
2503 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002504 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002505
2506 /* Prepare the target attributes. */
2507 psa_set_key_type( &target_attributes, target_type_arg );
2508 psa_set_key_bits( &target_attributes, target_bits_arg );
2509 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2510 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002511 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002512
2513 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002514 TEST_EQUAL( psa_copy_key( source_key,
2515 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002516 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002517
Ronald Cron5425a212020-08-04 14:58:35 +02002518 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002519
Gilles Peskine4a644642019-05-03 17:14:08 +02002520exit:
2521 psa_reset_key_attributes( &source_attributes );
2522 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002523 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002524}
2525/* END_CASE */
2526
2527/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002528void hash_operation_init( )
2529{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002530 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002531 /* Test each valid way of initializing the object, except for `= {0}`, as
2532 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2533 * though it's OK by the C standard. We could test for this, but we'd need
2534 * to supress the Clang warning for the test. */
2535 psa_hash_operation_t func = psa_hash_operation_init( );
2536 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2537 psa_hash_operation_t zero;
2538
2539 memset( &zero, 0, sizeof( zero ) );
2540
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002541 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002542 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2543 PSA_ERROR_BAD_STATE );
2544 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2545 PSA_ERROR_BAD_STATE );
2546 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2547 PSA_ERROR_BAD_STATE );
2548
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002549 /* A default hash operation should be abortable without error. */
2550 PSA_ASSERT( psa_hash_abort( &func ) );
2551 PSA_ASSERT( psa_hash_abort( &init ) );
2552 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002553}
2554/* END_CASE */
2555
2556/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002557void hash_setup( int alg_arg,
2558 int expected_status_arg )
2559{
2560 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002561 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002562 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563 psa_status_t status;
2564
Gilles Peskine8817f612018-12-18 00:18:46 +01002565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002566
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002567 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002568 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002569
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002570 /* Whether setup succeeded or failed, abort must succeed. */
2571 PSA_ASSERT( psa_hash_abort( &operation ) );
2572
2573 /* If setup failed, reproduce the failure, so as to
2574 * test the resulting state of the operation object. */
2575 if( status != PSA_SUCCESS )
2576 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2577
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002578 /* Now the operation object should be reusable. */
2579#if defined(KNOWN_SUPPORTED_HASH_ALG)
2580 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2581 PSA_ASSERT( psa_hash_abort( &operation ) );
2582#endif
2583
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002584exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002585 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002586}
2587/* END_CASE */
2588
2589/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002590void hash_compute_fail( int alg_arg, data_t *input,
2591 int output_size_arg, int expected_status_arg )
2592{
2593 psa_algorithm_t alg = alg_arg;
2594 uint8_t *output = NULL;
2595 size_t output_size = output_size_arg;
2596 size_t output_length = INVALID_EXPORT_LENGTH;
2597 psa_status_t expected_status = expected_status_arg;
2598 psa_status_t status;
2599
2600 ASSERT_ALLOC( output, output_size );
2601
2602 PSA_ASSERT( psa_crypto_init( ) );
2603
2604 status = psa_hash_compute( alg, input->x, input->len,
2605 output, output_size, &output_length );
2606 TEST_EQUAL( status, expected_status );
2607 TEST_ASSERT( output_length <= output_size );
2608
2609exit:
2610 mbedtls_free( output );
2611 PSA_DONE( );
2612}
2613/* END_CASE */
2614
2615/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002616void hash_compare_fail( int alg_arg, data_t *input,
2617 data_t *reference_hash,
2618 int expected_status_arg )
2619{
2620 psa_algorithm_t alg = alg_arg;
2621 psa_status_t expected_status = expected_status_arg;
2622 psa_status_t status;
2623
2624 PSA_ASSERT( psa_crypto_init( ) );
2625
2626 status = psa_hash_compare( alg, input->x, input->len,
2627 reference_hash->x, reference_hash->len );
2628 TEST_EQUAL( status, expected_status );
2629
2630exit:
2631 PSA_DONE( );
2632}
2633/* END_CASE */
2634
2635/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002636void hash_compute_compare( int alg_arg, data_t *input,
2637 data_t *expected_output )
2638{
2639 psa_algorithm_t alg = alg_arg;
2640 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2641 size_t output_length = INVALID_EXPORT_LENGTH;
2642 size_t i;
2643
2644 PSA_ASSERT( psa_crypto_init( ) );
2645
2646 /* Compute with tight buffer */
2647 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002648 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002649 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002650 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002651 ASSERT_COMPARE( output, output_length,
2652 expected_output->x, expected_output->len );
2653
2654 /* Compute with larger buffer */
2655 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2656 output, sizeof( output ),
2657 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002658 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002659 ASSERT_COMPARE( output, output_length,
2660 expected_output->x, expected_output->len );
2661
2662 /* Compare with correct hash */
2663 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2664 output, output_length ) );
2665
2666 /* Compare with trailing garbage */
2667 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2668 output, output_length + 1 ),
2669 PSA_ERROR_INVALID_SIGNATURE );
2670
2671 /* Compare with truncated hash */
2672 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2673 output, output_length - 1 ),
2674 PSA_ERROR_INVALID_SIGNATURE );
2675
2676 /* Compare with corrupted value */
2677 for( i = 0; i < output_length; i++ )
2678 {
2679 test_set_step( i );
2680 output[i] ^= 1;
2681 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2682 output, output_length ),
2683 PSA_ERROR_INVALID_SIGNATURE );
2684 output[i] ^= 1;
2685 }
2686
2687exit:
2688 PSA_DONE( );
2689}
2690/* END_CASE */
2691
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002692/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002693void hash_bad_order( )
2694{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002695 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002696 unsigned char input[] = "";
2697 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002698 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002699 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2700 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2701 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002702 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002703 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002704 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002705
Gilles Peskine8817f612018-12-18 00:18:46 +01002706 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002707
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002708 /* Call setup twice in a row. */
2709 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2710 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2711 PSA_ERROR_BAD_STATE );
2712 PSA_ASSERT( psa_hash_abort( &operation ) );
2713
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002714 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002715 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002716 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002717 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002718
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002719 /* Call update after finish. */
2720 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2721 PSA_ASSERT( psa_hash_finish( &operation,
2722 hash, sizeof( hash ), &hash_len ) );
2723 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002724 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002725 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002726
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002727 /* Call verify without calling setup beforehand. */
2728 TEST_EQUAL( psa_hash_verify( &operation,
2729 valid_hash, sizeof( valid_hash ) ),
2730 PSA_ERROR_BAD_STATE );
2731 PSA_ASSERT( psa_hash_abort( &operation ) );
2732
2733 /* Call verify after finish. */
2734 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2735 PSA_ASSERT( psa_hash_finish( &operation,
2736 hash, sizeof( hash ), &hash_len ) );
2737 TEST_EQUAL( psa_hash_verify( &operation,
2738 valid_hash, sizeof( valid_hash ) ),
2739 PSA_ERROR_BAD_STATE );
2740 PSA_ASSERT( psa_hash_abort( &operation ) );
2741
2742 /* Call verify twice in a row. */
2743 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2744 PSA_ASSERT( psa_hash_verify( &operation,
2745 valid_hash, sizeof( valid_hash ) ) );
2746 TEST_EQUAL( psa_hash_verify( &operation,
2747 valid_hash, sizeof( valid_hash ) ),
2748 PSA_ERROR_BAD_STATE );
2749 PSA_ASSERT( psa_hash_abort( &operation ) );
2750
2751 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002752 TEST_EQUAL( psa_hash_finish( &operation,
2753 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002754 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002755 PSA_ASSERT( psa_hash_abort( &operation ) );
2756
2757 /* Call finish twice in a row. */
2758 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2759 PSA_ASSERT( psa_hash_finish( &operation,
2760 hash, sizeof( hash ), &hash_len ) );
2761 TEST_EQUAL( psa_hash_finish( &operation,
2762 hash, sizeof( hash ), &hash_len ),
2763 PSA_ERROR_BAD_STATE );
2764 PSA_ASSERT( psa_hash_abort( &operation ) );
2765
2766 /* Call finish after calling verify. */
2767 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2768 PSA_ASSERT( psa_hash_verify( &operation,
2769 valid_hash, sizeof( valid_hash ) ) );
2770 TEST_EQUAL( psa_hash_finish( &operation,
2771 hash, sizeof( hash ), &hash_len ),
2772 PSA_ERROR_BAD_STATE );
2773 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002774
2775exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002776 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002777}
2778/* END_CASE */
2779
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002780/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002781void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002782{
2783 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002784 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2785 * appended to it */
2786 unsigned char hash[] = {
2787 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2788 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2789 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002790 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002791 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002792
Gilles Peskine8817f612018-12-18 00:18:46 +01002793 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002794
itayzafrir27e69452018-11-01 14:26:34 +02002795 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002796 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002797 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002798 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002799
itayzafrir27e69452018-11-01 14:26:34 +02002800 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002801 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002802 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002803 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002804
itayzafrir27e69452018-11-01 14:26:34 +02002805 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002806 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002807 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002808 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002809
itayzafrirec93d302018-10-18 18:01:10 +03002810exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002811 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002812}
2813/* END_CASE */
2814
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002815/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2816void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002817{
2818 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002819 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002820 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002821 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002822 size_t hash_len;
2823
Gilles Peskine8817f612018-12-18 00:18:46 +01002824 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002825
itayzafrir58028322018-10-25 10:22:01 +03002826 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002827 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002828 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002829 hash, expected_size - 1, &hash_len ),
2830 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002831
2832exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002833 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002834}
2835/* END_CASE */
2836
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002837/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2838void hash_clone_source_state( )
2839{
2840 psa_algorithm_t alg = PSA_ALG_SHA_256;
2841 unsigned char hash[PSA_HASH_MAX_SIZE];
2842 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2843 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2844 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2845 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2846 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2847 size_t hash_len;
2848
2849 PSA_ASSERT( psa_crypto_init( ) );
2850 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2851
2852 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2853 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2854 PSA_ASSERT( psa_hash_finish( &op_finished,
2855 hash, sizeof( hash ), &hash_len ) );
2856 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2857 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2858
2859 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2860 PSA_ERROR_BAD_STATE );
2861
2862 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2863 PSA_ASSERT( psa_hash_finish( &op_init,
2864 hash, sizeof( hash ), &hash_len ) );
2865 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2866 PSA_ASSERT( psa_hash_finish( &op_finished,
2867 hash, sizeof( hash ), &hash_len ) );
2868 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2869 PSA_ASSERT( psa_hash_finish( &op_aborted,
2870 hash, sizeof( hash ), &hash_len ) );
2871
2872exit:
2873 psa_hash_abort( &op_source );
2874 psa_hash_abort( &op_init );
2875 psa_hash_abort( &op_setup );
2876 psa_hash_abort( &op_finished );
2877 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002878 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002879}
2880/* END_CASE */
2881
2882/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2883void hash_clone_target_state( )
2884{
2885 psa_algorithm_t alg = PSA_ALG_SHA_256;
2886 unsigned char hash[PSA_HASH_MAX_SIZE];
2887 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2888 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2889 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2890 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2891 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2892 size_t hash_len;
2893
2894 PSA_ASSERT( psa_crypto_init( ) );
2895
2896 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2897 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2898 PSA_ASSERT( psa_hash_finish( &op_finished,
2899 hash, sizeof( hash ), &hash_len ) );
2900 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2901 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2902
2903 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2904 PSA_ASSERT( psa_hash_finish( &op_target,
2905 hash, sizeof( hash ), &hash_len ) );
2906
2907 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2908 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2909 PSA_ERROR_BAD_STATE );
2910 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2911 PSA_ERROR_BAD_STATE );
2912
2913exit:
2914 psa_hash_abort( &op_target );
2915 psa_hash_abort( &op_init );
2916 psa_hash_abort( &op_setup );
2917 psa_hash_abort( &op_finished );
2918 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002919 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002920}
2921/* END_CASE */
2922
itayzafrir58028322018-10-25 10:22:01 +03002923/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002924void mac_operation_init( )
2925{
Jaeden Amero252ef282019-02-15 14:05:35 +00002926 const uint8_t input[1] = { 0 };
2927
Jaeden Amero769ce272019-01-04 11:48:03 +00002928 /* Test each valid way of initializing the object, except for `= {0}`, as
2929 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2930 * though it's OK by the C standard. We could test for this, but we'd need
2931 * to supress the Clang warning for the test. */
2932 psa_mac_operation_t func = psa_mac_operation_init( );
2933 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2934 psa_mac_operation_t zero;
2935
2936 memset( &zero, 0, sizeof( zero ) );
2937
Jaeden Amero252ef282019-02-15 14:05:35 +00002938 /* A freshly-initialized MAC operation should not be usable. */
2939 TEST_EQUAL( psa_mac_update( &func,
2940 input, sizeof( input ) ),
2941 PSA_ERROR_BAD_STATE );
2942 TEST_EQUAL( psa_mac_update( &init,
2943 input, sizeof( input ) ),
2944 PSA_ERROR_BAD_STATE );
2945 TEST_EQUAL( psa_mac_update( &zero,
2946 input, sizeof( input ) ),
2947 PSA_ERROR_BAD_STATE );
2948
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002949 /* A default MAC operation should be abortable without error. */
2950 PSA_ASSERT( psa_mac_abort( &func ) );
2951 PSA_ASSERT( psa_mac_abort( &init ) );
2952 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002953}
2954/* END_CASE */
2955
2956/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002957void mac_setup( int key_type_arg,
2958 data_t *key,
2959 int alg_arg,
2960 int expected_status_arg )
2961{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002962 psa_key_type_t key_type = key_type_arg;
2963 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002964 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002965 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002966 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2967#if defined(KNOWN_SUPPORTED_MAC_ALG)
2968 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2969#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002970
Gilles Peskine8817f612018-12-18 00:18:46 +01002971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002972
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002973 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2974 &operation, &status ) )
2975 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002976 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002977
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002978 /* The operation object should be reusable. */
2979#if defined(KNOWN_SUPPORTED_MAC_ALG)
2980 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2981 smoke_test_key_data,
2982 sizeof( smoke_test_key_data ),
2983 KNOWN_SUPPORTED_MAC_ALG,
2984 &operation, &status ) )
2985 goto exit;
2986 TEST_EQUAL( status, PSA_SUCCESS );
2987#endif
2988
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002989exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002990 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002991}
2992/* END_CASE */
2993
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002994/* 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 +00002995void mac_bad_order( )
2996{
Ronald Cron5425a212020-08-04 14:58:35 +02002997 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002998 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2999 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003000 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003001 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3002 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3003 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003004 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003005 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3006 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3007 size_t sign_mac_length = 0;
3008 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3009 const uint8_t verify_mac[] = {
3010 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3011 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3012 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3013
3014 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003015 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003016 psa_set_key_algorithm( &attributes, alg );
3017 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003018
Ronald Cron5425a212020-08-04 14:58:35 +02003019 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3020 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003021
Jaeden Amero252ef282019-02-15 14:05:35 +00003022 /* Call update without calling setup beforehand. */
3023 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3024 PSA_ERROR_BAD_STATE );
3025 PSA_ASSERT( psa_mac_abort( &operation ) );
3026
3027 /* Call sign finish without calling setup beforehand. */
3028 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3029 &sign_mac_length),
3030 PSA_ERROR_BAD_STATE );
3031 PSA_ASSERT( psa_mac_abort( &operation ) );
3032
3033 /* Call verify finish without calling setup beforehand. */
3034 TEST_EQUAL( psa_mac_verify_finish( &operation,
3035 verify_mac, sizeof( verify_mac ) ),
3036 PSA_ERROR_BAD_STATE );
3037 PSA_ASSERT( psa_mac_abort( &operation ) );
3038
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003039 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003040 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
3041 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003042 PSA_ERROR_BAD_STATE );
3043 PSA_ASSERT( psa_mac_abort( &operation ) );
3044
Jaeden Amero252ef282019-02-15 14:05:35 +00003045 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003046 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003047 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3048 PSA_ASSERT( psa_mac_sign_finish( &operation,
3049 sign_mac, sizeof( sign_mac ),
3050 &sign_mac_length ) );
3051 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3052 PSA_ERROR_BAD_STATE );
3053 PSA_ASSERT( psa_mac_abort( &operation ) );
3054
3055 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003056 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003057 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3058 PSA_ASSERT( psa_mac_verify_finish( &operation,
3059 verify_mac, sizeof( verify_mac ) ) );
3060 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3061 PSA_ERROR_BAD_STATE );
3062 PSA_ASSERT( psa_mac_abort( &operation ) );
3063
3064 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003065 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003066 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3067 PSA_ASSERT( psa_mac_sign_finish( &operation,
3068 sign_mac, sizeof( sign_mac ),
3069 &sign_mac_length ) );
3070 TEST_EQUAL( psa_mac_sign_finish( &operation,
3071 sign_mac, sizeof( sign_mac ),
3072 &sign_mac_length ),
3073 PSA_ERROR_BAD_STATE );
3074 PSA_ASSERT( psa_mac_abort( &operation ) );
3075
3076 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003077 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003078 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3079 PSA_ASSERT( psa_mac_verify_finish( &operation,
3080 verify_mac, sizeof( verify_mac ) ) );
3081 TEST_EQUAL( psa_mac_verify_finish( &operation,
3082 verify_mac, sizeof( verify_mac ) ),
3083 PSA_ERROR_BAD_STATE );
3084 PSA_ASSERT( psa_mac_abort( &operation ) );
3085
3086 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003087 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003088 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3089 TEST_EQUAL( psa_mac_verify_finish( &operation,
3090 verify_mac, sizeof( verify_mac ) ),
3091 PSA_ERROR_BAD_STATE );
3092 PSA_ASSERT( psa_mac_abort( &operation ) );
3093
3094 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003095 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003096 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3097 TEST_EQUAL( psa_mac_sign_finish( &operation,
3098 sign_mac, sizeof( sign_mac ),
3099 &sign_mac_length ),
3100 PSA_ERROR_BAD_STATE );
3101 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003102
Ronald Cron5425a212020-08-04 14:58:35 +02003103 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003104
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003105exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003106 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003107}
3108/* END_CASE */
3109
3110/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003111void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003112 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003113 int alg_arg,
3114 data_t *input,
3115 data_t *expected_mac )
3116{
Ronald Cron5425a212020-08-04 14:58:35 +02003117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003118 psa_key_type_t key_type = key_type_arg;
3119 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003120 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003121 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003122 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003123 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003124 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003125 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003126 const size_t output_sizes_to_test[] = {
3127 0,
3128 1,
3129 expected_mac->len - 1,
3130 expected_mac->len,
3131 expected_mac->len + 1,
3132 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003133
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003134 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003135 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003136 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003137
Gilles Peskine8817f612018-12-18 00:18:46 +01003138 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003139
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003140 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003141 psa_set_key_algorithm( &attributes, alg );
3142 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003143
Ronald Cron5425a212020-08-04 14:58:35 +02003144 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3145 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003146
Gilles Peskine8b356b52020-08-25 23:44:59 +02003147 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3148 {
3149 const size_t output_size = output_sizes_to_test[i];
3150 psa_status_t expected_status =
3151 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3152 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003153
Gilles Peskine8b356b52020-08-25 23:44:59 +02003154 test_set_step( output_size );
3155 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003156
Gilles Peskine8b356b52020-08-25 23:44:59 +02003157 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003158 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003159 PSA_ASSERT( psa_mac_update( &operation,
3160 input->x, input->len ) );
3161 TEST_EQUAL( psa_mac_sign_finish( &operation,
3162 actual_mac, output_size,
3163 &mac_length ),
3164 expected_status );
3165 PSA_ASSERT( psa_mac_abort( &operation ) );
3166
3167 if( expected_status == PSA_SUCCESS )
3168 {
3169 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3170 actual_mac, mac_length );
3171 }
3172 mbedtls_free( actual_mac );
3173 actual_mac = NULL;
3174 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003175
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003176exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003177 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003178 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003179 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003180 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003181}
3182/* END_CASE */
3183
3184/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003185void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003186 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003187 int alg_arg,
3188 data_t *input,
3189 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003190{
Ronald Cron5425a212020-08-04 14:58:35 +02003191 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003192 psa_key_type_t key_type = key_type_arg;
3193 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003194 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003195 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003196 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003197
Gilles Peskine69c12672018-06-28 00:07:19 +02003198 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3199
Gilles Peskine8817f612018-12-18 00:18:46 +01003200 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003201
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003202 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003203 psa_set_key_algorithm( &attributes, alg );
3204 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003205
Ronald Cron5425a212020-08-04 14:58:35 +02003206 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3207 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003208
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003209 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003210 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_mac_update( &operation,
3212 input->x, input->len ) );
3213 PSA_ASSERT( psa_mac_verify_finish( &operation,
3214 expected_mac->x,
3215 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003216
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003217 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003218 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003219 PSA_ASSERT( psa_mac_update( &operation,
3220 input->x, input->len ) );
3221 TEST_EQUAL( psa_mac_verify_finish( &operation,
3222 expected_mac->x,
3223 expected_mac->len - 1 ),
3224 PSA_ERROR_INVALID_SIGNATURE );
3225
3226 /* Test a MAC that's too long. */
3227 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3228 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003229 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003230 PSA_ASSERT( psa_mac_update( &operation,
3231 input->x, input->len ) );
3232 TEST_EQUAL( psa_mac_verify_finish( &operation,
3233 perturbed_mac,
3234 expected_mac->len + 1 ),
3235 PSA_ERROR_INVALID_SIGNATURE );
3236
3237 /* Test changing one byte. */
3238 for( size_t i = 0; i < expected_mac->len; i++ )
3239 {
3240 test_set_step( i );
3241 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003242 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003243 PSA_ASSERT( psa_mac_update( &operation,
3244 input->x, input->len ) );
3245 TEST_EQUAL( psa_mac_verify_finish( &operation,
3246 perturbed_mac,
3247 expected_mac->len ),
3248 PSA_ERROR_INVALID_SIGNATURE );
3249 perturbed_mac[i] ^= 1;
3250 }
3251
Gilles Peskine8c9def32018-02-08 10:02:12 +01003252exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003253 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003254 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003255 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003256 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003257}
3258/* END_CASE */
3259
3260/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003261void cipher_operation_init( )
3262{
Jaeden Ameroab439972019-02-15 14:12:05 +00003263 const uint8_t input[1] = { 0 };
3264 unsigned char output[1] = { 0 };
3265 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003266 /* Test each valid way of initializing the object, except for `= {0}`, as
3267 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3268 * though it's OK by the C standard. We could test for this, but we'd need
3269 * to supress the Clang warning for the test. */
3270 psa_cipher_operation_t func = psa_cipher_operation_init( );
3271 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3272 psa_cipher_operation_t zero;
3273
3274 memset( &zero, 0, sizeof( zero ) );
3275
Jaeden Ameroab439972019-02-15 14:12:05 +00003276 /* A freshly-initialized cipher operation should not be usable. */
3277 TEST_EQUAL( psa_cipher_update( &func,
3278 input, sizeof( input ),
3279 output, sizeof( output ),
3280 &output_length ),
3281 PSA_ERROR_BAD_STATE );
3282 TEST_EQUAL( psa_cipher_update( &init,
3283 input, sizeof( input ),
3284 output, sizeof( output ),
3285 &output_length ),
3286 PSA_ERROR_BAD_STATE );
3287 TEST_EQUAL( psa_cipher_update( &zero,
3288 input, sizeof( input ),
3289 output, sizeof( output ),
3290 &output_length ),
3291 PSA_ERROR_BAD_STATE );
3292
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003293 /* A default cipher operation should be abortable without error. */
3294 PSA_ASSERT( psa_cipher_abort( &func ) );
3295 PSA_ASSERT( psa_cipher_abort( &init ) );
3296 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003297}
3298/* END_CASE */
3299
3300/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003301void cipher_setup( int key_type_arg,
3302 data_t *key,
3303 int alg_arg,
3304 int expected_status_arg )
3305{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003306 psa_key_type_t key_type = key_type_arg;
3307 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003308 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003309 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003310 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003311#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003312 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3313#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003314
Gilles Peskine8817f612018-12-18 00:18:46 +01003315 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003316
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003317 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3318 &operation, &status ) )
3319 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003320 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003321
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003322 /* The operation object should be reusable. */
3323#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3324 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3325 smoke_test_key_data,
3326 sizeof( smoke_test_key_data ),
3327 KNOWN_SUPPORTED_CIPHER_ALG,
3328 &operation, &status ) )
3329 goto exit;
3330 TEST_EQUAL( status, PSA_SUCCESS );
3331#endif
3332
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003333exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003334 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003335 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003336}
3337/* END_CASE */
3338
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003339/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003340void cipher_bad_order( )
3341{
Ronald Cron5425a212020-08-04 14:58:35 +02003342 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003343 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3344 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003346 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003347 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003348 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003349 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3350 0xaa, 0xaa, 0xaa, 0xaa };
3351 const uint8_t text[] = {
3352 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3353 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003354 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003355 size_t length = 0;
3356
3357 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003358 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3359 psa_set_key_algorithm( &attributes, alg );
3360 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003361 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3362 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003363
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003364 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003365 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3366 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003367 PSA_ERROR_BAD_STATE );
3368 PSA_ASSERT( psa_cipher_abort( &operation ) );
3369
3370 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003371 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3372 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003373 PSA_ERROR_BAD_STATE );
3374 PSA_ASSERT( psa_cipher_abort( &operation ) );
3375
Jaeden Ameroab439972019-02-15 14:12:05 +00003376 /* Generate an IV without calling setup beforehand. */
3377 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3378 buffer, sizeof( buffer ),
3379 &length ),
3380 PSA_ERROR_BAD_STATE );
3381 PSA_ASSERT( psa_cipher_abort( &operation ) );
3382
3383 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003384 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003385 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3386 buffer, sizeof( buffer ),
3387 &length ) );
3388 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3389 buffer, sizeof( buffer ),
3390 &length ),
3391 PSA_ERROR_BAD_STATE );
3392 PSA_ASSERT( psa_cipher_abort( &operation ) );
3393
3394 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003395 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003396 PSA_ASSERT( psa_cipher_set_iv( &operation,
3397 iv, sizeof( iv ) ) );
3398 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3399 buffer, sizeof( buffer ),
3400 &length ),
3401 PSA_ERROR_BAD_STATE );
3402 PSA_ASSERT( psa_cipher_abort( &operation ) );
3403
3404 /* Set an IV without calling setup beforehand. */
3405 TEST_EQUAL( psa_cipher_set_iv( &operation,
3406 iv, sizeof( iv ) ),
3407 PSA_ERROR_BAD_STATE );
3408 PSA_ASSERT( psa_cipher_abort( &operation ) );
3409
3410 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003411 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003412 PSA_ASSERT( psa_cipher_set_iv( &operation,
3413 iv, sizeof( iv ) ) );
3414 TEST_EQUAL( psa_cipher_set_iv( &operation,
3415 iv, sizeof( iv ) ),
3416 PSA_ERROR_BAD_STATE );
3417 PSA_ASSERT( psa_cipher_abort( &operation ) );
3418
3419 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003420 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003421 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3422 buffer, sizeof( buffer ),
3423 &length ) );
3424 TEST_EQUAL( psa_cipher_set_iv( &operation,
3425 iv, sizeof( iv ) ),
3426 PSA_ERROR_BAD_STATE );
3427 PSA_ASSERT( psa_cipher_abort( &operation ) );
3428
3429 /* Call update without calling setup beforehand. */
3430 TEST_EQUAL( psa_cipher_update( &operation,
3431 text, sizeof( text ),
3432 buffer, sizeof( buffer ),
3433 &length ),
3434 PSA_ERROR_BAD_STATE );
3435 PSA_ASSERT( psa_cipher_abort( &operation ) );
3436
3437 /* Call update without an IV where an IV is required. */
3438 TEST_EQUAL( psa_cipher_update( &operation,
3439 text, sizeof( text ),
3440 buffer, sizeof( buffer ),
3441 &length ),
3442 PSA_ERROR_BAD_STATE );
3443 PSA_ASSERT( psa_cipher_abort( &operation ) );
3444
3445 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003446 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003447 PSA_ASSERT( psa_cipher_set_iv( &operation,
3448 iv, sizeof( iv ) ) );
3449 PSA_ASSERT( psa_cipher_finish( &operation,
3450 buffer, sizeof( buffer ), &length ) );
3451 TEST_EQUAL( psa_cipher_update( &operation,
3452 text, sizeof( text ),
3453 buffer, sizeof( buffer ),
3454 &length ),
3455 PSA_ERROR_BAD_STATE );
3456 PSA_ASSERT( psa_cipher_abort( &operation ) );
3457
3458 /* Call finish without calling setup beforehand. */
3459 TEST_EQUAL( psa_cipher_finish( &operation,
3460 buffer, sizeof( buffer ), &length ),
3461 PSA_ERROR_BAD_STATE );
3462 PSA_ASSERT( psa_cipher_abort( &operation ) );
3463
3464 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003465 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003466 /* Not calling update means we are encrypting an empty buffer, which is OK
3467 * for cipher modes with padding. */
3468 TEST_EQUAL( psa_cipher_finish( &operation,
3469 buffer, sizeof( buffer ), &length ),
3470 PSA_ERROR_BAD_STATE );
3471 PSA_ASSERT( psa_cipher_abort( &operation ) );
3472
3473 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003474 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003475 PSA_ASSERT( psa_cipher_set_iv( &operation,
3476 iv, sizeof( iv ) ) );
3477 PSA_ASSERT( psa_cipher_finish( &operation,
3478 buffer, sizeof( buffer ), &length ) );
3479 TEST_EQUAL( psa_cipher_finish( &operation,
3480 buffer, sizeof( buffer ), &length ),
3481 PSA_ERROR_BAD_STATE );
3482 PSA_ASSERT( psa_cipher_abort( &operation ) );
3483
Ronald Cron5425a212020-08-04 14:58:35 +02003484 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003485
Jaeden Ameroab439972019-02-15 14:12:05 +00003486exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003487 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003488 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003489}
3490/* END_CASE */
3491
3492/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003493void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003494 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003495 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003496 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003497{
Ronald Cron5425a212020-08-04 14:58:35 +02003498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003499 psa_status_t status;
3500 psa_key_type_t key_type = key_type_arg;
3501 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003502 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003503 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003504 size_t output_buffer_size = 0;
3505 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003506 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003507 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003508 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003509
Gilles Peskine8817f612018-12-18 00:18:46 +01003510 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003511
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003512 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3513 psa_set_key_algorithm( &attributes, alg );
3514 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003515
Ronald Cron5425a212020-08-04 14:58:35 +02003516 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3517 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003518
Ronald Cron5425a212020-08-04 14:58:35 +02003519 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003520
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003521 if( iv->len > 0 )
3522 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003523 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003524 }
3525
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003526 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003527 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003528 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003529
Gilles Peskine8817f612018-12-18 00:18:46 +01003530 PSA_ASSERT( psa_cipher_update( &operation,
3531 input->x, input->len,
3532 output, output_buffer_size,
3533 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003534 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003535 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003536 output + total_output_length,
3537 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003538 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003539 total_output_length += function_output_length;
3540
Gilles Peskinefe11b722018-12-18 00:24:04 +01003541 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003542 if( expected_status == PSA_SUCCESS )
3543 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003544 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003545 ASSERT_COMPARE( expected_output->x, expected_output->len,
3546 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003547 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003548
Gilles Peskine50e586b2018-06-08 14:28:46 +02003549exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003550 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003551 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003552 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003553 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003554}
3555/* END_CASE */
3556
3557/* BEGIN_CASE */
3558void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003559 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003560 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003561 int first_part_size_arg,
3562 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003563 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003564{
Ronald Cron5425a212020-08-04 14:58:35 +02003565 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003566 psa_key_type_t key_type = key_type_arg;
3567 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003568 size_t first_part_size = first_part_size_arg;
3569 size_t output1_length = output1_length_arg;
3570 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003571 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003572 size_t output_buffer_size = 0;
3573 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003574 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003575 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003576 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003577
Gilles Peskine8817f612018-12-18 00:18:46 +01003578 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003579
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003580 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3581 psa_set_key_algorithm( &attributes, alg );
3582 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003583
Ronald Cron5425a212020-08-04 14:58:35 +02003584 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3585 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003586
Ronald Cron5425a212020-08-04 14:58:35 +02003587 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003588
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003589 if( iv->len > 0 )
3590 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003591 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003592 }
3593
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003594 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003595 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003596 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003597
Gilles Peskinee0866522019-02-19 19:44:00 +01003598 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003599 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3600 output, output_buffer_size,
3601 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003602 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003603 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003604 PSA_ASSERT( psa_cipher_update( &operation,
3605 input->x + first_part_size,
3606 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003607 output + total_output_length,
3608 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003609 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003610 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003611 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003612 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003613 output + total_output_length,
3614 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003615 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003616 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003617 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003618
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003619 ASSERT_COMPARE( expected_output->x, expected_output->len,
3620 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003621
3622exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003623 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003624 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003625 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003626 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003627}
3628/* END_CASE */
3629
3630/* BEGIN_CASE */
3631void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003632 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003633 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003634 int first_part_size_arg,
3635 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003636 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003637{
Ronald Cron5425a212020-08-04 14:58:35 +02003638 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003639 psa_key_type_t key_type = key_type_arg;
3640 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003641 size_t first_part_size = first_part_size_arg;
3642 size_t output1_length = output1_length_arg;
3643 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003644 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003645 size_t output_buffer_size = 0;
3646 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003647 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003648 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003650
Gilles Peskine8817f612018-12-18 00:18:46 +01003651 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003652
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003653 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3654 psa_set_key_algorithm( &attributes, alg );
3655 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003656
Ronald Cron5425a212020-08-04 14:58:35 +02003657 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3658 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003659
Ronald Cron5425a212020-08-04 14:58:35 +02003660 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003661
Steven Cooreman177deba2020-09-07 17:14:14 +02003662 if( iv->len > 0 )
3663 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003664 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003665 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003666
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003667 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003668 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003669 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003670
Gilles Peskinee0866522019-02-19 19:44:00 +01003671 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003672 PSA_ASSERT( psa_cipher_update( &operation,
3673 input->x, first_part_size,
3674 output, output_buffer_size,
3675 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003676 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003677 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003678 PSA_ASSERT( psa_cipher_update( &operation,
3679 input->x + first_part_size,
3680 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003681 output + total_output_length,
3682 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003683 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003684 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003685 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003686 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003687 output + total_output_length,
3688 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003689 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003690 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003691 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003692
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003693 ASSERT_COMPARE( expected_output->x, expected_output->len,
3694 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003695
3696exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003697 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003698 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003699 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003700 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003701}
3702/* END_CASE */
3703
Gilles Peskine50e586b2018-06-08 14:28:46 +02003704/* BEGIN_CASE */
3705void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003706 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003707 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003708 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003709{
Ronald Cron5425a212020-08-04 14:58:35 +02003710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003711 psa_status_t status;
3712 psa_key_type_t key_type = key_type_arg;
3713 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003714 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003715 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003716 size_t output_buffer_size = 0;
3717 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003718 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003719 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003721
Gilles Peskine8817f612018-12-18 00:18:46 +01003722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003723
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003724 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3725 psa_set_key_algorithm( &attributes, alg );
3726 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003727
Ronald Cron5425a212020-08-04 14:58:35 +02003728 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3729 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003730
Ronald Cron5425a212020-08-04 14:58:35 +02003731 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003732
Steven Cooreman177deba2020-09-07 17:14:14 +02003733 if( iv->len > 0 )
3734 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003735 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003736 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003737
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003738 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003739 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003740 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003741
Gilles Peskine8817f612018-12-18 00:18:46 +01003742 PSA_ASSERT( psa_cipher_update( &operation,
3743 input->x, input->len,
3744 output, output_buffer_size,
3745 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003746 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003747 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003748 output + total_output_length,
3749 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003750 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003751 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003752 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003753
3754 if( expected_status == PSA_SUCCESS )
3755 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003756 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003757 ASSERT_COMPARE( expected_output->x, expected_output->len,
3758 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003759 }
3760
Gilles Peskine50e586b2018-06-08 14:28:46 +02003761exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003762 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003763 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003764 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003765 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003766}
3767/* END_CASE */
3768
Gilles Peskine50e586b2018-06-08 14:28:46 +02003769/* BEGIN_CASE */
3770void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003771 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003772 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003773{
Ronald Cron5425a212020-08-04 14:58:35 +02003774 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003775 psa_key_type_t key_type = key_type_arg;
3776 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003777 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003778 size_t iv_size = 16;
3779 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003780 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003781 size_t output1_size = 0;
3782 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003783 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003784 size_t output2_size = 0;
3785 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003786 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003787 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3788 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003790
Gilles Peskine8817f612018-12-18 00:18:46 +01003791 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003792
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003793 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3794 psa_set_key_algorithm( &attributes, alg );
3795 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003796
Ronald Cron5425a212020-08-04 14:58:35 +02003797 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3798 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003799
Ronald Cron5425a212020-08-04 14:58:35 +02003800 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3801 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003802
Steven Cooreman177deba2020-09-07 17:14:14 +02003803 if( alg != PSA_ALG_ECB_NO_PADDING )
3804 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003805 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3806 iv, iv_size,
3807 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003808 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003809 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003810 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003811 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003812
Gilles Peskine8817f612018-12-18 00:18:46 +01003813 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3814 output1, output1_size,
3815 &output1_length ) );
3816 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003817 output1 + output1_length,
3818 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003819 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003820
Gilles Peskine048b7f02018-06-08 14:20:49 +02003821 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003822
Gilles Peskine8817f612018-12-18 00:18:46 +01003823 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003824
3825 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003826 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003827
Steven Cooreman177deba2020-09-07 17:14:14 +02003828 if( iv_length > 0 )
3829 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003830 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3831 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003832 }
3833
Gilles Peskine8817f612018-12-18 00:18:46 +01003834 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3835 output2, output2_size,
3836 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003837 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003838 PSA_ASSERT( psa_cipher_finish( &operation2,
3839 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003840 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003841 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003842
Gilles Peskine048b7f02018-06-08 14:20:49 +02003843 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003844
Gilles Peskine8817f612018-12-18 00:18:46 +01003845 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003846
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003847 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003848
3849exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003850 psa_cipher_abort( &operation1 );
3851 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003852 mbedtls_free( output1 );
3853 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003854 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003855 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003856}
3857/* END_CASE */
3858
3859/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003860void cipher_verify_output_multipart( int alg_arg,
3861 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003862 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003863 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003864 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003865{
Ronald Cron5425a212020-08-04 14:58:35 +02003866 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003867 psa_key_type_t key_type = key_type_arg;
3868 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003869 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003870 unsigned char iv[16] = {0};
3871 size_t iv_size = 16;
3872 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003873 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003874 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003875 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003876 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003877 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003878 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003879 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003880 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3881 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003883
Gilles Peskine8817f612018-12-18 00:18:46 +01003884 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003885
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003886 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3887 psa_set_key_algorithm( &attributes, alg );
3888 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003889
Ronald Cron5425a212020-08-04 14:58:35 +02003890 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3891 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003892
Ronald Cron5425a212020-08-04 14:58:35 +02003893 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3894 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003895
Steven Cooreman177deba2020-09-07 17:14:14 +02003896 if( alg != PSA_ALG_ECB_NO_PADDING )
3897 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003898 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3899 iv, iv_size,
3900 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003901 }
3902
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003903 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003904 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003905 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003906
Gilles Peskinee0866522019-02-19 19:44:00 +01003907 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003908
Gilles Peskine8817f612018-12-18 00:18:46 +01003909 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3910 output1, output1_buffer_size,
3911 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003912 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003913
Gilles Peskine8817f612018-12-18 00:18:46 +01003914 PSA_ASSERT( psa_cipher_update( &operation1,
3915 input->x + first_part_size,
3916 input->len - first_part_size,
3917 output1, output1_buffer_size,
3918 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003919 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003920
Gilles Peskine8817f612018-12-18 00:18:46 +01003921 PSA_ASSERT( psa_cipher_finish( &operation1,
3922 output1 + output1_length,
3923 output1_buffer_size - output1_length,
3924 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003925 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003926
Gilles Peskine8817f612018-12-18 00:18:46 +01003927 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003928
Gilles Peskine048b7f02018-06-08 14:20:49 +02003929 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003930 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003931
Steven Cooreman177deba2020-09-07 17:14:14 +02003932 if( iv_length > 0 )
3933 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003934 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3935 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003936 }
Moran Pekerded84402018-06-06 16:36:50 +03003937
Gilles Peskine8817f612018-12-18 00:18:46 +01003938 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3939 output2, output2_buffer_size,
3940 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003941 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003942
Gilles Peskine8817f612018-12-18 00:18:46 +01003943 PSA_ASSERT( psa_cipher_update( &operation2,
3944 output1 + first_part_size,
3945 output1_length - first_part_size,
3946 output2, output2_buffer_size,
3947 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003948 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003949
Gilles Peskine8817f612018-12-18 00:18:46 +01003950 PSA_ASSERT( psa_cipher_finish( &operation2,
3951 output2 + output2_length,
3952 output2_buffer_size - output2_length,
3953 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003954 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003955
Gilles Peskine8817f612018-12-18 00:18:46 +01003956 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003957
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003958 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003959
3960exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003961 psa_cipher_abort( &operation1 );
3962 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003963 mbedtls_free( output1 );
3964 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003965 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003966 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003967}
3968/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003969
Gilles Peskine20035e32018-02-03 22:44:14 +01003970/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003971void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003972 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003973 data_t *nonce,
3974 data_t *additional_data,
3975 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003976 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003977{
Ronald Cron5425a212020-08-04 14:58:35 +02003978 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003979 psa_key_type_t key_type = key_type_arg;
3980 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003981 unsigned char *output_data = NULL;
3982 size_t output_size = 0;
3983 size_t output_length = 0;
3984 unsigned char *output_data2 = NULL;
3985 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003986 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003987 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003989
Gilles Peskine4abf7412018-06-18 16:35:34 +02003990 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003991 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3992 * should be exact. */
3993 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3994 TEST_EQUAL( output_size,
3995 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003996 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003997
Gilles Peskine8817f612018-12-18 00:18:46 +01003998 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003999
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004000 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4001 psa_set_key_algorithm( &attributes, alg );
4002 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004003
Gilles Peskine049c7532019-05-15 20:22:09 +02004004 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004005 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004006
Ronald Cron5425a212020-08-04 14:58:35 +02004007 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004008 nonce->x, nonce->len,
4009 additional_data->x,
4010 additional_data->len,
4011 input_data->x, input_data->len,
4012 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004013 &output_length ),
4014 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004015
4016 if( PSA_SUCCESS == expected_result )
4017 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004018 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004019
Gilles Peskine003a4a92019-05-14 16:09:40 +02004020 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4021 * should be exact. */
4022 TEST_EQUAL( input_data->len,
4023 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
4024
Ronald Cron5425a212020-08-04 14:58:35 +02004025 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004026 nonce->x, nonce->len,
4027 additional_data->x,
4028 additional_data->len,
4029 output_data, output_length,
4030 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004031 &output_length2 ),
4032 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004033
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004034 ASSERT_COMPARE( input_data->x, input_data->len,
4035 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004036 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004037
Gilles Peskinea1cac842018-06-11 19:33:02 +02004038exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004039 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004040 mbedtls_free( output_data );
4041 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004042 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004043}
4044/* END_CASE */
4045
4046/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004047void aead_encrypt( int key_type_arg, data_t *key_data,
4048 int alg_arg,
4049 data_t *nonce,
4050 data_t *additional_data,
4051 data_t *input_data,
4052 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004053{
Ronald Cron5425a212020-08-04 14:58:35 +02004054 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004055 psa_key_type_t key_type = key_type_arg;
4056 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004057 unsigned char *output_data = NULL;
4058 size_t output_size = 0;
4059 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004060 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004061 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004062
Gilles Peskine4abf7412018-06-18 16:35:34 +02004063 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004064 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4065 * should be exact. */
4066 TEST_EQUAL( output_size,
4067 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004068 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004069
Gilles Peskine8817f612018-12-18 00:18:46 +01004070 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004071
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004072 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4073 psa_set_key_algorithm( &attributes, alg );
4074 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004075
Gilles Peskine049c7532019-05-15 20:22:09 +02004076 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004077 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004078
Ronald Cron5425a212020-08-04 14:58:35 +02004079 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004080 nonce->x, nonce->len,
4081 additional_data->x, additional_data->len,
4082 input_data->x, input_data->len,
4083 output_data, output_size,
4084 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004085
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004086 ASSERT_COMPARE( expected_result->x, expected_result->len,
4087 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004088
Gilles Peskinea1cac842018-06-11 19:33:02 +02004089exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004090 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004091 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004092 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004093}
4094/* END_CASE */
4095
4096/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004097void aead_decrypt( int key_type_arg, data_t *key_data,
4098 int alg_arg,
4099 data_t *nonce,
4100 data_t *additional_data,
4101 data_t *input_data,
4102 data_t *expected_data,
4103 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004104{
Ronald Cron5425a212020-08-04 14:58:35 +02004105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004106 psa_key_type_t key_type = key_type_arg;
4107 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004108 unsigned char *output_data = NULL;
4109 size_t output_size = 0;
4110 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004111 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004113 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004114
Gilles Peskine003a4a92019-05-14 16:09:40 +02004115 output_size = input_data->len - tag_length;
4116 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4117 * should be exact. */
4118 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4119 TEST_EQUAL( output_size,
4120 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004121 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004122
Gilles Peskine8817f612018-12-18 00:18:46 +01004123 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004124
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004125 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4126 psa_set_key_algorithm( &attributes, alg );
4127 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004128
Gilles Peskine049c7532019-05-15 20:22:09 +02004129 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004130 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004131
Ronald Cron5425a212020-08-04 14:58:35 +02004132 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004133 nonce->x, nonce->len,
4134 additional_data->x,
4135 additional_data->len,
4136 input_data->x, input_data->len,
4137 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004138 &output_length ),
4139 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004140
Gilles Peskine2d277862018-06-18 15:41:12 +02004141 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004142 ASSERT_COMPARE( expected_data->x, expected_data->len,
4143 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004144
Gilles Peskinea1cac842018-06-11 19:33:02 +02004145exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004146 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004147 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004148 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004149}
4150/* END_CASE */
4151
4152/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004153void signature_size( int type_arg,
4154 int bits,
4155 int alg_arg,
4156 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004157{
4158 psa_key_type_t type = type_arg;
4159 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004160 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004161
Gilles Peskinefe11b722018-12-18 00:24:04 +01004162 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004163#if defined(MBEDTLS_TEST_DEPRECATED)
4164 TEST_EQUAL( actual_size,
4165 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4166#endif /* MBEDTLS_TEST_DEPRECATED */
4167
Gilles Peskinee59236f2018-01-27 23:32:46 +01004168exit:
4169 ;
4170}
4171/* END_CASE */
4172
4173/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004174void sign_deterministic( int key_type_arg, data_t *key_data,
4175 int alg_arg, data_t *input_data,
4176 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004177{
Ronald Cron5425a212020-08-04 14:58:35 +02004178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004179 psa_key_type_t key_type = key_type_arg;
4180 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004181 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004182 unsigned char *signature = NULL;
4183 size_t signature_size;
4184 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004186
Gilles Peskine8817f612018-12-18 00:18:46 +01004187 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004188
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004189 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004190 psa_set_key_algorithm( &attributes, alg );
4191 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004192
Gilles Peskine049c7532019-05-15 20:22:09 +02004193 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004194 &key ) );
4195 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004196 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004197
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004198 /* Allocate a buffer which has the size advertized by the
4199 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004200 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004201 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004202 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004203 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004204 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004205
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004206 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004207 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004208 input_data->x, input_data->len,
4209 signature, signature_size,
4210 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004211 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004212 ASSERT_COMPARE( output_data->x, output_data->len,
4213 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004214
Gilles Peskine0627f982019-11-26 19:12:16 +01004215#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004216 memset( signature, 0, signature_size );
4217 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004218 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004219 input_data->x, input_data->len,
4220 signature, signature_size,
4221 &signature_length ) );
4222 ASSERT_COMPARE( output_data->x, output_data->len,
4223 signature, signature_length );
4224#endif /* MBEDTLS_TEST_DEPRECATED */
4225
Gilles Peskine20035e32018-02-03 22:44:14 +01004226exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004227 /*
4228 * Key attributes may have been returned by psa_get_key_attributes()
4229 * thus reset them as required.
4230 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004231 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004232
Ronald Cron5425a212020-08-04 14:58:35 +02004233 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004234 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004235 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004236}
4237/* END_CASE */
4238
4239/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004240void sign_fail( int key_type_arg, data_t *key_data,
4241 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004242 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004243{
Ronald Cron5425a212020-08-04 14:58:35 +02004244 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004245 psa_key_type_t key_type = key_type_arg;
4246 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004247 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004248 psa_status_t actual_status;
4249 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004250 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004251 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004253
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004254 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004255
Gilles Peskine8817f612018-12-18 00:18:46 +01004256 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004257
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004258 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004259 psa_set_key_algorithm( &attributes, alg );
4260 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004261
Gilles Peskine049c7532019-05-15 20:22:09 +02004262 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004263 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004264
Ronald Cron5425a212020-08-04 14:58:35 +02004265 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004266 input_data->x, input_data->len,
4267 signature, signature_size,
4268 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004269 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004270 /* The value of *signature_length is unspecified on error, but
4271 * whatever it is, it should be less than signature_size, so that
4272 * if the caller tries to read *signature_length bytes without
4273 * checking the error code then they don't overflow a buffer. */
4274 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004275
Gilles Peskine895242b2019-11-29 12:15:40 +01004276#if defined(MBEDTLS_TEST_DEPRECATED)
4277 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004278 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004279 input_data->x, input_data->len,
4280 signature, signature_size,
4281 &signature_length ),
4282 expected_status );
4283 TEST_ASSERT( signature_length <= signature_size );
4284#endif /* MBEDTLS_TEST_DEPRECATED */
4285
Gilles Peskine20035e32018-02-03 22:44:14 +01004286exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004287 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004288 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004289 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004290 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004291}
4292/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004293
4294/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004295void sign_verify( int key_type_arg, data_t *key_data,
4296 int alg_arg, data_t *input_data )
4297{
Ronald Cron5425a212020-08-04 14:58:35 +02004298 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004299 psa_key_type_t key_type = key_type_arg;
4300 psa_algorithm_t alg = alg_arg;
4301 size_t key_bits;
4302 unsigned char *signature = NULL;
4303 size_t signature_size;
4304 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004305 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004306
Gilles Peskine8817f612018-12-18 00:18:46 +01004307 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004308
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004309 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004310 psa_set_key_algorithm( &attributes, alg );
4311 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004312
Gilles Peskine049c7532019-05-15 20:22:09 +02004313 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004314 &key ) );
4315 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004316 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004317
4318 /* Allocate a buffer which has the size advertized by the
4319 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004320 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004321 key_bits, alg );
4322 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004323 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004324 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004325
4326 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004327 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004328 input_data->x, input_data->len,
4329 signature, signature_size,
4330 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004331 /* Check that the signature length looks sensible. */
4332 TEST_ASSERT( signature_length <= signature_size );
4333 TEST_ASSERT( signature_length > 0 );
4334
4335 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004336 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004337 input_data->x, input_data->len,
4338 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004339
4340 if( input_data->len != 0 )
4341 {
4342 /* Flip a bit in the input and verify that the signature is now
4343 * detected as invalid. Flip a bit at the beginning, not at the end,
4344 * because ECDSA may ignore the last few bits of the input. */
4345 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004346 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004347 input_data->x, input_data->len,
4348 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004349 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004350 }
4351
4352exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004353 /*
4354 * Key attributes may have been returned by psa_get_key_attributes()
4355 * thus reset them as required.
4356 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004357 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004358
Ronald Cron5425a212020-08-04 14:58:35 +02004359 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004360 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004361 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004362}
4363/* END_CASE */
4364
4365/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004366void asymmetric_verify( int key_type_arg, data_t *key_data,
4367 int alg_arg, data_t *hash_data,
4368 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004369{
Ronald Cron5425a212020-08-04 14:58:35 +02004370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004371 psa_key_type_t key_type = key_type_arg;
4372 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004374
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004375 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004376
Gilles Peskine8817f612018-12-18 00:18:46 +01004377 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004378
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004379 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004380 psa_set_key_algorithm( &attributes, alg );
4381 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004382
Gilles Peskine049c7532019-05-15 20:22:09 +02004383 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004384 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004385
Ronald Cron5425a212020-08-04 14:58:35 +02004386 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004387 hash_data->x, hash_data->len,
4388 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004389
4390#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004391 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004392 hash_data->x, hash_data->len,
4393 signature_data->x,
4394 signature_data->len ) );
4395
4396#endif /* MBEDTLS_TEST_DEPRECATED */
4397
itayzafrir5c753392018-05-08 11:18:38 +03004398exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004399 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004400 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004401 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004402}
4403/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004404
4405/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004406void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4407 int alg_arg, data_t *hash_data,
4408 data_t *signature_data,
4409 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004410{
Ronald Cron5425a212020-08-04 14:58:35 +02004411 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004412 psa_key_type_t key_type = key_type_arg;
4413 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004414 psa_status_t actual_status;
4415 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004417
Gilles Peskine8817f612018-12-18 00:18:46 +01004418 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004419
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004420 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004421 psa_set_key_algorithm( &attributes, alg );
4422 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004423
Gilles Peskine049c7532019-05-15 20:22:09 +02004424 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004425 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004426
Ronald Cron5425a212020-08-04 14:58:35 +02004427 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004428 hash_data->x, hash_data->len,
4429 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004430 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004431
Gilles Peskine895242b2019-11-29 12:15:40 +01004432#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004433 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004434 hash_data->x, hash_data->len,
4435 signature_data->x, signature_data->len ),
4436 expected_status );
4437#endif /* MBEDTLS_TEST_DEPRECATED */
4438
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004439exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004440 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004441 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004442 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004443}
4444/* END_CASE */
4445
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004446/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004447void asymmetric_encrypt( int key_type_arg,
4448 data_t *key_data,
4449 int alg_arg,
4450 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004451 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004452 int expected_output_length_arg,
4453 int expected_status_arg )
4454{
Ronald Cron5425a212020-08-04 14:58:35 +02004455 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004456 psa_key_type_t key_type = key_type_arg;
4457 psa_algorithm_t alg = alg_arg;
4458 size_t expected_output_length = expected_output_length_arg;
4459 size_t key_bits;
4460 unsigned char *output = NULL;
4461 size_t output_size;
4462 size_t output_length = ~0;
4463 psa_status_t actual_status;
4464 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004466
Gilles Peskine8817f612018-12-18 00:18:46 +01004467 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004468
Gilles Peskine656896e2018-06-29 19:12:28 +02004469 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004470 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4471 psa_set_key_algorithm( &attributes, alg );
4472 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004473 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004474 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004475
4476 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004477 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004478 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004479 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004480 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004481
4482 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004483 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004484 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004485 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004486 output, output_size,
4487 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004488 TEST_EQUAL( actual_status, expected_status );
4489 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004490
Gilles Peskine68428122018-06-30 18:42:41 +02004491 /* If the label is empty, the test framework puts a non-null pointer
4492 * in label->x. Test that a null pointer works as well. */
4493 if( label->len == 0 )
4494 {
4495 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004496 if( output_size != 0 )
4497 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004498 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004499 input_data->x, input_data->len,
4500 NULL, label->len,
4501 output, output_size,
4502 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004503 TEST_EQUAL( actual_status, expected_status );
4504 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004505 }
4506
Gilles Peskine656896e2018-06-29 19:12:28 +02004507exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004508 /*
4509 * Key attributes may have been returned by psa_get_key_attributes()
4510 * thus reset them as required.
4511 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004512 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004513
Ronald Cron5425a212020-08-04 14:58:35 +02004514 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004515 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004516 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004517}
4518/* END_CASE */
4519
4520/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004521void asymmetric_encrypt_decrypt( int key_type_arg,
4522 data_t *key_data,
4523 int alg_arg,
4524 data_t *input_data,
4525 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004526{
Ronald Cron5425a212020-08-04 14:58:35 +02004527 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004528 psa_key_type_t key_type = key_type_arg;
4529 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004530 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004531 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004532 size_t output_size;
4533 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004534 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004535 size_t output2_size;
4536 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004538
Gilles Peskine8817f612018-12-18 00:18:46 +01004539 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004540
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004541 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4542 psa_set_key_algorithm( &attributes, alg );
4543 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004544
Gilles Peskine049c7532019-05-15 20:22:09 +02004545 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004546 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004547
4548 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004549 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004550 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004551 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004552 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004553 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004554 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004555
Gilles Peskineeebd7382018-06-08 18:11:54 +02004556 /* We test encryption by checking that encrypt-then-decrypt gives back
4557 * the original plaintext because of the non-optional random
4558 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004559 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004560 input_data->x, input_data->len,
4561 label->x, label->len,
4562 output, output_size,
4563 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004564 /* We don't know what ciphertext length to expect, but check that
4565 * it looks sensible. */
4566 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004567
Ronald Cron5425a212020-08-04 14:58:35 +02004568 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004569 output, output_length,
4570 label->x, label->len,
4571 output2, output2_size,
4572 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004573 ASSERT_COMPARE( input_data->x, input_data->len,
4574 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004575
4576exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004577 /*
4578 * Key attributes may have been returned by psa_get_key_attributes()
4579 * thus reset them as required.
4580 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004581 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004582
Ronald Cron5425a212020-08-04 14:58:35 +02004583 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004584 mbedtls_free( output );
4585 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004586 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004587}
4588/* END_CASE */
4589
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004590/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004591void asymmetric_decrypt( int key_type_arg,
4592 data_t *key_data,
4593 int alg_arg,
4594 data_t *input_data,
4595 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004596 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004597{
Ronald Cron5425a212020-08-04 14:58:35 +02004598 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004599 psa_key_type_t key_type = key_type_arg;
4600 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004601 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004602 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004603 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004605
Jaeden Amero412654a2019-02-06 12:57:46 +00004606 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004607 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004608
Gilles Peskine8817f612018-12-18 00:18:46 +01004609 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004610
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004611 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4612 psa_set_key_algorithm( &attributes, alg );
4613 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004614
Gilles Peskine049c7532019-05-15 20:22:09 +02004615 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004616 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004617
Ronald Cron5425a212020-08-04 14:58:35 +02004618 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004619 input_data->x, input_data->len,
4620 label->x, label->len,
4621 output,
4622 output_size,
4623 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004624 ASSERT_COMPARE( expected_data->x, expected_data->len,
4625 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004626
Gilles Peskine68428122018-06-30 18:42:41 +02004627 /* If the label is empty, the test framework puts a non-null pointer
4628 * in label->x. Test that a null pointer works as well. */
4629 if( label->len == 0 )
4630 {
4631 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004632 if( output_size != 0 )
4633 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004634 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004635 input_data->x, input_data->len,
4636 NULL, label->len,
4637 output,
4638 output_size,
4639 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004640 ASSERT_COMPARE( expected_data->x, expected_data->len,
4641 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004642 }
4643
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004644exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004645 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004646 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004647 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004648 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004649}
4650/* END_CASE */
4651
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004652/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004653void asymmetric_decrypt_fail( int key_type_arg,
4654 data_t *key_data,
4655 int alg_arg,
4656 data_t *input_data,
4657 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004658 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004659 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004660{
Ronald Cron5425a212020-08-04 14:58:35 +02004661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004662 psa_key_type_t key_type = key_type_arg;
4663 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004664 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004665 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004666 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004667 psa_status_t actual_status;
4668 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004670
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004671 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004672
Gilles Peskine8817f612018-12-18 00:18:46 +01004673 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004674
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004675 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4676 psa_set_key_algorithm( &attributes, alg );
4677 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004678
Gilles Peskine049c7532019-05-15 20:22:09 +02004679 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004680 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004681
Ronald Cron5425a212020-08-04 14:58:35 +02004682 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004683 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004684 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004685 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004686 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004687 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004688 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004689
Gilles Peskine68428122018-06-30 18:42:41 +02004690 /* If the label is empty, the test framework puts a non-null pointer
4691 * in label->x. Test that a null pointer works as well. */
4692 if( label->len == 0 )
4693 {
4694 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004695 if( output_size != 0 )
4696 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004697 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004698 input_data->x, input_data->len,
4699 NULL, label->len,
4700 output, output_size,
4701 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004702 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004703 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004704 }
4705
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004706exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004707 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004708 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004709 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004710 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004711}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004712/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004713
4714/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004715void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004716{
4717 /* Test each valid way of initializing the object, except for `= {0}`, as
4718 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4719 * though it's OK by the C standard. We could test for this, but we'd need
4720 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004721 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004722 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4723 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4724 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004725
4726 memset( &zero, 0, sizeof( zero ) );
4727
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004729 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004730 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004731 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004732 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004733 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004734 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004735
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004737 PSA_ASSERT( psa_key_derivation_abort(&func) );
4738 PSA_ASSERT( psa_key_derivation_abort(&init) );
4739 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004740}
4741/* END_CASE */
4742
Janos Follath16de4a42019-06-13 16:32:24 +01004743/* BEGIN_CASE */
4744void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004745{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004746 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004747 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004748 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004749
Gilles Peskine8817f612018-12-18 00:18:46 +01004750 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004751
Janos Follath16de4a42019-06-13 16:32:24 +01004752 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004753 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004754
4755exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004757 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004758}
4759/* END_CASE */
4760
Janos Follathaf3c2a02019-06-12 12:34:34 +01004761/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004762void derive_set_capacity( int alg_arg, int capacity_arg,
4763 int expected_status_arg )
4764{
4765 psa_algorithm_t alg = alg_arg;
4766 size_t capacity = capacity_arg;
4767 psa_status_t expected_status = expected_status_arg;
4768 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4769
4770 PSA_ASSERT( psa_crypto_init( ) );
4771
4772 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4773
4774 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4775 expected_status );
4776
4777exit:
4778 psa_key_derivation_abort( &operation );
4779 PSA_DONE( );
4780}
4781/* END_CASE */
4782
4783/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004784void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004785 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004786 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004787 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004788 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004789 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004790 int expected_status_arg3,
4791 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004792{
4793 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004794 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4795 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004796 psa_status_t expected_statuses[] = {expected_status_arg1,
4797 expected_status_arg2,
4798 expected_status_arg3};
4799 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004800 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4801 MBEDTLS_SVC_KEY_ID_INIT,
4802 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004803 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4805 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004806 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004807 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004808 psa_status_t expected_output_status = expected_output_status_arg;
4809 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004810
4811 PSA_ASSERT( psa_crypto_init( ) );
4812
4813 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4814 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004815
4816 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4817
4818 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4819 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004820 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004821 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004822 psa_set_key_type( &attributes, key_types[i] );
4823 PSA_ASSERT( psa_import_key( &attributes,
4824 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004825 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004826 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4827 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4828 {
4829 // When taking a private key as secret input, use key agreement
4830 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004831 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004832 expected_statuses[i] );
4833 }
4834 else
4835 {
4836 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004837 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004838 expected_statuses[i] );
4839 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004840 }
4841 else
4842 {
4843 TEST_EQUAL( psa_key_derivation_input_bytes(
4844 &operation, steps[i],
4845 inputs[i]->x, inputs[i]->len ),
4846 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004847 }
4848 }
4849
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004850 if( output_key_type != PSA_KEY_TYPE_NONE )
4851 {
4852 psa_reset_key_attributes( &attributes );
4853 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4854 psa_set_key_bits( &attributes, 8 );
4855 actual_output_status =
4856 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004857 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004858 }
4859 else
4860 {
4861 uint8_t buffer[1];
4862 actual_output_status =
4863 psa_key_derivation_output_bytes( &operation,
4864 buffer, sizeof( buffer ) );
4865 }
4866 TEST_EQUAL( actual_output_status, expected_output_status );
4867
Janos Follathaf3c2a02019-06-12 12:34:34 +01004868exit:
4869 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004870 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4871 psa_destroy_key( keys[i] );
4872 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004873 PSA_DONE( );
4874}
4875/* END_CASE */
4876
Janos Follathd958bb72019-07-03 15:02:16 +01004877/* BEGIN_CASE */
4878void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004879{
Janos Follathd958bb72019-07-03 15:02:16 +01004880 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004882 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004883 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004884 unsigned char input1[] = "Input 1";
4885 size_t input1_length = sizeof( input1 );
4886 unsigned char input2[] = "Input 2";
4887 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004888 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004889 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004890 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4891 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4892 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004894
Gilles Peskine8817f612018-12-18 00:18:46 +01004895 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004896
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004897 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4898 psa_set_key_algorithm( &attributes, alg );
4899 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004900
Gilles Peskine73676cb2019-05-15 20:15:10 +02004901 PSA_ASSERT( psa_import_key( &attributes,
4902 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004903 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004904
4905 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004906 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004907 input1, input1_length,
4908 input2, input2_length,
4909 capacity ) )
4910 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004911
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004913 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004914 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004915
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004917
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004919 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004920
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004921exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004922 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004923 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004924 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004925}
4926/* END_CASE */
4927
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004928/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004929void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004930{
4931 uint8_t output_buffer[16];
4932 size_t buffer_size = 16;
4933 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004934 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004935
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004936 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4937 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004938 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004939
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004940 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004941 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004942
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004943 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004944
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004945 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4946 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004947 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004948
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004950 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004951
4952exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004954}
4955/* END_CASE */
4956
4957/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004958void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004959 int step1_arg, data_t *input1,
4960 int step2_arg, data_t *input2,
4961 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004962 int requested_capacity_arg,
4963 data_t *expected_output1,
4964 data_t *expected_output2 )
4965{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004966 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004967 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4968 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004969 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4970 MBEDTLS_SVC_KEY_ID_INIT,
4971 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004972 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004974 uint8_t *expected_outputs[2] =
4975 {expected_output1->x, expected_output2->x};
4976 size_t output_sizes[2] =
4977 {expected_output1->len, expected_output2->len};
4978 size_t output_buffer_size = 0;
4979 uint8_t *output_buffer = NULL;
4980 size_t expected_capacity;
4981 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004983 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004984 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004985
4986 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4987 {
4988 if( output_sizes[i] > output_buffer_size )
4989 output_buffer_size = output_sizes[i];
4990 if( output_sizes[i] == 0 )
4991 expected_outputs[i] = NULL;
4992 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004993 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004994 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004995
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4997 psa_set_key_algorithm( &attributes, alg );
4998 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004999
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005000 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005001 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5002 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5003 requested_capacity ) );
5004 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005005 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005006 switch( steps[i] )
5007 {
5008 case 0:
5009 break;
5010 case PSA_KEY_DERIVATION_INPUT_SECRET:
5011 PSA_ASSERT( psa_import_key( &attributes,
5012 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005013 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005014 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005015 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005016 break;
5017 default:
5018 PSA_ASSERT( psa_key_derivation_input_bytes(
5019 &operation, steps[i],
5020 inputs[i]->x, inputs[i]->len ) );
5021 break;
5022 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005023 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005024
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005025 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005026 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005027 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005028 expected_capacity = requested_capacity;
5029
5030 /* Expansion phase. */
5031 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5032 {
5033 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005034 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005035 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005036 if( expected_capacity == 0 && output_sizes[i] == 0 )
5037 {
5038 /* Reading 0 bytes when 0 bytes are available can go either way. */
5039 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005040 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005041 continue;
5042 }
5043 else if( expected_capacity == 0 ||
5044 output_sizes[i] > expected_capacity )
5045 {
5046 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005047 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005048 expected_capacity = 0;
5049 continue;
5050 }
5051 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005052 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005053 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005054 ASSERT_COMPARE( output_buffer, output_sizes[i],
5055 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005056 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005057 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005058 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005059 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005060 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005061 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005063
5064exit:
5065 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005066 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005067 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5068 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005069 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005070}
5071/* END_CASE */
5072
5073/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005074void derive_full( int alg_arg,
5075 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005076 data_t *input1,
5077 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005078 int requested_capacity_arg )
5079{
Ronald Cron5425a212020-08-04 14:58:35 +02005080 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005081 psa_algorithm_t alg = alg_arg;
5082 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005083 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005084 unsigned char output_buffer[16];
5085 size_t expected_capacity = requested_capacity;
5086 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005088
Gilles Peskine8817f612018-12-18 00:18:46 +01005089 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005090
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005091 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5092 psa_set_key_algorithm( &attributes, alg );
5093 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005094
Gilles Peskine049c7532019-05-15 20:22:09 +02005095 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005096 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005097
Ronald Cron5425a212020-08-04 14:58:35 +02005098 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005099 input1->x, input1->len,
5100 input2->x, input2->len,
5101 requested_capacity ) )
5102 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005103
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005104 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005105 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005106 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005107
5108 /* Expansion phase. */
5109 while( current_capacity > 0 )
5110 {
5111 size_t read_size = sizeof( output_buffer );
5112 if( read_size > current_capacity )
5113 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005114 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005115 output_buffer,
5116 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005117 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005118 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005119 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005120 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005121 }
5122
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005123 /* Check that the operation refuses to go over capacity. */
5124 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005125 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005126
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005128
5129exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005130 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005131 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005132 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005133}
5134/* END_CASE */
5135
Janos Follathe60c9052019-07-03 13:51:30 +01005136/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005137void derive_key_exercise( int alg_arg,
5138 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005139 data_t *input1,
5140 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005141 int derived_type_arg,
5142 int derived_bits_arg,
5143 int derived_usage_arg,
5144 int derived_alg_arg )
5145{
Ronald Cron5425a212020-08-04 14:58:35 +02005146 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5147 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005148 psa_algorithm_t alg = alg_arg;
5149 psa_key_type_t derived_type = derived_type_arg;
5150 size_t derived_bits = derived_bits_arg;
5151 psa_key_usage_t derived_usage = derived_usage_arg;
5152 psa_algorithm_t derived_alg = derived_alg_arg;
5153 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005154 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005155 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005156 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005157
Gilles Peskine8817f612018-12-18 00:18:46 +01005158 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005159
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005160 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5161 psa_set_key_algorithm( &attributes, alg );
5162 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005163 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005164 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005165
5166 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005167 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005168 input1->x, input1->len,
5169 input2->x, input2->len, capacity ) )
5170 goto exit;
5171
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005172 psa_set_key_usage_flags( &attributes, derived_usage );
5173 psa_set_key_algorithm( &attributes, derived_alg );
5174 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005175 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005176 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005177 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005178
5179 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005180 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005181 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5182 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005183
5184 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005185 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005186 goto exit;
5187
5188exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005189 /*
5190 * Key attributes may have been returned by psa_get_key_attributes()
5191 * thus reset them as required.
5192 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005193 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005194
5195 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005196 psa_destroy_key( base_key );
5197 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005198 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005199}
5200/* END_CASE */
5201
Janos Follath42fd8882019-07-03 14:17:09 +01005202/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005203void derive_key_export( int alg_arg,
5204 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005205 data_t *input1,
5206 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005207 int bytes1_arg,
5208 int bytes2_arg )
5209{
Ronald Cron5425a212020-08-04 14:58:35 +02005210 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5211 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005212 psa_algorithm_t alg = alg_arg;
5213 size_t bytes1 = bytes1_arg;
5214 size_t bytes2 = bytes2_arg;
5215 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005217 uint8_t *output_buffer = NULL;
5218 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005219 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5220 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005221 size_t length;
5222
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005223 ASSERT_ALLOC( output_buffer, capacity );
5224 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005225 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005226
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005227 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5228 psa_set_key_algorithm( &base_attributes, alg );
5229 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005230 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005231 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005232
5233 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005234 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005235 input1->x, input1->len,
5236 input2->x, input2->len, capacity ) )
5237 goto exit;
5238
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005240 output_buffer,
5241 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005242 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005243
5244 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005245 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005246 input1->x, input1->len,
5247 input2->x, input2->len, capacity ) )
5248 goto exit;
5249
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005250 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5251 psa_set_key_algorithm( &derived_attributes, 0 );
5252 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005253 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005254 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005255 &derived_key ) );
5256 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005257 export_buffer, bytes1,
5258 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005259 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005260 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005261 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005262 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005263 &derived_key ) );
5264 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005265 export_buffer + bytes1, bytes2,
5266 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005267 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005268
5269 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005270 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5271 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005272
5273exit:
5274 mbedtls_free( output_buffer );
5275 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005277 psa_destroy_key( base_key );
5278 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005279 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005280}
5281/* END_CASE */
5282
5283/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005284void derive_key( int alg_arg,
5285 data_t *key_data, data_t *input1, data_t *input2,
5286 int type_arg, int bits_arg,
5287 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005288{
Ronald Cron5425a212020-08-04 14:58:35 +02005289 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5290 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005291 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005292 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005293 size_t bits = bits_arg;
5294 psa_status_t expected_status = expected_status_arg;
5295 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5296 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5297 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5298
5299 PSA_ASSERT( psa_crypto_init( ) );
5300
5301 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5302 psa_set_key_algorithm( &base_attributes, alg );
5303 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5304 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005305 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005306
Ronald Cron5425a212020-08-04 14:58:35 +02005307 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005308 input1->x, input1->len,
5309 input2->x, input2->len, SIZE_MAX ) )
5310 goto exit;
5311
5312 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5313 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005314 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005315 psa_set_key_bits( &derived_attributes, bits );
5316 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005317 &derived_key ),
Gilles Peskinec744d992019-07-30 17:26:54 +02005318 expected_status );
5319
5320exit:
5321 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005322 psa_destroy_key( base_key );
5323 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005324 PSA_DONE( );
5325}
5326/* END_CASE */
5327
5328/* BEGIN_CASE */
Steven Cooreman69967ce2021-01-18 18:01:08 +01005329void derive_large_key( int alg_arg,
5330 data_t *key_data, data_t *input1, data_t *input2,
5331 int type_arg, int bits_arg,
5332 int expected_status_arg )
5333{
5334 size_t key_bytes = PSA_BITS_TO_BYTES(bits_arg);
5335 uint8_t* buffer = NULL;
5336
5337 /* Check that the target running this test can accomodate large
5338 * keys on its heap, before calling the actual generate_key test */
5339 ASSERT_ALLOC_WEAK(buffer, key_bytes);
5340 mbedtls_free( buffer );
5341
5342 test_derive_key( alg_arg, key_data, input1, input2, type_arg, bits_arg, expected_status_arg );
5343exit:
5344 return;
5345}
5346/* END_CASE */
5347
5348/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005349void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005350 int our_key_type_arg, int our_key_alg_arg,
5351 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005352 int expected_status_arg )
5353{
Ronald Cron5425a212020-08-04 14:58:35 +02005354 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005355 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005356 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005357 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005358 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005360 psa_status_t expected_status = expected_status_arg;
5361 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005362
Gilles Peskine8817f612018-12-18 00:18:46 +01005363 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005364
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005365 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005366 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005367 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005368 PSA_ASSERT( psa_import_key( &attributes,
5369 our_key_data->x, our_key_data->len,
5370 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005371
Gilles Peskine77f40d82019-04-11 21:27:06 +02005372 /* The tests currently include inputs that should fail at either step.
5373 * Test cases that fail at the setup step should be changed to call
5374 * key_derivation_setup instead, and this function should be renamed
5375 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005376 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005377 if( status == PSA_SUCCESS )
5378 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005379 TEST_EQUAL( psa_key_derivation_key_agreement(
5380 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5381 our_key,
5382 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005383 expected_status );
5384 }
5385 else
5386 {
5387 TEST_ASSERT( status == expected_status );
5388 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005389
5390exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005391 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005392 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005393 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005394}
5395/* END_CASE */
5396
5397/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005398void raw_key_agreement( int alg_arg,
5399 int our_key_type_arg, data_t *our_key_data,
5400 data_t *peer_key_data,
5401 data_t *expected_output )
5402{
Ronald Cron5425a212020-08-04 14:58:35 +02005403 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005404 psa_algorithm_t alg = alg_arg;
5405 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005407 unsigned char *output = NULL;
5408 size_t output_length = ~0;
5409
5410 ASSERT_ALLOC( output, expected_output->len );
5411 PSA_ASSERT( psa_crypto_init( ) );
5412
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005413 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5414 psa_set_key_algorithm( &attributes, alg );
5415 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005416 PSA_ASSERT( psa_import_key( &attributes,
5417 our_key_data->x, our_key_data->len,
5418 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005419
Gilles Peskinebe697d82019-05-16 18:00:41 +02005420 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5421 peer_key_data->x, peer_key_data->len,
5422 output, expected_output->len,
5423 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005424 ASSERT_COMPARE( output, output_length,
5425 expected_output->x, expected_output->len );
5426
5427exit:
5428 mbedtls_free( output );
5429 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005430 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005431}
5432/* END_CASE */
5433
5434/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005435void key_agreement_capacity( int alg_arg,
5436 int our_key_type_arg, data_t *our_key_data,
5437 data_t *peer_key_data,
5438 int expected_capacity_arg )
5439{
Ronald Cron5425a212020-08-04 14:58:35 +02005440 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005441 psa_algorithm_t alg = alg_arg;
5442 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005443 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005445 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005446 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005447
Gilles Peskine8817f612018-12-18 00:18:46 +01005448 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005449
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005450 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5451 psa_set_key_algorithm( &attributes, alg );
5452 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005453 PSA_ASSERT( psa_import_key( &attributes,
5454 our_key_data->x, our_key_data->len,
5455 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005456
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005457 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005458 PSA_ASSERT( psa_key_derivation_key_agreement(
5459 &operation,
5460 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5461 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005462 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5463 {
5464 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005465 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005466 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005467 NULL, 0 ) );
5468 }
Gilles Peskine59685592018-09-18 12:11:34 +02005469
Gilles Peskinebf491972018-10-25 22:36:12 +02005470 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005471 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005472 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005473 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005474
Gilles Peskinebf491972018-10-25 22:36:12 +02005475 /* Test the actual capacity by reading the output. */
5476 while( actual_capacity > sizeof( output ) )
5477 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005478 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005479 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005480 actual_capacity -= sizeof( output );
5481 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005482 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005483 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005484 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005485 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005486
Gilles Peskine59685592018-09-18 12:11:34 +02005487exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005489 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005490 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005491}
5492/* END_CASE */
5493
5494/* BEGIN_CASE */
5495void key_agreement_output( int alg_arg,
5496 int our_key_type_arg, data_t *our_key_data,
5497 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005498 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005499{
Ronald Cron5425a212020-08-04 14:58:35 +02005500 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005501 psa_algorithm_t alg = alg_arg;
5502 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005503 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005505 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005506
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005507 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5508 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005509
Gilles Peskine8817f612018-12-18 00:18:46 +01005510 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005511
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005512 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5513 psa_set_key_algorithm( &attributes, alg );
5514 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005515 PSA_ASSERT( psa_import_key( &attributes,
5516 our_key_data->x, our_key_data->len,
5517 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005518
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005519 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005520 PSA_ASSERT( psa_key_derivation_key_agreement(
5521 &operation,
5522 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5523 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005524 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5525 {
5526 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005527 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005528 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005529 NULL, 0 ) );
5530 }
Gilles Peskine59685592018-09-18 12:11:34 +02005531
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005532 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005533 actual_output,
5534 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005535 ASSERT_COMPARE( actual_output, expected_output1->len,
5536 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005537 if( expected_output2->len != 0 )
5538 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005539 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005540 actual_output,
5541 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005542 ASSERT_COMPARE( actual_output, expected_output2->len,
5543 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005544 }
Gilles Peskine59685592018-09-18 12:11:34 +02005545
5546exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005547 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005548 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005549 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005550 mbedtls_free( actual_output );
5551}
5552/* END_CASE */
5553
5554/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005555void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005556{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005557 size_t bytes = bytes_arg;
5558 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005559 unsigned char *output = NULL;
5560 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005561 size_t i;
5562 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005563
Simon Butcher49f8e312020-03-03 15:51:50 +00005564 TEST_ASSERT( bytes_arg >= 0 );
5565
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005566 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5567 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005568 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005569
Gilles Peskine8817f612018-12-18 00:18:46 +01005570 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005571
Gilles Peskinea50d7392018-06-21 10:22:13 +02005572 /* Run several times, to ensure that every output byte will be
5573 * nonzero at least once with overwhelming probability
5574 * (2^(-8*number_of_runs)). */
5575 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005576 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005577 if( bytes != 0 )
5578 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005579 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005580
5581 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005582 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5583 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005584
5585 for( i = 0; i < bytes; i++ )
5586 {
5587 if( output[i] != 0 )
5588 ++changed[i];
5589 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005590 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005591
5592 /* Check that every byte was changed to nonzero at least once. This
5593 * validates that psa_generate_random is overwriting every byte of
5594 * the output buffer. */
5595 for( i = 0; i < bytes; i++ )
5596 {
5597 TEST_ASSERT( changed[i] != 0 );
5598 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005599
5600exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005601 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005602 mbedtls_free( output );
5603 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005604}
5605/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005606
5607/* BEGIN_CASE */
5608void generate_key( int type_arg,
5609 int bits_arg,
5610 int usage_arg,
5611 int alg_arg,
5612 int expected_status_arg )
5613{
Ronald Cron5425a212020-08-04 14:58:35 +02005614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005615 psa_key_type_t type = type_arg;
5616 psa_key_usage_t usage = usage_arg;
5617 size_t bits = bits_arg;
5618 psa_algorithm_t alg = alg_arg;
5619 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005620 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005621 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005622
Gilles Peskine8817f612018-12-18 00:18:46 +01005623 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005624
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005625 psa_set_key_usage_flags( &attributes, usage );
5626 psa_set_key_algorithm( &attributes, alg );
5627 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005628 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005629
5630 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005631 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005632 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005633 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005634
5635 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005636 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005637 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5638 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005639
Gilles Peskine818ca122018-06-20 18:16:48 +02005640 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005641 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005642 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005643
5644exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005645 /*
5646 * Key attributes may have been returned by psa_get_key_attributes()
5647 * thus reset them as required.
5648 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005649 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005650
Ronald Cron5425a212020-08-04 14:58:35 +02005651 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005652 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005653}
5654/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005655
Steven Cooreman69967ce2021-01-18 18:01:08 +01005656/* BEGIN_CASE */
5657void generate_large_key( int type_arg,
5658 int bits_arg,
5659 int usage_arg,
5660 int alg_arg,
5661 int expected_status_arg )
5662{
5663 size_t key_bytes = PSA_BITS_TO_BYTES(bits_arg);
5664 uint8_t* buffer = NULL;
5665
5666 /* Check that the target running this test can accomodate large
5667 * keys on its heap, before calling the actual generate_key test */
5668 ASSERT_ALLOC_WEAK(buffer, key_bytes);
5669 mbedtls_free( buffer );
5670
5671 test_generate_key( type_arg, bits_arg, usage_arg, alg_arg, expected_status_arg );
5672exit:
5673 return;
5674}
5675/* END_CASE */
5676
Gilles Peskinee56e8782019-04-26 17:34:02 +02005677/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5678void generate_key_rsa( int bits_arg,
5679 data_t *e_arg,
5680 int expected_status_arg )
5681{
Ronald Cron5425a212020-08-04 14:58:35 +02005682 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005683 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005684 size_t bits = bits_arg;
5685 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5686 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5687 psa_status_t expected_status = expected_status_arg;
5688 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5689 uint8_t *exported = NULL;
5690 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005691 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005692 size_t exported_length = SIZE_MAX;
5693 uint8_t *e_read_buffer = NULL;
5694 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005695 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005696 size_t e_read_length = SIZE_MAX;
5697
5698 if( e_arg->len == 0 ||
5699 ( e_arg->len == 3 &&
5700 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5701 {
5702 is_default_public_exponent = 1;
5703 e_read_size = 0;
5704 }
5705 ASSERT_ALLOC( e_read_buffer, e_read_size );
5706 ASSERT_ALLOC( exported, exported_size );
5707
5708 PSA_ASSERT( psa_crypto_init( ) );
5709
5710 psa_set_key_usage_flags( &attributes, usage );
5711 psa_set_key_algorithm( &attributes, alg );
5712 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5713 e_arg->x, e_arg->len ) );
5714 psa_set_key_bits( &attributes, bits );
5715
5716 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005717 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005718 if( expected_status != PSA_SUCCESS )
5719 goto exit;
5720
5721 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005722 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005723 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5724 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5725 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5726 e_read_buffer, e_read_size,
5727 &e_read_length ) );
5728 if( is_default_public_exponent )
5729 TEST_EQUAL( e_read_length, 0 );
5730 else
5731 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5732
5733 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005734 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005735 goto exit;
5736
5737 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005738 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005739 exported, exported_size,
5740 &exported_length ) );
5741 {
5742 uint8_t *p = exported;
5743 uint8_t *end = exported + exported_length;
5744 size_t len;
5745 /* RSAPublicKey ::= SEQUENCE {
5746 * modulus INTEGER, -- n
5747 * publicExponent INTEGER } -- e
5748 */
5749 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005750 MBEDTLS_ASN1_SEQUENCE |
5751 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005752 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5753 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5754 MBEDTLS_ASN1_INTEGER ) );
5755 if( len >= 1 && p[0] == 0 )
5756 {
5757 ++p;
5758 --len;
5759 }
5760 if( e_arg->len == 0 )
5761 {
5762 TEST_EQUAL( len, 3 );
5763 TEST_EQUAL( p[0], 1 );
5764 TEST_EQUAL( p[1], 0 );
5765 TEST_EQUAL( p[2], 1 );
5766 }
5767 else
5768 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5769 }
5770
5771exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005772 /*
5773 * Key attributes may have been returned by psa_get_key_attributes() or
5774 * set by psa_set_key_domain_parameters() thus reset them as required.
5775 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005776 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005777
Ronald Cron5425a212020-08-04 14:58:35 +02005778 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005779 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005780 mbedtls_free( e_read_buffer );
5781 mbedtls_free( exported );
5782}
5783/* END_CASE */
5784
Darryl Greend49a4992018-06-18 17:27:26 +01005785/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005786void persistent_key_load_key_from_storage( data_t *data,
5787 int type_arg, int bits_arg,
5788 int usage_flags_arg, int alg_arg,
5789 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005790{
Ronald Cron71016a92020-08-28 19:01:50 +02005791 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005792 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5794 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005795 psa_key_type_t type = type_arg;
5796 size_t bits = bits_arg;
5797 psa_key_usage_t usage_flags = usage_flags_arg;
5798 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005799 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005800 unsigned char *first_export = NULL;
5801 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005802 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005803 size_t first_exported_length;
5804 size_t second_exported_length;
5805
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005806 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5807 {
5808 ASSERT_ALLOC( first_export, export_size );
5809 ASSERT_ALLOC( second_export, export_size );
5810 }
Darryl Greend49a4992018-06-18 17:27:26 +01005811
Gilles Peskine8817f612018-12-18 00:18:46 +01005812 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005813
Gilles Peskinec87af662019-05-15 16:12:22 +02005814 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005815 psa_set_key_usage_flags( &attributes, usage_flags );
5816 psa_set_key_algorithm( &attributes, alg );
5817 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005818 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005819
Darryl Green0c6575a2018-11-07 16:05:30 +00005820 switch( generation_method )
5821 {
5822 case IMPORT_KEY:
5823 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005824 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005825 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005826 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005827
Darryl Green0c6575a2018-11-07 16:05:30 +00005828 case GENERATE_KEY:
5829 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005830 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005831 break;
5832
5833 case DERIVE_KEY:
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005834#if PSA_WANT_ALG_HKDF && PSA_WANT_ALG_SHA_256
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005835 {
5836 /* Create base key */
5837 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5838 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5839 psa_set_key_usage_flags( &base_attributes,
5840 PSA_KEY_USAGE_DERIVE );
5841 psa_set_key_algorithm( &base_attributes, derive_alg );
5842 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005843 PSA_ASSERT( psa_import_key( &base_attributes,
5844 data->x, data->len,
5845 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005846 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005847 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005848 PSA_ASSERT( psa_key_derivation_input_key(
5849 &operation,
5850 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005851 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005852 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005853 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005854 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5855 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005856 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005857 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005858 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005859 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005860 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005861#else
5862 TEST_ASSUME( ! "KDF not supported in this configuration" );
5863#endif
5864 break;
5865
5866 default:
5867 TEST_ASSERT( ! "generation_method not implemented in test" );
5868 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005869 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005870 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005871
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005872 /* Export the key if permitted by the key policy. */
5873 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5874 {
Ronald Cron5425a212020-08-04 14:58:35 +02005875 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005876 first_export, export_size,
5877 &first_exported_length ) );
5878 if( generation_method == IMPORT_KEY )
5879 ASSERT_COMPARE( data->x, data->len,
5880 first_export, first_exported_length );
5881 }
Darryl Greend49a4992018-06-18 17:27:26 +01005882
5883 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005884 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005885 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005886 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005887
Darryl Greend49a4992018-06-18 17:27:26 +01005888 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005889 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005890 TEST_ASSERT( mbedtls_svc_key_id_equal(
5891 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005892 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5893 PSA_KEY_LIFETIME_PERSISTENT );
5894 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5895 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5896 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5897 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005898
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005899 /* Export the key again if permitted by the key policy. */
5900 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005901 {
Ronald Cron5425a212020-08-04 14:58:35 +02005902 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005903 second_export, export_size,
5904 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005905 ASSERT_COMPARE( first_export, first_exported_length,
5906 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005907 }
5908
5909 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005910 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005911 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005912
5913exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005914 /*
5915 * Key attributes may have been returned by psa_get_key_attributes()
5916 * thus reset them as required.
5917 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005918 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005919
Darryl Greend49a4992018-06-18 17:27:26 +01005920 mbedtls_free( first_export );
5921 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005922 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005923 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005924 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005925 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005926}
5927/* END_CASE */