blob: 7e2b53f4e84dd7145b1915f8259b2229725d23f1 [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 */
488 if( (alg & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) == PSA_ALG_GCM )
489 nonce_length = 12;
490
Gilles Peskine818ca122018-06-20 18:16:48 +0200491 if( usage & PSA_KEY_USAGE_ENCRYPT )
492 {
Ronald Cron5425a212020-08-04 14:58:35 +0200493 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100494 nonce, nonce_length,
495 NULL, 0,
496 plaintext, sizeof( plaintext ),
497 ciphertext, sizeof( ciphertext ),
498 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200499 }
500
501 if( usage & PSA_KEY_USAGE_DECRYPT )
502 {
503 psa_status_t verify_status =
504 ( usage & PSA_KEY_USAGE_ENCRYPT ?
505 PSA_SUCCESS :
506 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200507 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100508 nonce, nonce_length,
509 NULL, 0,
510 ciphertext, ciphertext_length,
511 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100512 &plaintext_length ),
513 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200514 }
515
516 return( 1 );
517
518exit:
519 return( 0 );
520}
521
Ronald Cron5425a212020-08-04 14:58:35 +0200522static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200523 psa_key_usage_t usage,
524 psa_algorithm_t alg )
525{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200526 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
527 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100528 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200529 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100530 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
531
532 /* If the policy allows signing with any hash, just pick one. */
533 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
534 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100535#if defined(KNOWN_SUPPORTED_HASH_ALG)
536 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
537 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100538#else
Chris Jones9634bb12021-01-20 15:56:42 +0000539 mbedtls_test_fail( "No hash algorithm for hash-and-sign testing",
540 __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100541 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100542#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100543 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200544
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100545 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200546 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200547 /* Some algorithms require the payload to have the size of
548 * the hash encoded in the algorithm. Use this input size
549 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200550 if( hash_alg != 0 )
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100551 payload_length = PSA_HASH_LENGTH( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200552 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100553 payload, payload_length,
554 signature, sizeof( signature ),
555 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200556 }
557
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100558 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200559 {
560 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100561 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200562 PSA_SUCCESS :
563 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200564 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100565 payload, payload_length,
566 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100567 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200568 }
569
570 return( 1 );
571
572exit:
573 return( 0 );
574}
575
Ronald Cron5425a212020-08-04 14:58:35 +0200576static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200577 psa_key_usage_t usage,
578 psa_algorithm_t alg )
579{
580 unsigned char plaintext[256] = "Hello, world...";
581 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
582 size_t ciphertext_length = sizeof( ciphertext );
583 size_t plaintext_length = 16;
584
585 if( usage & PSA_KEY_USAGE_ENCRYPT )
586 {
Ronald Cron5425a212020-08-04 14:58:35 +0200587 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100588 plaintext, plaintext_length,
589 NULL, 0,
590 ciphertext, sizeof( ciphertext ),
591 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200592 }
593
594 if( usage & PSA_KEY_USAGE_DECRYPT )
595 {
596 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200597 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200598 ciphertext, ciphertext_length,
599 NULL, 0,
600 plaintext, sizeof( plaintext ),
601 &plaintext_length );
602 TEST_ASSERT( status == PSA_SUCCESS ||
603 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
604 ( status == PSA_ERROR_INVALID_ARGUMENT ||
605 status == PSA_ERROR_INVALID_PADDING ) ) );
606 }
607
608 return( 1 );
609
610exit:
611 return( 0 );
612}
Gilles Peskine02b75072018-07-01 22:31:34 +0200613
Janos Follathf2815ea2019-07-03 12:41:36 +0100614static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200615 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100616 psa_algorithm_t alg,
617 unsigned char* input1, size_t input1_length,
618 unsigned char* input2, size_t input2_length,
619 size_t capacity )
620{
621 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
622 if( PSA_ALG_IS_HKDF( alg ) )
623 {
624 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
625 PSA_KEY_DERIVATION_INPUT_SALT,
626 input1, input1_length ) );
627 PSA_ASSERT( psa_key_derivation_input_key( operation,
628 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200629 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100630 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
631 PSA_KEY_DERIVATION_INPUT_INFO,
632 input2,
633 input2_length ) );
634 }
635 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
636 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
637 {
638 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
639 PSA_KEY_DERIVATION_INPUT_SEED,
640 input1, input1_length ) );
641 PSA_ASSERT( psa_key_derivation_input_key( operation,
642 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200643 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100644 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
645 PSA_KEY_DERIVATION_INPUT_LABEL,
646 input2, input2_length ) );
647 }
648 else
649 {
650 TEST_ASSERT( ! "Key derivation algorithm not supported" );
651 }
652
Gilles Peskinec744d992019-07-30 17:26:54 +0200653 if( capacity != SIZE_MAX )
654 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100655
656 return( 1 );
657
658exit:
659 return( 0 );
660}
661
662
Ronald Cron5425a212020-08-04 14:58:35 +0200663static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200664 psa_key_usage_t usage,
665 psa_algorithm_t alg )
666{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200667 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100668 unsigned char input1[] = "Input 1";
669 size_t input1_length = sizeof( input1 );
670 unsigned char input2[] = "Input 2";
671 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200672 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100673 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200674
675 if( usage & PSA_KEY_USAGE_DERIVE )
676 {
Ronald Cron5425a212020-08-04 14:58:35 +0200677 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100678 input1, input1_length,
679 input2, input2_length, capacity ) )
680 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200681
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200682 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200683 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100684 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200685 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200686 }
687
688 return( 1 );
689
690exit:
691 return( 0 );
692}
693
Gilles Peskinec7998b72018-11-07 18:45:02 +0100694/* We need two keys to exercise key agreement. Exercise the
695 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200696static psa_status_t key_agreement_with_self(
697 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200698 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100699{
700 psa_key_type_t private_key_type;
701 psa_key_type_t public_key_type;
702 size_t key_bits;
703 uint8_t *public_key = NULL;
704 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200705 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200706 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
707 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200708 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100710
Ronald Cron5425a212020-08-04 14:58:35 +0200711 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200712 private_key_type = psa_get_key_type( &attributes );
713 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200714 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100715 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100716 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200717 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100718 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100719
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200720 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200721 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200722 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100723exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100724 /*
725 * Key attributes may have been returned by psa_get_key_attributes()
726 * thus reset them as required.
727 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200728 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100729
730 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100731 return( status );
732}
733
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200734/* We need two keys to exercise key agreement. Exercise the
735 * private key against its own public key. */
736static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200737 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200738{
739 psa_key_type_t private_key_type;
740 psa_key_type_t public_key_type;
741 size_t key_bits;
742 uint8_t *public_key = NULL;
743 size_t public_key_length;
744 uint8_t output[1024];
745 size_t output_length;
746 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200747 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
748 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200749 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200750 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200751
Ronald Cron5425a212020-08-04 14:58:35 +0200752 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200753 private_key_type = psa_get_key_type( &attributes );
754 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200755 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100756 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200757 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200758 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200759 public_key, public_key_length,
760 &public_key_length ) );
761
Ronald Cron5425a212020-08-04 14:58:35 +0200762 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200763 public_key, public_key_length,
764 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200765exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100766 /*
767 * Key attributes may have been returned by psa_get_key_attributes()
768 * thus reset them as required.
769 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200770 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100771
772 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200773 return( status );
774}
775
Ronald Cron5425a212020-08-04 14:58:35 +0200776static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200777 psa_key_usage_t usage,
778 psa_algorithm_t alg )
779{
780 int ok = 0;
781
782 if( usage & PSA_KEY_USAGE_DERIVE )
783 {
784 /* We need two keys to exercise key agreement. Exercise the
785 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200786 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200787 }
788 ok = 1;
789
790exit:
791 return( ok );
792}
793
Ronald Cron5425a212020-08-04 14:58:35 +0200794static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200795 psa_key_usage_t usage,
796 psa_algorithm_t alg )
797{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200798 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200799 unsigned char output[1];
800 int ok = 0;
801
802 if( usage & PSA_KEY_USAGE_DERIVE )
803 {
804 /* We need two keys to exercise key agreement. Exercise the
805 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200806 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200807 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200808 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200809 output,
810 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200811 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200812 }
813 ok = 1;
814
815exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200816 return( ok );
817}
818
Jaeden Amerof7dca862019-06-27 17:31:33 +0100819int asn1_skip_integer( unsigned char **p, const unsigned char *end,
820 size_t min_bits, size_t max_bits,
821 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200822{
823 size_t len;
824 size_t actual_bits;
825 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100826 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100827 MBEDTLS_ASN1_INTEGER ),
828 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200829
830 /* Check if the retrieved length doesn't extend the actual buffer's size.
831 * It is assumed here, that end >= p, which validates casting to size_t. */
832 TEST_ASSERT( len <= (size_t)( end - *p) );
833
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200834 /* Tolerate a slight departure from DER encoding:
835 * - 0 may be represented by an empty string or a 1-byte string.
836 * - The sign bit may be used as a value bit. */
837 if( ( len == 1 && ( *p )[0] == 0 ) ||
838 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
839 {
840 ++( *p );
841 --len;
842 }
843 if( min_bits == 0 && len == 0 )
844 return( 1 );
845 msb = ( *p )[0];
846 TEST_ASSERT( msb != 0 );
847 actual_bits = 8 * ( len - 1 );
848 while( msb != 0 )
849 {
850 msb >>= 1;
851 ++actual_bits;
852 }
853 TEST_ASSERT( actual_bits >= min_bits );
854 TEST_ASSERT( actual_bits <= max_bits );
855 if( must_be_odd )
856 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
857 *p += len;
858 return( 1 );
859exit:
860 return( 0 );
861}
862
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200863static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
864 uint8_t *exported, size_t exported_length )
865{
866 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100867 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200868 else
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100869 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200870
871#if defined(MBEDTLS_DES_C)
872 if( type == PSA_KEY_TYPE_DES )
873 {
874 /* Check the parity bits. */
875 unsigned i;
876 for( i = 0; i < bits / 8; i++ )
877 {
878 unsigned bit_count = 0;
879 unsigned m;
880 for( m = 1; m <= 0x100; m <<= 1 )
881 {
882 if( exported[i] & m )
883 ++bit_count;
884 }
885 TEST_ASSERT( bit_count % 2 != 0 );
886 }
887 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200888 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200889#endif
890
891#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200892 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200893 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200894 uint8_t *p = exported;
895 uint8_t *end = exported + exported_length;
896 size_t len;
897 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200898 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200899 * modulus INTEGER, -- n
900 * publicExponent INTEGER, -- e
901 * privateExponent INTEGER, -- d
902 * prime1 INTEGER, -- p
903 * prime2 INTEGER, -- q
904 * exponent1 INTEGER, -- d mod (p-1)
905 * exponent2 INTEGER, -- d mod (q-1)
906 * coefficient INTEGER, -- (inverse of q) mod p
907 * }
908 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100909 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
910 MBEDTLS_ASN1_SEQUENCE |
911 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
912 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200913 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
914 goto exit;
915 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
916 goto exit;
917 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
918 goto exit;
919 /* Require d to be at least half the size of n. */
920 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
921 goto exit;
922 /* Require p and q to be at most half the size of n, rounded up. */
923 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
924 goto exit;
925 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
926 goto exit;
927 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
928 goto exit;
929 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
930 goto exit;
931 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
932 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100933 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100934 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200935 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200936#endif /* MBEDTLS_RSA_C */
937
938#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200939 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200940 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100941 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100942 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100943 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200944 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200945#endif /* MBEDTLS_ECP_C */
946
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200947 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
948 {
949 uint8_t *p = exported;
950 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200951#if defined(MBEDTLS_RSA_C)
952 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
953 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100954 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200955 /* RSAPublicKey ::= SEQUENCE {
956 * modulus INTEGER, -- n
957 * publicExponent INTEGER } -- e
958 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100959 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
960 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100961 MBEDTLS_ASN1_CONSTRUCTED ),
962 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100963 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200964 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
965 goto exit;
966 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
967 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100968 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200969 }
970 else
971#endif /* MBEDTLS_RSA_C */
972#if defined(MBEDTLS_ECP_C)
973 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
974 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200975 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
976 {
977 /* The representation of an ECC Montgomery public key is
978 * the raw compressed point */
979 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
980 }
981 else
982 {
983 /* The representation of an ECC Weierstrass public key is:
984 * - The byte 0x04;
985 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
986 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
987 * - where m is the bit size associated with the curve.
988 */
989 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
990 TEST_EQUAL( p[0], 4 );
991 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200992 }
993 else
994#endif /* MBEDTLS_ECP_C */
995 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100996 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200997 mbedtls_snprintf( message, sizeof( message ),
998 "No sanity check for public key type=0x%08lx",
999 (unsigned long) type );
Chris Jones9634bb12021-01-20 15:56:42 +00001000 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +02001001 (void) p;
1002 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001003 return( 0 );
1004 }
1005 }
1006 else
1007
1008 {
1009 /* No sanity checks for other types */
1010 }
1011
1012 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001013
1014exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001015 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001016}
1017
Ronald Cron5425a212020-08-04 14:58:35 +02001018static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001019 psa_key_usage_t usage )
1020{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001022 uint8_t *exported = NULL;
1023 size_t exported_size = 0;
1024 size_t exported_length = 0;
1025 int ok = 0;
1026
Ronald Cron5425a212020-08-04 14:58:35 +02001027 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001028
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001029 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1030 psa_get_key_type( &attributes ),
1031 psa_get_key_bits( &attributes ) );
1032 ASSERT_ALLOC( exported, exported_size );
1033
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001034 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001035 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001036 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001037 TEST_EQUAL( psa_export_key( key, exported,
1038 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001039 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001040 ok = 1;
1041 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001042 }
1043
Ronald Cron5425a212020-08-04 14:58:35 +02001044 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001045 exported, exported_size,
1046 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001047 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1048 psa_get_key_bits( &attributes ),
1049 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001050
1051exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001052 /*
1053 * Key attributes may have been returned by psa_get_key_attributes()
1054 * thus reset them as required.
1055 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001056 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001057
1058 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001059 return( ok );
1060}
1061
Ronald Cron5425a212020-08-04 14:58:35 +02001062static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001063{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001065 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001066 uint8_t *exported = NULL;
1067 size_t exported_size = 0;
1068 size_t exported_length = 0;
1069 int ok = 0;
1070
Ronald Cron5425a212020-08-04 14:58:35 +02001071 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001072 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001073 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001074 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1075 psa_get_key_type( &attributes ),
1076 psa_get_key_bits( &attributes ) );
1077 ASSERT_ALLOC( exported, exported_size );
1078
1079 TEST_EQUAL( psa_export_public_key( key, exported,
1080 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001081 PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001082 ok = 1;
1083 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001084 }
1085
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001086 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001087 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001088 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1089 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001090 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001091
Ronald Cron5425a212020-08-04 14:58:35 +02001092 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001093 exported, exported_size,
1094 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001095 ok = exported_key_sanity_check( public_type,
1096 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001097 exported, exported_length );
1098
1099exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001100 /*
1101 * Key attributes may have been returned by psa_get_key_attributes()
1102 * thus reset them as required.
1103 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001104 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001105
1106 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001107 return( ok );
1108}
1109
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001110/** Do smoke tests on a key.
1111 *
1112 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1113 * sign/verify, or derivation) that is permitted according to \p usage.
1114 * \p usage and \p alg should correspond to the expected policy on the
1115 * key.
1116 *
1117 * Export the key if permitted by \p usage, and check that the output
1118 * looks sensible. If \p usage forbids export, check that
1119 * \p psa_export_key correctly rejects the attempt. If the key is
1120 * asymmetric, also check \p psa_export_public_key.
1121 *
1122 * If the key fails the tests, this function calls the test framework's
Chris Jones9634bb12021-01-20 15:56:42 +00001123 * `mbedtls_test_fail` function and returns false. Otherwise this function
1124 * returns true. Therefore it should be used as follows:
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001125 * ```
1126 * if( ! exercise_key( ... ) ) goto exit;
1127 * ```
1128 *
Ronald Cron5425a212020-08-04 14:58:35 +02001129 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001130 * \p alg.
1131 * \param usage The usage flags to assume.
1132 * \param alg The algorithm to exercise.
1133 *
1134 * \retval 0 The key failed the smoke tests.
1135 * \retval 1 The key passed the smoke tests.
1136 */
Ronald Cron5425a212020-08-04 14:58:35 +02001137static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001138 psa_key_usage_t usage,
1139 psa_algorithm_t alg )
1140{
1141 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001142
Ronald Cron5425a212020-08-04 14:58:35 +02001143 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001144 return( 0 );
1145
Gilles Peskine02b75072018-07-01 22:31:34 +02001146 if( alg == 0 )
1147 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1148 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001149 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001150 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001151 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001152 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001153 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001154 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001155 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001156 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001157 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001158 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001159 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001160 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001161 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001162 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001163 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001164 else
1165 {
1166 char message[40];
1167 mbedtls_snprintf( message, sizeof( message ),
1168 "No code to exercise alg=0x%08lx",
1169 (unsigned long) alg );
Chris Jones9634bb12021-01-20 15:56:42 +00001170 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskine02b75072018-07-01 22:31:34 +02001171 ok = 0;
1172 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001173
Ronald Cron5425a212020-08-04 14:58:35 +02001174 ok = ok && exercise_export_key( key, usage );
1175 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001176
Gilles Peskine02b75072018-07-01 22:31:34 +02001177 return( ok );
1178}
1179
Gilles Peskine10df3412018-10-25 22:35:43 +02001180static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1181 psa_algorithm_t alg )
1182{
1183 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1184 {
1185 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001186 PSA_KEY_USAGE_VERIFY_HASH :
1187 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001188 }
1189 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1190 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1191 {
1192 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1193 PSA_KEY_USAGE_ENCRYPT :
1194 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1195 }
1196 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1197 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1198 {
1199 return( PSA_KEY_USAGE_DERIVE );
1200 }
1201 else
1202 {
1203 return( 0 );
1204 }
1205
1206}
Darryl Green0c6575a2018-11-07 16:05:30 +00001207
Ronald Cron5425a212020-08-04 14:58:35 +02001208static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001209{
1210 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001211 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001212 uint8_t buffer[1];
1213 size_t length;
1214 int ok = 0;
1215
Ronald Cronecfb2372020-07-23 17:13:42 +02001216 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001217 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1218 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1219 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001220 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001221 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001222 TEST_EQUAL(
1223 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1224 TEST_EQUAL(
1225 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001226 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001227 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1228 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1229 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1230 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1231
Ronald Cron5425a212020-08-04 14:58:35 +02001232 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001233 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001234 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001235 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001236 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001237
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001238 ok = 1;
1239
1240exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001241 /*
1242 * Key attributes may have been returned by psa_get_key_attributes()
1243 * thus reset them as required.
1244 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001245 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001246
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001247 return( ok );
1248}
1249
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001250/* Assert that a key isn't reported as having a slot number. */
1251#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1252#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1253 do \
1254 { \
1255 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1256 TEST_EQUAL( psa_get_key_slot_number( \
1257 attributes, \
1258 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1259 PSA_ERROR_INVALID_ARGUMENT ); \
1260 } \
1261 while( 0 )
1262#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1263#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1264 ( (void) 0 )
1265#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1266
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001267/* An overapproximation of the amount of storage needed for a key of the
1268 * given type and with the given content. The API doesn't make it easy
1269 * to find a good value for the size. The current implementation doesn't
1270 * care about the value anyway. */
1271#define KEY_BITS_FROM_DATA( type, data ) \
1272 ( data )->len
1273
Darryl Green0c6575a2018-11-07 16:05:30 +00001274typedef enum {
1275 IMPORT_KEY = 0,
1276 GENERATE_KEY = 1,
1277 DERIVE_KEY = 2
1278} generate_method;
1279
Gilles Peskinee59236f2018-01-27 23:32:46 +01001280/* END_HEADER */
1281
1282/* BEGIN_DEPENDENCIES
1283 * depends_on:MBEDTLS_PSA_CRYPTO_C
1284 * END_DEPENDENCIES
1285 */
1286
1287/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001288void static_checks( )
1289{
1290 size_t max_truncated_mac_size =
1291 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1292
1293 /* Check that the length for a truncated MAC always fits in the algorithm
1294 * encoding. The shifted mask is the maximum truncated value. The
1295 * untruncated algorithm may be one byte larger. */
1296 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001297
1298#if defined(MBEDTLS_TEST_DEPRECATED)
1299 /* Check deprecated constants. */
1300 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1301 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1302 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1303 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1304 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1305 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1306 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1307 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001308
Paul Elliott8ff510a2020-06-02 17:19:28 +01001309 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1310 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1311 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1312 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1314 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1326 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1328 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1329 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1330 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1331 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1332 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1333 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1334 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1335 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1336 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1337 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1338 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1339
1340 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1341 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1342 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1343 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1344 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1345 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1346 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1347 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001348
Paul Elliott75e27032020-06-03 15:17:39 +01001349 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1350 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1351 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1352 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1353 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1354
1355 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1356 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001357#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001358}
1359/* END_CASE */
1360
1361/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001362void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001363 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001364 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001365{
Gilles Peskine4747d192019-04-17 15:05:45 +02001366 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001367 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001368 psa_key_lifetime_t lifetime = lifetime_arg;
1369 psa_key_usage_t usage_flags = usage_flags_arg;
1370 psa_algorithm_t alg = alg_arg;
1371 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001372 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001373
Ronald Cronecfb2372020-07-23 17:13:42 +02001374 TEST_EQUAL(
1375 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1376 TEST_EQUAL(
1377 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001378 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1379 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1380 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1381 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001382 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001383
Gilles Peskinec87af662019-05-15 16:12:22 +02001384 psa_set_key_id( &attributes, id );
1385 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001386 psa_set_key_usage_flags( &attributes, usage_flags );
1387 psa_set_key_algorithm( &attributes, alg );
1388 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001389 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001390
Ronald Cronecfb2372020-07-23 17:13:42 +02001391 TEST_ASSERT( mbedtls_svc_key_id_equal(
1392 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001393 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1394 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1395 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1396 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001397 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001398
1399 psa_reset_key_attributes( &attributes );
1400
Ronald Cronecfb2372020-07-23 17:13:42 +02001401 TEST_EQUAL(
1402 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1403 TEST_EQUAL(
1404 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001405 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1406 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1407 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1408 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001409 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001410}
1411/* END_CASE */
1412
1413/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001414void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1415 int id2_arg, int owner_id2_arg,
1416 int expected_id_arg, int expected_owner_id_arg,
1417 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001418{
1419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001420 mbedtls_svc_key_id_t id1 =
1421 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001422 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001423 mbedtls_svc_key_id_t id2 =
1424 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001425 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001426 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001427 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1428
1429 if( id1_arg != -1 )
1430 psa_set_key_id( &attributes, id1 );
1431 if( lifetime_arg != -1 )
1432 psa_set_key_lifetime( &attributes, lifetime );
1433 if( id2_arg != -1 )
1434 psa_set_key_id( &attributes, id2 );
1435
Ronald Cronecfb2372020-07-23 17:13:42 +02001436 TEST_ASSERT( mbedtls_svc_key_id_equal(
1437 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001438 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1439}
1440/* END_CASE */
1441
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001442/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1443void slot_number_attribute( )
1444{
1445 psa_key_slot_number_t slot_number = 0xdeadbeef;
1446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1447
1448 /* Initially, there is no slot number. */
1449 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1450 PSA_ERROR_INVALID_ARGUMENT );
1451
1452 /* Test setting a slot number. */
1453 psa_set_key_slot_number( &attributes, 0 );
1454 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1455 TEST_EQUAL( slot_number, 0 );
1456
1457 /* Test changing the slot number. */
1458 psa_set_key_slot_number( &attributes, 42 );
1459 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1460 TEST_EQUAL( slot_number, 42 );
1461
1462 /* Test clearing the slot number. */
1463 psa_clear_key_slot_number( &attributes );
1464 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1465 PSA_ERROR_INVALID_ARGUMENT );
1466
1467 /* Clearing again should have no effect. */
1468 psa_clear_key_slot_number( &attributes );
1469 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1470 PSA_ERROR_INVALID_ARGUMENT );
1471
1472 /* Test that reset clears the slot number. */
1473 psa_set_key_slot_number( &attributes, 42 );
1474 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1475 TEST_EQUAL( slot_number, 42 );
1476 psa_reset_key_attributes( &attributes );
1477 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1478 PSA_ERROR_INVALID_ARGUMENT );
1479}
1480/* END_CASE */
1481
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001482/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001483void import_with_policy( int type_arg,
1484 int usage_arg, int alg_arg,
1485 int expected_status_arg )
1486{
1487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1488 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001489 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001490 psa_key_type_t type = type_arg;
1491 psa_key_usage_t usage = usage_arg;
1492 psa_algorithm_t alg = alg_arg;
1493 psa_status_t expected_status = expected_status_arg;
1494 const uint8_t key_material[16] = {0};
1495 psa_status_t status;
1496
1497 PSA_ASSERT( psa_crypto_init( ) );
1498
1499 psa_set_key_type( &attributes, type );
1500 psa_set_key_usage_flags( &attributes, usage );
1501 psa_set_key_algorithm( &attributes, alg );
1502
1503 status = psa_import_key( &attributes,
1504 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001505 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001506 TEST_EQUAL( status, expected_status );
1507 if( status != PSA_SUCCESS )
1508 goto exit;
1509
Ronald Cron5425a212020-08-04 14:58:35 +02001510 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001511 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1512 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1513 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001514 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001515
Ronald Cron5425a212020-08-04 14:58:35 +02001516 PSA_ASSERT( psa_destroy_key( key ) );
1517 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001518
1519exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001520 /*
1521 * Key attributes may have been returned by psa_get_key_attributes()
1522 * thus reset them as required.
1523 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001524 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001525
1526 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001527 PSA_DONE( );
1528}
1529/* END_CASE */
1530
1531/* BEGIN_CASE */
1532void import_with_data( data_t *data, int type_arg,
1533 int attr_bits_arg,
1534 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001535{
1536 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1537 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001538 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001539 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001540 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001541 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001542 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001543
Gilles Peskine8817f612018-12-18 00:18:46 +01001544 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545
Gilles Peskine4747d192019-04-17 15:05:45 +02001546 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001547 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001548
Ronald Cron5425a212020-08-04 14:58:35 +02001549 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001550 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001551 if( status != PSA_SUCCESS )
1552 goto exit;
1553
Ronald Cron5425a212020-08-04 14:58:35 +02001554 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001555 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001556 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001557 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001558 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001559
Ronald Cron5425a212020-08-04 14:58:35 +02001560 PSA_ASSERT( psa_destroy_key( key ) );
1561 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001562
1563exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001564 /*
1565 * Key attributes may have been returned by psa_get_key_attributes()
1566 * thus reset them as required.
1567 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001568 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001569
1570 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001571 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001572}
1573/* END_CASE */
1574
1575/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001576void import_large_key( int type_arg, int byte_size_arg,
1577 int expected_status_arg )
1578{
1579 psa_key_type_t type = type_arg;
1580 size_t byte_size = byte_size_arg;
1581 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1582 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001583 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001584 psa_status_t status;
1585 uint8_t *buffer = NULL;
1586 size_t buffer_size = byte_size + 1;
1587 size_t n;
1588
Steven Cooreman69967ce2021-01-18 18:01:08 +01001589 /* Skip the test case if the target running the test cannot
1590 * accomodate large keys due to heap size constraints */
1591 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001592 memset( buffer, 'K', byte_size );
1593
1594 PSA_ASSERT( psa_crypto_init( ) );
1595
1596 /* Try importing the key */
1597 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1598 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001599 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001600 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001601 TEST_EQUAL( status, expected_status );
1602
1603 if( status == PSA_SUCCESS )
1604 {
Ronald Cron5425a212020-08-04 14:58:35 +02001605 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001606 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1607 TEST_EQUAL( psa_get_key_bits( &attributes ),
1608 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001609 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001610 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001611 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001612 for( n = 0; n < byte_size; n++ )
1613 TEST_EQUAL( buffer[n], 'K' );
1614 for( n = byte_size; n < buffer_size; n++ )
1615 TEST_EQUAL( buffer[n], 0 );
1616 }
1617
1618exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001619 /*
1620 * Key attributes may have been returned by psa_get_key_attributes()
1621 * thus reset them as required.
1622 */
1623 psa_reset_key_attributes( &attributes );
1624
Ronald Cron5425a212020-08-04 14:58:35 +02001625 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001626 PSA_DONE( );
1627 mbedtls_free( buffer );
1628}
1629/* END_CASE */
1630
1631/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001632void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1633{
Ronald Cron5425a212020-08-04 14:58:35 +02001634 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001635 size_t bits = bits_arg;
1636 psa_status_t expected_status = expected_status_arg;
1637 psa_status_t status;
1638 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001639 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001640 size_t buffer_size = /* Slight overapproximations */
1641 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001642 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001643 unsigned char *p;
1644 int ret;
1645 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001647
Gilles Peskine8817f612018-12-18 00:18:46 +01001648 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001649 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001650
1651 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1652 bits, keypair ) ) >= 0 );
1653 length = ret;
1654
1655 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001656 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001657 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001658 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001659
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001660 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001661 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001662
1663exit:
1664 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001665 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001666}
1667/* END_CASE */
1668
1669/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001670void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001671 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001672 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001673 int expected_bits,
1674 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001675 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001676 int canonical_input )
1677{
Ronald Cron5425a212020-08-04 14:58:35 +02001678 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001679 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001680 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001681 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001682 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001683 unsigned char *exported = NULL;
1684 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001685 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001686 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001687 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001688 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001689 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001690
Moran Pekercb088e72018-07-17 17:36:59 +03001691 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001692 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001693 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001694 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001695 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001696
Gilles Peskine4747d192019-04-17 15:05:45 +02001697 psa_set_key_usage_flags( &attributes, usage_arg );
1698 psa_set_key_algorithm( &attributes, alg );
1699 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001700
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001701 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001702 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001703
1704 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001705 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001706 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1707 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001708 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001709
1710 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001711 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001712 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001713
1714 /* The exported length must be set by psa_export_key() to a value between 0
1715 * and export_size. On errors, the exported length must be 0. */
1716 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1717 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1718 TEST_ASSERT( exported_length <= export_size );
1719
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001720 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001721 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001722 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001723 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001724 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001725 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001726 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001727
Ronald Cron5425a212020-08-04 14:58:35 +02001728 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001729 goto exit;
1730
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001731 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001732 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001733 else
1734 {
Ronald Cron5425a212020-08-04 14:58:35 +02001735 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001736 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001737 &key2 ) );
1738 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001739 reexported,
1740 export_size,
1741 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001742 ASSERT_COMPARE( exported, exported_length,
1743 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001744 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001745 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001746 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001747
1748destroy:
1749 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001750 PSA_ASSERT( psa_destroy_key( key ) );
1751 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001752
1753exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001754 /*
1755 * Key attributes may have been returned by psa_get_key_attributes()
1756 * thus reset them as required.
1757 */
1758 psa_reset_key_attributes( &got_attributes );
1759
itayzafrir3e02b3b2018-06-12 17:06:52 +03001760 mbedtls_free( exported );
1761 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001762 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001763}
1764/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001765
Moran Pekerf709f4a2018-06-06 17:26:04 +03001766/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001767void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001768 int type_arg,
1769 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001770 int export_size_delta,
1771 int expected_export_status_arg,
1772 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001773{
Ronald Cron5425a212020-08-04 14:58:35 +02001774 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001775 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001776 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001777 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001778 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001779 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001780 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001781 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001783
Gilles Peskine8817f612018-12-18 00:18:46 +01001784 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001785
Gilles Peskine4747d192019-04-17 15:05:45 +02001786 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1787 psa_set_key_algorithm( &attributes, alg );
1788 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001789
1790 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001791 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001792
Gilles Peskine49c25912018-10-29 15:15:31 +01001793 /* Export the public key */
1794 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001795 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001796 exported, export_size,
1797 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001798 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001799 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001800 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001801 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001802 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001803 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001804 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001805 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001806 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001807 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1808 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001809 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001810
1811exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001812 /*
1813 * Key attributes may have been returned by psa_get_key_attributes()
1814 * thus reset them as required.
1815 */
1816 psa_reset_key_attributes( &attributes );
1817
itayzafrir3e02b3b2018-06-12 17:06:52 +03001818 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001819 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001820 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001821}
1822/* END_CASE */
1823
Gilles Peskine20035e32018-02-03 22:44:14 +01001824/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001825void import_and_exercise_key( data_t *data,
1826 int type_arg,
1827 int bits_arg,
1828 int alg_arg )
1829{
Ronald Cron5425a212020-08-04 14:58:35 +02001830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001831 psa_key_type_t type = type_arg;
1832 size_t bits = bits_arg;
1833 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001834 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001835 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001836 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001837
Gilles Peskine8817f612018-12-18 00:18:46 +01001838 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001839
Gilles Peskine4747d192019-04-17 15:05:45 +02001840 psa_set_key_usage_flags( &attributes, usage );
1841 psa_set_key_algorithm( &attributes, alg );
1842 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001843
1844 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001845 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001846
1847 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001848 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001849 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1850 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001851
1852 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001853 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001854 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001855
Ronald Cron5425a212020-08-04 14:58:35 +02001856 PSA_ASSERT( psa_destroy_key( key ) );
1857 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001858
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001859exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001860 /*
1861 * Key attributes may have been returned by psa_get_key_attributes()
1862 * thus reset them as required.
1863 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001864 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001865
1866 psa_reset_key_attributes( &attributes );
1867 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001868 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001869}
1870/* END_CASE */
1871
1872/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001873void effective_key_attributes( int type_arg, int expected_type_arg,
1874 int bits_arg, int expected_bits_arg,
1875 int usage_arg, int expected_usage_arg,
1876 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001877{
Ronald Cron5425a212020-08-04 14:58:35 +02001878 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001879 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001880 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001881 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001882 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001883 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001884 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001885 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001886 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001888
Gilles Peskine8817f612018-12-18 00:18:46 +01001889 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001890
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001891 psa_set_key_usage_flags( &attributes, usage );
1892 psa_set_key_algorithm( &attributes, alg );
1893 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001894 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001895
Ronald Cron5425a212020-08-04 14:58:35 +02001896 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001897 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001898
Ronald Cron5425a212020-08-04 14:58:35 +02001899 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001900 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1901 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1902 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1903 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001904
1905exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001906 /*
1907 * Key attributes may have been returned by psa_get_key_attributes()
1908 * thus reset them as required.
1909 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001910 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001911
1912 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001913 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001914}
1915/* END_CASE */
1916
1917/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001918void check_key_policy( int type_arg, int bits_arg,
1919 int usage_arg, int alg_arg )
1920{
1921 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1922 usage_arg, usage_arg, alg_arg, alg_arg );
1923 goto exit;
1924}
1925/* END_CASE */
1926
1927/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001928void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001929{
1930 /* Test each valid way of initializing the object, except for `= {0}`, as
1931 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1932 * though it's OK by the C standard. We could test for this, but we'd need
1933 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001934 psa_key_attributes_t func = psa_key_attributes_init( );
1935 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1936 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001937
1938 memset( &zero, 0, sizeof( zero ) );
1939
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001940 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1941 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1942 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001943
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001944 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1945 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1946 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1947
1948 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1949 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1950 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1951
1952 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1953 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1954 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1955
1956 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1957 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1958 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001959}
1960/* END_CASE */
1961
1962/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001963void mac_key_policy( int policy_usage,
1964 int policy_alg,
1965 int key_type,
1966 data_t *key_data,
1967 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001968{
Ronald Cron5425a212020-08-04 14:58:35 +02001969 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001970 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001971 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001972 psa_status_t status;
1973 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001974
Gilles Peskine8817f612018-12-18 00:18:46 +01001975 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001976
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001977 psa_set_key_usage_flags( &attributes, policy_usage );
1978 psa_set_key_algorithm( &attributes, policy_alg );
1979 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001980
Gilles Peskine049c7532019-05-15 20:22:09 +02001981 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001982 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001983
Ronald Cron5425a212020-08-04 14:58:35 +02001984 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001986 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001987 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001988 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001989 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001991
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001992 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001993 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001995 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001996 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001997 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001998 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001999
2000exit:
2001 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002002 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002003 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004}
2005/* END_CASE */
2006
2007/* BEGIN_CASE */
2008void cipher_key_policy( int policy_usage,
2009 int policy_alg,
2010 int key_type,
2011 data_t *key_data,
2012 int exercise_alg )
2013{
Ronald Cron5425a212020-08-04 14:58:35 +02002014 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002016 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017 psa_status_t status;
2018
Gilles Peskine8817f612018-12-18 00:18:46 +01002019 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002021 psa_set_key_usage_flags( &attributes, policy_usage );
2022 psa_set_key_algorithm( &attributes, policy_alg );
2023 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024
Gilles Peskine049c7532019-05-15 20:22:09 +02002025 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002026 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002027
Ronald Cron5425a212020-08-04 14:58:35 +02002028 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002029 if( policy_alg == exercise_alg &&
2030 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002031 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002033 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034 psa_cipher_abort( &operation );
2035
Ronald Cron5425a212020-08-04 14:58:35 +02002036 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037 if( policy_alg == exercise_alg &&
2038 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002039 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002040 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002041 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002042
2043exit:
2044 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002045 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002046 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002047}
2048/* END_CASE */
2049
2050/* BEGIN_CASE */
2051void aead_key_policy( int policy_usage,
2052 int policy_alg,
2053 int key_type,
2054 data_t *key_data,
2055 int nonce_length_arg,
2056 int tag_length_arg,
2057 int exercise_alg )
2058{
Ronald Cron5425a212020-08-04 14:58:35 +02002059 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061 psa_status_t status;
2062 unsigned char nonce[16] = {0};
2063 size_t nonce_length = nonce_length_arg;
2064 unsigned char tag[16];
2065 size_t tag_length = tag_length_arg;
2066 size_t output_length;
2067
2068 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
2069 TEST_ASSERT( tag_length <= sizeof( tag ) );
2070
Gilles Peskine8817f612018-12-18 00:18:46 +01002071 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002072
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002073 psa_set_key_usage_flags( &attributes, policy_usage );
2074 psa_set_key_algorithm( &attributes, policy_alg );
2075 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002076
Gilles Peskine049c7532019-05-15 20:22:09 +02002077 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002078 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079
Ronald Cron5425a212020-08-04 14:58:35 +02002080 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081 nonce, nonce_length,
2082 NULL, 0,
2083 NULL, 0,
2084 tag, tag_length,
2085 &output_length );
2086 if( policy_alg == exercise_alg &&
2087 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002088 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002089 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002090 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002091
2092 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002093 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094 nonce, nonce_length,
2095 NULL, 0,
2096 tag, tag_length,
2097 NULL, 0,
2098 &output_length );
2099 if( policy_alg == exercise_alg &&
2100 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002101 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002102 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002103 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002104
2105exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002106 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002107 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108}
2109/* END_CASE */
2110
2111/* BEGIN_CASE */
2112void asymmetric_encryption_key_policy( int policy_usage,
2113 int policy_alg,
2114 int key_type,
2115 data_t *key_data,
2116 int exercise_alg )
2117{
Ronald Cron5425a212020-08-04 14:58:35 +02002118 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002119 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002120 psa_status_t status;
2121 size_t key_bits;
2122 size_t buffer_length;
2123 unsigned char *buffer = NULL;
2124 size_t output_length;
2125
Gilles Peskine8817f612018-12-18 00:18:46 +01002126 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002127
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002128 psa_set_key_usage_flags( &attributes, policy_usage );
2129 psa_set_key_algorithm( &attributes, policy_alg );
2130 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002131
Gilles Peskine049c7532019-05-15 20:22:09 +02002132 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002133 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002134
Ronald Cron5425a212020-08-04 14:58:35 +02002135 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002136 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002137 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2138 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002139 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002140
Ronald Cron5425a212020-08-04 14:58:35 +02002141 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002142 NULL, 0,
2143 NULL, 0,
2144 buffer, buffer_length,
2145 &output_length );
2146 if( policy_alg == exercise_alg &&
2147 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002148 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002149 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002150 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002151
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002152 if( buffer_length != 0 )
2153 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002154 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002155 buffer, buffer_length,
2156 NULL, 0,
2157 buffer, buffer_length,
2158 &output_length );
2159 if( policy_alg == exercise_alg &&
2160 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002161 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002162 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002163 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002164
2165exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002166 /*
2167 * Key attributes may have been returned by psa_get_key_attributes()
2168 * thus reset them as required.
2169 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002170 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002171
2172 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002173 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002174 mbedtls_free( buffer );
2175}
2176/* END_CASE */
2177
2178/* BEGIN_CASE */
2179void asymmetric_signature_key_policy( int policy_usage,
2180 int policy_alg,
2181 int key_type,
2182 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002183 int exercise_alg,
2184 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002185{
Ronald Cron5425a212020-08-04 14:58:35 +02002186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002188 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002189 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2190 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2191 * compatible with the policy and `payload_length_arg` is supposed to be
2192 * a valid input length to sign. If `payload_length_arg <= 0`,
2193 * `exercise_alg` is supposed to be forbidden by the policy. */
2194 int compatible_alg = payload_length_arg > 0;
2195 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002196 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002197 size_t signature_length;
2198
Gilles Peskine8817f612018-12-18 00:18:46 +01002199 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002200
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002201 psa_set_key_usage_flags( &attributes, policy_usage );
2202 psa_set_key_algorithm( &attributes, policy_alg );
2203 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002204
Gilles Peskine049c7532019-05-15 20:22:09 +02002205 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002206 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002207
Ronald Cron5425a212020-08-04 14:58:35 +02002208 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002209 payload, payload_length,
2210 signature, sizeof( signature ),
2211 &signature_length );
2212 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002213 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002214 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002215 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002216
2217 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002218 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002219 payload, payload_length,
2220 signature, sizeof( signature ) );
2221 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002222 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002223 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002224 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002225
2226exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002227 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002228 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002229}
2230/* END_CASE */
2231
Janos Follathba3fab92019-06-11 14:50:16 +01002232/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002233void derive_key_policy( int policy_usage,
2234 int policy_alg,
2235 int key_type,
2236 data_t *key_data,
2237 int exercise_alg )
2238{
Ronald Cron5425a212020-08-04 14:58:35 +02002239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002241 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002242 psa_status_t status;
2243
Gilles Peskine8817f612018-12-18 00:18:46 +01002244 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002245
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002246 psa_set_key_usage_flags( &attributes, policy_usage );
2247 psa_set_key_algorithm( &attributes, policy_alg );
2248 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002249
Gilles Peskine049c7532019-05-15 20:22:09 +02002250 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002251 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002252
Janos Follathba3fab92019-06-11 14:50:16 +01002253 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2254
2255 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2256 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002257 {
Janos Follathba3fab92019-06-11 14:50:16 +01002258 PSA_ASSERT( psa_key_derivation_input_bytes(
2259 &operation,
2260 PSA_KEY_DERIVATION_INPUT_SEED,
2261 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002262 }
Janos Follathba3fab92019-06-11 14:50:16 +01002263
2264 status = psa_key_derivation_input_key( &operation,
2265 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002266 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002267
Gilles Peskineea0fb492018-07-12 17:17:20 +02002268 if( policy_alg == exercise_alg &&
2269 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002270 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002271 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002272 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002273
2274exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002275 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002276 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002277 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002278}
2279/* END_CASE */
2280
2281/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002282void agreement_key_policy( int policy_usage,
2283 int policy_alg,
2284 int key_type_arg,
2285 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002286 int exercise_alg,
2287 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002288{
Ronald Cron5425a212020-08-04 14:58:35 +02002289 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002291 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002292 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002293 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002294 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002295
Gilles Peskine8817f612018-12-18 00:18:46 +01002296 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002297
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002298 psa_set_key_usage_flags( &attributes, policy_usage );
2299 psa_set_key_algorithm( &attributes, policy_alg );
2300 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002301
Gilles Peskine049c7532019-05-15 20:22:09 +02002302 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002303 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002304
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002305 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002306 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002307
Steven Cooremance48e852020-10-05 16:02:45 +02002308 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002309
2310exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002311 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002312 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002313 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002314}
2315/* END_CASE */
2316
2317/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002318void key_policy_alg2( int key_type_arg, data_t *key_data,
2319 int usage_arg, int alg_arg, int alg2_arg )
2320{
Ronald Cron5425a212020-08-04 14:58:35 +02002321 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002322 psa_key_type_t key_type = key_type_arg;
2323 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2324 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2325 psa_key_usage_t usage = usage_arg;
2326 psa_algorithm_t alg = alg_arg;
2327 psa_algorithm_t alg2 = alg2_arg;
2328
2329 PSA_ASSERT( psa_crypto_init( ) );
2330
2331 psa_set_key_usage_flags( &attributes, usage );
2332 psa_set_key_algorithm( &attributes, alg );
2333 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2334 psa_set_key_type( &attributes, key_type );
2335 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002336 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002337
Ronald Cron5425a212020-08-04 14:58:35 +02002338 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002339 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2340 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2341 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2342
Ronald Cron5425a212020-08-04 14:58:35 +02002343 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002344 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002345 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002346 goto exit;
2347
2348exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002349 /*
2350 * Key attributes may have been returned by psa_get_key_attributes()
2351 * thus reset them as required.
2352 */
2353 psa_reset_key_attributes( &got_attributes );
2354
Ronald Cron5425a212020-08-04 14:58:35 +02002355 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002356 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002357}
2358/* END_CASE */
2359
2360/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002361void raw_agreement_key_policy( int policy_usage,
2362 int policy_alg,
2363 int key_type_arg,
2364 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002365 int exercise_alg,
2366 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002367{
Ronald Cron5425a212020-08-04 14:58:35 +02002368 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002370 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002371 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002372 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002373 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002374
2375 PSA_ASSERT( psa_crypto_init( ) );
2376
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002377 psa_set_key_usage_flags( &attributes, policy_usage );
2378 psa_set_key_algorithm( &attributes, policy_alg );
2379 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002380
Gilles Peskine049c7532019-05-15 20:22:09 +02002381 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002382 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002383
Ronald Cron5425a212020-08-04 14:58:35 +02002384 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002385
Steven Cooremance48e852020-10-05 16:02:45 +02002386 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002387
2388exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002389 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002390 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002391 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002392}
2393/* END_CASE */
2394
2395/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002396void copy_success( int source_usage_arg,
2397 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002398 int type_arg, data_t *material,
2399 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002400 int target_usage_arg,
2401 int target_alg_arg, int target_alg2_arg,
2402 int expected_usage_arg,
2403 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002404{
Gilles Peskineca25db92019-04-19 11:43:08 +02002405 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2406 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002407 psa_key_usage_t expected_usage = expected_usage_arg;
2408 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002409 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002410 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2411 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002412 uint8_t *export_buffer = NULL;
2413
Gilles Peskine57ab7212019-01-28 13:03:09 +01002414 PSA_ASSERT( psa_crypto_init( ) );
2415
Gilles Peskineca25db92019-04-19 11:43:08 +02002416 /* Prepare the source key. */
2417 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2418 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002419 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002420 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002421 PSA_ASSERT( psa_import_key( &source_attributes,
2422 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002423 &source_key ) );
2424 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002425
Gilles Peskineca25db92019-04-19 11:43:08 +02002426 /* Prepare the target attributes. */
2427 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002428 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002429 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002430 /* Set volatile lifetime to reset the key identifier to 0. */
2431 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2432 }
2433
Gilles Peskineca25db92019-04-19 11:43:08 +02002434 if( target_usage_arg != -1 )
2435 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2436 if( target_alg_arg != -1 )
2437 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002438 if( target_alg2_arg != -1 )
2439 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002440
2441 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002442 PSA_ASSERT( psa_copy_key( source_key,
2443 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002444
2445 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002446 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002447
2448 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002449 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002450 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2451 psa_get_key_type( &target_attributes ) );
2452 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2453 psa_get_key_bits( &target_attributes ) );
2454 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2455 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002456 TEST_EQUAL( expected_alg2,
2457 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002458 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2459 {
2460 size_t length;
2461 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002462 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002463 material->len, &length ) );
2464 ASSERT_COMPARE( material->x, material->len,
2465 export_buffer, length );
2466 }
Ronald Cron5425a212020-08-04 14:58:35 +02002467 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002469 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002470 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002471
Ronald Cron5425a212020-08-04 14:58:35 +02002472 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002473
2474exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002475 /*
2476 * Source and target key attributes may have been returned by
2477 * psa_get_key_attributes() thus reset them as required.
2478 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002479 psa_reset_key_attributes( &source_attributes );
2480 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002481
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002482 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002483 mbedtls_free( export_buffer );
2484}
2485/* END_CASE */
2486
2487/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002488void copy_fail( int source_usage_arg,
2489 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002490 int type_arg, data_t *material,
2491 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002492 int target_usage_arg,
2493 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002494 int expected_status_arg )
2495{
2496 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2497 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002498 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2499 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002500
2501 PSA_ASSERT( psa_crypto_init( ) );
2502
2503 /* Prepare the source key. */
2504 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2505 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002506 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002507 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002508 PSA_ASSERT( psa_import_key( &source_attributes,
2509 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002510 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002511
2512 /* Prepare the target attributes. */
2513 psa_set_key_type( &target_attributes, target_type_arg );
2514 psa_set_key_bits( &target_attributes, target_bits_arg );
2515 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2516 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002517 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002518
2519 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002520 TEST_EQUAL( psa_copy_key( source_key,
2521 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002522 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002523
Ronald Cron5425a212020-08-04 14:58:35 +02002524 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002525
Gilles Peskine4a644642019-05-03 17:14:08 +02002526exit:
2527 psa_reset_key_attributes( &source_attributes );
2528 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002529 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002530}
2531/* END_CASE */
2532
2533/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002534void hash_operation_init( )
2535{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002536 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002537 /* Test each valid way of initializing the object, except for `= {0}`, as
2538 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2539 * though it's OK by the C standard. We could test for this, but we'd need
2540 * to supress the Clang warning for the test. */
2541 psa_hash_operation_t func = psa_hash_operation_init( );
2542 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2543 psa_hash_operation_t zero;
2544
2545 memset( &zero, 0, sizeof( zero ) );
2546
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002547 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002548 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2549 PSA_ERROR_BAD_STATE );
2550 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2551 PSA_ERROR_BAD_STATE );
2552 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2553 PSA_ERROR_BAD_STATE );
2554
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002555 /* A default hash operation should be abortable without error. */
2556 PSA_ASSERT( psa_hash_abort( &func ) );
2557 PSA_ASSERT( psa_hash_abort( &init ) );
2558 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002559}
2560/* END_CASE */
2561
2562/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563void hash_setup( int alg_arg,
2564 int expected_status_arg )
2565{
2566 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002567 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002568 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002569 psa_status_t status;
2570
Gilles Peskine8817f612018-12-18 00:18:46 +01002571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002572
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002573 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002574 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002575
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002576 /* Whether setup succeeded or failed, abort must succeed. */
2577 PSA_ASSERT( psa_hash_abort( &operation ) );
2578
2579 /* If setup failed, reproduce the failure, so as to
2580 * test the resulting state of the operation object. */
2581 if( status != PSA_SUCCESS )
2582 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2583
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002584 /* Now the operation object should be reusable. */
2585#if defined(KNOWN_SUPPORTED_HASH_ALG)
2586 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2587 PSA_ASSERT( psa_hash_abort( &operation ) );
2588#endif
2589
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002590exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002591 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002592}
2593/* END_CASE */
2594
2595/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002596void hash_compute_fail( int alg_arg, data_t *input,
2597 int output_size_arg, int expected_status_arg )
2598{
2599 psa_algorithm_t alg = alg_arg;
2600 uint8_t *output = NULL;
2601 size_t output_size = output_size_arg;
2602 size_t output_length = INVALID_EXPORT_LENGTH;
2603 psa_status_t expected_status = expected_status_arg;
2604 psa_status_t status;
2605
2606 ASSERT_ALLOC( output, output_size );
2607
2608 PSA_ASSERT( psa_crypto_init( ) );
2609
2610 status = psa_hash_compute( alg, input->x, input->len,
2611 output, output_size, &output_length );
2612 TEST_EQUAL( status, expected_status );
2613 TEST_ASSERT( output_length <= output_size );
2614
2615exit:
2616 mbedtls_free( output );
2617 PSA_DONE( );
2618}
2619/* END_CASE */
2620
2621/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002622void hash_compare_fail( int alg_arg, data_t *input,
2623 data_t *reference_hash,
2624 int expected_status_arg )
2625{
2626 psa_algorithm_t alg = alg_arg;
2627 psa_status_t expected_status = expected_status_arg;
2628 psa_status_t status;
2629
2630 PSA_ASSERT( psa_crypto_init( ) );
2631
2632 status = psa_hash_compare( alg, input->x, input->len,
2633 reference_hash->x, reference_hash->len );
2634 TEST_EQUAL( status, expected_status );
2635
2636exit:
2637 PSA_DONE( );
2638}
2639/* END_CASE */
2640
2641/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002642void hash_compute_compare( int alg_arg, data_t *input,
2643 data_t *expected_output )
2644{
2645 psa_algorithm_t alg = alg_arg;
2646 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2647 size_t output_length = INVALID_EXPORT_LENGTH;
2648 size_t i;
2649
2650 PSA_ASSERT( psa_crypto_init( ) );
2651
2652 /* Compute with tight buffer */
2653 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002654 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002655 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002656 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002657 ASSERT_COMPARE( output, output_length,
2658 expected_output->x, expected_output->len );
2659
2660 /* Compute with larger buffer */
2661 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2662 output, sizeof( output ),
2663 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002664 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002665 ASSERT_COMPARE( output, output_length,
2666 expected_output->x, expected_output->len );
2667
2668 /* Compare with correct hash */
2669 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2670 output, output_length ) );
2671
2672 /* Compare with trailing garbage */
2673 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2674 output, output_length + 1 ),
2675 PSA_ERROR_INVALID_SIGNATURE );
2676
2677 /* Compare with truncated hash */
2678 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2679 output, output_length - 1 ),
2680 PSA_ERROR_INVALID_SIGNATURE );
2681
2682 /* Compare with corrupted value */
2683 for( i = 0; i < output_length; i++ )
2684 {
Chris Jones9634bb12021-01-20 15:56:42 +00002685 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002686 output[i] ^= 1;
2687 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2688 output, output_length ),
2689 PSA_ERROR_INVALID_SIGNATURE );
2690 output[i] ^= 1;
2691 }
2692
2693exit:
2694 PSA_DONE( );
2695}
2696/* END_CASE */
2697
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002698/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002699void hash_bad_order( )
2700{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002701 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002702 unsigned char input[] = "";
2703 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002704 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002705 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2706 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2707 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002708 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002709 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002710 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002711
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002713
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002714 /* Call setup twice in a row. */
2715 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2716 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2717 PSA_ERROR_BAD_STATE );
2718 PSA_ASSERT( psa_hash_abort( &operation ) );
2719
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002720 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002721 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002722 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002723 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002724
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002725 /* Call update after finish. */
2726 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2727 PSA_ASSERT( psa_hash_finish( &operation,
2728 hash, sizeof( hash ), &hash_len ) );
2729 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002730 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002731 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002732
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002733 /* Call verify without calling setup beforehand. */
2734 TEST_EQUAL( psa_hash_verify( &operation,
2735 valid_hash, sizeof( valid_hash ) ),
2736 PSA_ERROR_BAD_STATE );
2737 PSA_ASSERT( psa_hash_abort( &operation ) );
2738
2739 /* Call verify after finish. */
2740 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2741 PSA_ASSERT( psa_hash_finish( &operation,
2742 hash, sizeof( hash ), &hash_len ) );
2743 TEST_EQUAL( psa_hash_verify( &operation,
2744 valid_hash, sizeof( valid_hash ) ),
2745 PSA_ERROR_BAD_STATE );
2746 PSA_ASSERT( psa_hash_abort( &operation ) );
2747
2748 /* Call verify twice in a row. */
2749 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2750 PSA_ASSERT( psa_hash_verify( &operation,
2751 valid_hash, sizeof( valid_hash ) ) );
2752 TEST_EQUAL( psa_hash_verify( &operation,
2753 valid_hash, sizeof( valid_hash ) ),
2754 PSA_ERROR_BAD_STATE );
2755 PSA_ASSERT( psa_hash_abort( &operation ) );
2756
2757 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002758 TEST_EQUAL( psa_hash_finish( &operation,
2759 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002760 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002761 PSA_ASSERT( psa_hash_abort( &operation ) );
2762
2763 /* Call finish twice in a row. */
2764 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2765 PSA_ASSERT( psa_hash_finish( &operation,
2766 hash, sizeof( hash ), &hash_len ) );
2767 TEST_EQUAL( psa_hash_finish( &operation,
2768 hash, sizeof( hash ), &hash_len ),
2769 PSA_ERROR_BAD_STATE );
2770 PSA_ASSERT( psa_hash_abort( &operation ) );
2771
2772 /* Call finish after calling verify. */
2773 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2774 PSA_ASSERT( psa_hash_verify( &operation,
2775 valid_hash, sizeof( valid_hash ) ) );
2776 TEST_EQUAL( psa_hash_finish( &operation,
2777 hash, sizeof( hash ), &hash_len ),
2778 PSA_ERROR_BAD_STATE );
2779 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002780
2781exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002782 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002783}
2784/* END_CASE */
2785
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002786/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002787void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002788{
2789 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002790 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2791 * appended to it */
2792 unsigned char hash[] = {
2793 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2794 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2795 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002796 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002797 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002798
Gilles Peskine8817f612018-12-18 00:18:46 +01002799 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002800
itayzafrir27e69452018-11-01 14:26:34 +02002801 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002803 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002804 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002805
itayzafrir27e69452018-11-01 14:26:34 +02002806 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002807 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002808 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002809 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002810
itayzafrir27e69452018-11-01 14:26:34 +02002811 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002812 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002813 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002814 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002815
itayzafrirec93d302018-10-18 18:01:10 +03002816exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002817 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002818}
2819/* END_CASE */
2820
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002821/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2822void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002823{
2824 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002825 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002826 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002827 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002828 size_t hash_len;
2829
Gilles Peskine8817f612018-12-18 00:18:46 +01002830 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002831
itayzafrir58028322018-10-25 10:22:01 +03002832 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002833 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002834 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002835 hash, expected_size - 1, &hash_len ),
2836 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002837
2838exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002839 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002840}
2841/* END_CASE */
2842
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002843/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2844void hash_clone_source_state( )
2845{
2846 psa_algorithm_t alg = PSA_ALG_SHA_256;
2847 unsigned char hash[PSA_HASH_MAX_SIZE];
2848 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2849 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2850 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2851 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2852 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2853 size_t hash_len;
2854
2855 PSA_ASSERT( psa_crypto_init( ) );
2856 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2857
2858 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2859 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2860 PSA_ASSERT( psa_hash_finish( &op_finished,
2861 hash, sizeof( hash ), &hash_len ) );
2862 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2863 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2864
2865 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2866 PSA_ERROR_BAD_STATE );
2867
2868 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2869 PSA_ASSERT( psa_hash_finish( &op_init,
2870 hash, sizeof( hash ), &hash_len ) );
2871 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2872 PSA_ASSERT( psa_hash_finish( &op_finished,
2873 hash, sizeof( hash ), &hash_len ) );
2874 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2875 PSA_ASSERT( psa_hash_finish( &op_aborted,
2876 hash, sizeof( hash ), &hash_len ) );
2877
2878exit:
2879 psa_hash_abort( &op_source );
2880 psa_hash_abort( &op_init );
2881 psa_hash_abort( &op_setup );
2882 psa_hash_abort( &op_finished );
2883 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002884 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002885}
2886/* END_CASE */
2887
2888/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2889void hash_clone_target_state( )
2890{
2891 psa_algorithm_t alg = PSA_ALG_SHA_256;
2892 unsigned char hash[PSA_HASH_MAX_SIZE];
2893 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2894 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2895 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2896 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2897 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2898 size_t hash_len;
2899
2900 PSA_ASSERT( psa_crypto_init( ) );
2901
2902 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2903 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2904 PSA_ASSERT( psa_hash_finish( &op_finished,
2905 hash, sizeof( hash ), &hash_len ) );
2906 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2907 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2908
2909 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2910 PSA_ASSERT( psa_hash_finish( &op_target,
2911 hash, sizeof( hash ), &hash_len ) );
2912
2913 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2914 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2915 PSA_ERROR_BAD_STATE );
2916 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2917 PSA_ERROR_BAD_STATE );
2918
2919exit:
2920 psa_hash_abort( &op_target );
2921 psa_hash_abort( &op_init );
2922 psa_hash_abort( &op_setup );
2923 psa_hash_abort( &op_finished );
2924 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002925 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002926}
2927/* END_CASE */
2928
itayzafrir58028322018-10-25 10:22:01 +03002929/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002930void mac_operation_init( )
2931{
Jaeden Amero252ef282019-02-15 14:05:35 +00002932 const uint8_t input[1] = { 0 };
2933
Jaeden Amero769ce272019-01-04 11:48:03 +00002934 /* Test each valid way of initializing the object, except for `= {0}`, as
2935 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2936 * though it's OK by the C standard. We could test for this, but we'd need
2937 * to supress the Clang warning for the test. */
2938 psa_mac_operation_t func = psa_mac_operation_init( );
2939 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2940 psa_mac_operation_t zero;
2941
2942 memset( &zero, 0, sizeof( zero ) );
2943
Jaeden Amero252ef282019-02-15 14:05:35 +00002944 /* A freshly-initialized MAC operation should not be usable. */
2945 TEST_EQUAL( psa_mac_update( &func,
2946 input, sizeof( input ) ),
2947 PSA_ERROR_BAD_STATE );
2948 TEST_EQUAL( psa_mac_update( &init,
2949 input, sizeof( input ) ),
2950 PSA_ERROR_BAD_STATE );
2951 TEST_EQUAL( psa_mac_update( &zero,
2952 input, sizeof( input ) ),
2953 PSA_ERROR_BAD_STATE );
2954
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002955 /* A default MAC operation should be abortable without error. */
2956 PSA_ASSERT( psa_mac_abort( &func ) );
2957 PSA_ASSERT( psa_mac_abort( &init ) );
2958 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002959}
2960/* END_CASE */
2961
2962/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002963void mac_setup( int key_type_arg,
2964 data_t *key,
2965 int alg_arg,
2966 int expected_status_arg )
2967{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002968 psa_key_type_t key_type = key_type_arg;
2969 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002970 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002971 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002972 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2973#if defined(KNOWN_SUPPORTED_MAC_ALG)
2974 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2975#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002976
Gilles Peskine8817f612018-12-18 00:18:46 +01002977 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002978
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002979 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2980 &operation, &status ) )
2981 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002982 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002983
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002984 /* The operation object should be reusable. */
2985#if defined(KNOWN_SUPPORTED_MAC_ALG)
2986 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2987 smoke_test_key_data,
2988 sizeof( smoke_test_key_data ),
2989 KNOWN_SUPPORTED_MAC_ALG,
2990 &operation, &status ) )
2991 goto exit;
2992 TEST_EQUAL( status, PSA_SUCCESS );
2993#endif
2994
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002995exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002996 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002997}
2998/* END_CASE */
2999
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003000/* 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 +00003001void mac_bad_order( )
3002{
Ronald Cron5425a212020-08-04 14:58:35 +02003003 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003004 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3005 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003006 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003007 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3008 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3009 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003011 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3012 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3013 size_t sign_mac_length = 0;
3014 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3015 const uint8_t verify_mac[] = {
3016 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3017 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3018 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3019
3020 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003021 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003022 psa_set_key_algorithm( &attributes, alg );
3023 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003024
Ronald Cron5425a212020-08-04 14:58:35 +02003025 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3026 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003027
Jaeden Amero252ef282019-02-15 14:05:35 +00003028 /* Call update without calling setup beforehand. */
3029 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3030 PSA_ERROR_BAD_STATE );
3031 PSA_ASSERT( psa_mac_abort( &operation ) );
3032
3033 /* Call sign finish without calling setup beforehand. */
3034 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3035 &sign_mac_length),
3036 PSA_ERROR_BAD_STATE );
3037 PSA_ASSERT( psa_mac_abort( &operation ) );
3038
3039 /* Call verify finish without calling setup beforehand. */
3040 TEST_EQUAL( psa_mac_verify_finish( &operation,
3041 verify_mac, sizeof( verify_mac ) ),
3042 PSA_ERROR_BAD_STATE );
3043 PSA_ASSERT( psa_mac_abort( &operation ) );
3044
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003045 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003046 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
3047 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003048 PSA_ERROR_BAD_STATE );
3049 PSA_ASSERT( psa_mac_abort( &operation ) );
3050
Jaeden Amero252ef282019-02-15 14:05:35 +00003051 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003052 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003053 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3054 PSA_ASSERT( psa_mac_sign_finish( &operation,
3055 sign_mac, sizeof( sign_mac ),
3056 &sign_mac_length ) );
3057 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3058 PSA_ERROR_BAD_STATE );
3059 PSA_ASSERT( psa_mac_abort( &operation ) );
3060
3061 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003062 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003063 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3064 PSA_ASSERT( psa_mac_verify_finish( &operation,
3065 verify_mac, sizeof( verify_mac ) ) );
3066 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3067 PSA_ERROR_BAD_STATE );
3068 PSA_ASSERT( psa_mac_abort( &operation ) );
3069
3070 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003071 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003072 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3073 PSA_ASSERT( psa_mac_sign_finish( &operation,
3074 sign_mac, sizeof( sign_mac ),
3075 &sign_mac_length ) );
3076 TEST_EQUAL( psa_mac_sign_finish( &operation,
3077 sign_mac, sizeof( sign_mac ),
3078 &sign_mac_length ),
3079 PSA_ERROR_BAD_STATE );
3080 PSA_ASSERT( psa_mac_abort( &operation ) );
3081
3082 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003083 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003084 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3085 PSA_ASSERT( psa_mac_verify_finish( &operation,
3086 verify_mac, sizeof( verify_mac ) ) );
3087 TEST_EQUAL( psa_mac_verify_finish( &operation,
3088 verify_mac, sizeof( verify_mac ) ),
3089 PSA_ERROR_BAD_STATE );
3090 PSA_ASSERT( psa_mac_abort( &operation ) );
3091
3092 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003093 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003094 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3095 TEST_EQUAL( psa_mac_verify_finish( &operation,
3096 verify_mac, sizeof( verify_mac ) ),
3097 PSA_ERROR_BAD_STATE );
3098 PSA_ASSERT( psa_mac_abort( &operation ) );
3099
3100 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003101 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003102 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3103 TEST_EQUAL( psa_mac_sign_finish( &operation,
3104 sign_mac, sizeof( sign_mac ),
3105 &sign_mac_length ),
3106 PSA_ERROR_BAD_STATE );
3107 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003108
Ronald Cron5425a212020-08-04 14:58:35 +02003109 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003110
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003111exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003112 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003113}
3114/* END_CASE */
3115
3116/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003117void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003118 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003119 int alg_arg,
3120 data_t *input,
3121 data_t *expected_mac )
3122{
Ronald Cron5425a212020-08-04 14:58:35 +02003123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003124 psa_key_type_t key_type = key_type_arg;
3125 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003126 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003127 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003128 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003129 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003130 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003131 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003132 const size_t output_sizes_to_test[] = {
3133 0,
3134 1,
3135 expected_mac->len - 1,
3136 expected_mac->len,
3137 expected_mac->len + 1,
3138 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003139
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003140 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003141 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003142 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003143
Gilles Peskine8817f612018-12-18 00:18:46 +01003144 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003147 psa_set_key_algorithm( &attributes, alg );
3148 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003149
Ronald Cron5425a212020-08-04 14:58:35 +02003150 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3151 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003152
Gilles Peskine8b356b52020-08-25 23:44:59 +02003153 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3154 {
3155 const size_t output_size = output_sizes_to_test[i];
3156 psa_status_t expected_status =
3157 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3158 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003159
Chris Jones9634bb12021-01-20 15:56:42 +00003160 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003161 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003162
Gilles Peskine8b356b52020-08-25 23:44:59 +02003163 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003164 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003165 PSA_ASSERT( psa_mac_update( &operation,
3166 input->x, input->len ) );
3167 TEST_EQUAL( psa_mac_sign_finish( &operation,
3168 actual_mac, output_size,
3169 &mac_length ),
3170 expected_status );
3171 PSA_ASSERT( psa_mac_abort( &operation ) );
3172
3173 if( expected_status == PSA_SUCCESS )
3174 {
3175 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3176 actual_mac, mac_length );
3177 }
3178 mbedtls_free( actual_mac );
3179 actual_mac = NULL;
3180 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003181
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003182exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003183 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003184 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003185 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003186 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003187}
3188/* END_CASE */
3189
3190/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003191void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003192 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003193 int alg_arg,
3194 data_t *input,
3195 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003196{
Ronald Cron5425a212020-08-04 14:58:35 +02003197 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003198 psa_key_type_t key_type = key_type_arg;
3199 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003200 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003202 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003203
Gilles Peskine69c12672018-06-28 00:07:19 +02003204 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3205
Gilles Peskine8817f612018-12-18 00:18:46 +01003206 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003207
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003208 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003209 psa_set_key_algorithm( &attributes, alg );
3210 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003211
Ronald Cron5425a212020-08-04 14:58:35 +02003212 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3213 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003214
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003215 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003216 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003217 PSA_ASSERT( psa_mac_update( &operation,
3218 input->x, input->len ) );
3219 PSA_ASSERT( psa_mac_verify_finish( &operation,
3220 expected_mac->x,
3221 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003222
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003223 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003224 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003225 PSA_ASSERT( psa_mac_update( &operation,
3226 input->x, input->len ) );
3227 TEST_EQUAL( psa_mac_verify_finish( &operation,
3228 expected_mac->x,
3229 expected_mac->len - 1 ),
3230 PSA_ERROR_INVALID_SIGNATURE );
3231
3232 /* Test a MAC that's too long. */
3233 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3234 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003235 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003236 PSA_ASSERT( psa_mac_update( &operation,
3237 input->x, input->len ) );
3238 TEST_EQUAL( psa_mac_verify_finish( &operation,
3239 perturbed_mac,
3240 expected_mac->len + 1 ),
3241 PSA_ERROR_INVALID_SIGNATURE );
3242
3243 /* Test changing one byte. */
3244 for( size_t i = 0; i < expected_mac->len; i++ )
3245 {
Chris Jones9634bb12021-01-20 15:56:42 +00003246 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003247 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003248 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003249 PSA_ASSERT( psa_mac_update( &operation,
3250 input->x, input->len ) );
3251 TEST_EQUAL( psa_mac_verify_finish( &operation,
3252 perturbed_mac,
3253 expected_mac->len ),
3254 PSA_ERROR_INVALID_SIGNATURE );
3255 perturbed_mac[i] ^= 1;
3256 }
3257
Gilles Peskine8c9def32018-02-08 10:02:12 +01003258exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003259 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003260 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003261 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003262 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003263}
3264/* END_CASE */
3265
3266/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003267void cipher_operation_init( )
3268{
Jaeden Ameroab439972019-02-15 14:12:05 +00003269 const uint8_t input[1] = { 0 };
3270 unsigned char output[1] = { 0 };
3271 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003272 /* Test each valid way of initializing the object, except for `= {0}`, as
3273 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3274 * though it's OK by the C standard. We could test for this, but we'd need
3275 * to supress the Clang warning for the test. */
3276 psa_cipher_operation_t func = psa_cipher_operation_init( );
3277 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3278 psa_cipher_operation_t zero;
3279
3280 memset( &zero, 0, sizeof( zero ) );
3281
Jaeden Ameroab439972019-02-15 14:12:05 +00003282 /* A freshly-initialized cipher operation should not be usable. */
3283 TEST_EQUAL( psa_cipher_update( &func,
3284 input, sizeof( input ),
3285 output, sizeof( output ),
3286 &output_length ),
3287 PSA_ERROR_BAD_STATE );
3288 TEST_EQUAL( psa_cipher_update( &init,
3289 input, sizeof( input ),
3290 output, sizeof( output ),
3291 &output_length ),
3292 PSA_ERROR_BAD_STATE );
3293 TEST_EQUAL( psa_cipher_update( &zero,
3294 input, sizeof( input ),
3295 output, sizeof( output ),
3296 &output_length ),
3297 PSA_ERROR_BAD_STATE );
3298
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003299 /* A default cipher operation should be abortable without error. */
3300 PSA_ASSERT( psa_cipher_abort( &func ) );
3301 PSA_ASSERT( psa_cipher_abort( &init ) );
3302 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003303}
3304/* END_CASE */
3305
3306/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003307void cipher_setup( int key_type_arg,
3308 data_t *key,
3309 int alg_arg,
3310 int expected_status_arg )
3311{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003312 psa_key_type_t key_type = key_type_arg;
3313 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003314 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003315 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003316 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003317#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003318 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3319#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003320
Gilles Peskine8817f612018-12-18 00:18:46 +01003321 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003322
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003323 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3324 &operation, &status ) )
3325 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003326 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003327
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003328 /* The operation object should be reusable. */
3329#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3330 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3331 smoke_test_key_data,
3332 sizeof( smoke_test_key_data ),
3333 KNOWN_SUPPORTED_CIPHER_ALG,
3334 &operation, &status ) )
3335 goto exit;
3336 TEST_EQUAL( status, PSA_SUCCESS );
3337#endif
3338
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003339exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003340 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003341 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003342}
3343/* END_CASE */
3344
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003345/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003346void cipher_bad_order( )
3347{
Ronald Cron5425a212020-08-04 14:58:35 +02003348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003349 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3350 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003352 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003353 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003354 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003355 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3356 0xaa, 0xaa, 0xaa, 0xaa };
3357 const uint8_t text[] = {
3358 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3359 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003360 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003361 size_t length = 0;
3362
3363 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003364 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3365 psa_set_key_algorithm( &attributes, alg );
3366 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003367 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3368 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003369
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003370 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003371 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3372 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003373 PSA_ERROR_BAD_STATE );
3374 PSA_ASSERT( psa_cipher_abort( &operation ) );
3375
3376 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003377 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3378 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003379 PSA_ERROR_BAD_STATE );
3380 PSA_ASSERT( psa_cipher_abort( &operation ) );
3381
Jaeden Ameroab439972019-02-15 14:12:05 +00003382 /* Generate an IV without calling setup beforehand. */
3383 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3384 buffer, sizeof( buffer ),
3385 &length ),
3386 PSA_ERROR_BAD_STATE );
3387 PSA_ASSERT( psa_cipher_abort( &operation ) );
3388
3389 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003390 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003391 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3392 buffer, sizeof( buffer ),
3393 &length ) );
3394 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3395 buffer, sizeof( buffer ),
3396 &length ),
3397 PSA_ERROR_BAD_STATE );
3398 PSA_ASSERT( psa_cipher_abort( &operation ) );
3399
3400 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003401 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003402 PSA_ASSERT( psa_cipher_set_iv( &operation,
3403 iv, sizeof( iv ) ) );
3404 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3405 buffer, sizeof( buffer ),
3406 &length ),
3407 PSA_ERROR_BAD_STATE );
3408 PSA_ASSERT( psa_cipher_abort( &operation ) );
3409
3410 /* Set an IV without calling setup beforehand. */
3411 TEST_EQUAL( psa_cipher_set_iv( &operation,
3412 iv, sizeof( iv ) ),
3413 PSA_ERROR_BAD_STATE );
3414 PSA_ASSERT( psa_cipher_abort( &operation ) );
3415
3416 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003417 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003418 PSA_ASSERT( psa_cipher_set_iv( &operation,
3419 iv, sizeof( iv ) ) );
3420 TEST_EQUAL( psa_cipher_set_iv( &operation,
3421 iv, sizeof( iv ) ),
3422 PSA_ERROR_BAD_STATE );
3423 PSA_ASSERT( psa_cipher_abort( &operation ) );
3424
3425 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003426 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003427 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3428 buffer, sizeof( buffer ),
3429 &length ) );
3430 TEST_EQUAL( psa_cipher_set_iv( &operation,
3431 iv, sizeof( iv ) ),
3432 PSA_ERROR_BAD_STATE );
3433 PSA_ASSERT( psa_cipher_abort( &operation ) );
3434
3435 /* Call update without calling setup beforehand. */
3436 TEST_EQUAL( psa_cipher_update( &operation,
3437 text, sizeof( text ),
3438 buffer, sizeof( buffer ),
3439 &length ),
3440 PSA_ERROR_BAD_STATE );
3441 PSA_ASSERT( psa_cipher_abort( &operation ) );
3442
3443 /* Call update without an IV where an IV is required. */
3444 TEST_EQUAL( psa_cipher_update( &operation,
3445 text, sizeof( text ),
3446 buffer, sizeof( buffer ),
3447 &length ),
3448 PSA_ERROR_BAD_STATE );
3449 PSA_ASSERT( psa_cipher_abort( &operation ) );
3450
3451 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003452 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003453 PSA_ASSERT( psa_cipher_set_iv( &operation,
3454 iv, sizeof( iv ) ) );
3455 PSA_ASSERT( psa_cipher_finish( &operation,
3456 buffer, sizeof( buffer ), &length ) );
3457 TEST_EQUAL( psa_cipher_update( &operation,
3458 text, sizeof( text ),
3459 buffer, sizeof( buffer ),
3460 &length ),
3461 PSA_ERROR_BAD_STATE );
3462 PSA_ASSERT( psa_cipher_abort( &operation ) );
3463
3464 /* Call finish without calling setup beforehand. */
3465 TEST_EQUAL( psa_cipher_finish( &operation,
3466 buffer, sizeof( buffer ), &length ),
3467 PSA_ERROR_BAD_STATE );
3468 PSA_ASSERT( psa_cipher_abort( &operation ) );
3469
3470 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003471 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003472 /* Not calling update means we are encrypting an empty buffer, which is OK
3473 * for cipher modes with padding. */
3474 TEST_EQUAL( psa_cipher_finish( &operation,
3475 buffer, sizeof( buffer ), &length ),
3476 PSA_ERROR_BAD_STATE );
3477 PSA_ASSERT( psa_cipher_abort( &operation ) );
3478
3479 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003480 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003481 PSA_ASSERT( psa_cipher_set_iv( &operation,
3482 iv, sizeof( iv ) ) );
3483 PSA_ASSERT( psa_cipher_finish( &operation,
3484 buffer, sizeof( buffer ), &length ) );
3485 TEST_EQUAL( psa_cipher_finish( &operation,
3486 buffer, sizeof( buffer ), &length ),
3487 PSA_ERROR_BAD_STATE );
3488 PSA_ASSERT( psa_cipher_abort( &operation ) );
3489
Ronald Cron5425a212020-08-04 14:58:35 +02003490 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003491
Jaeden Ameroab439972019-02-15 14:12:05 +00003492exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003493 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003494 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003495}
3496/* END_CASE */
3497
3498/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003499void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003500 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003501 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003502 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003503{
Ronald Cron5425a212020-08-04 14:58:35 +02003504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003505 psa_status_t status;
3506 psa_key_type_t key_type = key_type_arg;
3507 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003508 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003509 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003510 size_t output_buffer_size = 0;
3511 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003512 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003513 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003514 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003515
Gilles Peskine8817f612018-12-18 00:18:46 +01003516 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003517
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003518 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3519 psa_set_key_algorithm( &attributes, alg );
3520 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003521
Ronald Cron5425a212020-08-04 14:58:35 +02003522 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3523 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003524
Ronald Cron5425a212020-08-04 14:58:35 +02003525 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003526
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003527 if( iv->len > 0 )
3528 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003529 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003530 }
3531
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003532 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003533 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003534 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003535
Gilles Peskine8817f612018-12-18 00:18:46 +01003536 PSA_ASSERT( psa_cipher_update( &operation,
3537 input->x, input->len,
3538 output, output_buffer_size,
3539 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003540 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003541 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003542 output + total_output_length,
3543 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003544 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003545 total_output_length += function_output_length;
3546
Gilles Peskinefe11b722018-12-18 00:24:04 +01003547 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003548 if( expected_status == PSA_SUCCESS )
3549 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003550 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003551 ASSERT_COMPARE( expected_output->x, expected_output->len,
3552 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003553 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003554
Gilles Peskine50e586b2018-06-08 14:28:46 +02003555exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003556 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003557 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003558 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003559 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003560}
3561/* END_CASE */
3562
3563/* BEGIN_CASE */
3564void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003565 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003566 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003567 int first_part_size_arg,
3568 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003569 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003570{
Ronald Cron5425a212020-08-04 14:58:35 +02003571 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003572 psa_key_type_t key_type = key_type_arg;
3573 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003574 size_t first_part_size = first_part_size_arg;
3575 size_t output1_length = output1_length_arg;
3576 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003577 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003578 size_t output_buffer_size = 0;
3579 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003580 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003581 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003582 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003583
Gilles Peskine8817f612018-12-18 00:18:46 +01003584 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003585
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003586 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3587 psa_set_key_algorithm( &attributes, alg );
3588 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003589
Ronald Cron5425a212020-08-04 14:58:35 +02003590 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3591 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003592
Ronald Cron5425a212020-08-04 14:58:35 +02003593 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003594
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003595 if( iv->len > 0 )
3596 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003597 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003598 }
3599
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003600 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003601 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003602 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003603
Gilles Peskinee0866522019-02-19 19:44:00 +01003604 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3606 output, output_buffer_size,
3607 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003608 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003609 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003610 PSA_ASSERT( psa_cipher_update( &operation,
3611 input->x + first_part_size,
3612 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003613 output + total_output_length,
3614 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003615 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003616 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003617 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003618 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003619 output + total_output_length,
3620 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003621 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003622 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003623 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003624
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003625 ASSERT_COMPARE( expected_output->x, expected_output->len,
3626 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003627
3628exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003629 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003630 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003631 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003632 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003633}
3634/* END_CASE */
3635
3636/* BEGIN_CASE */
3637void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003638 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003639 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003640 int first_part_size_arg,
3641 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003642 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003643{
Ronald Cron5425a212020-08-04 14:58:35 +02003644 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003645 psa_key_type_t key_type = key_type_arg;
3646 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003647 size_t first_part_size = first_part_size_arg;
3648 size_t output1_length = output1_length_arg;
3649 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003650 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003651 size_t output_buffer_size = 0;
3652 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003653 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003654 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003655 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003656
Gilles Peskine8817f612018-12-18 00:18:46 +01003657 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003658
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003659 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3660 psa_set_key_algorithm( &attributes, alg );
3661 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003662
Ronald Cron5425a212020-08-04 14:58:35 +02003663 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3664 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003665
Ronald Cron5425a212020-08-04 14:58:35 +02003666 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003667
Steven Cooreman177deba2020-09-07 17:14:14 +02003668 if( iv->len > 0 )
3669 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003670 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003671 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003672
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003673 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003674 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003675 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003676
Gilles Peskinee0866522019-02-19 19:44:00 +01003677 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003678 PSA_ASSERT( psa_cipher_update( &operation,
3679 input->x, first_part_size,
3680 output, output_buffer_size,
3681 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003682 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003683 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003684 PSA_ASSERT( psa_cipher_update( &operation,
3685 input->x + first_part_size,
3686 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003687 output + total_output_length,
3688 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003689 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003690 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003691 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003692 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003693 output + total_output_length,
3694 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003695 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003696 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003697 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003698
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003699 ASSERT_COMPARE( expected_output->x, expected_output->len,
3700 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003701
3702exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003703 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003704 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003705 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003706 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003707}
3708/* END_CASE */
3709
Gilles Peskine50e586b2018-06-08 14:28:46 +02003710/* BEGIN_CASE */
3711void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003712 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003713 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003714 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003715{
Ronald Cron5425a212020-08-04 14:58:35 +02003716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003717 psa_status_t status;
3718 psa_key_type_t key_type = key_type_arg;
3719 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003720 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003721 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003722 size_t output_buffer_size = 0;
3723 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003724 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003725 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003727
Gilles Peskine8817f612018-12-18 00:18:46 +01003728 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003729
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003730 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3731 psa_set_key_algorithm( &attributes, alg );
3732 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003733
Ronald Cron5425a212020-08-04 14:58:35 +02003734 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3735 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003736
Ronald Cron5425a212020-08-04 14:58:35 +02003737 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003738
Steven Cooreman177deba2020-09-07 17:14:14 +02003739 if( iv->len > 0 )
3740 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003741 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003742 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003743
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003744 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003745 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003746 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003747
Gilles Peskine8817f612018-12-18 00:18:46 +01003748 PSA_ASSERT( psa_cipher_update( &operation,
3749 input->x, input->len,
3750 output, output_buffer_size,
3751 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003752 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003753 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003754 output + total_output_length,
3755 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003756 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003757 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003758 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003759
3760 if( expected_status == PSA_SUCCESS )
3761 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003762 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003763 ASSERT_COMPARE( expected_output->x, expected_output->len,
3764 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003765 }
3766
Gilles Peskine50e586b2018-06-08 14:28:46 +02003767exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003768 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003769 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003770 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003771 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003772}
3773/* END_CASE */
3774
Gilles Peskine50e586b2018-06-08 14:28:46 +02003775/* BEGIN_CASE */
3776void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003777 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003778 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003779{
Ronald Cron5425a212020-08-04 14:58:35 +02003780 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003781 psa_key_type_t key_type = key_type_arg;
3782 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003783 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003784 size_t iv_size = 16;
3785 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003786 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003787 size_t output1_size = 0;
3788 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003789 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003790 size_t output2_size = 0;
3791 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003792 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003793 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3794 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003795 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003796
Gilles Peskine8817f612018-12-18 00:18:46 +01003797 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003798
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003799 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3800 psa_set_key_algorithm( &attributes, alg );
3801 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003802
Ronald Cron5425a212020-08-04 14:58:35 +02003803 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3804 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003805
Ronald Cron5425a212020-08-04 14:58:35 +02003806 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3807 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003808
Steven Cooreman177deba2020-09-07 17:14:14 +02003809 if( alg != PSA_ALG_ECB_NO_PADDING )
3810 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003811 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3812 iv, iv_size,
3813 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003814 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003815 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003816 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003817 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003818
Gilles Peskine8817f612018-12-18 00:18:46 +01003819 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3820 output1, output1_size,
3821 &output1_length ) );
3822 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003823 output1 + output1_length,
3824 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003825 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003826
Gilles Peskine048b7f02018-06-08 14:20:49 +02003827 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003828
Gilles Peskine8817f612018-12-18 00:18:46 +01003829 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003830
3831 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003832 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003833
Steven Cooreman177deba2020-09-07 17:14:14 +02003834 if( iv_length > 0 )
3835 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003836 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3837 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003838 }
3839
Gilles Peskine8817f612018-12-18 00:18:46 +01003840 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3841 output2, output2_size,
3842 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003843 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003844 PSA_ASSERT( psa_cipher_finish( &operation2,
3845 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003846 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003847 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003848
Gilles Peskine048b7f02018-06-08 14:20:49 +02003849 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003850
Gilles Peskine8817f612018-12-18 00:18:46 +01003851 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003852
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003853 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003854
3855exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003856 psa_cipher_abort( &operation1 );
3857 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003858 mbedtls_free( output1 );
3859 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003860 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003861 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003862}
3863/* END_CASE */
3864
3865/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003866void cipher_verify_output_multipart( int alg_arg,
3867 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003868 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003869 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003870 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003871{
Ronald Cron5425a212020-08-04 14:58:35 +02003872 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003873 psa_key_type_t key_type = key_type_arg;
3874 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003875 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003876 unsigned char iv[16] = {0};
3877 size_t iv_size = 16;
3878 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003879 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003880 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003881 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003882 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003883 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003884 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003885 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003886 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3887 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003888 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003889
Gilles Peskine8817f612018-12-18 00:18:46 +01003890 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003891
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003892 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3893 psa_set_key_algorithm( &attributes, alg );
3894 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003895
Ronald Cron5425a212020-08-04 14:58:35 +02003896 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3897 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003898
Ronald Cron5425a212020-08-04 14:58:35 +02003899 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3900 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003901
Steven Cooreman177deba2020-09-07 17:14:14 +02003902 if( alg != PSA_ALG_ECB_NO_PADDING )
3903 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003904 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3905 iv, iv_size,
3906 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003907 }
3908
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003909 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003910 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003911 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003912
Gilles Peskinee0866522019-02-19 19:44:00 +01003913 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003914
Gilles Peskine8817f612018-12-18 00:18:46 +01003915 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3916 output1, output1_buffer_size,
3917 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003918 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003919
Gilles Peskine8817f612018-12-18 00:18:46 +01003920 PSA_ASSERT( psa_cipher_update( &operation1,
3921 input->x + first_part_size,
3922 input->len - first_part_size,
3923 output1, output1_buffer_size,
3924 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003925 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003926
Gilles Peskine8817f612018-12-18 00:18:46 +01003927 PSA_ASSERT( psa_cipher_finish( &operation1,
3928 output1 + output1_length,
3929 output1_buffer_size - output1_length,
3930 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003931 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003932
Gilles Peskine8817f612018-12-18 00:18:46 +01003933 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003934
Gilles Peskine048b7f02018-06-08 14:20:49 +02003935 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003936 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003937
Steven Cooreman177deba2020-09-07 17:14:14 +02003938 if( iv_length > 0 )
3939 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003940 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3941 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003942 }
Moran Pekerded84402018-06-06 16:36:50 +03003943
Gilles Peskine8817f612018-12-18 00:18:46 +01003944 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3945 output2, output2_buffer_size,
3946 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003947 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003948
Gilles Peskine8817f612018-12-18 00:18:46 +01003949 PSA_ASSERT( psa_cipher_update( &operation2,
3950 output1 + first_part_size,
3951 output1_length - first_part_size,
3952 output2, output2_buffer_size,
3953 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003954 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003955
Gilles Peskine8817f612018-12-18 00:18:46 +01003956 PSA_ASSERT( psa_cipher_finish( &operation2,
3957 output2 + output2_length,
3958 output2_buffer_size - output2_length,
3959 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003960 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003961
Gilles Peskine8817f612018-12-18 00:18:46 +01003962 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003963
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003964 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003965
3966exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003967 psa_cipher_abort( &operation1 );
3968 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003969 mbedtls_free( output1 );
3970 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003971 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003972 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003973}
3974/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003975
Gilles Peskine20035e32018-02-03 22:44:14 +01003976/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003977void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003978 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003979 data_t *nonce,
3980 data_t *additional_data,
3981 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003982 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003983{
Ronald Cron5425a212020-08-04 14:58:35 +02003984 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003985 psa_key_type_t key_type = key_type_arg;
3986 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003987 unsigned char *output_data = NULL;
3988 size_t output_size = 0;
3989 size_t output_length = 0;
3990 unsigned char *output_data2 = NULL;
3991 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003992 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003993 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003995
Gilles Peskine4abf7412018-06-18 16:35:34 +02003996 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003997 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3998 * should be exact. */
3999 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4000 TEST_EQUAL( output_size,
4001 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004002 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004003
Gilles Peskine8817f612018-12-18 00:18:46 +01004004 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004005
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004006 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4007 psa_set_key_algorithm( &attributes, alg );
4008 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004009
Gilles Peskine049c7532019-05-15 20:22:09 +02004010 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004011 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004012
Ronald Cron5425a212020-08-04 14:58:35 +02004013 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004014 nonce->x, nonce->len,
4015 additional_data->x,
4016 additional_data->len,
4017 input_data->x, input_data->len,
4018 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004019 &output_length ),
4020 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004021
4022 if( PSA_SUCCESS == expected_result )
4023 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004024 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004025
Gilles Peskine003a4a92019-05-14 16:09:40 +02004026 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4027 * should be exact. */
4028 TEST_EQUAL( input_data->len,
4029 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
4030
Ronald Cron5425a212020-08-04 14:58:35 +02004031 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004032 nonce->x, nonce->len,
4033 additional_data->x,
4034 additional_data->len,
4035 output_data, output_length,
4036 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004037 &output_length2 ),
4038 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004039
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004040 ASSERT_COMPARE( input_data->x, input_data->len,
4041 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004042 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004043
Gilles Peskinea1cac842018-06-11 19:33:02 +02004044exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004045 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004046 mbedtls_free( output_data );
4047 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004048 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004049}
4050/* END_CASE */
4051
4052/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004053void aead_encrypt( int key_type_arg, data_t *key_data,
4054 int alg_arg,
4055 data_t *nonce,
4056 data_t *additional_data,
4057 data_t *input_data,
4058 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004059{
Ronald Cron5425a212020-08-04 14:58:35 +02004060 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004061 psa_key_type_t key_type = key_type_arg;
4062 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004063 unsigned char *output_data = NULL;
4064 size_t output_size = 0;
4065 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004066 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004068 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004069
Gilles Peskine4abf7412018-06-18 16:35:34 +02004070 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004071 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4072 * should be exact. */
4073 TEST_EQUAL( output_size,
4074 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004075 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004076
Gilles Peskine8817f612018-12-18 00:18:46 +01004077 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004078
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4080 psa_set_key_algorithm( &attributes, alg );
4081 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004082
Gilles Peskine049c7532019-05-15 20:22:09 +02004083 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004084 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004085
Steven Cooremand588ea12021-01-11 19:36:04 +01004086 status = psa_aead_encrypt( key, alg,
4087 nonce->x, nonce->len,
4088 additional_data->x, additional_data->len,
4089 input_data->x, input_data->len,
4090 output_data, output_size,
4091 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004092
Steven Cooremand588ea12021-01-11 19:36:04 +01004093#if defined(MBEDTLS_AES_ALT) || \
4094 defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
4095 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
4096 if( status == PSA_ERROR_NOT_SUPPORTED &&
4097 key_type == PSA_KEY_TYPE_AES &&
4098 key_data->len == 24 )
4099 {
4100 test_skip( "AES-192 not supported", __LINE__, __FILE__ );
4101 goto exit;
4102 }
4103#endif /* AES could be alternatively implemented */
Steven Cooreman82645b12021-01-11 20:33:20 +01004104#if defined(MBEDTLS_GCM_ALT) || \
4105 defined(MBEDTLS_PSA_ACCEL_ALG_GCM)
4106 if( status == PSA_ERROR_NOT_SUPPORTED &&
4107 (alg & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) == PSA_ALG_GCM &&
4108 nonce->len != 12 )
4109 {
4110 test_skip( "AES-GCM with non-12-byte IV is not supported", __LINE__, __FILE__ );
4111 goto exit;
4112 }
4113#endif /* AES-GCM could be alternatively implemented */
Steven Cooremand588ea12021-01-11 19:36:04 +01004114
4115 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004116 ASSERT_COMPARE( expected_result->x, expected_result->len,
4117 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004118
Gilles Peskinea1cac842018-06-11 19:33:02 +02004119exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004120 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004121 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004122 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004123}
4124/* END_CASE */
4125
4126/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004127void aead_decrypt( int key_type_arg, data_t *key_data,
4128 int alg_arg,
4129 data_t *nonce,
4130 data_t *additional_data,
4131 data_t *input_data,
4132 data_t *expected_data,
4133 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004134{
Ronald Cron5425a212020-08-04 14:58:35 +02004135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004136 psa_key_type_t key_type = key_type_arg;
4137 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004138 unsigned char *output_data = NULL;
4139 size_t output_size = 0;
4140 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004141 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004143 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004144 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004145
Gilles Peskine003a4a92019-05-14 16:09:40 +02004146 output_size = input_data->len - tag_length;
4147 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4148 * should be exact. */
4149 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4150 TEST_EQUAL( output_size,
4151 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004152 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004153
Gilles Peskine8817f612018-12-18 00:18:46 +01004154 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004155
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004156 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4157 psa_set_key_algorithm( &attributes, alg );
4158 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004159
Gilles Peskine049c7532019-05-15 20:22:09 +02004160 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004161 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004162
Steven Cooremand588ea12021-01-11 19:36:04 +01004163 status = psa_aead_decrypt( key, alg,
4164 nonce->x, nonce->len,
4165 additional_data->x,
4166 additional_data->len,
4167 input_data->x, input_data->len,
4168 output_data, output_size,
4169 &output_length );
4170
4171#if defined(MBEDTLS_AES_ALT) || \
4172 defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
4173 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
4174 if( status == PSA_ERROR_NOT_SUPPORTED &&
4175 key_type == PSA_KEY_TYPE_AES &&
4176 key_data->len == 24 )
4177 {
4178 test_skip( "AES-192 not supported", __LINE__, __FILE__ );
4179 goto exit;
4180 }
4181#endif /* AES could be alternatively implemented */
Steven Cooreman82645b12021-01-11 20:33:20 +01004182#if defined(MBEDTLS_GCM_ALT) || \
4183 defined(MBEDTLS_PSA_ACCEL_ALG_GCM)
4184 if( status == PSA_ERROR_NOT_SUPPORTED &&
4185 (alg & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) == PSA_ALG_GCM &&
4186 nonce->len != 12 )
4187 {
4188 test_skip( "AES-GCM with non-12-byte IV is not supported", __LINE__, __FILE__ );
4189 goto exit;
4190 }
4191#endif /* AES-GCM could be alternatively implemented */
Steven Cooremand588ea12021-01-11 19:36:04 +01004192
4193 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004194
Gilles Peskine2d277862018-06-18 15:41:12 +02004195 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004196 ASSERT_COMPARE( expected_data->x, expected_data->len,
4197 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004198
Gilles Peskinea1cac842018-06-11 19:33:02 +02004199exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004200 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004201 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004202 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004203}
4204/* END_CASE */
4205
4206/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004207void signature_size( int type_arg,
4208 int bits,
4209 int alg_arg,
4210 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004211{
4212 psa_key_type_t type = type_arg;
4213 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004214 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004215
Gilles Peskinefe11b722018-12-18 00:24:04 +01004216 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004217#if defined(MBEDTLS_TEST_DEPRECATED)
4218 TEST_EQUAL( actual_size,
4219 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4220#endif /* MBEDTLS_TEST_DEPRECATED */
4221
Gilles Peskinee59236f2018-01-27 23:32:46 +01004222exit:
4223 ;
4224}
4225/* END_CASE */
4226
4227/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004228void sign_deterministic( int key_type_arg, data_t *key_data,
4229 int alg_arg, data_t *input_data,
4230 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004231{
Ronald Cron5425a212020-08-04 14:58:35 +02004232 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004233 psa_key_type_t key_type = key_type_arg;
4234 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004235 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004236 unsigned char *signature = NULL;
4237 size_t signature_size;
4238 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004240
Gilles Peskine8817f612018-12-18 00:18:46 +01004241 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004242
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004243 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004244 psa_set_key_algorithm( &attributes, alg );
4245 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004246
Gilles Peskine049c7532019-05-15 20:22:09 +02004247 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004248 &key ) );
4249 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004250 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004251
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004252 /* Allocate a buffer which has the size advertized by the
4253 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004254 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004255 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004256 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004257 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004258 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004259
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004260 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004261 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004262 input_data->x, input_data->len,
4263 signature, signature_size,
4264 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004265 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004266 ASSERT_COMPARE( output_data->x, output_data->len,
4267 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004268
Gilles Peskine0627f982019-11-26 19:12:16 +01004269#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004270 memset( signature, 0, signature_size );
4271 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004272 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004273 input_data->x, input_data->len,
4274 signature, signature_size,
4275 &signature_length ) );
4276 ASSERT_COMPARE( output_data->x, output_data->len,
4277 signature, signature_length );
4278#endif /* MBEDTLS_TEST_DEPRECATED */
4279
Gilles Peskine20035e32018-02-03 22:44:14 +01004280exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004281 /*
4282 * Key attributes may have been returned by psa_get_key_attributes()
4283 * thus reset them as required.
4284 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004285 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004286
Ronald Cron5425a212020-08-04 14:58:35 +02004287 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004288 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004289 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004290}
4291/* END_CASE */
4292
4293/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004294void sign_fail( int key_type_arg, data_t *key_data,
4295 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004296 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004297{
Ronald Cron5425a212020-08-04 14:58:35 +02004298 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004299 psa_key_type_t key_type = key_type_arg;
4300 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004301 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004302 psa_status_t actual_status;
4303 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004304 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004305 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004307
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004308 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004309
Gilles Peskine8817f612018-12-18 00:18:46 +01004310 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004311
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004312 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004313 psa_set_key_algorithm( &attributes, alg );
4314 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004315
Gilles Peskine049c7532019-05-15 20:22:09 +02004316 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004317 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004318
Ronald Cron5425a212020-08-04 14:58:35 +02004319 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004320 input_data->x, input_data->len,
4321 signature, signature_size,
4322 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004323 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004324 /* The value of *signature_length is unspecified on error, but
4325 * whatever it is, it should be less than signature_size, so that
4326 * if the caller tries to read *signature_length bytes without
4327 * checking the error code then they don't overflow a buffer. */
4328 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004329
Gilles Peskine895242b2019-11-29 12:15:40 +01004330#if defined(MBEDTLS_TEST_DEPRECATED)
4331 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004332 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004333 input_data->x, input_data->len,
4334 signature, signature_size,
4335 &signature_length ),
4336 expected_status );
4337 TEST_ASSERT( signature_length <= signature_size );
4338#endif /* MBEDTLS_TEST_DEPRECATED */
4339
Gilles Peskine20035e32018-02-03 22:44:14 +01004340exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004341 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004342 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004343 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004344 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004345}
4346/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004347
4348/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004349void sign_verify( int key_type_arg, data_t *key_data,
4350 int alg_arg, data_t *input_data )
4351{
Ronald Cron5425a212020-08-04 14:58:35 +02004352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004353 psa_key_type_t key_type = key_type_arg;
4354 psa_algorithm_t alg = alg_arg;
4355 size_t key_bits;
4356 unsigned char *signature = NULL;
4357 size_t signature_size;
4358 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004360
Gilles Peskine8817f612018-12-18 00:18:46 +01004361 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004362
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004364 psa_set_key_algorithm( &attributes, alg );
4365 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004366
Gilles Peskine049c7532019-05-15 20:22:09 +02004367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004368 &key ) );
4369 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004370 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004371
4372 /* Allocate a buffer which has the size advertized by the
4373 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004374 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004375 key_bits, alg );
4376 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004377 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004378 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004379
4380 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004381 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004382 input_data->x, input_data->len,
4383 signature, signature_size,
4384 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004385 /* Check that the signature length looks sensible. */
4386 TEST_ASSERT( signature_length <= signature_size );
4387 TEST_ASSERT( signature_length > 0 );
4388
4389 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004390 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004391 input_data->x, input_data->len,
4392 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004393
4394 if( input_data->len != 0 )
4395 {
4396 /* Flip a bit in the input and verify that the signature is now
4397 * detected as invalid. Flip a bit at the beginning, not at the end,
4398 * because ECDSA may ignore the last few bits of the input. */
4399 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004400 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004401 input_data->x, input_data->len,
4402 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004403 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004404 }
4405
4406exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004407 /*
4408 * Key attributes may have been returned by psa_get_key_attributes()
4409 * thus reset them as required.
4410 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004411 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004412
Ronald Cron5425a212020-08-04 14:58:35 +02004413 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004414 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004415 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004416}
4417/* END_CASE */
4418
4419/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004420void asymmetric_verify( int key_type_arg, data_t *key_data,
4421 int alg_arg, data_t *hash_data,
4422 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004423{
Ronald Cron5425a212020-08-04 14:58:35 +02004424 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004425 psa_key_type_t key_type = key_type_arg;
4426 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004428
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004429 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004430
Gilles Peskine8817f612018-12-18 00:18:46 +01004431 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004432
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004433 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004434 psa_set_key_algorithm( &attributes, alg );
4435 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004436
Gilles Peskine049c7532019-05-15 20:22:09 +02004437 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004438 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004439
Ronald Cron5425a212020-08-04 14:58:35 +02004440 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004441 hash_data->x, hash_data->len,
4442 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004443
4444#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004445 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004446 hash_data->x, hash_data->len,
4447 signature_data->x,
4448 signature_data->len ) );
4449
4450#endif /* MBEDTLS_TEST_DEPRECATED */
4451
itayzafrir5c753392018-05-08 11:18:38 +03004452exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004453 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004454 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004455 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004456}
4457/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004458
4459/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004460void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4461 int alg_arg, data_t *hash_data,
4462 data_t *signature_data,
4463 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004464{
Ronald Cron5425a212020-08-04 14:58:35 +02004465 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004466 psa_key_type_t key_type = key_type_arg;
4467 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004468 psa_status_t actual_status;
4469 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004470 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004471
Gilles Peskine8817f612018-12-18 00:18:46 +01004472 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004473
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004474 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004475 psa_set_key_algorithm( &attributes, alg );
4476 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004477
Gilles Peskine049c7532019-05-15 20:22:09 +02004478 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004479 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004480
Ronald Cron5425a212020-08-04 14:58:35 +02004481 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004482 hash_data->x, hash_data->len,
4483 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004484 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004485
Gilles Peskine895242b2019-11-29 12:15:40 +01004486#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004487 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004488 hash_data->x, hash_data->len,
4489 signature_data->x, signature_data->len ),
4490 expected_status );
4491#endif /* MBEDTLS_TEST_DEPRECATED */
4492
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004493exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004494 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004495 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004496 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004497}
4498/* END_CASE */
4499
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004500/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004501void asymmetric_encrypt( int key_type_arg,
4502 data_t *key_data,
4503 int alg_arg,
4504 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004505 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004506 int expected_output_length_arg,
4507 int expected_status_arg )
4508{
Ronald Cron5425a212020-08-04 14:58:35 +02004509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004510 psa_key_type_t key_type = key_type_arg;
4511 psa_algorithm_t alg = alg_arg;
4512 size_t expected_output_length = expected_output_length_arg;
4513 size_t key_bits;
4514 unsigned char *output = NULL;
4515 size_t output_size;
4516 size_t output_length = ~0;
4517 psa_status_t actual_status;
4518 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004520
Gilles Peskine8817f612018-12-18 00:18:46 +01004521 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004522
Gilles Peskine656896e2018-06-29 19:12:28 +02004523 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004524 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4525 psa_set_key_algorithm( &attributes, alg );
4526 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004527 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004528 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004529
4530 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004531 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004532 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004533 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004534 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004535
4536 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004537 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004538 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004539 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004540 output, output_size,
4541 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004542 TEST_EQUAL( actual_status, expected_status );
4543 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004544
Gilles Peskine68428122018-06-30 18:42:41 +02004545 /* If the label is empty, the test framework puts a non-null pointer
4546 * in label->x. Test that a null pointer works as well. */
4547 if( label->len == 0 )
4548 {
4549 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004550 if( output_size != 0 )
4551 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004552 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004553 input_data->x, input_data->len,
4554 NULL, label->len,
4555 output, output_size,
4556 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004557 TEST_EQUAL( actual_status, expected_status );
4558 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004559 }
4560
Gilles Peskine656896e2018-06-29 19:12:28 +02004561exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004562 /*
4563 * Key attributes may have been returned by psa_get_key_attributes()
4564 * thus reset them as required.
4565 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004566 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004567
Ronald Cron5425a212020-08-04 14:58:35 +02004568 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004569 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004570 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004571}
4572/* END_CASE */
4573
4574/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004575void asymmetric_encrypt_decrypt( int key_type_arg,
4576 data_t *key_data,
4577 int alg_arg,
4578 data_t *input_data,
4579 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004580{
Ronald Cron5425a212020-08-04 14:58:35 +02004581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004582 psa_key_type_t key_type = key_type_arg;
4583 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004584 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004585 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004586 size_t output_size;
4587 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004588 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004589 size_t output2_size;
4590 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004592
Gilles Peskine8817f612018-12-18 00:18:46 +01004593 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004594
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004595 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4596 psa_set_key_algorithm( &attributes, alg );
4597 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004598
Gilles Peskine049c7532019-05-15 20:22:09 +02004599 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004600 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004601
4602 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004603 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004604 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004605 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004606 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004607 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004608 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004609
Gilles Peskineeebd7382018-06-08 18:11:54 +02004610 /* We test encryption by checking that encrypt-then-decrypt gives back
4611 * the original plaintext because of the non-optional random
4612 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004613 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004614 input_data->x, input_data->len,
4615 label->x, label->len,
4616 output, output_size,
4617 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004618 /* We don't know what ciphertext length to expect, but check that
4619 * it looks sensible. */
4620 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004621
Ronald Cron5425a212020-08-04 14:58:35 +02004622 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004623 output, output_length,
4624 label->x, label->len,
4625 output2, output2_size,
4626 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004627 ASSERT_COMPARE( input_data->x, input_data->len,
4628 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004629
4630exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004631 /*
4632 * Key attributes may have been returned by psa_get_key_attributes()
4633 * thus reset them as required.
4634 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004635 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004636
Ronald Cron5425a212020-08-04 14:58:35 +02004637 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004638 mbedtls_free( output );
4639 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004640 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004641}
4642/* END_CASE */
4643
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004644/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004645void asymmetric_decrypt( int key_type_arg,
4646 data_t *key_data,
4647 int alg_arg,
4648 data_t *input_data,
4649 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004650 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004651{
Ronald Cron5425a212020-08-04 14:58:35 +02004652 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004653 psa_key_type_t key_type = key_type_arg;
4654 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004655 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004656 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004657 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004659
Jaeden Amero412654a2019-02-06 12:57:46 +00004660 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004661 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004662
Gilles Peskine8817f612018-12-18 00:18:46 +01004663 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004664
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004665 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4666 psa_set_key_algorithm( &attributes, alg );
4667 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004668
Gilles Peskine049c7532019-05-15 20:22:09 +02004669 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004670 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004671
Ronald Cron5425a212020-08-04 14:58:35 +02004672 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004673 input_data->x, input_data->len,
4674 label->x, label->len,
4675 output,
4676 output_size,
4677 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004678 ASSERT_COMPARE( expected_data->x, expected_data->len,
4679 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004680
Gilles Peskine68428122018-06-30 18:42:41 +02004681 /* If the label is empty, the test framework puts a non-null pointer
4682 * in label->x. Test that a null pointer works as well. */
4683 if( label->len == 0 )
4684 {
4685 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004686 if( output_size != 0 )
4687 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004688 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004689 input_data->x, input_data->len,
4690 NULL, label->len,
4691 output,
4692 output_size,
4693 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004694 ASSERT_COMPARE( expected_data->x, expected_data->len,
4695 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004696 }
4697
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004698exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004699 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004700 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004701 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004702 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004703}
4704/* END_CASE */
4705
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004706/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004707void asymmetric_decrypt_fail( int key_type_arg,
4708 data_t *key_data,
4709 int alg_arg,
4710 data_t *input_data,
4711 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004712 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004713 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004714{
Ronald Cron5425a212020-08-04 14:58:35 +02004715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004716 psa_key_type_t key_type = key_type_arg;
4717 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004718 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004719 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004720 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004721 psa_status_t actual_status;
4722 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004723 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004724
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004725 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004726
Gilles Peskine8817f612018-12-18 00:18:46 +01004727 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004728
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004729 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4730 psa_set_key_algorithm( &attributes, alg );
4731 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004732
Gilles Peskine049c7532019-05-15 20:22:09 +02004733 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004734 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004735
Ronald Cron5425a212020-08-04 14:58:35 +02004736 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004737 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004738 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004739 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004740 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004741 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004742 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004743
Gilles Peskine68428122018-06-30 18:42:41 +02004744 /* If the label is empty, the test framework puts a non-null pointer
4745 * in label->x. Test that a null pointer works as well. */
4746 if( label->len == 0 )
4747 {
4748 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004749 if( output_size != 0 )
4750 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004751 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004752 input_data->x, input_data->len,
4753 NULL, label->len,
4754 output, output_size,
4755 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004756 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004757 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004758 }
4759
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004760exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004761 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004762 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004763 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004764 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004765}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004766/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004767
4768/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004769void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004770{
4771 /* Test each valid way of initializing the object, except for `= {0}`, as
4772 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4773 * though it's OK by the C standard. We could test for this, but we'd need
4774 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004775 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004776 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4777 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4778 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004779
4780 memset( &zero, 0, sizeof( zero ) );
4781
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004783 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004784 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004785 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004786 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004787 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004788 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004789
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004790 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004791 PSA_ASSERT( psa_key_derivation_abort(&func) );
4792 PSA_ASSERT( psa_key_derivation_abort(&init) );
4793 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004794}
4795/* END_CASE */
4796
Janos Follath16de4a42019-06-13 16:32:24 +01004797/* BEGIN_CASE */
4798void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004799{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004800 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004801 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004803
Gilles Peskine8817f612018-12-18 00:18:46 +01004804 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004805
Janos Follath16de4a42019-06-13 16:32:24 +01004806 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004807 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004808
4809exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004811 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004812}
4813/* END_CASE */
4814
Janos Follathaf3c2a02019-06-12 12:34:34 +01004815/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004816void derive_set_capacity( int alg_arg, int capacity_arg,
4817 int expected_status_arg )
4818{
4819 psa_algorithm_t alg = alg_arg;
4820 size_t capacity = capacity_arg;
4821 psa_status_t expected_status = expected_status_arg;
4822 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4823
4824 PSA_ASSERT( psa_crypto_init( ) );
4825
4826 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4827
4828 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4829 expected_status );
4830
4831exit:
4832 psa_key_derivation_abort( &operation );
4833 PSA_DONE( );
4834}
4835/* END_CASE */
4836
4837/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004838void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004839 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004840 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004841 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004842 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004843 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004844 int expected_status_arg3,
4845 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004846{
4847 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004848 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4849 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004850 psa_status_t expected_statuses[] = {expected_status_arg1,
4851 expected_status_arg2,
4852 expected_status_arg3};
4853 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004854 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4855 MBEDTLS_SVC_KEY_ID_INIT,
4856 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004857 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4859 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004860 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004861 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004862 psa_status_t expected_output_status = expected_output_status_arg;
4863 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004864
4865 PSA_ASSERT( psa_crypto_init( ) );
4866
4867 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4868 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004869
4870 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4871
4872 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4873 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004874 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004875 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004876 psa_set_key_type( &attributes, key_types[i] );
4877 PSA_ASSERT( psa_import_key( &attributes,
4878 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004879 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004880 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4881 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4882 {
4883 // When taking a private key as secret input, use key agreement
4884 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004885 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004886 expected_statuses[i] );
4887 }
4888 else
4889 {
4890 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004891 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004892 expected_statuses[i] );
4893 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004894 }
4895 else
4896 {
4897 TEST_EQUAL( psa_key_derivation_input_bytes(
4898 &operation, steps[i],
4899 inputs[i]->x, inputs[i]->len ),
4900 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004901 }
4902 }
4903
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004904 if( output_key_type != PSA_KEY_TYPE_NONE )
4905 {
4906 psa_reset_key_attributes( &attributes );
4907 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4908 psa_set_key_bits( &attributes, 8 );
4909 actual_output_status =
4910 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004911 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004912 }
4913 else
4914 {
4915 uint8_t buffer[1];
4916 actual_output_status =
4917 psa_key_derivation_output_bytes( &operation,
4918 buffer, sizeof( buffer ) );
4919 }
4920 TEST_EQUAL( actual_output_status, expected_output_status );
4921
Janos Follathaf3c2a02019-06-12 12:34:34 +01004922exit:
4923 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004924 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4925 psa_destroy_key( keys[i] );
4926 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004927 PSA_DONE( );
4928}
4929/* END_CASE */
4930
Janos Follathd958bb72019-07-03 15:02:16 +01004931/* BEGIN_CASE */
4932void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004933{
Janos Follathd958bb72019-07-03 15:02:16 +01004934 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004935 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004936 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004937 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004938 unsigned char input1[] = "Input 1";
4939 size_t input1_length = sizeof( input1 );
4940 unsigned char input2[] = "Input 2";
4941 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004942 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004943 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004944 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4945 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4946 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004947 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004948
Gilles Peskine8817f612018-12-18 00:18:46 +01004949 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004950
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004951 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4952 psa_set_key_algorithm( &attributes, alg );
4953 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004954
Gilles Peskine73676cb2019-05-15 20:15:10 +02004955 PSA_ASSERT( psa_import_key( &attributes,
4956 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004957 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004958
4959 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004960 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004961 input1, input1_length,
4962 input2, input2_length,
4963 capacity ) )
4964 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004965
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004967 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004968 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004969
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004971
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004973 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004974
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004975exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004977 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004978 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004979}
4980/* END_CASE */
4981
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004982/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004983void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004984{
4985 uint8_t output_buffer[16];
4986 size_t buffer_size = 16;
4987 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004988 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004989
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004990 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4991 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004992 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004993
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004994 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004995 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004996
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004997 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004998
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004999 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5000 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005001 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005002
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005003 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005004 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005005
5006exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005008}
5009/* END_CASE */
5010
5011/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005012void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005013 int step1_arg, data_t *input1,
5014 int step2_arg, data_t *input2,
5015 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005016 int requested_capacity_arg,
5017 data_t *expected_output1,
5018 data_t *expected_output2 )
5019{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005020 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005021 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5022 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005023 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5024 MBEDTLS_SVC_KEY_ID_INIT,
5025 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005026 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005027 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005028 uint8_t *expected_outputs[2] =
5029 {expected_output1->x, expected_output2->x};
5030 size_t output_sizes[2] =
5031 {expected_output1->len, expected_output2->len};
5032 size_t output_buffer_size = 0;
5033 uint8_t *output_buffer = NULL;
5034 size_t expected_capacity;
5035 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005036 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005037 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005038 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005039
5040 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5041 {
5042 if( output_sizes[i] > output_buffer_size )
5043 output_buffer_size = output_sizes[i];
5044 if( output_sizes[i] == 0 )
5045 expected_outputs[i] = NULL;
5046 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005047 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005048 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005049
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005050 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5051 psa_set_key_algorithm( &attributes, alg );
5052 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005053
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005054 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005055 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5056 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5057 requested_capacity ) );
5058 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005059 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005060 switch( steps[i] )
5061 {
5062 case 0:
5063 break;
5064 case PSA_KEY_DERIVATION_INPUT_SECRET:
5065 PSA_ASSERT( psa_import_key( &attributes,
5066 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005067 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005068 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005069 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005070 break;
5071 default:
5072 PSA_ASSERT( psa_key_derivation_input_bytes(
5073 &operation, steps[i],
5074 inputs[i]->x, inputs[i]->len ) );
5075 break;
5076 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005077 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005078
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005079 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005080 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005081 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005082 expected_capacity = requested_capacity;
5083
5084 /* Expansion phase. */
5085 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5086 {
5087 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005088 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005089 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005090 if( expected_capacity == 0 && output_sizes[i] == 0 )
5091 {
5092 /* Reading 0 bytes when 0 bytes are available can go either way. */
5093 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005094 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005095 continue;
5096 }
5097 else if( expected_capacity == 0 ||
5098 output_sizes[i] > expected_capacity )
5099 {
5100 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005101 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005102 expected_capacity = 0;
5103 continue;
5104 }
5105 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005106 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005107 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005108 ASSERT_COMPARE( output_buffer, output_sizes[i],
5109 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005111 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005113 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005114 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005115 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005117
5118exit:
5119 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005120 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005121 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5122 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005123 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005124}
5125/* END_CASE */
5126
5127/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005128void derive_full( int alg_arg,
5129 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005130 data_t *input1,
5131 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005132 int requested_capacity_arg )
5133{
Ronald Cron5425a212020-08-04 14:58:35 +02005134 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005135 psa_algorithm_t alg = alg_arg;
5136 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005137 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005138 unsigned char output_buffer[16];
5139 size_t expected_capacity = requested_capacity;
5140 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005141 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005142
Gilles Peskine8817f612018-12-18 00:18:46 +01005143 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005144
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005145 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5146 psa_set_key_algorithm( &attributes, alg );
5147 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005148
Gilles Peskine049c7532019-05-15 20:22:09 +02005149 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005150 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005151
Ronald Cron5425a212020-08-04 14:58:35 +02005152 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005153 input1->x, input1->len,
5154 input2->x, input2->len,
5155 requested_capacity ) )
5156 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005157
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005158 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005159 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005160 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005161
5162 /* Expansion phase. */
5163 while( current_capacity > 0 )
5164 {
5165 size_t read_size = sizeof( output_buffer );
5166 if( read_size > current_capacity )
5167 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005169 output_buffer,
5170 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005171 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005172 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005173 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005174 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005175 }
5176
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005177 /* Check that the operation refuses to go over capacity. */
5178 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005179 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005180
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005181 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005182
5183exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005184 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005186 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005187}
5188/* END_CASE */
5189
Janos Follathe60c9052019-07-03 13:51:30 +01005190/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005191void derive_key_exercise( int alg_arg,
5192 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005193 data_t *input1,
5194 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005195 int derived_type_arg,
5196 int derived_bits_arg,
5197 int derived_usage_arg,
5198 int derived_alg_arg )
5199{
Ronald Cron5425a212020-08-04 14:58:35 +02005200 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5201 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005202 psa_algorithm_t alg = alg_arg;
5203 psa_key_type_t derived_type = derived_type_arg;
5204 size_t derived_bits = derived_bits_arg;
5205 psa_key_usage_t derived_usage = derived_usage_arg;
5206 psa_algorithm_t derived_alg = derived_alg_arg;
5207 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005210 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005211
Gilles Peskine8817f612018-12-18 00:18:46 +01005212 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005213
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005214 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5215 psa_set_key_algorithm( &attributes, alg );
5216 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005217 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005218 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005219
5220 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005221 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005222 input1->x, input1->len,
5223 input2->x, input2->len, capacity ) )
5224 goto exit;
5225
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005226 psa_set_key_usage_flags( &attributes, derived_usage );
5227 psa_set_key_algorithm( &attributes, derived_alg );
5228 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005229 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005231 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005232
5233 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005234 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005235 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5236 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005237
5238 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005239 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005240 goto exit;
5241
5242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005243 /*
5244 * Key attributes may have been returned by psa_get_key_attributes()
5245 * thus reset them as required.
5246 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005247 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005248
5249 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005250 psa_destroy_key( base_key );
5251 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005252 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005253}
5254/* END_CASE */
5255
Janos Follath42fd8882019-07-03 14:17:09 +01005256/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005257void derive_key_export( int alg_arg,
5258 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005259 data_t *input1,
5260 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005261 int bytes1_arg,
5262 int bytes2_arg )
5263{
Ronald Cron5425a212020-08-04 14:58:35 +02005264 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5265 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005266 psa_algorithm_t alg = alg_arg;
5267 size_t bytes1 = bytes1_arg;
5268 size_t bytes2 = bytes2_arg;
5269 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005270 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005271 uint8_t *output_buffer = NULL;
5272 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005273 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5274 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005275 size_t length;
5276
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005277 ASSERT_ALLOC( output_buffer, capacity );
5278 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005279 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005280
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005281 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5282 psa_set_key_algorithm( &base_attributes, alg );
5283 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005284 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005285 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005286
5287 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005288 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005289 input1->x, input1->len,
5290 input2->x, input2->len, capacity ) )
5291 goto exit;
5292
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005294 output_buffer,
5295 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005296 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005297
5298 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005299 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005300 input1->x, input1->len,
5301 input2->x, input2->len, capacity ) )
5302 goto exit;
5303
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005304 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5305 psa_set_key_algorithm( &derived_attributes, 0 );
5306 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005307 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005308 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005309 &derived_key ) );
5310 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005311 export_buffer, bytes1,
5312 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005313 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005314 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005315 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005316 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005317 &derived_key ) );
5318 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005319 export_buffer + bytes1, bytes2,
5320 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005321 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005322
5323 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005324 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5325 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005326
5327exit:
5328 mbedtls_free( output_buffer );
5329 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005330 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005331 psa_destroy_key( base_key );
5332 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005333 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005334}
5335/* END_CASE */
5336
5337/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005338void derive_key( int alg_arg,
5339 data_t *key_data, data_t *input1, data_t *input2,
5340 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005341 int expected_status_arg,
5342 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005343{
Ronald Cron5425a212020-08-04 14:58:35 +02005344 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5345 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005346 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005347 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005348 size_t bits = bits_arg;
5349 psa_status_t expected_status = expected_status_arg;
5350 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5351 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5352 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5353
5354 PSA_ASSERT( psa_crypto_init( ) );
5355
5356 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5357 psa_set_key_algorithm( &base_attributes, alg );
5358 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5359 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005360 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005361
Ronald Cron5425a212020-08-04 14:58:35 +02005362 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005363 input1->x, input1->len,
5364 input2->x, input2->len, SIZE_MAX ) )
5365 goto exit;
5366
5367 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5368 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005369 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005370 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005371
5372 psa_status_t status =
5373 psa_key_derivation_output_key( &derived_attributes,
5374 &operation,
5375 &derived_key );
5376 if( is_large_output > 0 )
5377 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5378 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005379
5380exit:
5381 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005382 psa_destroy_key( base_key );
5383 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005384 PSA_DONE( );
5385}
5386/* END_CASE */
5387
5388/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005389void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005390 int our_key_type_arg, int our_key_alg_arg,
5391 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005392 int expected_status_arg )
5393{
Ronald Cron5425a212020-08-04 14:58:35 +02005394 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005395 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005396 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005397 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005398 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005399 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005400 psa_status_t expected_status = expected_status_arg;
5401 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005402
Gilles Peskine8817f612018-12-18 00:18:46 +01005403 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005404
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005405 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005406 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005407 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005408 PSA_ASSERT( psa_import_key( &attributes,
5409 our_key_data->x, our_key_data->len,
5410 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005411
Gilles Peskine77f40d82019-04-11 21:27:06 +02005412 /* The tests currently include inputs that should fail at either step.
5413 * Test cases that fail at the setup step should be changed to call
5414 * key_derivation_setup instead, and this function should be renamed
5415 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005416 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005417 if( status == PSA_SUCCESS )
5418 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005419 TEST_EQUAL( psa_key_derivation_key_agreement(
5420 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5421 our_key,
5422 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005423 expected_status );
5424 }
5425 else
5426 {
5427 TEST_ASSERT( status == expected_status );
5428 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005429
5430exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005431 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005432 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005433 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005434}
5435/* END_CASE */
5436
5437/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005438void raw_key_agreement( int alg_arg,
5439 int our_key_type_arg, data_t *our_key_data,
5440 data_t *peer_key_data,
5441 data_t *expected_output )
5442{
Ronald Cron5425a212020-08-04 14:58:35 +02005443 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005444 psa_algorithm_t alg = alg_arg;
5445 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005447 unsigned char *output = NULL;
5448 size_t output_length = ~0;
5449
5450 ASSERT_ALLOC( output, expected_output->len );
5451 PSA_ASSERT( psa_crypto_init( ) );
5452
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005453 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5454 psa_set_key_algorithm( &attributes, alg );
5455 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005456 PSA_ASSERT( psa_import_key( &attributes,
5457 our_key_data->x, our_key_data->len,
5458 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005459
Gilles Peskinebe697d82019-05-16 18:00:41 +02005460 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5461 peer_key_data->x, peer_key_data->len,
5462 output, expected_output->len,
5463 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005464 ASSERT_COMPARE( output, output_length,
5465 expected_output->x, expected_output->len );
5466
5467exit:
5468 mbedtls_free( output );
5469 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005470 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005471}
5472/* END_CASE */
5473
5474/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005475void key_agreement_capacity( int alg_arg,
5476 int our_key_type_arg, data_t *our_key_data,
5477 data_t *peer_key_data,
5478 int expected_capacity_arg )
5479{
Ronald Cron5425a212020-08-04 14:58:35 +02005480 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005481 psa_algorithm_t alg = alg_arg;
5482 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005483 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005484 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005485 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005486 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005487
Gilles Peskine8817f612018-12-18 00:18:46 +01005488 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005489
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005490 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5491 psa_set_key_algorithm( &attributes, alg );
5492 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005493 PSA_ASSERT( psa_import_key( &attributes,
5494 our_key_data->x, our_key_data->len,
5495 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005496
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005497 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005498 PSA_ASSERT( psa_key_derivation_key_agreement(
5499 &operation,
5500 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5501 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005502 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5503 {
5504 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005505 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005506 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005507 NULL, 0 ) );
5508 }
Gilles Peskine59685592018-09-18 12:11:34 +02005509
Gilles Peskinebf491972018-10-25 22:36:12 +02005510 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005511 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005512 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005513 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005514
Gilles Peskinebf491972018-10-25 22:36:12 +02005515 /* Test the actual capacity by reading the output. */
5516 while( actual_capacity > sizeof( output ) )
5517 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005518 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005519 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005520 actual_capacity -= sizeof( output );
5521 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005523 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005524 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005525 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005526
Gilles Peskine59685592018-09-18 12:11:34 +02005527exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005528 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005529 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005530 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005531}
5532/* END_CASE */
5533
5534/* BEGIN_CASE */
5535void key_agreement_output( int alg_arg,
5536 int our_key_type_arg, data_t *our_key_data,
5537 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005538 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005539{
Ronald Cron5425a212020-08-04 14:58:35 +02005540 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005541 psa_algorithm_t alg = alg_arg;
5542 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005543 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005545 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005546
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005547 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5548 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005549
Gilles Peskine8817f612018-12-18 00:18:46 +01005550 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005551
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005552 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5553 psa_set_key_algorithm( &attributes, alg );
5554 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005555 PSA_ASSERT( psa_import_key( &attributes,
5556 our_key_data->x, our_key_data->len,
5557 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005558
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005559 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005560 PSA_ASSERT( psa_key_derivation_key_agreement(
5561 &operation,
5562 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5563 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005564 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5565 {
5566 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005567 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005568 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005569 NULL, 0 ) );
5570 }
Gilles Peskine59685592018-09-18 12:11:34 +02005571
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005572 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005573 actual_output,
5574 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005575 ASSERT_COMPARE( actual_output, expected_output1->len,
5576 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005577 if( expected_output2->len != 0 )
5578 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005579 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005580 actual_output,
5581 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005582 ASSERT_COMPARE( actual_output, expected_output2->len,
5583 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005584 }
Gilles Peskine59685592018-09-18 12:11:34 +02005585
5586exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005587 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005588 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005589 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005590 mbedtls_free( actual_output );
5591}
5592/* END_CASE */
5593
5594/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005595void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005596{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005597 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005598 unsigned char *output = NULL;
5599 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005600 size_t i;
5601 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005602
Simon Butcher49f8e312020-03-03 15:51:50 +00005603 TEST_ASSERT( bytes_arg >= 0 );
5604
Gilles Peskine91892022021-02-08 19:50:26 +01005605 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005606 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005607
Gilles Peskine8817f612018-12-18 00:18:46 +01005608 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005609
Gilles Peskinea50d7392018-06-21 10:22:13 +02005610 /* Run several times, to ensure that every output byte will be
5611 * nonzero at least once with overwhelming probability
5612 * (2^(-8*number_of_runs)). */
5613 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005614 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005615 if( bytes != 0 )
5616 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005617 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005618
Gilles Peskinea50d7392018-06-21 10:22:13 +02005619 for( i = 0; i < bytes; i++ )
5620 {
5621 if( output[i] != 0 )
5622 ++changed[i];
5623 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005624 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005625
5626 /* Check that every byte was changed to nonzero at least once. This
5627 * validates that psa_generate_random is overwriting every byte of
5628 * the output buffer. */
5629 for( i = 0; i < bytes; i++ )
5630 {
5631 TEST_ASSERT( changed[i] != 0 );
5632 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005633
5634exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005635 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005636 mbedtls_free( output );
5637 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005638}
5639/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005640
5641/* BEGIN_CASE */
5642void generate_key( int type_arg,
5643 int bits_arg,
5644 int usage_arg,
5645 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005646 int expected_status_arg,
5647 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005648{
Ronald Cron5425a212020-08-04 14:58:35 +02005649 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005650 psa_key_type_t type = type_arg;
5651 psa_key_usage_t usage = usage_arg;
5652 size_t bits = bits_arg;
5653 psa_algorithm_t alg = alg_arg;
5654 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005655 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005656 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005657
Gilles Peskine8817f612018-12-18 00:18:46 +01005658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005659
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005660 psa_set_key_usage_flags( &attributes, usage );
5661 psa_set_key_algorithm( &attributes, alg );
5662 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005663 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005664
5665 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005666 psa_status_t status = psa_generate_key( &attributes, &key );
5667
5668 if( is_large_key > 0 )
5669 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5670 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005671 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005672 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005673
5674 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005675 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005676 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5677 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005678
Gilles Peskine818ca122018-06-20 18:16:48 +02005679 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005680 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005681 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005682
5683exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005684 /*
5685 * Key attributes may have been returned by psa_get_key_attributes()
5686 * thus reset them as required.
5687 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005688 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005689
Ronald Cron5425a212020-08-04 14:58:35 +02005690 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005691 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005692}
5693/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005694
Gilles Peskinee56e8782019-04-26 17:34:02 +02005695/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5696void generate_key_rsa( int bits_arg,
5697 data_t *e_arg,
5698 int expected_status_arg )
5699{
Ronald Cron5425a212020-08-04 14:58:35 +02005700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005701 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005702 size_t bits = bits_arg;
5703 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5704 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5705 psa_status_t expected_status = expected_status_arg;
5706 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5707 uint8_t *exported = NULL;
5708 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005709 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005710 size_t exported_length = SIZE_MAX;
5711 uint8_t *e_read_buffer = NULL;
5712 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005713 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005714 size_t e_read_length = SIZE_MAX;
5715
5716 if( e_arg->len == 0 ||
5717 ( e_arg->len == 3 &&
5718 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5719 {
5720 is_default_public_exponent = 1;
5721 e_read_size = 0;
5722 }
5723 ASSERT_ALLOC( e_read_buffer, e_read_size );
5724 ASSERT_ALLOC( exported, exported_size );
5725
5726 PSA_ASSERT( psa_crypto_init( ) );
5727
5728 psa_set_key_usage_flags( &attributes, usage );
5729 psa_set_key_algorithm( &attributes, alg );
5730 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5731 e_arg->x, e_arg->len ) );
5732 psa_set_key_bits( &attributes, bits );
5733
5734 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005735 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005736 if( expected_status != PSA_SUCCESS )
5737 goto exit;
5738
5739 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005740 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005741 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5742 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5743 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5744 e_read_buffer, e_read_size,
5745 &e_read_length ) );
5746 if( is_default_public_exponent )
5747 TEST_EQUAL( e_read_length, 0 );
5748 else
5749 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5750
5751 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005752 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005753 goto exit;
5754
5755 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005756 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005757 exported, exported_size,
5758 &exported_length ) );
5759 {
5760 uint8_t *p = exported;
5761 uint8_t *end = exported + exported_length;
5762 size_t len;
5763 /* RSAPublicKey ::= SEQUENCE {
5764 * modulus INTEGER, -- n
5765 * publicExponent INTEGER } -- e
5766 */
5767 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005768 MBEDTLS_ASN1_SEQUENCE |
5769 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005770 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5771 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5772 MBEDTLS_ASN1_INTEGER ) );
5773 if( len >= 1 && p[0] == 0 )
5774 {
5775 ++p;
5776 --len;
5777 }
5778 if( e_arg->len == 0 )
5779 {
5780 TEST_EQUAL( len, 3 );
5781 TEST_EQUAL( p[0], 1 );
5782 TEST_EQUAL( p[1], 0 );
5783 TEST_EQUAL( p[2], 1 );
5784 }
5785 else
5786 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5787 }
5788
5789exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005790 /*
5791 * Key attributes may have been returned by psa_get_key_attributes() or
5792 * set by psa_set_key_domain_parameters() thus reset them as required.
5793 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005794 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005795
Ronald Cron5425a212020-08-04 14:58:35 +02005796 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005797 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005798 mbedtls_free( e_read_buffer );
5799 mbedtls_free( exported );
5800}
5801/* END_CASE */
5802
Darryl Greend49a4992018-06-18 17:27:26 +01005803/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005804void persistent_key_load_key_from_storage( data_t *data,
5805 int type_arg, int bits_arg,
5806 int usage_flags_arg, int alg_arg,
5807 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005808{
Ronald Cron71016a92020-08-28 19:01:50 +02005809 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005810 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5812 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005813 psa_key_type_t type = type_arg;
5814 size_t bits = bits_arg;
5815 psa_key_usage_t usage_flags = usage_flags_arg;
5816 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005817 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005818 unsigned char *first_export = NULL;
5819 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005820 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005821 size_t first_exported_length;
5822 size_t second_exported_length;
5823
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005824 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5825 {
5826 ASSERT_ALLOC( first_export, export_size );
5827 ASSERT_ALLOC( second_export, export_size );
5828 }
Darryl Greend49a4992018-06-18 17:27:26 +01005829
Gilles Peskine8817f612018-12-18 00:18:46 +01005830 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005831
Gilles Peskinec87af662019-05-15 16:12:22 +02005832 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005833 psa_set_key_usage_flags( &attributes, usage_flags );
5834 psa_set_key_algorithm( &attributes, alg );
5835 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005836 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005837
Darryl Green0c6575a2018-11-07 16:05:30 +00005838 switch( generation_method )
5839 {
5840 case IMPORT_KEY:
5841 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005842 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005843 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005844 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005845
Darryl Green0c6575a2018-11-07 16:05:30 +00005846 case GENERATE_KEY:
5847 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005848 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005849 break;
5850
5851 case DERIVE_KEY:
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005852#if PSA_WANT_ALG_HKDF && PSA_WANT_ALG_SHA_256
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005853 {
5854 /* Create base key */
5855 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5856 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5857 psa_set_key_usage_flags( &base_attributes,
5858 PSA_KEY_USAGE_DERIVE );
5859 psa_set_key_algorithm( &base_attributes, derive_alg );
5860 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005861 PSA_ASSERT( psa_import_key( &base_attributes,
5862 data->x, data->len,
5863 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005864 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005865 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005866 PSA_ASSERT( psa_key_derivation_input_key(
5867 &operation,
5868 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005869 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005870 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005871 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005872 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5873 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005874 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005875 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005876 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005877 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005878 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005879#else
5880 TEST_ASSUME( ! "KDF not supported in this configuration" );
5881#endif
5882 break;
5883
5884 default:
5885 TEST_ASSERT( ! "generation_method not implemented in test" );
5886 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005887 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005888 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005889
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005890 /* Export the key if permitted by the key policy. */
5891 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5892 {
Ronald Cron5425a212020-08-04 14:58:35 +02005893 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005894 first_export, export_size,
5895 &first_exported_length ) );
5896 if( generation_method == IMPORT_KEY )
5897 ASSERT_COMPARE( data->x, data->len,
5898 first_export, first_exported_length );
5899 }
Darryl Greend49a4992018-06-18 17:27:26 +01005900
5901 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005902 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005903 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005904 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005905
Darryl Greend49a4992018-06-18 17:27:26 +01005906 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005907 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005908 TEST_ASSERT( mbedtls_svc_key_id_equal(
5909 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005910 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5911 PSA_KEY_LIFETIME_PERSISTENT );
5912 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5913 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5914 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5915 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005916
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005917 /* Export the key again if permitted by the key policy. */
5918 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005919 {
Ronald Cron5425a212020-08-04 14:58:35 +02005920 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005921 second_export, export_size,
5922 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005923 ASSERT_COMPARE( first_export, first_exported_length,
5924 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005925 }
5926
5927 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005928 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005929 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005930
5931exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005932 /*
5933 * Key attributes may have been returned by psa_get_key_attributes()
5934 * thus reset them as required.
5935 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005936 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005937
Darryl Greend49a4992018-06-18 17:27:26 +01005938 mbedtls_free( first_export );
5939 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005940 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005941 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005942 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005943 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005944}
5945/* END_CASE */