blob: b68eb7c6f8d197b0f122103a4b463d96479fa87f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Jaeden Amerof24c7f82018-06-27 17:20:43 +010015/** An invalid export length that will never be set by psa_export_key(). */
16static const size_t INVALID_EXPORT_LENGTH = ~0U;
17
Gilles Peskinef426e0f2019-02-25 17:42:03 +010018/* A hash algorithm that is known to be supported.
19 *
20 * This is used in some smoke tests.
21 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010022#if defined(PSA_WANT_ALG_MD2)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010023#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
Gilles Peskined6dc40c2021-01-12 12:55:31 +010024#elif defined(PSA_WANT_ALG_MD4)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010025#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
Gilles Peskined6dc40c2021-01-12 12:55:31 +010026#elif defined(PSA_WANT_ALG_MD5)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010027#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
28/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
29 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
30 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
31 * implausible anyway. */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010032#elif defined(PSA_WANT_ALG_SHA_1)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010033#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
Gilles Peskined6dc40c2021-01-12 12:55:31 +010034#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010035#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
Gilles Peskined6dc40c2021-01-12 12:55:31 +010036#elif defined(PSA_WANT_ALG_SHA_384)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010037#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
Gilles Peskined6dc40c2021-01-12 12:55:31 +010038#elif defined(PSA_WANT_ALG_SHA_512)
39#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
40#elif defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010041#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
42#else
43#undef KNOWN_SUPPORTED_HASH_ALG
44#endif
45
46/* A block cipher that is known to be supported.
47 *
48 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
49 */
50#if defined(MBEDTLS_AES_C)
51#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
52#elif defined(MBEDTLS_ARIA_C)
53#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
54#elif defined(MBEDTLS_CAMELLIA_C)
55#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
56#undef KNOWN_SUPPORTED_BLOCK_CIPHER
57#endif
58
59/* A MAC mode that is known to be supported.
60 *
61 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
62 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
63 *
64 * This is used in some smoke tests.
65 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010066#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010067#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
68#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
69#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
70#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
71#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
72#else
73#undef KNOWN_SUPPORTED_MAC_ALG
74#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
75#endif
76
77/* A cipher algorithm and key type that are known to be supported.
78 *
79 * This is used in some smoke tests.
80 */
81#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
82#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
83#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
84#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
85#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
86#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
87#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
89#else
90#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91#endif
92#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
93#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
94#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
95#elif defined(MBEDTLS_RC4_C)
96#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
97#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
98#else
99#undef KNOWN_SUPPORTED_CIPHER_ALG
100#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
101#endif
102
Gilles Peskine667c1112019-12-03 19:03:20 +0100103#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100104int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
Gilles Peskine667c1112019-12-03 19:03:20 +0100105{
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100106 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
107 PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100108}
109#else
110int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
111{
112 (void) lifetime;
113 return( 0 );
114}
115#endif
116
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200117/** Test if a buffer contains a constant byte value.
118 *
119 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200120 *
121 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200122 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200123 * \param size Size of the buffer in bytes.
124 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200125 * \return 1 if the buffer is all-bits-zero.
126 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200127 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200128static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200129{
130 size_t i;
131 for( i = 0; i < size; i++ )
132 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200133 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200134 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200135 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200136 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200137}
Gilles Peskine818ca122018-06-20 18:16:48 +0200138
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200139/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
140static int asn1_write_10x( unsigned char **p,
141 unsigned char *start,
142 size_t bits,
143 unsigned char x )
144{
145 int ret;
146 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200147 if( bits == 0 )
148 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
149 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200150 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300151 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200152 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
153 *p -= len;
154 ( *p )[len-1] = x;
155 if( bits % 8 == 0 )
156 ( *p )[1] |= 1;
157 else
158 ( *p )[0] |= 1 << ( bits % 8 );
159 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
160 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
161 MBEDTLS_ASN1_INTEGER ) );
162 return( len );
163}
164
165static int construct_fake_rsa_key( unsigned char *buffer,
166 size_t buffer_size,
167 unsigned char **p,
168 size_t bits,
169 int keypair )
170{
171 size_t half_bits = ( bits + 1 ) / 2;
172 int ret;
173 int len = 0;
174 /* Construct something that looks like a DER encoding of
175 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
176 * RSAPrivateKey ::= SEQUENCE {
177 * version Version,
178 * modulus INTEGER, -- n
179 * publicExponent INTEGER, -- e
180 * privateExponent INTEGER, -- d
181 * prime1 INTEGER, -- p
182 * prime2 INTEGER, -- q
183 * exponent1 INTEGER, -- d mod (p-1)
184 * exponent2 INTEGER, -- d mod (q-1)
185 * coefficient INTEGER, -- (inverse of q) mod p
186 * otherPrimeInfos OtherPrimeInfos OPTIONAL
187 * }
188 * Or, for a public key, the same structure with only
189 * version, modulus and publicExponent.
190 */
191 *p = buffer + buffer_size;
192 if( keypair )
193 {
194 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
195 asn1_write_10x( p, buffer, half_bits, 1 ) );
196 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
197 asn1_write_10x( p, buffer, half_bits, 1 ) );
198 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
199 asn1_write_10x( p, buffer, half_bits, 1 ) );
200 MBEDTLS_ASN1_CHK_ADD( len, /* q */
201 asn1_write_10x( p, buffer, half_bits, 1 ) );
202 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
203 asn1_write_10x( p, buffer, half_bits, 3 ) );
204 MBEDTLS_ASN1_CHK_ADD( len, /* d */
205 asn1_write_10x( p, buffer, bits, 1 ) );
206 }
207 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
208 asn1_write_10x( p, buffer, 17, 1 ) );
209 MBEDTLS_ASN1_CHK_ADD( len, /* n */
210 asn1_write_10x( p, buffer, bits, 1 ) );
211 if( keypair )
212 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
213 mbedtls_asn1_write_int( p, buffer, 0 ) );
214 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
215 {
216 const unsigned char tag =
217 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
218 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
219 }
220 return( len );
221}
222
Ronald Cron5425a212020-08-04 14:58:35 +0200223int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100224{
225 int ok = 0;
226 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
227 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200228 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100229 psa_key_type_t type;
230 psa_key_type_t bits;
231
232 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
233 lifetime = psa_get_key_lifetime( &attributes );
234 id = psa_get_key_id( &attributes );
235 type = psa_get_key_type( &attributes );
236 bits = psa_get_key_bits( &attributes );
237
238 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200239 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200240 {
241 TEST_ASSERT(
242 ( PSA_KEY_ID_VOLATILE_MIN <=
243 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
244 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
245 PSA_KEY_ID_VOLATILE_MAX ) );
246 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100247 else
248 {
249 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200250 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
251 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100252 }
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254 /* randomly-generated 64-bit constant, should never appear in test data */
255 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
256 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100257 if( lifetime_is_dynamic_secure_element( lifetime ) )
Gilles Peskine667c1112019-12-03 19:03:20 +0100258 {
259 /* Mbed Crypto currently always exposes the slot number to
260 * applications. This is not mandated by the PSA specification
261 * and may change in future versions. */
262 TEST_EQUAL( status, 0 );
263 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
264 }
265 else
266 {
267 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
268 }
269#endif
270
271 /* Type and size */
272 TEST_ASSERT( type != 0 );
273 TEST_ASSERT( bits != 0 );
274 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
275 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
276 TEST_ASSERT( bits % 8 == 0 );
277
278 /* MAX macros concerning specific key types */
279 if( PSA_KEY_TYPE_IS_ECC( type ) )
280 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
281 else if( PSA_KEY_TYPE_IS_RSA( type ) )
282 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100283 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100284
285 ok = 1;
286
287exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100288 /*
289 * Key attributes may have been returned by psa_get_key_attributes()
290 * thus reset them as required.
291 */
Gilles Peskine667c1112019-12-03 19:03:20 +0100292 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100293
Gilles Peskine667c1112019-12-03 19:03:20 +0100294 return( ok );
295}
296
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100297int exercise_mac_setup( psa_key_type_t key_type,
298 const unsigned char *key_bytes,
299 size_t key_length,
300 psa_algorithm_t alg,
301 psa_mac_operation_t *operation,
302 psa_status_t *status )
303{
Ronald Cron5425a212020-08-04 14:58:35 +0200304 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200305 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100306
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100307 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200308 psa_set_key_algorithm( &attributes, alg );
309 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200310 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100311
Ronald Cron5425a212020-08-04 14:58:35 +0200312 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100313 /* Whether setup succeeded or failed, abort must succeed. */
314 PSA_ASSERT( psa_mac_abort( operation ) );
315 /* If setup failed, reproduce the failure, so that the caller can
316 * test the resulting state of the operation object. */
317 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100318 {
Ronald Cron5425a212020-08-04 14:58:35 +0200319 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100320 }
321
Ronald Cron5425a212020-08-04 14:58:35 +0200322 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100323 return( 1 );
324
325exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200326 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100327 return( 0 );
328}
329
330int exercise_cipher_setup( psa_key_type_t key_type,
331 const unsigned char *key_bytes,
332 size_t key_length,
333 psa_algorithm_t alg,
334 psa_cipher_operation_t *operation,
335 psa_status_t *status )
336{
Ronald Cron5425a212020-08-04 14:58:35 +0200337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100339
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200340 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
341 psa_set_key_algorithm( &attributes, alg );
342 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200343 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100344
Ronald Cron5425a212020-08-04 14:58:35 +0200345 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100346 /* Whether setup succeeded or failed, abort must succeed. */
347 PSA_ASSERT( psa_cipher_abort( operation ) );
348 /* If setup failed, reproduce the failure, so that the caller can
349 * test the resulting state of the operation object. */
350 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100351 {
Ronald Cron5425a212020-08-04 14:58:35 +0200352 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100353 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100354 }
355
Ronald Cron5425a212020-08-04 14:58:35 +0200356 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100357 return( 1 );
358
359exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200360 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100361 return( 0 );
362}
363
Ronald Cron5425a212020-08-04 14:58:35 +0200364static int exercise_mac_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200365 psa_key_usage_t usage,
366 psa_algorithm_t alg )
367{
Jaeden Amero769ce272019-01-04 11:48:03 +0000368 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200369 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200370 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200371 size_t mac_length = sizeof( mac );
372
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100373 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 {
Ronald Cron5425a212020-08-04 14:58:35 +0200375 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100376 PSA_ASSERT( psa_mac_update( &operation,
377 input, sizeof( input ) ) );
378 PSA_ASSERT( psa_mac_sign_finish( &operation,
379 mac, sizeof( mac ),
380 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200381 }
382
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100383 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200384 {
385 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100386 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200387 PSA_SUCCESS :
388 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200389 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100390 PSA_ASSERT( psa_mac_update( &operation,
391 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100392 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
393 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200394 }
395
396 return( 1 );
397
398exit:
399 psa_mac_abort( &operation );
400 return( 0 );
401}
402
Ronald Cron5425a212020-08-04 14:58:35 +0200403static int exercise_cipher_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200404 psa_key_usage_t usage,
405 psa_algorithm_t alg )
406{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000407 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200408 unsigned char iv[16] = {0};
409 size_t iv_length = sizeof( iv );
410 const unsigned char plaintext[16] = "Hello, world...";
411 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
412 size_t ciphertext_length = sizeof( ciphertext );
413 unsigned char decrypted[sizeof( ciphertext )];
414 size_t part_length;
415
416 if( usage & PSA_KEY_USAGE_ENCRYPT )
417 {
Ronald Cron5425a212020-08-04 14:58:35 +0200418 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100419 PSA_ASSERT( psa_cipher_generate_iv( &operation,
420 iv, sizeof( iv ),
421 &iv_length ) );
422 PSA_ASSERT( psa_cipher_update( &operation,
423 plaintext, sizeof( plaintext ),
424 ciphertext, sizeof( ciphertext ),
425 &ciphertext_length ) );
426 PSA_ASSERT( psa_cipher_finish( &operation,
427 ciphertext + ciphertext_length,
428 sizeof( ciphertext ) - ciphertext_length,
429 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200430 ciphertext_length += part_length;
431 }
432
433 if( usage & PSA_KEY_USAGE_DECRYPT )
434 {
435 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200436 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200437 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
438 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200440 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200441 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
442 * have this macro yet. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100443 iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200444 psa_get_key_type( &attributes ) );
445 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100446 psa_reset_key_attributes( &attributes );
Gilles Peskine818ca122018-06-20 18:16:48 +0200447 }
Ronald Cron5425a212020-08-04 14:58:35 +0200448 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100449 PSA_ASSERT( psa_cipher_set_iv( &operation,
450 iv, iv_length ) );
451 PSA_ASSERT( psa_cipher_update( &operation,
452 ciphertext, ciphertext_length,
453 decrypted, sizeof( decrypted ),
454 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200455 status = psa_cipher_finish( &operation,
456 decrypted + part_length,
457 sizeof( decrypted ) - part_length,
458 &part_length );
459 /* For a stream cipher, all inputs are valid. For a block cipher,
460 * if the input is some aribtrary data rather than an actual
461 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200462 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200463 TEST_ASSERT( status == PSA_SUCCESS ||
464 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200465 else
466 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200467 }
468
469 return( 1 );
470
471exit:
472 psa_cipher_abort( &operation );
473 return( 0 );
474}
475
Ronald Cron5425a212020-08-04 14:58:35 +0200476static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200477 psa_key_usage_t usage,
478 psa_algorithm_t alg )
479{
480 unsigned char nonce[16] = {0};
481 size_t nonce_length = sizeof( nonce );
482 unsigned char plaintext[16] = "Hello, world...";
483 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
484 size_t ciphertext_length = sizeof( ciphertext );
485 size_t plaintext_length = sizeof( ciphertext );
486
Steven Cooreman2f099132021-01-11 20:33:45 +0100487 /* Default IV length for AES-GCM is 12 bytes */
Steven Cooreman50f1f5e2021-01-25 10:26:49 +0100488 if( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ==
489 PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ) )
490 {
Steven Cooreman2f099132021-01-11 20:33:45 +0100491 nonce_length = 12;
Steven Cooreman50f1f5e2021-01-25 10:26:49 +0100492 }
Steven Cooreman2f099132021-01-11 20:33:45 +0100493
Gilles Peskine818ca122018-06-20 18:16:48 +0200494 if( usage & PSA_KEY_USAGE_ENCRYPT )
495 {
Ronald Cron5425a212020-08-04 14:58:35 +0200496 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100497 nonce, nonce_length,
498 NULL, 0,
499 plaintext, sizeof( plaintext ),
500 ciphertext, sizeof( ciphertext ),
501 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200502 }
503
504 if( usage & PSA_KEY_USAGE_DECRYPT )
505 {
506 psa_status_t verify_status =
507 ( usage & PSA_KEY_USAGE_ENCRYPT ?
508 PSA_SUCCESS :
509 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200510 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100511 nonce, nonce_length,
512 NULL, 0,
513 ciphertext, ciphertext_length,
514 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100515 &plaintext_length ),
516 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200517 }
518
519 return( 1 );
520
521exit:
522 return( 0 );
523}
524
Ronald Cron5425a212020-08-04 14:58:35 +0200525static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200526 psa_key_usage_t usage,
527 psa_algorithm_t alg )
528{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200529 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
530 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100531 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200532 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100533 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
534
535 /* If the policy allows signing with any hash, just pick one. */
536 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
537 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100538#if defined(KNOWN_SUPPORTED_HASH_ALG)
539 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
540 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100541#else
Chris Jones9634bb12021-01-20 15:56:42 +0000542 mbedtls_test_fail( "No hash algorithm for hash-and-sign testing",
543 __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100544 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100545#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100546 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200547
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100548 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200549 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200550 /* Some algorithms require the payload to have the size of
551 * the hash encoded in the algorithm. Use this input size
552 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200553 if( hash_alg != 0 )
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100554 payload_length = PSA_HASH_LENGTH( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200555 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100556 payload, payload_length,
557 signature, sizeof( signature ),
558 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200559 }
560
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100561 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200562 {
563 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100564 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200565 PSA_SUCCESS :
566 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200567 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100568 payload, payload_length,
569 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100570 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200571 }
572
573 return( 1 );
574
575exit:
576 return( 0 );
577}
578
Ronald Cron5425a212020-08-04 14:58:35 +0200579static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200580 psa_key_usage_t usage,
581 psa_algorithm_t alg )
582{
583 unsigned char plaintext[256] = "Hello, world...";
584 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
585 size_t ciphertext_length = sizeof( ciphertext );
586 size_t plaintext_length = 16;
587
588 if( usage & PSA_KEY_USAGE_ENCRYPT )
589 {
Ronald Cron5425a212020-08-04 14:58:35 +0200590 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100591 plaintext, plaintext_length,
592 NULL, 0,
593 ciphertext, sizeof( ciphertext ),
594 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200595 }
596
597 if( usage & PSA_KEY_USAGE_DECRYPT )
598 {
599 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200600 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200601 ciphertext, ciphertext_length,
602 NULL, 0,
603 plaintext, sizeof( plaintext ),
604 &plaintext_length );
605 TEST_ASSERT( status == PSA_SUCCESS ||
606 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
607 ( status == PSA_ERROR_INVALID_ARGUMENT ||
608 status == PSA_ERROR_INVALID_PADDING ) ) );
609 }
610
611 return( 1 );
612
613exit:
614 return( 0 );
615}
Gilles Peskine02b75072018-07-01 22:31:34 +0200616
Janos Follathf2815ea2019-07-03 12:41:36 +0100617static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200618 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100619 psa_algorithm_t alg,
620 unsigned char* input1, size_t input1_length,
621 unsigned char* input2, size_t input2_length,
622 size_t capacity )
623{
624 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
625 if( PSA_ALG_IS_HKDF( alg ) )
626 {
627 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
628 PSA_KEY_DERIVATION_INPUT_SALT,
629 input1, input1_length ) );
630 PSA_ASSERT( psa_key_derivation_input_key( operation,
631 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200632 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100633 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
634 PSA_KEY_DERIVATION_INPUT_INFO,
635 input2,
636 input2_length ) );
637 }
638 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
639 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
640 {
641 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
642 PSA_KEY_DERIVATION_INPUT_SEED,
643 input1, input1_length ) );
644 PSA_ASSERT( psa_key_derivation_input_key( operation,
645 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200646 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100647 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
648 PSA_KEY_DERIVATION_INPUT_LABEL,
649 input2, input2_length ) );
650 }
651 else
652 {
653 TEST_ASSERT( ! "Key derivation algorithm not supported" );
654 }
655
Gilles Peskinec744d992019-07-30 17:26:54 +0200656 if( capacity != SIZE_MAX )
657 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100658
659 return( 1 );
660
661exit:
662 return( 0 );
663}
664
665
Ronald Cron5425a212020-08-04 14:58:35 +0200666static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200667 psa_key_usage_t usage,
668 psa_algorithm_t alg )
669{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200670 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100671 unsigned char input1[] = "Input 1";
672 size_t input1_length = sizeof( input1 );
673 unsigned char input2[] = "Input 2";
674 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200675 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100676 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200677
678 if( usage & PSA_KEY_USAGE_DERIVE )
679 {
Ronald Cron5425a212020-08-04 14:58:35 +0200680 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100681 input1, input1_length,
682 input2, input2_length, capacity ) )
683 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200684
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200685 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200686 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100687 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200688 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200689 }
690
691 return( 1 );
692
693exit:
694 return( 0 );
695}
696
Gilles Peskinec7998b72018-11-07 18:45:02 +0100697/* We need two keys to exercise key agreement. Exercise the
698 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200699static psa_status_t key_agreement_with_self(
700 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200701 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100702{
703 psa_key_type_t private_key_type;
704 psa_key_type_t public_key_type;
705 size_t key_bits;
706 uint8_t *public_key = NULL;
707 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200708 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200709 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
710 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200711 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100713
Ronald Cron5425a212020-08-04 14:58:35 +0200714 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200715 private_key_type = psa_get_key_type( &attributes );
716 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200717 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100718 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100719 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200720 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100721 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100722
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200723 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200724 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200725 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100726exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100727 /*
728 * Key attributes may have been returned by psa_get_key_attributes()
729 * thus reset them as required.
730 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200731 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100732
733 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100734 return( status );
735}
736
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200737/* We need two keys to exercise key agreement. Exercise the
738 * private key against its own public key. */
739static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200740 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200741{
742 psa_key_type_t private_key_type;
743 psa_key_type_t public_key_type;
744 size_t key_bits;
745 uint8_t *public_key = NULL;
746 size_t public_key_length;
747 uint8_t output[1024];
748 size_t output_length;
749 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200750 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
751 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200752 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200754
Ronald Cron5425a212020-08-04 14:58:35 +0200755 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200756 private_key_type = psa_get_key_type( &attributes );
757 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200758 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100759 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200760 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200761 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200762 public_key, public_key_length,
763 &public_key_length ) );
764
Ronald Cron5425a212020-08-04 14:58:35 +0200765 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200766 public_key, public_key_length,
767 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200768exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100769 /*
770 * Key attributes may have been returned by psa_get_key_attributes()
771 * thus reset them as required.
772 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200773 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100774
775 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200776 return( status );
777}
778
Ronald Cron5425a212020-08-04 14:58:35 +0200779static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200780 psa_key_usage_t usage,
781 psa_algorithm_t alg )
782{
783 int ok = 0;
784
785 if( usage & PSA_KEY_USAGE_DERIVE )
786 {
787 /* We need two keys to exercise key agreement. Exercise the
788 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200789 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200790 }
791 ok = 1;
792
793exit:
794 return( ok );
795}
796
Ronald Cron5425a212020-08-04 14:58:35 +0200797static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200798 psa_key_usage_t usage,
799 psa_algorithm_t alg )
800{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200801 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200802 unsigned char output[1];
803 int ok = 0;
804
805 if( usage & PSA_KEY_USAGE_DERIVE )
806 {
807 /* We need two keys to exercise key agreement. Exercise the
808 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200809 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200810 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200811 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200812 output,
813 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200814 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200815 }
816 ok = 1;
817
818exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200819 return( ok );
820}
821
Jaeden Amerof7dca862019-06-27 17:31:33 +0100822int asn1_skip_integer( unsigned char **p, const unsigned char *end,
823 size_t min_bits, size_t max_bits,
824 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200825{
826 size_t len;
827 size_t actual_bits;
828 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100829 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100830 MBEDTLS_ASN1_INTEGER ),
831 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200832
833 /* Check if the retrieved length doesn't extend the actual buffer's size.
834 * It is assumed here, that end >= p, which validates casting to size_t. */
835 TEST_ASSERT( len <= (size_t)( end - *p) );
836
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200837 /* Tolerate a slight departure from DER encoding:
838 * - 0 may be represented by an empty string or a 1-byte string.
839 * - The sign bit may be used as a value bit. */
840 if( ( len == 1 && ( *p )[0] == 0 ) ||
841 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
842 {
843 ++( *p );
844 --len;
845 }
846 if( min_bits == 0 && len == 0 )
847 return( 1 );
848 msb = ( *p )[0];
849 TEST_ASSERT( msb != 0 );
850 actual_bits = 8 * ( len - 1 );
851 while( msb != 0 )
852 {
853 msb >>= 1;
854 ++actual_bits;
855 }
856 TEST_ASSERT( actual_bits >= min_bits );
857 TEST_ASSERT( actual_bits <= max_bits );
858 if( must_be_odd )
859 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
860 *p += len;
861 return( 1 );
862exit:
863 return( 0 );
864}
865
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200866static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
867 uint8_t *exported, size_t exported_length )
868{
869 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100870 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200871 else
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100872 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200873
874#if defined(MBEDTLS_DES_C)
875 if( type == PSA_KEY_TYPE_DES )
876 {
877 /* Check the parity bits. */
878 unsigned i;
879 for( i = 0; i < bits / 8; i++ )
880 {
881 unsigned bit_count = 0;
882 unsigned m;
883 for( m = 1; m <= 0x100; m <<= 1 )
884 {
885 if( exported[i] & m )
886 ++bit_count;
887 }
888 TEST_ASSERT( bit_count % 2 != 0 );
889 }
890 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200891 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200892#endif
893
894#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200895 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200896 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200897 uint8_t *p = exported;
898 uint8_t *end = exported + exported_length;
899 size_t len;
900 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200901 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200902 * modulus INTEGER, -- n
903 * publicExponent INTEGER, -- e
904 * privateExponent INTEGER, -- d
905 * prime1 INTEGER, -- p
906 * prime2 INTEGER, -- q
907 * exponent1 INTEGER, -- d mod (p-1)
908 * exponent2 INTEGER, -- d mod (q-1)
909 * coefficient INTEGER, -- (inverse of q) mod p
910 * }
911 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100912 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
913 MBEDTLS_ASN1_SEQUENCE |
914 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
915 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200916 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
917 goto exit;
918 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
919 goto exit;
920 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
921 goto exit;
922 /* Require d to be at least half the size of n. */
923 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
924 goto exit;
925 /* Require p and q to be at most half the size of n, rounded up. */
926 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
927 goto exit;
928 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
929 goto exit;
930 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
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;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100936 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100937 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200938 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200939#endif /* MBEDTLS_RSA_C */
940
941#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200942 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200943 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100944 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100946 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200947 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200948#endif /* MBEDTLS_ECP_C */
949
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200950 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
951 {
952 uint8_t *p = exported;
953 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200954#if defined(MBEDTLS_RSA_C)
955 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
956 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100957 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200958 /* RSAPublicKey ::= SEQUENCE {
959 * modulus INTEGER, -- n
960 * publicExponent INTEGER } -- e
961 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100962 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
963 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100964 MBEDTLS_ASN1_CONSTRUCTED ),
965 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100966 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200967 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
968 goto exit;
969 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
970 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100971 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200972 }
973 else
974#endif /* MBEDTLS_RSA_C */
975#if defined(MBEDTLS_ECP_C)
976 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
977 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200978 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
979 {
980 /* The representation of an ECC Montgomery public key is
981 * the raw compressed point */
982 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
983 }
984 else
985 {
986 /* The representation of an ECC Weierstrass public key is:
987 * - The byte 0x04;
988 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
989 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
990 * - where m is the bit size associated with the curve.
991 */
992 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
993 TEST_EQUAL( p[0], 4 );
994 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200995 }
996 else
997#endif /* MBEDTLS_ECP_C */
998 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100999 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001000 mbedtls_snprintf( message, sizeof( message ),
1001 "No sanity check for public key type=0x%08lx",
1002 (unsigned long) type );
Chris Jones9634bb12021-01-20 15:56:42 +00001003 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +02001004 (void) p;
1005 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001006 return( 0 );
1007 }
1008 }
1009 else
1010
1011 {
1012 /* No sanity checks for other types */
1013 }
1014
1015 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001016
1017exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001018 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001019}
1020
Ronald Cron5425a212020-08-04 14:58:35 +02001021static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001022 psa_key_usage_t usage )
1023{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001025 uint8_t *exported = NULL;
1026 size_t exported_size = 0;
1027 size_t exported_length = 0;
1028 int ok = 0;
1029
Ronald Cron5425a212020-08-04 14:58:35 +02001030 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001031
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001032 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1033 psa_get_key_type( &attributes ),
1034 psa_get_key_bits( &attributes ) );
1035 ASSERT_ALLOC( exported, exported_size );
1036
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001037 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001038 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001039 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001040 TEST_EQUAL( psa_export_key( key, exported,
1041 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001042 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001043 ok = 1;
1044 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001045 }
1046
Ronald Cron5425a212020-08-04 14:58:35 +02001047 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001048 exported, exported_size,
1049 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001050 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1051 psa_get_key_bits( &attributes ),
1052 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001053
1054exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001055 /*
1056 * Key attributes may have been returned by psa_get_key_attributes()
1057 * thus reset them as required.
1058 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001059 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001060
1061 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001062 return( ok );
1063}
1064
Ronald Cron5425a212020-08-04 14:58:35 +02001065static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001066{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001068 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001069 uint8_t *exported = NULL;
1070 size_t exported_size = 0;
1071 size_t exported_length = 0;
1072 int ok = 0;
1073
Ronald Cron5425a212020-08-04 14:58:35 +02001074 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001075 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001076 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001077 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1078 psa_get_key_type( &attributes ),
1079 psa_get_key_bits( &attributes ) );
1080 ASSERT_ALLOC( exported, exported_size );
1081
1082 TEST_EQUAL( psa_export_public_key( key, exported,
1083 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001084 PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001085 ok = 1;
1086 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001087 }
1088
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001089 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001090 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001091 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1092 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001093 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001094
Ronald Cron5425a212020-08-04 14:58:35 +02001095 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001096 exported, exported_size,
1097 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001098 ok = exported_key_sanity_check( public_type,
1099 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001100 exported, exported_length );
1101
1102exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001103 /*
1104 * Key attributes may have been returned by psa_get_key_attributes()
1105 * thus reset them as required.
1106 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001107 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001108
1109 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001110 return( ok );
1111}
1112
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001113/** Do smoke tests on a key.
1114 *
1115 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1116 * sign/verify, or derivation) that is permitted according to \p usage.
1117 * \p usage and \p alg should correspond to the expected policy on the
1118 * key.
1119 *
1120 * Export the key if permitted by \p usage, and check that the output
1121 * looks sensible. If \p usage forbids export, check that
1122 * \p psa_export_key correctly rejects the attempt. If the key is
1123 * asymmetric, also check \p psa_export_public_key.
1124 *
1125 * If the key fails the tests, this function calls the test framework's
Chris Jones9634bb12021-01-20 15:56:42 +00001126 * `mbedtls_test_fail` function and returns false. Otherwise this function
1127 * returns true. Therefore it should be used as follows:
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001128 * ```
1129 * if( ! exercise_key( ... ) ) goto exit;
1130 * ```
1131 *
Ronald Cron5425a212020-08-04 14:58:35 +02001132 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001133 * \p alg.
1134 * \param usage The usage flags to assume.
1135 * \param alg The algorithm to exercise.
1136 *
1137 * \retval 0 The key failed the smoke tests.
1138 * \retval 1 The key passed the smoke tests.
1139 */
Ronald Cron5425a212020-08-04 14:58:35 +02001140static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001141 psa_key_usage_t usage,
1142 psa_algorithm_t alg )
1143{
1144 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001145
Ronald Cron5425a212020-08-04 14:58:35 +02001146 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001147 return( 0 );
1148
Gilles Peskine02b75072018-07-01 22:31:34 +02001149 if( alg == 0 )
1150 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1151 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001152 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001153 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001154 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001155 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001156 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001157 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001158 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001159 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001160 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001161 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001162 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001163 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001164 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001165 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001166 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001167 else
1168 {
1169 char message[40];
1170 mbedtls_snprintf( message, sizeof( message ),
1171 "No code to exercise alg=0x%08lx",
1172 (unsigned long) alg );
Chris Jones9634bb12021-01-20 15:56:42 +00001173 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskine02b75072018-07-01 22:31:34 +02001174 ok = 0;
1175 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001176
Ronald Cron5425a212020-08-04 14:58:35 +02001177 ok = ok && exercise_export_key( key, usage );
1178 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001179
Gilles Peskine02b75072018-07-01 22:31:34 +02001180 return( ok );
1181}
1182
Gilles Peskine10df3412018-10-25 22:35:43 +02001183static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1184 psa_algorithm_t alg )
1185{
1186 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1187 {
1188 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001189 PSA_KEY_USAGE_VERIFY_HASH :
1190 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001191 }
1192 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1193 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1194 {
1195 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1196 PSA_KEY_USAGE_ENCRYPT :
1197 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1198 }
1199 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1200 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1201 {
1202 return( PSA_KEY_USAGE_DERIVE );
1203 }
1204 else
1205 {
1206 return( 0 );
1207 }
1208
1209}
Darryl Green0c6575a2018-11-07 16:05:30 +00001210
Ronald Cron5425a212020-08-04 14:58:35 +02001211static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001212{
1213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001214 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001215 uint8_t buffer[1];
1216 size_t length;
1217 int ok = 0;
1218
Ronald Cronecfb2372020-07-23 17:13:42 +02001219 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1221 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1222 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001223 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001224 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001225 TEST_EQUAL(
1226 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1227 TEST_EQUAL(
1228 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001229 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001230 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1231 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1232 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1233 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1234
Ronald Cron5425a212020-08-04 14:58:35 +02001235 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001236 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001237 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001238 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001239 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001240
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001241 ok = 1;
1242
1243exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001244 /*
1245 * Key attributes may have been returned by psa_get_key_attributes()
1246 * thus reset them as required.
1247 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001248 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001249
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001250 return( ok );
1251}
1252
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001253/* Assert that a key isn't reported as having a slot number. */
1254#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1255#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1256 do \
1257 { \
1258 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1259 TEST_EQUAL( psa_get_key_slot_number( \
1260 attributes, \
1261 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1262 PSA_ERROR_INVALID_ARGUMENT ); \
1263 } \
1264 while( 0 )
1265#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1266#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1267 ( (void) 0 )
1268#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1269
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001270/* An overapproximation of the amount of storage needed for a key of the
1271 * given type and with the given content. The API doesn't make it easy
1272 * to find a good value for the size. The current implementation doesn't
1273 * care about the value anyway. */
1274#define KEY_BITS_FROM_DATA( type, data ) \
1275 ( data )->len
1276
Darryl Green0c6575a2018-11-07 16:05:30 +00001277typedef enum {
1278 IMPORT_KEY = 0,
1279 GENERATE_KEY = 1,
1280 DERIVE_KEY = 2
1281} generate_method;
1282
Gilles Peskinee59236f2018-01-27 23:32:46 +01001283/* END_HEADER */
1284
1285/* BEGIN_DEPENDENCIES
1286 * depends_on:MBEDTLS_PSA_CRYPTO_C
1287 * END_DEPENDENCIES
1288 */
1289
1290/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001291void static_checks( )
1292{
1293 size_t max_truncated_mac_size =
1294 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1295
1296 /* Check that the length for a truncated MAC always fits in the algorithm
1297 * encoding. The shifted mask is the maximum truncated value. The
1298 * untruncated algorithm may be one byte larger. */
1299 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001300
1301#if defined(MBEDTLS_TEST_DEPRECATED)
1302 /* Check deprecated constants. */
1303 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1304 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1305 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1306 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1307 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1308 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1309 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1310 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001311
Paul Elliott8ff510a2020-06-02 17:19:28 +01001312 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1314 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1326 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1328 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1329 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1330 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1331 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1332 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1333 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1334 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1335 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1336 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1337 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1338 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1339 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1340 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1341 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1342
1343 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1344 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1345 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1346 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1347 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1348 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1349 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1350 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001351
Paul Elliott75e27032020-06-03 15:17:39 +01001352 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1353 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1354 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1355 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1356 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1357
1358 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1359 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001360#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001361}
1362/* END_CASE */
1363
1364/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001365void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001366 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001367 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001368{
Gilles Peskine4747d192019-04-17 15:05:45 +02001369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001370 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001371 psa_key_lifetime_t lifetime = lifetime_arg;
1372 psa_key_usage_t usage_flags = usage_flags_arg;
1373 psa_algorithm_t alg = alg_arg;
1374 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001375 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001376
Ronald Cronecfb2372020-07-23 17:13:42 +02001377 TEST_EQUAL(
1378 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1379 TEST_EQUAL(
1380 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001381 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1382 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1383 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1384 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001385 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001386
Gilles Peskinec87af662019-05-15 16:12:22 +02001387 psa_set_key_id( &attributes, id );
1388 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001389 psa_set_key_usage_flags( &attributes, usage_flags );
1390 psa_set_key_algorithm( &attributes, alg );
1391 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001392 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001393
Ronald Cronecfb2372020-07-23 17:13:42 +02001394 TEST_ASSERT( mbedtls_svc_key_id_equal(
1395 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001396 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1397 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1398 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1399 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001400 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001401
1402 psa_reset_key_attributes( &attributes );
1403
Ronald Cronecfb2372020-07-23 17:13:42 +02001404 TEST_EQUAL(
1405 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1406 TEST_EQUAL(
1407 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001408 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1409 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1410 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1411 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001412 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001413}
1414/* END_CASE */
1415
1416/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001417void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1418 int id2_arg, int owner_id2_arg,
1419 int expected_id_arg, int expected_owner_id_arg,
1420 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001421{
1422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001423 mbedtls_svc_key_id_t id1 =
1424 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001425 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001426 mbedtls_svc_key_id_t id2 =
1427 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001428 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001429 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001430 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1431
1432 if( id1_arg != -1 )
1433 psa_set_key_id( &attributes, id1 );
1434 if( lifetime_arg != -1 )
1435 psa_set_key_lifetime( &attributes, lifetime );
1436 if( id2_arg != -1 )
1437 psa_set_key_id( &attributes, id2 );
1438
Ronald Cronecfb2372020-07-23 17:13:42 +02001439 TEST_ASSERT( mbedtls_svc_key_id_equal(
1440 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001441 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1442}
1443/* END_CASE */
1444
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001445/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1446void slot_number_attribute( )
1447{
1448 psa_key_slot_number_t slot_number = 0xdeadbeef;
1449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1450
1451 /* Initially, there is no slot number. */
1452 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1453 PSA_ERROR_INVALID_ARGUMENT );
1454
1455 /* Test setting a slot number. */
1456 psa_set_key_slot_number( &attributes, 0 );
1457 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1458 TEST_EQUAL( slot_number, 0 );
1459
1460 /* Test changing the slot number. */
1461 psa_set_key_slot_number( &attributes, 42 );
1462 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1463 TEST_EQUAL( slot_number, 42 );
1464
1465 /* Test clearing the slot number. */
1466 psa_clear_key_slot_number( &attributes );
1467 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1468 PSA_ERROR_INVALID_ARGUMENT );
1469
1470 /* Clearing again should have no effect. */
1471 psa_clear_key_slot_number( &attributes );
1472 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1473 PSA_ERROR_INVALID_ARGUMENT );
1474
1475 /* Test that reset clears the slot number. */
1476 psa_set_key_slot_number( &attributes, 42 );
1477 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1478 TEST_EQUAL( slot_number, 42 );
1479 psa_reset_key_attributes( &attributes );
1480 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1481 PSA_ERROR_INVALID_ARGUMENT );
1482}
1483/* END_CASE */
1484
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001485/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001486void import_with_policy( int type_arg,
1487 int usage_arg, int alg_arg,
1488 int expected_status_arg )
1489{
1490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1491 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001493 psa_key_type_t type = type_arg;
1494 psa_key_usage_t usage = usage_arg;
1495 psa_algorithm_t alg = alg_arg;
1496 psa_status_t expected_status = expected_status_arg;
1497 const uint8_t key_material[16] = {0};
1498 psa_status_t status;
1499
1500 PSA_ASSERT( psa_crypto_init( ) );
1501
1502 psa_set_key_type( &attributes, type );
1503 psa_set_key_usage_flags( &attributes, usage );
1504 psa_set_key_algorithm( &attributes, alg );
1505
1506 status = psa_import_key( &attributes,
1507 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001508 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001509 TEST_EQUAL( status, expected_status );
1510 if( status != PSA_SUCCESS )
1511 goto exit;
1512
Ronald Cron5425a212020-08-04 14:58:35 +02001513 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001514 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1515 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1516 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001517 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001518
Ronald Cron5425a212020-08-04 14:58:35 +02001519 PSA_ASSERT( psa_destroy_key( key ) );
1520 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001521
1522exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001523 /*
1524 * Key attributes may have been returned by psa_get_key_attributes()
1525 * thus reset them as required.
1526 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001527 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001528
1529 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001530 PSA_DONE( );
1531}
1532/* END_CASE */
1533
1534/* BEGIN_CASE */
1535void import_with_data( data_t *data, int type_arg,
1536 int attr_bits_arg,
1537 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001538{
1539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1540 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001541 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001542 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001543 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001544 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001546
Gilles Peskine8817f612018-12-18 00:18:46 +01001547 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001548
Gilles Peskine4747d192019-04-17 15:05:45 +02001549 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001550 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001551
Ronald Cron5425a212020-08-04 14:58:35 +02001552 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001553 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001554 if( status != PSA_SUCCESS )
1555 goto exit;
1556
Ronald Cron5425a212020-08-04 14:58:35 +02001557 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001558 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001559 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001560 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001561 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001562
Ronald Cron5425a212020-08-04 14:58:35 +02001563 PSA_ASSERT( psa_destroy_key( key ) );
1564 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001565
1566exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001567 /*
1568 * Key attributes may have been returned by psa_get_key_attributes()
1569 * thus reset them as required.
1570 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001571 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001572
1573 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001574 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001575}
1576/* END_CASE */
1577
1578/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001579void import_large_key( int type_arg, int byte_size_arg,
1580 int expected_status_arg )
1581{
1582 psa_key_type_t type = type_arg;
1583 size_t byte_size = byte_size_arg;
1584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1585 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001587 psa_status_t status;
1588 uint8_t *buffer = NULL;
1589 size_t buffer_size = byte_size + 1;
1590 size_t n;
1591
Steven Cooreman69967ce2021-01-18 18:01:08 +01001592 /* Skip the test case if the target running the test cannot
1593 * accomodate large keys due to heap size constraints */
1594 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001595 memset( buffer, 'K', byte_size );
1596
1597 PSA_ASSERT( psa_crypto_init( ) );
1598
1599 /* Try importing the key */
1600 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1601 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001602 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001603 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001604 TEST_EQUAL( status, expected_status );
1605
1606 if( status == PSA_SUCCESS )
1607 {
Ronald Cron5425a212020-08-04 14:58:35 +02001608 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001609 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1610 TEST_EQUAL( psa_get_key_bits( &attributes ),
1611 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001612 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001613 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001614 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001615 for( n = 0; n < byte_size; n++ )
1616 TEST_EQUAL( buffer[n], 'K' );
1617 for( n = byte_size; n < buffer_size; n++ )
1618 TEST_EQUAL( buffer[n], 0 );
1619 }
1620
1621exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001622 /*
1623 * Key attributes may have been returned by psa_get_key_attributes()
1624 * thus reset them as required.
1625 */
1626 psa_reset_key_attributes( &attributes );
1627
Ronald Cron5425a212020-08-04 14:58:35 +02001628 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001629 PSA_DONE( );
1630 mbedtls_free( buffer );
1631}
1632/* END_CASE */
1633
1634/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001635void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1636{
Ronald Cron5425a212020-08-04 14:58:35 +02001637 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001638 size_t bits = bits_arg;
1639 psa_status_t expected_status = expected_status_arg;
1640 psa_status_t status;
1641 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001642 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001643 size_t buffer_size = /* Slight overapproximations */
1644 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001645 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001646 unsigned char *p;
1647 int ret;
1648 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001650
Gilles Peskine8817f612018-12-18 00:18:46 +01001651 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001652 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001653
1654 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1655 bits, keypair ) ) >= 0 );
1656 length = ret;
1657
1658 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001659 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001660 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001661 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001662
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001663 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001664 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001665
1666exit:
1667 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001668 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001669}
1670/* END_CASE */
1671
1672/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001673void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001674 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001675 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001676 int expected_bits,
1677 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001678 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001679 int canonical_input )
1680{
Ronald Cron5425a212020-08-04 14:58:35 +02001681 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001682 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001683 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001684 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001685 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001686 unsigned char *exported = NULL;
1687 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001688 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001689 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001690 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001691 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001692 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001693
Moran Pekercb088e72018-07-17 17:36:59 +03001694 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001695 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001696 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001697 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001698 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001699
Gilles Peskine4747d192019-04-17 15:05:45 +02001700 psa_set_key_usage_flags( &attributes, usage_arg );
1701 psa_set_key_algorithm( &attributes, alg );
1702 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001703
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001704 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001705 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001706
1707 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001708 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001709 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1710 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001711 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001712
1713 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001714 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001715 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001716
1717 /* The exported length must be set by psa_export_key() to a value between 0
1718 * and export_size. On errors, the exported length must be 0. */
1719 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1720 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1721 TEST_ASSERT( exported_length <= export_size );
1722
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001723 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001724 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001725 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001726 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001727 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001728 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001729 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001730
Ronald Cron5425a212020-08-04 14:58:35 +02001731 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001732 goto exit;
1733
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001734 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001735 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001736 else
1737 {
Ronald Cron5425a212020-08-04 14:58:35 +02001738 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001739 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001740 &key2 ) );
1741 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001742 reexported,
1743 export_size,
1744 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001745 ASSERT_COMPARE( exported, exported_length,
1746 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001747 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001748 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001749 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001750
1751destroy:
1752 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001753 PSA_ASSERT( psa_destroy_key( key ) );
1754 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001755
1756exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001757 /*
1758 * Key attributes may have been returned by psa_get_key_attributes()
1759 * thus reset them as required.
1760 */
1761 psa_reset_key_attributes( &got_attributes );
1762
itayzafrir3e02b3b2018-06-12 17:06:52 +03001763 mbedtls_free( exported );
1764 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001765 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001766}
1767/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001768
Moran Pekerf709f4a2018-06-06 17:26:04 +03001769/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001770void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001771 int type_arg,
1772 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001773 int export_size_delta,
1774 int expected_export_status_arg,
1775 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001776{
Ronald Cron5425a212020-08-04 14:58:35 +02001777 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001778 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001779 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001780 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001781 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001782 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001783 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001784 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001786
Gilles Peskine8817f612018-12-18 00:18:46 +01001787 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001788
Gilles Peskine4747d192019-04-17 15:05:45 +02001789 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1790 psa_set_key_algorithm( &attributes, alg );
1791 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001792
1793 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001794 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001795
Gilles Peskine49c25912018-10-29 15:15:31 +01001796 /* Export the public key */
1797 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001798 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001799 exported, export_size,
1800 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001801 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001802 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001803 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001804 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001805 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001806 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001807 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001808 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001809 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001810 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1811 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001812 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001813
1814exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001815 /*
1816 * Key attributes may have been returned by psa_get_key_attributes()
1817 * thus reset them as required.
1818 */
1819 psa_reset_key_attributes( &attributes );
1820
itayzafrir3e02b3b2018-06-12 17:06:52 +03001821 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001822 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001823 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001824}
1825/* END_CASE */
1826
Gilles Peskine20035e32018-02-03 22:44:14 +01001827/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001828void import_and_exercise_key( data_t *data,
1829 int type_arg,
1830 int bits_arg,
1831 int alg_arg )
1832{
Ronald Cron5425a212020-08-04 14:58:35 +02001833 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001834 psa_key_type_t type = type_arg;
1835 size_t bits = bits_arg;
1836 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001837 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001838 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001839 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001840
Gilles Peskine8817f612018-12-18 00:18:46 +01001841 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001842
Gilles Peskine4747d192019-04-17 15:05:45 +02001843 psa_set_key_usage_flags( &attributes, usage );
1844 psa_set_key_algorithm( &attributes, alg );
1845 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001846
1847 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001848 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001849
1850 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001851 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001852 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1853 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001854
1855 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001856 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001857 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001858
Ronald Cron5425a212020-08-04 14:58:35 +02001859 PSA_ASSERT( psa_destroy_key( key ) );
1860 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001861
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001862exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001863 /*
1864 * Key attributes may have been returned by psa_get_key_attributes()
1865 * thus reset them as required.
1866 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001867 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001868
1869 psa_reset_key_attributes( &attributes );
1870 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001871 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001872}
1873/* END_CASE */
1874
1875/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001876void effective_key_attributes( int type_arg, int expected_type_arg,
1877 int bits_arg, int expected_bits_arg,
1878 int usage_arg, int expected_usage_arg,
1879 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001880{
Ronald Cron5425a212020-08-04 14:58:35 +02001881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001882 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001883 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001884 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001885 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001886 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001887 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001888 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001889 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001890 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001891
Gilles Peskine8817f612018-12-18 00:18:46 +01001892 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001893
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001894 psa_set_key_usage_flags( &attributes, usage );
1895 psa_set_key_algorithm( &attributes, alg );
1896 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001897 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001898
Ronald Cron5425a212020-08-04 14:58:35 +02001899 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001900 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001901
Ronald Cron5425a212020-08-04 14:58:35 +02001902 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001903 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1904 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1905 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1906 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001907
1908exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001909 /*
1910 * Key attributes may have been returned by psa_get_key_attributes()
1911 * thus reset them as required.
1912 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001913 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001914
1915 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001916 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001917}
1918/* END_CASE */
1919
1920/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001921void check_key_policy( int type_arg, int bits_arg,
1922 int usage_arg, int alg_arg )
1923{
1924 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1925 usage_arg, usage_arg, alg_arg, alg_arg );
1926 goto exit;
1927}
1928/* END_CASE */
1929
1930/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001931void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001932{
1933 /* Test each valid way of initializing the object, except for `= {0}`, as
1934 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1935 * though it's OK by the C standard. We could test for this, but we'd need
1936 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001937 psa_key_attributes_t func = psa_key_attributes_init( );
1938 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1939 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001940
1941 memset( &zero, 0, sizeof( zero ) );
1942
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001943 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1944 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1945 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001946
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001947 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1948 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1949 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1950
1951 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1952 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1953 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1954
1955 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1956 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1957 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1958
1959 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1960 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1961 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001962}
1963/* END_CASE */
1964
1965/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001966void mac_key_policy( int policy_usage,
1967 int policy_alg,
1968 int key_type,
1969 data_t *key_data,
1970 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001971{
Ronald Cron5425a212020-08-04 14:58:35 +02001972 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001973 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001974 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001975 psa_status_t status;
1976 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001977
Gilles Peskine8817f612018-12-18 00:18:46 +01001978 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001979
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001980 psa_set_key_usage_flags( &attributes, policy_usage );
1981 psa_set_key_algorithm( &attributes, policy_alg );
1982 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001983
Gilles Peskine049c7532019-05-15 20:22:09 +02001984 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001985 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001986
Ronald Cron5425a212020-08-04 14:58:35 +02001987 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001988 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001989 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001990 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001991 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001992 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001993 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001994
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001996 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001997 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001998 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001999 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002000 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002001 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002
2003exit:
2004 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002005 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002006 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002007}
2008/* END_CASE */
2009
2010/* BEGIN_CASE */
2011void cipher_key_policy( int policy_usage,
2012 int policy_alg,
2013 int key_type,
2014 data_t *key_data,
2015 int exercise_alg )
2016{
Ronald Cron5425a212020-08-04 14:58:35 +02002017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002019 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020 psa_status_t status;
2021
Gilles Peskine8817f612018-12-18 00:18:46 +01002022 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002024 psa_set_key_usage_flags( &attributes, policy_usage );
2025 psa_set_key_algorithm( &attributes, policy_alg );
2026 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002027
Gilles Peskine049c7532019-05-15 20:22:09 +02002028 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002029 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030
Ronald Cron5425a212020-08-04 14:58:35 +02002031 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032 if( policy_alg == exercise_alg &&
2033 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002034 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002036 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037 psa_cipher_abort( &operation );
2038
Ronald Cron5425a212020-08-04 14:58:35 +02002039 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002040 if( policy_alg == exercise_alg &&
2041 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002042 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002044 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002045
2046exit:
2047 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002048 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002049 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050}
2051/* END_CASE */
2052
2053/* BEGIN_CASE */
2054void aead_key_policy( int policy_usage,
2055 int policy_alg,
2056 int key_type,
2057 data_t *key_data,
2058 int nonce_length_arg,
2059 int tag_length_arg,
2060 int exercise_alg )
2061{
Ronald Cron5425a212020-08-04 14:58:35 +02002062 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002063 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002064 psa_status_t status;
2065 unsigned char nonce[16] = {0};
2066 size_t nonce_length = nonce_length_arg;
2067 unsigned char tag[16];
2068 size_t tag_length = tag_length_arg;
2069 size_t output_length;
2070
2071 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
2072 TEST_ASSERT( tag_length <= sizeof( tag ) );
2073
Gilles Peskine8817f612018-12-18 00:18:46 +01002074 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002076 psa_set_key_usage_flags( &attributes, policy_usage );
2077 psa_set_key_algorithm( &attributes, policy_alg );
2078 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079
Gilles Peskine049c7532019-05-15 20:22:09 +02002080 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002081 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002082
Ronald Cron5425a212020-08-04 14:58:35 +02002083 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084 nonce, nonce_length,
2085 NULL, 0,
2086 NULL, 0,
2087 tag, tag_length,
2088 &output_length );
2089 if( policy_alg == exercise_alg &&
2090 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002091 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002092 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002093 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094
2095 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002096 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002097 nonce, nonce_length,
2098 NULL, 0,
2099 tag, tag_length,
2100 NULL, 0,
2101 &output_length );
2102 if( policy_alg == exercise_alg &&
2103 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002104 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002105 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002106 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002107
2108exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002109 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002110 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002111}
2112/* END_CASE */
2113
2114/* BEGIN_CASE */
2115void asymmetric_encryption_key_policy( int policy_usage,
2116 int policy_alg,
2117 int key_type,
2118 data_t *key_data,
2119 int exercise_alg )
2120{
Ronald Cron5425a212020-08-04 14:58:35 +02002121 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123 psa_status_t status;
2124 size_t key_bits;
2125 size_t buffer_length;
2126 unsigned char *buffer = NULL;
2127 size_t output_length;
2128
Gilles Peskine8817f612018-12-18 00:18:46 +01002129 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002130
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002131 psa_set_key_usage_flags( &attributes, policy_usage );
2132 psa_set_key_algorithm( &attributes, policy_alg );
2133 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002134
Gilles Peskine049c7532019-05-15 20:22:09 +02002135 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002136 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002137
Ronald Cron5425a212020-08-04 14:58:35 +02002138 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002139 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002140 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2141 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002142 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002143
Ronald Cron5425a212020-08-04 14:58:35 +02002144 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145 NULL, 0,
2146 NULL, 0,
2147 buffer, buffer_length,
2148 &output_length );
2149 if( policy_alg == exercise_alg &&
2150 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002151 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002152 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002153 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002154
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002155 if( buffer_length != 0 )
2156 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002157 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002158 buffer, buffer_length,
2159 NULL, 0,
2160 buffer, buffer_length,
2161 &output_length );
2162 if( policy_alg == exercise_alg &&
2163 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002164 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002165 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002166 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002167
2168exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002169 /*
2170 * Key attributes may have been returned by psa_get_key_attributes()
2171 * thus reset them as required.
2172 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002173 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002174
2175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002176 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002177 mbedtls_free( buffer );
2178}
2179/* END_CASE */
2180
2181/* BEGIN_CASE */
2182void asymmetric_signature_key_policy( int policy_usage,
2183 int policy_alg,
2184 int key_type,
2185 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002186 int exercise_alg,
2187 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002188{
Ronald Cron5425a212020-08-04 14:58:35 +02002189 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002190 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002191 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002192 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2193 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2194 * compatible with the policy and `payload_length_arg` is supposed to be
2195 * a valid input length to sign. If `payload_length_arg <= 0`,
2196 * `exercise_alg` is supposed to be forbidden by the policy. */
2197 int compatible_alg = payload_length_arg > 0;
2198 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002199 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002200 size_t signature_length;
2201
Gilles Peskine8817f612018-12-18 00:18:46 +01002202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002203
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002204 psa_set_key_usage_flags( &attributes, policy_usage );
2205 psa_set_key_algorithm( &attributes, policy_alg );
2206 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002207
Gilles Peskine049c7532019-05-15 20:22:09 +02002208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002209 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002210
Ronald Cron5425a212020-08-04 14:58:35 +02002211 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002212 payload, payload_length,
2213 signature, sizeof( signature ),
2214 &signature_length );
2215 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002216 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002217 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002218 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002219
2220 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002221 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002222 payload, payload_length,
2223 signature, sizeof( signature ) );
2224 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002225 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002226 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002227 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002228
2229exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002230 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002231 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002232}
2233/* END_CASE */
2234
Janos Follathba3fab92019-06-11 14:50:16 +01002235/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002236void derive_key_policy( int policy_usage,
2237 int policy_alg,
2238 int key_type,
2239 data_t *key_data,
2240 int exercise_alg )
2241{
Ronald Cron5425a212020-08-04 14:58:35 +02002242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002243 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002244 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002245 psa_status_t status;
2246
Gilles Peskine8817f612018-12-18 00:18:46 +01002247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002248
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002249 psa_set_key_usage_flags( &attributes, policy_usage );
2250 psa_set_key_algorithm( &attributes, policy_alg );
2251 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002252
Gilles Peskine049c7532019-05-15 20:22:09 +02002253 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002254 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002255
Janos Follathba3fab92019-06-11 14:50:16 +01002256 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2257
2258 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2259 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002260 {
Janos Follathba3fab92019-06-11 14:50:16 +01002261 PSA_ASSERT( psa_key_derivation_input_bytes(
2262 &operation,
2263 PSA_KEY_DERIVATION_INPUT_SEED,
2264 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002265 }
Janos Follathba3fab92019-06-11 14:50:16 +01002266
2267 status = psa_key_derivation_input_key( &operation,
2268 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002269 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002270
Gilles Peskineea0fb492018-07-12 17:17:20 +02002271 if( policy_alg == exercise_alg &&
2272 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002273 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002274 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002275 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002276
2277exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002278 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002279 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002280 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002281}
2282/* END_CASE */
2283
2284/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002285void agreement_key_policy( int policy_usage,
2286 int policy_alg,
2287 int key_type_arg,
2288 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002289 int exercise_alg,
2290 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002291{
Ronald Cron5425a212020-08-04 14:58:35 +02002292 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002294 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002295 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002296 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002297 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002298
Gilles Peskine8817f612018-12-18 00:18:46 +01002299 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002300
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002301 psa_set_key_usage_flags( &attributes, policy_usage );
2302 psa_set_key_algorithm( &attributes, policy_alg );
2303 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002304
Gilles Peskine049c7532019-05-15 20:22:09 +02002305 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002306 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002307
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002308 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002309 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002310
Steven Cooremance48e852020-10-05 16:02:45 +02002311 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002312
2313exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002314 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002315 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002316 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002317}
2318/* END_CASE */
2319
2320/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002321void key_policy_alg2( int key_type_arg, data_t *key_data,
2322 int usage_arg, int alg_arg, int alg2_arg )
2323{
Ronald Cron5425a212020-08-04 14:58:35 +02002324 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002325 psa_key_type_t key_type = key_type_arg;
2326 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2327 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2328 psa_key_usage_t usage = usage_arg;
2329 psa_algorithm_t alg = alg_arg;
2330 psa_algorithm_t alg2 = alg2_arg;
2331
2332 PSA_ASSERT( psa_crypto_init( ) );
2333
2334 psa_set_key_usage_flags( &attributes, usage );
2335 psa_set_key_algorithm( &attributes, alg );
2336 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2337 psa_set_key_type( &attributes, key_type );
2338 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002339 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002340
Ronald Cron5425a212020-08-04 14:58:35 +02002341 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002342 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2343 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2344 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2345
Ronald Cron5425a212020-08-04 14:58:35 +02002346 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002347 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002348 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002349 goto exit;
2350
2351exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002352 /*
2353 * Key attributes may have been returned by psa_get_key_attributes()
2354 * thus reset them as required.
2355 */
2356 psa_reset_key_attributes( &got_attributes );
2357
Ronald Cron5425a212020-08-04 14:58:35 +02002358 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002359 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002360}
2361/* END_CASE */
2362
2363/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002364void raw_agreement_key_policy( int policy_usage,
2365 int policy_alg,
2366 int key_type_arg,
2367 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002368 int exercise_alg,
2369 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002370{
Ronald Cron5425a212020-08-04 14:58:35 +02002371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002373 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002374 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002375 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002376 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002377
2378 PSA_ASSERT( psa_crypto_init( ) );
2379
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002380 psa_set_key_usage_flags( &attributes, policy_usage );
2381 psa_set_key_algorithm( &attributes, policy_alg );
2382 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002383
Gilles Peskine049c7532019-05-15 20:22:09 +02002384 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002385 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002386
Ronald Cron5425a212020-08-04 14:58:35 +02002387 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002388
Steven Cooremance48e852020-10-05 16:02:45 +02002389 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002390
2391exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002392 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002393 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002394 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002395}
2396/* END_CASE */
2397
2398/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002399void copy_success( int source_usage_arg,
2400 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002401 int type_arg, data_t *material,
2402 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002403 int target_usage_arg,
2404 int target_alg_arg, int target_alg2_arg,
2405 int expected_usage_arg,
2406 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002407{
Gilles Peskineca25db92019-04-19 11:43:08 +02002408 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2409 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002410 psa_key_usage_t expected_usage = expected_usage_arg;
2411 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002412 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002413 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2414 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002415 uint8_t *export_buffer = NULL;
2416
Gilles Peskine57ab7212019-01-28 13:03:09 +01002417 PSA_ASSERT( psa_crypto_init( ) );
2418
Gilles Peskineca25db92019-04-19 11:43:08 +02002419 /* Prepare the source key. */
2420 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2421 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002422 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002423 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002424 PSA_ASSERT( psa_import_key( &source_attributes,
2425 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002426 &source_key ) );
2427 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002428
Gilles Peskineca25db92019-04-19 11:43:08 +02002429 /* Prepare the target attributes. */
2430 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002431 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002432 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002433 /* Set volatile lifetime to reset the key identifier to 0. */
2434 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2435 }
2436
Gilles Peskineca25db92019-04-19 11:43:08 +02002437 if( target_usage_arg != -1 )
2438 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2439 if( target_alg_arg != -1 )
2440 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002441 if( target_alg2_arg != -1 )
2442 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002443
2444 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002445 PSA_ASSERT( psa_copy_key( source_key,
2446 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002447
2448 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002449 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002450
2451 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002452 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002453 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2454 psa_get_key_type( &target_attributes ) );
2455 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2456 psa_get_key_bits( &target_attributes ) );
2457 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2458 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002459 TEST_EQUAL( expected_alg2,
2460 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002461 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2462 {
2463 size_t length;
2464 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002465 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002466 material->len, &length ) );
2467 ASSERT_COMPARE( material->x, material->len,
2468 export_buffer, length );
2469 }
Ronald Cron5425a212020-08-04 14:58:35 +02002470 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002471 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002472 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002473 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002474
Ronald Cron5425a212020-08-04 14:58:35 +02002475 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002476
2477exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002478 /*
2479 * Source and target key attributes may have been returned by
2480 * psa_get_key_attributes() thus reset them as required.
2481 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002482 psa_reset_key_attributes( &source_attributes );
2483 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002484
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002485 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002486 mbedtls_free( export_buffer );
2487}
2488/* END_CASE */
2489
2490/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002491void copy_fail( int source_usage_arg,
2492 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002493 int type_arg, data_t *material,
2494 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002495 int target_usage_arg,
2496 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002497 int expected_status_arg )
2498{
2499 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2500 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002501 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2502 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002503
2504 PSA_ASSERT( psa_crypto_init( ) );
2505
2506 /* Prepare the source key. */
2507 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2508 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002509 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002510 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002511 PSA_ASSERT( psa_import_key( &source_attributes,
2512 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002513 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002514
2515 /* Prepare the target attributes. */
2516 psa_set_key_type( &target_attributes, target_type_arg );
2517 psa_set_key_bits( &target_attributes, target_bits_arg );
2518 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2519 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002520 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002521
2522 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002523 TEST_EQUAL( psa_copy_key( source_key,
2524 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002525 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002526
Ronald Cron5425a212020-08-04 14:58:35 +02002527 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002528
Gilles Peskine4a644642019-05-03 17:14:08 +02002529exit:
2530 psa_reset_key_attributes( &source_attributes );
2531 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002532 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002533}
2534/* END_CASE */
2535
2536/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002537void hash_operation_init( )
2538{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002539 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002540 /* Test each valid way of initializing the object, except for `= {0}`, as
2541 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2542 * though it's OK by the C standard. We could test for this, but we'd need
2543 * to supress the Clang warning for the test. */
2544 psa_hash_operation_t func = psa_hash_operation_init( );
2545 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2546 psa_hash_operation_t zero;
2547
2548 memset( &zero, 0, sizeof( zero ) );
2549
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002550 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002551 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2552 PSA_ERROR_BAD_STATE );
2553 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2554 PSA_ERROR_BAD_STATE );
2555 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2556 PSA_ERROR_BAD_STATE );
2557
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002558 /* A default hash operation should be abortable without error. */
2559 PSA_ASSERT( psa_hash_abort( &func ) );
2560 PSA_ASSERT( psa_hash_abort( &init ) );
2561 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002562}
2563/* END_CASE */
2564
2565/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002566void hash_setup( int alg_arg,
2567 int expected_status_arg )
2568{
2569 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002570 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002571 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002572 psa_status_t status;
2573
Gilles Peskine8817f612018-12-18 00:18:46 +01002574 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002575
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002576 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002577 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002578
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002579 /* Whether setup succeeded or failed, abort must succeed. */
2580 PSA_ASSERT( psa_hash_abort( &operation ) );
2581
2582 /* If setup failed, reproduce the failure, so as to
2583 * test the resulting state of the operation object. */
2584 if( status != PSA_SUCCESS )
2585 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2586
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002587 /* Now the operation object should be reusable. */
2588#if defined(KNOWN_SUPPORTED_HASH_ALG)
2589 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2590 PSA_ASSERT( psa_hash_abort( &operation ) );
2591#endif
2592
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002593exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002594 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002595}
2596/* END_CASE */
2597
2598/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002599void hash_compute_fail( int alg_arg, data_t *input,
2600 int output_size_arg, int expected_status_arg )
2601{
2602 psa_algorithm_t alg = alg_arg;
2603 uint8_t *output = NULL;
2604 size_t output_size = output_size_arg;
2605 size_t output_length = INVALID_EXPORT_LENGTH;
2606 psa_status_t expected_status = expected_status_arg;
2607 psa_status_t status;
2608
2609 ASSERT_ALLOC( output, output_size );
2610
2611 PSA_ASSERT( psa_crypto_init( ) );
2612
2613 status = psa_hash_compute( alg, input->x, input->len,
2614 output, output_size, &output_length );
2615 TEST_EQUAL( status, expected_status );
2616 TEST_ASSERT( output_length <= output_size );
2617
2618exit:
2619 mbedtls_free( output );
2620 PSA_DONE( );
2621}
2622/* END_CASE */
2623
2624/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002625void hash_compare_fail( int alg_arg, data_t *input,
2626 data_t *reference_hash,
2627 int expected_status_arg )
2628{
2629 psa_algorithm_t alg = alg_arg;
2630 psa_status_t expected_status = expected_status_arg;
2631 psa_status_t status;
2632
2633 PSA_ASSERT( psa_crypto_init( ) );
2634
2635 status = psa_hash_compare( alg, input->x, input->len,
2636 reference_hash->x, reference_hash->len );
2637 TEST_EQUAL( status, expected_status );
2638
2639exit:
2640 PSA_DONE( );
2641}
2642/* END_CASE */
2643
2644/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002645void hash_compute_compare( int alg_arg, data_t *input,
2646 data_t *expected_output )
2647{
2648 psa_algorithm_t alg = alg_arg;
2649 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2650 size_t output_length = INVALID_EXPORT_LENGTH;
2651 size_t i;
2652
2653 PSA_ASSERT( psa_crypto_init( ) );
2654
2655 /* Compute with tight buffer */
2656 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002657 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002658 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002659 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002660 ASSERT_COMPARE( output, output_length,
2661 expected_output->x, expected_output->len );
2662
2663 /* Compute with larger buffer */
2664 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2665 output, sizeof( output ),
2666 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002667 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002668 ASSERT_COMPARE( output, output_length,
2669 expected_output->x, expected_output->len );
2670
2671 /* Compare with correct hash */
2672 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2673 output, output_length ) );
2674
2675 /* Compare with trailing garbage */
2676 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2677 output, output_length + 1 ),
2678 PSA_ERROR_INVALID_SIGNATURE );
2679
2680 /* Compare with truncated hash */
2681 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2682 output, output_length - 1 ),
2683 PSA_ERROR_INVALID_SIGNATURE );
2684
2685 /* Compare with corrupted value */
2686 for( i = 0; i < output_length; i++ )
2687 {
Chris Jones9634bb12021-01-20 15:56:42 +00002688 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002689 output[i] ^= 1;
2690 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2691 output, output_length ),
2692 PSA_ERROR_INVALID_SIGNATURE );
2693 output[i] ^= 1;
2694 }
2695
2696exit:
2697 PSA_DONE( );
2698}
2699/* END_CASE */
2700
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002701/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002702void hash_bad_order( )
2703{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002704 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002705 unsigned char input[] = "";
2706 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002707 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002708 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2709 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2710 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002711 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002712 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002713 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002714
Gilles Peskine8817f612018-12-18 00:18:46 +01002715 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002716
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002717 /* Call setup twice in a row. */
2718 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2719 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2720 PSA_ERROR_BAD_STATE );
2721 PSA_ASSERT( psa_hash_abort( &operation ) );
2722
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002723 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002724 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002725 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002726 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002727
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002728 /* Call update after finish. */
2729 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2730 PSA_ASSERT( psa_hash_finish( &operation,
2731 hash, sizeof( hash ), &hash_len ) );
2732 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002733 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002734 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002735
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002736 /* Call verify without calling setup beforehand. */
2737 TEST_EQUAL( psa_hash_verify( &operation,
2738 valid_hash, sizeof( valid_hash ) ),
2739 PSA_ERROR_BAD_STATE );
2740 PSA_ASSERT( psa_hash_abort( &operation ) );
2741
2742 /* Call verify after finish. */
2743 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2744 PSA_ASSERT( psa_hash_finish( &operation,
2745 hash, sizeof( hash ), &hash_len ) );
2746 TEST_EQUAL( psa_hash_verify( &operation,
2747 valid_hash, sizeof( valid_hash ) ),
2748 PSA_ERROR_BAD_STATE );
2749 PSA_ASSERT( psa_hash_abort( &operation ) );
2750
2751 /* Call verify twice in a row. */
2752 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2753 PSA_ASSERT( psa_hash_verify( &operation,
2754 valid_hash, sizeof( valid_hash ) ) );
2755 TEST_EQUAL( psa_hash_verify( &operation,
2756 valid_hash, sizeof( valid_hash ) ),
2757 PSA_ERROR_BAD_STATE );
2758 PSA_ASSERT( psa_hash_abort( &operation ) );
2759
2760 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002761 TEST_EQUAL( psa_hash_finish( &operation,
2762 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002763 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002764 PSA_ASSERT( psa_hash_abort( &operation ) );
2765
2766 /* Call finish twice in a row. */
2767 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2768 PSA_ASSERT( psa_hash_finish( &operation,
2769 hash, sizeof( hash ), &hash_len ) );
2770 TEST_EQUAL( psa_hash_finish( &operation,
2771 hash, sizeof( hash ), &hash_len ),
2772 PSA_ERROR_BAD_STATE );
2773 PSA_ASSERT( psa_hash_abort( &operation ) );
2774
2775 /* Call finish after calling verify. */
2776 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2777 PSA_ASSERT( psa_hash_verify( &operation,
2778 valid_hash, sizeof( valid_hash ) ) );
2779 TEST_EQUAL( psa_hash_finish( &operation,
2780 hash, sizeof( hash ), &hash_len ),
2781 PSA_ERROR_BAD_STATE );
2782 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002783
2784exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002785 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002786}
2787/* END_CASE */
2788
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002789/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002790void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002791{
2792 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002793 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2794 * appended to it */
2795 unsigned char hash[] = {
2796 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2797 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2798 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002799 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002800 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002801
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002803
itayzafrir27e69452018-11-01 14:26:34 +02002804 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002805 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002806 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002807 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002808
itayzafrir27e69452018-11-01 14:26:34 +02002809 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002810 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002811 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002812 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002813
itayzafrir27e69452018-11-01 14:26:34 +02002814 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002815 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002816 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002817 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002818
itayzafrirec93d302018-10-18 18:01:10 +03002819exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002820 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002821}
2822/* END_CASE */
2823
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002824/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2825void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002826{
2827 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002828 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002829 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002830 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002831 size_t hash_len;
2832
Gilles Peskine8817f612018-12-18 00:18:46 +01002833 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002834
itayzafrir58028322018-10-25 10:22:01 +03002835 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002836 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002837 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002838 hash, expected_size - 1, &hash_len ),
2839 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002840
2841exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002842 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002843}
2844/* END_CASE */
2845
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002846/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2847void hash_clone_source_state( )
2848{
2849 psa_algorithm_t alg = PSA_ALG_SHA_256;
2850 unsigned char hash[PSA_HASH_MAX_SIZE];
2851 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2852 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2853 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2854 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2855 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2856 size_t hash_len;
2857
2858 PSA_ASSERT( psa_crypto_init( ) );
2859 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2860
2861 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2862 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2863 PSA_ASSERT( psa_hash_finish( &op_finished,
2864 hash, sizeof( hash ), &hash_len ) );
2865 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2866 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2867
2868 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2869 PSA_ERROR_BAD_STATE );
2870
2871 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2872 PSA_ASSERT( psa_hash_finish( &op_init,
2873 hash, sizeof( hash ), &hash_len ) );
2874 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2875 PSA_ASSERT( psa_hash_finish( &op_finished,
2876 hash, sizeof( hash ), &hash_len ) );
2877 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2878 PSA_ASSERT( psa_hash_finish( &op_aborted,
2879 hash, sizeof( hash ), &hash_len ) );
2880
2881exit:
2882 psa_hash_abort( &op_source );
2883 psa_hash_abort( &op_init );
2884 psa_hash_abort( &op_setup );
2885 psa_hash_abort( &op_finished );
2886 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002887 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002888}
2889/* END_CASE */
2890
2891/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2892void hash_clone_target_state( )
2893{
2894 psa_algorithm_t alg = PSA_ALG_SHA_256;
2895 unsigned char hash[PSA_HASH_MAX_SIZE];
2896 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2897 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2898 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2899 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2900 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2901 size_t hash_len;
2902
2903 PSA_ASSERT( psa_crypto_init( ) );
2904
2905 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2906 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2907 PSA_ASSERT( psa_hash_finish( &op_finished,
2908 hash, sizeof( hash ), &hash_len ) );
2909 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2910 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2911
2912 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2913 PSA_ASSERT( psa_hash_finish( &op_target,
2914 hash, sizeof( hash ), &hash_len ) );
2915
2916 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2917 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2918 PSA_ERROR_BAD_STATE );
2919 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2920 PSA_ERROR_BAD_STATE );
2921
2922exit:
2923 psa_hash_abort( &op_target );
2924 psa_hash_abort( &op_init );
2925 psa_hash_abort( &op_setup );
2926 psa_hash_abort( &op_finished );
2927 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002928 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002929}
2930/* END_CASE */
2931
itayzafrir58028322018-10-25 10:22:01 +03002932/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002933void mac_operation_init( )
2934{
Jaeden Amero252ef282019-02-15 14:05:35 +00002935 const uint8_t input[1] = { 0 };
2936
Jaeden Amero769ce272019-01-04 11:48:03 +00002937 /* Test each valid way of initializing the object, except for `= {0}`, as
2938 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2939 * though it's OK by the C standard. We could test for this, but we'd need
2940 * to supress the Clang warning for the test. */
2941 psa_mac_operation_t func = psa_mac_operation_init( );
2942 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2943 psa_mac_operation_t zero;
2944
2945 memset( &zero, 0, sizeof( zero ) );
2946
Jaeden Amero252ef282019-02-15 14:05:35 +00002947 /* A freshly-initialized MAC operation should not be usable. */
2948 TEST_EQUAL( psa_mac_update( &func,
2949 input, sizeof( input ) ),
2950 PSA_ERROR_BAD_STATE );
2951 TEST_EQUAL( psa_mac_update( &init,
2952 input, sizeof( input ) ),
2953 PSA_ERROR_BAD_STATE );
2954 TEST_EQUAL( psa_mac_update( &zero,
2955 input, sizeof( input ) ),
2956 PSA_ERROR_BAD_STATE );
2957
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002958 /* A default MAC operation should be abortable without error. */
2959 PSA_ASSERT( psa_mac_abort( &func ) );
2960 PSA_ASSERT( psa_mac_abort( &init ) );
2961 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002962}
2963/* END_CASE */
2964
2965/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002966void mac_setup( int key_type_arg,
2967 data_t *key,
2968 int alg_arg,
2969 int expected_status_arg )
2970{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002971 psa_key_type_t key_type = key_type_arg;
2972 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002973 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002974 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002975 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2976#if defined(KNOWN_SUPPORTED_MAC_ALG)
2977 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2978#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002979
Gilles Peskine8817f612018-12-18 00:18:46 +01002980 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002981
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002982 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2983 &operation, &status ) )
2984 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002985 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002986
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002987 /* The operation object should be reusable. */
2988#if defined(KNOWN_SUPPORTED_MAC_ALG)
2989 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2990 smoke_test_key_data,
2991 sizeof( smoke_test_key_data ),
2992 KNOWN_SUPPORTED_MAC_ALG,
2993 &operation, &status ) )
2994 goto exit;
2995 TEST_EQUAL( status, PSA_SUCCESS );
2996#endif
2997
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002998exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002999 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003000}
3001/* END_CASE */
3002
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003003/* 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 +00003004void mac_bad_order( )
3005{
Ronald Cron5425a212020-08-04 14:58:35 +02003006 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003007 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3008 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003009 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003010 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3011 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3012 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003014 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3015 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3016 size_t sign_mac_length = 0;
3017 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3018 const uint8_t verify_mac[] = {
3019 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3020 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3021 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3022
3023 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003024 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003025 psa_set_key_algorithm( &attributes, alg );
3026 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003027
Ronald Cron5425a212020-08-04 14:58:35 +02003028 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3029 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003030
Jaeden Amero252ef282019-02-15 14:05:35 +00003031 /* Call update without calling setup beforehand. */
3032 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3033 PSA_ERROR_BAD_STATE );
3034 PSA_ASSERT( psa_mac_abort( &operation ) );
3035
3036 /* Call sign finish without calling setup beforehand. */
3037 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3038 &sign_mac_length),
3039 PSA_ERROR_BAD_STATE );
3040 PSA_ASSERT( psa_mac_abort( &operation ) );
3041
3042 /* Call verify finish without calling setup beforehand. */
3043 TEST_EQUAL( psa_mac_verify_finish( &operation,
3044 verify_mac, sizeof( verify_mac ) ),
3045 PSA_ERROR_BAD_STATE );
3046 PSA_ASSERT( psa_mac_abort( &operation ) );
3047
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003048 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003049 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
3050 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003051 PSA_ERROR_BAD_STATE );
3052 PSA_ASSERT( psa_mac_abort( &operation ) );
3053
Jaeden Amero252ef282019-02-15 14:05:35 +00003054 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003055 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003056 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3057 PSA_ASSERT( psa_mac_sign_finish( &operation,
3058 sign_mac, sizeof( sign_mac ),
3059 &sign_mac_length ) );
3060 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3061 PSA_ERROR_BAD_STATE );
3062 PSA_ASSERT( psa_mac_abort( &operation ) );
3063
3064 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003065 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003066 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3067 PSA_ASSERT( psa_mac_verify_finish( &operation,
3068 verify_mac, sizeof( verify_mac ) ) );
3069 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3070 PSA_ERROR_BAD_STATE );
3071 PSA_ASSERT( psa_mac_abort( &operation ) );
3072
3073 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003074 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003075 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3076 PSA_ASSERT( psa_mac_sign_finish( &operation,
3077 sign_mac, sizeof( sign_mac ),
3078 &sign_mac_length ) );
3079 TEST_EQUAL( psa_mac_sign_finish( &operation,
3080 sign_mac, sizeof( sign_mac ),
3081 &sign_mac_length ),
3082 PSA_ERROR_BAD_STATE );
3083 PSA_ASSERT( psa_mac_abort( &operation ) );
3084
3085 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003086 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003087 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3088 PSA_ASSERT( psa_mac_verify_finish( &operation,
3089 verify_mac, sizeof( verify_mac ) ) );
3090 TEST_EQUAL( psa_mac_verify_finish( &operation,
3091 verify_mac, sizeof( verify_mac ) ),
3092 PSA_ERROR_BAD_STATE );
3093 PSA_ASSERT( psa_mac_abort( &operation ) );
3094
3095 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003096 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003097 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3098 TEST_EQUAL( psa_mac_verify_finish( &operation,
3099 verify_mac, sizeof( verify_mac ) ),
3100 PSA_ERROR_BAD_STATE );
3101 PSA_ASSERT( psa_mac_abort( &operation ) );
3102
3103 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003104 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003105 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3106 TEST_EQUAL( psa_mac_sign_finish( &operation,
3107 sign_mac, sizeof( sign_mac ),
3108 &sign_mac_length ),
3109 PSA_ERROR_BAD_STATE );
3110 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003111
Ronald Cron5425a212020-08-04 14:58:35 +02003112 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003113
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003114exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003115 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003116}
3117/* END_CASE */
3118
3119/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003120void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003121 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003122 int alg_arg,
3123 data_t *input,
3124 data_t *expected_mac )
3125{
Ronald Cron5425a212020-08-04 14:58:35 +02003126 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003127 psa_key_type_t key_type = key_type_arg;
3128 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003129 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003130 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003131 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003132 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003133 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003134 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003135 const size_t output_sizes_to_test[] = {
3136 0,
3137 1,
3138 expected_mac->len - 1,
3139 expected_mac->len,
3140 expected_mac->len + 1,
3141 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003142
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003143 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003144 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003145 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003146
Gilles Peskine8817f612018-12-18 00:18:46 +01003147 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003148
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003149 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003150 psa_set_key_algorithm( &attributes, alg );
3151 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003152
Ronald Cron5425a212020-08-04 14:58:35 +02003153 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3154 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003155
Gilles Peskine8b356b52020-08-25 23:44:59 +02003156 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3157 {
3158 const size_t output_size = output_sizes_to_test[i];
3159 psa_status_t expected_status =
3160 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3161 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003162
Chris Jones9634bb12021-01-20 15:56:42 +00003163 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003164 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003165
Gilles Peskine8b356b52020-08-25 23:44:59 +02003166 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003167 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003168 PSA_ASSERT( psa_mac_update( &operation,
3169 input->x, input->len ) );
3170 TEST_EQUAL( psa_mac_sign_finish( &operation,
3171 actual_mac, output_size,
3172 &mac_length ),
3173 expected_status );
3174 PSA_ASSERT( psa_mac_abort( &operation ) );
3175
3176 if( expected_status == PSA_SUCCESS )
3177 {
3178 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3179 actual_mac, mac_length );
3180 }
3181 mbedtls_free( actual_mac );
3182 actual_mac = NULL;
3183 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003184
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003185exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003186 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003187 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003188 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003189 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003190}
3191/* END_CASE */
3192
3193/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003194void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003195 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003196 int alg_arg,
3197 data_t *input,
3198 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003199{
Ronald Cron5425a212020-08-04 14:58:35 +02003200 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003201 psa_key_type_t key_type = key_type_arg;
3202 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003203 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003204 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003205 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003206
Gilles Peskine69c12672018-06-28 00:07:19 +02003207 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3208
Gilles Peskine8817f612018-12-18 00:18:46 +01003209 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003210
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003211 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003212 psa_set_key_algorithm( &attributes, alg );
3213 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003214
Ronald Cron5425a212020-08-04 14:58:35 +02003215 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3216 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003217
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003218 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003219 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_mac_update( &operation,
3221 input->x, input->len ) );
3222 PSA_ASSERT( psa_mac_verify_finish( &operation,
3223 expected_mac->x,
3224 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003225
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003226 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003227 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003228 PSA_ASSERT( psa_mac_update( &operation,
3229 input->x, input->len ) );
3230 TEST_EQUAL( psa_mac_verify_finish( &operation,
3231 expected_mac->x,
3232 expected_mac->len - 1 ),
3233 PSA_ERROR_INVALID_SIGNATURE );
3234
3235 /* Test a MAC that's too long. */
3236 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3237 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003238 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003239 PSA_ASSERT( psa_mac_update( &operation,
3240 input->x, input->len ) );
3241 TEST_EQUAL( psa_mac_verify_finish( &operation,
3242 perturbed_mac,
3243 expected_mac->len + 1 ),
3244 PSA_ERROR_INVALID_SIGNATURE );
3245
3246 /* Test changing one byte. */
3247 for( size_t i = 0; i < expected_mac->len; i++ )
3248 {
Chris Jones9634bb12021-01-20 15:56:42 +00003249 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003250 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003251 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003252 PSA_ASSERT( psa_mac_update( &operation,
3253 input->x, input->len ) );
3254 TEST_EQUAL( psa_mac_verify_finish( &operation,
3255 perturbed_mac,
3256 expected_mac->len ),
3257 PSA_ERROR_INVALID_SIGNATURE );
3258 perturbed_mac[i] ^= 1;
3259 }
3260
Gilles Peskine8c9def32018-02-08 10:02:12 +01003261exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003262 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003263 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003264 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003265 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003266}
3267/* END_CASE */
3268
3269/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003270void cipher_operation_init( )
3271{
Jaeden Ameroab439972019-02-15 14:12:05 +00003272 const uint8_t input[1] = { 0 };
3273 unsigned char output[1] = { 0 };
3274 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003275 /* Test each valid way of initializing the object, except for `= {0}`, as
3276 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3277 * though it's OK by the C standard. We could test for this, but we'd need
3278 * to supress the Clang warning for the test. */
3279 psa_cipher_operation_t func = psa_cipher_operation_init( );
3280 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3281 psa_cipher_operation_t zero;
3282
3283 memset( &zero, 0, sizeof( zero ) );
3284
Jaeden Ameroab439972019-02-15 14:12:05 +00003285 /* A freshly-initialized cipher operation should not be usable. */
3286 TEST_EQUAL( psa_cipher_update( &func,
3287 input, sizeof( input ),
3288 output, sizeof( output ),
3289 &output_length ),
3290 PSA_ERROR_BAD_STATE );
3291 TEST_EQUAL( psa_cipher_update( &init,
3292 input, sizeof( input ),
3293 output, sizeof( output ),
3294 &output_length ),
3295 PSA_ERROR_BAD_STATE );
3296 TEST_EQUAL( psa_cipher_update( &zero,
3297 input, sizeof( input ),
3298 output, sizeof( output ),
3299 &output_length ),
3300 PSA_ERROR_BAD_STATE );
3301
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003302 /* A default cipher operation should be abortable without error. */
3303 PSA_ASSERT( psa_cipher_abort( &func ) );
3304 PSA_ASSERT( psa_cipher_abort( &init ) );
3305 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003306}
3307/* END_CASE */
3308
3309/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003310void cipher_setup( int key_type_arg,
3311 data_t *key,
3312 int alg_arg,
3313 int expected_status_arg )
3314{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003315 psa_key_type_t key_type = key_type_arg;
3316 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003317 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003318 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003319 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003320#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003321 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3322#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003323
Gilles Peskine8817f612018-12-18 00:18:46 +01003324 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003325
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003326 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3327 &operation, &status ) )
3328 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003329 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003330
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003331 /* The operation object should be reusable. */
3332#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3333 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3334 smoke_test_key_data,
3335 sizeof( smoke_test_key_data ),
3336 KNOWN_SUPPORTED_CIPHER_ALG,
3337 &operation, &status ) )
3338 goto exit;
3339 TEST_EQUAL( status, PSA_SUCCESS );
3340#endif
3341
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003342exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003343 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003344 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003345}
3346/* END_CASE */
3347
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003348/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003349void cipher_bad_order( )
3350{
Ronald Cron5425a212020-08-04 14:58:35 +02003351 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003352 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3353 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003354 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003355 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003356 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003357 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003358 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3359 0xaa, 0xaa, 0xaa, 0xaa };
3360 const uint8_t text[] = {
3361 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3362 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003363 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003364 size_t length = 0;
3365
3366 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003367 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3368 psa_set_key_algorithm( &attributes, alg );
3369 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003370 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3371 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003372
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003373 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003374 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3375 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003376 PSA_ERROR_BAD_STATE );
3377 PSA_ASSERT( psa_cipher_abort( &operation ) );
3378
3379 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003380 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3381 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003382 PSA_ERROR_BAD_STATE );
3383 PSA_ASSERT( psa_cipher_abort( &operation ) );
3384
Jaeden Ameroab439972019-02-15 14:12:05 +00003385 /* Generate an IV without calling setup beforehand. */
3386 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3387 buffer, sizeof( buffer ),
3388 &length ),
3389 PSA_ERROR_BAD_STATE );
3390 PSA_ASSERT( psa_cipher_abort( &operation ) );
3391
3392 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003393 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003394 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3395 buffer, sizeof( buffer ),
3396 &length ) );
3397 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3398 buffer, sizeof( buffer ),
3399 &length ),
3400 PSA_ERROR_BAD_STATE );
3401 PSA_ASSERT( psa_cipher_abort( &operation ) );
3402
3403 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003404 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003405 PSA_ASSERT( psa_cipher_set_iv( &operation,
3406 iv, sizeof( iv ) ) );
3407 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3408 buffer, sizeof( buffer ),
3409 &length ),
3410 PSA_ERROR_BAD_STATE );
3411 PSA_ASSERT( psa_cipher_abort( &operation ) );
3412
3413 /* Set an IV without calling setup beforehand. */
3414 TEST_EQUAL( psa_cipher_set_iv( &operation,
3415 iv, sizeof( iv ) ),
3416 PSA_ERROR_BAD_STATE );
3417 PSA_ASSERT( psa_cipher_abort( &operation ) );
3418
3419 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003420 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003421 PSA_ASSERT( psa_cipher_set_iv( &operation,
3422 iv, sizeof( iv ) ) );
3423 TEST_EQUAL( psa_cipher_set_iv( &operation,
3424 iv, sizeof( iv ) ),
3425 PSA_ERROR_BAD_STATE );
3426 PSA_ASSERT( psa_cipher_abort( &operation ) );
3427
3428 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003429 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003430 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3431 buffer, sizeof( buffer ),
3432 &length ) );
3433 TEST_EQUAL( psa_cipher_set_iv( &operation,
3434 iv, sizeof( iv ) ),
3435 PSA_ERROR_BAD_STATE );
3436 PSA_ASSERT( psa_cipher_abort( &operation ) );
3437
3438 /* Call update without calling setup beforehand. */
3439 TEST_EQUAL( psa_cipher_update( &operation,
3440 text, sizeof( text ),
3441 buffer, sizeof( buffer ),
3442 &length ),
3443 PSA_ERROR_BAD_STATE );
3444 PSA_ASSERT( psa_cipher_abort( &operation ) );
3445
3446 /* Call update without an IV where an IV is required. */
3447 TEST_EQUAL( psa_cipher_update( &operation,
3448 text, sizeof( text ),
3449 buffer, sizeof( buffer ),
3450 &length ),
3451 PSA_ERROR_BAD_STATE );
3452 PSA_ASSERT( psa_cipher_abort( &operation ) );
3453
3454 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003455 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003456 PSA_ASSERT( psa_cipher_set_iv( &operation,
3457 iv, sizeof( iv ) ) );
3458 PSA_ASSERT( psa_cipher_finish( &operation,
3459 buffer, sizeof( buffer ), &length ) );
3460 TEST_EQUAL( psa_cipher_update( &operation,
3461 text, sizeof( text ),
3462 buffer, sizeof( buffer ),
3463 &length ),
3464 PSA_ERROR_BAD_STATE );
3465 PSA_ASSERT( psa_cipher_abort( &operation ) );
3466
3467 /* Call finish without calling setup beforehand. */
3468 TEST_EQUAL( psa_cipher_finish( &operation,
3469 buffer, sizeof( buffer ), &length ),
3470 PSA_ERROR_BAD_STATE );
3471 PSA_ASSERT( psa_cipher_abort( &operation ) );
3472
3473 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003474 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003475 /* Not calling update means we are encrypting an empty buffer, which is OK
3476 * for cipher modes with padding. */
3477 TEST_EQUAL( psa_cipher_finish( &operation,
3478 buffer, sizeof( buffer ), &length ),
3479 PSA_ERROR_BAD_STATE );
3480 PSA_ASSERT( psa_cipher_abort( &operation ) );
3481
3482 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003483 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003484 PSA_ASSERT( psa_cipher_set_iv( &operation,
3485 iv, sizeof( iv ) ) );
3486 PSA_ASSERT( psa_cipher_finish( &operation,
3487 buffer, sizeof( buffer ), &length ) );
3488 TEST_EQUAL( psa_cipher_finish( &operation,
3489 buffer, sizeof( buffer ), &length ),
3490 PSA_ERROR_BAD_STATE );
3491 PSA_ASSERT( psa_cipher_abort( &operation ) );
3492
Ronald Cron5425a212020-08-04 14:58:35 +02003493 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003494
Jaeden Ameroab439972019-02-15 14:12:05 +00003495exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003496 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003497 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003498}
3499/* END_CASE */
3500
3501/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003502void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003503 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003504 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003505 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003506{
Ronald Cron5425a212020-08-04 14:58:35 +02003507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003508 psa_status_t status;
3509 psa_key_type_t key_type = key_type_arg;
3510 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003511 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003512 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003513 size_t output_buffer_size = 0;
3514 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003515 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003516 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003517 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003518
Gilles Peskine8817f612018-12-18 00:18:46 +01003519 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003520
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003521 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3522 psa_set_key_algorithm( &attributes, alg );
3523 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003524
Ronald Cron5425a212020-08-04 14:58:35 +02003525 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3526 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003527
Ronald Cron5425a212020-08-04 14:58:35 +02003528 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003529
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003530 if( iv->len > 0 )
3531 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003532 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003533 }
3534
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003535 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003536 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003537 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003538
Gilles Peskine8817f612018-12-18 00:18:46 +01003539 PSA_ASSERT( psa_cipher_update( &operation,
3540 input->x, input->len,
3541 output, output_buffer_size,
3542 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003543 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003544 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003545 output + total_output_length,
3546 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003547 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003548 total_output_length += function_output_length;
3549
Gilles Peskinefe11b722018-12-18 00:24:04 +01003550 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003551 if( expected_status == PSA_SUCCESS )
3552 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003553 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003554 ASSERT_COMPARE( expected_output->x, expected_output->len,
3555 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003556 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003557
Gilles Peskine50e586b2018-06-08 14:28:46 +02003558exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003559 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003560 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003561 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003562 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003563}
3564/* END_CASE */
3565
3566/* BEGIN_CASE */
3567void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003568 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003569 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003570 int first_part_size_arg,
3571 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003572 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003573{
Ronald Cron5425a212020-08-04 14:58:35 +02003574 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003575 psa_key_type_t key_type = key_type_arg;
3576 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003577 size_t first_part_size = first_part_size_arg;
3578 size_t output1_length = output1_length_arg;
3579 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003580 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003581 size_t output_buffer_size = 0;
3582 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003583 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003584 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003586
Gilles Peskine8817f612018-12-18 00:18:46 +01003587 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003588
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003589 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3590 psa_set_key_algorithm( &attributes, alg );
3591 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003592
Ronald Cron5425a212020-08-04 14:58:35 +02003593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3594 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003595
Ronald Cron5425a212020-08-04 14:58:35 +02003596 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003597
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003598 if( iv->len > 0 )
3599 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003600 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003601 }
3602
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003603 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003604 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003605 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003606
Gilles Peskinee0866522019-02-19 19:44:00 +01003607 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003608 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3609 output, output_buffer_size,
3610 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003611 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003612 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 PSA_ASSERT( psa_cipher_update( &operation,
3614 input->x + first_part_size,
3615 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003616 output + total_output_length,
3617 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003618 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003619 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003620 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003621 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003622 output + total_output_length,
3623 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003624 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003625 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003626 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003627
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003628 ASSERT_COMPARE( expected_output->x, expected_output->len,
3629 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003630
3631exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003632 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003633 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003634 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003635 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003636}
3637/* END_CASE */
3638
3639/* BEGIN_CASE */
3640void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003641 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003642 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003643 int first_part_size_arg,
3644 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003645 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003646{
Ronald Cron5425a212020-08-04 14:58:35 +02003647 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003648 psa_key_type_t key_type = key_type_arg;
3649 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003650 size_t first_part_size = first_part_size_arg;
3651 size_t output1_length = output1_length_arg;
3652 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003653 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003654 size_t output_buffer_size = 0;
3655 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003656 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003657 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003659
Gilles Peskine8817f612018-12-18 00:18:46 +01003660 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003661
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003662 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3663 psa_set_key_algorithm( &attributes, alg );
3664 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003665
Ronald Cron5425a212020-08-04 14:58:35 +02003666 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3667 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003668
Ronald Cron5425a212020-08-04 14:58:35 +02003669 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003670
Steven Cooreman177deba2020-09-07 17:14:14 +02003671 if( iv->len > 0 )
3672 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003673 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003674 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003675
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003676 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003677 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003678 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003679
Gilles Peskinee0866522019-02-19 19:44:00 +01003680 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003681 PSA_ASSERT( psa_cipher_update( &operation,
3682 input->x, first_part_size,
3683 output, output_buffer_size,
3684 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003685 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003686 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003687 PSA_ASSERT( psa_cipher_update( &operation,
3688 input->x + first_part_size,
3689 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003690 output + total_output_length,
3691 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003692 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003693 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003694 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003695 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003696 output + total_output_length,
3697 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003698 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003699 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003700 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003701
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003702 ASSERT_COMPARE( expected_output->x, expected_output->len,
3703 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003704
3705exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003706 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003707 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003708 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003709 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003710}
3711/* END_CASE */
3712
Gilles Peskine50e586b2018-06-08 14:28:46 +02003713/* BEGIN_CASE */
3714void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003715 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003716 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003717 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003718{
Ronald Cron5425a212020-08-04 14:58:35 +02003719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003720 psa_status_t status;
3721 psa_key_type_t key_type = key_type_arg;
3722 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003723 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003724 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003725 size_t output_buffer_size = 0;
3726 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003727 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003728 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003729 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003730
Gilles Peskine8817f612018-12-18 00:18:46 +01003731 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003732
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003733 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3734 psa_set_key_algorithm( &attributes, alg );
3735 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003736
Ronald Cron5425a212020-08-04 14:58:35 +02003737 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3738 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003739
Ronald Cron5425a212020-08-04 14:58:35 +02003740 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003741
Steven Cooreman177deba2020-09-07 17:14:14 +02003742 if( iv->len > 0 )
3743 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003744 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003745 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003746
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003747 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003748 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003749 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003750
Gilles Peskine8817f612018-12-18 00:18:46 +01003751 PSA_ASSERT( psa_cipher_update( &operation,
3752 input->x, input->len,
3753 output, output_buffer_size,
3754 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003755 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003756 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003757 output + total_output_length,
3758 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003759 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003760 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003761 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003762
3763 if( expected_status == PSA_SUCCESS )
3764 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003765 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003766 ASSERT_COMPARE( expected_output->x, expected_output->len,
3767 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003768 }
3769
Gilles Peskine50e586b2018-06-08 14:28:46 +02003770exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003771 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003772 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003773 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003774 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003775}
3776/* END_CASE */
3777
Gilles Peskine50e586b2018-06-08 14:28:46 +02003778/* BEGIN_CASE */
3779void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003780 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003781 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003782{
Ronald Cron5425a212020-08-04 14:58:35 +02003783 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003784 psa_key_type_t key_type = key_type_arg;
3785 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003786 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003787 size_t iv_size = 16;
3788 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003789 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003790 size_t output1_size = 0;
3791 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003792 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003793 size_t output2_size = 0;
3794 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003795 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003796 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3797 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003799
Gilles Peskine8817f612018-12-18 00:18:46 +01003800 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003801
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003802 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3803 psa_set_key_algorithm( &attributes, alg );
3804 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003805
Ronald Cron5425a212020-08-04 14:58:35 +02003806 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3807 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003808
Ronald Cron5425a212020-08-04 14:58:35 +02003809 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3810 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003811
Steven Cooreman177deba2020-09-07 17:14:14 +02003812 if( alg != PSA_ALG_ECB_NO_PADDING )
3813 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003814 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3815 iv, iv_size,
3816 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003817 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003818 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003819 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003820 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003821
Gilles Peskine8817f612018-12-18 00:18:46 +01003822 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3823 output1, output1_size,
3824 &output1_length ) );
3825 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003826 output1 + output1_length,
3827 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003828 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003829
Gilles Peskine048b7f02018-06-08 14:20:49 +02003830 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003831
Gilles Peskine8817f612018-12-18 00:18:46 +01003832 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003833
3834 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003835 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003836
Steven Cooreman177deba2020-09-07 17:14:14 +02003837 if( iv_length > 0 )
3838 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003839 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3840 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003841 }
3842
Gilles Peskine8817f612018-12-18 00:18:46 +01003843 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3844 output2, output2_size,
3845 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003846 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003847 PSA_ASSERT( psa_cipher_finish( &operation2,
3848 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003849 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003850 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003851
Gilles Peskine048b7f02018-06-08 14:20:49 +02003852 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003853
Gilles Peskine8817f612018-12-18 00:18:46 +01003854 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003855
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003856 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003857
3858exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003859 psa_cipher_abort( &operation1 );
3860 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003861 mbedtls_free( output1 );
3862 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003863 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003864 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003865}
3866/* END_CASE */
3867
3868/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003869void cipher_verify_output_multipart( int alg_arg,
3870 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003871 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003872 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003873 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003874{
Ronald Cron5425a212020-08-04 14:58:35 +02003875 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003876 psa_key_type_t key_type = key_type_arg;
3877 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003878 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003879 unsigned char iv[16] = {0};
3880 size_t iv_size = 16;
3881 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003882 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003883 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003884 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003885 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003886 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003887 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003888 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003889 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3890 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003892
Gilles Peskine8817f612018-12-18 00:18:46 +01003893 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003894
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3896 psa_set_key_algorithm( &attributes, alg );
3897 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003898
Ronald Cron5425a212020-08-04 14:58:35 +02003899 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3900 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003901
Ronald Cron5425a212020-08-04 14:58:35 +02003902 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3903 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003904
Steven Cooreman177deba2020-09-07 17:14:14 +02003905 if( alg != PSA_ALG_ECB_NO_PADDING )
3906 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003907 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3908 iv, iv_size,
3909 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003910 }
3911
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003912 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003913 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003914 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003915
Gilles Peskinee0866522019-02-19 19:44:00 +01003916 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003917
Gilles Peskine8817f612018-12-18 00:18:46 +01003918 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3919 output1, output1_buffer_size,
3920 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003921 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003922
Gilles Peskine8817f612018-12-18 00:18:46 +01003923 PSA_ASSERT( psa_cipher_update( &operation1,
3924 input->x + first_part_size,
3925 input->len - first_part_size,
3926 output1, output1_buffer_size,
3927 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003928 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003929
Gilles Peskine8817f612018-12-18 00:18:46 +01003930 PSA_ASSERT( psa_cipher_finish( &operation1,
3931 output1 + output1_length,
3932 output1_buffer_size - output1_length,
3933 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003934 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003935
Gilles Peskine8817f612018-12-18 00:18:46 +01003936 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003937
Gilles Peskine048b7f02018-06-08 14:20:49 +02003938 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003939 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003940
Steven Cooreman177deba2020-09-07 17:14:14 +02003941 if( iv_length > 0 )
3942 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003943 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3944 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003945 }
Moran Pekerded84402018-06-06 16:36:50 +03003946
Gilles Peskine8817f612018-12-18 00:18:46 +01003947 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3948 output2, output2_buffer_size,
3949 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003950 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003951
Gilles Peskine8817f612018-12-18 00:18:46 +01003952 PSA_ASSERT( psa_cipher_update( &operation2,
3953 output1 + first_part_size,
3954 output1_length - first_part_size,
3955 output2, output2_buffer_size,
3956 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003957 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003958
Gilles Peskine8817f612018-12-18 00:18:46 +01003959 PSA_ASSERT( psa_cipher_finish( &operation2,
3960 output2 + output2_length,
3961 output2_buffer_size - output2_length,
3962 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003963 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003964
Gilles Peskine8817f612018-12-18 00:18:46 +01003965 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003966
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003967 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003968
3969exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003970 psa_cipher_abort( &operation1 );
3971 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003972 mbedtls_free( output1 );
3973 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003974 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003975 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003976}
3977/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003978
Gilles Peskine20035e32018-02-03 22:44:14 +01003979/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003980void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003981 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003982 data_t *nonce,
3983 data_t *additional_data,
3984 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003985 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003986{
Ronald Cron5425a212020-08-04 14:58:35 +02003987 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003988 psa_key_type_t key_type = key_type_arg;
3989 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003990 unsigned char *output_data = NULL;
3991 size_t output_size = 0;
3992 size_t output_length = 0;
3993 unsigned char *output_data2 = NULL;
3994 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003995 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003996 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003998
Gilles Peskine4abf7412018-06-18 16:35:34 +02003999 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004000 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4001 * should be exact. */
4002 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4003 TEST_EQUAL( output_size,
4004 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004005 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004006
Gilles Peskine8817f612018-12-18 00:18:46 +01004007 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004008
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004009 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4010 psa_set_key_algorithm( &attributes, alg );
4011 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004012
Gilles Peskine049c7532019-05-15 20:22:09 +02004013 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004014 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004015
Ronald Cron5425a212020-08-04 14:58:35 +02004016 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004017 nonce->x, nonce->len,
4018 additional_data->x,
4019 additional_data->len,
4020 input_data->x, input_data->len,
4021 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004022 &output_length ),
4023 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004024
4025 if( PSA_SUCCESS == expected_result )
4026 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004027 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004028
Gilles Peskine003a4a92019-05-14 16:09:40 +02004029 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4030 * should be exact. */
4031 TEST_EQUAL( input_data->len,
4032 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
4033
Ronald Cron5425a212020-08-04 14:58:35 +02004034 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004035 nonce->x, nonce->len,
4036 additional_data->x,
4037 additional_data->len,
4038 output_data, output_length,
4039 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004040 &output_length2 ),
4041 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004042
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004043 ASSERT_COMPARE( input_data->x, input_data->len,
4044 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004045 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004046
Gilles Peskinea1cac842018-06-11 19:33:02 +02004047exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004048 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004049 mbedtls_free( output_data );
4050 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004051 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004052}
4053/* END_CASE */
4054
4055/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004056void aead_encrypt( int key_type_arg, data_t *key_data,
4057 int alg_arg,
4058 data_t *nonce,
4059 data_t *additional_data,
4060 data_t *input_data,
4061 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004062{
Ronald Cron5425a212020-08-04 14:58:35 +02004063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004064 psa_key_type_t key_type = key_type_arg;
4065 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004066 unsigned char *output_data = NULL;
4067 size_t output_size = 0;
4068 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004069 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004071 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004072
Gilles Peskine4abf7412018-06-18 16:35:34 +02004073 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004074 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4075 * should be exact. */
4076 TEST_EQUAL( output_size,
4077 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004078 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004079
Gilles Peskine8817f612018-12-18 00:18:46 +01004080 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004081
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004082 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4083 psa_set_key_algorithm( &attributes, alg );
4084 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004085
Gilles Peskine049c7532019-05-15 20:22:09 +02004086 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004087 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004088
Steven Cooremand588ea12021-01-11 19:36:04 +01004089 status = psa_aead_encrypt( key, alg,
4090 nonce->x, nonce->len,
4091 additional_data->x, additional_data->len,
4092 input_data->x, input_data->len,
4093 output_data, output_size,
4094 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004095
Steven Cooremand588ea12021-01-11 19:36:04 +01004096#if defined(MBEDTLS_AES_ALT) || \
4097 defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
4098 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
4099 if( status == PSA_ERROR_NOT_SUPPORTED &&
4100 key_type == PSA_KEY_TYPE_AES &&
4101 key_data->len == 24 )
4102 {
4103 test_skip( "AES-192 not supported", __LINE__, __FILE__ );
4104 goto exit;
4105 }
4106#endif /* AES could be alternatively implemented */
Steven Cooreman82645b12021-01-11 20:33:20 +01004107#if defined(MBEDTLS_GCM_ALT) || \
4108 defined(MBEDTLS_PSA_ACCEL_ALG_GCM)
4109 if( status == PSA_ERROR_NOT_SUPPORTED &&
Steven Cooreman50f1f5e2021-01-25 10:26:49 +01004110 ( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ==
4111 PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ) ) &&
Steven Cooreman82645b12021-01-11 20:33:20 +01004112 nonce->len != 12 )
4113 {
4114 test_skip( "AES-GCM with non-12-byte IV is not supported", __LINE__, __FILE__ );
4115 goto exit;
4116 }
4117#endif /* AES-GCM could be alternatively implemented */
Steven Cooremand588ea12021-01-11 19:36:04 +01004118
4119 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004120 ASSERT_COMPARE( expected_result->x, expected_result->len,
4121 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004122
Gilles Peskinea1cac842018-06-11 19:33:02 +02004123exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004124 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004125 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004126 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004127}
4128/* END_CASE */
4129
4130/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004131void aead_decrypt( int key_type_arg, data_t *key_data,
4132 int alg_arg,
4133 data_t *nonce,
4134 data_t *additional_data,
4135 data_t *input_data,
4136 data_t *expected_data,
4137 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004138{
Ronald Cron5425a212020-08-04 14:58:35 +02004139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004140 psa_key_type_t key_type = key_type_arg;
4141 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004142 unsigned char *output_data = NULL;
4143 size_t output_size = 0;
4144 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004145 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004146 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004147 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004148 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004149
Gilles Peskine003a4a92019-05-14 16:09:40 +02004150 output_size = input_data->len - tag_length;
4151 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4152 * should be exact. */
4153 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4154 TEST_EQUAL( output_size,
4155 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004156 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004157
Gilles Peskine8817f612018-12-18 00:18:46 +01004158 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004159
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004160 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4161 psa_set_key_algorithm( &attributes, alg );
4162 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004163
Gilles Peskine049c7532019-05-15 20:22:09 +02004164 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004165 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004166
Steven Cooremand588ea12021-01-11 19:36:04 +01004167 status = psa_aead_decrypt( key, alg,
4168 nonce->x, nonce->len,
4169 additional_data->x,
4170 additional_data->len,
4171 input_data->x, input_data->len,
4172 output_data, output_size,
4173 &output_length );
4174
4175#if defined(MBEDTLS_AES_ALT) || \
4176 defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
4177 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
4178 if( status == PSA_ERROR_NOT_SUPPORTED &&
4179 key_type == PSA_KEY_TYPE_AES &&
4180 key_data->len == 24 )
4181 {
4182 test_skip( "AES-192 not supported", __LINE__, __FILE__ );
4183 goto exit;
4184 }
4185#endif /* AES could be alternatively implemented */
Steven Cooreman82645b12021-01-11 20:33:20 +01004186#if defined(MBEDTLS_GCM_ALT) || \
4187 defined(MBEDTLS_PSA_ACCEL_ALG_GCM)
4188 if( status == PSA_ERROR_NOT_SUPPORTED &&
Steven Cooreman50f1f5e2021-01-25 10:26:49 +01004189 ( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) ==
4190 PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ) ) &&
Steven Cooreman82645b12021-01-11 20:33:20 +01004191 nonce->len != 12 )
4192 {
4193 test_skip( "AES-GCM with non-12-byte IV is not supported", __LINE__, __FILE__ );
4194 goto exit;
4195 }
4196#endif /* AES-GCM could be alternatively implemented */
Steven Cooremand588ea12021-01-11 19:36:04 +01004197
4198 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004199
Gilles Peskine2d277862018-06-18 15:41:12 +02004200 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004201 ASSERT_COMPARE( expected_data->x, expected_data->len,
4202 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004203
Gilles Peskinea1cac842018-06-11 19:33:02 +02004204exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004205 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004206 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004207 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004208}
4209/* END_CASE */
4210
4211/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004212void signature_size( int type_arg,
4213 int bits,
4214 int alg_arg,
4215 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004216{
4217 psa_key_type_t type = type_arg;
4218 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004219 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004220
Gilles Peskinefe11b722018-12-18 00:24:04 +01004221 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004222#if defined(MBEDTLS_TEST_DEPRECATED)
4223 TEST_EQUAL( actual_size,
4224 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4225#endif /* MBEDTLS_TEST_DEPRECATED */
4226
Gilles Peskinee59236f2018-01-27 23:32:46 +01004227exit:
4228 ;
4229}
4230/* END_CASE */
4231
4232/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004233void sign_deterministic( int key_type_arg, data_t *key_data,
4234 int alg_arg, data_t *input_data,
4235 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004236{
Ronald Cron5425a212020-08-04 14:58:35 +02004237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004238 psa_key_type_t key_type = key_type_arg;
4239 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004240 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004241 unsigned char *signature = NULL;
4242 size_t signature_size;
4243 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004244 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004245
Gilles Peskine8817f612018-12-18 00:18:46 +01004246 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004247
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004248 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004249 psa_set_key_algorithm( &attributes, alg );
4250 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004251
Gilles Peskine049c7532019-05-15 20:22:09 +02004252 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004253 &key ) );
4254 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004255 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004256
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004257 /* Allocate a buffer which has the size advertized by the
4258 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004259 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004260 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004261 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004262 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004263 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004264
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004265 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004266 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004267 input_data->x, input_data->len,
4268 signature, signature_size,
4269 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004270 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004271 ASSERT_COMPARE( output_data->x, output_data->len,
4272 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004273
Gilles Peskine0627f982019-11-26 19:12:16 +01004274#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004275 memset( signature, 0, signature_size );
4276 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004277 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004278 input_data->x, input_data->len,
4279 signature, signature_size,
4280 &signature_length ) );
4281 ASSERT_COMPARE( output_data->x, output_data->len,
4282 signature, signature_length );
4283#endif /* MBEDTLS_TEST_DEPRECATED */
4284
Gilles Peskine20035e32018-02-03 22:44:14 +01004285exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004286 /*
4287 * Key attributes may have been returned by psa_get_key_attributes()
4288 * thus reset them as required.
4289 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004290 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004291
Ronald Cron5425a212020-08-04 14:58:35 +02004292 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004293 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004294 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004295}
4296/* END_CASE */
4297
4298/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004299void sign_fail( int key_type_arg, data_t *key_data,
4300 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004301 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004302{
Ronald Cron5425a212020-08-04 14:58:35 +02004303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004304 psa_key_type_t key_type = key_type_arg;
4305 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004306 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004307 psa_status_t actual_status;
4308 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004309 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004310 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004311 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004312
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004313 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004314
Gilles Peskine8817f612018-12-18 00:18:46 +01004315 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004316
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004317 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004318 psa_set_key_algorithm( &attributes, alg );
4319 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004320
Gilles Peskine049c7532019-05-15 20:22:09 +02004321 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004322 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004323
Ronald Cron5425a212020-08-04 14:58:35 +02004324 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004325 input_data->x, input_data->len,
4326 signature, signature_size,
4327 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004328 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004329 /* The value of *signature_length is unspecified on error, but
4330 * whatever it is, it should be less than signature_size, so that
4331 * if the caller tries to read *signature_length bytes without
4332 * checking the error code then they don't overflow a buffer. */
4333 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004334
Gilles Peskine895242b2019-11-29 12:15:40 +01004335#if defined(MBEDTLS_TEST_DEPRECATED)
4336 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004337 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004338 input_data->x, input_data->len,
4339 signature, signature_size,
4340 &signature_length ),
4341 expected_status );
4342 TEST_ASSERT( signature_length <= signature_size );
4343#endif /* MBEDTLS_TEST_DEPRECATED */
4344
Gilles Peskine20035e32018-02-03 22:44:14 +01004345exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004346 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004347 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004348 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004349 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004350}
4351/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004352
4353/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004354void sign_verify( int key_type_arg, data_t *key_data,
4355 int alg_arg, data_t *input_data )
4356{
Ronald Cron5425a212020-08-04 14:58:35 +02004357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004358 psa_key_type_t key_type = key_type_arg;
4359 psa_algorithm_t alg = alg_arg;
4360 size_t key_bits;
4361 unsigned char *signature = NULL;
4362 size_t signature_size;
4363 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004365
Gilles Peskine8817f612018-12-18 00:18:46 +01004366 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004367
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004368 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004369 psa_set_key_algorithm( &attributes, alg );
4370 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004371
Gilles Peskine049c7532019-05-15 20:22:09 +02004372 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004373 &key ) );
4374 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004375 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004376
4377 /* Allocate a buffer which has the size advertized by the
4378 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004379 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004380 key_bits, alg );
4381 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004382 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004383 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004384
4385 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004386 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004387 input_data->x, input_data->len,
4388 signature, signature_size,
4389 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004390 /* Check that the signature length looks sensible. */
4391 TEST_ASSERT( signature_length <= signature_size );
4392 TEST_ASSERT( signature_length > 0 );
4393
4394 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004395 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004396 input_data->x, input_data->len,
4397 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004398
4399 if( input_data->len != 0 )
4400 {
4401 /* Flip a bit in the input and verify that the signature is now
4402 * detected as invalid. Flip a bit at the beginning, not at the end,
4403 * because ECDSA may ignore the last few bits of the input. */
4404 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004405 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004406 input_data->x, input_data->len,
4407 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004408 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004409 }
4410
4411exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004412 /*
4413 * Key attributes may have been returned by psa_get_key_attributes()
4414 * thus reset them as required.
4415 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004416 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004417
Ronald Cron5425a212020-08-04 14:58:35 +02004418 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004419 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004420 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004421}
4422/* END_CASE */
4423
4424/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004425void asymmetric_verify( int key_type_arg, data_t *key_data,
4426 int alg_arg, data_t *hash_data,
4427 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004428{
Ronald Cron5425a212020-08-04 14:58:35 +02004429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004430 psa_key_type_t key_type = key_type_arg;
4431 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004432 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004433
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004434 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004435
Gilles Peskine8817f612018-12-18 00:18:46 +01004436 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004437
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004438 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004439 psa_set_key_algorithm( &attributes, alg );
4440 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004441
Gilles Peskine049c7532019-05-15 20:22:09 +02004442 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004443 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004444
Ronald Cron5425a212020-08-04 14:58:35 +02004445 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004446 hash_data->x, hash_data->len,
4447 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004448
4449#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004450 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004451 hash_data->x, hash_data->len,
4452 signature_data->x,
4453 signature_data->len ) );
4454
4455#endif /* MBEDTLS_TEST_DEPRECATED */
4456
itayzafrir5c753392018-05-08 11:18:38 +03004457exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004458 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004459 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004460 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004461}
4462/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004463
4464/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004465void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4466 int alg_arg, data_t *hash_data,
4467 data_t *signature_data,
4468 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004469{
Ronald Cron5425a212020-08-04 14:58:35 +02004470 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004471 psa_key_type_t key_type = key_type_arg;
4472 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004473 psa_status_t actual_status;
4474 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004475 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004476
Gilles Peskine8817f612018-12-18 00:18:46 +01004477 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004478
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004479 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004480 psa_set_key_algorithm( &attributes, alg );
4481 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004482
Gilles Peskine049c7532019-05-15 20:22:09 +02004483 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004484 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004485
Ronald Cron5425a212020-08-04 14:58:35 +02004486 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004487 hash_data->x, hash_data->len,
4488 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004489 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004490
Gilles Peskine895242b2019-11-29 12:15:40 +01004491#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004492 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004493 hash_data->x, hash_data->len,
4494 signature_data->x, signature_data->len ),
4495 expected_status );
4496#endif /* MBEDTLS_TEST_DEPRECATED */
4497
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004498exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004499 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004500 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004501 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004502}
4503/* END_CASE */
4504
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004505/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004506void asymmetric_encrypt( int key_type_arg,
4507 data_t *key_data,
4508 int alg_arg,
4509 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004510 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004511 int expected_output_length_arg,
4512 int expected_status_arg )
4513{
Ronald Cron5425a212020-08-04 14:58:35 +02004514 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004515 psa_key_type_t key_type = key_type_arg;
4516 psa_algorithm_t alg = alg_arg;
4517 size_t expected_output_length = expected_output_length_arg;
4518 size_t key_bits;
4519 unsigned char *output = NULL;
4520 size_t output_size;
4521 size_t output_length = ~0;
4522 psa_status_t actual_status;
4523 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004524 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004525
Gilles Peskine8817f612018-12-18 00:18:46 +01004526 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004527
Gilles Peskine656896e2018-06-29 19:12:28 +02004528 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004529 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4530 psa_set_key_algorithm( &attributes, alg );
4531 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004532 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004533 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004534
4535 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004536 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004537 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004538 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004539 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004540
4541 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004542 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004543 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004544 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004545 output, output_size,
4546 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004547 TEST_EQUAL( actual_status, expected_status );
4548 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004549
Gilles Peskine68428122018-06-30 18:42:41 +02004550 /* If the label is empty, the test framework puts a non-null pointer
4551 * in label->x. Test that a null pointer works as well. */
4552 if( label->len == 0 )
4553 {
4554 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004555 if( output_size != 0 )
4556 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004557 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004558 input_data->x, input_data->len,
4559 NULL, label->len,
4560 output, output_size,
4561 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004562 TEST_EQUAL( actual_status, expected_status );
4563 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004564 }
4565
Gilles Peskine656896e2018-06-29 19:12:28 +02004566exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004567 /*
4568 * Key attributes may have been returned by psa_get_key_attributes()
4569 * thus reset them as required.
4570 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004571 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004572
Ronald Cron5425a212020-08-04 14:58:35 +02004573 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004574 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004575 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004576}
4577/* END_CASE */
4578
4579/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004580void asymmetric_encrypt_decrypt( int key_type_arg,
4581 data_t *key_data,
4582 int alg_arg,
4583 data_t *input_data,
4584 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004585{
Ronald Cron5425a212020-08-04 14:58:35 +02004586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004587 psa_key_type_t key_type = key_type_arg;
4588 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004589 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004590 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004591 size_t output_size;
4592 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004593 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004594 size_t output2_size;
4595 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004596 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004597
Gilles Peskine8817f612018-12-18 00:18:46 +01004598 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004599
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004600 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4601 psa_set_key_algorithm( &attributes, alg );
4602 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004603
Gilles Peskine049c7532019-05-15 20:22:09 +02004604 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004605 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004606
4607 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004608 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004609 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004610 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004611 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004612 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004613 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004614
Gilles Peskineeebd7382018-06-08 18:11:54 +02004615 /* We test encryption by checking that encrypt-then-decrypt gives back
4616 * the original plaintext because of the non-optional random
4617 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004618 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004619 input_data->x, input_data->len,
4620 label->x, label->len,
4621 output, output_size,
4622 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004623 /* We don't know what ciphertext length to expect, but check that
4624 * it looks sensible. */
4625 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004626
Ronald Cron5425a212020-08-04 14:58:35 +02004627 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004628 output, output_length,
4629 label->x, label->len,
4630 output2, output2_size,
4631 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004632 ASSERT_COMPARE( input_data->x, input_data->len,
4633 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004634
4635exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004636 /*
4637 * Key attributes may have been returned by psa_get_key_attributes()
4638 * thus reset them as required.
4639 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004640 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004641
Ronald Cron5425a212020-08-04 14:58:35 +02004642 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004643 mbedtls_free( output );
4644 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004645 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004646}
4647/* END_CASE */
4648
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004649/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004650void asymmetric_decrypt( int key_type_arg,
4651 data_t *key_data,
4652 int alg_arg,
4653 data_t *input_data,
4654 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004655 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004656{
Ronald Cron5425a212020-08-04 14:58:35 +02004657 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004658 psa_key_type_t key_type = key_type_arg;
4659 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004660 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004661 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004662 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004664
Jaeden Amero412654a2019-02-06 12:57:46 +00004665 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004666 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004667
Gilles Peskine8817f612018-12-18 00:18:46 +01004668 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004669
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004670 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4671 psa_set_key_algorithm( &attributes, alg );
4672 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004673
Gilles Peskine049c7532019-05-15 20:22:09 +02004674 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004675 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004676
Ronald Cron5425a212020-08-04 14:58:35 +02004677 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004678 input_data->x, input_data->len,
4679 label->x, label->len,
4680 output,
4681 output_size,
4682 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004683 ASSERT_COMPARE( expected_data->x, expected_data->len,
4684 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004685
Gilles Peskine68428122018-06-30 18:42:41 +02004686 /* If the label is empty, the test framework puts a non-null pointer
4687 * in label->x. Test that a null pointer works as well. */
4688 if( label->len == 0 )
4689 {
4690 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004691 if( output_size != 0 )
4692 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004693 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004694 input_data->x, input_data->len,
4695 NULL, label->len,
4696 output,
4697 output_size,
4698 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004699 ASSERT_COMPARE( expected_data->x, expected_data->len,
4700 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004701 }
4702
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004703exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004704 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004705 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004706 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004707 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004708}
4709/* END_CASE */
4710
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004711/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004712void asymmetric_decrypt_fail( int key_type_arg,
4713 data_t *key_data,
4714 int alg_arg,
4715 data_t *input_data,
4716 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004717 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004718 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004719{
Ronald Cron5425a212020-08-04 14:58:35 +02004720 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004721 psa_key_type_t key_type = key_type_arg;
4722 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004723 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004724 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004725 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004726 psa_status_t actual_status;
4727 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004728 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004729
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004730 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004731
Gilles Peskine8817f612018-12-18 00:18:46 +01004732 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004733
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004734 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4735 psa_set_key_algorithm( &attributes, alg );
4736 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004737
Gilles Peskine049c7532019-05-15 20:22:09 +02004738 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004739 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004740
Ronald Cron5425a212020-08-04 14:58:35 +02004741 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004742 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004743 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004744 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004745 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004746 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004747 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004748
Gilles Peskine68428122018-06-30 18:42:41 +02004749 /* If the label is empty, the test framework puts a non-null pointer
4750 * in label->x. Test that a null pointer works as well. */
4751 if( label->len == 0 )
4752 {
4753 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004754 if( output_size != 0 )
4755 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004756 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004757 input_data->x, input_data->len,
4758 NULL, label->len,
4759 output, output_size,
4760 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004761 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004762 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004763 }
4764
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004765exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004766 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004767 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004768 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004769 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004770}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004771/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004772
4773/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004774void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004775{
4776 /* Test each valid way of initializing the object, except for `= {0}`, as
4777 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4778 * though it's OK by the C standard. We could test for this, but we'd need
4779 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004780 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004781 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4782 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4783 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004784
4785 memset( &zero, 0, sizeof( zero ) );
4786
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004787 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004788 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004789 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004790 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004791 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004792 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004793 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004794
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004795 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004796 PSA_ASSERT( psa_key_derivation_abort(&func) );
4797 PSA_ASSERT( psa_key_derivation_abort(&init) );
4798 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004799}
4800/* END_CASE */
4801
Janos Follath16de4a42019-06-13 16:32:24 +01004802/* BEGIN_CASE */
4803void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004804{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004805 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004806 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004808
Gilles Peskine8817f612018-12-18 00:18:46 +01004809 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004810
Janos Follath16de4a42019-06-13 16:32:24 +01004811 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004812 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004813
4814exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004816 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004817}
4818/* END_CASE */
4819
Janos Follathaf3c2a02019-06-12 12:34:34 +01004820/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004821void derive_set_capacity( int alg_arg, int capacity_arg,
4822 int expected_status_arg )
4823{
4824 psa_algorithm_t alg = alg_arg;
4825 size_t capacity = capacity_arg;
4826 psa_status_t expected_status = expected_status_arg;
4827 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4828
4829 PSA_ASSERT( psa_crypto_init( ) );
4830
4831 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4832
4833 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4834 expected_status );
4835
4836exit:
4837 psa_key_derivation_abort( &operation );
4838 PSA_DONE( );
4839}
4840/* END_CASE */
4841
4842/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004843void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004844 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004845 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004846 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004847 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004848 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004849 int expected_status_arg3,
4850 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004851{
4852 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004853 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4854 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004855 psa_status_t expected_statuses[] = {expected_status_arg1,
4856 expected_status_arg2,
4857 expected_status_arg3};
4858 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004859 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4860 MBEDTLS_SVC_KEY_ID_INIT,
4861 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004862 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4863 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4864 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004865 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004866 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004867 psa_status_t expected_output_status = expected_output_status_arg;
4868 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004869
4870 PSA_ASSERT( psa_crypto_init( ) );
4871
4872 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4873 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004874
4875 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4876
4877 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4878 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004879 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004880 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004881 psa_set_key_type( &attributes, key_types[i] );
4882 PSA_ASSERT( psa_import_key( &attributes,
4883 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004884 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004885 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4886 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4887 {
4888 // When taking a private key as secret input, use key agreement
4889 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004890 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004891 expected_statuses[i] );
4892 }
4893 else
4894 {
4895 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004896 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004897 expected_statuses[i] );
4898 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004899 }
4900 else
4901 {
4902 TEST_EQUAL( psa_key_derivation_input_bytes(
4903 &operation, steps[i],
4904 inputs[i]->x, inputs[i]->len ),
4905 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004906 }
4907 }
4908
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004909 if( output_key_type != PSA_KEY_TYPE_NONE )
4910 {
4911 psa_reset_key_attributes( &attributes );
4912 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4913 psa_set_key_bits( &attributes, 8 );
4914 actual_output_status =
4915 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004916 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004917 }
4918 else
4919 {
4920 uint8_t buffer[1];
4921 actual_output_status =
4922 psa_key_derivation_output_bytes( &operation,
4923 buffer, sizeof( buffer ) );
4924 }
4925 TEST_EQUAL( actual_output_status, expected_output_status );
4926
Janos Follathaf3c2a02019-06-12 12:34:34 +01004927exit:
4928 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004929 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4930 psa_destroy_key( keys[i] );
4931 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004932 PSA_DONE( );
4933}
4934/* END_CASE */
4935
Janos Follathd958bb72019-07-03 15:02:16 +01004936/* BEGIN_CASE */
4937void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004938{
Janos Follathd958bb72019-07-03 15:02:16 +01004939 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004941 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004942 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004943 unsigned char input1[] = "Input 1";
4944 size_t input1_length = sizeof( input1 );
4945 unsigned char input2[] = "Input 2";
4946 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004947 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004948 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004949 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4950 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4951 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004952 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004953
Gilles Peskine8817f612018-12-18 00:18:46 +01004954 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004955
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004956 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4957 psa_set_key_algorithm( &attributes, alg );
4958 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004959
Gilles Peskine73676cb2019-05-15 20:15:10 +02004960 PSA_ASSERT( psa_import_key( &attributes,
4961 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004962 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004963
4964 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004965 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004966 input1, input1_length,
4967 input2, input2_length,
4968 capacity ) )
4969 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004970
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004972 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004973 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004974
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004976
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004978 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004979
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004980exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004982 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004983 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004984}
4985/* END_CASE */
4986
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004987/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004988void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004989{
4990 uint8_t output_buffer[16];
4991 size_t buffer_size = 16;
4992 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004993 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004994
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004995 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4996 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004997 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004998
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004999 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005000 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005001
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005002 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005003
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005004 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5005 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005006 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005007
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005009 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005010
5011exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005012 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005013}
5014/* END_CASE */
5015
5016/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005017void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005018 int step1_arg, data_t *input1,
5019 int step2_arg, data_t *input2,
5020 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005021 int requested_capacity_arg,
5022 data_t *expected_output1,
5023 data_t *expected_output2 )
5024{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005025 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005026 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5027 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005028 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5029 MBEDTLS_SVC_KEY_ID_INIT,
5030 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005031 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005033 uint8_t *expected_outputs[2] =
5034 {expected_output1->x, expected_output2->x};
5035 size_t output_sizes[2] =
5036 {expected_output1->len, expected_output2->len};
5037 size_t output_buffer_size = 0;
5038 uint8_t *output_buffer = NULL;
5039 size_t expected_capacity;
5040 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005041 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005042 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005043 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005044
5045 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5046 {
5047 if( output_sizes[i] > output_buffer_size )
5048 output_buffer_size = output_sizes[i];
5049 if( output_sizes[i] == 0 )
5050 expected_outputs[i] = NULL;
5051 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005052 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005053 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005054
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005055 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5056 psa_set_key_algorithm( &attributes, alg );
5057 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005058
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005059 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005060 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5061 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5062 requested_capacity ) );
5063 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005064 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005065 switch( steps[i] )
5066 {
5067 case 0:
5068 break;
5069 case PSA_KEY_DERIVATION_INPUT_SECRET:
5070 PSA_ASSERT( psa_import_key( &attributes,
5071 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005072 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005073 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005074 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005075 break;
5076 default:
5077 PSA_ASSERT( psa_key_derivation_input_bytes(
5078 &operation, steps[i],
5079 inputs[i]->x, inputs[i]->len ) );
5080 break;
5081 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005082 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005083
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005084 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005085 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005086 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005087 expected_capacity = requested_capacity;
5088
5089 /* Expansion phase. */
5090 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5091 {
5092 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005093 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005094 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005095 if( expected_capacity == 0 && output_sizes[i] == 0 )
5096 {
5097 /* Reading 0 bytes when 0 bytes are available can go either way. */
5098 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005099 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005100 continue;
5101 }
5102 else if( expected_capacity == 0 ||
5103 output_sizes[i] > expected_capacity )
5104 {
5105 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005106 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005107 expected_capacity = 0;
5108 continue;
5109 }
5110 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005111 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005112 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005113 ASSERT_COMPARE( output_buffer, output_sizes[i],
5114 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005116 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005117 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005118 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005119 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005120 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005122
5123exit:
5124 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005126 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5127 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005128 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005129}
5130/* END_CASE */
5131
5132/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005133void derive_full( int alg_arg,
5134 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005135 data_t *input1,
5136 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005137 int requested_capacity_arg )
5138{
Ronald Cron5425a212020-08-04 14:58:35 +02005139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005140 psa_algorithm_t alg = alg_arg;
5141 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005142 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005143 unsigned char output_buffer[16];
5144 size_t expected_capacity = requested_capacity;
5145 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005146 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005147
Gilles Peskine8817f612018-12-18 00:18:46 +01005148 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005149
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005150 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5151 psa_set_key_algorithm( &attributes, alg );
5152 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005153
Gilles Peskine049c7532019-05-15 20:22:09 +02005154 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005155 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005156
Ronald Cron5425a212020-08-04 14:58:35 +02005157 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005158 input1->x, input1->len,
5159 input2->x, input2->len,
5160 requested_capacity ) )
5161 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005162
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005163 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005164 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005165 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005166
5167 /* Expansion phase. */
5168 while( current_capacity > 0 )
5169 {
5170 size_t read_size = sizeof( output_buffer );
5171 if( read_size > current_capacity )
5172 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005173 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005174 output_buffer,
5175 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005176 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005177 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005178 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005179 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005180 }
5181
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005182 /* Check that the operation refuses to go over capacity. */
5183 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005184 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005185
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005186 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005187
5188exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005189 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005190 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005191 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005192}
5193/* END_CASE */
5194
Janos Follathe60c9052019-07-03 13:51:30 +01005195/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005196void derive_key_exercise( int alg_arg,
5197 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005198 data_t *input1,
5199 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005200 int derived_type_arg,
5201 int derived_bits_arg,
5202 int derived_usage_arg,
5203 int derived_alg_arg )
5204{
Ronald Cron5425a212020-08-04 14:58:35 +02005205 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5206 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005207 psa_algorithm_t alg = alg_arg;
5208 psa_key_type_t derived_type = derived_type_arg;
5209 size_t derived_bits = derived_bits_arg;
5210 psa_key_usage_t derived_usage = derived_usage_arg;
5211 psa_algorithm_t derived_alg = derived_alg_arg;
5212 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005215 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005216
Gilles Peskine8817f612018-12-18 00:18:46 +01005217 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005218
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5220 psa_set_key_algorithm( &attributes, alg );
5221 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005222 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005223 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005224
5225 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005226 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005227 input1->x, input1->len,
5228 input2->x, input2->len, capacity ) )
5229 goto exit;
5230
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005231 psa_set_key_usage_flags( &attributes, derived_usage );
5232 psa_set_key_algorithm( &attributes, derived_alg );
5233 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005234 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005235 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005236 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005237
5238 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005239 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005240 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5241 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005242
5243 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005244 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005245 goto exit;
5246
5247exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005248 /*
5249 * Key attributes may have been returned by psa_get_key_attributes()
5250 * thus reset them as required.
5251 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005252 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005253
5254 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005255 psa_destroy_key( base_key );
5256 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005257 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005258}
5259/* END_CASE */
5260
Janos Follath42fd8882019-07-03 14:17:09 +01005261/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005262void derive_key_export( int alg_arg,
5263 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005264 data_t *input1,
5265 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005266 int bytes1_arg,
5267 int bytes2_arg )
5268{
Ronald Cron5425a212020-08-04 14:58:35 +02005269 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5270 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005271 psa_algorithm_t alg = alg_arg;
5272 size_t bytes1 = bytes1_arg;
5273 size_t bytes2 = bytes2_arg;
5274 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005276 uint8_t *output_buffer = NULL;
5277 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005278 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5279 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005280 size_t length;
5281
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005282 ASSERT_ALLOC( output_buffer, capacity );
5283 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005284 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005285
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005286 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5287 psa_set_key_algorithm( &base_attributes, alg );
5288 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005289 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005290 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005291
5292 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005293 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005294 input1->x, input1->len,
5295 input2->x, input2->len, capacity ) )
5296 goto exit;
5297
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005299 output_buffer,
5300 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005301 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005302
5303 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005304 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005305 input1->x, input1->len,
5306 input2->x, input2->len, capacity ) )
5307 goto exit;
5308
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005309 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5310 psa_set_key_algorithm( &derived_attributes, 0 );
5311 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005312 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005313 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005314 &derived_key ) );
5315 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005316 export_buffer, bytes1,
5317 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005318 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005319 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005320 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005321 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005322 &derived_key ) );
5323 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005324 export_buffer + bytes1, bytes2,
5325 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005326 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005327
5328 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005329 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5330 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005331
5332exit:
5333 mbedtls_free( output_buffer );
5334 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005335 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005336 psa_destroy_key( base_key );
5337 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005338 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005339}
5340/* END_CASE */
5341
5342/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005343void derive_key( int alg_arg,
5344 data_t *key_data, data_t *input1, data_t *input2,
5345 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005346 int expected_status_arg,
5347 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005348{
Ronald Cron5425a212020-08-04 14:58:35 +02005349 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5350 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005351 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005352 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005353 size_t bits = bits_arg;
5354 psa_status_t expected_status = expected_status_arg;
5355 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5356 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5357 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5358
5359 PSA_ASSERT( psa_crypto_init( ) );
5360
5361 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5362 psa_set_key_algorithm( &base_attributes, alg );
5363 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5364 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005365 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005366
Ronald Cron5425a212020-08-04 14:58:35 +02005367 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005368 input1->x, input1->len,
5369 input2->x, input2->len, SIZE_MAX ) )
5370 goto exit;
5371
5372 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5373 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005374 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005375 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005376
5377 psa_status_t status =
5378 psa_key_derivation_output_key( &derived_attributes,
5379 &operation,
5380 &derived_key );
5381 if( is_large_output > 0 )
5382 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5383 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005384
5385exit:
5386 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005387 psa_destroy_key( base_key );
5388 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005389 PSA_DONE( );
5390}
5391/* END_CASE */
5392
5393/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005394void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005395 int our_key_type_arg, int our_key_alg_arg,
5396 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005397 int expected_status_arg )
5398{
Ronald Cron5425a212020-08-04 14:58:35 +02005399 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005400 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005401 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005402 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005403 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005404 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005405 psa_status_t expected_status = expected_status_arg;
5406 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005407
Gilles Peskine8817f612018-12-18 00:18:46 +01005408 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005409
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005410 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005411 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005412 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005413 PSA_ASSERT( psa_import_key( &attributes,
5414 our_key_data->x, our_key_data->len,
5415 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005416
Gilles Peskine77f40d82019-04-11 21:27:06 +02005417 /* The tests currently include inputs that should fail at either step.
5418 * Test cases that fail at the setup step should be changed to call
5419 * key_derivation_setup instead, and this function should be renamed
5420 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005421 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005422 if( status == PSA_SUCCESS )
5423 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005424 TEST_EQUAL( psa_key_derivation_key_agreement(
5425 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5426 our_key,
5427 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005428 expected_status );
5429 }
5430 else
5431 {
5432 TEST_ASSERT( status == expected_status );
5433 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005434
5435exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005436 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005437 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005438 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005439}
5440/* END_CASE */
5441
5442/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005443void raw_key_agreement( int alg_arg,
5444 int our_key_type_arg, data_t *our_key_data,
5445 data_t *peer_key_data,
5446 data_t *expected_output )
5447{
Ronald Cron5425a212020-08-04 14:58:35 +02005448 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005449 psa_algorithm_t alg = alg_arg;
5450 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005452 unsigned char *output = NULL;
5453 size_t output_length = ~0;
5454
5455 ASSERT_ALLOC( output, expected_output->len );
5456 PSA_ASSERT( psa_crypto_init( ) );
5457
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005458 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5459 psa_set_key_algorithm( &attributes, alg );
5460 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005461 PSA_ASSERT( psa_import_key( &attributes,
5462 our_key_data->x, our_key_data->len,
5463 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005464
Gilles Peskinebe697d82019-05-16 18:00:41 +02005465 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5466 peer_key_data->x, peer_key_data->len,
5467 output, expected_output->len,
5468 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005469 ASSERT_COMPARE( output, output_length,
5470 expected_output->x, expected_output->len );
5471
5472exit:
5473 mbedtls_free( output );
5474 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005475 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005476}
5477/* END_CASE */
5478
5479/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005480void key_agreement_capacity( int alg_arg,
5481 int our_key_type_arg, data_t *our_key_data,
5482 data_t *peer_key_data,
5483 int expected_capacity_arg )
5484{
Ronald Cron5425a212020-08-04 14:58:35 +02005485 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005486 psa_algorithm_t alg = alg_arg;
5487 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005490 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005491 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005492
Gilles Peskine8817f612018-12-18 00:18:46 +01005493 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005494
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005495 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5496 psa_set_key_algorithm( &attributes, alg );
5497 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005498 PSA_ASSERT( psa_import_key( &attributes,
5499 our_key_data->x, our_key_data->len,
5500 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005501
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005502 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005503 PSA_ASSERT( psa_key_derivation_key_agreement(
5504 &operation,
5505 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5506 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005507 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5508 {
5509 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005510 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005511 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005512 NULL, 0 ) );
5513 }
Gilles Peskine59685592018-09-18 12:11:34 +02005514
Gilles Peskinebf491972018-10-25 22:36:12 +02005515 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005516 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005518 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005519
Gilles Peskinebf491972018-10-25 22:36:12 +02005520 /* Test the actual capacity by reading the output. */
5521 while( actual_capacity > sizeof( output ) )
5522 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005524 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005525 actual_capacity -= sizeof( output );
5526 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005527 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005528 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005529 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005530 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005531
Gilles Peskine59685592018-09-18 12:11:34 +02005532exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005533 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005534 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005535 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005536}
5537/* END_CASE */
5538
5539/* BEGIN_CASE */
5540void key_agreement_output( int alg_arg,
5541 int our_key_type_arg, data_t *our_key_data,
5542 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005543 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005544{
Ronald Cron5425a212020-08-04 14:58:35 +02005545 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005546 psa_algorithm_t alg = alg_arg;
5547 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005548 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005549 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005550 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005551
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005552 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5553 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005554
Gilles Peskine8817f612018-12-18 00:18:46 +01005555 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005556
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005557 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5558 psa_set_key_algorithm( &attributes, alg );
5559 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005560 PSA_ASSERT( psa_import_key( &attributes,
5561 our_key_data->x, our_key_data->len,
5562 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005563
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005564 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005565 PSA_ASSERT( psa_key_derivation_key_agreement(
5566 &operation,
5567 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5568 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005569 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5570 {
5571 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005572 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005573 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005574 NULL, 0 ) );
5575 }
Gilles Peskine59685592018-09-18 12:11:34 +02005576
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005577 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005578 actual_output,
5579 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005580 ASSERT_COMPARE( actual_output, expected_output1->len,
5581 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005582 if( expected_output2->len != 0 )
5583 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005584 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005585 actual_output,
5586 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005587 ASSERT_COMPARE( actual_output, expected_output2->len,
5588 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005589 }
Gilles Peskine59685592018-09-18 12:11:34 +02005590
5591exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005592 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005593 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005594 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005595 mbedtls_free( actual_output );
5596}
5597/* END_CASE */
5598
5599/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005600void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005601{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005602 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005603 unsigned char *output = NULL;
5604 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005605 size_t i;
5606 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005607
Simon Butcher49f8e312020-03-03 15:51:50 +00005608 TEST_ASSERT( bytes_arg >= 0 );
5609
Gilles Peskine91892022021-02-08 19:50:26 +01005610 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005611 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005612
Gilles Peskine8817f612018-12-18 00:18:46 +01005613 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005614
Gilles Peskinea50d7392018-06-21 10:22:13 +02005615 /* Run several times, to ensure that every output byte will be
5616 * nonzero at least once with overwhelming probability
5617 * (2^(-8*number_of_runs)). */
5618 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005619 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005620 if( bytes != 0 )
5621 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005622 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005623
Gilles Peskinea50d7392018-06-21 10:22:13 +02005624 for( i = 0; i < bytes; i++ )
5625 {
5626 if( output[i] != 0 )
5627 ++changed[i];
5628 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005629 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005630
5631 /* Check that every byte was changed to nonzero at least once. This
5632 * validates that psa_generate_random is overwriting every byte of
5633 * the output buffer. */
5634 for( i = 0; i < bytes; i++ )
5635 {
5636 TEST_ASSERT( changed[i] != 0 );
5637 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005638
5639exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005640 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005641 mbedtls_free( output );
5642 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005643}
5644/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005645
5646/* BEGIN_CASE */
5647void generate_key( int type_arg,
5648 int bits_arg,
5649 int usage_arg,
5650 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005651 int expected_status_arg,
5652 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005653{
Ronald Cron5425a212020-08-04 14:58:35 +02005654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005655 psa_key_type_t type = type_arg;
5656 psa_key_usage_t usage = usage_arg;
5657 size_t bits = bits_arg;
5658 psa_algorithm_t alg = alg_arg;
5659 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005661 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005662
Gilles Peskine8817f612018-12-18 00:18:46 +01005663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005664
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005665 psa_set_key_usage_flags( &attributes, usage );
5666 psa_set_key_algorithm( &attributes, alg );
5667 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005668 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005669
5670 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005671 psa_status_t status = psa_generate_key( &attributes, &key );
5672
5673 if( is_large_key > 0 )
5674 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5675 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005676 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005677 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005678
5679 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005680 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005681 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5682 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005683
Gilles Peskine818ca122018-06-20 18:16:48 +02005684 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005685 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005686 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005687
5688exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005689 /*
5690 * Key attributes may have been returned by psa_get_key_attributes()
5691 * thus reset them as required.
5692 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005693 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005694
Ronald Cron5425a212020-08-04 14:58:35 +02005695 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005696 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005697}
5698/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005699
Gilles Peskinee56e8782019-04-26 17:34:02 +02005700/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5701void generate_key_rsa( int bits_arg,
5702 data_t *e_arg,
5703 int expected_status_arg )
5704{
Ronald Cron5425a212020-08-04 14:58:35 +02005705 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005706 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005707 size_t bits = bits_arg;
5708 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5709 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5710 psa_status_t expected_status = expected_status_arg;
5711 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5712 uint8_t *exported = NULL;
5713 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005714 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005715 size_t exported_length = SIZE_MAX;
5716 uint8_t *e_read_buffer = NULL;
5717 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005718 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005719 size_t e_read_length = SIZE_MAX;
5720
5721 if( e_arg->len == 0 ||
5722 ( e_arg->len == 3 &&
5723 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5724 {
5725 is_default_public_exponent = 1;
5726 e_read_size = 0;
5727 }
5728 ASSERT_ALLOC( e_read_buffer, e_read_size );
5729 ASSERT_ALLOC( exported, exported_size );
5730
5731 PSA_ASSERT( psa_crypto_init( ) );
5732
5733 psa_set_key_usage_flags( &attributes, usage );
5734 psa_set_key_algorithm( &attributes, alg );
5735 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5736 e_arg->x, e_arg->len ) );
5737 psa_set_key_bits( &attributes, bits );
5738
5739 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005740 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005741 if( expected_status != PSA_SUCCESS )
5742 goto exit;
5743
5744 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005745 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005746 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5747 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5748 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5749 e_read_buffer, e_read_size,
5750 &e_read_length ) );
5751 if( is_default_public_exponent )
5752 TEST_EQUAL( e_read_length, 0 );
5753 else
5754 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5755
5756 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005757 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005758 goto exit;
5759
5760 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005761 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005762 exported, exported_size,
5763 &exported_length ) );
5764 {
5765 uint8_t *p = exported;
5766 uint8_t *end = exported + exported_length;
5767 size_t len;
5768 /* RSAPublicKey ::= SEQUENCE {
5769 * modulus INTEGER, -- n
5770 * publicExponent INTEGER } -- e
5771 */
5772 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005773 MBEDTLS_ASN1_SEQUENCE |
5774 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005775 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5776 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5777 MBEDTLS_ASN1_INTEGER ) );
5778 if( len >= 1 && p[0] == 0 )
5779 {
5780 ++p;
5781 --len;
5782 }
5783 if( e_arg->len == 0 )
5784 {
5785 TEST_EQUAL( len, 3 );
5786 TEST_EQUAL( p[0], 1 );
5787 TEST_EQUAL( p[1], 0 );
5788 TEST_EQUAL( p[2], 1 );
5789 }
5790 else
5791 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5792 }
5793
5794exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005795 /*
5796 * Key attributes may have been returned by psa_get_key_attributes() or
5797 * set by psa_set_key_domain_parameters() thus reset them as required.
5798 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005799 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005800
Ronald Cron5425a212020-08-04 14:58:35 +02005801 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005802 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005803 mbedtls_free( e_read_buffer );
5804 mbedtls_free( exported );
5805}
5806/* END_CASE */
5807
Darryl Greend49a4992018-06-18 17:27:26 +01005808/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005809void persistent_key_load_key_from_storage( data_t *data,
5810 int type_arg, int bits_arg,
5811 int usage_flags_arg, int alg_arg,
5812 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005813{
Ronald Cron71016a92020-08-28 19:01:50 +02005814 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005815 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005816 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5817 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005818 psa_key_type_t type = type_arg;
5819 size_t bits = bits_arg;
5820 psa_key_usage_t usage_flags = usage_flags_arg;
5821 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005822 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005823 unsigned char *first_export = NULL;
5824 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005825 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005826 size_t first_exported_length;
5827 size_t second_exported_length;
5828
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005829 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5830 {
5831 ASSERT_ALLOC( first_export, export_size );
5832 ASSERT_ALLOC( second_export, export_size );
5833 }
Darryl Greend49a4992018-06-18 17:27:26 +01005834
Gilles Peskine8817f612018-12-18 00:18:46 +01005835 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005836
Gilles Peskinec87af662019-05-15 16:12:22 +02005837 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005838 psa_set_key_usage_flags( &attributes, usage_flags );
5839 psa_set_key_algorithm( &attributes, alg );
5840 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005841 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005842
Darryl Green0c6575a2018-11-07 16:05:30 +00005843 switch( generation_method )
5844 {
5845 case IMPORT_KEY:
5846 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005847 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005848 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005849 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005850
Darryl Green0c6575a2018-11-07 16:05:30 +00005851 case GENERATE_KEY:
5852 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005853 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005854 break;
5855
5856 case DERIVE_KEY:
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005857#if PSA_WANT_ALG_HKDF && PSA_WANT_ALG_SHA_256
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005858 {
5859 /* Create base key */
5860 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5861 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5862 psa_set_key_usage_flags( &base_attributes,
5863 PSA_KEY_USAGE_DERIVE );
5864 psa_set_key_algorithm( &base_attributes, derive_alg );
5865 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005866 PSA_ASSERT( psa_import_key( &base_attributes,
5867 data->x, data->len,
5868 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005869 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005870 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005871 PSA_ASSERT( psa_key_derivation_input_key(
5872 &operation,
5873 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005874 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005875 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005876 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005877 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5878 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005879 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005880 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005881 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005882 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005883 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005884#else
5885 TEST_ASSUME( ! "KDF not supported in this configuration" );
5886#endif
5887 break;
5888
5889 default:
5890 TEST_ASSERT( ! "generation_method not implemented in test" );
5891 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005892 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005893 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005894
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005895 /* Export the key if permitted by the key policy. */
5896 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5897 {
Ronald Cron5425a212020-08-04 14:58:35 +02005898 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005899 first_export, export_size,
5900 &first_exported_length ) );
5901 if( generation_method == IMPORT_KEY )
5902 ASSERT_COMPARE( data->x, data->len,
5903 first_export, first_exported_length );
5904 }
Darryl Greend49a4992018-06-18 17:27:26 +01005905
5906 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005907 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005908 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005909 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005910
Darryl Greend49a4992018-06-18 17:27:26 +01005911 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005912 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005913 TEST_ASSERT( mbedtls_svc_key_id_equal(
5914 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005915 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5916 PSA_KEY_LIFETIME_PERSISTENT );
5917 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5918 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5919 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5920 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005921
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005922 /* Export the key again if permitted by the key policy. */
5923 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005924 {
Ronald Cron5425a212020-08-04 14:58:35 +02005925 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005926 second_export, export_size,
5927 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005928 ASSERT_COMPARE( first_export, first_exported_length,
5929 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005930 }
5931
5932 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005933 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005934 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005935
5936exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005937 /*
5938 * Key attributes may have been returned by psa_get_key_attributes()
5939 * thus reset them as required.
5940 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005941 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005942
Darryl Greend49a4992018-06-18 17:27:26 +01005943 mbedtls_free( first_export );
5944 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005945 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005946 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005947 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005948 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005949}
5950/* END_CASE */