blob: 4cf53ca0b3604861fcc561bda21e463942f68eea [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Jaeden Amerof24c7f82018-06-27 17:20:43 +010015/** An invalid export length that will never be set by psa_export_key(). */
16static const size_t INVALID_EXPORT_LENGTH = ~0U;
17
Gilles Peskinef426e0f2019-02-25 17:42:03 +010018/* A hash algorithm that is known to be supported.
19 *
20 * This is used in some smoke tests.
21 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010022#if defined(PSA_WANT_ALG_MD2)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010023#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
Gilles Peskined6dc40c2021-01-12 12:55:31 +010024#elif defined(PSA_WANT_ALG_MD4)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010025#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
Gilles Peskined6dc40c2021-01-12 12:55:31 +010026#elif defined(PSA_WANT_ALG_MD5)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010027#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
28/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
29 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
30 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
31 * implausible anyway. */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010032#elif defined(PSA_WANT_ALG_SHA_1)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010033#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
Gilles Peskined6dc40c2021-01-12 12:55:31 +010034#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010035#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
Gilles Peskined6dc40c2021-01-12 12:55:31 +010036#elif defined(PSA_WANT_ALG_SHA_384)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010037#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
Gilles Peskined6dc40c2021-01-12 12:55:31 +010038#elif defined(PSA_WANT_ALG_SHA_512)
39#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
40#elif defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010041#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
42#else
43#undef KNOWN_SUPPORTED_HASH_ALG
44#endif
45
46/* A block cipher that is known to be supported.
47 *
48 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
49 */
50#if defined(MBEDTLS_AES_C)
51#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
52#elif defined(MBEDTLS_ARIA_C)
53#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
54#elif defined(MBEDTLS_CAMELLIA_C)
55#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
56#undef KNOWN_SUPPORTED_BLOCK_CIPHER
57#endif
58
59/* A MAC mode that is known to be supported.
60 *
61 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
62 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
63 *
64 * This is used in some smoke tests.
65 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010066#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010067#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
68#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
69#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
70#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
71#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
72#else
73#undef KNOWN_SUPPORTED_MAC_ALG
74#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
75#endif
76
77/* A cipher algorithm and key type that are known to be supported.
78 *
79 * This is used in some smoke tests.
80 */
81#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
82#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
83#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
84#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
85#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
86#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
87#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
89#else
90#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91#endif
92#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
93#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
94#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
95#elif defined(MBEDTLS_RC4_C)
96#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
97#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
98#else
99#undef KNOWN_SUPPORTED_CIPHER_ALG
100#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
101#endif
102
Gilles Peskine667c1112019-12-03 19:03:20 +0100103#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100104int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
Gilles Peskine667c1112019-12-03 19:03:20 +0100105{
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100106 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
107 PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100108}
109#else
110int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
111{
112 (void) lifetime;
113 return( 0 );
114}
115#endif
116
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200117/** Test if a buffer contains a constant byte value.
118 *
119 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200120 *
121 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200122 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200123 * \param size Size of the buffer in bytes.
124 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200125 * \return 1 if the buffer is all-bits-zero.
126 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200127 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200128static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200129{
130 size_t i;
131 for( i = 0; i < size; i++ )
132 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200133 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200134 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200135 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200136 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200137}
Gilles Peskine818ca122018-06-20 18:16:48 +0200138
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200139/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
140static int asn1_write_10x( unsigned char **p,
141 unsigned char *start,
142 size_t bits,
143 unsigned char x )
144{
145 int ret;
146 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200147 if( bits == 0 )
148 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
149 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200150 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300151 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200152 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
153 *p -= len;
154 ( *p )[len-1] = x;
155 if( bits % 8 == 0 )
156 ( *p )[1] |= 1;
157 else
158 ( *p )[0] |= 1 << ( bits % 8 );
159 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
160 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
161 MBEDTLS_ASN1_INTEGER ) );
162 return( len );
163}
164
165static int construct_fake_rsa_key( unsigned char *buffer,
166 size_t buffer_size,
167 unsigned char **p,
168 size_t bits,
169 int keypair )
170{
171 size_t half_bits = ( bits + 1 ) / 2;
172 int ret;
173 int len = 0;
174 /* Construct something that looks like a DER encoding of
175 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
176 * RSAPrivateKey ::= SEQUENCE {
177 * version Version,
178 * modulus INTEGER, -- n
179 * publicExponent INTEGER, -- e
180 * privateExponent INTEGER, -- d
181 * prime1 INTEGER, -- p
182 * prime2 INTEGER, -- q
183 * exponent1 INTEGER, -- d mod (p-1)
184 * exponent2 INTEGER, -- d mod (q-1)
185 * coefficient INTEGER, -- (inverse of q) mod p
186 * otherPrimeInfos OtherPrimeInfos OPTIONAL
187 * }
188 * Or, for a public key, the same structure with only
189 * version, modulus and publicExponent.
190 */
191 *p = buffer + buffer_size;
192 if( keypair )
193 {
194 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
195 asn1_write_10x( p, buffer, half_bits, 1 ) );
196 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
197 asn1_write_10x( p, buffer, half_bits, 1 ) );
198 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
199 asn1_write_10x( p, buffer, half_bits, 1 ) );
200 MBEDTLS_ASN1_CHK_ADD( len, /* q */
201 asn1_write_10x( p, buffer, half_bits, 1 ) );
202 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
203 asn1_write_10x( p, buffer, half_bits, 3 ) );
204 MBEDTLS_ASN1_CHK_ADD( len, /* d */
205 asn1_write_10x( p, buffer, bits, 1 ) );
206 }
207 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
208 asn1_write_10x( p, buffer, 17, 1 ) );
209 MBEDTLS_ASN1_CHK_ADD( len, /* n */
210 asn1_write_10x( p, buffer, bits, 1 ) );
211 if( keypair )
212 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
213 mbedtls_asn1_write_int( p, buffer, 0 ) );
214 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
215 {
216 const unsigned char tag =
217 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
218 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
219 }
220 return( len );
221}
222
Ronald Cron5425a212020-08-04 14:58:35 +0200223int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100224{
225 int ok = 0;
226 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
227 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200228 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100229 psa_key_type_t type;
230 psa_key_type_t bits;
231
232 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
233 lifetime = psa_get_key_lifetime( &attributes );
234 id = psa_get_key_id( &attributes );
235 type = psa_get_key_type( &attributes );
236 bits = psa_get_key_bits( &attributes );
237
238 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200239 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200240 {
241 TEST_ASSERT(
242 ( PSA_KEY_ID_VOLATILE_MIN <=
243 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
244 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
245 PSA_KEY_ID_VOLATILE_MAX ) );
246 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100247 else
248 {
249 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200250 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
251 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100252 }
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254 /* randomly-generated 64-bit constant, should never appear in test data */
255 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
256 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100257 if( lifetime_is_dynamic_secure_element( lifetime ) )
Gilles Peskine667c1112019-12-03 19:03:20 +0100258 {
259 /* Mbed Crypto currently always exposes the slot number to
260 * applications. This is not mandated by the PSA specification
261 * and may change in future versions. */
262 TEST_EQUAL( status, 0 );
263 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
264 }
265 else
266 {
267 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
268 }
269#endif
270
271 /* Type and size */
272 TEST_ASSERT( type != 0 );
273 TEST_ASSERT( bits != 0 );
274 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
275 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
276 TEST_ASSERT( bits % 8 == 0 );
277
278 /* MAX macros concerning specific key types */
279 if( PSA_KEY_TYPE_IS_ECC( type ) )
280 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
281 else if( PSA_KEY_TYPE_IS_RSA( type ) )
282 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100283 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100284
285 ok = 1;
286
287exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100288 /*
289 * Key attributes may have been returned by psa_get_key_attributes()
290 * thus reset them as required.
291 */
Gilles Peskine667c1112019-12-03 19:03:20 +0100292 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100293
Gilles Peskine667c1112019-12-03 19:03:20 +0100294 return( ok );
295}
296
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100297int exercise_mac_setup( psa_key_type_t key_type,
298 const unsigned char *key_bytes,
299 size_t key_length,
300 psa_algorithm_t alg,
301 psa_mac_operation_t *operation,
302 psa_status_t *status )
303{
Ronald Cron5425a212020-08-04 14:58:35 +0200304 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200305 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100306
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100307 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200308 psa_set_key_algorithm( &attributes, alg );
309 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200310 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100311
Ronald Cron5425a212020-08-04 14:58:35 +0200312 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100313 /* Whether setup succeeded or failed, abort must succeed. */
314 PSA_ASSERT( psa_mac_abort( operation ) );
315 /* If setup failed, reproduce the failure, so that the caller can
316 * test the resulting state of the operation object. */
317 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100318 {
Ronald Cron5425a212020-08-04 14:58:35 +0200319 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100320 }
321
Ronald Cron5425a212020-08-04 14:58:35 +0200322 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100323 return( 1 );
324
325exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200326 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100327 return( 0 );
328}
329
330int exercise_cipher_setup( psa_key_type_t key_type,
331 const unsigned char *key_bytes,
332 size_t key_length,
333 psa_algorithm_t alg,
334 psa_cipher_operation_t *operation,
335 psa_status_t *status )
336{
Ronald Cron5425a212020-08-04 14:58:35 +0200337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100339
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200340 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
341 psa_set_key_algorithm( &attributes, alg );
342 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200343 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100344
Ronald Cron5425a212020-08-04 14:58:35 +0200345 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100346 /* Whether setup succeeded or failed, abort must succeed. */
347 PSA_ASSERT( psa_cipher_abort( operation ) );
348 /* If setup failed, reproduce the failure, so that the caller can
349 * test the resulting state of the operation object. */
350 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100351 {
Ronald Cron5425a212020-08-04 14:58:35 +0200352 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100353 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100354 }
355
Ronald Cron5425a212020-08-04 14:58:35 +0200356 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100357 return( 1 );
358
359exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200360 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100361 return( 0 );
362}
363
Ronald Cron5425a212020-08-04 14:58:35 +0200364static int exercise_mac_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200365 psa_key_usage_t usage,
366 psa_algorithm_t alg )
367{
Jaeden Amero769ce272019-01-04 11:48:03 +0000368 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200369 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200370 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200371 size_t mac_length = sizeof( mac );
372
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100373 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 {
Ronald Cron5425a212020-08-04 14:58:35 +0200375 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100376 PSA_ASSERT( psa_mac_update( &operation,
377 input, sizeof( input ) ) );
378 PSA_ASSERT( psa_mac_sign_finish( &operation,
379 mac, sizeof( mac ),
380 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200381 }
382
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100383 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200384 {
385 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100386 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200387 PSA_SUCCESS :
388 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200389 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100390 PSA_ASSERT( psa_mac_update( &operation,
391 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100392 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
393 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200394 }
395
396 return( 1 );
397
398exit:
399 psa_mac_abort( &operation );
400 return( 0 );
401}
402
Ronald Cron5425a212020-08-04 14:58:35 +0200403static int exercise_cipher_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200404 psa_key_usage_t usage,
405 psa_algorithm_t alg )
406{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000407 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200408 unsigned char iv[16] = {0};
409 size_t iv_length = sizeof( iv );
410 const unsigned char plaintext[16] = "Hello, world...";
411 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
412 size_t ciphertext_length = sizeof( ciphertext );
413 unsigned char decrypted[sizeof( ciphertext )];
414 size_t part_length;
415
416 if( usage & PSA_KEY_USAGE_ENCRYPT )
417 {
Ronald Cron5425a212020-08-04 14:58:35 +0200418 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100419 PSA_ASSERT( psa_cipher_generate_iv( &operation,
420 iv, sizeof( iv ),
421 &iv_length ) );
422 PSA_ASSERT( psa_cipher_update( &operation,
423 plaintext, sizeof( plaintext ),
424 ciphertext, sizeof( ciphertext ),
425 &ciphertext_length ) );
426 PSA_ASSERT( psa_cipher_finish( &operation,
427 ciphertext + ciphertext_length,
428 sizeof( ciphertext ) - ciphertext_length,
429 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200430 ciphertext_length += part_length;
431 }
432
433 if( usage & PSA_KEY_USAGE_DECRYPT )
434 {
435 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200436 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200437 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
438 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200440 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200441 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
442 * have this macro yet. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100443 iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200444 psa_get_key_type( &attributes ) );
445 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100446 psa_reset_key_attributes( &attributes );
Gilles Peskine818ca122018-06-20 18:16:48 +0200447 }
Ronald Cron5425a212020-08-04 14:58:35 +0200448 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100449 PSA_ASSERT( psa_cipher_set_iv( &operation,
450 iv, iv_length ) );
451 PSA_ASSERT( psa_cipher_update( &operation,
452 ciphertext, ciphertext_length,
453 decrypted, sizeof( decrypted ),
454 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200455 status = psa_cipher_finish( &operation,
456 decrypted + part_length,
457 sizeof( decrypted ) - part_length,
458 &part_length );
459 /* For a stream cipher, all inputs are valid. For a block cipher,
460 * if the input is some aribtrary data rather than an actual
461 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200462 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200463 TEST_ASSERT( status == PSA_SUCCESS ||
464 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200465 else
466 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200467 }
468
469 return( 1 );
470
471exit:
472 psa_cipher_abort( &operation );
473 return( 0 );
474}
475
Ronald Cron5425a212020-08-04 14:58:35 +0200476static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200477 psa_key_usage_t usage,
478 psa_algorithm_t alg )
479{
480 unsigned char nonce[16] = {0};
481 size_t nonce_length = sizeof( nonce );
482 unsigned char plaintext[16] = "Hello, world...";
483 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
484 size_t ciphertext_length = sizeof( ciphertext );
485 size_t plaintext_length = sizeof( ciphertext );
486
487 if( usage & PSA_KEY_USAGE_ENCRYPT )
488 {
Ronald Cron5425a212020-08-04 14:58:35 +0200489 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100490 nonce, nonce_length,
491 NULL, 0,
492 plaintext, sizeof( plaintext ),
493 ciphertext, sizeof( ciphertext ),
494 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200495 }
496
497 if( usage & PSA_KEY_USAGE_DECRYPT )
498 {
499 psa_status_t verify_status =
500 ( usage & PSA_KEY_USAGE_ENCRYPT ?
501 PSA_SUCCESS :
502 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200503 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100504 nonce, nonce_length,
505 NULL, 0,
506 ciphertext, ciphertext_length,
507 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100508 &plaintext_length ),
509 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200510 }
511
512 return( 1 );
513
514exit:
515 return( 0 );
516}
517
Ronald Cron5425a212020-08-04 14:58:35 +0200518static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200519 psa_key_usage_t usage,
520 psa_algorithm_t alg )
521{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200522 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
523 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100524 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200525 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100526 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
527
528 /* If the policy allows signing with any hash, just pick one. */
529 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
530 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100531#if defined(KNOWN_SUPPORTED_HASH_ALG)
532 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
533 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100534#else
Chris Jones9634bb12021-01-20 15:56:42 +0000535 mbedtls_test_fail( "No hash algorithm for hash-and-sign testing",
536 __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100537 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100538#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100539 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200540
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100541 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200542 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200543 /* Some algorithms require the payload to have the size of
544 * the hash encoded in the algorithm. Use this input size
545 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200546 if( hash_alg != 0 )
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100547 payload_length = PSA_HASH_LENGTH( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200548 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100549 payload, payload_length,
550 signature, sizeof( signature ),
551 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200552 }
553
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100554 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200555 {
556 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100557 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200558 PSA_SUCCESS :
559 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200560 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100561 payload, payload_length,
562 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100563 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200564 }
565
566 return( 1 );
567
568exit:
569 return( 0 );
570}
571
Ronald Cron5425a212020-08-04 14:58:35 +0200572static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200573 psa_key_usage_t usage,
574 psa_algorithm_t alg )
575{
576 unsigned char plaintext[256] = "Hello, world...";
577 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
578 size_t ciphertext_length = sizeof( ciphertext );
579 size_t plaintext_length = 16;
580
581 if( usage & PSA_KEY_USAGE_ENCRYPT )
582 {
Ronald Cron5425a212020-08-04 14:58:35 +0200583 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100584 plaintext, plaintext_length,
585 NULL, 0,
586 ciphertext, sizeof( ciphertext ),
587 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200588 }
589
590 if( usage & PSA_KEY_USAGE_DECRYPT )
591 {
592 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200593 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200594 ciphertext, ciphertext_length,
595 NULL, 0,
596 plaintext, sizeof( plaintext ),
597 &plaintext_length );
598 TEST_ASSERT( status == PSA_SUCCESS ||
599 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
600 ( status == PSA_ERROR_INVALID_ARGUMENT ||
601 status == PSA_ERROR_INVALID_PADDING ) ) );
602 }
603
604 return( 1 );
605
606exit:
607 return( 0 );
608}
Gilles Peskine02b75072018-07-01 22:31:34 +0200609
Janos Follathf2815ea2019-07-03 12:41:36 +0100610static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200611 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100612 psa_algorithm_t alg,
613 unsigned char* input1, size_t input1_length,
614 unsigned char* input2, size_t input2_length,
615 size_t capacity )
616{
617 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
618 if( PSA_ALG_IS_HKDF( alg ) )
619 {
620 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
621 PSA_KEY_DERIVATION_INPUT_SALT,
622 input1, input1_length ) );
623 PSA_ASSERT( psa_key_derivation_input_key( operation,
624 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200625 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100626 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
627 PSA_KEY_DERIVATION_INPUT_INFO,
628 input2,
629 input2_length ) );
630 }
631 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
632 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
633 {
634 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
635 PSA_KEY_DERIVATION_INPUT_SEED,
636 input1, input1_length ) );
637 PSA_ASSERT( psa_key_derivation_input_key( operation,
638 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200639 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100640 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
641 PSA_KEY_DERIVATION_INPUT_LABEL,
642 input2, input2_length ) );
643 }
644 else
645 {
646 TEST_ASSERT( ! "Key derivation algorithm not supported" );
647 }
648
Gilles Peskinec744d992019-07-30 17:26:54 +0200649 if( capacity != SIZE_MAX )
650 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100651
652 return( 1 );
653
654exit:
655 return( 0 );
656}
657
658
Ronald Cron5425a212020-08-04 14:58:35 +0200659static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200660 psa_key_usage_t usage,
661 psa_algorithm_t alg )
662{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200663 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100664 unsigned char input1[] = "Input 1";
665 size_t input1_length = sizeof( input1 );
666 unsigned char input2[] = "Input 2";
667 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200668 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100669 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200670
671 if( usage & PSA_KEY_USAGE_DERIVE )
672 {
Ronald Cron5425a212020-08-04 14:58:35 +0200673 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100674 input1, input1_length,
675 input2, input2_length, capacity ) )
676 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200677
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200678 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200679 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100680 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200681 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200682 }
683
684 return( 1 );
685
686exit:
687 return( 0 );
688}
689
Gilles Peskinec7998b72018-11-07 18:45:02 +0100690/* We need two keys to exercise key agreement. Exercise the
691 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200692static psa_status_t key_agreement_with_self(
693 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200694 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100695{
696 psa_key_type_t private_key_type;
697 psa_key_type_t public_key_type;
698 size_t key_bits;
699 uint8_t *public_key = NULL;
700 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200701 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200702 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
703 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200704 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100706
Ronald Cron5425a212020-08-04 14:58:35 +0200707 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200708 private_key_type = psa_get_key_type( &attributes );
709 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200710 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100711 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100712 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200713 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100714 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100715
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200716 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200717 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200718 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100719exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100720 /*
721 * Key attributes may have been returned by psa_get_key_attributes()
722 * thus reset them as required.
723 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200724 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100725
726 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100727 return( status );
728}
729
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200730/* We need two keys to exercise key agreement. Exercise the
731 * private key against its own public key. */
732static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200733 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200734{
735 psa_key_type_t private_key_type;
736 psa_key_type_t public_key_type;
737 size_t key_bits;
738 uint8_t *public_key = NULL;
739 size_t public_key_length;
740 uint8_t output[1024];
741 size_t output_length;
742 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200743 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
744 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200745 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200747
Ronald Cron5425a212020-08-04 14:58:35 +0200748 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200749 private_key_type = psa_get_key_type( &attributes );
750 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200751 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100752 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200753 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200754 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200755 public_key, public_key_length,
756 &public_key_length ) );
757
Ronald Cron5425a212020-08-04 14:58:35 +0200758 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200759 public_key, public_key_length,
760 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200761exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100762 /*
763 * Key attributes may have been returned by psa_get_key_attributes()
764 * thus reset them as required.
765 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200766 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100767
768 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200769 return( status );
770}
771
Ronald Cron5425a212020-08-04 14:58:35 +0200772static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200773 psa_key_usage_t usage,
774 psa_algorithm_t alg )
775{
776 int ok = 0;
777
778 if( usage & PSA_KEY_USAGE_DERIVE )
779 {
780 /* We need two keys to exercise key agreement. Exercise the
781 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200782 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200783 }
784 ok = 1;
785
786exit:
787 return( ok );
788}
789
Ronald Cron5425a212020-08-04 14:58:35 +0200790static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200791 psa_key_usage_t usage,
792 psa_algorithm_t alg )
793{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200794 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200795 unsigned char output[1];
796 int ok = 0;
797
798 if( usage & PSA_KEY_USAGE_DERIVE )
799 {
800 /* We need two keys to exercise key agreement. Exercise the
801 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200802 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200803 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200804 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200805 output,
806 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200807 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200808 }
809 ok = 1;
810
811exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200812 return( ok );
813}
814
Jaeden Amerof7dca862019-06-27 17:31:33 +0100815int asn1_skip_integer( unsigned char **p, const unsigned char *end,
816 size_t min_bits, size_t max_bits,
817 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200818{
819 size_t len;
820 size_t actual_bits;
821 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100822 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100823 MBEDTLS_ASN1_INTEGER ),
824 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200825
826 /* Check if the retrieved length doesn't extend the actual buffer's size.
827 * It is assumed here, that end >= p, which validates casting to size_t. */
828 TEST_ASSERT( len <= (size_t)( end - *p) );
829
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200830 /* Tolerate a slight departure from DER encoding:
831 * - 0 may be represented by an empty string or a 1-byte string.
832 * - The sign bit may be used as a value bit. */
833 if( ( len == 1 && ( *p )[0] == 0 ) ||
834 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
835 {
836 ++( *p );
837 --len;
838 }
839 if( min_bits == 0 && len == 0 )
840 return( 1 );
841 msb = ( *p )[0];
842 TEST_ASSERT( msb != 0 );
843 actual_bits = 8 * ( len - 1 );
844 while( msb != 0 )
845 {
846 msb >>= 1;
847 ++actual_bits;
848 }
849 TEST_ASSERT( actual_bits >= min_bits );
850 TEST_ASSERT( actual_bits <= max_bits );
851 if( must_be_odd )
852 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
853 *p += len;
854 return( 1 );
855exit:
856 return( 0 );
857}
858
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200859static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
860 uint8_t *exported, size_t exported_length )
861{
862 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100863 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200864 else
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100865 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200866
867#if defined(MBEDTLS_DES_C)
868 if( type == PSA_KEY_TYPE_DES )
869 {
870 /* Check the parity bits. */
871 unsigned i;
872 for( i = 0; i < bits / 8; i++ )
873 {
874 unsigned bit_count = 0;
875 unsigned m;
876 for( m = 1; m <= 0x100; m <<= 1 )
877 {
878 if( exported[i] & m )
879 ++bit_count;
880 }
881 TEST_ASSERT( bit_count % 2 != 0 );
882 }
883 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200884 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200885#endif
886
887#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200888 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200889 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200890 uint8_t *p = exported;
891 uint8_t *end = exported + exported_length;
892 size_t len;
893 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200894 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200895 * modulus INTEGER, -- n
896 * publicExponent INTEGER, -- e
897 * privateExponent INTEGER, -- d
898 * prime1 INTEGER, -- p
899 * prime2 INTEGER, -- q
900 * exponent1 INTEGER, -- d mod (p-1)
901 * exponent2 INTEGER, -- d mod (q-1)
902 * coefficient INTEGER, -- (inverse of q) mod p
903 * }
904 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100905 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
906 MBEDTLS_ASN1_SEQUENCE |
907 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
908 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200909 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
910 goto exit;
911 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
912 goto exit;
913 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
914 goto exit;
915 /* Require d to be at least half the size of n. */
916 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
917 goto exit;
918 /* Require p and q to be at most half the size of n, rounded up. */
919 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
920 goto exit;
921 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
922 goto exit;
923 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
924 goto exit;
925 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
926 goto exit;
927 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
928 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100929 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100930 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200931 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200932#endif /* MBEDTLS_RSA_C */
933
934#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200935 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200936 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100937 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100938 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100939 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200940 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200941#endif /* MBEDTLS_ECP_C */
942
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200943 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
944 {
945 uint8_t *p = exported;
946 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200947#if defined(MBEDTLS_RSA_C)
948 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
949 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100950 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200951 /* RSAPublicKey ::= SEQUENCE {
952 * modulus INTEGER, -- n
953 * publicExponent INTEGER } -- e
954 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100955 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
956 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100957 MBEDTLS_ASN1_CONSTRUCTED ),
958 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100959 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200960 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
961 goto exit;
962 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
963 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100964 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200965 }
966 else
967#endif /* MBEDTLS_RSA_C */
968#if defined(MBEDTLS_ECP_C)
969 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
970 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200971 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
972 {
973 /* The representation of an ECC Montgomery public key is
974 * the raw compressed point */
975 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
976 }
977 else
978 {
979 /* The representation of an ECC Weierstrass public key is:
980 * - The byte 0x04;
981 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
982 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
983 * - where m is the bit size associated with the curve.
984 */
985 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
986 TEST_EQUAL( p[0], 4 );
987 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200988 }
989 else
990#endif /* MBEDTLS_ECP_C */
991 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100992 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200993 mbedtls_snprintf( message, sizeof( message ),
994 "No sanity check for public key type=0x%08lx",
995 (unsigned long) type );
Chris Jones9634bb12021-01-20 15:56:42 +0000996 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200997 (void) p;
998 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200999 return( 0 );
1000 }
1001 }
1002 else
1003
1004 {
1005 /* No sanity checks for other types */
1006 }
1007
1008 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001009
1010exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001011 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001012}
1013
Ronald Cron5425a212020-08-04 14:58:35 +02001014static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001015 psa_key_usage_t usage )
1016{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001018 uint8_t *exported = NULL;
1019 size_t exported_size = 0;
1020 size_t exported_length = 0;
1021 int ok = 0;
1022
Ronald Cron5425a212020-08-04 14:58:35 +02001023 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001024
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001025 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1026 psa_get_key_type( &attributes ),
1027 psa_get_key_bits( &attributes ) );
1028 ASSERT_ALLOC( exported, exported_size );
1029
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001030 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001031 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001032 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001033 TEST_EQUAL( psa_export_key( key, exported,
1034 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001035 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001036 ok = 1;
1037 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001038 }
1039
Ronald Cron5425a212020-08-04 14:58:35 +02001040 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001041 exported, exported_size,
1042 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001043 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1044 psa_get_key_bits( &attributes ),
1045 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001046
1047exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001048 /*
1049 * Key attributes may have been returned by psa_get_key_attributes()
1050 * thus reset them as required.
1051 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001052 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001053
1054 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001055 return( ok );
1056}
1057
Ronald Cron5425a212020-08-04 14:58:35 +02001058static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001059{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001061 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001062 uint8_t *exported = NULL;
1063 size_t exported_size = 0;
1064 size_t exported_length = 0;
1065 int ok = 0;
1066
Ronald Cron5425a212020-08-04 14:58:35 +02001067 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001068 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001069 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001070 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1071 psa_get_key_type( &attributes ),
1072 psa_get_key_bits( &attributes ) );
1073 ASSERT_ALLOC( exported, exported_size );
1074
1075 TEST_EQUAL( psa_export_public_key( key, exported,
1076 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001077 PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001078 ok = 1;
1079 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001080 }
1081
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001082 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001083 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001084 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1085 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001086 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001087
Ronald Cron5425a212020-08-04 14:58:35 +02001088 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001089 exported, exported_size,
1090 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001091 ok = exported_key_sanity_check( public_type,
1092 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001093 exported, exported_length );
1094
1095exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001096 /*
1097 * Key attributes may have been returned by psa_get_key_attributes()
1098 * thus reset them as required.
1099 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001100 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001101
1102 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001103 return( ok );
1104}
1105
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001106/** Do smoke tests on a key.
1107 *
1108 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1109 * sign/verify, or derivation) that is permitted according to \p usage.
1110 * \p usage and \p alg should correspond to the expected policy on the
1111 * key.
1112 *
1113 * Export the key if permitted by \p usage, and check that the output
1114 * looks sensible. If \p usage forbids export, check that
1115 * \p psa_export_key correctly rejects the attempt. If the key is
1116 * asymmetric, also check \p psa_export_public_key.
1117 *
1118 * If the key fails the tests, this function calls the test framework's
Chris Jones9634bb12021-01-20 15:56:42 +00001119 * `mbedtls_test_fail` function and returns false. Otherwise this function
1120 * returns true. Therefore it should be used as follows:
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001121 * ```
1122 * if( ! exercise_key( ... ) ) goto exit;
1123 * ```
1124 *
Ronald Cron5425a212020-08-04 14:58:35 +02001125 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001126 * \p alg.
1127 * \param usage The usage flags to assume.
1128 * \param alg The algorithm to exercise.
1129 *
1130 * \retval 0 The key failed the smoke tests.
1131 * \retval 1 The key passed the smoke tests.
1132 */
Ronald Cron5425a212020-08-04 14:58:35 +02001133static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001134 psa_key_usage_t usage,
1135 psa_algorithm_t alg )
1136{
1137 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001138
Ronald Cron5425a212020-08-04 14:58:35 +02001139 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001140 return( 0 );
1141
Gilles Peskine02b75072018-07-01 22:31:34 +02001142 if( alg == 0 )
1143 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1144 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001145 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001146 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001147 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001148 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001149 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001150 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001151 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001152 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001153 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001154 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001155 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001156 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001157 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001158 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001159 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001160 else
1161 {
1162 char message[40];
1163 mbedtls_snprintf( message, sizeof( message ),
1164 "No code to exercise alg=0x%08lx",
1165 (unsigned long) alg );
Chris Jones9634bb12021-01-20 15:56:42 +00001166 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskine02b75072018-07-01 22:31:34 +02001167 ok = 0;
1168 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001169
Ronald Cron5425a212020-08-04 14:58:35 +02001170 ok = ok && exercise_export_key( key, usage );
1171 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001172
Gilles Peskine02b75072018-07-01 22:31:34 +02001173 return( ok );
1174}
1175
Gilles Peskine10df3412018-10-25 22:35:43 +02001176static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1177 psa_algorithm_t alg )
1178{
1179 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1180 {
1181 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001182 PSA_KEY_USAGE_VERIFY_HASH :
1183 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001184 }
1185 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1186 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1187 {
1188 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1189 PSA_KEY_USAGE_ENCRYPT :
1190 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1191 }
1192 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1193 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1194 {
1195 return( PSA_KEY_USAGE_DERIVE );
1196 }
1197 else
1198 {
1199 return( 0 );
1200 }
1201
1202}
Darryl Green0c6575a2018-11-07 16:05:30 +00001203
Ronald Cron5425a212020-08-04 14:58:35 +02001204static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001205{
1206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001207 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001208 uint8_t buffer[1];
1209 size_t length;
1210 int ok = 0;
1211
Ronald Cronecfb2372020-07-23 17:13:42 +02001212 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001213 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1214 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1215 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001216 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001217 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001218 TEST_EQUAL(
1219 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1220 TEST_EQUAL(
1221 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001222 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001223 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1224 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1225 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1226 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1227
Ronald Cron5425a212020-08-04 14:58:35 +02001228 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001229 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001230 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001231 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001232 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001233
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001234 ok = 1;
1235
1236exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001237 /*
1238 * Key attributes may have been returned by psa_get_key_attributes()
1239 * thus reset them as required.
1240 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001241 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001242
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001243 return( ok );
1244}
1245
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001246/* Assert that a key isn't reported as having a slot number. */
1247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1248#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1249 do \
1250 { \
1251 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1252 TEST_EQUAL( psa_get_key_slot_number( \
1253 attributes, \
1254 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1255 PSA_ERROR_INVALID_ARGUMENT ); \
1256 } \
1257 while( 0 )
1258#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1259#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1260 ( (void) 0 )
1261#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1262
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001263/* An overapproximation of the amount of storage needed for a key of the
1264 * given type and with the given content. The API doesn't make it easy
1265 * to find a good value for the size. The current implementation doesn't
1266 * care about the value anyway. */
1267#define KEY_BITS_FROM_DATA( type, data ) \
1268 ( data )->len
1269
Darryl Green0c6575a2018-11-07 16:05:30 +00001270typedef enum {
1271 IMPORT_KEY = 0,
1272 GENERATE_KEY = 1,
1273 DERIVE_KEY = 2
1274} generate_method;
1275
Gilles Peskinee59236f2018-01-27 23:32:46 +01001276/* END_HEADER */
1277
1278/* BEGIN_DEPENDENCIES
1279 * depends_on:MBEDTLS_PSA_CRYPTO_C
1280 * END_DEPENDENCIES
1281 */
1282
1283/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001284void static_checks( )
1285{
1286 size_t max_truncated_mac_size =
1287 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1288
1289 /* Check that the length for a truncated MAC always fits in the algorithm
1290 * encoding. The shifted mask is the maximum truncated value. The
1291 * untruncated algorithm may be one byte larger. */
1292 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001293
1294#if defined(MBEDTLS_TEST_DEPRECATED)
1295 /* Check deprecated constants. */
1296 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1297 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1298 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1299 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1300 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1301 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1302 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1303 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001304
Paul Elliott8ff510a2020-06-02 17:19:28 +01001305 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1306 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1308 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1309 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1310 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1311 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1312 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1314 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1326 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1328 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1329 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1330 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1331 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1332 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1333 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1334 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1335
1336 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1337 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1338 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1339 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1340 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1341 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1342 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1343 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001344
Paul Elliott75e27032020-06-03 15:17:39 +01001345 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1346 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1347 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1348 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1349 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1350
1351 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1352 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001353#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001354}
1355/* END_CASE */
1356
1357/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001358void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001359 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001360 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001361{
Gilles Peskine4747d192019-04-17 15:05:45 +02001362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001363 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001364 psa_key_lifetime_t lifetime = lifetime_arg;
1365 psa_key_usage_t usage_flags = usage_flags_arg;
1366 psa_algorithm_t alg = alg_arg;
1367 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001368 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001369
Ronald Cronecfb2372020-07-23 17:13:42 +02001370 TEST_EQUAL(
1371 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1372 TEST_EQUAL(
1373 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001374 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1375 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1376 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1377 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001378 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001379
Gilles Peskinec87af662019-05-15 16:12:22 +02001380 psa_set_key_id( &attributes, id );
1381 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001382 psa_set_key_usage_flags( &attributes, usage_flags );
1383 psa_set_key_algorithm( &attributes, alg );
1384 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001385 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001386
Ronald Cronecfb2372020-07-23 17:13:42 +02001387 TEST_ASSERT( mbedtls_svc_key_id_equal(
1388 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001389 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1390 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1391 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1392 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001393 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001394
1395 psa_reset_key_attributes( &attributes );
1396
Ronald Cronecfb2372020-07-23 17:13:42 +02001397 TEST_EQUAL(
1398 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1399 TEST_EQUAL(
1400 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001401 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1402 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1403 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1404 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001405 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001406}
1407/* END_CASE */
1408
1409/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001410void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1411 int id2_arg, int owner_id2_arg,
1412 int expected_id_arg, int expected_owner_id_arg,
1413 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001414{
1415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001416 mbedtls_svc_key_id_t id1 =
1417 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001418 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001419 mbedtls_svc_key_id_t id2 =
1420 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001421 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001422 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001423 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1424
1425 if( id1_arg != -1 )
1426 psa_set_key_id( &attributes, id1 );
1427 if( lifetime_arg != -1 )
1428 psa_set_key_lifetime( &attributes, lifetime );
1429 if( id2_arg != -1 )
1430 psa_set_key_id( &attributes, id2 );
1431
Ronald Cronecfb2372020-07-23 17:13:42 +02001432 TEST_ASSERT( mbedtls_svc_key_id_equal(
1433 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001434 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1435}
1436/* END_CASE */
1437
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001438/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1439void slot_number_attribute( )
1440{
1441 psa_key_slot_number_t slot_number = 0xdeadbeef;
1442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1443
1444 /* Initially, there is no slot number. */
1445 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1446 PSA_ERROR_INVALID_ARGUMENT );
1447
1448 /* Test setting a slot number. */
1449 psa_set_key_slot_number( &attributes, 0 );
1450 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1451 TEST_EQUAL( slot_number, 0 );
1452
1453 /* Test changing the slot number. */
1454 psa_set_key_slot_number( &attributes, 42 );
1455 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1456 TEST_EQUAL( slot_number, 42 );
1457
1458 /* Test clearing the slot number. */
1459 psa_clear_key_slot_number( &attributes );
1460 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1461 PSA_ERROR_INVALID_ARGUMENT );
1462
1463 /* Clearing again should have no effect. */
1464 psa_clear_key_slot_number( &attributes );
1465 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1466 PSA_ERROR_INVALID_ARGUMENT );
1467
1468 /* Test that reset clears the slot number. */
1469 psa_set_key_slot_number( &attributes, 42 );
1470 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1471 TEST_EQUAL( slot_number, 42 );
1472 psa_reset_key_attributes( &attributes );
1473 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1474 PSA_ERROR_INVALID_ARGUMENT );
1475}
1476/* END_CASE */
1477
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001478/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001479void import_with_policy( int type_arg,
1480 int usage_arg, int alg_arg,
1481 int expected_status_arg )
1482{
1483 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1484 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001486 psa_key_type_t type = type_arg;
1487 psa_key_usage_t usage = usage_arg;
1488 psa_algorithm_t alg = alg_arg;
1489 psa_status_t expected_status = expected_status_arg;
1490 const uint8_t key_material[16] = {0};
1491 psa_status_t status;
1492
1493 PSA_ASSERT( psa_crypto_init( ) );
1494
1495 psa_set_key_type( &attributes, type );
1496 psa_set_key_usage_flags( &attributes, usage );
1497 psa_set_key_algorithm( &attributes, alg );
1498
1499 status = psa_import_key( &attributes,
1500 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001501 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001502 TEST_EQUAL( status, expected_status );
1503 if( status != PSA_SUCCESS )
1504 goto exit;
1505
Ronald Cron5425a212020-08-04 14:58:35 +02001506 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001507 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1508 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1509 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001510 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001511
Ronald Cron5425a212020-08-04 14:58:35 +02001512 PSA_ASSERT( psa_destroy_key( key ) );
1513 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001514
1515exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001516 /*
1517 * Key attributes may have been returned by psa_get_key_attributes()
1518 * thus reset them as required.
1519 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001520 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001521
1522 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001523 PSA_DONE( );
1524}
1525/* END_CASE */
1526
1527/* BEGIN_CASE */
1528void import_with_data( data_t *data, int type_arg,
1529 int attr_bits_arg,
1530 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001531{
1532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1533 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001534 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001535 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001536 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001537 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001539
Gilles Peskine8817f612018-12-18 00:18:46 +01001540 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001541
Gilles Peskine4747d192019-04-17 15:05:45 +02001542 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001543 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001544
Ronald Cron5425a212020-08-04 14:58:35 +02001545 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001546 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001547 if( status != PSA_SUCCESS )
1548 goto exit;
1549
Ronald Cron5425a212020-08-04 14:58:35 +02001550 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001551 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001552 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001553 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001554 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001555
Ronald Cron5425a212020-08-04 14:58:35 +02001556 PSA_ASSERT( psa_destroy_key( key ) );
1557 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001558
1559exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001560 /*
1561 * Key attributes may have been returned by psa_get_key_attributes()
1562 * thus reset them as required.
1563 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001564 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001565
1566 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001567 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568}
1569/* END_CASE */
1570
1571/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001572void import_large_key( int type_arg, int byte_size_arg,
1573 int expected_status_arg )
1574{
1575 psa_key_type_t type = type_arg;
1576 size_t byte_size = byte_size_arg;
1577 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1578 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001579 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001580 psa_status_t status;
1581 uint8_t *buffer = NULL;
1582 size_t buffer_size = byte_size + 1;
1583 size_t n;
1584
Steven Cooreman69967ce2021-01-18 18:01:08 +01001585 /* Skip the test case if the target running the test cannot
1586 * accomodate large keys due to heap size constraints */
1587 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001588 memset( buffer, 'K', byte_size );
1589
1590 PSA_ASSERT( psa_crypto_init( ) );
1591
1592 /* Try importing the key */
1593 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1594 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001595 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001596 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001597 TEST_EQUAL( status, expected_status );
1598
1599 if( status == PSA_SUCCESS )
1600 {
Ronald Cron5425a212020-08-04 14:58:35 +02001601 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001602 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1603 TEST_EQUAL( psa_get_key_bits( &attributes ),
1604 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001605 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001606 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001607 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001608 for( n = 0; n < byte_size; n++ )
1609 TEST_EQUAL( buffer[n], 'K' );
1610 for( n = byte_size; n < buffer_size; n++ )
1611 TEST_EQUAL( buffer[n], 0 );
1612 }
1613
1614exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001615 /*
1616 * Key attributes may have been returned by psa_get_key_attributes()
1617 * thus reset them as required.
1618 */
1619 psa_reset_key_attributes( &attributes );
1620
Ronald Cron5425a212020-08-04 14:58:35 +02001621 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001622 PSA_DONE( );
1623 mbedtls_free( buffer );
1624}
1625/* END_CASE */
1626
1627/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001628void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1629{
Ronald Cron5425a212020-08-04 14:58:35 +02001630 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001631 size_t bits = bits_arg;
1632 psa_status_t expected_status = expected_status_arg;
1633 psa_status_t status;
1634 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001635 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001636 size_t buffer_size = /* Slight overapproximations */
1637 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001638 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001639 unsigned char *p;
1640 int ret;
1641 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001643
Gilles Peskine8817f612018-12-18 00:18:46 +01001644 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001645 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001646
1647 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1648 bits, keypair ) ) >= 0 );
1649 length = ret;
1650
1651 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001652 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001653 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001654 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001655
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001656 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001657 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001658
1659exit:
1660 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001661 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001662}
1663/* END_CASE */
1664
1665/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001666void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001667 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001668 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001669 int expected_bits,
1670 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001671 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001672 int canonical_input )
1673{
Ronald Cron5425a212020-08-04 14:58:35 +02001674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001675 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001676 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001677 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001678 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001679 unsigned char *exported = NULL;
1680 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001681 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001682 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001683 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001685 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001686
Moran Pekercb088e72018-07-17 17:36:59 +03001687 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001688 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001689 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001690 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001691 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001692
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 psa_set_key_usage_flags( &attributes, usage_arg );
1694 psa_set_key_algorithm( &attributes, alg );
1695 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001696
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001697 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001698 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001699
1700 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001701 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001702 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1703 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001704 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001705
1706 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001707 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001708 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001709
1710 /* The exported length must be set by psa_export_key() to a value between 0
1711 * and export_size. On errors, the exported length must be 0. */
1712 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1713 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1714 TEST_ASSERT( exported_length <= export_size );
1715
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001716 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001717 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001718 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001719 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001720 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001721 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001722 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001723
Ronald Cron5425a212020-08-04 14:58:35 +02001724 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001725 goto exit;
1726
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001727 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001728 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001729 else
1730 {
Ronald Cron5425a212020-08-04 14:58:35 +02001731 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001732 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001733 &key2 ) );
1734 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001735 reexported,
1736 export_size,
1737 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001738 ASSERT_COMPARE( exported, exported_length,
1739 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001740 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001741 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001742 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001743
1744destroy:
1745 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001746 PSA_ASSERT( psa_destroy_key( key ) );
1747 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001748
1749exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001750 /*
1751 * Key attributes may have been returned by psa_get_key_attributes()
1752 * thus reset them as required.
1753 */
1754 psa_reset_key_attributes( &got_attributes );
1755
itayzafrir3e02b3b2018-06-12 17:06:52 +03001756 mbedtls_free( exported );
1757 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001758 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001759}
1760/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001761
Moran Pekerf709f4a2018-06-06 17:26:04 +03001762/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001763void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001764 int type_arg,
1765 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001766 int export_size_delta,
1767 int expected_export_status_arg,
1768 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001769{
Ronald Cron5425a212020-08-04 14:58:35 +02001770 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001771 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001772 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001773 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001774 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001775 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001776 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001777 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001779
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001781
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1783 psa_set_key_algorithm( &attributes, alg );
1784 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001785
1786 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001787 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001788
Gilles Peskine49c25912018-10-29 15:15:31 +01001789 /* Export the public key */
1790 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001791 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001792 exported, export_size,
1793 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001794 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001795 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001796 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001797 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001798 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001799 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001800 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001801 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001802 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001803 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1804 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001805 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001806
1807exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001808 /*
1809 * Key attributes may have been returned by psa_get_key_attributes()
1810 * thus reset them as required.
1811 */
1812 psa_reset_key_attributes( &attributes );
1813
itayzafrir3e02b3b2018-06-12 17:06:52 +03001814 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001815 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001816 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001817}
1818/* END_CASE */
1819
Gilles Peskine20035e32018-02-03 22:44:14 +01001820/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001821void import_and_exercise_key( data_t *data,
1822 int type_arg,
1823 int bits_arg,
1824 int alg_arg )
1825{
Ronald Cron5425a212020-08-04 14:58:35 +02001826 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001827 psa_key_type_t type = type_arg;
1828 size_t bits = bits_arg;
1829 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001830 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001831 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001832 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001833
Gilles Peskine8817f612018-12-18 00:18:46 +01001834 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001835
Gilles Peskine4747d192019-04-17 15:05:45 +02001836 psa_set_key_usage_flags( &attributes, usage );
1837 psa_set_key_algorithm( &attributes, alg );
1838 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001839
1840 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001841 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001842
1843 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001844 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001845 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1846 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001847
1848 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001849 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001850 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001851
Ronald Cron5425a212020-08-04 14:58:35 +02001852 PSA_ASSERT( psa_destroy_key( key ) );
1853 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001854
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001855exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001856 /*
1857 * Key attributes may have been returned by psa_get_key_attributes()
1858 * thus reset them as required.
1859 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001860 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001861
1862 psa_reset_key_attributes( &attributes );
1863 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001864 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001865}
1866/* END_CASE */
1867
1868/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001869void effective_key_attributes( int type_arg, int expected_type_arg,
1870 int bits_arg, int expected_bits_arg,
1871 int usage_arg, int expected_usage_arg,
1872 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001873{
Ronald Cron5425a212020-08-04 14:58:35 +02001874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001875 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001876 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001877 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001878 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001879 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001880 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001881 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001882 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001883 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001884
Gilles Peskine8817f612018-12-18 00:18:46 +01001885 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001886
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001887 psa_set_key_usage_flags( &attributes, usage );
1888 psa_set_key_algorithm( &attributes, alg );
1889 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001890 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001891
Ronald Cron5425a212020-08-04 14:58:35 +02001892 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001893 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001894
Ronald Cron5425a212020-08-04 14:58:35 +02001895 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001896 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1897 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1898 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1899 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001900
1901exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001902 /*
1903 * Key attributes may have been returned by psa_get_key_attributes()
1904 * thus reset them as required.
1905 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001906 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001907
1908 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001909 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001910}
1911/* END_CASE */
1912
1913/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001914void check_key_policy( int type_arg, int bits_arg,
1915 int usage_arg, int alg_arg )
1916{
1917 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1918 usage_arg, usage_arg, alg_arg, alg_arg );
1919 goto exit;
1920}
1921/* END_CASE */
1922
1923/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001924void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001925{
1926 /* Test each valid way of initializing the object, except for `= {0}`, as
1927 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1928 * though it's OK by the C standard. We could test for this, but we'd need
1929 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001930 psa_key_attributes_t func = psa_key_attributes_init( );
1931 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1932 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001933
1934 memset( &zero, 0, sizeof( zero ) );
1935
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001936 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1937 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1938 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001939
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001940 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1941 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1942 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1943
1944 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1945 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1946 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1947
1948 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1949 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1950 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1951
1952 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1953 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1954 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001955}
1956/* END_CASE */
1957
1958/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001959void mac_key_policy( int policy_usage,
1960 int policy_alg,
1961 int key_type,
1962 data_t *key_data,
1963 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001964{
Ronald Cron5425a212020-08-04 14:58:35 +02001965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001967 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001968 psa_status_t status;
1969 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001970
Gilles Peskine8817f612018-12-18 00:18:46 +01001971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001972
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001973 psa_set_key_usage_flags( &attributes, policy_usage );
1974 psa_set_key_algorithm( &attributes, policy_alg );
1975 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001976
Gilles Peskine049c7532019-05-15 20:22:09 +02001977 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001978 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001979
Ronald Cron5425a212020-08-04 14:58:35 +02001980 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001981 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001982 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001983 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001985 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001987
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001988 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001989 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001991 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001992 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001993 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001994 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995
1996exit:
1997 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001998 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001999 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002000}
2001/* END_CASE */
2002
2003/* BEGIN_CASE */
2004void cipher_key_policy( int policy_usage,
2005 int policy_alg,
2006 int key_type,
2007 data_t *key_data,
2008 int exercise_alg )
2009{
Ronald Cron5425a212020-08-04 14:58:35 +02002010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002012 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013 psa_status_t status;
2014
Gilles Peskine8817f612018-12-18 00:18:46 +01002015 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002016
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002017 psa_set_key_usage_flags( &attributes, policy_usage );
2018 psa_set_key_algorithm( &attributes, policy_alg );
2019 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020
Gilles Peskine049c7532019-05-15 20:22:09 +02002021 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002022 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023
Ronald Cron5425a212020-08-04 14:58:35 +02002024 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025 if( policy_alg == exercise_alg &&
2026 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002027 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002029 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030 psa_cipher_abort( &operation );
2031
Ronald Cron5425a212020-08-04 14:58:35 +02002032 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033 if( policy_alg == exercise_alg &&
2034 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002035 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002036 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002037 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038
2039exit:
2040 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002041 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002042 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043}
2044/* END_CASE */
2045
2046/* BEGIN_CASE */
2047void aead_key_policy( int policy_usage,
2048 int policy_alg,
2049 int key_type,
2050 data_t *key_data,
2051 int nonce_length_arg,
2052 int tag_length_arg,
2053 int exercise_alg )
2054{
Ronald Cron5425a212020-08-04 14:58:35 +02002055 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002057 psa_status_t status;
2058 unsigned char nonce[16] = {0};
2059 size_t nonce_length = nonce_length_arg;
2060 unsigned char tag[16];
2061 size_t tag_length = tag_length_arg;
2062 size_t output_length;
2063
2064 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
2065 TEST_ASSERT( tag_length <= sizeof( tag ) );
2066
Gilles Peskine8817f612018-12-18 00:18:46 +01002067 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002068
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002069 psa_set_key_usage_flags( &attributes, policy_usage );
2070 psa_set_key_algorithm( &attributes, policy_alg );
2071 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002072
Gilles Peskine049c7532019-05-15 20:22:09 +02002073 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002074 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075
Ronald Cron5425a212020-08-04 14:58:35 +02002076 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077 nonce, nonce_length,
2078 NULL, 0,
2079 NULL, 0,
2080 tag, tag_length,
2081 &output_length );
2082 if( policy_alg == exercise_alg &&
2083 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002084 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002085 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002086 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002087
2088 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002089 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090 nonce, nonce_length,
2091 NULL, 0,
2092 tag, tag_length,
2093 NULL, 0,
2094 &output_length );
2095 if( policy_alg == exercise_alg &&
2096 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002097 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002098 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002099 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100
2101exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002102 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002103 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002104}
2105/* END_CASE */
2106
2107/* BEGIN_CASE */
2108void asymmetric_encryption_key_policy( int policy_usage,
2109 int policy_alg,
2110 int key_type,
2111 data_t *key_data,
2112 int exercise_alg )
2113{
Ronald Cron5425a212020-08-04 14:58:35 +02002114 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002116 psa_status_t status;
2117 size_t key_bits;
2118 size_t buffer_length;
2119 unsigned char *buffer = NULL;
2120 size_t output_length;
2121
Gilles Peskine8817f612018-12-18 00:18:46 +01002122 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002124 psa_set_key_usage_flags( &attributes, policy_usage );
2125 psa_set_key_algorithm( &attributes, policy_alg );
2126 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002127
Gilles Peskine049c7532019-05-15 20:22:09 +02002128 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002129 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002130
Ronald Cron5425a212020-08-04 14:58:35 +02002131 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002132 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002133 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2134 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002135 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002136
Ronald Cron5425a212020-08-04 14:58:35 +02002137 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002138 NULL, 0,
2139 NULL, 0,
2140 buffer, buffer_length,
2141 &output_length );
2142 if( policy_alg == exercise_alg &&
2143 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002144 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002146 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002147
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002148 if( buffer_length != 0 )
2149 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002150 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002151 buffer, buffer_length,
2152 NULL, 0,
2153 buffer, buffer_length,
2154 &output_length );
2155 if( policy_alg == exercise_alg &&
2156 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002157 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002158 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002159 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002160
2161exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002162 /*
2163 * Key attributes may have been returned by psa_get_key_attributes()
2164 * thus reset them as required.
2165 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002166 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002167
2168 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002169 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002170 mbedtls_free( buffer );
2171}
2172/* END_CASE */
2173
2174/* BEGIN_CASE */
2175void asymmetric_signature_key_policy( int policy_usage,
2176 int policy_alg,
2177 int key_type,
2178 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002179 int exercise_alg,
2180 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002181{
Ronald Cron5425a212020-08-04 14:58:35 +02002182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002184 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002185 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2186 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2187 * compatible with the policy and `payload_length_arg` is supposed to be
2188 * a valid input length to sign. If `payload_length_arg <= 0`,
2189 * `exercise_alg` is supposed to be forbidden by the policy. */
2190 int compatible_alg = payload_length_arg > 0;
2191 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002192 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002193 size_t signature_length;
2194
Gilles Peskine8817f612018-12-18 00:18:46 +01002195 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002196
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002197 psa_set_key_usage_flags( &attributes, policy_usage );
2198 psa_set_key_algorithm( &attributes, policy_alg );
2199 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002200
Gilles Peskine049c7532019-05-15 20:22:09 +02002201 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002202 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002203
Ronald Cron5425a212020-08-04 14:58:35 +02002204 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002205 payload, payload_length,
2206 signature, sizeof( signature ),
2207 &signature_length );
2208 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002209 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002210 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002211 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002212
2213 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002214 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002215 payload, payload_length,
2216 signature, sizeof( signature ) );
2217 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002218 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002219 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002220 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002221
2222exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002223 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002224 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002225}
2226/* END_CASE */
2227
Janos Follathba3fab92019-06-11 14:50:16 +01002228/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002229void derive_key_policy( int policy_usage,
2230 int policy_alg,
2231 int key_type,
2232 data_t *key_data,
2233 int exercise_alg )
2234{
Ronald Cron5425a212020-08-04 14:58:35 +02002235 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002236 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002237 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002238 psa_status_t status;
2239
Gilles Peskine8817f612018-12-18 00:18:46 +01002240 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002241
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002242 psa_set_key_usage_flags( &attributes, policy_usage );
2243 psa_set_key_algorithm( &attributes, policy_alg );
2244 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002245
Gilles Peskine049c7532019-05-15 20:22:09 +02002246 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002247 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002248
Janos Follathba3fab92019-06-11 14:50:16 +01002249 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2250
2251 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2252 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002253 {
Janos Follathba3fab92019-06-11 14:50:16 +01002254 PSA_ASSERT( psa_key_derivation_input_bytes(
2255 &operation,
2256 PSA_KEY_DERIVATION_INPUT_SEED,
2257 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002258 }
Janos Follathba3fab92019-06-11 14:50:16 +01002259
2260 status = psa_key_derivation_input_key( &operation,
2261 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002262 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002263
Gilles Peskineea0fb492018-07-12 17:17:20 +02002264 if( policy_alg == exercise_alg &&
2265 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002266 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002267 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002268 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002269
2270exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002271 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002272 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002273 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002274}
2275/* END_CASE */
2276
2277/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002278void agreement_key_policy( int policy_usage,
2279 int policy_alg,
2280 int key_type_arg,
2281 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002282 int exercise_alg,
2283 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002284{
Ronald Cron5425a212020-08-04 14:58:35 +02002285 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002287 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002288 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002289 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002290 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002291
Gilles Peskine8817f612018-12-18 00:18:46 +01002292 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002293
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002294 psa_set_key_usage_flags( &attributes, policy_usage );
2295 psa_set_key_algorithm( &attributes, policy_alg );
2296 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002297
Gilles Peskine049c7532019-05-15 20:22:09 +02002298 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002299 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002300
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002301 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002302 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002303
Steven Cooremance48e852020-10-05 16:02:45 +02002304 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002305
2306exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002307 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002308 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002309 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002310}
2311/* END_CASE */
2312
2313/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002314void key_policy_alg2( int key_type_arg, data_t *key_data,
2315 int usage_arg, int alg_arg, int alg2_arg )
2316{
Ronald Cron5425a212020-08-04 14:58:35 +02002317 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002318 psa_key_type_t key_type = key_type_arg;
2319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2320 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2321 psa_key_usage_t usage = usage_arg;
2322 psa_algorithm_t alg = alg_arg;
2323 psa_algorithm_t alg2 = alg2_arg;
2324
2325 PSA_ASSERT( psa_crypto_init( ) );
2326
2327 psa_set_key_usage_flags( &attributes, usage );
2328 psa_set_key_algorithm( &attributes, alg );
2329 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2330 psa_set_key_type( &attributes, key_type );
2331 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002332 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002333
Ronald Cron5425a212020-08-04 14:58:35 +02002334 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002335 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2336 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2337 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2338
Ronald Cron5425a212020-08-04 14:58:35 +02002339 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002340 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002341 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002342 goto exit;
2343
2344exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002345 /*
2346 * Key attributes may have been returned by psa_get_key_attributes()
2347 * thus reset them as required.
2348 */
2349 psa_reset_key_attributes( &got_attributes );
2350
Ronald Cron5425a212020-08-04 14:58:35 +02002351 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002352 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002353}
2354/* END_CASE */
2355
2356/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002357void raw_agreement_key_policy( int policy_usage,
2358 int policy_alg,
2359 int key_type_arg,
2360 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002361 int exercise_alg,
2362 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002363{
Ronald Cron5425a212020-08-04 14:58:35 +02002364 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002366 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002367 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002368 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002369 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002370
2371 PSA_ASSERT( psa_crypto_init( ) );
2372
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002373 psa_set_key_usage_flags( &attributes, policy_usage );
2374 psa_set_key_algorithm( &attributes, policy_alg );
2375 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002376
Gilles Peskine049c7532019-05-15 20:22:09 +02002377 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002378 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002379
Ronald Cron5425a212020-08-04 14:58:35 +02002380 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002381
Steven Cooremance48e852020-10-05 16:02:45 +02002382 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002383
2384exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002385 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002386 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002387 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002388}
2389/* END_CASE */
2390
2391/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002392void copy_success( int source_usage_arg,
2393 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002394 int type_arg, data_t *material,
2395 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002396 int target_usage_arg,
2397 int target_alg_arg, int target_alg2_arg,
2398 int expected_usage_arg,
2399 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002400{
Gilles Peskineca25db92019-04-19 11:43:08 +02002401 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2402 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002403 psa_key_usage_t expected_usage = expected_usage_arg;
2404 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002405 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002406 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2407 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002408 uint8_t *export_buffer = NULL;
2409
Gilles Peskine57ab7212019-01-28 13:03:09 +01002410 PSA_ASSERT( psa_crypto_init( ) );
2411
Gilles Peskineca25db92019-04-19 11:43:08 +02002412 /* Prepare the source key. */
2413 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2414 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002415 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002416 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002417 PSA_ASSERT( psa_import_key( &source_attributes,
2418 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002419 &source_key ) );
2420 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002421
Gilles Peskineca25db92019-04-19 11:43:08 +02002422 /* Prepare the target attributes. */
2423 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002424 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002425 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002426 /* Set volatile lifetime to reset the key identifier to 0. */
2427 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2428 }
2429
Gilles Peskineca25db92019-04-19 11:43:08 +02002430 if( target_usage_arg != -1 )
2431 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2432 if( target_alg_arg != -1 )
2433 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002434 if( target_alg2_arg != -1 )
2435 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002436
2437 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002438 PSA_ASSERT( psa_copy_key( source_key,
2439 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002440
2441 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002442 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002443
2444 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002445 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002446 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2447 psa_get_key_type( &target_attributes ) );
2448 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2449 psa_get_key_bits( &target_attributes ) );
2450 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2451 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002452 TEST_EQUAL( expected_alg2,
2453 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002454 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2455 {
2456 size_t length;
2457 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002458 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002459 material->len, &length ) );
2460 ASSERT_COMPARE( material->x, material->len,
2461 export_buffer, length );
2462 }
Ronald Cron5425a212020-08-04 14:58:35 +02002463 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002464 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002465 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002466 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002467
Ronald Cron5425a212020-08-04 14:58:35 +02002468 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002469
2470exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002471 /*
2472 * Source and target key attributes may have been returned by
2473 * psa_get_key_attributes() thus reset them as required.
2474 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002475 psa_reset_key_attributes( &source_attributes );
2476 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002477
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002478 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002479 mbedtls_free( export_buffer );
2480}
2481/* END_CASE */
2482
2483/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002484void copy_fail( int source_usage_arg,
2485 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002486 int type_arg, data_t *material,
2487 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002488 int target_usage_arg,
2489 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002490 int expected_status_arg )
2491{
2492 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2493 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002494 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2495 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002496
2497 PSA_ASSERT( psa_crypto_init( ) );
2498
2499 /* Prepare the source key. */
2500 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2501 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002502 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002503 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002504 PSA_ASSERT( psa_import_key( &source_attributes,
2505 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002506 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002507
2508 /* Prepare the target attributes. */
2509 psa_set_key_type( &target_attributes, target_type_arg );
2510 psa_set_key_bits( &target_attributes, target_bits_arg );
2511 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2512 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002513 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002514
2515 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002516 TEST_EQUAL( psa_copy_key( source_key,
2517 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002518 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002519
Ronald Cron5425a212020-08-04 14:58:35 +02002520 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002521
Gilles Peskine4a644642019-05-03 17:14:08 +02002522exit:
2523 psa_reset_key_attributes( &source_attributes );
2524 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002525 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002526}
2527/* END_CASE */
2528
2529/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002530void hash_operation_init( )
2531{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002532 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002533 /* Test each valid way of initializing the object, except for `= {0}`, as
2534 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2535 * though it's OK by the C standard. We could test for this, but we'd need
2536 * to supress the Clang warning for the test. */
2537 psa_hash_operation_t func = psa_hash_operation_init( );
2538 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2539 psa_hash_operation_t zero;
2540
2541 memset( &zero, 0, sizeof( zero ) );
2542
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002543 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002544 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2545 PSA_ERROR_BAD_STATE );
2546 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2547 PSA_ERROR_BAD_STATE );
2548 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2549 PSA_ERROR_BAD_STATE );
2550
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002551 /* A default hash operation should be abortable without error. */
2552 PSA_ASSERT( psa_hash_abort( &func ) );
2553 PSA_ASSERT( psa_hash_abort( &init ) );
2554 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002555}
2556/* END_CASE */
2557
2558/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002559void hash_setup( int alg_arg,
2560 int expected_status_arg )
2561{
2562 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002563 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002564 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002565 psa_status_t status;
2566
Gilles Peskine8817f612018-12-18 00:18:46 +01002567 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002568
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002569 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002570 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002571
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002572 /* Whether setup succeeded or failed, abort must succeed. */
2573 PSA_ASSERT( psa_hash_abort( &operation ) );
2574
2575 /* If setup failed, reproduce the failure, so as to
2576 * test the resulting state of the operation object. */
2577 if( status != PSA_SUCCESS )
2578 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2579
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002580 /* Now the operation object should be reusable. */
2581#if defined(KNOWN_SUPPORTED_HASH_ALG)
2582 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2583 PSA_ASSERT( psa_hash_abort( &operation ) );
2584#endif
2585
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002586exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002587 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002588}
2589/* END_CASE */
2590
2591/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002592void hash_compute_fail( int alg_arg, data_t *input,
2593 int output_size_arg, int expected_status_arg )
2594{
2595 psa_algorithm_t alg = alg_arg;
2596 uint8_t *output = NULL;
2597 size_t output_size = output_size_arg;
2598 size_t output_length = INVALID_EXPORT_LENGTH;
2599 psa_status_t expected_status = expected_status_arg;
2600 psa_status_t status;
2601
2602 ASSERT_ALLOC( output, output_size );
2603
2604 PSA_ASSERT( psa_crypto_init( ) );
2605
2606 status = psa_hash_compute( alg, input->x, input->len,
2607 output, output_size, &output_length );
2608 TEST_EQUAL( status, expected_status );
2609 TEST_ASSERT( output_length <= output_size );
2610
2611exit:
2612 mbedtls_free( output );
2613 PSA_DONE( );
2614}
2615/* END_CASE */
2616
2617/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002618void hash_compare_fail( int alg_arg, data_t *input,
2619 data_t *reference_hash,
2620 int expected_status_arg )
2621{
2622 psa_algorithm_t alg = alg_arg;
2623 psa_status_t expected_status = expected_status_arg;
2624 psa_status_t status;
2625
2626 PSA_ASSERT( psa_crypto_init( ) );
2627
2628 status = psa_hash_compare( alg, input->x, input->len,
2629 reference_hash->x, reference_hash->len );
2630 TEST_EQUAL( status, expected_status );
2631
2632exit:
2633 PSA_DONE( );
2634}
2635/* END_CASE */
2636
2637/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002638void hash_compute_compare( int alg_arg, data_t *input,
2639 data_t *expected_output )
2640{
2641 psa_algorithm_t alg = alg_arg;
2642 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2643 size_t output_length = INVALID_EXPORT_LENGTH;
2644 size_t i;
2645
2646 PSA_ASSERT( psa_crypto_init( ) );
2647
2648 /* Compute with tight buffer */
2649 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002650 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002651 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002652 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002653 ASSERT_COMPARE( output, output_length,
2654 expected_output->x, expected_output->len );
2655
2656 /* Compute with larger buffer */
2657 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2658 output, sizeof( output ),
2659 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002660 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002661 ASSERT_COMPARE( output, output_length,
2662 expected_output->x, expected_output->len );
2663
2664 /* Compare with correct hash */
2665 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2666 output, output_length ) );
2667
2668 /* Compare with trailing garbage */
2669 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2670 output, output_length + 1 ),
2671 PSA_ERROR_INVALID_SIGNATURE );
2672
2673 /* Compare with truncated hash */
2674 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2675 output, output_length - 1 ),
2676 PSA_ERROR_INVALID_SIGNATURE );
2677
2678 /* Compare with corrupted value */
2679 for( i = 0; i < output_length; i++ )
2680 {
Chris Jones9634bb12021-01-20 15:56:42 +00002681 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002682 output[i] ^= 1;
2683 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2684 output, output_length ),
2685 PSA_ERROR_INVALID_SIGNATURE );
2686 output[i] ^= 1;
2687 }
2688
2689exit:
2690 PSA_DONE( );
2691}
2692/* END_CASE */
2693
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002694/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002695void hash_bad_order( )
2696{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002697 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002698 unsigned char input[] = "";
2699 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002700 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002701 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2702 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2703 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002704 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002705 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002706 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002707
Gilles Peskine8817f612018-12-18 00:18:46 +01002708 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002709
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002710 /* Call setup twice in a row. */
2711 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2712 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2713 PSA_ERROR_BAD_STATE );
2714 PSA_ASSERT( psa_hash_abort( &operation ) );
2715
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002716 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002717 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002718 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002719 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002720
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002721 /* Call update after finish. */
2722 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2723 PSA_ASSERT( psa_hash_finish( &operation,
2724 hash, sizeof( hash ), &hash_len ) );
2725 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002726 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002727 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002728
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002729 /* Call verify without calling setup beforehand. */
2730 TEST_EQUAL( psa_hash_verify( &operation,
2731 valid_hash, sizeof( valid_hash ) ),
2732 PSA_ERROR_BAD_STATE );
2733 PSA_ASSERT( psa_hash_abort( &operation ) );
2734
2735 /* Call verify after finish. */
2736 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2737 PSA_ASSERT( psa_hash_finish( &operation,
2738 hash, sizeof( hash ), &hash_len ) );
2739 TEST_EQUAL( psa_hash_verify( &operation,
2740 valid_hash, sizeof( valid_hash ) ),
2741 PSA_ERROR_BAD_STATE );
2742 PSA_ASSERT( psa_hash_abort( &operation ) );
2743
2744 /* Call verify twice in a row. */
2745 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2746 PSA_ASSERT( psa_hash_verify( &operation,
2747 valid_hash, sizeof( valid_hash ) ) );
2748 TEST_EQUAL( psa_hash_verify( &operation,
2749 valid_hash, sizeof( valid_hash ) ),
2750 PSA_ERROR_BAD_STATE );
2751 PSA_ASSERT( psa_hash_abort( &operation ) );
2752
2753 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002754 TEST_EQUAL( psa_hash_finish( &operation,
2755 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002756 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002757 PSA_ASSERT( psa_hash_abort( &operation ) );
2758
2759 /* Call finish twice in a row. */
2760 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2761 PSA_ASSERT( psa_hash_finish( &operation,
2762 hash, sizeof( hash ), &hash_len ) );
2763 TEST_EQUAL( psa_hash_finish( &operation,
2764 hash, sizeof( hash ), &hash_len ),
2765 PSA_ERROR_BAD_STATE );
2766 PSA_ASSERT( psa_hash_abort( &operation ) );
2767
2768 /* Call finish after calling verify. */
2769 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2770 PSA_ASSERT( psa_hash_verify( &operation,
2771 valid_hash, sizeof( valid_hash ) ) );
2772 TEST_EQUAL( psa_hash_finish( &operation,
2773 hash, sizeof( hash ), &hash_len ),
2774 PSA_ERROR_BAD_STATE );
2775 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002776
2777exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002778 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002779}
2780/* END_CASE */
2781
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002782/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002783void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002784{
2785 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002786 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2787 * appended to it */
2788 unsigned char hash[] = {
2789 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2790 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2791 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002792 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002793 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002794
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002796
itayzafrir27e69452018-11-01 14:26:34 +02002797 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002799 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002800 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002801
itayzafrir27e69452018-11-01 14:26:34 +02002802 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002804 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002805 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002806
itayzafrir27e69452018-11-01 14:26:34 +02002807 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002809 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002810 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002811
itayzafrirec93d302018-10-18 18:01:10 +03002812exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002813 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002814}
2815/* END_CASE */
2816
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002817/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2818void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002819{
2820 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002821 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002822 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002823 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002824 size_t hash_len;
2825
Gilles Peskine8817f612018-12-18 00:18:46 +01002826 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002827
itayzafrir58028322018-10-25 10:22:01 +03002828 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002829 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002830 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002831 hash, expected_size - 1, &hash_len ),
2832 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002833
2834exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002835 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002836}
2837/* END_CASE */
2838
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002839/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2840void hash_clone_source_state( )
2841{
2842 psa_algorithm_t alg = PSA_ALG_SHA_256;
2843 unsigned char hash[PSA_HASH_MAX_SIZE];
2844 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2845 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2846 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2847 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2848 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2849 size_t hash_len;
2850
2851 PSA_ASSERT( psa_crypto_init( ) );
2852 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2853
2854 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2855 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2856 PSA_ASSERT( psa_hash_finish( &op_finished,
2857 hash, sizeof( hash ), &hash_len ) );
2858 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2859 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2860
2861 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2862 PSA_ERROR_BAD_STATE );
2863
2864 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2865 PSA_ASSERT( psa_hash_finish( &op_init,
2866 hash, sizeof( hash ), &hash_len ) );
2867 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2868 PSA_ASSERT( psa_hash_finish( &op_finished,
2869 hash, sizeof( hash ), &hash_len ) );
2870 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2871 PSA_ASSERT( psa_hash_finish( &op_aborted,
2872 hash, sizeof( hash ), &hash_len ) );
2873
2874exit:
2875 psa_hash_abort( &op_source );
2876 psa_hash_abort( &op_init );
2877 psa_hash_abort( &op_setup );
2878 psa_hash_abort( &op_finished );
2879 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002880 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002881}
2882/* END_CASE */
2883
2884/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2885void hash_clone_target_state( )
2886{
2887 psa_algorithm_t alg = PSA_ALG_SHA_256;
2888 unsigned char hash[PSA_HASH_MAX_SIZE];
2889 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2890 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2891 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2892 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2893 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2894 size_t hash_len;
2895
2896 PSA_ASSERT( psa_crypto_init( ) );
2897
2898 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2899 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2900 PSA_ASSERT( psa_hash_finish( &op_finished,
2901 hash, sizeof( hash ), &hash_len ) );
2902 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2903 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2904
2905 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2906 PSA_ASSERT( psa_hash_finish( &op_target,
2907 hash, sizeof( hash ), &hash_len ) );
2908
2909 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2910 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2911 PSA_ERROR_BAD_STATE );
2912 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2913 PSA_ERROR_BAD_STATE );
2914
2915exit:
2916 psa_hash_abort( &op_target );
2917 psa_hash_abort( &op_init );
2918 psa_hash_abort( &op_setup );
2919 psa_hash_abort( &op_finished );
2920 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002921 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002922}
2923/* END_CASE */
2924
itayzafrir58028322018-10-25 10:22:01 +03002925/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002926void mac_operation_init( )
2927{
Jaeden Amero252ef282019-02-15 14:05:35 +00002928 const uint8_t input[1] = { 0 };
2929
Jaeden Amero769ce272019-01-04 11:48:03 +00002930 /* Test each valid way of initializing the object, except for `= {0}`, as
2931 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2932 * though it's OK by the C standard. We could test for this, but we'd need
2933 * to supress the Clang warning for the test. */
2934 psa_mac_operation_t func = psa_mac_operation_init( );
2935 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2936 psa_mac_operation_t zero;
2937
2938 memset( &zero, 0, sizeof( zero ) );
2939
Jaeden Amero252ef282019-02-15 14:05:35 +00002940 /* A freshly-initialized MAC operation should not be usable. */
2941 TEST_EQUAL( psa_mac_update( &func,
2942 input, sizeof( input ) ),
2943 PSA_ERROR_BAD_STATE );
2944 TEST_EQUAL( psa_mac_update( &init,
2945 input, sizeof( input ) ),
2946 PSA_ERROR_BAD_STATE );
2947 TEST_EQUAL( psa_mac_update( &zero,
2948 input, sizeof( input ) ),
2949 PSA_ERROR_BAD_STATE );
2950
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002951 /* A default MAC operation should be abortable without error. */
2952 PSA_ASSERT( psa_mac_abort( &func ) );
2953 PSA_ASSERT( psa_mac_abort( &init ) );
2954 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002955}
2956/* END_CASE */
2957
2958/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002959void mac_setup( int key_type_arg,
2960 data_t *key,
2961 int alg_arg,
2962 int expected_status_arg )
2963{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002964 psa_key_type_t key_type = key_type_arg;
2965 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002966 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002967 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002968 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2969#if defined(KNOWN_SUPPORTED_MAC_ALG)
2970 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2971#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002972
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002974
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002975 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2976 &operation, &status ) )
2977 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002978 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002979
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002980 /* The operation object should be reusable. */
2981#if defined(KNOWN_SUPPORTED_MAC_ALG)
2982 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2983 smoke_test_key_data,
2984 sizeof( smoke_test_key_data ),
2985 KNOWN_SUPPORTED_MAC_ALG,
2986 &operation, &status ) )
2987 goto exit;
2988 TEST_EQUAL( status, PSA_SUCCESS );
2989#endif
2990
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002991exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002992 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002993}
2994/* END_CASE */
2995
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002996/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00002997void mac_bad_order( )
2998{
Ronald Cron5425a212020-08-04 14:58:35 +02002999 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003000 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3001 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003002 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003003 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3004 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3005 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003007 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3008 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3009 size_t sign_mac_length = 0;
3010 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3011 const uint8_t verify_mac[] = {
3012 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3013 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3014 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3015
3016 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003017 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003018 psa_set_key_algorithm( &attributes, alg );
3019 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003020
Ronald Cron5425a212020-08-04 14:58:35 +02003021 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3022 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003023
Jaeden Amero252ef282019-02-15 14:05:35 +00003024 /* Call update without calling setup beforehand. */
3025 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3026 PSA_ERROR_BAD_STATE );
3027 PSA_ASSERT( psa_mac_abort( &operation ) );
3028
3029 /* Call sign finish without calling setup beforehand. */
3030 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3031 &sign_mac_length),
3032 PSA_ERROR_BAD_STATE );
3033 PSA_ASSERT( psa_mac_abort( &operation ) );
3034
3035 /* Call verify finish without calling setup beforehand. */
3036 TEST_EQUAL( psa_mac_verify_finish( &operation,
3037 verify_mac, sizeof( verify_mac ) ),
3038 PSA_ERROR_BAD_STATE );
3039 PSA_ASSERT( psa_mac_abort( &operation ) );
3040
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003041 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003042 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
3043 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003044 PSA_ERROR_BAD_STATE );
3045 PSA_ASSERT( psa_mac_abort( &operation ) );
3046
Jaeden Amero252ef282019-02-15 14:05:35 +00003047 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003048 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003049 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3050 PSA_ASSERT( psa_mac_sign_finish( &operation,
3051 sign_mac, sizeof( sign_mac ),
3052 &sign_mac_length ) );
3053 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3054 PSA_ERROR_BAD_STATE );
3055 PSA_ASSERT( psa_mac_abort( &operation ) );
3056
3057 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003058 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003059 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3060 PSA_ASSERT( psa_mac_verify_finish( &operation,
3061 verify_mac, sizeof( verify_mac ) ) );
3062 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3063 PSA_ERROR_BAD_STATE );
3064 PSA_ASSERT( psa_mac_abort( &operation ) );
3065
3066 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003067 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003068 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3069 PSA_ASSERT( psa_mac_sign_finish( &operation,
3070 sign_mac, sizeof( sign_mac ),
3071 &sign_mac_length ) );
3072 TEST_EQUAL( psa_mac_sign_finish( &operation,
3073 sign_mac, sizeof( sign_mac ),
3074 &sign_mac_length ),
3075 PSA_ERROR_BAD_STATE );
3076 PSA_ASSERT( psa_mac_abort( &operation ) );
3077
3078 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003079 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003080 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3081 PSA_ASSERT( psa_mac_verify_finish( &operation,
3082 verify_mac, sizeof( verify_mac ) ) );
3083 TEST_EQUAL( psa_mac_verify_finish( &operation,
3084 verify_mac, sizeof( verify_mac ) ),
3085 PSA_ERROR_BAD_STATE );
3086 PSA_ASSERT( psa_mac_abort( &operation ) );
3087
3088 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003089 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003090 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3091 TEST_EQUAL( psa_mac_verify_finish( &operation,
3092 verify_mac, sizeof( verify_mac ) ),
3093 PSA_ERROR_BAD_STATE );
3094 PSA_ASSERT( psa_mac_abort( &operation ) );
3095
3096 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003097 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003098 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3099 TEST_EQUAL( psa_mac_sign_finish( &operation,
3100 sign_mac, sizeof( sign_mac ),
3101 &sign_mac_length ),
3102 PSA_ERROR_BAD_STATE );
3103 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003104
Ronald Cron5425a212020-08-04 14:58:35 +02003105 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003106
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003107exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003108 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003109}
3110/* END_CASE */
3111
3112/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003113void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003114 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003115 int alg_arg,
3116 data_t *input,
3117 data_t *expected_mac )
3118{
Ronald Cron5425a212020-08-04 14:58:35 +02003119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003120 psa_key_type_t key_type = key_type_arg;
3121 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003122 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003124 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003125 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003126 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003127 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003128 const size_t output_sizes_to_test[] = {
3129 0,
3130 1,
3131 expected_mac->len - 1,
3132 expected_mac->len,
3133 expected_mac->len + 1,
3134 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003135
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003136 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003137 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003138 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003139
Gilles Peskine8817f612018-12-18 00:18:46 +01003140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003141
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003142 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003143 psa_set_key_algorithm( &attributes, alg );
3144 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003145
Ronald Cron5425a212020-08-04 14:58:35 +02003146 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3147 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003148
Gilles Peskine8b356b52020-08-25 23:44:59 +02003149 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3150 {
3151 const size_t output_size = output_sizes_to_test[i];
3152 psa_status_t expected_status =
3153 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3154 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003155
Chris Jones9634bb12021-01-20 15:56:42 +00003156 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003157 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003158
Gilles Peskine8b356b52020-08-25 23:44:59 +02003159 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003160 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003161 PSA_ASSERT( psa_mac_update( &operation,
3162 input->x, input->len ) );
3163 TEST_EQUAL( psa_mac_sign_finish( &operation,
3164 actual_mac, output_size,
3165 &mac_length ),
3166 expected_status );
3167 PSA_ASSERT( psa_mac_abort( &operation ) );
3168
3169 if( expected_status == PSA_SUCCESS )
3170 {
3171 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3172 actual_mac, mac_length );
3173 }
3174 mbedtls_free( actual_mac );
3175 actual_mac = NULL;
3176 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003177
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003178exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003179 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003180 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003181 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003182 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003183}
3184/* END_CASE */
3185
3186/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003187void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003188 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003189 int alg_arg,
3190 data_t *input,
3191 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003192{
Ronald Cron5425a212020-08-04 14:58:35 +02003193 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003194 psa_key_type_t key_type = key_type_arg;
3195 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003196 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003198 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003199
Gilles Peskine69c12672018-06-28 00:07:19 +02003200 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3201
Gilles Peskine8817f612018-12-18 00:18:46 +01003202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003203
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003205 psa_set_key_algorithm( &attributes, alg );
3206 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003207
Ronald Cron5425a212020-08-04 14:58:35 +02003208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3209 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003210
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003211 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003212 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003213 PSA_ASSERT( psa_mac_update( &operation,
3214 input->x, input->len ) );
3215 PSA_ASSERT( psa_mac_verify_finish( &operation,
3216 expected_mac->x,
3217 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003218
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003219 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003220 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003221 PSA_ASSERT( psa_mac_update( &operation,
3222 input->x, input->len ) );
3223 TEST_EQUAL( psa_mac_verify_finish( &operation,
3224 expected_mac->x,
3225 expected_mac->len - 1 ),
3226 PSA_ERROR_INVALID_SIGNATURE );
3227
3228 /* Test a MAC that's too long. */
3229 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3230 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003231 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003232 PSA_ASSERT( psa_mac_update( &operation,
3233 input->x, input->len ) );
3234 TEST_EQUAL( psa_mac_verify_finish( &operation,
3235 perturbed_mac,
3236 expected_mac->len + 1 ),
3237 PSA_ERROR_INVALID_SIGNATURE );
3238
3239 /* Test changing one byte. */
3240 for( size_t i = 0; i < expected_mac->len; i++ )
3241 {
Chris Jones9634bb12021-01-20 15:56:42 +00003242 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003243 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003244 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003245 PSA_ASSERT( psa_mac_update( &operation,
3246 input->x, input->len ) );
3247 TEST_EQUAL( psa_mac_verify_finish( &operation,
3248 perturbed_mac,
3249 expected_mac->len ),
3250 PSA_ERROR_INVALID_SIGNATURE );
3251 perturbed_mac[i] ^= 1;
3252 }
3253
Gilles Peskine8c9def32018-02-08 10:02:12 +01003254exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003255 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003256 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003257 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003258 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003259}
3260/* END_CASE */
3261
3262/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003263void cipher_operation_init( )
3264{
Jaeden Ameroab439972019-02-15 14:12:05 +00003265 const uint8_t input[1] = { 0 };
3266 unsigned char output[1] = { 0 };
3267 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003268 /* Test each valid way of initializing the object, except for `= {0}`, as
3269 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3270 * though it's OK by the C standard. We could test for this, but we'd need
3271 * to supress the Clang warning for the test. */
3272 psa_cipher_operation_t func = psa_cipher_operation_init( );
3273 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3274 psa_cipher_operation_t zero;
3275
3276 memset( &zero, 0, sizeof( zero ) );
3277
Jaeden Ameroab439972019-02-15 14:12:05 +00003278 /* A freshly-initialized cipher operation should not be usable. */
3279 TEST_EQUAL( psa_cipher_update( &func,
3280 input, sizeof( input ),
3281 output, sizeof( output ),
3282 &output_length ),
3283 PSA_ERROR_BAD_STATE );
3284 TEST_EQUAL( psa_cipher_update( &init,
3285 input, sizeof( input ),
3286 output, sizeof( output ),
3287 &output_length ),
3288 PSA_ERROR_BAD_STATE );
3289 TEST_EQUAL( psa_cipher_update( &zero,
3290 input, sizeof( input ),
3291 output, sizeof( output ),
3292 &output_length ),
3293 PSA_ERROR_BAD_STATE );
3294
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003295 /* A default cipher operation should be abortable without error. */
3296 PSA_ASSERT( psa_cipher_abort( &func ) );
3297 PSA_ASSERT( psa_cipher_abort( &init ) );
3298 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003299}
3300/* END_CASE */
3301
3302/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003303void cipher_setup( int key_type_arg,
3304 data_t *key,
3305 int alg_arg,
3306 int expected_status_arg )
3307{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003308 psa_key_type_t key_type = key_type_arg;
3309 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003310 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003311 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003312 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003313#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003314 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3315#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003316
Gilles Peskine8817f612018-12-18 00:18:46 +01003317 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003318
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003319 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3320 &operation, &status ) )
3321 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003322 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003323
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003324 /* The operation object should be reusable. */
3325#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3326 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3327 smoke_test_key_data,
3328 sizeof( smoke_test_key_data ),
3329 KNOWN_SUPPORTED_CIPHER_ALG,
3330 &operation, &status ) )
3331 goto exit;
3332 TEST_EQUAL( status, PSA_SUCCESS );
3333#endif
3334
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003335exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003336 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003337 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003338}
3339/* END_CASE */
3340
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003341/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003342void cipher_bad_order( )
3343{
Ronald Cron5425a212020-08-04 14:58:35 +02003344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003345 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3346 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003348 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003349 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003350 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003351 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3352 0xaa, 0xaa, 0xaa, 0xaa };
3353 const uint8_t text[] = {
3354 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3355 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003356 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003357 size_t length = 0;
3358
3359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003360 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3361 psa_set_key_algorithm( &attributes, alg );
3362 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003363 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3364 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003365
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003366 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003367 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3368 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003369 PSA_ERROR_BAD_STATE );
3370 PSA_ASSERT( psa_cipher_abort( &operation ) );
3371
3372 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003373 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3374 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003375 PSA_ERROR_BAD_STATE );
3376 PSA_ASSERT( psa_cipher_abort( &operation ) );
3377
Jaeden Ameroab439972019-02-15 14:12:05 +00003378 /* Generate an IV without calling setup beforehand. */
3379 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3380 buffer, sizeof( buffer ),
3381 &length ),
3382 PSA_ERROR_BAD_STATE );
3383 PSA_ASSERT( psa_cipher_abort( &operation ) );
3384
3385 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003386 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003387 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3388 buffer, sizeof( buffer ),
3389 &length ) );
3390 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3391 buffer, sizeof( buffer ),
3392 &length ),
3393 PSA_ERROR_BAD_STATE );
3394 PSA_ASSERT( psa_cipher_abort( &operation ) );
3395
3396 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003397 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003398 PSA_ASSERT( psa_cipher_set_iv( &operation,
3399 iv, sizeof( iv ) ) );
3400 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3401 buffer, sizeof( buffer ),
3402 &length ),
3403 PSA_ERROR_BAD_STATE );
3404 PSA_ASSERT( psa_cipher_abort( &operation ) );
3405
3406 /* Set an IV without calling setup beforehand. */
3407 TEST_EQUAL( psa_cipher_set_iv( &operation,
3408 iv, sizeof( iv ) ),
3409 PSA_ERROR_BAD_STATE );
3410 PSA_ASSERT( psa_cipher_abort( &operation ) );
3411
3412 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003413 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003414 PSA_ASSERT( psa_cipher_set_iv( &operation,
3415 iv, sizeof( iv ) ) );
3416 TEST_EQUAL( psa_cipher_set_iv( &operation,
3417 iv, sizeof( iv ) ),
3418 PSA_ERROR_BAD_STATE );
3419 PSA_ASSERT( psa_cipher_abort( &operation ) );
3420
3421 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003422 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003423 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3424 buffer, sizeof( buffer ),
3425 &length ) );
3426 TEST_EQUAL( psa_cipher_set_iv( &operation,
3427 iv, sizeof( iv ) ),
3428 PSA_ERROR_BAD_STATE );
3429 PSA_ASSERT( psa_cipher_abort( &operation ) );
3430
3431 /* Call update without calling setup beforehand. */
3432 TEST_EQUAL( psa_cipher_update( &operation,
3433 text, sizeof( text ),
3434 buffer, sizeof( buffer ),
3435 &length ),
3436 PSA_ERROR_BAD_STATE );
3437 PSA_ASSERT( psa_cipher_abort( &operation ) );
3438
3439 /* Call update without an IV where an IV is required. */
3440 TEST_EQUAL( psa_cipher_update( &operation,
3441 text, sizeof( text ),
3442 buffer, sizeof( buffer ),
3443 &length ),
3444 PSA_ERROR_BAD_STATE );
3445 PSA_ASSERT( psa_cipher_abort( &operation ) );
3446
3447 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003448 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003449 PSA_ASSERT( psa_cipher_set_iv( &operation,
3450 iv, sizeof( iv ) ) );
3451 PSA_ASSERT( psa_cipher_finish( &operation,
3452 buffer, sizeof( buffer ), &length ) );
3453 TEST_EQUAL( psa_cipher_update( &operation,
3454 text, sizeof( text ),
3455 buffer, sizeof( buffer ),
3456 &length ),
3457 PSA_ERROR_BAD_STATE );
3458 PSA_ASSERT( psa_cipher_abort( &operation ) );
3459
3460 /* Call finish without calling setup beforehand. */
3461 TEST_EQUAL( psa_cipher_finish( &operation,
3462 buffer, sizeof( buffer ), &length ),
3463 PSA_ERROR_BAD_STATE );
3464 PSA_ASSERT( psa_cipher_abort( &operation ) );
3465
3466 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003467 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003468 /* Not calling update means we are encrypting an empty buffer, which is OK
3469 * for cipher modes with padding. */
3470 TEST_EQUAL( psa_cipher_finish( &operation,
3471 buffer, sizeof( buffer ), &length ),
3472 PSA_ERROR_BAD_STATE );
3473 PSA_ASSERT( psa_cipher_abort( &operation ) );
3474
3475 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003476 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003477 PSA_ASSERT( psa_cipher_set_iv( &operation,
3478 iv, sizeof( iv ) ) );
3479 PSA_ASSERT( psa_cipher_finish( &operation,
3480 buffer, sizeof( buffer ), &length ) );
3481 TEST_EQUAL( psa_cipher_finish( &operation,
3482 buffer, sizeof( buffer ), &length ),
3483 PSA_ERROR_BAD_STATE );
3484 PSA_ASSERT( psa_cipher_abort( &operation ) );
3485
Ronald Cron5425a212020-08-04 14:58:35 +02003486 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003487
Jaeden Ameroab439972019-02-15 14:12:05 +00003488exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003489 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003490 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003491}
3492/* END_CASE */
3493
3494/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003495void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003496 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003497 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003498 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003499{
Ronald Cron5425a212020-08-04 14:58:35 +02003500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003501 psa_status_t status;
3502 psa_key_type_t key_type = key_type_arg;
3503 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003504 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003505 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003506 size_t output_buffer_size = 0;
3507 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003508 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003509 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003511
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003513
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003514 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3515 psa_set_key_algorithm( &attributes, alg );
3516 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003517
Ronald Cron5425a212020-08-04 14:58:35 +02003518 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3519 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003520
Ronald Cron5425a212020-08-04 14:58:35 +02003521 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003522
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003523 if( iv->len > 0 )
3524 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003525 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003526 }
3527
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003528 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003529 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003530 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003531
Gilles Peskine8817f612018-12-18 00:18:46 +01003532 PSA_ASSERT( psa_cipher_update( &operation,
3533 input->x, input->len,
3534 output, output_buffer_size,
3535 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003536 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003537 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003538 output + total_output_length,
3539 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003540 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003541 total_output_length += function_output_length;
3542
Gilles Peskinefe11b722018-12-18 00:24:04 +01003543 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003544 if( expected_status == PSA_SUCCESS )
3545 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003547 ASSERT_COMPARE( expected_output->x, expected_output->len,
3548 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003549 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003550
Gilles Peskine50e586b2018-06-08 14:28:46 +02003551exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003552 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003553 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003554 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003555 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003556}
3557/* END_CASE */
3558
3559/* BEGIN_CASE */
3560void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003561 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003562 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003563 int first_part_size_arg,
3564 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003565 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003566{
Ronald Cron5425a212020-08-04 14:58:35 +02003567 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003568 psa_key_type_t key_type = key_type_arg;
3569 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003570 size_t first_part_size = first_part_size_arg;
3571 size_t output1_length = output1_length_arg;
3572 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003573 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003574 size_t output_buffer_size = 0;
3575 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003576 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003577 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003578 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003579
Gilles Peskine8817f612018-12-18 00:18:46 +01003580 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003581
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003582 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3583 psa_set_key_algorithm( &attributes, alg );
3584 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003585
Ronald Cron5425a212020-08-04 14:58:35 +02003586 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3587 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003588
Ronald Cron5425a212020-08-04 14:58:35 +02003589 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003590
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003591 if( iv->len > 0 )
3592 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003593 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003594 }
3595
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003596 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003597 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003598 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003599
Gilles Peskinee0866522019-02-19 19:44:00 +01003600 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003601 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3602 output, output_buffer_size,
3603 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003604 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003605 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003606 PSA_ASSERT( psa_cipher_update( &operation,
3607 input->x + first_part_size,
3608 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003609 output + total_output_length,
3610 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003611 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003612 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003613 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003614 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003615 output + total_output_length,
3616 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003617 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003618 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003619 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003620
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003621 ASSERT_COMPARE( expected_output->x, expected_output->len,
3622 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003623
3624exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003625 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003626 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003627 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003628 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003629}
3630/* END_CASE */
3631
3632/* BEGIN_CASE */
3633void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003634 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003635 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003636 int first_part_size_arg,
3637 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003638 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003639{
Ronald Cron5425a212020-08-04 14:58:35 +02003640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003641 psa_key_type_t key_type = key_type_arg;
3642 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003643 size_t first_part_size = first_part_size_arg;
3644 size_t output1_length = output1_length_arg;
3645 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003646 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003647 size_t output_buffer_size = 0;
3648 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003649 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003650 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003652
Gilles Peskine8817f612018-12-18 00:18:46 +01003653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003654
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003655 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3656 psa_set_key_algorithm( &attributes, alg );
3657 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003658
Ronald Cron5425a212020-08-04 14:58:35 +02003659 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3660 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003661
Ronald Cron5425a212020-08-04 14:58:35 +02003662 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003663
Steven Cooreman177deba2020-09-07 17:14:14 +02003664 if( iv->len > 0 )
3665 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003666 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003667 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003668
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003669 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003670 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003671 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003672
Gilles Peskinee0866522019-02-19 19:44:00 +01003673 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003674 PSA_ASSERT( psa_cipher_update( &operation,
3675 input->x, first_part_size,
3676 output, output_buffer_size,
3677 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003678 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003679 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003680 PSA_ASSERT( psa_cipher_update( &operation,
3681 input->x + first_part_size,
3682 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003683 output + total_output_length,
3684 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003685 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003686 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003687 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003688 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003689 output + total_output_length,
3690 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003691 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003692 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003693 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003694
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003695 ASSERT_COMPARE( expected_output->x, expected_output->len,
3696 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003697
3698exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003699 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003700 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003702 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003703}
3704/* END_CASE */
3705
Gilles Peskine50e586b2018-06-08 14:28:46 +02003706/* BEGIN_CASE */
3707void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003708 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003709 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003710 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003711{
Ronald Cron5425a212020-08-04 14:58:35 +02003712 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003713 psa_status_t status;
3714 psa_key_type_t key_type = key_type_arg;
3715 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003716 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003717 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003718 size_t output_buffer_size = 0;
3719 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003720 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003721 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003723
Gilles Peskine8817f612018-12-18 00:18:46 +01003724 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003725
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003726 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3727 psa_set_key_algorithm( &attributes, alg );
3728 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003729
Ronald Cron5425a212020-08-04 14:58:35 +02003730 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3731 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003732
Ronald Cron5425a212020-08-04 14:58:35 +02003733 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003734
Steven Cooreman177deba2020-09-07 17:14:14 +02003735 if( iv->len > 0 )
3736 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003737 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003738 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003739
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003740 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003741 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003742 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003743
Gilles Peskine8817f612018-12-18 00:18:46 +01003744 PSA_ASSERT( psa_cipher_update( &operation,
3745 input->x, input->len,
3746 output, output_buffer_size,
3747 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003748 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003749 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003750 output + total_output_length,
3751 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003752 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003753 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003754 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003755
3756 if( expected_status == PSA_SUCCESS )
3757 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003758 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003759 ASSERT_COMPARE( expected_output->x, expected_output->len,
3760 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003761 }
3762
Gilles Peskine50e586b2018-06-08 14:28:46 +02003763exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003764 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003765 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003766 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003767 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003768}
3769/* END_CASE */
3770
Gilles Peskine50e586b2018-06-08 14:28:46 +02003771/* BEGIN_CASE */
3772void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003773 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003774 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003775{
Ronald Cron5425a212020-08-04 14:58:35 +02003776 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003777 psa_key_type_t key_type = key_type_arg;
3778 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003779 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003780 size_t iv_size = 16;
3781 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003782 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003783 size_t output1_size = 0;
3784 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003785 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003786 size_t output2_size = 0;
3787 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003788 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003789 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3790 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003791 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003792
Gilles Peskine8817f612018-12-18 00:18:46 +01003793 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003794
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003795 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3796 psa_set_key_algorithm( &attributes, alg );
3797 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003798
Ronald Cron5425a212020-08-04 14:58:35 +02003799 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3800 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003801
Ronald Cron5425a212020-08-04 14:58:35 +02003802 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3803 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003804
Steven Cooreman177deba2020-09-07 17:14:14 +02003805 if( alg != PSA_ALG_ECB_NO_PADDING )
3806 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003807 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3808 iv, iv_size,
3809 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003810 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003811 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003812 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003813 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003814
Gilles Peskine8817f612018-12-18 00:18:46 +01003815 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3816 output1, output1_size,
3817 &output1_length ) );
3818 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003819 output1 + output1_length,
3820 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003821 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003822
Gilles Peskine048b7f02018-06-08 14:20:49 +02003823 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003824
Gilles Peskine8817f612018-12-18 00:18:46 +01003825 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003826
3827 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003828 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003829
Steven Cooreman177deba2020-09-07 17:14:14 +02003830 if( iv_length > 0 )
3831 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003832 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3833 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003834 }
3835
Gilles Peskine8817f612018-12-18 00:18:46 +01003836 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3837 output2, output2_size,
3838 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003839 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003840 PSA_ASSERT( psa_cipher_finish( &operation2,
3841 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003842 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003843 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003844
Gilles Peskine048b7f02018-06-08 14:20:49 +02003845 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003846
Gilles Peskine8817f612018-12-18 00:18:46 +01003847 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003848
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003849 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003850
3851exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003852 psa_cipher_abort( &operation1 );
3853 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003854 mbedtls_free( output1 );
3855 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003856 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003857 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003858}
3859/* END_CASE */
3860
3861/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003862void cipher_verify_output_multipart( int alg_arg,
3863 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003864 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003865 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003866 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003867{
Ronald Cron5425a212020-08-04 14:58:35 +02003868 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003869 psa_key_type_t key_type = key_type_arg;
3870 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003871 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003872 unsigned char iv[16] = {0};
3873 size_t iv_size = 16;
3874 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003875 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003876 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003877 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003878 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003879 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003880 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003881 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003882 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3883 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003884 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003885
Gilles Peskine8817f612018-12-18 00:18:46 +01003886 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003887
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003888 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3889 psa_set_key_algorithm( &attributes, alg );
3890 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003891
Ronald Cron5425a212020-08-04 14:58:35 +02003892 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3893 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003894
Ronald Cron5425a212020-08-04 14:58:35 +02003895 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3896 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003897
Steven Cooreman177deba2020-09-07 17:14:14 +02003898 if( alg != PSA_ALG_ECB_NO_PADDING )
3899 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003900 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3901 iv, iv_size,
3902 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003903 }
3904
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003905 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003906 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003907 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003908
Gilles Peskinee0866522019-02-19 19:44:00 +01003909 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003910
Gilles Peskine8817f612018-12-18 00:18:46 +01003911 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3912 output1, output1_buffer_size,
3913 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003914 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003915
Gilles Peskine8817f612018-12-18 00:18:46 +01003916 PSA_ASSERT( psa_cipher_update( &operation1,
3917 input->x + first_part_size,
3918 input->len - first_part_size,
3919 output1, output1_buffer_size,
3920 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003921 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003922
Gilles Peskine8817f612018-12-18 00:18:46 +01003923 PSA_ASSERT( psa_cipher_finish( &operation1,
3924 output1 + output1_length,
3925 output1_buffer_size - output1_length,
3926 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003927 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003928
Gilles Peskine8817f612018-12-18 00:18:46 +01003929 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003930
Gilles Peskine048b7f02018-06-08 14:20:49 +02003931 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003932 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003933
Steven Cooreman177deba2020-09-07 17:14:14 +02003934 if( iv_length > 0 )
3935 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003936 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3937 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003938 }
Moran Pekerded84402018-06-06 16:36:50 +03003939
Gilles Peskine8817f612018-12-18 00:18:46 +01003940 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3941 output2, output2_buffer_size,
3942 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003943 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003944
Gilles Peskine8817f612018-12-18 00:18:46 +01003945 PSA_ASSERT( psa_cipher_update( &operation2,
3946 output1 + first_part_size,
3947 output1_length - first_part_size,
3948 output2, output2_buffer_size,
3949 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003950 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003951
Gilles Peskine8817f612018-12-18 00:18:46 +01003952 PSA_ASSERT( psa_cipher_finish( &operation2,
3953 output2 + output2_length,
3954 output2_buffer_size - output2_length,
3955 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003956 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003957
Gilles Peskine8817f612018-12-18 00:18:46 +01003958 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003959
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003960 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003961
3962exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003963 psa_cipher_abort( &operation1 );
3964 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003965 mbedtls_free( output1 );
3966 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003967 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003968 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003969}
3970/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003971
Gilles Peskine20035e32018-02-03 22:44:14 +01003972/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003973void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003974 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003975 data_t *nonce,
3976 data_t *additional_data,
3977 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003978 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003979{
Ronald Cron5425a212020-08-04 14:58:35 +02003980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003981 psa_key_type_t key_type = key_type_arg;
3982 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003983 unsigned char *output_data = NULL;
3984 size_t output_size = 0;
3985 size_t output_length = 0;
3986 unsigned char *output_data2 = NULL;
3987 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003988 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003989 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003991
Gilles Peskine4abf7412018-06-18 16:35:34 +02003992 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003993 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3994 * should be exact. */
3995 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3996 TEST_EQUAL( output_size,
3997 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003998 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003999
Gilles Peskine8817f612018-12-18 00:18:46 +01004000 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004001
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004002 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4003 psa_set_key_algorithm( &attributes, alg );
4004 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004005
Gilles Peskine049c7532019-05-15 20:22:09 +02004006 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004007 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004008
Ronald Cron5425a212020-08-04 14:58:35 +02004009 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004010 nonce->x, nonce->len,
4011 additional_data->x,
4012 additional_data->len,
4013 input_data->x, input_data->len,
4014 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004015 &output_length ),
4016 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004017
4018 if( PSA_SUCCESS == expected_result )
4019 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004020 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004021
Gilles Peskine003a4a92019-05-14 16:09:40 +02004022 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4023 * should be exact. */
4024 TEST_EQUAL( input_data->len,
4025 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
4026
Ronald Cron5425a212020-08-04 14:58:35 +02004027 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004028 nonce->x, nonce->len,
4029 additional_data->x,
4030 additional_data->len,
4031 output_data, output_length,
4032 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004033 &output_length2 ),
4034 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004035
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004036 ASSERT_COMPARE( input_data->x, input_data->len,
4037 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004038 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004039
Gilles Peskinea1cac842018-06-11 19:33:02 +02004040exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004041 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004042 mbedtls_free( output_data );
4043 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004044 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004045}
4046/* END_CASE */
4047
4048/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004049void aead_encrypt( int key_type_arg, data_t *key_data,
4050 int alg_arg,
4051 data_t *nonce,
4052 data_t *additional_data,
4053 data_t *input_data,
4054 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004055{
Ronald Cron5425a212020-08-04 14:58:35 +02004056 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004057 psa_key_type_t key_type = key_type_arg;
4058 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004059 unsigned char *output_data = NULL;
4060 size_t output_size = 0;
4061 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004062 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004063 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004064
Gilles Peskine4abf7412018-06-18 16:35:34 +02004065 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004066 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4067 * should be exact. */
4068 TEST_EQUAL( output_size,
4069 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004070 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004071
Gilles Peskine8817f612018-12-18 00:18:46 +01004072 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004073
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004074 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4075 psa_set_key_algorithm( &attributes, alg );
4076 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004077
Gilles Peskine049c7532019-05-15 20:22:09 +02004078 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004079 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004080
Ronald Cron5425a212020-08-04 14:58:35 +02004081 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004082 nonce->x, nonce->len,
4083 additional_data->x, additional_data->len,
4084 input_data->x, input_data->len,
4085 output_data, output_size,
4086 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004087
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004088 ASSERT_COMPARE( expected_result->x, expected_result->len,
4089 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004090
Gilles Peskinea1cac842018-06-11 19:33:02 +02004091exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004092 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004093 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004094 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004095}
4096/* END_CASE */
4097
4098/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004099void aead_decrypt( int key_type_arg, data_t *key_data,
4100 int alg_arg,
4101 data_t *nonce,
4102 data_t *additional_data,
4103 data_t *input_data,
4104 data_t *expected_data,
4105 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004106{
Ronald Cron5425a212020-08-04 14:58:35 +02004107 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004108 psa_key_type_t key_type = key_type_arg;
4109 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004110 unsigned char *output_data = NULL;
4111 size_t output_size = 0;
4112 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004113 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004115 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004116
Gilles Peskine003a4a92019-05-14 16:09:40 +02004117 output_size = input_data->len - tag_length;
4118 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4119 * should be exact. */
4120 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4121 TEST_EQUAL( output_size,
4122 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004123 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004124
Gilles Peskine8817f612018-12-18 00:18:46 +01004125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004126
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004127 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4128 psa_set_key_algorithm( &attributes, alg );
4129 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004130
Gilles Peskine049c7532019-05-15 20:22:09 +02004131 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004132 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004133
Ronald Cron5425a212020-08-04 14:58:35 +02004134 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004135 nonce->x, nonce->len,
4136 additional_data->x,
4137 additional_data->len,
4138 input_data->x, input_data->len,
4139 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004140 &output_length ),
4141 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004142
Gilles Peskine2d277862018-06-18 15:41:12 +02004143 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004144 ASSERT_COMPARE( expected_data->x, expected_data->len,
4145 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004146
Gilles Peskinea1cac842018-06-11 19:33:02 +02004147exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004148 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004149 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004150 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004151}
4152/* END_CASE */
4153
4154/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004155void signature_size( int type_arg,
4156 int bits,
4157 int alg_arg,
4158 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004159{
4160 psa_key_type_t type = type_arg;
4161 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004162 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004163
Gilles Peskinefe11b722018-12-18 00:24:04 +01004164 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004165#if defined(MBEDTLS_TEST_DEPRECATED)
4166 TEST_EQUAL( actual_size,
4167 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4168#endif /* MBEDTLS_TEST_DEPRECATED */
4169
Gilles Peskinee59236f2018-01-27 23:32:46 +01004170exit:
4171 ;
4172}
4173/* END_CASE */
4174
4175/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004176void sign_deterministic( int key_type_arg, data_t *key_data,
4177 int alg_arg, data_t *input_data,
4178 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004179{
Ronald Cron5425a212020-08-04 14:58:35 +02004180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004181 psa_key_type_t key_type = key_type_arg;
4182 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004183 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004184 unsigned char *signature = NULL;
4185 size_t signature_size;
4186 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004188
Gilles Peskine8817f612018-12-18 00:18:46 +01004189 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004190
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004191 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004192 psa_set_key_algorithm( &attributes, alg );
4193 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004194
Gilles Peskine049c7532019-05-15 20:22:09 +02004195 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004196 &key ) );
4197 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004198 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004199
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004200 /* Allocate a buffer which has the size advertized by the
4201 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004202 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004203 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004204 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004205 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004206 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004207
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004208 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004209 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004210 input_data->x, input_data->len,
4211 signature, signature_size,
4212 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004213 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004214 ASSERT_COMPARE( output_data->x, output_data->len,
4215 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004216
Gilles Peskine0627f982019-11-26 19:12:16 +01004217#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004218 memset( signature, 0, signature_size );
4219 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004220 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004221 input_data->x, input_data->len,
4222 signature, signature_size,
4223 &signature_length ) );
4224 ASSERT_COMPARE( output_data->x, output_data->len,
4225 signature, signature_length );
4226#endif /* MBEDTLS_TEST_DEPRECATED */
4227
Gilles Peskine20035e32018-02-03 22:44:14 +01004228exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004229 /*
4230 * Key attributes may have been returned by psa_get_key_attributes()
4231 * thus reset them as required.
4232 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004233 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004234
Ronald Cron5425a212020-08-04 14:58:35 +02004235 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004236 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004237 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004238}
4239/* END_CASE */
4240
4241/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004242void sign_fail( int key_type_arg, data_t *key_data,
4243 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004244 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004245{
Ronald Cron5425a212020-08-04 14:58:35 +02004246 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004247 psa_key_type_t key_type = key_type_arg;
4248 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004249 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004250 psa_status_t actual_status;
4251 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004252 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004253 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004254 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004255
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004256 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004257
Gilles Peskine8817f612018-12-18 00:18:46 +01004258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004259
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004261 psa_set_key_algorithm( &attributes, alg );
4262 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004263
Gilles Peskine049c7532019-05-15 20:22:09 +02004264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004265 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004266
Ronald Cron5425a212020-08-04 14:58:35 +02004267 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004268 input_data->x, input_data->len,
4269 signature, signature_size,
4270 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004271 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004272 /* The value of *signature_length is unspecified on error, but
4273 * whatever it is, it should be less than signature_size, so that
4274 * if the caller tries to read *signature_length bytes without
4275 * checking the error code then they don't overflow a buffer. */
4276 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004277
Gilles Peskine895242b2019-11-29 12:15:40 +01004278#if defined(MBEDTLS_TEST_DEPRECATED)
4279 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004280 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004281 input_data->x, input_data->len,
4282 signature, signature_size,
4283 &signature_length ),
4284 expected_status );
4285 TEST_ASSERT( signature_length <= signature_size );
4286#endif /* MBEDTLS_TEST_DEPRECATED */
4287
Gilles Peskine20035e32018-02-03 22:44:14 +01004288exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004289 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004290 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004291 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004292 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004293}
4294/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004295
4296/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004297void sign_verify( int key_type_arg, data_t *key_data,
4298 int alg_arg, data_t *input_data )
4299{
Ronald Cron5425a212020-08-04 14:58:35 +02004300 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004301 psa_key_type_t key_type = key_type_arg;
4302 psa_algorithm_t alg = alg_arg;
4303 size_t key_bits;
4304 unsigned char *signature = NULL;
4305 size_t signature_size;
4306 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004307 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004308
Gilles Peskine8817f612018-12-18 00:18:46 +01004309 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004310
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004311 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004312 psa_set_key_algorithm( &attributes, alg );
4313 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004314
Gilles Peskine049c7532019-05-15 20:22:09 +02004315 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004316 &key ) );
4317 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004318 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004319
4320 /* Allocate a buffer which has the size advertized by the
4321 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004322 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004323 key_bits, alg );
4324 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004325 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004326 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004327
4328 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004329 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004330 input_data->x, input_data->len,
4331 signature, signature_size,
4332 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004333 /* Check that the signature length looks sensible. */
4334 TEST_ASSERT( signature_length <= signature_size );
4335 TEST_ASSERT( signature_length > 0 );
4336
4337 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004338 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004339 input_data->x, input_data->len,
4340 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004341
4342 if( input_data->len != 0 )
4343 {
4344 /* Flip a bit in the input and verify that the signature is now
4345 * detected as invalid. Flip a bit at the beginning, not at the end,
4346 * because ECDSA may ignore the last few bits of the input. */
4347 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004348 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004349 input_data->x, input_data->len,
4350 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004351 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004352 }
4353
4354exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004355 /*
4356 * Key attributes may have been returned by psa_get_key_attributes()
4357 * thus reset them as required.
4358 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004359 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004360
Ronald Cron5425a212020-08-04 14:58:35 +02004361 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004362 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004363 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004364}
4365/* END_CASE */
4366
4367/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004368void asymmetric_verify( int key_type_arg, data_t *key_data,
4369 int alg_arg, data_t *hash_data,
4370 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004371{
Ronald Cron5425a212020-08-04 14:58:35 +02004372 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004373 psa_key_type_t key_type = key_type_arg;
4374 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004376
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004377 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004378
Gilles Peskine8817f612018-12-18 00:18:46 +01004379 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004380
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004381 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004382 psa_set_key_algorithm( &attributes, alg );
4383 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004384
Gilles Peskine049c7532019-05-15 20:22:09 +02004385 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004386 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004387
Ronald Cron5425a212020-08-04 14:58:35 +02004388 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004389 hash_data->x, hash_data->len,
4390 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004391
4392#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004393 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004394 hash_data->x, hash_data->len,
4395 signature_data->x,
4396 signature_data->len ) );
4397
4398#endif /* MBEDTLS_TEST_DEPRECATED */
4399
itayzafrir5c753392018-05-08 11:18:38 +03004400exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004401 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004402 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004403 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004404}
4405/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004406
4407/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004408void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4409 int alg_arg, data_t *hash_data,
4410 data_t *signature_data,
4411 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004412{
Ronald Cron5425a212020-08-04 14:58:35 +02004413 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004414 psa_key_type_t key_type = key_type_arg;
4415 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004416 psa_status_t actual_status;
4417 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004418 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004419
Gilles Peskine8817f612018-12-18 00:18:46 +01004420 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004421
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004422 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004423 psa_set_key_algorithm( &attributes, alg );
4424 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004425
Gilles Peskine049c7532019-05-15 20:22:09 +02004426 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004427 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004428
Ronald Cron5425a212020-08-04 14:58:35 +02004429 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004430 hash_data->x, hash_data->len,
4431 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004432 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004433
Gilles Peskine895242b2019-11-29 12:15:40 +01004434#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004435 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004436 hash_data->x, hash_data->len,
4437 signature_data->x, signature_data->len ),
4438 expected_status );
4439#endif /* MBEDTLS_TEST_DEPRECATED */
4440
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004441exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004442 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004443 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004444 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004445}
4446/* END_CASE */
4447
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004448/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004449void asymmetric_encrypt( int key_type_arg,
4450 data_t *key_data,
4451 int alg_arg,
4452 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004453 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004454 int expected_output_length_arg,
4455 int expected_status_arg )
4456{
Ronald Cron5425a212020-08-04 14:58:35 +02004457 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004458 psa_key_type_t key_type = key_type_arg;
4459 psa_algorithm_t alg = alg_arg;
4460 size_t expected_output_length = expected_output_length_arg;
4461 size_t key_bits;
4462 unsigned char *output = NULL;
4463 size_t output_size;
4464 size_t output_length = ~0;
4465 psa_status_t actual_status;
4466 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004468
Gilles Peskine8817f612018-12-18 00:18:46 +01004469 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004470
Gilles Peskine656896e2018-06-29 19:12:28 +02004471 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4473 psa_set_key_algorithm( &attributes, alg );
4474 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004475 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004476 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004477
4478 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004479 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004480 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004481 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004482 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004483
4484 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004485 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004486 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004487 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004488 output, output_size,
4489 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004490 TEST_EQUAL( actual_status, expected_status );
4491 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004492
Gilles Peskine68428122018-06-30 18:42:41 +02004493 /* If the label is empty, the test framework puts a non-null pointer
4494 * in label->x. Test that a null pointer works as well. */
4495 if( label->len == 0 )
4496 {
4497 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004498 if( output_size != 0 )
4499 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004500 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004501 input_data->x, input_data->len,
4502 NULL, label->len,
4503 output, output_size,
4504 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004505 TEST_EQUAL( actual_status, expected_status );
4506 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004507 }
4508
Gilles Peskine656896e2018-06-29 19:12:28 +02004509exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004510 /*
4511 * Key attributes may have been returned by psa_get_key_attributes()
4512 * thus reset them as required.
4513 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004514 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004515
Ronald Cron5425a212020-08-04 14:58:35 +02004516 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004517 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004518 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004519}
4520/* END_CASE */
4521
4522/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004523void asymmetric_encrypt_decrypt( int key_type_arg,
4524 data_t *key_data,
4525 int alg_arg,
4526 data_t *input_data,
4527 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004528{
Ronald Cron5425a212020-08-04 14:58:35 +02004529 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004530 psa_key_type_t key_type = key_type_arg;
4531 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004532 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004533 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004534 size_t output_size;
4535 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004536 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004537 size_t output2_size;
4538 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004540
Gilles Peskine8817f612018-12-18 00:18:46 +01004541 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004542
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004543 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4544 psa_set_key_algorithm( &attributes, alg );
4545 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004546
Gilles Peskine049c7532019-05-15 20:22:09 +02004547 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004548 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004549
4550 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004551 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004552 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004553 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004554 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004555 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004556 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004557
Gilles Peskineeebd7382018-06-08 18:11:54 +02004558 /* We test encryption by checking that encrypt-then-decrypt gives back
4559 * the original plaintext because of the non-optional random
4560 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004561 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004562 input_data->x, input_data->len,
4563 label->x, label->len,
4564 output, output_size,
4565 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004566 /* We don't know what ciphertext length to expect, but check that
4567 * it looks sensible. */
4568 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004569
Ronald Cron5425a212020-08-04 14:58:35 +02004570 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004571 output, output_length,
4572 label->x, label->len,
4573 output2, output2_size,
4574 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004575 ASSERT_COMPARE( input_data->x, input_data->len,
4576 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004577
4578exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004579 /*
4580 * Key attributes may have been returned by psa_get_key_attributes()
4581 * thus reset them as required.
4582 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004583 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004584
Ronald Cron5425a212020-08-04 14:58:35 +02004585 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004586 mbedtls_free( output );
4587 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004588 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004589}
4590/* END_CASE */
4591
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004592/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004593void asymmetric_decrypt( int key_type_arg,
4594 data_t *key_data,
4595 int alg_arg,
4596 data_t *input_data,
4597 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004598 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004599{
Ronald Cron5425a212020-08-04 14:58:35 +02004600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004601 psa_key_type_t key_type = key_type_arg;
4602 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004603 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004604 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004605 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004606 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004607
Jaeden Amero412654a2019-02-06 12:57:46 +00004608 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004609 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004610
Gilles Peskine8817f612018-12-18 00:18:46 +01004611 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004612
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004613 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4614 psa_set_key_algorithm( &attributes, alg );
4615 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004616
Gilles Peskine049c7532019-05-15 20:22:09 +02004617 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004618 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004619
Ronald Cron5425a212020-08-04 14:58:35 +02004620 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004621 input_data->x, input_data->len,
4622 label->x, label->len,
4623 output,
4624 output_size,
4625 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004626 ASSERT_COMPARE( expected_data->x, expected_data->len,
4627 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004628
Gilles Peskine68428122018-06-30 18:42:41 +02004629 /* If the label is empty, the test framework puts a non-null pointer
4630 * in label->x. Test that a null pointer works as well. */
4631 if( label->len == 0 )
4632 {
4633 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004634 if( output_size != 0 )
4635 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004636 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004637 input_data->x, input_data->len,
4638 NULL, label->len,
4639 output,
4640 output_size,
4641 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004642 ASSERT_COMPARE( expected_data->x, expected_data->len,
4643 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004644 }
4645
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004646exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004647 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004648 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004649 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004650 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004651}
4652/* END_CASE */
4653
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004654/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004655void asymmetric_decrypt_fail( int key_type_arg,
4656 data_t *key_data,
4657 int alg_arg,
4658 data_t *input_data,
4659 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004660 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004661 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004662{
Ronald Cron5425a212020-08-04 14:58:35 +02004663 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004664 psa_key_type_t key_type = key_type_arg;
4665 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004666 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004667 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004668 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004669 psa_status_t actual_status;
4670 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004672
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004673 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004674
Gilles Peskine8817f612018-12-18 00:18:46 +01004675 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004676
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004677 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4678 psa_set_key_algorithm( &attributes, alg );
4679 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004680
Gilles Peskine049c7532019-05-15 20:22:09 +02004681 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004682 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004683
Ronald Cron5425a212020-08-04 14:58:35 +02004684 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004685 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004686 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004687 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004688 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004689 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004690 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004691
Gilles Peskine68428122018-06-30 18:42:41 +02004692 /* If the label is empty, the test framework puts a non-null pointer
4693 * in label->x. Test that a null pointer works as well. */
4694 if( label->len == 0 )
4695 {
4696 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004697 if( output_size != 0 )
4698 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004699 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004700 input_data->x, input_data->len,
4701 NULL, label->len,
4702 output, output_size,
4703 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004704 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004705 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004706 }
4707
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004708exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004709 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004710 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004711 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004712 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004713}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004714/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004715
4716/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004717void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004718{
4719 /* Test each valid way of initializing the object, except for `= {0}`, as
4720 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4721 * though it's OK by the C standard. We could test for this, but we'd need
4722 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004723 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004724 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4725 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4726 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004727
4728 memset( &zero, 0, sizeof( zero ) );
4729
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004731 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &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( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004734 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004735 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004736 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004737
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004739 PSA_ASSERT( psa_key_derivation_abort(&func) );
4740 PSA_ASSERT( psa_key_derivation_abort(&init) );
4741 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004742}
4743/* END_CASE */
4744
Janos Follath16de4a42019-06-13 16:32:24 +01004745/* BEGIN_CASE */
4746void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004747{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004748 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004749 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004751
Gilles Peskine8817f612018-12-18 00:18:46 +01004752 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004753
Janos Follath16de4a42019-06-13 16:32:24 +01004754 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004755 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004756
4757exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004758 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004759 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004760}
4761/* END_CASE */
4762
Janos Follathaf3c2a02019-06-12 12:34:34 +01004763/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004764void derive_set_capacity( int alg_arg, int capacity_arg,
4765 int expected_status_arg )
4766{
4767 psa_algorithm_t alg = alg_arg;
4768 size_t capacity = capacity_arg;
4769 psa_status_t expected_status = expected_status_arg;
4770 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4771
4772 PSA_ASSERT( psa_crypto_init( ) );
4773
4774 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4775
4776 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4777 expected_status );
4778
4779exit:
4780 psa_key_derivation_abort( &operation );
4781 PSA_DONE( );
4782}
4783/* END_CASE */
4784
4785/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004786void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004787 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004788 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004789 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004790 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004791 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004792 int expected_status_arg3,
4793 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004794{
4795 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004796 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4797 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004798 psa_status_t expected_statuses[] = {expected_status_arg1,
4799 expected_status_arg2,
4800 expected_status_arg3};
4801 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004802 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4803 MBEDTLS_SVC_KEY_ID_INIT,
4804 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004805 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4807 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004808 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004809 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004810 psa_status_t expected_output_status = expected_output_status_arg;
4811 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004812
4813 PSA_ASSERT( psa_crypto_init( ) );
4814
4815 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4816 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004817
4818 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4819
4820 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4821 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004822 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004823 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004824 psa_set_key_type( &attributes, key_types[i] );
4825 PSA_ASSERT( psa_import_key( &attributes,
4826 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004827 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004828 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4829 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4830 {
4831 // When taking a private key as secret input, use key agreement
4832 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004833 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004834 expected_statuses[i] );
4835 }
4836 else
4837 {
4838 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004839 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004840 expected_statuses[i] );
4841 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004842 }
4843 else
4844 {
4845 TEST_EQUAL( psa_key_derivation_input_bytes(
4846 &operation, steps[i],
4847 inputs[i]->x, inputs[i]->len ),
4848 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004849 }
4850 }
4851
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004852 if( output_key_type != PSA_KEY_TYPE_NONE )
4853 {
4854 psa_reset_key_attributes( &attributes );
4855 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4856 psa_set_key_bits( &attributes, 8 );
4857 actual_output_status =
4858 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004859 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004860 }
4861 else
4862 {
4863 uint8_t buffer[1];
4864 actual_output_status =
4865 psa_key_derivation_output_bytes( &operation,
4866 buffer, sizeof( buffer ) );
4867 }
4868 TEST_EQUAL( actual_output_status, expected_output_status );
4869
Janos Follathaf3c2a02019-06-12 12:34:34 +01004870exit:
4871 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004872 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4873 psa_destroy_key( keys[i] );
4874 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004875 PSA_DONE( );
4876}
4877/* END_CASE */
4878
Janos Follathd958bb72019-07-03 15:02:16 +01004879/* BEGIN_CASE */
4880void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004881{
Janos Follathd958bb72019-07-03 15:02:16 +01004882 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004883 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004884 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004885 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004886 unsigned char input1[] = "Input 1";
4887 size_t input1_length = sizeof( input1 );
4888 unsigned char input2[] = "Input 2";
4889 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004890 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004891 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004892 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4893 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4894 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004895 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004896
Gilles Peskine8817f612018-12-18 00:18:46 +01004897 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004898
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004899 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4900 psa_set_key_algorithm( &attributes, alg );
4901 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004902
Gilles Peskine73676cb2019-05-15 20:15:10 +02004903 PSA_ASSERT( psa_import_key( &attributes,
4904 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004905 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004906
4907 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004908 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004909 input1, input1_length,
4910 input2, input2_length,
4911 capacity ) )
4912 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004913
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004915 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004916 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004917
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004919
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004921 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004922
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004923exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004924 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004925 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004926 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004927}
4928/* END_CASE */
4929
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004930/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004931void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004932{
4933 uint8_t output_buffer[16];
4934 size_t buffer_size = 16;
4935 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004937
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004938 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4939 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004940 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004941
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004942 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004943 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004944
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004946
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004947 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4948 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004949 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004950
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004951 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004952 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004953
4954exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004956}
4957/* END_CASE */
4958
4959/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004960void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004961 int step1_arg, data_t *input1,
4962 int step2_arg, data_t *input2,
4963 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004964 int requested_capacity_arg,
4965 data_t *expected_output1,
4966 data_t *expected_output2 )
4967{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004968 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004969 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4970 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004971 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4972 MBEDTLS_SVC_KEY_ID_INIT,
4973 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004974 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004976 uint8_t *expected_outputs[2] =
4977 {expected_output1->x, expected_output2->x};
4978 size_t output_sizes[2] =
4979 {expected_output1->len, expected_output2->len};
4980 size_t output_buffer_size = 0;
4981 uint8_t *output_buffer = NULL;
4982 size_t expected_capacity;
4983 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004984 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004985 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004986 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004987
4988 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4989 {
4990 if( output_sizes[i] > output_buffer_size )
4991 output_buffer_size = output_sizes[i];
4992 if( output_sizes[i] == 0 )
4993 expected_outputs[i] = NULL;
4994 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004995 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004996 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004997
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004998 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4999 psa_set_key_algorithm( &attributes, alg );
5000 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005001
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005002 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005003 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5004 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5005 requested_capacity ) );
5006 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005007 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005008 switch( steps[i] )
5009 {
5010 case 0:
5011 break;
5012 case PSA_KEY_DERIVATION_INPUT_SECRET:
5013 PSA_ASSERT( psa_import_key( &attributes,
5014 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005015 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005016 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005017 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005018 break;
5019 default:
5020 PSA_ASSERT( psa_key_derivation_input_bytes(
5021 &operation, steps[i],
5022 inputs[i]->x, inputs[i]->len ) );
5023 break;
5024 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005025 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005026
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005027 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005028 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005029 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005030 expected_capacity = requested_capacity;
5031
5032 /* Expansion phase. */
5033 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5034 {
5035 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005036 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005037 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005038 if( expected_capacity == 0 && output_sizes[i] == 0 )
5039 {
5040 /* Reading 0 bytes when 0 bytes are available can go either way. */
5041 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005042 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005043 continue;
5044 }
5045 else if( expected_capacity == 0 ||
5046 output_sizes[i] > expected_capacity )
5047 {
5048 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005049 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005050 expected_capacity = 0;
5051 continue;
5052 }
5053 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005054 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005055 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005056 ASSERT_COMPARE( output_buffer, output_sizes[i],
5057 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005058 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005059 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005060 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005061 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005062 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005063 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005065
5066exit:
5067 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005068 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005069 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5070 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005071 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005072}
5073/* END_CASE */
5074
5075/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005076void derive_full( int alg_arg,
5077 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005078 data_t *input1,
5079 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005080 int requested_capacity_arg )
5081{
Ronald Cron5425a212020-08-04 14:58:35 +02005082 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005083 psa_algorithm_t alg = alg_arg;
5084 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005085 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005086 unsigned char output_buffer[16];
5087 size_t expected_capacity = requested_capacity;
5088 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005089 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005090
Gilles Peskine8817f612018-12-18 00:18:46 +01005091 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005092
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005093 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5094 psa_set_key_algorithm( &attributes, alg );
5095 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005096
Gilles Peskine049c7532019-05-15 20:22:09 +02005097 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005098 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005099
Ronald Cron5425a212020-08-04 14:58:35 +02005100 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005101 input1->x, input1->len,
5102 input2->x, input2->len,
5103 requested_capacity ) )
5104 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005105
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005106 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005107 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005108 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005109
5110 /* Expansion phase. */
5111 while( current_capacity > 0 )
5112 {
5113 size_t read_size = sizeof( output_buffer );
5114 if( read_size > current_capacity )
5115 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005117 output_buffer,
5118 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005119 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005120 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005121 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005122 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005123 }
5124
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 /* Check that the operation refuses to go over capacity. */
5126 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005127 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005128
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005129 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005130
5131exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005133 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005134 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005135}
5136/* END_CASE */
5137
Janos Follathe60c9052019-07-03 13:51:30 +01005138/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005139void derive_key_exercise( int alg_arg,
5140 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005141 data_t *input1,
5142 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005143 int derived_type_arg,
5144 int derived_bits_arg,
5145 int derived_usage_arg,
5146 int derived_alg_arg )
5147{
Ronald Cron5425a212020-08-04 14:58:35 +02005148 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5149 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005150 psa_algorithm_t alg = alg_arg;
5151 psa_key_type_t derived_type = derived_type_arg;
5152 size_t derived_bits = derived_bits_arg;
5153 psa_key_usage_t derived_usage = derived_usage_arg;
5154 psa_algorithm_t derived_alg = derived_alg_arg;
5155 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005156 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005158 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005159
Gilles Peskine8817f612018-12-18 00:18:46 +01005160 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005161
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005162 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5163 psa_set_key_algorithm( &attributes, alg );
5164 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005165 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005166 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005167
5168 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005169 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005170 input1->x, input1->len,
5171 input2->x, input2->len, capacity ) )
5172 goto exit;
5173
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005174 psa_set_key_usage_flags( &attributes, derived_usage );
5175 psa_set_key_algorithm( &attributes, derived_alg );
5176 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005177 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005178 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005179 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005180
5181 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005182 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005183 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5184 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005185
5186 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005187 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005188 goto exit;
5189
5190exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005191 /*
5192 * Key attributes may have been returned by psa_get_key_attributes()
5193 * thus reset them as required.
5194 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005195 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005196
5197 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005198 psa_destroy_key( base_key );
5199 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005200 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005201}
5202/* END_CASE */
5203
Janos Follath42fd8882019-07-03 14:17:09 +01005204/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005205void derive_key_export( int alg_arg,
5206 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005207 data_t *input1,
5208 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005209 int bytes1_arg,
5210 int bytes2_arg )
5211{
Ronald Cron5425a212020-08-04 14:58:35 +02005212 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5213 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005214 psa_algorithm_t alg = alg_arg;
5215 size_t bytes1 = bytes1_arg;
5216 size_t bytes2 = bytes2_arg;
5217 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005219 uint8_t *output_buffer = NULL;
5220 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005221 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5222 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005223 size_t length;
5224
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005225 ASSERT_ALLOC( output_buffer, capacity );
5226 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005227 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005228
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005229 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5230 psa_set_key_algorithm( &base_attributes, alg );
5231 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005232 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005233 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005234
5235 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005236 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005237 input1->x, input1->len,
5238 input2->x, input2->len, capacity ) )
5239 goto exit;
5240
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005241 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005242 output_buffer,
5243 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005244 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005245
5246 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005247 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005248 input1->x, input1->len,
5249 input2->x, input2->len, capacity ) )
5250 goto exit;
5251
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005252 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5253 psa_set_key_algorithm( &derived_attributes, 0 );
5254 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005255 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005257 &derived_key ) );
5258 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005259 export_buffer, bytes1,
5260 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005261 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005262 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005263 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005264 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005265 &derived_key ) );
5266 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005267 export_buffer + bytes1, bytes2,
5268 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005269 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005270
5271 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005272 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5273 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005274
5275exit:
5276 mbedtls_free( output_buffer );
5277 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005278 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005279 psa_destroy_key( base_key );
5280 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005281 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005282}
5283/* END_CASE */
5284
5285/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005286void derive_key( int alg_arg,
5287 data_t *key_data, data_t *input1, data_t *input2,
5288 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005289 int expected_status_arg,
5290 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005291{
Ronald Cron5425a212020-08-04 14:58:35 +02005292 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5293 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005294 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005295 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005296 size_t bits = bits_arg;
5297 psa_status_t expected_status = expected_status_arg;
5298 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5299 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5300 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5301
5302 PSA_ASSERT( psa_crypto_init( ) );
5303
5304 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5305 psa_set_key_algorithm( &base_attributes, alg );
5306 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5307 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005308 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005309
Ronald Cron5425a212020-08-04 14:58:35 +02005310 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005311 input1->x, input1->len,
5312 input2->x, input2->len, SIZE_MAX ) )
5313 goto exit;
5314
5315 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5316 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005317 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005318 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005319
5320 psa_status_t status =
5321 psa_key_derivation_output_key( &derived_attributes,
5322 &operation,
5323 &derived_key );
5324 if( is_large_output > 0 )
5325 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5326 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005327
5328exit:
5329 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005330 psa_destroy_key( base_key );
5331 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005332 PSA_DONE( );
5333}
5334/* END_CASE */
5335
5336/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005337void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005338 int our_key_type_arg, int our_key_alg_arg,
5339 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005340 int expected_status_arg )
5341{
Ronald Cron5425a212020-08-04 14:58:35 +02005342 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005343 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005344 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005345 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005346 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005348 psa_status_t expected_status = expected_status_arg;
5349 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005350
Gilles Peskine8817f612018-12-18 00:18:46 +01005351 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005352
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005353 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005354 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005355 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005356 PSA_ASSERT( psa_import_key( &attributes,
5357 our_key_data->x, our_key_data->len,
5358 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005359
Gilles Peskine77f40d82019-04-11 21:27:06 +02005360 /* The tests currently include inputs that should fail at either step.
5361 * Test cases that fail at the setup step should be changed to call
5362 * key_derivation_setup instead, and this function should be renamed
5363 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005364 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005365 if( status == PSA_SUCCESS )
5366 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005367 TEST_EQUAL( psa_key_derivation_key_agreement(
5368 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5369 our_key,
5370 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005371 expected_status );
5372 }
5373 else
5374 {
5375 TEST_ASSERT( status == expected_status );
5376 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005377
5378exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005379 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005380 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005381 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005382}
5383/* END_CASE */
5384
5385/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005386void raw_key_agreement( int alg_arg,
5387 int our_key_type_arg, data_t *our_key_data,
5388 data_t *peer_key_data,
5389 data_t *expected_output )
5390{
Ronald Cron5425a212020-08-04 14:58:35 +02005391 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005392 psa_algorithm_t alg = alg_arg;
5393 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005394 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005395 unsigned char *output = NULL;
5396 size_t output_length = ~0;
5397
5398 ASSERT_ALLOC( output, expected_output->len );
5399 PSA_ASSERT( psa_crypto_init( ) );
5400
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005401 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5402 psa_set_key_algorithm( &attributes, alg );
5403 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005404 PSA_ASSERT( psa_import_key( &attributes,
5405 our_key_data->x, our_key_data->len,
5406 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005407
Gilles Peskinebe697d82019-05-16 18:00:41 +02005408 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5409 peer_key_data->x, peer_key_data->len,
5410 output, expected_output->len,
5411 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005412 ASSERT_COMPARE( output, output_length,
5413 expected_output->x, expected_output->len );
5414
5415exit:
5416 mbedtls_free( output );
5417 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005418 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005419}
5420/* END_CASE */
5421
5422/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005423void key_agreement_capacity( int alg_arg,
5424 int our_key_type_arg, data_t *our_key_data,
5425 data_t *peer_key_data,
5426 int expected_capacity_arg )
5427{
Ronald Cron5425a212020-08-04 14:58:35 +02005428 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005429 psa_algorithm_t alg = alg_arg;
5430 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005431 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005432 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005433 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005434 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005435
Gilles Peskine8817f612018-12-18 00:18:46 +01005436 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005437
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005438 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5439 psa_set_key_algorithm( &attributes, alg );
5440 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005441 PSA_ASSERT( psa_import_key( &attributes,
5442 our_key_data->x, our_key_data->len,
5443 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005444
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005445 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005446 PSA_ASSERT( psa_key_derivation_key_agreement(
5447 &operation,
5448 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5449 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005450 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5451 {
5452 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005453 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005454 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005455 NULL, 0 ) );
5456 }
Gilles Peskine59685592018-09-18 12:11:34 +02005457
Gilles Peskinebf491972018-10-25 22:36:12 +02005458 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005459 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005460 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005461 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005462
Gilles Peskinebf491972018-10-25 22:36:12 +02005463 /* Test the actual capacity by reading the output. */
5464 while( actual_capacity > sizeof( output ) )
5465 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005467 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005468 actual_capacity -= sizeof( output );
5469 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005471 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005472 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005473 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005474
Gilles Peskine59685592018-09-18 12:11:34 +02005475exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005476 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005477 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005478 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005479}
5480/* END_CASE */
5481
5482/* BEGIN_CASE */
5483void key_agreement_output( int alg_arg,
5484 int our_key_type_arg, data_t *our_key_data,
5485 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005486 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005487{
Ronald Cron5425a212020-08-04 14:58:35 +02005488 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005489 psa_algorithm_t alg = alg_arg;
5490 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005491 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005492 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005493 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005494
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005495 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5496 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005497
Gilles Peskine8817f612018-12-18 00:18:46 +01005498 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005499
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005500 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5501 psa_set_key_algorithm( &attributes, alg );
5502 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005503 PSA_ASSERT( psa_import_key( &attributes,
5504 our_key_data->x, our_key_data->len,
5505 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005506
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005507 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005508 PSA_ASSERT( psa_key_derivation_key_agreement(
5509 &operation,
5510 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5511 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005512 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5513 {
5514 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005516 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005517 NULL, 0 ) );
5518 }
Gilles Peskine59685592018-09-18 12:11:34 +02005519
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005520 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005521 actual_output,
5522 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005523 ASSERT_COMPARE( actual_output, expected_output1->len,
5524 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005525 if( expected_output2->len != 0 )
5526 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005527 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005528 actual_output,
5529 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005530 ASSERT_COMPARE( actual_output, expected_output2->len,
5531 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005532 }
Gilles Peskine59685592018-09-18 12:11:34 +02005533
5534exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005535 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005536 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005537 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005538 mbedtls_free( actual_output );
5539}
5540/* END_CASE */
5541
5542/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005543void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005544{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005545 size_t bytes = bytes_arg;
5546 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005547 unsigned char *output = NULL;
5548 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005549 size_t i;
5550 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005551
Simon Butcher49f8e312020-03-03 15:51:50 +00005552 TEST_ASSERT( bytes_arg >= 0 );
5553
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005554 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5555 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005556 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005557
Gilles Peskine8817f612018-12-18 00:18:46 +01005558 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005559
Gilles Peskinea50d7392018-06-21 10:22:13 +02005560 /* Run several times, to ensure that every output byte will be
5561 * nonzero at least once with overwhelming probability
5562 * (2^(-8*number_of_runs)). */
5563 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005564 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005565 if( bytes != 0 )
5566 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005567 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005568
5569 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005570 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5571 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005572
5573 for( i = 0; i < bytes; i++ )
5574 {
5575 if( output[i] != 0 )
5576 ++changed[i];
5577 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005578 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005579
5580 /* Check that every byte was changed to nonzero at least once. This
5581 * validates that psa_generate_random is overwriting every byte of
5582 * the output buffer. */
5583 for( i = 0; i < bytes; i++ )
5584 {
5585 TEST_ASSERT( changed[i] != 0 );
5586 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005587
5588exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005589 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005590 mbedtls_free( output );
5591 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005592}
5593/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005594
5595/* BEGIN_CASE */
5596void generate_key( int type_arg,
5597 int bits_arg,
5598 int usage_arg,
5599 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005600 int expected_status_arg,
5601 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005602{
Ronald Cron5425a212020-08-04 14:58:35 +02005603 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005604 psa_key_type_t type = type_arg;
5605 psa_key_usage_t usage = usage_arg;
5606 size_t bits = bits_arg;
5607 psa_algorithm_t alg = alg_arg;
5608 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005610 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005611
Gilles Peskine8817f612018-12-18 00:18:46 +01005612 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005613
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005614 psa_set_key_usage_flags( &attributes, usage );
5615 psa_set_key_algorithm( &attributes, alg );
5616 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005617 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005618
5619 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005620 psa_status_t status = psa_generate_key( &attributes, &key );
5621
5622 if( is_large_key > 0 )
5623 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5624 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005625 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005626 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005627
5628 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005629 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005630 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5631 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005632
Gilles Peskine818ca122018-06-20 18:16:48 +02005633 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005634 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005635 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005636
5637exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005638 /*
5639 * Key attributes may have been returned by psa_get_key_attributes()
5640 * thus reset them as required.
5641 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005642 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005643
Ronald Cron5425a212020-08-04 14:58:35 +02005644 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005645 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005646}
5647/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005648
Gilles Peskinee56e8782019-04-26 17:34:02 +02005649/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5650void generate_key_rsa( int bits_arg,
5651 data_t *e_arg,
5652 int expected_status_arg )
5653{
Ronald Cron5425a212020-08-04 14:58:35 +02005654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005655 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005656 size_t bits = bits_arg;
5657 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5658 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5659 psa_status_t expected_status = expected_status_arg;
5660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5661 uint8_t *exported = NULL;
5662 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005663 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005664 size_t exported_length = SIZE_MAX;
5665 uint8_t *e_read_buffer = NULL;
5666 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005667 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005668 size_t e_read_length = SIZE_MAX;
5669
5670 if( e_arg->len == 0 ||
5671 ( e_arg->len == 3 &&
5672 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5673 {
5674 is_default_public_exponent = 1;
5675 e_read_size = 0;
5676 }
5677 ASSERT_ALLOC( e_read_buffer, e_read_size );
5678 ASSERT_ALLOC( exported, exported_size );
5679
5680 PSA_ASSERT( psa_crypto_init( ) );
5681
5682 psa_set_key_usage_flags( &attributes, usage );
5683 psa_set_key_algorithm( &attributes, alg );
5684 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5685 e_arg->x, e_arg->len ) );
5686 psa_set_key_bits( &attributes, bits );
5687
5688 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005689 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005690 if( expected_status != PSA_SUCCESS )
5691 goto exit;
5692
5693 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005694 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005695 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5696 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5697 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5698 e_read_buffer, e_read_size,
5699 &e_read_length ) );
5700 if( is_default_public_exponent )
5701 TEST_EQUAL( e_read_length, 0 );
5702 else
5703 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5704
5705 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005706 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005707 goto exit;
5708
5709 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005710 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005711 exported, exported_size,
5712 &exported_length ) );
5713 {
5714 uint8_t *p = exported;
5715 uint8_t *end = exported + exported_length;
5716 size_t len;
5717 /* RSAPublicKey ::= SEQUENCE {
5718 * modulus INTEGER, -- n
5719 * publicExponent INTEGER } -- e
5720 */
5721 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005722 MBEDTLS_ASN1_SEQUENCE |
5723 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005724 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5725 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5726 MBEDTLS_ASN1_INTEGER ) );
5727 if( len >= 1 && p[0] == 0 )
5728 {
5729 ++p;
5730 --len;
5731 }
5732 if( e_arg->len == 0 )
5733 {
5734 TEST_EQUAL( len, 3 );
5735 TEST_EQUAL( p[0], 1 );
5736 TEST_EQUAL( p[1], 0 );
5737 TEST_EQUAL( p[2], 1 );
5738 }
5739 else
5740 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5741 }
5742
5743exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005744 /*
5745 * Key attributes may have been returned by psa_get_key_attributes() or
5746 * set by psa_set_key_domain_parameters() thus reset them as required.
5747 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005748 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005749
Ronald Cron5425a212020-08-04 14:58:35 +02005750 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005751 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005752 mbedtls_free( e_read_buffer );
5753 mbedtls_free( exported );
5754}
5755/* END_CASE */
5756
Darryl Greend49a4992018-06-18 17:27:26 +01005757/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005758void persistent_key_load_key_from_storage( data_t *data,
5759 int type_arg, int bits_arg,
5760 int usage_flags_arg, int alg_arg,
5761 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005762{
Ronald Cron71016a92020-08-28 19:01:50 +02005763 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5766 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005767 psa_key_type_t type = type_arg;
5768 size_t bits = bits_arg;
5769 psa_key_usage_t usage_flags = usage_flags_arg;
5770 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005771 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005772 unsigned char *first_export = NULL;
5773 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005774 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005775 size_t first_exported_length;
5776 size_t second_exported_length;
5777
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005778 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5779 {
5780 ASSERT_ALLOC( first_export, export_size );
5781 ASSERT_ALLOC( second_export, export_size );
5782 }
Darryl Greend49a4992018-06-18 17:27:26 +01005783
Gilles Peskine8817f612018-12-18 00:18:46 +01005784 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005785
Gilles Peskinec87af662019-05-15 16:12:22 +02005786 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005787 psa_set_key_usage_flags( &attributes, usage_flags );
5788 psa_set_key_algorithm( &attributes, alg );
5789 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005790 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005791
Darryl Green0c6575a2018-11-07 16:05:30 +00005792 switch( generation_method )
5793 {
5794 case IMPORT_KEY:
5795 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005796 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005797 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005798 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005799
Darryl Green0c6575a2018-11-07 16:05:30 +00005800 case GENERATE_KEY:
5801 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005802 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005803 break;
5804
5805 case DERIVE_KEY:
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005806#if PSA_WANT_ALG_HKDF && PSA_WANT_ALG_SHA_256
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005807 {
5808 /* Create base key */
5809 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5810 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5811 psa_set_key_usage_flags( &base_attributes,
5812 PSA_KEY_USAGE_DERIVE );
5813 psa_set_key_algorithm( &base_attributes, derive_alg );
5814 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005815 PSA_ASSERT( psa_import_key( &base_attributes,
5816 data->x, data->len,
5817 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005818 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005819 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005820 PSA_ASSERT( psa_key_derivation_input_key(
5821 &operation,
5822 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005823 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005824 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005825 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005826 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5827 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005828 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005829 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005830 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005831 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005832 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005833#else
5834 TEST_ASSUME( ! "KDF not supported in this configuration" );
5835#endif
5836 break;
5837
5838 default:
5839 TEST_ASSERT( ! "generation_method not implemented in test" );
5840 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005841 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005842 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005843
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005844 /* Export the key if permitted by the key policy. */
5845 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5846 {
Ronald Cron5425a212020-08-04 14:58:35 +02005847 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005848 first_export, export_size,
5849 &first_exported_length ) );
5850 if( generation_method == IMPORT_KEY )
5851 ASSERT_COMPARE( data->x, data->len,
5852 first_export, first_exported_length );
5853 }
Darryl Greend49a4992018-06-18 17:27:26 +01005854
5855 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005856 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005857 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005858 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005859
Darryl Greend49a4992018-06-18 17:27:26 +01005860 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005861 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005862 TEST_ASSERT( mbedtls_svc_key_id_equal(
5863 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005864 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5865 PSA_KEY_LIFETIME_PERSISTENT );
5866 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5867 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5868 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5869 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005870
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005871 /* Export the key again if permitted by the key policy. */
5872 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005873 {
Ronald Cron5425a212020-08-04 14:58:35 +02005874 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005875 second_export, export_size,
5876 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005877 ASSERT_COMPARE( first_export, first_exported_length,
5878 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005879 }
5880
5881 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005882 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005883 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005884
5885exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005886 /*
5887 * Key attributes may have been returned by psa_get_key_attributes()
5888 * thus reset them as required.
5889 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005890 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005891
Darryl Greend49a4992018-06-18 17:27:26 +01005892 mbedtls_free( first_export );
5893 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005894 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005895 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005896 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005897 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005898}
5899/* END_CASE */