blob: 66446a8fc3351ffbdb0d0c99e296e67f483fbe10 [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
Ronald Cron28a45ed2021-02-09 20:35:42 +010015#include "test/psa_crypto_helpers.h"
16
Jaeden Amerof24c7f82018-06-27 17:20:43 +010017/** An invalid export length that will never be set by psa_export_key(). */
18static const size_t INVALID_EXPORT_LENGTH = ~0U;
19
Gilles Peskinef426e0f2019-02-25 17:42:03 +010020/* A hash algorithm that is known to be supported.
21 *
22 * This is used in some smoke tests.
23 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010024#if defined(PSA_WANT_ALG_MD2)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010025#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
Gilles Peskined6dc40c2021-01-12 12:55:31 +010026#elif defined(PSA_WANT_ALG_MD4)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010027#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
Gilles Peskined6dc40c2021-01-12 12:55:31 +010028#elif defined(PSA_WANT_ALG_MD5)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010029#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
30/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
31 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
32 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
33 * implausible anyway. */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010034#elif defined(PSA_WANT_ALG_SHA_1)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010035#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
Gilles Peskined6dc40c2021-01-12 12:55:31 +010036#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010037#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
Gilles Peskined6dc40c2021-01-12 12:55:31 +010038#elif defined(PSA_WANT_ALG_SHA_384)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010039#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
Gilles Peskined6dc40c2021-01-12 12:55:31 +010040#elif defined(PSA_WANT_ALG_SHA_512)
41#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
42#elif defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010043#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
44#else
45#undef KNOWN_SUPPORTED_HASH_ALG
46#endif
47
48/* A block cipher that is known to be supported.
49 *
50 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
51 */
52#if defined(MBEDTLS_AES_C)
53#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
54#elif defined(MBEDTLS_ARIA_C)
55#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
56#elif defined(MBEDTLS_CAMELLIA_C)
57#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
58#undef KNOWN_SUPPORTED_BLOCK_CIPHER
59#endif
60
61/* A MAC mode that is known to be supported.
62 *
63 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
64 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
65 *
66 * This is used in some smoke tests.
67 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010068#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010069#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
70#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
71#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
72#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
73#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
74#else
75#undef KNOWN_SUPPORTED_MAC_ALG
76#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
77#endif
78
79/* A cipher algorithm and key type that are known to be supported.
80 *
81 * This is used in some smoke tests.
82 */
83#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
84#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
85#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
86#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
87#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
89#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
90#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
91#else
92#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
93#endif
94#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
95#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
96#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
97#elif defined(MBEDTLS_RC4_C)
98#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
99#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
100#else
101#undef KNOWN_SUPPORTED_CIPHER_ALG
102#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
103#endif
104
Gilles Peskine667c1112019-12-03 19:03:20 +0100105#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100106int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
Gilles Peskine667c1112019-12-03 19:03:20 +0100107{
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100108 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
109 PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100110}
111#else
112int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
113{
114 (void) lifetime;
115 return( 0 );
116}
117#endif
118
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200119/** Test if a buffer contains a constant byte value.
120 *
121 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200122 *
123 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200124 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200125 * \param size Size of the buffer in bytes.
126 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200127 * \return 1 if the buffer is all-bits-zero.
128 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200129 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200130static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200131{
132 size_t i;
133 for( i = 0; i < size; i++ )
134 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200135 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200136 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200137 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200138 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200139}
Gilles Peskine818ca122018-06-20 18:16:48 +0200140
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200141/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
142static int asn1_write_10x( unsigned char **p,
143 unsigned char *start,
144 size_t bits,
145 unsigned char x )
146{
147 int ret;
148 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200149 if( bits == 0 )
150 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
151 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200152 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300153 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200154 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
155 *p -= len;
156 ( *p )[len-1] = x;
157 if( bits % 8 == 0 )
158 ( *p )[1] |= 1;
159 else
160 ( *p )[0] |= 1 << ( bits % 8 );
161 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
162 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
163 MBEDTLS_ASN1_INTEGER ) );
164 return( len );
165}
166
167static int construct_fake_rsa_key( unsigned char *buffer,
168 size_t buffer_size,
169 unsigned char **p,
170 size_t bits,
171 int keypair )
172{
173 size_t half_bits = ( bits + 1 ) / 2;
174 int ret;
175 int len = 0;
176 /* Construct something that looks like a DER encoding of
177 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
178 * RSAPrivateKey ::= SEQUENCE {
179 * version Version,
180 * modulus INTEGER, -- n
181 * publicExponent INTEGER, -- e
182 * privateExponent INTEGER, -- d
183 * prime1 INTEGER, -- p
184 * prime2 INTEGER, -- q
185 * exponent1 INTEGER, -- d mod (p-1)
186 * exponent2 INTEGER, -- d mod (q-1)
187 * coefficient INTEGER, -- (inverse of q) mod p
188 * otherPrimeInfos OtherPrimeInfos OPTIONAL
189 * }
190 * Or, for a public key, the same structure with only
191 * version, modulus and publicExponent.
192 */
193 *p = buffer + buffer_size;
194 if( keypair )
195 {
196 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
197 asn1_write_10x( p, buffer, half_bits, 1 ) );
198 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
199 asn1_write_10x( p, buffer, half_bits, 1 ) );
200 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
201 asn1_write_10x( p, buffer, half_bits, 1 ) );
202 MBEDTLS_ASN1_CHK_ADD( len, /* q */
203 asn1_write_10x( p, buffer, half_bits, 1 ) );
204 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
205 asn1_write_10x( p, buffer, half_bits, 3 ) );
206 MBEDTLS_ASN1_CHK_ADD( len, /* d */
207 asn1_write_10x( p, buffer, bits, 1 ) );
208 }
209 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
210 asn1_write_10x( p, buffer, 17, 1 ) );
211 MBEDTLS_ASN1_CHK_ADD( len, /* n */
212 asn1_write_10x( p, buffer, bits, 1 ) );
213 if( keypair )
214 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
215 mbedtls_asn1_write_int( p, buffer, 0 ) );
216 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
217 {
218 const unsigned char tag =
219 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
220 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
221 }
222 return( len );
223}
224
Ronald Cron5425a212020-08-04 14:58:35 +0200225int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100226{
227 int ok = 0;
228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
229 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200230 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100231 psa_key_type_t type;
232 psa_key_type_t bits;
233
234 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
235 lifetime = psa_get_key_lifetime( &attributes );
236 id = psa_get_key_id( &attributes );
237 type = psa_get_key_type( &attributes );
238 bits = psa_get_key_bits( &attributes );
239
240 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200241 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200242 {
243 TEST_ASSERT(
244 ( PSA_KEY_ID_VOLATILE_MIN <=
245 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
246 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
247 PSA_KEY_ID_VOLATILE_MAX ) );
248 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100249 else
250 {
251 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200252 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
253 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100254 }
255#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
256 /* randomly-generated 64-bit constant, should never appear in test data */
257 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
258 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100259 if( lifetime_is_dynamic_secure_element( lifetime ) )
Gilles Peskine667c1112019-12-03 19:03:20 +0100260 {
261 /* Mbed Crypto currently always exposes the slot number to
262 * applications. This is not mandated by the PSA specification
263 * and may change in future versions. */
264 TEST_EQUAL( status, 0 );
265 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
266 }
267 else
268 {
269 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
270 }
271#endif
272
273 /* Type and size */
274 TEST_ASSERT( type != 0 );
275 TEST_ASSERT( bits != 0 );
276 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
277 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
278 TEST_ASSERT( bits % 8 == 0 );
279
280 /* MAX macros concerning specific key types */
281 if( PSA_KEY_TYPE_IS_ECC( type ) )
282 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
283 else if( PSA_KEY_TYPE_IS_RSA( type ) )
284 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100285 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100286
287 ok = 1;
288
289exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100290 /*
291 * Key attributes may have been returned by psa_get_key_attributes()
292 * thus reset them as required.
293 */
Gilles Peskine667c1112019-12-03 19:03:20 +0100294 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100295
Gilles Peskine667c1112019-12-03 19:03:20 +0100296 return( ok );
297}
298
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100299int exercise_mac_setup( psa_key_type_t key_type,
300 const unsigned char *key_bytes,
301 size_t key_length,
302 psa_algorithm_t alg,
303 psa_mac_operation_t *operation,
304 psa_status_t *status )
305{
Ronald Cron5425a212020-08-04 14:58:35 +0200306 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200307 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100308
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100309 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200310 psa_set_key_algorithm( &attributes, alg );
311 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200312 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100313
Ronald Cron5425a212020-08-04 14:58:35 +0200314 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100315 /* Whether setup succeeded or failed, abort must succeed. */
316 PSA_ASSERT( psa_mac_abort( operation ) );
317 /* If setup failed, reproduce the failure, so that the caller can
318 * test the resulting state of the operation object. */
319 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100320 {
Ronald Cron5425a212020-08-04 14:58:35 +0200321 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100322 }
323
Ronald Cron5425a212020-08-04 14:58:35 +0200324 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100325 return( 1 );
326
327exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200328 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100329 return( 0 );
330}
331
332int exercise_cipher_setup( psa_key_type_t key_type,
333 const unsigned char *key_bytes,
334 size_t key_length,
335 psa_algorithm_t alg,
336 psa_cipher_operation_t *operation,
337 psa_status_t *status )
338{
Ronald Cron5425a212020-08-04 14:58:35 +0200339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100341
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
343 psa_set_key_algorithm( &attributes, alg );
344 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200345 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100346
Ronald Cron5425a212020-08-04 14:58:35 +0200347 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100348 /* Whether setup succeeded or failed, abort must succeed. */
349 PSA_ASSERT( psa_cipher_abort( operation ) );
350 /* If setup failed, reproduce the failure, so that the caller can
351 * test the resulting state of the operation object. */
352 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100353 {
Ronald Cron5425a212020-08-04 14:58:35 +0200354 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100355 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100356 }
357
Ronald Cron5425a212020-08-04 14:58:35 +0200358 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100359 return( 1 );
360
361exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200362 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100363 return( 0 );
364}
365
Ronald Cron5425a212020-08-04 14:58:35 +0200366static int exercise_mac_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200367 psa_key_usage_t usage,
368 psa_algorithm_t alg )
369{
Jaeden Amero769ce272019-01-04 11:48:03 +0000370 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200371 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200372 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200373 size_t mac_length = sizeof( mac );
374
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100375 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200376 {
Ronald Cron5425a212020-08-04 14:58:35 +0200377 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100378 PSA_ASSERT( psa_mac_update( &operation,
379 input, sizeof( input ) ) );
380 PSA_ASSERT( psa_mac_sign_finish( &operation,
381 mac, sizeof( mac ),
382 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200383 }
384
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100385 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200386 {
387 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100388 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200389 PSA_SUCCESS :
390 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200391 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100392 PSA_ASSERT( psa_mac_update( &operation,
393 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100394 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
395 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200396 }
397
398 return( 1 );
399
400exit:
401 psa_mac_abort( &operation );
402 return( 0 );
403}
404
Ronald Cron5425a212020-08-04 14:58:35 +0200405static int exercise_cipher_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200406 psa_key_usage_t usage,
407 psa_algorithm_t alg )
408{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000409 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200410 unsigned char iv[16] = {0};
411 size_t iv_length = sizeof( iv );
412 const unsigned char plaintext[16] = "Hello, world...";
413 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
414 size_t ciphertext_length = sizeof( ciphertext );
415 unsigned char decrypted[sizeof( ciphertext )];
416 size_t part_length;
417
418 if( usage & PSA_KEY_USAGE_ENCRYPT )
419 {
Ronald Cron5425a212020-08-04 14:58:35 +0200420 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100421 PSA_ASSERT( psa_cipher_generate_iv( &operation,
422 iv, sizeof( iv ),
423 &iv_length ) );
424 PSA_ASSERT( psa_cipher_update( &operation,
425 plaintext, sizeof( plaintext ),
426 ciphertext, sizeof( ciphertext ),
427 &ciphertext_length ) );
428 PSA_ASSERT( psa_cipher_finish( &operation,
429 ciphertext + ciphertext_length,
430 sizeof( ciphertext ) - ciphertext_length,
431 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200432 ciphertext_length += part_length;
433 }
434
435 if( usage & PSA_KEY_USAGE_DECRYPT )
436 {
437 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200438 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200439 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
440 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200442 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200443 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
444 * have this macro yet. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100445 iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200446 psa_get_key_type( &attributes ) );
447 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100448 psa_reset_key_attributes( &attributes );
Gilles Peskine818ca122018-06-20 18:16:48 +0200449 }
Ronald Cron5425a212020-08-04 14:58:35 +0200450 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100451 PSA_ASSERT( psa_cipher_set_iv( &operation,
452 iv, iv_length ) );
453 PSA_ASSERT( psa_cipher_update( &operation,
454 ciphertext, ciphertext_length,
455 decrypted, sizeof( decrypted ),
456 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200457 status = psa_cipher_finish( &operation,
458 decrypted + part_length,
459 sizeof( decrypted ) - part_length,
460 &part_length );
461 /* For a stream cipher, all inputs are valid. For a block cipher,
462 * if the input is some aribtrary data rather than an actual
463 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200464 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200465 TEST_ASSERT( status == PSA_SUCCESS ||
466 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200467 else
468 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200469 }
470
471 return( 1 );
472
473exit:
474 psa_cipher_abort( &operation );
475 return( 0 );
476}
477
Ronald Cron5425a212020-08-04 14:58:35 +0200478static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200479 psa_key_usage_t usage,
480 psa_algorithm_t alg )
481{
482 unsigned char nonce[16] = {0};
483 size_t nonce_length = sizeof( nonce );
484 unsigned char plaintext[16] = "Hello, world...";
485 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
486 size_t ciphertext_length = sizeof( ciphertext );
487 size_t plaintext_length = sizeof( ciphertext );
488
Steven Cooreman2f099132021-01-11 20:33:45 +0100489 /* Default IV length for AES-GCM is 12 bytes */
Bence Szépkútia63b20d2020-12-16 11:36:46 +0100490 if( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ==
491 PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ) )
Steven Cooreman50f1f5e2021-01-25 10:26:49 +0100492 {
Steven Cooreman2f099132021-01-11 20:33:45 +0100493 nonce_length = 12;
Steven Cooreman50f1f5e2021-01-25 10:26:49 +0100494 }
Steven Cooreman2f099132021-01-11 20:33:45 +0100495
Gilles Peskine818ca122018-06-20 18:16:48 +0200496 if( usage & PSA_KEY_USAGE_ENCRYPT )
497 {
Ronald Cron5425a212020-08-04 14:58:35 +0200498 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100499 nonce, nonce_length,
500 NULL, 0,
501 plaintext, sizeof( plaintext ),
502 ciphertext, sizeof( ciphertext ),
503 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200504 }
505
506 if( usage & PSA_KEY_USAGE_DECRYPT )
507 {
508 psa_status_t verify_status =
509 ( usage & PSA_KEY_USAGE_ENCRYPT ?
510 PSA_SUCCESS :
511 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200512 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100513 nonce, nonce_length,
514 NULL, 0,
515 ciphertext, ciphertext_length,
516 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100517 &plaintext_length ),
518 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200519 }
520
521 return( 1 );
522
523exit:
524 return( 0 );
525}
526
Ronald Cron5425a212020-08-04 14:58:35 +0200527static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200528 psa_key_usage_t usage,
529 psa_algorithm_t alg )
530{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200531 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
532 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100533 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200534 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100535 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
536
537 /* If the policy allows signing with any hash, just pick one. */
538 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
539 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100540#if defined(KNOWN_SUPPORTED_HASH_ALG)
541 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
542 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100543#else
Chris Jones9634bb12021-01-20 15:56:42 +0000544 mbedtls_test_fail( "No hash algorithm for hash-and-sign testing",
545 __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100546 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100547#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100548 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200549
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100550 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200551 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200552 /* Some algorithms require the payload to have the size of
553 * the hash encoded in the algorithm. Use this input size
554 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200555 if( hash_alg != 0 )
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100556 payload_length = PSA_HASH_LENGTH( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200557 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100558 payload, payload_length,
559 signature, sizeof( signature ),
560 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200561 }
562
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100563 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200564 {
565 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100566 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200567 PSA_SUCCESS :
568 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200569 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100570 payload, payload_length,
571 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100572 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200573 }
574
575 return( 1 );
576
577exit:
578 return( 0 );
579}
580
Ronald Cron5425a212020-08-04 14:58:35 +0200581static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200582 psa_key_usage_t usage,
583 psa_algorithm_t alg )
584{
585 unsigned char plaintext[256] = "Hello, world...";
586 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
587 size_t ciphertext_length = sizeof( ciphertext );
588 size_t plaintext_length = 16;
589
590 if( usage & PSA_KEY_USAGE_ENCRYPT )
591 {
Ronald Cron5425a212020-08-04 14:58:35 +0200592 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100593 plaintext, plaintext_length,
594 NULL, 0,
595 ciphertext, sizeof( ciphertext ),
596 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200597 }
598
599 if( usage & PSA_KEY_USAGE_DECRYPT )
600 {
601 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200602 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200603 ciphertext, ciphertext_length,
604 NULL, 0,
605 plaintext, sizeof( plaintext ),
606 &plaintext_length );
607 TEST_ASSERT( status == PSA_SUCCESS ||
608 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
609 ( status == PSA_ERROR_INVALID_ARGUMENT ||
610 status == PSA_ERROR_INVALID_PADDING ) ) );
611 }
612
613 return( 1 );
614
615exit:
616 return( 0 );
617}
Gilles Peskine02b75072018-07-01 22:31:34 +0200618
Janos Follathf2815ea2019-07-03 12:41:36 +0100619static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200620 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100621 psa_algorithm_t alg,
622 unsigned char* input1, size_t input1_length,
623 unsigned char* input2, size_t input2_length,
624 size_t capacity )
625{
626 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
627 if( PSA_ALG_IS_HKDF( alg ) )
628 {
629 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
630 PSA_KEY_DERIVATION_INPUT_SALT,
631 input1, input1_length ) );
632 PSA_ASSERT( psa_key_derivation_input_key( operation,
633 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200634 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100635 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
636 PSA_KEY_DERIVATION_INPUT_INFO,
637 input2,
638 input2_length ) );
639 }
640 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
641 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
642 {
643 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
644 PSA_KEY_DERIVATION_INPUT_SEED,
645 input1, input1_length ) );
646 PSA_ASSERT( psa_key_derivation_input_key( operation,
647 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200648 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100649 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
650 PSA_KEY_DERIVATION_INPUT_LABEL,
651 input2, input2_length ) );
652 }
653 else
654 {
655 TEST_ASSERT( ! "Key derivation algorithm not supported" );
656 }
657
Gilles Peskinec744d992019-07-30 17:26:54 +0200658 if( capacity != SIZE_MAX )
659 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100660
661 return( 1 );
662
663exit:
664 return( 0 );
665}
666
667
Ronald Cron5425a212020-08-04 14:58:35 +0200668static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200669 psa_key_usage_t usage,
670 psa_algorithm_t alg )
671{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200672 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100673 unsigned char input1[] = "Input 1";
674 size_t input1_length = sizeof( input1 );
675 unsigned char input2[] = "Input 2";
676 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200677 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100678 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200679
680 if( usage & PSA_KEY_USAGE_DERIVE )
681 {
Ronald Cron5425a212020-08-04 14:58:35 +0200682 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100683 input1, input1_length,
684 input2, input2_length, capacity ) )
685 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200686
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200687 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200688 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100689 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200690 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200691 }
692
693 return( 1 );
694
695exit:
696 return( 0 );
697}
698
Gilles Peskinec7998b72018-11-07 18:45:02 +0100699/* We need two keys to exercise key agreement. Exercise the
700 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200701static psa_status_t key_agreement_with_self(
702 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200703 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100704{
705 psa_key_type_t private_key_type;
706 psa_key_type_t public_key_type;
707 size_t key_bits;
708 uint8_t *public_key = NULL;
709 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200710 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200711 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
712 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200713 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200714 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100715
Ronald Cron5425a212020-08-04 14:58:35 +0200716 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200717 private_key_type = psa_get_key_type( &attributes );
718 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200719 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100720 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100721 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200722 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100723 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100724
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200725 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200726 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200727 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100728exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100729 /*
730 * Key attributes may have been returned by psa_get_key_attributes()
731 * thus reset them as required.
732 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200733 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100734
735 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100736 return( status );
737}
738
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200739/* We need two keys to exercise key agreement. Exercise the
740 * private key against its own public key. */
741static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200742 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200743{
744 psa_key_type_t private_key_type;
745 psa_key_type_t public_key_type;
746 size_t key_bits;
747 uint8_t *public_key = NULL;
748 size_t public_key_length;
749 uint8_t output[1024];
750 size_t output_length;
751 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200752 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
753 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200754 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200755 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200756
Ronald Cron5425a212020-08-04 14:58:35 +0200757 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200758 private_key_type = psa_get_key_type( &attributes );
759 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200760 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100761 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200762 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200763 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200764 public_key, public_key_length,
765 &public_key_length ) );
766
Ronald Cron5425a212020-08-04 14:58:35 +0200767 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200768 public_key, public_key_length,
769 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200770exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100771 /*
772 * Key attributes may have been returned by psa_get_key_attributes()
773 * thus reset them as required.
774 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200775 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100776
777 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200778 return( status );
779}
780
Ronald Cron5425a212020-08-04 14:58:35 +0200781static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200782 psa_key_usage_t usage,
783 psa_algorithm_t alg )
784{
785 int ok = 0;
786
787 if( usage & PSA_KEY_USAGE_DERIVE )
788 {
789 /* We need two keys to exercise key agreement. Exercise the
790 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200791 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200792 }
793 ok = 1;
794
795exit:
796 return( ok );
797}
798
Ronald Cron5425a212020-08-04 14:58:35 +0200799static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200800 psa_key_usage_t usage,
801 psa_algorithm_t alg )
802{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200803 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200804 unsigned char output[1];
805 int ok = 0;
806
807 if( usage & PSA_KEY_USAGE_DERIVE )
808 {
809 /* We need two keys to exercise key agreement. Exercise the
810 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200811 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200812 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200813 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200814 output,
815 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200816 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200817 }
818 ok = 1;
819
820exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200821 return( ok );
822}
823
Jaeden Amerof7dca862019-06-27 17:31:33 +0100824int asn1_skip_integer( unsigned char **p, const unsigned char *end,
825 size_t min_bits, size_t max_bits,
826 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200827{
828 size_t len;
829 size_t actual_bits;
830 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100831 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100832 MBEDTLS_ASN1_INTEGER ),
833 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200834
835 /* Check if the retrieved length doesn't extend the actual buffer's size.
836 * It is assumed here, that end >= p, which validates casting to size_t. */
837 TEST_ASSERT( len <= (size_t)( end - *p) );
838
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200839 /* Tolerate a slight departure from DER encoding:
840 * - 0 may be represented by an empty string or a 1-byte string.
841 * - The sign bit may be used as a value bit. */
842 if( ( len == 1 && ( *p )[0] == 0 ) ||
843 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
844 {
845 ++( *p );
846 --len;
847 }
848 if( min_bits == 0 && len == 0 )
849 return( 1 );
850 msb = ( *p )[0];
851 TEST_ASSERT( msb != 0 );
852 actual_bits = 8 * ( len - 1 );
853 while( msb != 0 )
854 {
855 msb >>= 1;
856 ++actual_bits;
857 }
858 TEST_ASSERT( actual_bits >= min_bits );
859 TEST_ASSERT( actual_bits <= max_bits );
860 if( must_be_odd )
861 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
862 *p += len;
863 return( 1 );
864exit:
865 return( 0 );
866}
867
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200868static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
869 uint8_t *exported, size_t exported_length )
870{
871 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100872 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200873 else
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100874 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200875
876#if defined(MBEDTLS_DES_C)
877 if( type == PSA_KEY_TYPE_DES )
878 {
879 /* Check the parity bits. */
880 unsigned i;
881 for( i = 0; i < bits / 8; i++ )
882 {
883 unsigned bit_count = 0;
884 unsigned m;
885 for( m = 1; m <= 0x100; m <<= 1 )
886 {
887 if( exported[i] & m )
888 ++bit_count;
889 }
890 TEST_ASSERT( bit_count % 2 != 0 );
891 }
892 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200893 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200894#endif
895
896#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200897 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200898 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200899 uint8_t *p = exported;
900 uint8_t *end = exported + exported_length;
901 size_t len;
902 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200903 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200904 * modulus INTEGER, -- n
905 * publicExponent INTEGER, -- e
906 * privateExponent INTEGER, -- d
907 * prime1 INTEGER, -- p
908 * prime2 INTEGER, -- q
909 * exponent1 INTEGER, -- d mod (p-1)
910 * exponent2 INTEGER, -- d mod (q-1)
911 * coefficient INTEGER, -- (inverse of q) mod p
912 * }
913 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100914 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
915 MBEDTLS_ASN1_SEQUENCE |
916 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
917 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200918 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
919 goto exit;
920 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
921 goto exit;
922 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
923 goto exit;
924 /* Require d to be at least half the size of n. */
925 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
926 goto exit;
927 /* Require p and q to be at most half the size of n, rounded up. */
928 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
929 goto exit;
930 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
931 goto exit;
932 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
933 goto exit;
934 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
935 goto exit;
936 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
937 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100938 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100939 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200940 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200941#endif /* MBEDTLS_RSA_C */
942
943#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200944 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200945 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100946 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100947 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100948 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200949 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200950#endif /* MBEDTLS_ECP_C */
951
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200952 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
953 {
954 uint8_t *p = exported;
955 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200956#if defined(MBEDTLS_RSA_C)
957 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
958 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100959 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200960 /* RSAPublicKey ::= SEQUENCE {
961 * modulus INTEGER, -- n
962 * publicExponent INTEGER } -- e
963 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100964 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
965 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100966 MBEDTLS_ASN1_CONSTRUCTED ),
967 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100968 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200969 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
970 goto exit;
971 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
972 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100973 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200974 }
975 else
976#endif /* MBEDTLS_RSA_C */
977#if defined(MBEDTLS_ECP_C)
978 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
979 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200980 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
981 {
982 /* The representation of an ECC Montgomery public key is
983 * the raw compressed point */
984 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
985 }
986 else
987 {
988 /* The representation of an ECC Weierstrass public key is:
989 * - The byte 0x04;
990 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
991 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
992 * - where m is the bit size associated with the curve.
993 */
994 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
995 TEST_EQUAL( p[0], 4 );
996 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200997 }
998 else
999#endif /* MBEDTLS_ECP_C */
1000 {
Jaeden Amero594a3302018-10-26 17:07:22 +01001001 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001002 mbedtls_snprintf( message, sizeof( message ),
1003 "No sanity check for public key type=0x%08lx",
1004 (unsigned long) type );
Chris Jones9634bb12021-01-20 15:56:42 +00001005 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +02001006 (void) p;
1007 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001008 return( 0 );
1009 }
1010 }
1011 else
1012
1013 {
1014 /* No sanity checks for other types */
1015 }
1016
1017 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001018
1019exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001020 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001021}
1022
Ronald Cron5425a212020-08-04 14:58:35 +02001023static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001024 psa_key_usage_t usage )
1025{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001027 uint8_t *exported = NULL;
1028 size_t exported_size = 0;
1029 size_t exported_length = 0;
1030 int ok = 0;
1031
Ronald Cron5425a212020-08-04 14:58:35 +02001032 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001033
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001034 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1035 psa_get_key_type( &attributes ),
1036 psa_get_key_bits( &attributes ) );
1037 ASSERT_ALLOC( exported, exported_size );
1038
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001039 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001040 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001041 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001042 TEST_EQUAL( psa_export_key( key, exported,
1043 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001044 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001045 ok = 1;
1046 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001047 }
1048
Ronald Cron5425a212020-08-04 14:58:35 +02001049 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001050 exported, exported_size,
1051 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001052 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1053 psa_get_key_bits( &attributes ),
1054 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001055
1056exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001057 /*
1058 * Key attributes may have been returned by psa_get_key_attributes()
1059 * thus reset them as required.
1060 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001061 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001062
1063 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001064 return( ok );
1065}
1066
Ronald Cron5425a212020-08-04 14:58:35 +02001067static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001068{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001069 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001070 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001071 uint8_t *exported = NULL;
1072 size_t exported_size = 0;
1073 size_t exported_length = 0;
1074 int ok = 0;
1075
Ronald Cron5425a212020-08-04 14:58:35 +02001076 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001077 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001078 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001079 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1080 psa_get_key_type( &attributes ),
1081 psa_get_key_bits( &attributes ) );
1082 ASSERT_ALLOC( exported, exported_size );
1083
1084 TEST_EQUAL( psa_export_public_key( key, exported,
1085 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001086 PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001087 ok = 1;
1088 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001089 }
1090
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001091 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001092 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001093 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1094 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001095 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001096
Ronald Cron5425a212020-08-04 14:58:35 +02001097 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001098 exported, exported_size,
1099 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001100 ok = exported_key_sanity_check( public_type,
1101 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001102 exported, exported_length );
1103
1104exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001105 /*
1106 * Key attributes may have been returned by psa_get_key_attributes()
1107 * thus reset them as required.
1108 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001109 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001110
1111 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001112 return( ok );
1113}
1114
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001115/** Do smoke tests on a key.
1116 *
1117 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1118 * sign/verify, or derivation) that is permitted according to \p usage.
1119 * \p usage and \p alg should correspond to the expected policy on the
1120 * key.
1121 *
1122 * Export the key if permitted by \p usage, and check that the output
1123 * looks sensible. If \p usage forbids export, check that
1124 * \p psa_export_key correctly rejects the attempt. If the key is
1125 * asymmetric, also check \p psa_export_public_key.
1126 *
1127 * If the key fails the tests, this function calls the test framework's
Chris Jones9634bb12021-01-20 15:56:42 +00001128 * `mbedtls_test_fail` function and returns false. Otherwise this function
1129 * returns true. Therefore it should be used as follows:
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001130 * ```
1131 * if( ! exercise_key( ... ) ) goto exit;
1132 * ```
1133 *
Ronald Cron5425a212020-08-04 14:58:35 +02001134 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001135 * \p alg.
1136 * \param usage The usage flags to assume.
1137 * \param alg The algorithm to exercise.
1138 *
1139 * \retval 0 The key failed the smoke tests.
1140 * \retval 1 The key passed the smoke tests.
1141 */
Ronald Cron5425a212020-08-04 14:58:35 +02001142static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001143 psa_key_usage_t usage,
1144 psa_algorithm_t alg )
1145{
1146 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001147
Ronald Cron5425a212020-08-04 14:58:35 +02001148 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001149 return( 0 );
1150
Gilles Peskine02b75072018-07-01 22:31:34 +02001151 if( alg == 0 )
1152 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1153 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001154 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001155 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001156 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001157 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001158 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001159 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001160 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001161 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001162 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001163 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001164 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001165 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001166 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001167 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001168 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001169 else
1170 {
1171 char message[40];
1172 mbedtls_snprintf( message, sizeof( message ),
1173 "No code to exercise alg=0x%08lx",
1174 (unsigned long) alg );
Chris Jones9634bb12021-01-20 15:56:42 +00001175 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskine02b75072018-07-01 22:31:34 +02001176 ok = 0;
1177 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001178
Ronald Cron5425a212020-08-04 14:58:35 +02001179 ok = ok && exercise_export_key( key, usage );
1180 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001181
Gilles Peskine02b75072018-07-01 22:31:34 +02001182 return( ok );
1183}
1184
Gilles Peskine10df3412018-10-25 22:35:43 +02001185static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1186 psa_algorithm_t alg )
1187{
1188 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1189 {
1190 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001191 PSA_KEY_USAGE_VERIFY_HASH :
1192 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001193 }
1194 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1195 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1196 {
1197 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1198 PSA_KEY_USAGE_ENCRYPT :
1199 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1200 }
1201 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1202 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1203 {
1204 return( PSA_KEY_USAGE_DERIVE );
1205 }
1206 else
1207 {
1208 return( 0 );
1209 }
1210
1211}
Darryl Green0c6575a2018-11-07 16:05:30 +00001212
Ronald Cron5425a212020-08-04 14:58:35 +02001213static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001214{
1215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001216 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001217 uint8_t buffer[1];
1218 size_t length;
1219 int ok = 0;
1220
Ronald Cronecfb2372020-07-23 17:13:42 +02001221 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001222 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1223 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1224 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001225 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001226 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001227 TEST_EQUAL(
1228 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1229 TEST_EQUAL(
1230 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001231 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001232 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1233 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1234 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1235 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1236
Ronald Cron5425a212020-08-04 14:58:35 +02001237 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001238 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001239 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001240 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001241 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001242
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001243 ok = 1;
1244
1245exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001246 /*
1247 * Key attributes may have been returned by psa_get_key_attributes()
1248 * thus reset them as required.
1249 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001250 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001251
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001252 return( ok );
1253}
1254
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001255/* Assert that a key isn't reported as having a slot number. */
1256#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1257#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1258 do \
1259 { \
1260 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1261 TEST_EQUAL( psa_get_key_slot_number( \
1262 attributes, \
1263 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1264 PSA_ERROR_INVALID_ARGUMENT ); \
1265 } \
1266 while( 0 )
1267#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1268#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1269 ( (void) 0 )
1270#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1271
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001272/* An overapproximation of the amount of storage needed for a key of the
1273 * given type and with the given content. The API doesn't make it easy
1274 * to find a good value for the size. The current implementation doesn't
1275 * care about the value anyway. */
1276#define KEY_BITS_FROM_DATA( type, data ) \
1277 ( data )->len
1278
Darryl Green0c6575a2018-11-07 16:05:30 +00001279typedef enum {
1280 IMPORT_KEY = 0,
1281 GENERATE_KEY = 1,
1282 DERIVE_KEY = 2
1283} generate_method;
1284
Gilles Peskinee59236f2018-01-27 23:32:46 +01001285/* END_HEADER */
1286
1287/* BEGIN_DEPENDENCIES
1288 * depends_on:MBEDTLS_PSA_CRYPTO_C
1289 * END_DEPENDENCIES
1290 */
1291
1292/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001293void static_checks( )
1294{
1295 size_t max_truncated_mac_size =
1296 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1297
1298 /* Check that the length for a truncated MAC always fits in the algorithm
1299 * encoding. The shifted mask is the maximum truncated value. The
1300 * untruncated algorithm may be one byte larger. */
1301 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001302
1303#if defined(MBEDTLS_TEST_DEPRECATED)
1304 /* Check deprecated constants. */
1305 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1306 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1307 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1308 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1309 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1310 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1311 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1312 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001313
Paul Elliott8ff510a2020-06-02 17:19:28 +01001314 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1326 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1328 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1329 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1330 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1331 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1332 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1333 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1334 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1335 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1336 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1337 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1338 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1339 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1340 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1341 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1342 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1343 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1344
1345 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1346 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1347 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1348 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1349 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1350 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1351 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1352 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001353
Paul Elliott75e27032020-06-03 15:17:39 +01001354 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1355 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1356 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1357 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1358 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1359
1360 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1361 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001362#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001363}
1364/* END_CASE */
1365
1366/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001367void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001368 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001369 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370{
Gilles Peskine4747d192019-04-17 15:05:45 +02001371 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001372 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001373 psa_key_lifetime_t lifetime = lifetime_arg;
1374 psa_key_usage_t usage_flags = usage_flags_arg;
1375 psa_algorithm_t alg = alg_arg;
1376 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001377 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001378
Ronald Cronecfb2372020-07-23 17:13:42 +02001379 TEST_EQUAL(
1380 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1381 TEST_EQUAL(
1382 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001383 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1384 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1385 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1386 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001387 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001388
Gilles Peskinec87af662019-05-15 16:12:22 +02001389 psa_set_key_id( &attributes, id );
1390 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001391 psa_set_key_usage_flags( &attributes, usage_flags );
1392 psa_set_key_algorithm( &attributes, alg );
1393 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001394 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001395
Ronald Cronecfb2372020-07-23 17:13:42 +02001396 TEST_ASSERT( mbedtls_svc_key_id_equal(
1397 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001398 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1399 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1400 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1401 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001402 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001403
1404 psa_reset_key_attributes( &attributes );
1405
Ronald Cronecfb2372020-07-23 17:13:42 +02001406 TEST_EQUAL(
1407 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1408 TEST_EQUAL(
1409 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001410 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1411 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1412 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1413 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001414 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001415}
1416/* END_CASE */
1417
1418/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001419void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1420 int id2_arg, int owner_id2_arg,
1421 int expected_id_arg, int expected_owner_id_arg,
1422 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001423{
1424 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001425 mbedtls_svc_key_id_t id1 =
1426 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001427 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001428 mbedtls_svc_key_id_t id2 =
1429 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001430 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001431 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001432 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1433
1434 if( id1_arg != -1 )
1435 psa_set_key_id( &attributes, id1 );
1436 if( lifetime_arg != -1 )
1437 psa_set_key_lifetime( &attributes, lifetime );
1438 if( id2_arg != -1 )
1439 psa_set_key_id( &attributes, id2 );
1440
Ronald Cronecfb2372020-07-23 17:13:42 +02001441 TEST_ASSERT( mbedtls_svc_key_id_equal(
1442 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001443 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1444}
1445/* END_CASE */
1446
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001447/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1448void slot_number_attribute( )
1449{
1450 psa_key_slot_number_t slot_number = 0xdeadbeef;
1451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1452
1453 /* Initially, there is no slot number. */
1454 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1455 PSA_ERROR_INVALID_ARGUMENT );
1456
1457 /* Test setting a slot number. */
1458 psa_set_key_slot_number( &attributes, 0 );
1459 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1460 TEST_EQUAL( slot_number, 0 );
1461
1462 /* Test changing the slot number. */
1463 psa_set_key_slot_number( &attributes, 42 );
1464 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1465 TEST_EQUAL( slot_number, 42 );
1466
1467 /* Test clearing the slot number. */
1468 psa_clear_key_slot_number( &attributes );
1469 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1470 PSA_ERROR_INVALID_ARGUMENT );
1471
1472 /* Clearing again should have no effect. */
1473 psa_clear_key_slot_number( &attributes );
1474 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1475 PSA_ERROR_INVALID_ARGUMENT );
1476
1477 /* Test that reset clears the slot number. */
1478 psa_set_key_slot_number( &attributes, 42 );
1479 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1480 TEST_EQUAL( slot_number, 42 );
1481 psa_reset_key_attributes( &attributes );
1482 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1483 PSA_ERROR_INVALID_ARGUMENT );
1484}
1485/* END_CASE */
1486
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001487/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001488void import_with_policy( int type_arg,
1489 int usage_arg, int alg_arg,
1490 int expected_status_arg )
1491{
1492 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1493 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001494 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001495 psa_key_type_t type = type_arg;
1496 psa_key_usage_t usage = usage_arg;
1497 psa_algorithm_t alg = alg_arg;
1498 psa_status_t expected_status = expected_status_arg;
1499 const uint8_t key_material[16] = {0};
1500 psa_status_t status;
1501
1502 PSA_ASSERT( psa_crypto_init( ) );
1503
1504 psa_set_key_type( &attributes, type );
1505 psa_set_key_usage_flags( &attributes, usage );
1506 psa_set_key_algorithm( &attributes, alg );
1507
1508 status = psa_import_key( &attributes,
1509 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001510 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001511 TEST_EQUAL( status, expected_status );
1512 if( status != PSA_SUCCESS )
1513 goto exit;
1514
Ronald Cron5425a212020-08-04 14:58:35 +02001515 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001516 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1517 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1518 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001519 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001520
Ronald Cron5425a212020-08-04 14:58:35 +02001521 PSA_ASSERT( psa_destroy_key( key ) );
1522 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001523
1524exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001525 /*
1526 * Key attributes may have been returned by psa_get_key_attributes()
1527 * thus reset them as required.
1528 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001529 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001530
1531 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001532 PSA_DONE( );
1533}
1534/* END_CASE */
1535
1536/* BEGIN_CASE */
1537void import_with_data( data_t *data, int type_arg,
1538 int attr_bits_arg,
1539 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001540{
1541 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1542 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001543 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001544 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001545 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001546 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001547 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001548
Gilles Peskine8817f612018-12-18 00:18:46 +01001549 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001550
Gilles Peskine4747d192019-04-17 15:05:45 +02001551 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001552 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001553
Ronald Cron5425a212020-08-04 14:58:35 +02001554 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001555 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001556 if( status != PSA_SUCCESS )
1557 goto exit;
1558
Ronald Cron5425a212020-08-04 14:58:35 +02001559 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001560 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001561 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001562 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001563 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001564
Ronald Cron5425a212020-08-04 14:58:35 +02001565 PSA_ASSERT( psa_destroy_key( key ) );
1566 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567
1568exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001569 /*
1570 * Key attributes may have been returned by psa_get_key_attributes()
1571 * thus reset them as required.
1572 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001573 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001574
1575 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001576 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001577}
1578/* END_CASE */
1579
1580/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001581void import_large_key( int type_arg, int byte_size_arg,
1582 int expected_status_arg )
1583{
1584 psa_key_type_t type = type_arg;
1585 size_t byte_size = byte_size_arg;
1586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1587 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001588 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001589 psa_status_t status;
1590 uint8_t *buffer = NULL;
1591 size_t buffer_size = byte_size + 1;
1592 size_t n;
1593
Steven Cooreman69967ce2021-01-18 18:01:08 +01001594 /* Skip the test case if the target running the test cannot
1595 * accomodate large keys due to heap size constraints */
1596 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001597 memset( buffer, 'K', byte_size );
1598
1599 PSA_ASSERT( psa_crypto_init( ) );
1600
1601 /* Try importing the key */
1602 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1603 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001604 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001605 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001606 TEST_EQUAL( status, expected_status );
1607
1608 if( status == PSA_SUCCESS )
1609 {
Ronald Cron5425a212020-08-04 14:58:35 +02001610 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001611 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1612 TEST_EQUAL( psa_get_key_bits( &attributes ),
1613 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001614 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001615 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001616 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001617 for( n = 0; n < byte_size; n++ )
1618 TEST_EQUAL( buffer[n], 'K' );
1619 for( n = byte_size; n < buffer_size; n++ )
1620 TEST_EQUAL( buffer[n], 0 );
1621 }
1622
1623exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001624 /*
1625 * Key attributes may have been returned by psa_get_key_attributes()
1626 * thus reset them as required.
1627 */
1628 psa_reset_key_attributes( &attributes );
1629
Ronald Cron5425a212020-08-04 14:58:35 +02001630 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001631 PSA_DONE( );
1632 mbedtls_free( buffer );
1633}
1634/* END_CASE */
1635
1636/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001637void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1638{
Ronald Cron5425a212020-08-04 14:58:35 +02001639 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001640 size_t bits = bits_arg;
1641 psa_status_t expected_status = expected_status_arg;
1642 psa_status_t status;
1643 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001644 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001645 size_t buffer_size = /* Slight overapproximations */
1646 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001647 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001648 unsigned char *p;
1649 int ret;
1650 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001652
Gilles Peskine8817f612018-12-18 00:18:46 +01001653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001654 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001655
1656 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1657 bits, keypair ) ) >= 0 );
1658 length = ret;
1659
1660 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001661 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001662 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001663 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001664
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001665 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001666 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001667
1668exit:
1669 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001670 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001671}
1672/* END_CASE */
1673
1674/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001675void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001676 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001677 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001678 int expected_bits,
1679 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001680 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001681 int canonical_input )
1682{
Ronald Cron5425a212020-08-04 14:58:35 +02001683 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001684 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001685 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001686 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001687 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001688 unsigned char *exported = NULL;
1689 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001690 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001691 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001692 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001694 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001695
Moran Pekercb088e72018-07-17 17:36:59 +03001696 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001697 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001698 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001699 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001700 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001701
Gilles Peskine4747d192019-04-17 15:05:45 +02001702 psa_set_key_usage_flags( &attributes, usage_arg );
1703 psa_set_key_algorithm( &attributes, alg );
1704 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001705
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001706 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001707 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001708
1709 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001710 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001711 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1712 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001713 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001714
1715 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001716 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001717 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001718
1719 /* The exported length must be set by psa_export_key() to a value between 0
1720 * and export_size. On errors, the exported length must be 0. */
1721 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1722 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1723 TEST_ASSERT( exported_length <= export_size );
1724
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001725 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001726 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001727 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001728 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001729 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001730 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001731 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001732
Ronald Cron5425a212020-08-04 14:58:35 +02001733 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001734 goto exit;
1735
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001736 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001737 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001738 else
1739 {
Ronald Cron5425a212020-08-04 14:58:35 +02001740 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001741 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001742 &key2 ) );
1743 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001744 reexported,
1745 export_size,
1746 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001747 ASSERT_COMPARE( exported, exported_length,
1748 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001749 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001750 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001751 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001752
1753destroy:
1754 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001755 PSA_ASSERT( psa_destroy_key( key ) );
1756 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001757
1758exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001759 /*
1760 * Key attributes may have been returned by psa_get_key_attributes()
1761 * thus reset them as required.
1762 */
1763 psa_reset_key_attributes( &got_attributes );
1764
itayzafrir3e02b3b2018-06-12 17:06:52 +03001765 mbedtls_free( exported );
1766 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001767 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001768}
1769/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001770
Moran Pekerf709f4a2018-06-06 17:26:04 +03001771/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001772void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001773 int type_arg,
1774 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001775 int export_size_delta,
1776 int expected_export_status_arg,
1777 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001778{
Ronald Cron5425a212020-08-04 14:58:35 +02001779 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001780 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001781 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001782 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001783 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001784 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001785 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001786 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001788
Gilles Peskine8817f612018-12-18 00:18:46 +01001789 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001790
Gilles Peskine4747d192019-04-17 15:05:45 +02001791 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1792 psa_set_key_algorithm( &attributes, alg );
1793 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001794
1795 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001796 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001797
Gilles Peskine49c25912018-10-29 15:15:31 +01001798 /* Export the public key */
1799 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001800 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001801 exported, export_size,
1802 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001803 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001804 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001805 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001806 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001807 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001808 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001809 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001810 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001811 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001812 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1813 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001814 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001815
1816exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001817 /*
1818 * Key attributes may have been returned by psa_get_key_attributes()
1819 * thus reset them as required.
1820 */
1821 psa_reset_key_attributes( &attributes );
1822
itayzafrir3e02b3b2018-06-12 17:06:52 +03001823 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001824 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001825 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001826}
1827/* END_CASE */
1828
Gilles Peskine20035e32018-02-03 22:44:14 +01001829/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001830void import_and_exercise_key( data_t *data,
1831 int type_arg,
1832 int bits_arg,
1833 int alg_arg )
1834{
Ronald Cron5425a212020-08-04 14:58:35 +02001835 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001836 psa_key_type_t type = type_arg;
1837 size_t bits = bits_arg;
1838 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001839 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001841 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001842
Gilles Peskine8817f612018-12-18 00:18:46 +01001843 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001844
Gilles Peskine4747d192019-04-17 15:05:45 +02001845 psa_set_key_usage_flags( &attributes, usage );
1846 psa_set_key_algorithm( &attributes, alg );
1847 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001848
1849 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001850 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001851
1852 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001853 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001854 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1855 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001856
1857 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001858 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001859 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001860
Ronald Cron5425a212020-08-04 14:58:35 +02001861 PSA_ASSERT( psa_destroy_key( key ) );
1862 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001863
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001864exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001865 /*
1866 * Key attributes may have been returned by psa_get_key_attributes()
1867 * thus reset them as required.
1868 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001869 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001870
1871 psa_reset_key_attributes( &attributes );
1872 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001873 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001874}
1875/* END_CASE */
1876
1877/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001878void effective_key_attributes( int type_arg, int expected_type_arg,
1879 int bits_arg, int expected_bits_arg,
1880 int usage_arg, int expected_usage_arg,
1881 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001882{
Ronald Cron5425a212020-08-04 14:58:35 +02001883 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001884 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001885 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001886 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001887 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001888 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001889 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001890 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001891 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001893
Gilles Peskine8817f612018-12-18 00:18:46 +01001894 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001895
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001896 psa_set_key_usage_flags( &attributes, usage );
1897 psa_set_key_algorithm( &attributes, alg );
1898 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001899 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001900
Ronald Cron5425a212020-08-04 14:58:35 +02001901 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001902 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001903
Ronald Cron5425a212020-08-04 14:58:35 +02001904 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001905 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1906 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1907 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1908 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001909
1910exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001911 /*
1912 * Key attributes may have been returned by psa_get_key_attributes()
1913 * thus reset them as required.
1914 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001915 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001916
1917 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001918 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001919}
1920/* END_CASE */
1921
1922/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001923void check_key_policy( int type_arg, int bits_arg,
1924 int usage_arg, int alg_arg )
1925{
1926 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1927 usage_arg, usage_arg, alg_arg, alg_arg );
1928 goto exit;
1929}
1930/* END_CASE */
1931
1932/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001933void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001934{
1935 /* Test each valid way of initializing the object, except for `= {0}`, as
1936 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1937 * though it's OK by the C standard. We could test for this, but we'd need
1938 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001939 psa_key_attributes_t func = psa_key_attributes_init( );
1940 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1941 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001942
1943 memset( &zero, 0, sizeof( zero ) );
1944
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001945 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1946 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1947 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001948
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001949 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1950 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1951 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1952
1953 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1954 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1955 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1956
1957 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1958 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1959 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1960
1961 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1962 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1963 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001964}
1965/* END_CASE */
1966
1967/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001968void mac_key_policy( int policy_usage,
1969 int policy_alg,
1970 int key_type,
1971 data_t *key_data,
1972 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001973{
Ronald Cron5425a212020-08-04 14:58:35 +02001974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001975 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001976 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977 psa_status_t status;
1978 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001979
Gilles Peskine8817f612018-12-18 00:18:46 +01001980 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001981
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001982 psa_set_key_usage_flags( &attributes, policy_usage );
1983 psa_set_key_algorithm( &attributes, policy_alg );
1984 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001985
Gilles Peskine049c7532019-05-15 20:22:09 +02001986 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001987 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001988
Ronald Cron5425a212020-08-04 14:58:35 +02001989 status = psa_mac_sign_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_SIGN_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 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001996
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001997 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001998 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001999 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002000 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002001 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002003 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004
2005exit:
2006 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002007 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002008 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002009}
2010/* END_CASE */
2011
2012/* BEGIN_CASE */
2013void cipher_key_policy( int policy_usage,
2014 int policy_alg,
2015 int key_type,
2016 data_t *key_data,
2017 int exercise_alg )
2018{
Ronald Cron5425a212020-08-04 14:58:35 +02002019 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002021 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022 psa_status_t status;
2023
Gilles Peskine8817f612018-12-18 00:18:46 +01002024 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002026 psa_set_key_usage_flags( &attributes, policy_usage );
2027 psa_set_key_algorithm( &attributes, policy_alg );
2028 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002029
Gilles Peskine049c7532019-05-15 20:22:09 +02002030 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002031 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032
Ronald Cron5425a212020-08-04 14:58:35 +02002033 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034 if( policy_alg == exercise_alg &&
2035 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002036 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002038 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039 psa_cipher_abort( &operation );
2040
Ronald Cron5425a212020-08-04 14:58:35 +02002041 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002042 if( policy_alg == exercise_alg &&
2043 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002044 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002045 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002046 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002047
2048exit:
2049 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002050 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002051 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002052}
2053/* END_CASE */
2054
2055/* BEGIN_CASE */
2056void aead_key_policy( int policy_usage,
2057 int policy_alg,
2058 int key_type,
2059 data_t *key_data,
2060 int nonce_length_arg,
2061 int tag_length_arg,
2062 int exercise_alg )
2063{
Ronald Cron5425a212020-08-04 14:58:35 +02002064 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002065 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066 psa_status_t status;
2067 unsigned char nonce[16] = {0};
2068 size_t nonce_length = nonce_length_arg;
2069 unsigned char tag[16];
2070 size_t tag_length = tag_length_arg;
2071 size_t output_length;
2072
2073 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
2074 TEST_ASSERT( tag_length <= sizeof( tag ) );
2075
Gilles Peskine8817f612018-12-18 00:18:46 +01002076 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002078 psa_set_key_usage_flags( &attributes, policy_usage );
2079 psa_set_key_algorithm( &attributes, policy_alg );
2080 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081
Gilles Peskine049c7532019-05-15 20:22:09 +02002082 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002083 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084
Ronald Cron5425a212020-08-04 14:58:35 +02002085 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002086 nonce, nonce_length,
2087 NULL, 0,
2088 NULL, 0,
2089 tag, tag_length,
2090 &output_length );
2091 if( policy_alg == exercise_alg &&
2092 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002093 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002095 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096
2097 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002098 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099 nonce, nonce_length,
2100 NULL, 0,
2101 tag, tag_length,
2102 NULL, 0,
2103 &output_length );
2104 if( policy_alg == exercise_alg &&
2105 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002106 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002107 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002108 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002109
2110exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002111 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002112 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113}
2114/* END_CASE */
2115
2116/* BEGIN_CASE */
2117void asymmetric_encryption_key_policy( int policy_usage,
2118 int policy_alg,
2119 int key_type,
2120 data_t *key_data,
2121 int exercise_alg )
2122{
Ronald Cron5425a212020-08-04 14:58:35 +02002123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125 psa_status_t status;
2126 size_t key_bits;
2127 size_t buffer_length;
2128 unsigned char *buffer = NULL;
2129 size_t output_length;
2130
Gilles Peskine8817f612018-12-18 00:18:46 +01002131 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002133 psa_set_key_usage_flags( &attributes, policy_usage );
2134 psa_set_key_algorithm( &attributes, policy_alg );
2135 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002136
Gilles Peskine049c7532019-05-15 20:22:09 +02002137 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002138 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002139
Ronald Cron5425a212020-08-04 14:58:35 +02002140 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002141 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002142 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2143 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002144 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145
Ronald Cron5425a212020-08-04 14:58:35 +02002146 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002147 NULL, 0,
2148 NULL, 0,
2149 buffer, buffer_length,
2150 &output_length );
2151 if( policy_alg == exercise_alg &&
2152 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002153 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002154 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002155 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002156
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002157 if( buffer_length != 0 )
2158 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002159 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002160 buffer, buffer_length,
2161 NULL, 0,
2162 buffer, buffer_length,
2163 &output_length );
2164 if( policy_alg == exercise_alg &&
2165 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002166 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002167 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002168 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002169
2170exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002171 /*
2172 * Key attributes may have been returned by psa_get_key_attributes()
2173 * thus reset them as required.
2174 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002175 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002176
2177 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002178 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179 mbedtls_free( buffer );
2180}
2181/* END_CASE */
2182
2183/* BEGIN_CASE */
2184void asymmetric_signature_key_policy( int policy_usage,
2185 int policy_alg,
2186 int key_type,
2187 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002188 int exercise_alg,
2189 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002190{
Ronald Cron5425a212020-08-04 14:58:35 +02002191 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002193 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002194 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2195 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2196 * compatible with the policy and `payload_length_arg` is supposed to be
2197 * a valid input length to sign. If `payload_length_arg <= 0`,
2198 * `exercise_alg` is supposed to be forbidden by the policy. */
2199 int compatible_alg = payload_length_arg > 0;
2200 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002201 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002202 size_t signature_length;
2203
Gilles Peskine8817f612018-12-18 00:18:46 +01002204 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002205
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002206 psa_set_key_usage_flags( &attributes, policy_usage );
2207 psa_set_key_algorithm( &attributes, policy_alg );
2208 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002209
Gilles Peskine049c7532019-05-15 20:22:09 +02002210 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002211 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002212
Ronald Cron5425a212020-08-04 14:58:35 +02002213 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002214 payload, payload_length,
2215 signature, sizeof( signature ),
2216 &signature_length );
2217 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002218 PSA_ASSERT( status );
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 Peskine76f5c7b2018-07-06 16:53:09 +02002221
2222 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002223 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002224 payload, payload_length,
2225 signature, sizeof( signature ) );
2226 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002227 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002228 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002229 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002230
2231exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002232 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002233 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002234}
2235/* END_CASE */
2236
Janos Follathba3fab92019-06-11 14:50:16 +01002237/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002238void derive_key_policy( int policy_usage,
2239 int policy_alg,
2240 int key_type,
2241 data_t *key_data,
2242 int exercise_alg )
2243{
Ronald Cron5425a212020-08-04 14:58:35 +02002244 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002246 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002247 psa_status_t status;
2248
Gilles Peskine8817f612018-12-18 00:18:46 +01002249 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002250
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002251 psa_set_key_usage_flags( &attributes, policy_usage );
2252 psa_set_key_algorithm( &attributes, policy_alg );
2253 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002254
Gilles Peskine049c7532019-05-15 20:22:09 +02002255 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002256 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002257
Janos Follathba3fab92019-06-11 14:50:16 +01002258 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2259
2260 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2261 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002262 {
Janos Follathba3fab92019-06-11 14:50:16 +01002263 PSA_ASSERT( psa_key_derivation_input_bytes(
2264 &operation,
2265 PSA_KEY_DERIVATION_INPUT_SEED,
2266 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002267 }
Janos Follathba3fab92019-06-11 14:50:16 +01002268
2269 status = psa_key_derivation_input_key( &operation,
2270 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002271 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002272
Gilles Peskineea0fb492018-07-12 17:17:20 +02002273 if( policy_alg == exercise_alg &&
2274 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002275 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002276 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002277 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002278
2279exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002280 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002281 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002282 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002283}
2284/* END_CASE */
2285
2286/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002287void agreement_key_policy( int policy_usage,
2288 int policy_alg,
2289 int key_type_arg,
2290 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002291 int exercise_alg,
2292 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002293{
Ronald Cron5425a212020-08-04 14:58:35 +02002294 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002295 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002296 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002297 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002298 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002299 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002300
Gilles Peskine8817f612018-12-18 00:18:46 +01002301 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002302
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002303 psa_set_key_usage_flags( &attributes, policy_usage );
2304 psa_set_key_algorithm( &attributes, policy_alg );
2305 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002306
Gilles Peskine049c7532019-05-15 20:22:09 +02002307 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002308 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002309
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002310 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002311 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002312
Steven Cooremance48e852020-10-05 16:02:45 +02002313 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002314
2315exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002316 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002317 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002318 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002319}
2320/* END_CASE */
2321
2322/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002323void key_policy_alg2( int key_type_arg, data_t *key_data,
2324 int usage_arg, int alg_arg, int alg2_arg )
2325{
Ronald Cron5425a212020-08-04 14:58:35 +02002326 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002327 psa_key_type_t key_type = key_type_arg;
2328 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2329 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2330 psa_key_usage_t usage = usage_arg;
2331 psa_algorithm_t alg = alg_arg;
2332 psa_algorithm_t alg2 = alg2_arg;
2333
2334 PSA_ASSERT( psa_crypto_init( ) );
2335
2336 psa_set_key_usage_flags( &attributes, usage );
2337 psa_set_key_algorithm( &attributes, alg );
2338 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2339 psa_set_key_type( &attributes, key_type );
2340 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002341 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002342
Ronald Cron5425a212020-08-04 14:58:35 +02002343 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002344 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2345 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2346 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2347
Ronald Cron5425a212020-08-04 14:58:35 +02002348 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002349 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002350 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002351 goto exit;
2352
2353exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002354 /*
2355 * Key attributes may have been returned by psa_get_key_attributes()
2356 * thus reset them as required.
2357 */
2358 psa_reset_key_attributes( &got_attributes );
2359
Ronald Cron5425a212020-08-04 14:58:35 +02002360 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002361 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002362}
2363/* END_CASE */
2364
2365/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002366void raw_agreement_key_policy( int policy_usage,
2367 int policy_alg,
2368 int key_type_arg,
2369 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002370 int exercise_alg,
2371 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002372{
Ronald Cron5425a212020-08-04 14:58:35 +02002373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002375 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002376 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002377 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002378 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002379
2380 PSA_ASSERT( psa_crypto_init( ) );
2381
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002382 psa_set_key_usage_flags( &attributes, policy_usage );
2383 psa_set_key_algorithm( &attributes, policy_alg );
2384 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002385
Gilles Peskine049c7532019-05-15 20:22:09 +02002386 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002387 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002388
Ronald Cron5425a212020-08-04 14:58:35 +02002389 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002390
Steven Cooremance48e852020-10-05 16:02:45 +02002391 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002392
2393exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002394 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002395 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002396 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002397}
2398/* END_CASE */
2399
2400/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002401void copy_success( int source_usage_arg,
2402 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002403 int type_arg, data_t *material,
2404 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002405 int target_usage_arg,
2406 int target_alg_arg, int target_alg2_arg,
2407 int expected_usage_arg,
2408 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002409{
Gilles Peskineca25db92019-04-19 11:43:08 +02002410 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2411 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002412 psa_key_usage_t expected_usage = expected_usage_arg;
2413 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002414 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002415 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2416 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002417 uint8_t *export_buffer = NULL;
2418
Gilles Peskine57ab7212019-01-28 13:03:09 +01002419 PSA_ASSERT( psa_crypto_init( ) );
2420
Gilles Peskineca25db92019-04-19 11:43:08 +02002421 /* Prepare the source key. */
2422 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2423 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002424 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002425 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002426 PSA_ASSERT( psa_import_key( &source_attributes,
2427 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002428 &source_key ) );
2429 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002430
Gilles Peskineca25db92019-04-19 11:43:08 +02002431 /* Prepare the target attributes. */
2432 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002433 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002434 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002435 /* Set volatile lifetime to reset the key identifier to 0. */
2436 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2437 }
2438
Gilles Peskineca25db92019-04-19 11:43:08 +02002439 if( target_usage_arg != -1 )
2440 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2441 if( target_alg_arg != -1 )
2442 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002443 if( target_alg2_arg != -1 )
2444 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002445
2446 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002447 PSA_ASSERT( psa_copy_key( source_key,
2448 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002449
2450 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002451 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002452
2453 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002454 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002455 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2456 psa_get_key_type( &target_attributes ) );
2457 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2458 psa_get_key_bits( &target_attributes ) );
2459 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2460 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002461 TEST_EQUAL( expected_alg2,
2462 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002463 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2464 {
2465 size_t length;
2466 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002467 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468 material->len, &length ) );
2469 ASSERT_COMPARE( material->x, material->len,
2470 export_buffer, length );
2471 }
Ronald Cron5425a212020-08-04 14:58:35 +02002472 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002473 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002474 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002475 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002476
Ronald Cron5425a212020-08-04 14:58:35 +02002477 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002478
2479exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002480 /*
2481 * Source and target key attributes may have been returned by
2482 * psa_get_key_attributes() thus reset them as required.
2483 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002484 psa_reset_key_attributes( &source_attributes );
2485 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002486
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002487 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002488 mbedtls_free( export_buffer );
2489}
2490/* END_CASE */
2491
2492/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002493void copy_fail( int source_usage_arg,
2494 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002495 int type_arg, data_t *material,
2496 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002497 int target_usage_arg,
2498 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002499 int expected_status_arg )
2500{
2501 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2502 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002503 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2504 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002505
2506 PSA_ASSERT( psa_crypto_init( ) );
2507
2508 /* Prepare the source key. */
2509 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2510 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002511 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002512 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002513 PSA_ASSERT( psa_import_key( &source_attributes,
2514 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002515 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002516
2517 /* Prepare the target attributes. */
2518 psa_set_key_type( &target_attributes, target_type_arg );
2519 psa_set_key_bits( &target_attributes, target_bits_arg );
2520 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2521 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002522 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002523
2524 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002525 TEST_EQUAL( psa_copy_key( source_key,
2526 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002527 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002528
Ronald Cron5425a212020-08-04 14:58:35 +02002529 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002530
Gilles Peskine4a644642019-05-03 17:14:08 +02002531exit:
2532 psa_reset_key_attributes( &source_attributes );
2533 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002534 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002535}
2536/* END_CASE */
2537
2538/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002539void hash_operation_init( )
2540{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002541 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002542 /* Test each valid way of initializing the object, except for `= {0}`, as
2543 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2544 * though it's OK by the C standard. We could test for this, but we'd need
2545 * to supress the Clang warning for the test. */
2546 psa_hash_operation_t func = psa_hash_operation_init( );
2547 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2548 psa_hash_operation_t zero;
2549
2550 memset( &zero, 0, sizeof( zero ) );
2551
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002552 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002553 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2554 PSA_ERROR_BAD_STATE );
2555 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2556 PSA_ERROR_BAD_STATE );
2557 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2558 PSA_ERROR_BAD_STATE );
2559
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002560 /* A default hash operation should be abortable without error. */
2561 PSA_ASSERT( psa_hash_abort( &func ) );
2562 PSA_ASSERT( psa_hash_abort( &init ) );
2563 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002564}
2565/* END_CASE */
2566
2567/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002568void hash_setup( int alg_arg,
2569 int expected_status_arg )
2570{
2571 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002572 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002573 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002574 psa_status_t status;
2575
Gilles Peskine8817f612018-12-18 00:18:46 +01002576 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002577
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002578 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002579 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002580
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002581 /* Whether setup succeeded or failed, abort must succeed. */
2582 PSA_ASSERT( psa_hash_abort( &operation ) );
2583
2584 /* If setup failed, reproduce the failure, so as to
2585 * test the resulting state of the operation object. */
2586 if( status != PSA_SUCCESS )
2587 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2588
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002589 /* Now the operation object should be reusable. */
2590#if defined(KNOWN_SUPPORTED_HASH_ALG)
2591 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2592 PSA_ASSERT( psa_hash_abort( &operation ) );
2593#endif
2594
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002595exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002596 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002597}
2598/* END_CASE */
2599
2600/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002601void hash_compute_fail( int alg_arg, data_t *input,
2602 int output_size_arg, int expected_status_arg )
2603{
2604 psa_algorithm_t alg = alg_arg;
2605 uint8_t *output = NULL;
2606 size_t output_size = output_size_arg;
2607 size_t output_length = INVALID_EXPORT_LENGTH;
2608 psa_status_t expected_status = expected_status_arg;
2609 psa_status_t status;
2610
2611 ASSERT_ALLOC( output, output_size );
2612
2613 PSA_ASSERT( psa_crypto_init( ) );
2614
2615 status = psa_hash_compute( alg, input->x, input->len,
2616 output, output_size, &output_length );
2617 TEST_EQUAL( status, expected_status );
2618 TEST_ASSERT( output_length <= output_size );
2619
2620exit:
2621 mbedtls_free( output );
2622 PSA_DONE( );
2623}
2624/* END_CASE */
2625
2626/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002627void hash_compare_fail( int alg_arg, data_t *input,
2628 data_t *reference_hash,
2629 int expected_status_arg )
2630{
2631 psa_algorithm_t alg = alg_arg;
2632 psa_status_t expected_status = expected_status_arg;
2633 psa_status_t status;
2634
2635 PSA_ASSERT( psa_crypto_init( ) );
2636
2637 status = psa_hash_compare( alg, input->x, input->len,
2638 reference_hash->x, reference_hash->len );
2639 TEST_EQUAL( status, expected_status );
2640
2641exit:
2642 PSA_DONE( );
2643}
2644/* END_CASE */
2645
2646/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002647void hash_compute_compare( int alg_arg, data_t *input,
2648 data_t *expected_output )
2649{
2650 psa_algorithm_t alg = alg_arg;
2651 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2652 size_t output_length = INVALID_EXPORT_LENGTH;
2653 size_t i;
2654
2655 PSA_ASSERT( psa_crypto_init( ) );
2656
2657 /* Compute with tight buffer */
2658 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002659 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002660 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002661 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002662 ASSERT_COMPARE( output, output_length,
2663 expected_output->x, expected_output->len );
2664
2665 /* Compute with larger buffer */
2666 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2667 output, sizeof( output ),
2668 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002669 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002670 ASSERT_COMPARE( output, output_length,
2671 expected_output->x, expected_output->len );
2672
2673 /* Compare with correct hash */
2674 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2675 output, output_length ) );
2676
2677 /* Compare with trailing garbage */
2678 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2679 output, output_length + 1 ),
2680 PSA_ERROR_INVALID_SIGNATURE );
2681
2682 /* Compare with truncated hash */
2683 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2684 output, output_length - 1 ),
2685 PSA_ERROR_INVALID_SIGNATURE );
2686
2687 /* Compare with corrupted value */
2688 for( i = 0; i < output_length; i++ )
2689 {
Chris Jones9634bb12021-01-20 15:56:42 +00002690 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002691 output[i] ^= 1;
2692 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2693 output, output_length ),
2694 PSA_ERROR_INVALID_SIGNATURE );
2695 output[i] ^= 1;
2696 }
2697
2698exit:
2699 PSA_DONE( );
2700}
2701/* END_CASE */
2702
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002703/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002704void hash_bad_order( )
2705{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002706 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002707 unsigned char input[] = "";
2708 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002709 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002710 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2711 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2712 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002713 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002714 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002715 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002716
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002718
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002719 /* Call setup twice in a row. */
2720 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2721 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2722 PSA_ERROR_BAD_STATE );
2723 PSA_ASSERT( psa_hash_abort( &operation ) );
2724
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002725 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002726 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002727 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002728 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002729
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002730 /* Call update after finish. */
2731 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2732 PSA_ASSERT( psa_hash_finish( &operation,
2733 hash, sizeof( hash ), &hash_len ) );
2734 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002735 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002736 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002737
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002738 /* Call verify without calling setup beforehand. */
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 after finish. */
2745 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2746 PSA_ASSERT( psa_hash_finish( &operation,
2747 hash, sizeof( hash ), &hash_len ) );
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 verify twice in a row. */
2754 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2755 PSA_ASSERT( psa_hash_verify( &operation,
2756 valid_hash, sizeof( valid_hash ) ) );
2757 TEST_EQUAL( psa_hash_verify( &operation,
2758 valid_hash, sizeof( valid_hash ) ),
2759 PSA_ERROR_BAD_STATE );
2760 PSA_ASSERT( psa_hash_abort( &operation ) );
2761
2762 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002763 TEST_EQUAL( psa_hash_finish( &operation,
2764 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002765 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002766 PSA_ASSERT( psa_hash_abort( &operation ) );
2767
2768 /* Call finish twice in a row. */
2769 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2770 PSA_ASSERT( psa_hash_finish( &operation,
2771 hash, sizeof( hash ), &hash_len ) );
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 ) );
2776
2777 /* Call finish after calling verify. */
2778 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2779 PSA_ASSERT( psa_hash_verify( &operation,
2780 valid_hash, sizeof( valid_hash ) ) );
2781 TEST_EQUAL( psa_hash_finish( &operation,
2782 hash, sizeof( hash ), &hash_len ),
2783 PSA_ERROR_BAD_STATE );
2784 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002785
2786exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002787 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002788}
2789/* END_CASE */
2790
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002791/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002792void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002793{
2794 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002795 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2796 * appended to it */
2797 unsigned char hash[] = {
2798 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2799 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2800 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002801 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002802 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002803
Gilles Peskine8817f612018-12-18 00:18:46 +01002804 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002805
itayzafrir27e69452018-11-01 14:26:34 +02002806 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002807 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002808 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002809 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002810
itayzafrir27e69452018-11-01 14:26:34 +02002811 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002812 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002813 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002814 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002815
itayzafrir27e69452018-11-01 14:26:34 +02002816 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002817 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002818 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002819 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002820
itayzafrirec93d302018-10-18 18:01:10 +03002821exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002822 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002823}
2824/* END_CASE */
2825
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002826/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2827void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002828{
2829 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002830 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002831 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002832 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002833 size_t hash_len;
2834
Gilles Peskine8817f612018-12-18 00:18:46 +01002835 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002836
itayzafrir58028322018-10-25 10:22:01 +03002837 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002838 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002839 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002840 hash, expected_size - 1, &hash_len ),
2841 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002842
2843exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002844 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002845}
2846/* END_CASE */
2847
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002848/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2849void hash_clone_source_state( )
2850{
2851 psa_algorithm_t alg = PSA_ALG_SHA_256;
2852 unsigned char hash[PSA_HASH_MAX_SIZE];
2853 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2854 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2855 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2856 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2857 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2858 size_t hash_len;
2859
2860 PSA_ASSERT( psa_crypto_init( ) );
2861 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2862
2863 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2864 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2865 PSA_ASSERT( psa_hash_finish( &op_finished,
2866 hash, sizeof( hash ), &hash_len ) );
2867 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2868 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2869
2870 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2871 PSA_ERROR_BAD_STATE );
2872
2873 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2874 PSA_ASSERT( psa_hash_finish( &op_init,
2875 hash, sizeof( hash ), &hash_len ) );
2876 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2877 PSA_ASSERT( psa_hash_finish( &op_finished,
2878 hash, sizeof( hash ), &hash_len ) );
2879 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2880 PSA_ASSERT( psa_hash_finish( &op_aborted,
2881 hash, sizeof( hash ), &hash_len ) );
2882
2883exit:
2884 psa_hash_abort( &op_source );
2885 psa_hash_abort( &op_init );
2886 psa_hash_abort( &op_setup );
2887 psa_hash_abort( &op_finished );
2888 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002889 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002890}
2891/* END_CASE */
2892
2893/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2894void hash_clone_target_state( )
2895{
2896 psa_algorithm_t alg = PSA_ALG_SHA_256;
2897 unsigned char hash[PSA_HASH_MAX_SIZE];
2898 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2899 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2900 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2901 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2902 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2903 size_t hash_len;
2904
2905 PSA_ASSERT( psa_crypto_init( ) );
2906
2907 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2908 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2909 PSA_ASSERT( psa_hash_finish( &op_finished,
2910 hash, sizeof( hash ), &hash_len ) );
2911 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2912 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2913
2914 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2915 PSA_ASSERT( psa_hash_finish( &op_target,
2916 hash, sizeof( hash ), &hash_len ) );
2917
2918 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2919 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2920 PSA_ERROR_BAD_STATE );
2921 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2922 PSA_ERROR_BAD_STATE );
2923
2924exit:
2925 psa_hash_abort( &op_target );
2926 psa_hash_abort( &op_init );
2927 psa_hash_abort( &op_setup );
2928 psa_hash_abort( &op_finished );
2929 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002930 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002931}
2932/* END_CASE */
2933
itayzafrir58028322018-10-25 10:22:01 +03002934/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002935void mac_operation_init( )
2936{
Jaeden Amero252ef282019-02-15 14:05:35 +00002937 const uint8_t input[1] = { 0 };
2938
Jaeden Amero769ce272019-01-04 11:48:03 +00002939 /* Test each valid way of initializing the object, except for `= {0}`, as
2940 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2941 * though it's OK by the C standard. We could test for this, but we'd need
2942 * to supress the Clang warning for the test. */
2943 psa_mac_operation_t func = psa_mac_operation_init( );
2944 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2945 psa_mac_operation_t zero;
2946
2947 memset( &zero, 0, sizeof( zero ) );
2948
Jaeden Amero252ef282019-02-15 14:05:35 +00002949 /* A freshly-initialized MAC operation should not be usable. */
2950 TEST_EQUAL( psa_mac_update( &func,
2951 input, sizeof( input ) ),
2952 PSA_ERROR_BAD_STATE );
2953 TEST_EQUAL( psa_mac_update( &init,
2954 input, sizeof( input ) ),
2955 PSA_ERROR_BAD_STATE );
2956 TEST_EQUAL( psa_mac_update( &zero,
2957 input, sizeof( input ) ),
2958 PSA_ERROR_BAD_STATE );
2959
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002960 /* A default MAC operation should be abortable without error. */
2961 PSA_ASSERT( psa_mac_abort( &func ) );
2962 PSA_ASSERT( psa_mac_abort( &init ) );
2963 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002964}
2965/* END_CASE */
2966
2967/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002968void mac_setup( int key_type_arg,
2969 data_t *key,
2970 int alg_arg,
2971 int expected_status_arg )
2972{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002973 psa_key_type_t key_type = key_type_arg;
2974 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002975 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002976 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002977 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2978#if defined(KNOWN_SUPPORTED_MAC_ALG)
2979 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2980#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002981
Gilles Peskine8817f612018-12-18 00:18:46 +01002982 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002983
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002984 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2985 &operation, &status ) )
2986 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002987 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002988
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002989 /* The operation object should be reusable. */
2990#if defined(KNOWN_SUPPORTED_MAC_ALG)
2991 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2992 smoke_test_key_data,
2993 sizeof( smoke_test_key_data ),
2994 KNOWN_SUPPORTED_MAC_ALG,
2995 &operation, &status ) )
2996 goto exit;
2997 TEST_EQUAL( status, PSA_SUCCESS );
2998#endif
2999
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003000exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003001 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003002}
3003/* END_CASE */
3004
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003005/* 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 +00003006void mac_bad_order( )
3007{
Ronald Cron5425a212020-08-04 14:58:35 +02003008 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003009 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3010 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003011 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003012 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3013 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3014 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003016 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3017 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3018 size_t sign_mac_length = 0;
3019 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3020 const uint8_t verify_mac[] = {
3021 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3022 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3023 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3024
3025 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003026 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003027 psa_set_key_algorithm( &attributes, alg );
3028 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003029
Ronald Cron5425a212020-08-04 14:58:35 +02003030 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3031 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003032
Jaeden Amero252ef282019-02-15 14:05:35 +00003033 /* Call update without calling setup beforehand. */
3034 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3035 PSA_ERROR_BAD_STATE );
3036 PSA_ASSERT( psa_mac_abort( &operation ) );
3037
3038 /* Call sign finish without calling setup beforehand. */
3039 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3040 &sign_mac_length),
3041 PSA_ERROR_BAD_STATE );
3042 PSA_ASSERT( psa_mac_abort( &operation ) );
3043
3044 /* Call verify finish without calling setup beforehand. */
3045 TEST_EQUAL( psa_mac_verify_finish( &operation,
3046 verify_mac, sizeof( verify_mac ) ),
3047 PSA_ERROR_BAD_STATE );
3048 PSA_ASSERT( psa_mac_abort( &operation ) );
3049
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003050 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003051 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
3052 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003053 PSA_ERROR_BAD_STATE );
3054 PSA_ASSERT( psa_mac_abort( &operation ) );
3055
Jaeden Amero252ef282019-02-15 14:05:35 +00003056 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003057 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003058 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3059 PSA_ASSERT( psa_mac_sign_finish( &operation,
3060 sign_mac, sizeof( sign_mac ),
3061 &sign_mac_length ) );
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 update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003067 PSA_ASSERT( psa_mac_verify_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_verify_finish( &operation,
3070 verify_mac, sizeof( verify_mac ) ) );
3071 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3072 PSA_ERROR_BAD_STATE );
3073 PSA_ASSERT( psa_mac_abort( &operation ) );
3074
3075 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003076 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003077 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3078 PSA_ASSERT( psa_mac_sign_finish( &operation,
3079 sign_mac, sizeof( sign_mac ),
3080 &sign_mac_length ) );
3081 TEST_EQUAL( psa_mac_sign_finish( &operation,
3082 sign_mac, sizeof( sign_mac ),
3083 &sign_mac_length ),
3084 PSA_ERROR_BAD_STATE );
3085 PSA_ASSERT( psa_mac_abort( &operation ) );
3086
3087 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003088 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003089 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3090 PSA_ASSERT( psa_mac_verify_finish( &operation,
3091 verify_mac, sizeof( verify_mac ) ) );
3092 TEST_EQUAL( psa_mac_verify_finish( &operation,
3093 verify_mac, sizeof( verify_mac ) ),
3094 PSA_ERROR_BAD_STATE );
3095 PSA_ASSERT( psa_mac_abort( &operation ) );
3096
3097 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003098 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003099 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3100 TEST_EQUAL( psa_mac_verify_finish( &operation,
3101 verify_mac, sizeof( verify_mac ) ),
3102 PSA_ERROR_BAD_STATE );
3103 PSA_ASSERT( psa_mac_abort( &operation ) );
3104
3105 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003106 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003107 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3108 TEST_EQUAL( psa_mac_sign_finish( &operation,
3109 sign_mac, sizeof( sign_mac ),
3110 &sign_mac_length ),
3111 PSA_ERROR_BAD_STATE );
3112 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003113
Ronald Cron5425a212020-08-04 14:58:35 +02003114 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003115
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003116exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003117 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003118}
3119/* END_CASE */
3120
3121/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003122void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003123 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003124 int alg_arg,
3125 data_t *input,
3126 data_t *expected_mac )
3127{
Ronald Cron5425a212020-08-04 14:58:35 +02003128 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003129 psa_key_type_t key_type = key_type_arg;
3130 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003131 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003132 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003133 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003134 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003135 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003136 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003137 const size_t output_sizes_to_test[] = {
3138 0,
3139 1,
3140 expected_mac->len - 1,
3141 expected_mac->len,
3142 expected_mac->len + 1,
3143 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003144
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003145 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003146 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003147 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003148
Gilles Peskine8817f612018-12-18 00:18:46 +01003149 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003150
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003151 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003152 psa_set_key_algorithm( &attributes, alg );
3153 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003154
Ronald Cron5425a212020-08-04 14:58:35 +02003155 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3156 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003157
Gilles Peskine8b356b52020-08-25 23:44:59 +02003158 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3159 {
3160 const size_t output_size = output_sizes_to_test[i];
3161 psa_status_t expected_status =
3162 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3163 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003164
Chris Jones9634bb12021-01-20 15:56:42 +00003165 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003166 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003167
Gilles Peskine8b356b52020-08-25 23:44:59 +02003168 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003169 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003170 PSA_ASSERT( psa_mac_update( &operation,
3171 input->x, input->len ) );
3172 TEST_EQUAL( psa_mac_sign_finish( &operation,
3173 actual_mac, output_size,
3174 &mac_length ),
3175 expected_status );
3176 PSA_ASSERT( psa_mac_abort( &operation ) );
3177
3178 if( expected_status == PSA_SUCCESS )
3179 {
3180 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3181 actual_mac, mac_length );
3182 }
3183 mbedtls_free( actual_mac );
3184 actual_mac = NULL;
3185 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003186
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003187exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003188 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003189 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003190 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003191 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003192}
3193/* END_CASE */
3194
3195/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003196void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003197 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003198 int alg_arg,
3199 data_t *input,
3200 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003201{
Ronald Cron5425a212020-08-04 14:58:35 +02003202 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003203 psa_key_type_t key_type = key_type_arg;
3204 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003205 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003207 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003208
Gilles Peskine69c12672018-06-28 00:07:19 +02003209 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3210
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003212
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003213 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003214 psa_set_key_algorithm( &attributes, alg );
3215 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003216
Ronald Cron5425a212020-08-04 14:58:35 +02003217 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3218 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003219
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003220 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003221 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003222 PSA_ASSERT( psa_mac_update( &operation,
3223 input->x, input->len ) );
3224 PSA_ASSERT( psa_mac_verify_finish( &operation,
3225 expected_mac->x,
3226 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003227
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003228 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003229 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003230 PSA_ASSERT( psa_mac_update( &operation,
3231 input->x, input->len ) );
3232 TEST_EQUAL( psa_mac_verify_finish( &operation,
3233 expected_mac->x,
3234 expected_mac->len - 1 ),
3235 PSA_ERROR_INVALID_SIGNATURE );
3236
3237 /* Test a MAC that's too long. */
3238 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3239 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003240 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003241 PSA_ASSERT( psa_mac_update( &operation,
3242 input->x, input->len ) );
3243 TEST_EQUAL( psa_mac_verify_finish( &operation,
3244 perturbed_mac,
3245 expected_mac->len + 1 ),
3246 PSA_ERROR_INVALID_SIGNATURE );
3247
3248 /* Test changing one byte. */
3249 for( size_t i = 0; i < expected_mac->len; i++ )
3250 {
Chris Jones9634bb12021-01-20 15:56:42 +00003251 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003252 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003253 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003254 PSA_ASSERT( psa_mac_update( &operation,
3255 input->x, input->len ) );
3256 TEST_EQUAL( psa_mac_verify_finish( &operation,
3257 perturbed_mac,
3258 expected_mac->len ),
3259 PSA_ERROR_INVALID_SIGNATURE );
3260 perturbed_mac[i] ^= 1;
3261 }
3262
Gilles Peskine8c9def32018-02-08 10:02:12 +01003263exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003264 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003265 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003266 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003267 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003268}
3269/* END_CASE */
3270
3271/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003272void cipher_operation_init( )
3273{
Jaeden Ameroab439972019-02-15 14:12:05 +00003274 const uint8_t input[1] = { 0 };
3275 unsigned char output[1] = { 0 };
3276 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003277 /* Test each valid way of initializing the object, except for `= {0}`, as
3278 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3279 * though it's OK by the C standard. We could test for this, but we'd need
3280 * to supress the Clang warning for the test. */
3281 psa_cipher_operation_t func = psa_cipher_operation_init( );
3282 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3283 psa_cipher_operation_t zero;
3284
3285 memset( &zero, 0, sizeof( zero ) );
3286
Jaeden Ameroab439972019-02-15 14:12:05 +00003287 /* A freshly-initialized cipher operation should not be usable. */
3288 TEST_EQUAL( psa_cipher_update( &func,
3289 input, sizeof( input ),
3290 output, sizeof( output ),
3291 &output_length ),
3292 PSA_ERROR_BAD_STATE );
3293 TEST_EQUAL( psa_cipher_update( &init,
3294 input, sizeof( input ),
3295 output, sizeof( output ),
3296 &output_length ),
3297 PSA_ERROR_BAD_STATE );
3298 TEST_EQUAL( psa_cipher_update( &zero,
3299 input, sizeof( input ),
3300 output, sizeof( output ),
3301 &output_length ),
3302 PSA_ERROR_BAD_STATE );
3303
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003304 /* A default cipher operation should be abortable without error. */
3305 PSA_ASSERT( psa_cipher_abort( &func ) );
3306 PSA_ASSERT( psa_cipher_abort( &init ) );
3307 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003308}
3309/* END_CASE */
3310
3311/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003312void cipher_setup( int key_type_arg,
3313 data_t *key,
3314 int alg_arg,
3315 int expected_status_arg )
3316{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003317 psa_key_type_t key_type = key_type_arg;
3318 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003319 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003320 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003321 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003322#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003323 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3324#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003325
Gilles Peskine8817f612018-12-18 00:18:46 +01003326 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003327
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003328 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3329 &operation, &status ) )
3330 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003331 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003332
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003333 /* The operation object should be reusable. */
3334#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3335 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3336 smoke_test_key_data,
3337 sizeof( smoke_test_key_data ),
3338 KNOWN_SUPPORTED_CIPHER_ALG,
3339 &operation, &status ) )
3340 goto exit;
3341 TEST_EQUAL( status, PSA_SUCCESS );
3342#endif
3343
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003344exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003345 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003346 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003347}
3348/* END_CASE */
3349
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003350/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003351void cipher_bad_order( )
3352{
Ronald Cron5425a212020-08-04 14:58:35 +02003353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003354 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3355 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003357 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003358 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003359 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003360 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3361 0xaa, 0xaa, 0xaa, 0xaa };
3362 const uint8_t text[] = {
3363 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3364 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003365 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003366 size_t length = 0;
3367
3368 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003369 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3370 psa_set_key_algorithm( &attributes, alg );
3371 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003372 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3373 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003374
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003375 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003376 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3377 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003378 PSA_ERROR_BAD_STATE );
3379 PSA_ASSERT( psa_cipher_abort( &operation ) );
3380
3381 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003382 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3383 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003384 PSA_ERROR_BAD_STATE );
3385 PSA_ASSERT( psa_cipher_abort( &operation ) );
3386
Jaeden Ameroab439972019-02-15 14:12:05 +00003387 /* Generate an IV without calling setup beforehand. */
3388 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3389 buffer, sizeof( buffer ),
3390 &length ),
3391 PSA_ERROR_BAD_STATE );
3392 PSA_ASSERT( psa_cipher_abort( &operation ) );
3393
3394 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003395 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003396 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3397 buffer, sizeof( buffer ),
3398 &length ) );
3399 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3400 buffer, sizeof( buffer ),
3401 &length ),
3402 PSA_ERROR_BAD_STATE );
3403 PSA_ASSERT( psa_cipher_abort( &operation ) );
3404
3405 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003406 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003407 PSA_ASSERT( psa_cipher_set_iv( &operation,
3408 iv, sizeof( iv ) ) );
3409 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3410 buffer, sizeof( buffer ),
3411 &length ),
3412 PSA_ERROR_BAD_STATE );
3413 PSA_ASSERT( psa_cipher_abort( &operation ) );
3414
3415 /* Set an IV without calling setup beforehand. */
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 set. */
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_set_iv( &operation,
3424 iv, sizeof( iv ) ) );
3425 TEST_EQUAL( psa_cipher_set_iv( &operation,
3426 iv, sizeof( iv ) ),
3427 PSA_ERROR_BAD_STATE );
3428 PSA_ASSERT( psa_cipher_abort( &operation ) );
3429
3430 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003431 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003432 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3433 buffer, sizeof( buffer ),
3434 &length ) );
3435 TEST_EQUAL( psa_cipher_set_iv( &operation,
3436 iv, sizeof( iv ) ),
3437 PSA_ERROR_BAD_STATE );
3438 PSA_ASSERT( psa_cipher_abort( &operation ) );
3439
3440 /* Call update without calling setup beforehand. */
3441 TEST_EQUAL( psa_cipher_update( &operation,
3442 text, sizeof( text ),
3443 buffer, sizeof( buffer ),
3444 &length ),
3445 PSA_ERROR_BAD_STATE );
3446 PSA_ASSERT( psa_cipher_abort( &operation ) );
3447
3448 /* Call update without an IV where an IV is required. */
3449 TEST_EQUAL( psa_cipher_update( &operation,
3450 text, sizeof( text ),
3451 buffer, sizeof( buffer ),
3452 &length ),
3453 PSA_ERROR_BAD_STATE );
3454 PSA_ASSERT( psa_cipher_abort( &operation ) );
3455
3456 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003457 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003458 PSA_ASSERT( psa_cipher_set_iv( &operation,
3459 iv, sizeof( iv ) ) );
3460 PSA_ASSERT( psa_cipher_finish( &operation,
3461 buffer, sizeof( buffer ), &length ) );
3462 TEST_EQUAL( psa_cipher_update( &operation,
3463 text, sizeof( text ),
3464 buffer, sizeof( buffer ),
3465 &length ),
3466 PSA_ERROR_BAD_STATE );
3467 PSA_ASSERT( psa_cipher_abort( &operation ) );
3468
3469 /* Call finish without calling setup beforehand. */
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 without an IV where an IV is required. */
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 /* Not calling update means we are encrypting an empty buffer, which is OK
3478 * for cipher modes with padding. */
3479 TEST_EQUAL( psa_cipher_finish( &operation,
3480 buffer, sizeof( buffer ), &length ),
3481 PSA_ERROR_BAD_STATE );
3482 PSA_ASSERT( psa_cipher_abort( &operation ) );
3483
3484 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003485 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003486 PSA_ASSERT( psa_cipher_set_iv( &operation,
3487 iv, sizeof( iv ) ) );
3488 PSA_ASSERT( psa_cipher_finish( &operation,
3489 buffer, sizeof( buffer ), &length ) );
3490 TEST_EQUAL( psa_cipher_finish( &operation,
3491 buffer, sizeof( buffer ), &length ),
3492 PSA_ERROR_BAD_STATE );
3493 PSA_ASSERT( psa_cipher_abort( &operation ) );
3494
Ronald Cron5425a212020-08-04 14:58:35 +02003495 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003496
Jaeden Ameroab439972019-02-15 14:12:05 +00003497exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003498 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003499 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003500}
3501/* END_CASE */
3502
3503/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003504void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003505 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003506 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003507 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003508{
Ronald Cron5425a212020-08-04 14:58:35 +02003509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003510 psa_status_t status;
3511 psa_key_type_t key_type = key_type_arg;
3512 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003513 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003514 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003515 size_t output_buffer_size = 0;
3516 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003517 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003518 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003520
Gilles Peskine8817f612018-12-18 00:18:46 +01003521 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003522
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003523 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3524 psa_set_key_algorithm( &attributes, alg );
3525 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003526
Ronald Cron5425a212020-08-04 14:58:35 +02003527 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3528 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003529
Ronald Cron5425a212020-08-04 14:58:35 +02003530 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003531
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003532 if( iv->len > 0 )
3533 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003534 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003535 }
3536
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003537 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003538 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003539 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003540
Gilles Peskine8817f612018-12-18 00:18:46 +01003541 PSA_ASSERT( psa_cipher_update( &operation,
3542 input->x, input->len,
3543 output, output_buffer_size,
3544 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003545 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003546 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003547 output + total_output_length,
3548 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003549 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003550 total_output_length += function_output_length;
3551
Gilles Peskinefe11b722018-12-18 00:24:04 +01003552 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003553 if( expected_status == PSA_SUCCESS )
3554 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003555 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003556 ASSERT_COMPARE( expected_output->x, expected_output->len,
3557 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003558 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003559
Gilles Peskine50e586b2018-06-08 14:28:46 +02003560exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003561 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003562 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003563 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003564 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003565}
3566/* END_CASE */
3567
3568/* BEGIN_CASE */
3569void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003570 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003571 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003572 int first_part_size_arg,
3573 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003574 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003575{
Ronald Cron5425a212020-08-04 14:58:35 +02003576 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003577 psa_key_type_t key_type = key_type_arg;
3578 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003579 size_t first_part_size = first_part_size_arg;
3580 size_t output1_length = output1_length_arg;
3581 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003582 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003583 size_t output_buffer_size = 0;
3584 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003585 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003586 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003587 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003588
Gilles Peskine8817f612018-12-18 00:18:46 +01003589 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003590
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003591 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3592 psa_set_key_algorithm( &attributes, alg );
3593 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003594
Ronald Cron5425a212020-08-04 14:58:35 +02003595 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3596 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003597
Ronald Cron5425a212020-08-04 14:58:35 +02003598 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003599
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003600 if( iv->len > 0 )
3601 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003602 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003603 }
3604
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003605 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003606 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003607 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003608
Gilles Peskinee0866522019-02-19 19:44:00 +01003609 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003610 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3611 output, output_buffer_size,
3612 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003613 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003614 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003615 PSA_ASSERT( psa_cipher_update( &operation,
3616 input->x + first_part_size,
3617 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003618 output + total_output_length,
3619 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003620 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003621 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003622 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003623 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003624 output + total_output_length,
3625 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003626 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003627 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003628 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003629
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003630 ASSERT_COMPARE( expected_output->x, expected_output->len,
3631 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003632
3633exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003634 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003635 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003636 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003637 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003638}
3639/* END_CASE */
3640
3641/* BEGIN_CASE */
3642void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003643 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003644 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003645 int first_part_size_arg,
3646 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003647 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003648{
Ronald Cron5425a212020-08-04 14:58:35 +02003649 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003650 psa_key_type_t key_type = key_type_arg;
3651 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003652 size_t first_part_size = first_part_size_arg;
3653 size_t output1_length = output1_length_arg;
3654 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003655 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003656 size_t output_buffer_size = 0;
3657 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003658 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003659 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003661
Gilles Peskine8817f612018-12-18 00:18:46 +01003662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003663
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003664 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3665 psa_set_key_algorithm( &attributes, alg );
3666 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003667
Ronald Cron5425a212020-08-04 14:58:35 +02003668 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3669 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003670
Ronald Cron5425a212020-08-04 14:58:35 +02003671 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003672
Steven Cooreman177deba2020-09-07 17:14:14 +02003673 if( iv->len > 0 )
3674 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003675 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003676 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003677
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003678 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003679 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003680 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003681
Gilles Peskinee0866522019-02-19 19:44:00 +01003682 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003683 PSA_ASSERT( psa_cipher_update( &operation,
3684 input->x, first_part_size,
3685 output, output_buffer_size,
3686 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003687 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003688 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003689 PSA_ASSERT( psa_cipher_update( &operation,
3690 input->x + first_part_size,
3691 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003692 output + total_output_length,
3693 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003694 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003695 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003696 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003697 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003698 output + total_output_length,
3699 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003700 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003701 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003702 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003703
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003704 ASSERT_COMPARE( expected_output->x, expected_output->len,
3705 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003706
3707exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003708 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003709 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003710 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003711 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003712}
3713/* END_CASE */
3714
Gilles Peskine50e586b2018-06-08 14:28:46 +02003715/* BEGIN_CASE */
3716void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003717 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003718 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003719 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003720{
Ronald Cron5425a212020-08-04 14:58:35 +02003721 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003722 psa_status_t status;
3723 psa_key_type_t key_type = key_type_arg;
3724 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003725 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003726 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003727 size_t output_buffer_size = 0;
3728 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003729 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003730 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003731 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003732
Gilles Peskine8817f612018-12-18 00:18:46 +01003733 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003734
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003735 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3736 psa_set_key_algorithm( &attributes, alg );
3737 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003738
Ronald Cron5425a212020-08-04 14:58:35 +02003739 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3740 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003741
Ronald Cron5425a212020-08-04 14:58:35 +02003742 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003743
Steven Cooreman177deba2020-09-07 17:14:14 +02003744 if( iv->len > 0 )
3745 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003746 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003747 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003748
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003749 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003750 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003751 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003752
Gilles Peskine8817f612018-12-18 00:18:46 +01003753 PSA_ASSERT( psa_cipher_update( &operation,
3754 input->x, input->len,
3755 output, output_buffer_size,
3756 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003757 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003758 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003759 output + total_output_length,
3760 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003761 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003762 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003763 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003764
3765 if( expected_status == PSA_SUCCESS )
3766 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003767 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003768 ASSERT_COMPARE( expected_output->x, expected_output->len,
3769 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003770 }
3771
Gilles Peskine50e586b2018-06-08 14:28:46 +02003772exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003773 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003774 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003775 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003776 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003777}
3778/* END_CASE */
3779
Gilles Peskine50e586b2018-06-08 14:28:46 +02003780/* BEGIN_CASE */
3781void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003782 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003783 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003784{
Ronald Cron5425a212020-08-04 14:58:35 +02003785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003786 psa_key_type_t key_type = key_type_arg;
3787 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003788 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003789 size_t iv_size = 16;
3790 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003791 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003792 size_t output1_size = 0;
3793 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003794 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003795 size_t output2_size = 0;
3796 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003797 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003798 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3799 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003801
Gilles Peskine8817f612018-12-18 00:18:46 +01003802 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003803
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003804 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3805 psa_set_key_algorithm( &attributes, alg );
3806 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003807
Ronald Cron5425a212020-08-04 14:58:35 +02003808 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3809 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003810
Ronald Cron5425a212020-08-04 14:58:35 +02003811 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3812 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003813
Steven Cooreman177deba2020-09-07 17:14:14 +02003814 if( alg != PSA_ALG_ECB_NO_PADDING )
3815 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003816 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3817 iv, iv_size,
3818 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003819 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003820 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003821 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003822 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003823
Gilles Peskine8817f612018-12-18 00:18:46 +01003824 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3825 output1, output1_size,
3826 &output1_length ) );
3827 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003828 output1 + output1_length,
3829 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003830 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003831
Gilles Peskine048b7f02018-06-08 14:20:49 +02003832 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003833
Gilles Peskine8817f612018-12-18 00:18:46 +01003834 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003835
3836 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003837 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003838
Steven Cooreman177deba2020-09-07 17:14:14 +02003839 if( iv_length > 0 )
3840 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003841 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3842 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003843 }
3844
Gilles Peskine8817f612018-12-18 00:18:46 +01003845 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3846 output2, output2_size,
3847 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003848 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003849 PSA_ASSERT( psa_cipher_finish( &operation2,
3850 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003851 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003852 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003853
Gilles Peskine048b7f02018-06-08 14:20:49 +02003854 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003855
Gilles Peskine8817f612018-12-18 00:18:46 +01003856 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003857
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003858 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003859
3860exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003861 psa_cipher_abort( &operation1 );
3862 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003863 mbedtls_free( output1 );
3864 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003865 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003866 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003867}
3868/* END_CASE */
3869
3870/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003871void cipher_verify_output_multipart( int alg_arg,
3872 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003873 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003874 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003875 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003876{
Ronald Cron5425a212020-08-04 14:58:35 +02003877 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003878 psa_key_type_t key_type = key_type_arg;
3879 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003880 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003881 unsigned char iv[16] = {0};
3882 size_t iv_size = 16;
3883 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003884 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003885 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003886 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003887 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003888 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003889 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003890 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003891 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3892 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003894
Gilles Peskine8817f612018-12-18 00:18:46 +01003895 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003896
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003897 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3898 psa_set_key_algorithm( &attributes, alg );
3899 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003900
Ronald Cron5425a212020-08-04 14:58:35 +02003901 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3902 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003903
Ronald Cron5425a212020-08-04 14:58:35 +02003904 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3905 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003906
Steven Cooreman177deba2020-09-07 17:14:14 +02003907 if( alg != PSA_ALG_ECB_NO_PADDING )
3908 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003909 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3910 iv, iv_size,
3911 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003912 }
3913
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003914 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003915 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003916 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003917
Gilles Peskinee0866522019-02-19 19:44:00 +01003918 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003919
Gilles Peskine8817f612018-12-18 00:18:46 +01003920 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3921 output1, output1_buffer_size,
3922 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003923 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003924
Gilles Peskine8817f612018-12-18 00:18:46 +01003925 PSA_ASSERT( psa_cipher_update( &operation1,
3926 input->x + first_part_size,
3927 input->len - first_part_size,
3928 output1, output1_buffer_size,
3929 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003930 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003931
Gilles Peskine8817f612018-12-18 00:18:46 +01003932 PSA_ASSERT( psa_cipher_finish( &operation1,
3933 output1 + output1_length,
3934 output1_buffer_size - output1_length,
3935 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003936 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003937
Gilles Peskine8817f612018-12-18 00:18:46 +01003938 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003939
Gilles Peskine048b7f02018-06-08 14:20:49 +02003940 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003941 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003942
Steven Cooreman177deba2020-09-07 17:14:14 +02003943 if( iv_length > 0 )
3944 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003945 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3946 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003947 }
Moran Pekerded84402018-06-06 16:36:50 +03003948
Gilles Peskine8817f612018-12-18 00:18:46 +01003949 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3950 output2, output2_buffer_size,
3951 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003952 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003953
Gilles Peskine8817f612018-12-18 00:18:46 +01003954 PSA_ASSERT( psa_cipher_update( &operation2,
3955 output1 + first_part_size,
3956 output1_length - first_part_size,
3957 output2, output2_buffer_size,
3958 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003959 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003960
Gilles Peskine8817f612018-12-18 00:18:46 +01003961 PSA_ASSERT( psa_cipher_finish( &operation2,
3962 output2 + output2_length,
3963 output2_buffer_size - output2_length,
3964 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003965 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003966
Gilles Peskine8817f612018-12-18 00:18:46 +01003967 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003968
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003969 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003970
3971exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003972 psa_cipher_abort( &operation1 );
3973 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003974 mbedtls_free( output1 );
3975 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003976 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003977 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003978}
3979/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003980
Gilles Peskine20035e32018-02-03 22:44:14 +01003981/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003982void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003983 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003984 data_t *nonce,
3985 data_t *additional_data,
3986 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003987 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003988{
Ronald Cron5425a212020-08-04 14:58:35 +02003989 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003990 psa_key_type_t key_type = key_type_arg;
3991 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003992 unsigned char *output_data = NULL;
3993 size_t output_size = 0;
3994 size_t output_length = 0;
3995 unsigned char *output_data2 = NULL;
3996 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003997 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003998 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004000
Gilles Peskine4abf7412018-06-18 16:35:34 +02004001 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004002 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4003 * should be exact. */
4004 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4005 TEST_EQUAL( output_size,
4006 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004007 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004008
Gilles Peskine8817f612018-12-18 00:18:46 +01004009 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004010
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004011 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4012 psa_set_key_algorithm( &attributes, alg );
4013 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004014
Gilles Peskine049c7532019-05-15 20:22:09 +02004015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004016 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004017
Ronald Cron5425a212020-08-04 14:58:35 +02004018 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004019 nonce->x, nonce->len,
4020 additional_data->x,
4021 additional_data->len,
4022 input_data->x, input_data->len,
4023 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004024 &output_length ),
4025 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004026
4027 if( PSA_SUCCESS == expected_result )
4028 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004029 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004030
Gilles Peskine003a4a92019-05-14 16:09:40 +02004031 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4032 * should be exact. */
4033 TEST_EQUAL( input_data->len,
4034 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
4035
Ronald Cron5425a212020-08-04 14:58:35 +02004036 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004037 nonce->x, nonce->len,
4038 additional_data->x,
4039 additional_data->len,
4040 output_data, output_length,
4041 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004042 &output_length2 ),
4043 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004044
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004045 ASSERT_COMPARE( input_data->x, input_data->len,
4046 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004047 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004048
Gilles Peskinea1cac842018-06-11 19:33:02 +02004049exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004050 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004051 mbedtls_free( output_data );
4052 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004053 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004054}
4055/* END_CASE */
4056
4057/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004058void aead_encrypt( int key_type_arg, data_t *key_data,
4059 int alg_arg,
4060 data_t *nonce,
4061 data_t *additional_data,
4062 data_t *input_data,
4063 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004064{
Ronald Cron5425a212020-08-04 14:58:35 +02004065 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004066 psa_key_type_t key_type = key_type_arg;
4067 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004068 unsigned char *output_data = NULL;
4069 size_t output_size = 0;
4070 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004071 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004072 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004073 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004074
Gilles Peskine4abf7412018-06-18 16:35:34 +02004075 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004076 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4077 * should be exact. */
4078 TEST_EQUAL( output_size,
4079 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004080 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004081
Gilles Peskine8817f612018-12-18 00:18:46 +01004082 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004083
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004084 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4085 psa_set_key_algorithm( &attributes, alg );
4086 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004087
Gilles Peskine049c7532019-05-15 20:22:09 +02004088 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004089 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004090
Steven Cooremand588ea12021-01-11 19:36:04 +01004091 status = psa_aead_encrypt( key, alg,
4092 nonce->x, nonce->len,
4093 additional_data->x, additional_data->len,
4094 input_data->x, input_data->len,
4095 output_data, output_size,
4096 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004097
Ronald Cron28a45ed2021-02-09 20:35:42 +01004098 /* If the operation is not supported, just skip and not fail in case the
4099 * encryption involves a common limitation of cryptography hardwares and
4100 * an alternative implementation. */
4101 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004102 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004103 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4104 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004105 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004106
4107 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004108 ASSERT_COMPARE( expected_result->x, expected_result->len,
4109 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004110
Gilles Peskinea1cac842018-06-11 19:33:02 +02004111exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004112 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004113 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004114 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004115}
4116/* END_CASE */
4117
4118/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004119void aead_decrypt( int key_type_arg, data_t *key_data,
4120 int alg_arg,
4121 data_t *nonce,
4122 data_t *additional_data,
4123 data_t *input_data,
4124 data_t *expected_data,
4125 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004126{
Ronald Cron5425a212020-08-04 14:58:35 +02004127 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004128 psa_key_type_t key_type = key_type_arg;
4129 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004130 unsigned char *output_data = NULL;
4131 size_t output_size = 0;
4132 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004133 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004135 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004136 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004137
Gilles Peskine003a4a92019-05-14 16:09:40 +02004138 output_size = input_data->len - tag_length;
4139 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4140 * should be exact. */
4141 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4142 TEST_EQUAL( output_size,
4143 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004144 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004145
Gilles Peskine8817f612018-12-18 00:18:46 +01004146 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004147
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004148 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4149 psa_set_key_algorithm( &attributes, alg );
4150 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004151
Gilles Peskine049c7532019-05-15 20:22:09 +02004152 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004153 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004154
Steven Cooremand588ea12021-01-11 19:36:04 +01004155 status = psa_aead_decrypt( key, alg,
4156 nonce->x, nonce->len,
4157 additional_data->x,
4158 additional_data->len,
4159 input_data->x, input_data->len,
4160 output_data, output_size,
4161 &output_length );
4162
Ronald Cron28a45ed2021-02-09 20:35:42 +01004163 /* If the operation is not supported, just skip and not fail in case the
4164 * decryption involves a common limitation of cryptography hardwares and
4165 * an alternative implementation. */
4166 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004167 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004168 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4169 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004170 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004171
4172 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004173
Gilles Peskine2d277862018-06-18 15:41:12 +02004174 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004175 ASSERT_COMPARE( expected_data->x, expected_data->len,
4176 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004177
Gilles Peskinea1cac842018-06-11 19:33:02 +02004178exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004179 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004180 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004181 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004182}
4183/* END_CASE */
4184
4185/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004186void signature_size( int type_arg,
4187 int bits,
4188 int alg_arg,
4189 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004190{
4191 psa_key_type_t type = type_arg;
4192 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004193 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004194
Gilles Peskinefe11b722018-12-18 00:24:04 +01004195 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004196#if defined(MBEDTLS_TEST_DEPRECATED)
4197 TEST_EQUAL( actual_size,
4198 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4199#endif /* MBEDTLS_TEST_DEPRECATED */
4200
Gilles Peskinee59236f2018-01-27 23:32:46 +01004201exit:
4202 ;
4203}
4204/* END_CASE */
4205
4206/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004207void sign_deterministic( int key_type_arg, data_t *key_data,
4208 int alg_arg, data_t *input_data,
4209 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004210{
Ronald Cron5425a212020-08-04 14:58:35 +02004211 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004212 psa_key_type_t key_type = key_type_arg;
4213 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004214 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004215 unsigned char *signature = NULL;
4216 size_t signature_size;
4217 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004218 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004219
Gilles Peskine8817f612018-12-18 00:18:46 +01004220 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004221
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004222 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004223 psa_set_key_algorithm( &attributes, alg );
4224 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004225
Gilles Peskine049c7532019-05-15 20:22:09 +02004226 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004227 &key ) );
4228 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004229 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004230
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004231 /* Allocate a buffer which has the size advertized by the
4232 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004233 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004234 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004235 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004236 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004237 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004238
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004239 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004240 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004241 input_data->x, input_data->len,
4242 signature, signature_size,
4243 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004244 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004245 ASSERT_COMPARE( output_data->x, output_data->len,
4246 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004247
Gilles Peskine0627f982019-11-26 19:12:16 +01004248#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004249 memset( signature, 0, signature_size );
4250 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004251 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004252 input_data->x, input_data->len,
4253 signature, signature_size,
4254 &signature_length ) );
4255 ASSERT_COMPARE( output_data->x, output_data->len,
4256 signature, signature_length );
4257#endif /* MBEDTLS_TEST_DEPRECATED */
4258
Gilles Peskine20035e32018-02-03 22:44:14 +01004259exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004260 /*
4261 * Key attributes may have been returned by psa_get_key_attributes()
4262 * thus reset them as required.
4263 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004264 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004265
Ronald Cron5425a212020-08-04 14:58:35 +02004266 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004267 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004268 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004269}
4270/* END_CASE */
4271
4272/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004273void sign_fail( int key_type_arg, data_t *key_data,
4274 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004275 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004276{
Ronald Cron5425a212020-08-04 14:58:35 +02004277 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004278 psa_key_type_t key_type = key_type_arg;
4279 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004280 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004281 psa_status_t actual_status;
4282 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004283 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004284 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004286
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004287 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004288
Gilles Peskine8817f612018-12-18 00:18:46 +01004289 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004290
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004291 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004292 psa_set_key_algorithm( &attributes, alg );
4293 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004294
Gilles Peskine049c7532019-05-15 20:22:09 +02004295 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004296 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004297
Ronald Cron5425a212020-08-04 14:58:35 +02004298 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004299 input_data->x, input_data->len,
4300 signature, signature_size,
4301 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004302 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004303 /* The value of *signature_length is unspecified on error, but
4304 * whatever it is, it should be less than signature_size, so that
4305 * if the caller tries to read *signature_length bytes without
4306 * checking the error code then they don't overflow a buffer. */
4307 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004308
Gilles Peskine895242b2019-11-29 12:15:40 +01004309#if defined(MBEDTLS_TEST_DEPRECATED)
4310 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004311 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004312 input_data->x, input_data->len,
4313 signature, signature_size,
4314 &signature_length ),
4315 expected_status );
4316 TEST_ASSERT( signature_length <= signature_size );
4317#endif /* MBEDTLS_TEST_DEPRECATED */
4318
Gilles Peskine20035e32018-02-03 22:44:14 +01004319exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004320 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004321 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004322 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004323 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004324}
4325/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004326
4327/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004328void sign_verify( int key_type_arg, data_t *key_data,
4329 int alg_arg, data_t *input_data )
4330{
Ronald Cron5425a212020-08-04 14:58:35 +02004331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004332 psa_key_type_t key_type = key_type_arg;
4333 psa_algorithm_t alg = alg_arg;
4334 size_t key_bits;
4335 unsigned char *signature = NULL;
4336 size_t signature_size;
4337 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004339
Gilles Peskine8817f612018-12-18 00:18:46 +01004340 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004341
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004343 psa_set_key_algorithm( &attributes, alg );
4344 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004345
Gilles Peskine049c7532019-05-15 20:22:09 +02004346 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004347 &key ) );
4348 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004349 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004350
4351 /* Allocate a buffer which has the size advertized by the
4352 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004353 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004354 key_bits, alg );
4355 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004356 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004357 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004358
4359 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004360 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004361 input_data->x, input_data->len,
4362 signature, signature_size,
4363 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004364 /* Check that the signature length looks sensible. */
4365 TEST_ASSERT( signature_length <= signature_size );
4366 TEST_ASSERT( signature_length > 0 );
4367
4368 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004369 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004370 input_data->x, input_data->len,
4371 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004372
4373 if( input_data->len != 0 )
4374 {
4375 /* Flip a bit in the input and verify that the signature is now
4376 * detected as invalid. Flip a bit at the beginning, not at the end,
4377 * because ECDSA may ignore the last few bits of the input. */
4378 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004379 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004380 input_data->x, input_data->len,
4381 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004382 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004383 }
4384
4385exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004386 /*
4387 * Key attributes may have been returned by psa_get_key_attributes()
4388 * thus reset them as required.
4389 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004390 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004391
Ronald Cron5425a212020-08-04 14:58:35 +02004392 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004393 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004394 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004395}
4396/* END_CASE */
4397
4398/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004399void asymmetric_verify( int key_type_arg, data_t *key_data,
4400 int alg_arg, data_t *hash_data,
4401 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004402{
Ronald Cron5425a212020-08-04 14:58:35 +02004403 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004404 psa_key_type_t key_type = key_type_arg;
4405 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004407
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004408 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004409
Gilles Peskine8817f612018-12-18 00:18:46 +01004410 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004411
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004413 psa_set_key_algorithm( &attributes, alg );
4414 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004415
Gilles Peskine049c7532019-05-15 20:22:09 +02004416 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004417 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004418
Ronald Cron5425a212020-08-04 14:58:35 +02004419 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004420 hash_data->x, hash_data->len,
4421 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004422
4423#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004424 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004425 hash_data->x, hash_data->len,
4426 signature_data->x,
4427 signature_data->len ) );
4428
4429#endif /* MBEDTLS_TEST_DEPRECATED */
4430
itayzafrir5c753392018-05-08 11:18:38 +03004431exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004432 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004433 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004434 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004435}
4436/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004437
4438/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004439void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4440 int alg_arg, data_t *hash_data,
4441 data_t *signature_data,
4442 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004443{
Ronald Cron5425a212020-08-04 14:58:35 +02004444 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004445 psa_key_type_t key_type = key_type_arg;
4446 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004447 psa_status_t actual_status;
4448 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004450
Gilles Peskine8817f612018-12-18 00:18:46 +01004451 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004452
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004453 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004454 psa_set_key_algorithm( &attributes, alg );
4455 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004456
Gilles Peskine049c7532019-05-15 20:22:09 +02004457 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004458 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004459
Ronald Cron5425a212020-08-04 14:58:35 +02004460 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004461 hash_data->x, hash_data->len,
4462 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004463 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004464
Gilles Peskine895242b2019-11-29 12:15:40 +01004465#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004466 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004467 hash_data->x, hash_data->len,
4468 signature_data->x, signature_data->len ),
4469 expected_status );
4470#endif /* MBEDTLS_TEST_DEPRECATED */
4471
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004472exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004473 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004474 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004475 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004476}
4477/* END_CASE */
4478
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004479/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004480void asymmetric_encrypt( int key_type_arg,
4481 data_t *key_data,
4482 int alg_arg,
4483 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004484 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004485 int expected_output_length_arg,
4486 int expected_status_arg )
4487{
Ronald Cron5425a212020-08-04 14:58:35 +02004488 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004489 psa_key_type_t key_type = key_type_arg;
4490 psa_algorithm_t alg = alg_arg;
4491 size_t expected_output_length = expected_output_length_arg;
4492 size_t key_bits;
4493 unsigned char *output = NULL;
4494 size_t output_size;
4495 size_t output_length = ~0;
4496 psa_status_t actual_status;
4497 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004499
Gilles Peskine8817f612018-12-18 00:18:46 +01004500 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004501
Gilles Peskine656896e2018-06-29 19:12:28 +02004502 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004503 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4504 psa_set_key_algorithm( &attributes, alg );
4505 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004506 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004507 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004508
4509 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004510 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004511 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004512 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004513 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004514
4515 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004516 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004517 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004518 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004519 output, output_size,
4520 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004521 TEST_EQUAL( actual_status, expected_status );
4522 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004523
Gilles Peskine68428122018-06-30 18:42:41 +02004524 /* If the label is empty, the test framework puts a non-null pointer
4525 * in label->x. Test that a null pointer works as well. */
4526 if( label->len == 0 )
4527 {
4528 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004529 if( output_size != 0 )
4530 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004531 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004532 input_data->x, input_data->len,
4533 NULL, label->len,
4534 output, output_size,
4535 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004536 TEST_EQUAL( actual_status, expected_status );
4537 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004538 }
4539
Gilles Peskine656896e2018-06-29 19:12:28 +02004540exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004541 /*
4542 * Key attributes may have been returned by psa_get_key_attributes()
4543 * thus reset them as required.
4544 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004545 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004546
Ronald Cron5425a212020-08-04 14:58:35 +02004547 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004548 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004549 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004550}
4551/* END_CASE */
4552
4553/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004554void asymmetric_encrypt_decrypt( int key_type_arg,
4555 data_t *key_data,
4556 int alg_arg,
4557 data_t *input_data,
4558 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004559{
Ronald Cron5425a212020-08-04 14:58:35 +02004560 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004561 psa_key_type_t key_type = key_type_arg;
4562 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004563 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004564 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004565 size_t output_size;
4566 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004567 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004568 size_t output2_size;
4569 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004571
Gilles Peskine8817f612018-12-18 00:18:46 +01004572 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004573
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004574 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4575 psa_set_key_algorithm( &attributes, alg );
4576 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004577
Gilles Peskine049c7532019-05-15 20:22:09 +02004578 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004579 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004580
4581 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004582 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004583 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004584 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004585 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004586 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004587 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004588
Gilles Peskineeebd7382018-06-08 18:11:54 +02004589 /* We test encryption by checking that encrypt-then-decrypt gives back
4590 * the original plaintext because of the non-optional random
4591 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004592 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004593 input_data->x, input_data->len,
4594 label->x, label->len,
4595 output, output_size,
4596 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004597 /* We don't know what ciphertext length to expect, but check that
4598 * it looks sensible. */
4599 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004600
Ronald Cron5425a212020-08-04 14:58:35 +02004601 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004602 output, output_length,
4603 label->x, label->len,
4604 output2, output2_size,
4605 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004606 ASSERT_COMPARE( input_data->x, input_data->len,
4607 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004608
4609exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004610 /*
4611 * Key attributes may have been returned by psa_get_key_attributes()
4612 * thus reset them as required.
4613 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004614 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004615
Ronald Cron5425a212020-08-04 14:58:35 +02004616 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004617 mbedtls_free( output );
4618 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004619 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004620}
4621/* END_CASE */
4622
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004623/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004624void asymmetric_decrypt( int key_type_arg,
4625 data_t *key_data,
4626 int alg_arg,
4627 data_t *input_data,
4628 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004629 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004630{
Ronald Cron5425a212020-08-04 14:58:35 +02004631 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004632 psa_key_type_t key_type = key_type_arg;
4633 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004634 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004635 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004636 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004637 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004638
Jaeden Amero412654a2019-02-06 12:57:46 +00004639 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004640 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004641
Gilles Peskine8817f612018-12-18 00:18:46 +01004642 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004643
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004644 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4645 psa_set_key_algorithm( &attributes, alg );
4646 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004647
Gilles Peskine049c7532019-05-15 20:22:09 +02004648 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004649 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004650
Ronald Cron5425a212020-08-04 14:58:35 +02004651 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004652 input_data->x, input_data->len,
4653 label->x, label->len,
4654 output,
4655 output_size,
4656 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004657 ASSERT_COMPARE( expected_data->x, expected_data->len,
4658 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004659
Gilles Peskine68428122018-06-30 18:42:41 +02004660 /* If the label is empty, the test framework puts a non-null pointer
4661 * in label->x. Test that a null pointer works as well. */
4662 if( label->len == 0 )
4663 {
4664 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004665 if( output_size != 0 )
4666 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004667 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004668 input_data->x, input_data->len,
4669 NULL, label->len,
4670 output,
4671 output_size,
4672 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004673 ASSERT_COMPARE( expected_data->x, expected_data->len,
4674 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004675 }
4676
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004677exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004678 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004679 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004680 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004681 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004682}
4683/* END_CASE */
4684
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004685/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004686void asymmetric_decrypt_fail( int key_type_arg,
4687 data_t *key_data,
4688 int alg_arg,
4689 data_t *input_data,
4690 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004691 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004692 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004693{
Ronald Cron5425a212020-08-04 14:58:35 +02004694 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004695 psa_key_type_t key_type = key_type_arg;
4696 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004697 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004698 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004699 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004700 psa_status_t actual_status;
4701 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004703
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004704 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004705
Gilles Peskine8817f612018-12-18 00:18:46 +01004706 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004707
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004708 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4709 psa_set_key_algorithm( &attributes, alg );
4710 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004711
Gilles Peskine049c7532019-05-15 20:22:09 +02004712 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004713 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004714
Ronald Cron5425a212020-08-04 14:58:35 +02004715 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004716 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004717 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004718 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004719 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004720 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004721 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004722
Gilles Peskine68428122018-06-30 18:42:41 +02004723 /* If the label is empty, the test framework puts a non-null pointer
4724 * in label->x. Test that a null pointer works as well. */
4725 if( label->len == 0 )
4726 {
4727 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004728 if( output_size != 0 )
4729 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004730 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004731 input_data->x, input_data->len,
4732 NULL, label->len,
4733 output, output_size,
4734 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004735 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004736 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004737 }
4738
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004739exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004740 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004741 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004742 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004743 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004744}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004745/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004746
4747/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004748void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004749{
4750 /* Test each valid way of initializing the object, except for `= {0}`, as
4751 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4752 * though it's OK by the C standard. We could test for this, but we'd need
4753 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004754 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004755 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4756 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4757 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004758
4759 memset( &zero, 0, sizeof( zero ) );
4760
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004761 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004762 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004763 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004764 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004765 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004766 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004767 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004768
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004769 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004770 PSA_ASSERT( psa_key_derivation_abort(&func) );
4771 PSA_ASSERT( psa_key_derivation_abort(&init) );
4772 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004773}
4774/* END_CASE */
4775
Janos Follath16de4a42019-06-13 16:32:24 +01004776/* BEGIN_CASE */
4777void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004778{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004779 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004780 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004781 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004782
Gilles Peskine8817f612018-12-18 00:18:46 +01004783 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004784
Janos Follath16de4a42019-06-13 16:32:24 +01004785 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004786 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004787
4788exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004790 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004791}
4792/* END_CASE */
4793
Janos Follathaf3c2a02019-06-12 12:34:34 +01004794/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004795void derive_set_capacity( int alg_arg, int capacity_arg,
4796 int expected_status_arg )
4797{
4798 psa_algorithm_t alg = alg_arg;
4799 size_t capacity = capacity_arg;
4800 psa_status_t expected_status = expected_status_arg;
4801 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4802
4803 PSA_ASSERT( psa_crypto_init( ) );
4804
4805 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4806
4807 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4808 expected_status );
4809
4810exit:
4811 psa_key_derivation_abort( &operation );
4812 PSA_DONE( );
4813}
4814/* END_CASE */
4815
4816/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004817void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004818 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004819 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004820 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004821 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004822 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004823 int expected_status_arg3,
4824 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004825{
4826 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004827 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4828 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004829 psa_status_t expected_statuses[] = {expected_status_arg1,
4830 expected_status_arg2,
4831 expected_status_arg3};
4832 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004833 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4834 MBEDTLS_SVC_KEY_ID_INIT,
4835 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004836 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4838 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004839 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004840 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004841 psa_status_t expected_output_status = expected_output_status_arg;
4842 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004843
4844 PSA_ASSERT( psa_crypto_init( ) );
4845
4846 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4847 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004848
4849 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4850
4851 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4852 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004853 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004854 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004855 psa_set_key_type( &attributes, key_types[i] );
4856 PSA_ASSERT( psa_import_key( &attributes,
4857 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004858 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004859 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4860 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4861 {
4862 // When taking a private key as secret input, use key agreement
4863 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004864 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004865 expected_statuses[i] );
4866 }
4867 else
4868 {
4869 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004870 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004871 expected_statuses[i] );
4872 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004873 }
4874 else
4875 {
4876 TEST_EQUAL( psa_key_derivation_input_bytes(
4877 &operation, steps[i],
4878 inputs[i]->x, inputs[i]->len ),
4879 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004880 }
4881 }
4882
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004883 if( output_key_type != PSA_KEY_TYPE_NONE )
4884 {
4885 psa_reset_key_attributes( &attributes );
4886 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4887 psa_set_key_bits( &attributes, 8 );
4888 actual_output_status =
4889 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004890 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004891 }
4892 else
4893 {
4894 uint8_t buffer[1];
4895 actual_output_status =
4896 psa_key_derivation_output_bytes( &operation,
4897 buffer, sizeof( buffer ) );
4898 }
4899 TEST_EQUAL( actual_output_status, expected_output_status );
4900
Janos Follathaf3c2a02019-06-12 12:34:34 +01004901exit:
4902 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004903 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4904 psa_destroy_key( keys[i] );
4905 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004906 PSA_DONE( );
4907}
4908/* END_CASE */
4909
Janos Follathd958bb72019-07-03 15:02:16 +01004910/* BEGIN_CASE */
4911void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004912{
Janos Follathd958bb72019-07-03 15:02:16 +01004913 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004915 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004917 unsigned char input1[] = "Input 1";
4918 size_t input1_length = sizeof( input1 );
4919 unsigned char input2[] = "Input 2";
4920 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004921 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004922 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004923 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4924 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4925 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004927
Gilles Peskine8817f612018-12-18 00:18:46 +01004928 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004929
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004930 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4931 psa_set_key_algorithm( &attributes, alg );
4932 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004933
Gilles Peskine73676cb2019-05-15 20:15:10 +02004934 PSA_ASSERT( psa_import_key( &attributes,
4935 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004936 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004937
4938 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004939 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004940 input1, input1_length,
4941 input2, input2_length,
4942 capacity ) )
4943 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004944
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004946 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004947 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004948
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004950
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004951 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004952 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004953
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004954exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004956 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004957 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004958}
4959/* END_CASE */
4960
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004961/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004962void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004963{
4964 uint8_t output_buffer[16];
4965 size_t buffer_size = 16;
4966 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004968
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004969 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4970 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004971 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004972
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004974 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004975
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004977
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004978 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4979 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004980 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004981
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004983 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004984
4985exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004987}
4988/* END_CASE */
4989
4990/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004991void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004992 int step1_arg, data_t *input1,
4993 int step2_arg, data_t *input2,
4994 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004995 int requested_capacity_arg,
4996 data_t *expected_output1,
4997 data_t *expected_output2 )
4998{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004999 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005000 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5001 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005002 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5003 MBEDTLS_SVC_KEY_ID_INIT,
5004 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005005 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005006 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005007 uint8_t *expected_outputs[2] =
5008 {expected_output1->x, expected_output2->x};
5009 size_t output_sizes[2] =
5010 {expected_output1->len, expected_output2->len};
5011 size_t output_buffer_size = 0;
5012 uint8_t *output_buffer = NULL;
5013 size_t expected_capacity;
5014 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005016 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005017 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005018
5019 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5020 {
5021 if( output_sizes[i] > output_buffer_size )
5022 output_buffer_size = output_sizes[i];
5023 if( output_sizes[i] == 0 )
5024 expected_outputs[i] = NULL;
5025 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005026 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005027 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005028
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005029 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5030 psa_set_key_algorithm( &attributes, alg );
5031 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005032
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005033 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005034 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5035 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5036 requested_capacity ) );
5037 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005038 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005039 switch( steps[i] )
5040 {
5041 case 0:
5042 break;
5043 case PSA_KEY_DERIVATION_INPUT_SECRET:
5044 PSA_ASSERT( psa_import_key( &attributes,
5045 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005046 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005047 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005048 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005049 break;
5050 default:
5051 PSA_ASSERT( psa_key_derivation_input_bytes(
5052 &operation, steps[i],
5053 inputs[i]->x, inputs[i]->len ) );
5054 break;
5055 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005056 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005057
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005058 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005059 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005060 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005061 expected_capacity = requested_capacity;
5062
5063 /* Expansion phase. */
5064 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5065 {
5066 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005067 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005068 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005069 if( expected_capacity == 0 && output_sizes[i] == 0 )
5070 {
5071 /* Reading 0 bytes when 0 bytes are available can go either way. */
5072 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005073 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005074 continue;
5075 }
5076 else if( expected_capacity == 0 ||
5077 output_sizes[i] > expected_capacity )
5078 {
5079 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005080 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005081 expected_capacity = 0;
5082 continue;
5083 }
5084 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005085 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005086 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005087 ASSERT_COMPARE( output_buffer, output_sizes[i],
5088 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005089 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005090 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005091 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005092 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005093 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005094 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005095 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005096
5097exit:
5098 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005100 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5101 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005102 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005103}
5104/* END_CASE */
5105
5106/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005107void derive_full( int alg_arg,
5108 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005109 data_t *input1,
5110 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005111 int requested_capacity_arg )
5112{
Ronald Cron5425a212020-08-04 14:58:35 +02005113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005114 psa_algorithm_t alg = alg_arg;
5115 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005117 unsigned char output_buffer[16];
5118 size_t expected_capacity = requested_capacity;
5119 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005121
Gilles Peskine8817f612018-12-18 00:18:46 +01005122 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005123
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005124 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5125 psa_set_key_algorithm( &attributes, alg );
5126 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005127
Gilles Peskine049c7532019-05-15 20:22:09 +02005128 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005129 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005130
Ronald Cron5425a212020-08-04 14:58:35 +02005131 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005132 input1->x, input1->len,
5133 input2->x, input2->len,
5134 requested_capacity ) )
5135 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005136
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005137 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005138 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005139 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005140
5141 /* Expansion phase. */
5142 while( current_capacity > 0 )
5143 {
5144 size_t read_size = sizeof( output_buffer );
5145 if( read_size > current_capacity )
5146 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005147 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005148 output_buffer,
5149 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005150 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005152 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005153 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005154 }
5155
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005156 /* Check that the operation refuses to go over capacity. */
5157 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005158 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005159
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005160 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005161
5162exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005163 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005164 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005165 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005166}
5167/* END_CASE */
5168
Janos Follathe60c9052019-07-03 13:51:30 +01005169/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005170void derive_key_exercise( int alg_arg,
5171 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005172 data_t *input1,
5173 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005174 int derived_type_arg,
5175 int derived_bits_arg,
5176 int derived_usage_arg,
5177 int derived_alg_arg )
5178{
Ronald Cron5425a212020-08-04 14:58:35 +02005179 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5180 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005181 psa_algorithm_t alg = alg_arg;
5182 psa_key_type_t derived_type = derived_type_arg;
5183 size_t derived_bits = derived_bits_arg;
5184 psa_key_usage_t derived_usage = derived_usage_arg;
5185 psa_algorithm_t derived_alg = derived_alg_arg;
5186 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005187 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005189 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005190
Gilles Peskine8817f612018-12-18 00:18:46 +01005191 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005192
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005193 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5194 psa_set_key_algorithm( &attributes, alg );
5195 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005196 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005197 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005198
5199 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005200 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005201 input1->x, input1->len,
5202 input2->x, input2->len, capacity ) )
5203 goto exit;
5204
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005205 psa_set_key_usage_flags( &attributes, derived_usage );
5206 psa_set_key_algorithm( &attributes, derived_alg );
5207 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005208 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005210 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005211
5212 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005213 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005214 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5215 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005216
5217 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005218 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005219 goto exit;
5220
5221exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005222 /*
5223 * Key attributes may have been returned by psa_get_key_attributes()
5224 * thus reset them as required.
5225 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005226 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005227
5228 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005229 psa_destroy_key( base_key );
5230 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005231 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005232}
5233/* END_CASE */
5234
Janos Follath42fd8882019-07-03 14:17:09 +01005235/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005236void derive_key_export( int alg_arg,
5237 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005238 data_t *input1,
5239 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005240 int bytes1_arg,
5241 int bytes2_arg )
5242{
Ronald Cron5425a212020-08-04 14:58:35 +02005243 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5244 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005245 psa_algorithm_t alg = alg_arg;
5246 size_t bytes1 = bytes1_arg;
5247 size_t bytes2 = bytes2_arg;
5248 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005249 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005250 uint8_t *output_buffer = NULL;
5251 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005252 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5253 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005254 size_t length;
5255
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005256 ASSERT_ALLOC( output_buffer, capacity );
5257 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005259
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005260 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5261 psa_set_key_algorithm( &base_attributes, alg );
5262 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005263 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005264 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005265
5266 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005267 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005268 input1->x, input1->len,
5269 input2->x, input2->len, capacity ) )
5270 goto exit;
5271
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005273 output_buffer,
5274 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005276
5277 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005278 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005279 input1->x, input1->len,
5280 input2->x, input2->len, capacity ) )
5281 goto exit;
5282
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005283 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5284 psa_set_key_algorithm( &derived_attributes, 0 );
5285 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005286 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005288 &derived_key ) );
5289 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005290 export_buffer, bytes1,
5291 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005292 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005293 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005294 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005295 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005296 &derived_key ) );
5297 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005298 export_buffer + bytes1, bytes2,
5299 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005300 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005301
5302 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005303 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5304 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005305
5306exit:
5307 mbedtls_free( output_buffer );
5308 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005310 psa_destroy_key( base_key );
5311 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005312 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005313}
5314/* END_CASE */
5315
5316/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005317void derive_key( int alg_arg,
5318 data_t *key_data, data_t *input1, data_t *input2,
5319 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005320 int expected_status_arg,
5321 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005322{
Ronald Cron5425a212020-08-04 14:58:35 +02005323 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5324 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005325 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005326 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005327 size_t bits = bits_arg;
5328 psa_status_t expected_status = expected_status_arg;
5329 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5330 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5331 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5332
5333 PSA_ASSERT( psa_crypto_init( ) );
5334
5335 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5336 psa_set_key_algorithm( &base_attributes, alg );
5337 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5338 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005339 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005340
Ronald Cron5425a212020-08-04 14:58:35 +02005341 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005342 input1->x, input1->len,
5343 input2->x, input2->len, SIZE_MAX ) )
5344 goto exit;
5345
5346 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5347 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005348 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005349 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005350
5351 psa_status_t status =
5352 psa_key_derivation_output_key( &derived_attributes,
5353 &operation,
5354 &derived_key );
5355 if( is_large_output > 0 )
5356 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5357 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005358
5359exit:
5360 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005361 psa_destroy_key( base_key );
5362 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005363 PSA_DONE( );
5364}
5365/* END_CASE */
5366
5367/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005368void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005369 int our_key_type_arg, int our_key_alg_arg,
5370 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005371 int expected_status_arg )
5372{
Ronald Cron5425a212020-08-04 14:58:35 +02005373 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005374 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005375 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005376 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005377 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005379 psa_status_t expected_status = expected_status_arg;
5380 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005381
Gilles Peskine8817f612018-12-18 00:18:46 +01005382 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005383
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005384 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005385 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005386 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005387 PSA_ASSERT( psa_import_key( &attributes,
5388 our_key_data->x, our_key_data->len,
5389 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005390
Gilles Peskine77f40d82019-04-11 21:27:06 +02005391 /* The tests currently include inputs that should fail at either step.
5392 * Test cases that fail at the setup step should be changed to call
5393 * key_derivation_setup instead, and this function should be renamed
5394 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005395 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005396 if( status == PSA_SUCCESS )
5397 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005398 TEST_EQUAL( psa_key_derivation_key_agreement(
5399 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5400 our_key,
5401 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005402 expected_status );
5403 }
5404 else
5405 {
5406 TEST_ASSERT( status == expected_status );
5407 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005408
5409exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005410 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005411 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005412 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005413}
5414/* END_CASE */
5415
5416/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005417void raw_key_agreement( int alg_arg,
5418 int our_key_type_arg, data_t *our_key_data,
5419 data_t *peer_key_data,
5420 data_t *expected_output )
5421{
Ronald Cron5425a212020-08-04 14:58:35 +02005422 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005423 psa_algorithm_t alg = alg_arg;
5424 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005425 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005426 unsigned char *output = NULL;
5427 size_t output_length = ~0;
5428
5429 ASSERT_ALLOC( output, expected_output->len );
5430 PSA_ASSERT( psa_crypto_init( ) );
5431
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005432 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5433 psa_set_key_algorithm( &attributes, alg );
5434 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005435 PSA_ASSERT( psa_import_key( &attributes,
5436 our_key_data->x, our_key_data->len,
5437 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005438
Gilles Peskinebe697d82019-05-16 18:00:41 +02005439 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5440 peer_key_data->x, peer_key_data->len,
5441 output, expected_output->len,
5442 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005443 ASSERT_COMPARE( output, output_length,
5444 expected_output->x, expected_output->len );
5445
5446exit:
5447 mbedtls_free( output );
5448 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005449 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005450}
5451/* END_CASE */
5452
5453/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005454void key_agreement_capacity( int alg_arg,
5455 int our_key_type_arg, data_t *our_key_data,
5456 data_t *peer_key_data,
5457 int expected_capacity_arg )
5458{
Ronald Cron5425a212020-08-04 14:58:35 +02005459 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005460 psa_algorithm_t alg = alg_arg;
5461 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005462 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005463 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005464 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005465 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005466
Gilles Peskine8817f612018-12-18 00:18:46 +01005467 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005468
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005469 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5470 psa_set_key_algorithm( &attributes, alg );
5471 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005472 PSA_ASSERT( psa_import_key( &attributes,
5473 our_key_data->x, our_key_data->len,
5474 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005475
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005476 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005477 PSA_ASSERT( psa_key_derivation_key_agreement(
5478 &operation,
5479 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5480 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005481 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5482 {
5483 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005484 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005485 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005486 NULL, 0 ) );
5487 }
Gilles Peskine59685592018-09-18 12:11:34 +02005488
Gilles Peskinebf491972018-10-25 22:36:12 +02005489 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005490 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005491 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005492 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005493
Gilles Peskinebf491972018-10-25 22:36:12 +02005494 /* Test the actual capacity by reading the output. */
5495 while( actual_capacity > sizeof( output ) )
5496 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005497 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005498 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005499 actual_capacity -= sizeof( output );
5500 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005501 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005502 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005503 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005504 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005505
Gilles Peskine59685592018-09-18 12:11:34 +02005506exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005507 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005508 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005509 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005510}
5511/* END_CASE */
5512
5513/* BEGIN_CASE */
5514void key_agreement_output( int alg_arg,
5515 int our_key_type_arg, data_t *our_key_data,
5516 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005517 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005518{
Ronald Cron5425a212020-08-04 14:58:35 +02005519 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005520 psa_algorithm_t alg = alg_arg;
5521 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005524 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005525
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005526 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5527 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005528
Gilles Peskine8817f612018-12-18 00:18:46 +01005529 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005530
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005531 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5532 psa_set_key_algorithm( &attributes, alg );
5533 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005534 PSA_ASSERT( psa_import_key( &attributes,
5535 our_key_data->x, our_key_data->len,
5536 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005537
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005538 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005539 PSA_ASSERT( psa_key_derivation_key_agreement(
5540 &operation,
5541 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5542 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005543 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5544 {
5545 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005546 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005547 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005548 NULL, 0 ) );
5549 }
Gilles Peskine59685592018-09-18 12:11:34 +02005550
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005551 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005552 actual_output,
5553 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005554 ASSERT_COMPARE( actual_output, expected_output1->len,
5555 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005556 if( expected_output2->len != 0 )
5557 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005558 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005559 actual_output,
5560 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005561 ASSERT_COMPARE( actual_output, expected_output2->len,
5562 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005563 }
Gilles Peskine59685592018-09-18 12:11:34 +02005564
5565exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005566 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005567 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005568 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005569 mbedtls_free( actual_output );
5570}
5571/* END_CASE */
5572
5573/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005574void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005575{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005576 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005577 unsigned char *output = NULL;
5578 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005579 size_t i;
5580 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005581
Simon Butcher49f8e312020-03-03 15:51:50 +00005582 TEST_ASSERT( bytes_arg >= 0 );
5583
Gilles Peskine91892022021-02-08 19:50:26 +01005584 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005585 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005586
Gilles Peskine8817f612018-12-18 00:18:46 +01005587 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005588
Gilles Peskinea50d7392018-06-21 10:22:13 +02005589 /* Run several times, to ensure that every output byte will be
5590 * nonzero at least once with overwhelming probability
5591 * (2^(-8*number_of_runs)). */
5592 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005593 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005594 if( bytes != 0 )
5595 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005596 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005597
Gilles Peskinea50d7392018-06-21 10:22:13 +02005598 for( i = 0; i < bytes; i++ )
5599 {
5600 if( output[i] != 0 )
5601 ++changed[i];
5602 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005603 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005604
5605 /* Check that every byte was changed to nonzero at least once. This
5606 * validates that psa_generate_random is overwriting every byte of
5607 * the output buffer. */
5608 for( i = 0; i < bytes; i++ )
5609 {
5610 TEST_ASSERT( changed[i] != 0 );
5611 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005612
5613exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005614 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005615 mbedtls_free( output );
5616 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005617}
5618/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005619
5620/* BEGIN_CASE */
5621void generate_key( int type_arg,
5622 int bits_arg,
5623 int usage_arg,
5624 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005625 int expected_status_arg,
5626 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005627{
Ronald Cron5425a212020-08-04 14:58:35 +02005628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005629 psa_key_type_t type = type_arg;
5630 psa_key_usage_t usage = usage_arg;
5631 size_t bits = bits_arg;
5632 psa_algorithm_t alg = alg_arg;
5633 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005635 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005636
Gilles Peskine8817f612018-12-18 00:18:46 +01005637 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005638
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005639 psa_set_key_usage_flags( &attributes, usage );
5640 psa_set_key_algorithm( &attributes, alg );
5641 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005642 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005643
5644 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005645 psa_status_t status = psa_generate_key( &attributes, &key );
5646
5647 if( is_large_key > 0 )
5648 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5649 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005650 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005651 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005652
5653 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005654 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005655 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5656 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005657
Gilles Peskine818ca122018-06-20 18:16:48 +02005658 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005659 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005660 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005661
5662exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005663 /*
5664 * Key attributes may have been returned by psa_get_key_attributes()
5665 * thus reset them as required.
5666 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005667 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005668
Ronald Cron5425a212020-08-04 14:58:35 +02005669 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005670 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005671}
5672/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005673
Gilles Peskinee56e8782019-04-26 17:34:02 +02005674/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5675void generate_key_rsa( int bits_arg,
5676 data_t *e_arg,
5677 int expected_status_arg )
5678{
Ronald Cron5425a212020-08-04 14:58:35 +02005679 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005680 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005681 size_t bits = bits_arg;
5682 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5683 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5684 psa_status_t expected_status = expected_status_arg;
5685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5686 uint8_t *exported = NULL;
5687 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005688 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005689 size_t exported_length = SIZE_MAX;
5690 uint8_t *e_read_buffer = NULL;
5691 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005692 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005693 size_t e_read_length = SIZE_MAX;
5694
5695 if( e_arg->len == 0 ||
5696 ( e_arg->len == 3 &&
5697 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5698 {
5699 is_default_public_exponent = 1;
5700 e_read_size = 0;
5701 }
5702 ASSERT_ALLOC( e_read_buffer, e_read_size );
5703 ASSERT_ALLOC( exported, exported_size );
5704
5705 PSA_ASSERT( psa_crypto_init( ) );
5706
5707 psa_set_key_usage_flags( &attributes, usage );
5708 psa_set_key_algorithm( &attributes, alg );
5709 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5710 e_arg->x, e_arg->len ) );
5711 psa_set_key_bits( &attributes, bits );
5712
5713 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005714 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005715 if( expected_status != PSA_SUCCESS )
5716 goto exit;
5717
5718 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005719 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005720 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5721 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5722 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5723 e_read_buffer, e_read_size,
5724 &e_read_length ) );
5725 if( is_default_public_exponent )
5726 TEST_EQUAL( e_read_length, 0 );
5727 else
5728 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5729
5730 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005731 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005732 goto exit;
5733
5734 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005735 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005736 exported, exported_size,
5737 &exported_length ) );
5738 {
5739 uint8_t *p = exported;
5740 uint8_t *end = exported + exported_length;
5741 size_t len;
5742 /* RSAPublicKey ::= SEQUENCE {
5743 * modulus INTEGER, -- n
5744 * publicExponent INTEGER } -- e
5745 */
5746 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005747 MBEDTLS_ASN1_SEQUENCE |
5748 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005749 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5750 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5751 MBEDTLS_ASN1_INTEGER ) );
5752 if( len >= 1 && p[0] == 0 )
5753 {
5754 ++p;
5755 --len;
5756 }
5757 if( e_arg->len == 0 )
5758 {
5759 TEST_EQUAL( len, 3 );
5760 TEST_EQUAL( p[0], 1 );
5761 TEST_EQUAL( p[1], 0 );
5762 TEST_EQUAL( p[2], 1 );
5763 }
5764 else
5765 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5766 }
5767
5768exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005769 /*
5770 * Key attributes may have been returned by psa_get_key_attributes() or
5771 * set by psa_set_key_domain_parameters() thus reset them as required.
5772 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005773 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005774
Ronald Cron5425a212020-08-04 14:58:35 +02005775 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005776 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005777 mbedtls_free( e_read_buffer );
5778 mbedtls_free( exported );
5779}
5780/* END_CASE */
5781
Darryl Greend49a4992018-06-18 17:27:26 +01005782/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005783void persistent_key_load_key_from_storage( data_t *data,
5784 int type_arg, int bits_arg,
5785 int usage_flags_arg, int alg_arg,
5786 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005787{
Ronald Cron71016a92020-08-28 19:01:50 +02005788 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5791 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005792 psa_key_type_t type = type_arg;
5793 size_t bits = bits_arg;
5794 psa_key_usage_t usage_flags = usage_flags_arg;
5795 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005796 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005797 unsigned char *first_export = NULL;
5798 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005799 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005800 size_t first_exported_length;
5801 size_t second_exported_length;
5802
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005803 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5804 {
5805 ASSERT_ALLOC( first_export, export_size );
5806 ASSERT_ALLOC( second_export, export_size );
5807 }
Darryl Greend49a4992018-06-18 17:27:26 +01005808
Gilles Peskine8817f612018-12-18 00:18:46 +01005809 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005810
Gilles Peskinec87af662019-05-15 16:12:22 +02005811 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005812 psa_set_key_usage_flags( &attributes, usage_flags );
5813 psa_set_key_algorithm( &attributes, alg );
5814 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005815 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005816
Darryl Green0c6575a2018-11-07 16:05:30 +00005817 switch( generation_method )
5818 {
5819 case IMPORT_KEY:
5820 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005821 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005822 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005823 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005824
Darryl Green0c6575a2018-11-07 16:05:30 +00005825 case GENERATE_KEY:
5826 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005827 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005828 break;
5829
5830 case DERIVE_KEY:
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005831#if PSA_WANT_ALG_HKDF && PSA_WANT_ALG_SHA_256
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005832 {
5833 /* Create base key */
5834 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5835 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5836 psa_set_key_usage_flags( &base_attributes,
5837 PSA_KEY_USAGE_DERIVE );
5838 psa_set_key_algorithm( &base_attributes, derive_alg );
5839 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005840 PSA_ASSERT( psa_import_key( &base_attributes,
5841 data->x, data->len,
5842 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005843 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005844 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005845 PSA_ASSERT( psa_key_derivation_input_key(
5846 &operation,
5847 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005848 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005849 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005850 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005851 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5852 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005853 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005854 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005855 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005856 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005857 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005858#else
5859 TEST_ASSUME( ! "KDF not supported in this configuration" );
5860#endif
5861 break;
5862
5863 default:
5864 TEST_ASSERT( ! "generation_method not implemented in test" );
5865 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005866 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005867 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005868
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005869 /* Export the key if permitted by the key policy. */
5870 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5871 {
Ronald Cron5425a212020-08-04 14:58:35 +02005872 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005873 first_export, export_size,
5874 &first_exported_length ) );
5875 if( generation_method == IMPORT_KEY )
5876 ASSERT_COMPARE( data->x, data->len,
5877 first_export, first_exported_length );
5878 }
Darryl Greend49a4992018-06-18 17:27:26 +01005879
5880 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005881 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005882 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005883 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005884
Darryl Greend49a4992018-06-18 17:27:26 +01005885 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005886 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005887 TEST_ASSERT( mbedtls_svc_key_id_equal(
5888 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005889 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5890 PSA_KEY_LIFETIME_PERSISTENT );
5891 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5892 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5893 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5894 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005895
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005896 /* Export the key again if permitted by the key policy. */
5897 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005898 {
Ronald Cron5425a212020-08-04 14:58:35 +02005899 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005900 second_export, export_size,
5901 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005902 ASSERT_COMPARE( first_export, first_exported_length,
5903 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005904 }
5905
5906 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005907 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005908 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005909
5910exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005911 /*
5912 * Key attributes may have been returned by psa_get_key_attributes()
5913 * thus reset them as required.
5914 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005915 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005916
Darryl Greend49a4992018-06-18 17:27:26 +01005917 mbedtls_free( first_export );
5918 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005919 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005920 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005921 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005922 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005923}
5924/* END_CASE */