blob: 1051e0cae0b9325355641f5e4190e5c60558bcaf [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Jaeden Amerof24c7f82018-06-27 17:20:43 +010015/** An invalid export length that will never be set by psa_export_key(). */
16static const size_t INVALID_EXPORT_LENGTH = ~0U;
17
Gilles Peskinef426e0f2019-02-25 17:42:03 +010018/* A hash algorithm that is known to be supported.
19 *
20 * This is used in some smoke tests.
21 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010022#if defined(PSA_WANT_ALG_MD2)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010023#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
Gilles Peskined6dc40c2021-01-12 12:55:31 +010024#elif defined(PSA_WANT_ALG_MD4)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010025#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
Gilles Peskined6dc40c2021-01-12 12:55:31 +010026#elif defined(PSA_WANT_ALG_MD5)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010027#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
28/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
29 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
30 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
31 * implausible anyway. */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010032#elif defined(PSA_WANT_ALG_SHA_1)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010033#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
Gilles Peskined6dc40c2021-01-12 12:55:31 +010034#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010035#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
Gilles Peskined6dc40c2021-01-12 12:55:31 +010036#elif defined(PSA_WANT_ALG_SHA_384)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010037#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
Gilles Peskined6dc40c2021-01-12 12:55:31 +010038#elif defined(PSA_WANT_ALG_SHA_512)
39#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
40#elif defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010041#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
42#else
43#undef KNOWN_SUPPORTED_HASH_ALG
44#endif
45
46/* A block cipher that is known to be supported.
47 *
48 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
49 */
50#if defined(MBEDTLS_AES_C)
51#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
52#elif defined(MBEDTLS_ARIA_C)
53#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
54#elif defined(MBEDTLS_CAMELLIA_C)
55#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
56#undef KNOWN_SUPPORTED_BLOCK_CIPHER
57#endif
58
59/* A MAC mode that is known to be supported.
60 *
61 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
62 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
63 *
64 * This is used in some smoke tests.
65 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010066#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010067#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
68#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
69#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
70#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
71#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
72#else
73#undef KNOWN_SUPPORTED_MAC_ALG
74#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
75#endif
76
77/* A cipher algorithm and key type that are known to be supported.
78 *
79 * This is used in some smoke tests.
80 */
81#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
82#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
83#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
84#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
85#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
86#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
87#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
89#else
90#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91#endif
92#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
93#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
94#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
95#elif defined(MBEDTLS_RC4_C)
96#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
97#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
98#else
99#undef KNOWN_SUPPORTED_CIPHER_ALG
100#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
101#endif
102
Gilles Peskine667c1112019-12-03 19:03:20 +0100103#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100104int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
Gilles Peskine667c1112019-12-03 19:03:20 +0100105{
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100106 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
107 PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100108}
109#else
110int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
111{
112 (void) lifetime;
113 return( 0 );
114}
115#endif
116
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200117/** Test if a buffer contains a constant byte value.
118 *
119 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200120 *
121 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200122 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200123 * \param size Size of the buffer in bytes.
124 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200125 * \return 1 if the buffer is all-bits-zero.
126 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200127 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200128static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200129{
130 size_t i;
131 for( i = 0; i < size; i++ )
132 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200133 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200134 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200135 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200136 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200137}
Gilles Peskine818ca122018-06-20 18:16:48 +0200138
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200139/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
140static int asn1_write_10x( unsigned char **p,
141 unsigned char *start,
142 size_t bits,
143 unsigned char x )
144{
145 int ret;
146 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200147 if( bits == 0 )
148 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
149 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200150 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300151 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200152 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
153 *p -= len;
154 ( *p )[len-1] = x;
155 if( bits % 8 == 0 )
156 ( *p )[1] |= 1;
157 else
158 ( *p )[0] |= 1 << ( bits % 8 );
159 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
160 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
161 MBEDTLS_ASN1_INTEGER ) );
162 return( len );
163}
164
165static int construct_fake_rsa_key( unsigned char *buffer,
166 size_t buffer_size,
167 unsigned char **p,
168 size_t bits,
169 int keypair )
170{
171 size_t half_bits = ( bits + 1 ) / 2;
172 int ret;
173 int len = 0;
174 /* Construct something that looks like a DER encoding of
175 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
176 * RSAPrivateKey ::= SEQUENCE {
177 * version Version,
178 * modulus INTEGER, -- n
179 * publicExponent INTEGER, -- e
180 * privateExponent INTEGER, -- d
181 * prime1 INTEGER, -- p
182 * prime2 INTEGER, -- q
183 * exponent1 INTEGER, -- d mod (p-1)
184 * exponent2 INTEGER, -- d mod (q-1)
185 * coefficient INTEGER, -- (inverse of q) mod p
186 * otherPrimeInfos OtherPrimeInfos OPTIONAL
187 * }
188 * Or, for a public key, the same structure with only
189 * version, modulus and publicExponent.
190 */
191 *p = buffer + buffer_size;
192 if( keypair )
193 {
194 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
195 asn1_write_10x( p, buffer, half_bits, 1 ) );
196 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
197 asn1_write_10x( p, buffer, half_bits, 1 ) );
198 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
199 asn1_write_10x( p, buffer, half_bits, 1 ) );
200 MBEDTLS_ASN1_CHK_ADD( len, /* q */
201 asn1_write_10x( p, buffer, half_bits, 1 ) );
202 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
203 asn1_write_10x( p, buffer, half_bits, 3 ) );
204 MBEDTLS_ASN1_CHK_ADD( len, /* d */
205 asn1_write_10x( p, buffer, bits, 1 ) );
206 }
207 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
208 asn1_write_10x( p, buffer, 17, 1 ) );
209 MBEDTLS_ASN1_CHK_ADD( len, /* n */
210 asn1_write_10x( p, buffer, bits, 1 ) );
211 if( keypair )
212 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
213 mbedtls_asn1_write_int( p, buffer, 0 ) );
214 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
215 {
216 const unsigned char tag =
217 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
218 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
219 }
220 return( len );
221}
222
Ronald Cron5425a212020-08-04 14:58:35 +0200223int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100224{
225 int ok = 0;
226 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
227 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200228 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100229 psa_key_type_t type;
230 psa_key_type_t bits;
231
232 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
233 lifetime = psa_get_key_lifetime( &attributes );
234 id = psa_get_key_id( &attributes );
235 type = psa_get_key_type( &attributes );
236 bits = psa_get_key_bits( &attributes );
237
238 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200239 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200240 {
241 TEST_ASSERT(
242 ( PSA_KEY_ID_VOLATILE_MIN <=
243 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
244 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
245 PSA_KEY_ID_VOLATILE_MAX ) );
246 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100247 else
248 {
249 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200250 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
251 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100252 }
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254 /* randomly-generated 64-bit constant, should never appear in test data */
255 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
256 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100257 if( lifetime_is_dynamic_secure_element( lifetime ) )
Gilles Peskine667c1112019-12-03 19:03:20 +0100258 {
259 /* Mbed Crypto currently always exposes the slot number to
260 * applications. This is not mandated by the PSA specification
261 * and may change in future versions. */
262 TEST_EQUAL( status, 0 );
263 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
264 }
265 else
266 {
267 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
268 }
269#endif
270
271 /* Type and size */
272 TEST_ASSERT( type != 0 );
273 TEST_ASSERT( bits != 0 );
274 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
275 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
276 TEST_ASSERT( bits % 8 == 0 );
277
278 /* MAX macros concerning specific key types */
279 if( PSA_KEY_TYPE_IS_ECC( type ) )
280 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
281 else if( PSA_KEY_TYPE_IS_RSA( type ) )
282 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100283 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100284
285 ok = 1;
286
287exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100288 /*
289 * Key attributes may have been returned by psa_get_key_attributes()
290 * thus reset them as required.
291 */
Gilles Peskine667c1112019-12-03 19:03:20 +0100292 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100293
Gilles Peskine667c1112019-12-03 19:03:20 +0100294 return( ok );
295}
296
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100297int exercise_mac_setup( psa_key_type_t key_type,
298 const unsigned char *key_bytes,
299 size_t key_length,
300 psa_algorithm_t alg,
301 psa_mac_operation_t *operation,
302 psa_status_t *status )
303{
Ronald Cron5425a212020-08-04 14:58:35 +0200304 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200305 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100306
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100307 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200308 psa_set_key_algorithm( &attributes, alg );
309 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200310 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100311
Ronald Cron5425a212020-08-04 14:58:35 +0200312 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100313 /* Whether setup succeeded or failed, abort must succeed. */
314 PSA_ASSERT( psa_mac_abort( operation ) );
315 /* If setup failed, reproduce the failure, so that the caller can
316 * test the resulting state of the operation object. */
317 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100318 {
Ronald Cron5425a212020-08-04 14:58:35 +0200319 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100320 }
321
Ronald Cron5425a212020-08-04 14:58:35 +0200322 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100323 return( 1 );
324
325exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200326 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100327 return( 0 );
328}
329
330int exercise_cipher_setup( psa_key_type_t key_type,
331 const unsigned char *key_bytes,
332 size_t key_length,
333 psa_algorithm_t alg,
334 psa_cipher_operation_t *operation,
335 psa_status_t *status )
336{
Ronald Cron5425a212020-08-04 14:58:35 +0200337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100339
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200340 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
341 psa_set_key_algorithm( &attributes, alg );
342 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200343 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100344
Ronald Cron5425a212020-08-04 14:58:35 +0200345 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100346 /* Whether setup succeeded or failed, abort must succeed. */
347 PSA_ASSERT( psa_cipher_abort( operation ) );
348 /* If setup failed, reproduce the failure, so that the caller can
349 * test the resulting state of the operation object. */
350 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100351 {
Ronald Cron5425a212020-08-04 14:58:35 +0200352 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100353 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100354 }
355
Ronald Cron5425a212020-08-04 14:58:35 +0200356 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100357 return( 1 );
358
359exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200360 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100361 return( 0 );
362}
363
Ronald Cron5425a212020-08-04 14:58:35 +0200364static int exercise_mac_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200365 psa_key_usage_t usage,
366 psa_algorithm_t alg )
367{
Jaeden Amero769ce272019-01-04 11:48:03 +0000368 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200369 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200370 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200371 size_t mac_length = sizeof( mac );
372
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100373 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 {
Ronald Cron5425a212020-08-04 14:58:35 +0200375 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100376 PSA_ASSERT( psa_mac_update( &operation,
377 input, sizeof( input ) ) );
378 PSA_ASSERT( psa_mac_sign_finish( &operation,
379 mac, sizeof( mac ),
380 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200381 }
382
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100383 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200384 {
385 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100386 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200387 PSA_SUCCESS :
388 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200389 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100390 PSA_ASSERT( psa_mac_update( &operation,
391 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100392 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
393 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200394 }
395
396 return( 1 );
397
398exit:
399 psa_mac_abort( &operation );
400 return( 0 );
401}
402
Ronald Cron5425a212020-08-04 14:58:35 +0200403static int exercise_cipher_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200404 psa_key_usage_t usage,
405 psa_algorithm_t alg )
406{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000407 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200408 unsigned char iv[16] = {0};
409 size_t iv_length = sizeof( iv );
410 const unsigned char plaintext[16] = "Hello, world...";
411 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
412 size_t ciphertext_length = sizeof( ciphertext );
413 unsigned char decrypted[sizeof( ciphertext )];
414 size_t part_length;
415
416 if( usage & PSA_KEY_USAGE_ENCRYPT )
417 {
Ronald Cron5425a212020-08-04 14:58:35 +0200418 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100419 PSA_ASSERT( psa_cipher_generate_iv( &operation,
420 iv, sizeof( iv ),
421 &iv_length ) );
422 PSA_ASSERT( psa_cipher_update( &operation,
423 plaintext, sizeof( plaintext ),
424 ciphertext, sizeof( ciphertext ),
425 &ciphertext_length ) );
426 PSA_ASSERT( psa_cipher_finish( &operation,
427 ciphertext + ciphertext_length,
428 sizeof( ciphertext ) - ciphertext_length,
429 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200430 ciphertext_length += part_length;
431 }
432
433 if( usage & PSA_KEY_USAGE_DECRYPT )
434 {
435 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200436 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200437 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
438 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200440 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200441 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
442 * have this macro yet. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100443 iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200444 psa_get_key_type( &attributes ) );
445 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100446 psa_reset_key_attributes( &attributes );
Gilles Peskine818ca122018-06-20 18:16:48 +0200447 }
Ronald Cron5425a212020-08-04 14:58:35 +0200448 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100449 PSA_ASSERT( psa_cipher_set_iv( &operation,
450 iv, iv_length ) );
451 PSA_ASSERT( psa_cipher_update( &operation,
452 ciphertext, ciphertext_length,
453 decrypted, sizeof( decrypted ),
454 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200455 status = psa_cipher_finish( &operation,
456 decrypted + part_length,
457 sizeof( decrypted ) - part_length,
458 &part_length );
459 /* For a stream cipher, all inputs are valid. For a block cipher,
460 * if the input is some aribtrary data rather than an actual
461 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200462 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200463 TEST_ASSERT( status == PSA_SUCCESS ||
464 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200465 else
466 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200467 }
468
469 return( 1 );
470
471exit:
472 psa_cipher_abort( &operation );
473 return( 0 );
474}
475
Ronald Cron5425a212020-08-04 14:58:35 +0200476static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200477 psa_key_usage_t usage,
478 psa_algorithm_t alg )
479{
480 unsigned char nonce[16] = {0};
481 size_t nonce_length = sizeof( nonce );
482 unsigned char plaintext[16] = "Hello, world...";
483 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
484 size_t ciphertext_length = sizeof( ciphertext );
485 size_t plaintext_length = sizeof( ciphertext );
486
487 if( usage & PSA_KEY_USAGE_ENCRYPT )
488 {
Ronald Cron5425a212020-08-04 14:58:35 +0200489 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100490 nonce, nonce_length,
491 NULL, 0,
492 plaintext, sizeof( plaintext ),
493 ciphertext, sizeof( ciphertext ),
494 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200495 }
496
497 if( usage & PSA_KEY_USAGE_DECRYPT )
498 {
499 psa_status_t verify_status =
500 ( usage & PSA_KEY_USAGE_ENCRYPT ?
501 PSA_SUCCESS :
502 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200503 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100504 nonce, nonce_length,
505 NULL, 0,
506 ciphertext, ciphertext_length,
507 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100508 &plaintext_length ),
509 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200510 }
511
512 return( 1 );
513
514exit:
515 return( 0 );
516}
517
Ronald Cron5425a212020-08-04 14:58:35 +0200518static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200519 psa_key_usage_t usage,
520 psa_algorithm_t alg )
521{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200522 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
523 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100524 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200525 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100526 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
527
528 /* If the policy allows signing with any hash, just pick one. */
529 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
530 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100531#if defined(KNOWN_SUPPORTED_HASH_ALG)
532 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
533 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100534#else
535 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100536 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100537#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100538 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200539
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100540 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200541 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200542 /* Some algorithms require the payload to have the size of
543 * the hash encoded in the algorithm. Use this input size
544 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200545 if( hash_alg != 0 )
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100546 payload_length = PSA_HASH_LENGTH( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200547 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100548 payload, payload_length,
549 signature, sizeof( signature ),
550 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200551 }
552
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100553 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200554 {
555 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100556 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200557 PSA_SUCCESS :
558 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200559 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100560 payload, payload_length,
561 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100562 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200563 }
564
565 return( 1 );
566
567exit:
568 return( 0 );
569}
570
Ronald Cron5425a212020-08-04 14:58:35 +0200571static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200572 psa_key_usage_t usage,
573 psa_algorithm_t alg )
574{
575 unsigned char plaintext[256] = "Hello, world...";
576 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
577 size_t ciphertext_length = sizeof( ciphertext );
578 size_t plaintext_length = 16;
579
580 if( usage & PSA_KEY_USAGE_ENCRYPT )
581 {
Ronald Cron5425a212020-08-04 14:58:35 +0200582 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100583 plaintext, plaintext_length,
584 NULL, 0,
585 ciphertext, sizeof( ciphertext ),
586 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200587 }
588
589 if( usage & PSA_KEY_USAGE_DECRYPT )
590 {
591 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200592 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200593 ciphertext, ciphertext_length,
594 NULL, 0,
595 plaintext, sizeof( plaintext ),
596 &plaintext_length );
597 TEST_ASSERT( status == PSA_SUCCESS ||
598 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
599 ( status == PSA_ERROR_INVALID_ARGUMENT ||
600 status == PSA_ERROR_INVALID_PADDING ) ) );
601 }
602
603 return( 1 );
604
605exit:
606 return( 0 );
607}
Gilles Peskine02b75072018-07-01 22:31:34 +0200608
Janos Follathf2815ea2019-07-03 12:41:36 +0100609static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200610 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100611 psa_algorithm_t alg,
612 unsigned char* input1, size_t input1_length,
613 unsigned char* input2, size_t input2_length,
614 size_t capacity )
615{
616 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
617 if( PSA_ALG_IS_HKDF( alg ) )
618 {
619 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
620 PSA_KEY_DERIVATION_INPUT_SALT,
621 input1, input1_length ) );
622 PSA_ASSERT( psa_key_derivation_input_key( operation,
623 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200624 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100625 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
626 PSA_KEY_DERIVATION_INPUT_INFO,
627 input2,
628 input2_length ) );
629 }
630 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
631 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
632 {
633 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
634 PSA_KEY_DERIVATION_INPUT_SEED,
635 input1, input1_length ) );
636 PSA_ASSERT( psa_key_derivation_input_key( operation,
637 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200638 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100639 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
640 PSA_KEY_DERIVATION_INPUT_LABEL,
641 input2, input2_length ) );
642 }
643 else
644 {
645 TEST_ASSERT( ! "Key derivation algorithm not supported" );
646 }
647
Gilles Peskinec744d992019-07-30 17:26:54 +0200648 if( capacity != SIZE_MAX )
649 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100650
651 return( 1 );
652
653exit:
654 return( 0 );
655}
656
657
Ronald Cron5425a212020-08-04 14:58:35 +0200658static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200659 psa_key_usage_t usage,
660 psa_algorithm_t alg )
661{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200662 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100663 unsigned char input1[] = "Input 1";
664 size_t input1_length = sizeof( input1 );
665 unsigned char input2[] = "Input 2";
666 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200667 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100668 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200669
670 if( usage & PSA_KEY_USAGE_DERIVE )
671 {
Ronald Cron5425a212020-08-04 14:58:35 +0200672 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100673 input1, input1_length,
674 input2, input2_length, capacity ) )
675 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200676
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200677 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200678 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100679 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200680 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200681 }
682
683 return( 1 );
684
685exit:
686 return( 0 );
687}
688
Gilles Peskinec7998b72018-11-07 18:45:02 +0100689/* We need two keys to exercise key agreement. Exercise the
690 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200691static psa_status_t key_agreement_with_self(
692 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200693 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100694{
695 psa_key_type_t private_key_type;
696 psa_key_type_t public_key_type;
697 size_t key_bits;
698 uint8_t *public_key = NULL;
699 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200700 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200701 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
702 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200703 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100705
Ronald Cron5425a212020-08-04 14:58:35 +0200706 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200707 private_key_type = psa_get_key_type( &attributes );
708 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200709 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100710 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100711 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200712 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100713 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100714
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200715 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200716 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200717 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100718exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100719 /*
720 * Key attributes may have been returned by psa_get_key_attributes()
721 * thus reset them as required.
722 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200723 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100724
725 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100726 return( status );
727}
728
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200729/* We need two keys to exercise key agreement. Exercise the
730 * private key against its own public key. */
731static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200732 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200733{
734 psa_key_type_t private_key_type;
735 psa_key_type_t public_key_type;
736 size_t key_bits;
737 uint8_t *public_key = NULL;
738 size_t public_key_length;
739 uint8_t output[1024];
740 size_t output_length;
741 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200742 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
743 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200744 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200746
Ronald Cron5425a212020-08-04 14:58:35 +0200747 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200748 private_key_type = psa_get_key_type( &attributes );
749 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200750 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100751 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200752 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200753 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200754 public_key, public_key_length,
755 &public_key_length ) );
756
Ronald Cron5425a212020-08-04 14:58:35 +0200757 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200758 public_key, public_key_length,
759 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200760exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100761 /*
762 * Key attributes may have been returned by psa_get_key_attributes()
763 * thus reset them as required.
764 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200765 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100766
767 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200768 return( status );
769}
770
Ronald Cron5425a212020-08-04 14:58:35 +0200771static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200772 psa_key_usage_t usage,
773 psa_algorithm_t alg )
774{
775 int ok = 0;
776
777 if( usage & PSA_KEY_USAGE_DERIVE )
778 {
779 /* We need two keys to exercise key agreement. Exercise the
780 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200781 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200782 }
783 ok = 1;
784
785exit:
786 return( ok );
787}
788
Ronald Cron5425a212020-08-04 14:58:35 +0200789static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200790 psa_key_usage_t usage,
791 psa_algorithm_t alg )
792{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200793 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200794 unsigned char output[1];
795 int ok = 0;
796
797 if( usage & PSA_KEY_USAGE_DERIVE )
798 {
799 /* We need two keys to exercise key agreement. Exercise the
800 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200801 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200802 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200803 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200804 output,
805 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200806 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200807 }
808 ok = 1;
809
810exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200811 return( ok );
812}
813
Jaeden Amerof7dca862019-06-27 17:31:33 +0100814int asn1_skip_integer( unsigned char **p, const unsigned char *end,
815 size_t min_bits, size_t max_bits,
816 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200817{
818 size_t len;
819 size_t actual_bits;
820 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100821 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100822 MBEDTLS_ASN1_INTEGER ),
823 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200824
825 /* Check if the retrieved length doesn't extend the actual buffer's size.
826 * It is assumed here, that end >= p, which validates casting to size_t. */
827 TEST_ASSERT( len <= (size_t)( end - *p) );
828
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200829 /* Tolerate a slight departure from DER encoding:
830 * - 0 may be represented by an empty string or a 1-byte string.
831 * - The sign bit may be used as a value bit. */
832 if( ( len == 1 && ( *p )[0] == 0 ) ||
833 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
834 {
835 ++( *p );
836 --len;
837 }
838 if( min_bits == 0 && len == 0 )
839 return( 1 );
840 msb = ( *p )[0];
841 TEST_ASSERT( msb != 0 );
842 actual_bits = 8 * ( len - 1 );
843 while( msb != 0 )
844 {
845 msb >>= 1;
846 ++actual_bits;
847 }
848 TEST_ASSERT( actual_bits >= min_bits );
849 TEST_ASSERT( actual_bits <= max_bits );
850 if( must_be_odd )
851 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
852 *p += len;
853 return( 1 );
854exit:
855 return( 0 );
856}
857
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200858static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
859 uint8_t *exported, size_t exported_length )
860{
861 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100862 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200863 else
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100864 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200865
866#if defined(MBEDTLS_DES_C)
867 if( type == PSA_KEY_TYPE_DES )
868 {
869 /* Check the parity bits. */
870 unsigned i;
871 for( i = 0; i < bits / 8; i++ )
872 {
873 unsigned bit_count = 0;
874 unsigned m;
875 for( m = 1; m <= 0x100; m <<= 1 )
876 {
877 if( exported[i] & m )
878 ++bit_count;
879 }
880 TEST_ASSERT( bit_count % 2 != 0 );
881 }
882 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200883 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200884#endif
885
886#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200887 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200888 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200889 uint8_t *p = exported;
890 uint8_t *end = exported + exported_length;
891 size_t len;
892 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200893 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200894 * modulus INTEGER, -- n
895 * publicExponent INTEGER, -- e
896 * privateExponent INTEGER, -- d
897 * prime1 INTEGER, -- p
898 * prime2 INTEGER, -- q
899 * exponent1 INTEGER, -- d mod (p-1)
900 * exponent2 INTEGER, -- d mod (q-1)
901 * coefficient INTEGER, -- (inverse of q) mod p
902 * }
903 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100904 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
905 MBEDTLS_ASN1_SEQUENCE |
906 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
907 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200908 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
909 goto exit;
910 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
911 goto exit;
912 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
913 goto exit;
914 /* Require d to be at least half the size of n. */
915 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
916 goto exit;
917 /* Require p and q to be at most half the size of n, rounded up. */
918 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
919 goto exit;
920 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
921 goto exit;
922 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
923 goto exit;
924 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
925 goto exit;
926 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
927 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100928 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100929 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200930 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200931#endif /* MBEDTLS_RSA_C */
932
933#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200934 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200935 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100936 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100937 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100938 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200939 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200940#endif /* MBEDTLS_ECP_C */
941
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200942 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
943 {
944 uint8_t *p = exported;
945 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200946#if defined(MBEDTLS_RSA_C)
947 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
948 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100949 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200950 /* RSAPublicKey ::= SEQUENCE {
951 * modulus INTEGER, -- n
952 * publicExponent INTEGER } -- e
953 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100954 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
955 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100956 MBEDTLS_ASN1_CONSTRUCTED ),
957 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100958 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200959 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
960 goto exit;
961 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
962 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100963 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200964 }
965 else
966#endif /* MBEDTLS_RSA_C */
967#if defined(MBEDTLS_ECP_C)
968 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
969 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200970 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
971 {
972 /* The representation of an ECC Montgomery public key is
973 * the raw compressed point */
974 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
975 }
976 else
977 {
978 /* The representation of an ECC Weierstrass public key is:
979 * - The byte 0x04;
980 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
981 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
982 * - where m is the bit size associated with the curve.
983 */
984 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
985 TEST_EQUAL( p[0], 4 );
986 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200987 }
988 else
989#endif /* MBEDTLS_ECP_C */
990 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100991 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200992 mbedtls_snprintf( message, sizeof( message ),
993 "No sanity check for public key type=0x%08lx",
994 (unsigned long) type );
995 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200996 (void) p;
997 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200998 return( 0 );
999 }
1000 }
1001 else
1002
1003 {
1004 /* No sanity checks for other types */
1005 }
1006
1007 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001008
1009exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001010 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001011}
1012
Ronald Cron5425a212020-08-04 14:58:35 +02001013static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001014 psa_key_usage_t usage )
1015{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001017 uint8_t *exported = NULL;
1018 size_t exported_size = 0;
1019 size_t exported_length = 0;
1020 int ok = 0;
1021
Ronald Cron5425a212020-08-04 14:58:35 +02001022 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001023
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001024 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1025 psa_get_key_type( &attributes ),
1026 psa_get_key_bits( &attributes ) );
1027 ASSERT_ALLOC( exported, exported_size );
1028
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001029 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001030 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001031 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001032 TEST_EQUAL( psa_export_key( key, exported,
1033 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001034 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001035 ok = 1;
1036 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001037 }
1038
Ronald Cron5425a212020-08-04 14:58:35 +02001039 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001040 exported, exported_size,
1041 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001042 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1043 psa_get_key_bits( &attributes ),
1044 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001045
1046exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001047 /*
1048 * Key attributes may have been returned by psa_get_key_attributes()
1049 * thus reset them as required.
1050 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001051 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001052
1053 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001054 return( ok );
1055}
1056
Ronald Cron5425a212020-08-04 14:58:35 +02001057static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001058{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001060 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001061 uint8_t *exported = NULL;
1062 size_t exported_size = 0;
1063 size_t exported_length = 0;
1064 int ok = 0;
1065
Ronald Cron5425a212020-08-04 14:58:35 +02001066 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001067 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001068 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001069 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1070 psa_get_key_type( &attributes ),
1071 psa_get_key_bits( &attributes ) );
1072 ASSERT_ALLOC( exported, exported_size );
1073
1074 TEST_EQUAL( psa_export_public_key( key, exported,
1075 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001076 PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001077 ok = 1;
1078 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001079 }
1080
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001081 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001082 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001083 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1084 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001085 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001086
Ronald Cron5425a212020-08-04 14:58:35 +02001087 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001088 exported, exported_size,
1089 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001090 ok = exported_key_sanity_check( public_type,
1091 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001092 exported, exported_length );
1093
1094exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001095 /*
1096 * Key attributes may have been returned by psa_get_key_attributes()
1097 * thus reset them as required.
1098 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001099 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001100
1101 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001102 return( ok );
1103}
1104
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001105/** Do smoke tests on a key.
1106 *
1107 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1108 * sign/verify, or derivation) that is permitted according to \p usage.
1109 * \p usage and \p alg should correspond to the expected policy on the
1110 * key.
1111 *
1112 * Export the key if permitted by \p usage, and check that the output
1113 * looks sensible. If \p usage forbids export, check that
1114 * \p psa_export_key correctly rejects the attempt. If the key is
1115 * asymmetric, also check \p psa_export_public_key.
1116 *
1117 * If the key fails the tests, this function calls the test framework's
1118 * `test_fail` function and returns false. Otherwise this function returns
1119 * true. Therefore it should be used as follows:
1120 * ```
1121 * if( ! exercise_key( ... ) ) goto exit;
1122 * ```
1123 *
Ronald Cron5425a212020-08-04 14:58:35 +02001124 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001125 * \p alg.
1126 * \param usage The usage flags to assume.
1127 * \param alg The algorithm to exercise.
1128 *
1129 * \retval 0 The key failed the smoke tests.
1130 * \retval 1 The key passed the smoke tests.
1131 */
Ronald Cron5425a212020-08-04 14:58:35 +02001132static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001133 psa_key_usage_t usage,
1134 psa_algorithm_t alg )
1135{
1136 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001137
Ronald Cron5425a212020-08-04 14:58:35 +02001138 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001139 return( 0 );
1140
Gilles Peskine02b75072018-07-01 22:31:34 +02001141 if( alg == 0 )
1142 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1143 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001144 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001145 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001146 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001147 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001148 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001149 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001150 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001151 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001152 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001153 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001154 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001155 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001156 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001157 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001158 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001159 else
1160 {
1161 char message[40];
1162 mbedtls_snprintf( message, sizeof( message ),
1163 "No code to exercise alg=0x%08lx",
1164 (unsigned long) alg );
1165 test_fail( message, __LINE__, __FILE__ );
1166 ok = 0;
1167 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001168
Ronald Cron5425a212020-08-04 14:58:35 +02001169 ok = ok && exercise_export_key( key, usage );
1170 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001171
Gilles Peskine02b75072018-07-01 22:31:34 +02001172 return( ok );
1173}
1174
Gilles Peskine10df3412018-10-25 22:35:43 +02001175static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1176 psa_algorithm_t alg )
1177{
1178 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1179 {
1180 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001181 PSA_KEY_USAGE_VERIFY_HASH :
1182 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001183 }
1184 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1185 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1186 {
1187 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1188 PSA_KEY_USAGE_ENCRYPT :
1189 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1190 }
1191 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1192 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1193 {
1194 return( PSA_KEY_USAGE_DERIVE );
1195 }
1196 else
1197 {
1198 return( 0 );
1199 }
1200
1201}
Darryl Green0c6575a2018-11-07 16:05:30 +00001202
Ronald Cron5425a212020-08-04 14:58:35 +02001203static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001204{
1205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001206 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001207 uint8_t buffer[1];
1208 size_t length;
1209 int ok = 0;
1210
Ronald Cronecfb2372020-07-23 17:13:42 +02001211 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1213 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1214 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001215 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001216 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001217 TEST_EQUAL(
1218 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1219 TEST_EQUAL(
1220 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001221 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001222 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1223 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1224 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1225 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1226
Ronald Cron5425a212020-08-04 14:58:35 +02001227 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001228 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001229 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001230 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001231 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001232
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001233 ok = 1;
1234
1235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001236 /*
1237 * Key attributes may have been returned by psa_get_key_attributes()
1238 * thus reset them as required.
1239 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001240 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001241
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001242 return( ok );
1243}
1244
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001245/* Assert that a key isn't reported as having a slot number. */
1246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1247#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1248 do \
1249 { \
1250 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1251 TEST_EQUAL( psa_get_key_slot_number( \
1252 attributes, \
1253 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1254 PSA_ERROR_INVALID_ARGUMENT ); \
1255 } \
1256 while( 0 )
1257#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1258#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1259 ( (void) 0 )
1260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1261
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001262/* An overapproximation of the amount of storage needed for a key of the
1263 * given type and with the given content. The API doesn't make it easy
1264 * to find a good value for the size. The current implementation doesn't
1265 * care about the value anyway. */
1266#define KEY_BITS_FROM_DATA( type, data ) \
1267 ( data )->len
1268
Darryl Green0c6575a2018-11-07 16:05:30 +00001269typedef enum {
1270 IMPORT_KEY = 0,
1271 GENERATE_KEY = 1,
1272 DERIVE_KEY = 2
1273} generate_method;
1274
Gilles Peskinee59236f2018-01-27 23:32:46 +01001275/* END_HEADER */
1276
1277/* BEGIN_DEPENDENCIES
1278 * depends_on:MBEDTLS_PSA_CRYPTO_C
1279 * END_DEPENDENCIES
1280 */
1281
1282/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001283void static_checks( )
1284{
1285 size_t max_truncated_mac_size =
1286 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1287
1288 /* Check that the length for a truncated MAC always fits in the algorithm
1289 * encoding. The shifted mask is the maximum truncated value. The
1290 * untruncated algorithm may be one byte larger. */
1291 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001292
1293#if defined(MBEDTLS_TEST_DEPRECATED)
1294 /* Check deprecated constants. */
1295 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1296 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1297 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1298 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1299 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1300 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1301 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1302 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001303
Paul Elliott8ff510a2020-06-02 17:19:28 +01001304 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1305 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1306 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1308 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1309 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1310 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1311 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1312 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1314 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1326 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1328 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1329 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1330 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1331 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1332 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1333 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1334
1335 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1336 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1337 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1338 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1339 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1340 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1341 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1342 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001343
Paul Elliott75e27032020-06-03 15:17:39 +01001344 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1345 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1346 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1347 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1348 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1349
1350 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1351 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001352#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001353}
1354/* END_CASE */
1355
1356/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001357void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001358 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001359 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001360{
Gilles Peskine4747d192019-04-17 15:05:45 +02001361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001362 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001363 psa_key_lifetime_t lifetime = lifetime_arg;
1364 psa_key_usage_t usage_flags = usage_flags_arg;
1365 psa_algorithm_t alg = alg_arg;
1366 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001367 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001368
Ronald Cronecfb2372020-07-23 17:13:42 +02001369 TEST_EQUAL(
1370 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1371 TEST_EQUAL(
1372 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001373 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1374 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1375 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1376 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001377 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001378
Gilles Peskinec87af662019-05-15 16:12:22 +02001379 psa_set_key_id( &attributes, id );
1380 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001381 psa_set_key_usage_flags( &attributes, usage_flags );
1382 psa_set_key_algorithm( &attributes, alg );
1383 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001384 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001385
Ronald Cronecfb2372020-07-23 17:13:42 +02001386 TEST_ASSERT( mbedtls_svc_key_id_equal(
1387 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001388 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1389 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1390 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1391 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001392 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001393
1394 psa_reset_key_attributes( &attributes );
1395
Ronald Cronecfb2372020-07-23 17:13:42 +02001396 TEST_EQUAL(
1397 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1398 TEST_EQUAL(
1399 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001400 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1401 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1402 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1403 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001404 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001405}
1406/* END_CASE */
1407
1408/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001409void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1410 int id2_arg, int owner_id2_arg,
1411 int expected_id_arg, int expected_owner_id_arg,
1412 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001413{
1414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001415 mbedtls_svc_key_id_t id1 =
1416 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001417 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001418 mbedtls_svc_key_id_t id2 =
1419 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001420 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001421 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001422 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1423
1424 if( id1_arg != -1 )
1425 psa_set_key_id( &attributes, id1 );
1426 if( lifetime_arg != -1 )
1427 psa_set_key_lifetime( &attributes, lifetime );
1428 if( id2_arg != -1 )
1429 psa_set_key_id( &attributes, id2 );
1430
Ronald Cronecfb2372020-07-23 17:13:42 +02001431 TEST_ASSERT( mbedtls_svc_key_id_equal(
1432 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001433 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1434}
1435/* END_CASE */
1436
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001437/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1438void slot_number_attribute( )
1439{
1440 psa_key_slot_number_t slot_number = 0xdeadbeef;
1441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1442
1443 /* Initially, there is no slot number. */
1444 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1445 PSA_ERROR_INVALID_ARGUMENT );
1446
1447 /* Test setting a slot number. */
1448 psa_set_key_slot_number( &attributes, 0 );
1449 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1450 TEST_EQUAL( slot_number, 0 );
1451
1452 /* Test changing the slot number. */
1453 psa_set_key_slot_number( &attributes, 42 );
1454 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1455 TEST_EQUAL( slot_number, 42 );
1456
1457 /* Test clearing the slot number. */
1458 psa_clear_key_slot_number( &attributes );
1459 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1460 PSA_ERROR_INVALID_ARGUMENT );
1461
1462 /* Clearing again should have no effect. */
1463 psa_clear_key_slot_number( &attributes );
1464 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1465 PSA_ERROR_INVALID_ARGUMENT );
1466
1467 /* Test that reset clears the slot number. */
1468 psa_set_key_slot_number( &attributes, 42 );
1469 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1470 TEST_EQUAL( slot_number, 42 );
1471 psa_reset_key_attributes( &attributes );
1472 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1473 PSA_ERROR_INVALID_ARGUMENT );
1474}
1475/* END_CASE */
1476
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001477/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001478void import_with_policy( int type_arg,
1479 int usage_arg, int alg_arg,
1480 int expected_status_arg )
1481{
1482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1483 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001484 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001485 psa_key_type_t type = type_arg;
1486 psa_key_usage_t usage = usage_arg;
1487 psa_algorithm_t alg = alg_arg;
1488 psa_status_t expected_status = expected_status_arg;
1489 const uint8_t key_material[16] = {0};
1490 psa_status_t status;
1491
1492 PSA_ASSERT( psa_crypto_init( ) );
1493
1494 psa_set_key_type( &attributes, type );
1495 psa_set_key_usage_flags( &attributes, usage );
1496 psa_set_key_algorithm( &attributes, alg );
1497
1498 status = psa_import_key( &attributes,
1499 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001500 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001501 TEST_EQUAL( status, expected_status );
1502 if( status != PSA_SUCCESS )
1503 goto exit;
1504
Ronald Cron5425a212020-08-04 14:58:35 +02001505 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001506 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1507 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1508 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001509 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001510
Ronald Cron5425a212020-08-04 14:58:35 +02001511 PSA_ASSERT( psa_destroy_key( key ) );
1512 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001513
1514exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001515 /*
1516 * Key attributes may have been returned by psa_get_key_attributes()
1517 * thus reset them as required.
1518 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001519 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001520
1521 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001522 PSA_DONE( );
1523}
1524/* END_CASE */
1525
1526/* BEGIN_CASE */
1527void import_with_data( data_t *data, int type_arg,
1528 int attr_bits_arg,
1529 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001530{
1531 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1532 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001533 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001534 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001535 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001536 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001537 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538
Gilles Peskine8817f612018-12-18 00:18:46 +01001539 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540
Gilles Peskine4747d192019-04-17 15:05:45 +02001541 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001542 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001543
Ronald Cron5425a212020-08-04 14:58:35 +02001544 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001545 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001546 if( status != PSA_SUCCESS )
1547 goto exit;
1548
Ronald Cron5425a212020-08-04 14:58:35 +02001549 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001550 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001551 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001552 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001553 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001554
Ronald Cron5425a212020-08-04 14:58:35 +02001555 PSA_ASSERT( psa_destroy_key( key ) );
1556 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001557
1558exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001559 /*
1560 * Key attributes may have been returned by psa_get_key_attributes()
1561 * thus reset them as required.
1562 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001563 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001564
1565 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001566 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567}
1568/* END_CASE */
1569
1570/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001571void import_large_key( int type_arg, int byte_size_arg,
1572 int expected_status_arg )
1573{
1574 psa_key_type_t type = type_arg;
1575 size_t byte_size = byte_size_arg;
1576 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1577 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001578 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001579 psa_status_t status;
1580 uint8_t *buffer = NULL;
1581 size_t buffer_size = byte_size + 1;
1582 size_t n;
1583
Steven Cooreman69967ce2021-01-18 18:01:08 +01001584 /* Skip the test case if the target running the test cannot
1585 * accomodate large keys due to heap size constraints */
1586 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001587 memset( buffer, 'K', byte_size );
1588
1589 PSA_ASSERT( psa_crypto_init( ) );
1590
1591 /* Try importing the key */
1592 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1593 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001594 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001595 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001596 TEST_EQUAL( status, expected_status );
1597
1598 if( status == PSA_SUCCESS )
1599 {
Ronald Cron5425a212020-08-04 14:58:35 +02001600 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001601 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1602 TEST_EQUAL( psa_get_key_bits( &attributes ),
1603 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001604 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001605 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001606 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001607 for( n = 0; n < byte_size; n++ )
1608 TEST_EQUAL( buffer[n], 'K' );
1609 for( n = byte_size; n < buffer_size; n++ )
1610 TEST_EQUAL( buffer[n], 0 );
1611 }
1612
1613exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001614 /*
1615 * Key attributes may have been returned by psa_get_key_attributes()
1616 * thus reset them as required.
1617 */
1618 psa_reset_key_attributes( &attributes );
1619
Ronald Cron5425a212020-08-04 14:58:35 +02001620 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001621 PSA_DONE( );
1622 mbedtls_free( buffer );
1623}
1624/* END_CASE */
1625
1626/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001627void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1628{
Ronald Cron5425a212020-08-04 14:58:35 +02001629 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001630 size_t bits = bits_arg;
1631 psa_status_t expected_status = expected_status_arg;
1632 psa_status_t status;
1633 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001634 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001635 size_t buffer_size = /* Slight overapproximations */
1636 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001637 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001638 unsigned char *p;
1639 int ret;
1640 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001642
Gilles Peskine8817f612018-12-18 00:18:46 +01001643 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001644 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001645
1646 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1647 bits, keypair ) ) >= 0 );
1648 length = ret;
1649
1650 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001651 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001652 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001653 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001654
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001655 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001656 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001657
1658exit:
1659 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001660 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001661}
1662/* END_CASE */
1663
1664/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001665void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001666 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001667 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001668 int expected_bits,
1669 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001670 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001671 int canonical_input )
1672{
Ronald Cron5425a212020-08-04 14:58:35 +02001673 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001674 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001675 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001676 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001677 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001678 unsigned char *exported = NULL;
1679 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001680 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001681 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001682 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001683 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001684 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001685
Moran Pekercb088e72018-07-17 17:36:59 +03001686 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001687 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001688 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001689 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001690 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001691
Gilles Peskine4747d192019-04-17 15:05:45 +02001692 psa_set_key_usage_flags( &attributes, usage_arg );
1693 psa_set_key_algorithm( &attributes, alg );
1694 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001695
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001696 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001697 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001698
1699 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001700 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001701 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1702 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001703 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001704
1705 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001706 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001707 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001708
1709 /* The exported length must be set by psa_export_key() to a value between 0
1710 * and export_size. On errors, the exported length must be 0. */
1711 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1712 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1713 TEST_ASSERT( exported_length <= export_size );
1714
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001715 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001716 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001717 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001718 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001719 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001720 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001721 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001722
Ronald Cron5425a212020-08-04 14:58:35 +02001723 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001724 goto exit;
1725
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001726 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001727 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001728 else
1729 {
Ronald Cron5425a212020-08-04 14:58:35 +02001730 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001731 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001732 &key2 ) );
1733 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001734 reexported,
1735 export_size,
1736 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001737 ASSERT_COMPARE( exported, exported_length,
1738 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001739 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001740 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001741 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001742
1743destroy:
1744 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001745 PSA_ASSERT( psa_destroy_key( key ) );
1746 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001747
1748exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001749 /*
1750 * Key attributes may have been returned by psa_get_key_attributes()
1751 * thus reset them as required.
1752 */
1753 psa_reset_key_attributes( &got_attributes );
1754
itayzafrir3e02b3b2018-06-12 17:06:52 +03001755 mbedtls_free( exported );
1756 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001757 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001758}
1759/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001760
Moran Pekerf709f4a2018-06-06 17:26:04 +03001761/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001762void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001763 int type_arg,
1764 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001765 int export_size_delta,
1766 int expected_export_status_arg,
1767 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001768{
Ronald Cron5425a212020-08-04 14:58:35 +02001769 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001770 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001771 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001772 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001773 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001774 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001775 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001776 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001778
Gilles Peskine8817f612018-12-18 00:18:46 +01001779 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001780
Gilles Peskine4747d192019-04-17 15:05:45 +02001781 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1782 psa_set_key_algorithm( &attributes, alg );
1783 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001784
1785 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001786 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001787
Gilles Peskine49c25912018-10-29 15:15:31 +01001788 /* Export the public key */
1789 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001790 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001791 exported, export_size,
1792 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001793 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001794 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001795 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001796 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001797 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001798 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001799 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001800 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001801 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001802 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1803 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001804 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001805
1806exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001807 /*
1808 * Key attributes may have been returned by psa_get_key_attributes()
1809 * thus reset them as required.
1810 */
1811 psa_reset_key_attributes( &attributes );
1812
itayzafrir3e02b3b2018-06-12 17:06:52 +03001813 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001814 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001815 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001816}
1817/* END_CASE */
1818
Gilles Peskine20035e32018-02-03 22:44:14 +01001819/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001820void import_and_exercise_key( data_t *data,
1821 int type_arg,
1822 int bits_arg,
1823 int alg_arg )
1824{
Ronald Cron5425a212020-08-04 14:58:35 +02001825 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001826 psa_key_type_t type = type_arg;
1827 size_t bits = bits_arg;
1828 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001829 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001830 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001831 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001832
Gilles Peskine8817f612018-12-18 00:18:46 +01001833 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001834
Gilles Peskine4747d192019-04-17 15:05:45 +02001835 psa_set_key_usage_flags( &attributes, usage );
1836 psa_set_key_algorithm( &attributes, alg );
1837 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001838
1839 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001840 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001841
1842 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001843 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001844 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1845 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001846
1847 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001848 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001849 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001850
Ronald Cron5425a212020-08-04 14:58:35 +02001851 PSA_ASSERT( psa_destroy_key( key ) );
1852 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001853
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001854exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001855 /*
1856 * Key attributes may have been returned by psa_get_key_attributes()
1857 * thus reset them as required.
1858 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001859 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001860
1861 psa_reset_key_attributes( &attributes );
1862 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001863 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001864}
1865/* END_CASE */
1866
1867/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001868void effective_key_attributes( int type_arg, int expected_type_arg,
1869 int bits_arg, int expected_bits_arg,
1870 int usage_arg, int expected_usage_arg,
1871 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001872{
Ronald Cron5425a212020-08-04 14:58:35 +02001873 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001874 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001875 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001876 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001877 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001878 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001879 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001880 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001881 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001883
Gilles Peskine8817f612018-12-18 00:18:46 +01001884 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001885
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001886 psa_set_key_usage_flags( &attributes, usage );
1887 psa_set_key_algorithm( &attributes, alg );
1888 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001889 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001890
Ronald Cron5425a212020-08-04 14:58:35 +02001891 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001892 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001893
Ronald Cron5425a212020-08-04 14:58:35 +02001894 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001895 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1896 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1897 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1898 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001899
1900exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001901 /*
1902 * Key attributes may have been returned by psa_get_key_attributes()
1903 * thus reset them as required.
1904 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001905 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001906
1907 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001908 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001909}
1910/* END_CASE */
1911
1912/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001913void check_key_policy( int type_arg, int bits_arg,
1914 int usage_arg, int alg_arg )
1915{
1916 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1917 usage_arg, usage_arg, alg_arg, alg_arg );
1918 goto exit;
1919}
1920/* END_CASE */
1921
1922/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001923void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001924{
1925 /* Test each valid way of initializing the object, except for `= {0}`, as
1926 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1927 * though it's OK by the C standard. We could test for this, but we'd need
1928 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001929 psa_key_attributes_t func = psa_key_attributes_init( );
1930 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1931 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001932
1933 memset( &zero, 0, sizeof( zero ) );
1934
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001935 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1936 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1937 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001938
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001939 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1940 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1941 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1942
1943 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1944 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1945 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1946
1947 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1948 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1949 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1950
1951 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1952 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1953 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001954}
1955/* END_CASE */
1956
1957/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958void mac_key_policy( int policy_usage,
1959 int policy_alg,
1960 int key_type,
1961 data_t *key_data,
1962 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001963{
Ronald Cron5425a212020-08-04 14:58:35 +02001964 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001965 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001966 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001967 psa_status_t status;
1968 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001969
Gilles Peskine8817f612018-12-18 00:18:46 +01001970 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001971
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001972 psa_set_key_usage_flags( &attributes, policy_usage );
1973 psa_set_key_algorithm( &attributes, policy_alg );
1974 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001975
Gilles Peskine049c7532019-05-15 20:22:09 +02001976 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001977 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001978
Ronald Cron5425a212020-08-04 14:58:35 +02001979 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001980 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001981 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001982 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001984 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001986
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001987 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001988 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001989 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001990 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001991 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001992 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001993 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994
1995exit:
1996 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001997 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001998 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001999}
2000/* END_CASE */
2001
2002/* BEGIN_CASE */
2003void cipher_key_policy( int policy_usage,
2004 int policy_alg,
2005 int key_type,
2006 data_t *key_data,
2007 int exercise_alg )
2008{
Ronald Cron5425a212020-08-04 14:58:35 +02002009 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002011 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002012 psa_status_t status;
2013
Gilles Peskine8817f612018-12-18 00:18:46 +01002014 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002015
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002016 psa_set_key_usage_flags( &attributes, policy_usage );
2017 psa_set_key_algorithm( &attributes, policy_alg );
2018 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019
Gilles Peskine049c7532019-05-15 20:22:09 +02002020 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002021 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022
Ronald Cron5425a212020-08-04 14:58:35 +02002023 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 if( policy_alg == exercise_alg &&
2025 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002026 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002027 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002028 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002029 psa_cipher_abort( &operation );
2030
Ronald Cron5425a212020-08-04 14:58:35 +02002031 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032 if( policy_alg == exercise_alg &&
2033 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002034 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002036 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037
2038exit:
2039 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002040 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002041 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002042}
2043/* END_CASE */
2044
2045/* BEGIN_CASE */
2046void aead_key_policy( int policy_usage,
2047 int policy_alg,
2048 int key_type,
2049 data_t *key_data,
2050 int nonce_length_arg,
2051 int tag_length_arg,
2052 int exercise_alg )
2053{
Ronald Cron5425a212020-08-04 14:58:35 +02002054 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002055 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056 psa_status_t status;
2057 unsigned char nonce[16] = {0};
2058 size_t nonce_length = nonce_length_arg;
2059 unsigned char tag[16];
2060 size_t tag_length = tag_length_arg;
2061 size_t output_length;
2062
2063 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
2064 TEST_ASSERT( tag_length <= sizeof( tag ) );
2065
Gilles Peskine8817f612018-12-18 00:18:46 +01002066 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002067
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002068 psa_set_key_usage_flags( &attributes, policy_usage );
2069 psa_set_key_algorithm( &attributes, policy_alg );
2070 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002071
Gilles Peskine049c7532019-05-15 20:22:09 +02002072 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002073 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002074
Ronald Cron5425a212020-08-04 14:58:35 +02002075 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002076 nonce, nonce_length,
2077 NULL, 0,
2078 NULL, 0,
2079 tag, tag_length,
2080 &output_length );
2081 if( policy_alg == exercise_alg &&
2082 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002083 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002085 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002086
2087 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002088 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002089 nonce, nonce_length,
2090 NULL, 0,
2091 tag, tag_length,
2092 NULL, 0,
2093 &output_length );
2094 if( policy_alg == exercise_alg &&
2095 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002096 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002097 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002098 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099
2100exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002101 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002102 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103}
2104/* END_CASE */
2105
2106/* BEGIN_CASE */
2107void asymmetric_encryption_key_policy( int policy_usage,
2108 int policy_alg,
2109 int key_type,
2110 data_t *key_data,
2111 int exercise_alg )
2112{
Ronald Cron5425a212020-08-04 14:58:35 +02002113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002115 psa_status_t status;
2116 size_t key_bits;
2117 size_t buffer_length;
2118 unsigned char *buffer = NULL;
2119 size_t output_length;
2120
Gilles Peskine8817f612018-12-18 00:18:46 +01002121 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002123 psa_set_key_usage_flags( &attributes, policy_usage );
2124 psa_set_key_algorithm( &attributes, policy_alg );
2125 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002126
Gilles Peskine049c7532019-05-15 20:22:09 +02002127 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002128 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002129
Ronald Cron5425a212020-08-04 14:58:35 +02002130 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2133 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002134 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002135
Ronald Cron5425a212020-08-04 14:58:35 +02002136 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002137 NULL, 0,
2138 NULL, 0,
2139 buffer, buffer_length,
2140 &output_length );
2141 if( policy_alg == exercise_alg &&
2142 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002143 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002144 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002145 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002146
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002147 if( buffer_length != 0 )
2148 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002149 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002150 buffer, buffer_length,
2151 NULL, 0,
2152 buffer, buffer_length,
2153 &output_length );
2154 if( policy_alg == exercise_alg &&
2155 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002156 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002157 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002158 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002159
2160exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002161 /*
2162 * Key attributes may have been returned by psa_get_key_attributes()
2163 * thus reset them as required.
2164 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002165 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002166
2167 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002168 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002169 mbedtls_free( buffer );
2170}
2171/* END_CASE */
2172
2173/* BEGIN_CASE */
2174void asymmetric_signature_key_policy( int policy_usage,
2175 int policy_alg,
2176 int key_type,
2177 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002178 int exercise_alg,
2179 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002180{
Ronald Cron5425a212020-08-04 14:58:35 +02002181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002183 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002184 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2185 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2186 * compatible with the policy and `payload_length_arg` is supposed to be
2187 * a valid input length to sign. If `payload_length_arg <= 0`,
2188 * `exercise_alg` is supposed to be forbidden by the policy. */
2189 int compatible_alg = payload_length_arg > 0;
2190 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002191 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002192 size_t signature_length;
2193
Gilles Peskine8817f612018-12-18 00:18:46 +01002194 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002195
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002196 psa_set_key_usage_flags( &attributes, policy_usage );
2197 psa_set_key_algorithm( &attributes, policy_alg );
2198 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002199
Gilles Peskine049c7532019-05-15 20:22:09 +02002200 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002201 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002202
Ronald Cron5425a212020-08-04 14:58:35 +02002203 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002204 payload, payload_length,
2205 signature, sizeof( signature ),
2206 &signature_length );
2207 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002208 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002209 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002210 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002211
2212 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002213 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002214 payload, payload_length,
2215 signature, sizeof( signature ) );
2216 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002217 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002218 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002219 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002220
2221exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002222 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002223 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002224}
2225/* END_CASE */
2226
Janos Follathba3fab92019-06-11 14:50:16 +01002227/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002228void derive_key_policy( int policy_usage,
2229 int policy_alg,
2230 int key_type,
2231 data_t *key_data,
2232 int exercise_alg )
2233{
Ronald Cron5425a212020-08-04 14:58:35 +02002234 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002236 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002237 psa_status_t status;
2238
Gilles Peskine8817f612018-12-18 00:18:46 +01002239 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002240
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002241 psa_set_key_usage_flags( &attributes, policy_usage );
2242 psa_set_key_algorithm( &attributes, policy_alg );
2243 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002244
Gilles Peskine049c7532019-05-15 20:22:09 +02002245 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002246 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002247
Janos Follathba3fab92019-06-11 14:50:16 +01002248 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2249
2250 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2251 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002252 {
Janos Follathba3fab92019-06-11 14:50:16 +01002253 PSA_ASSERT( psa_key_derivation_input_bytes(
2254 &operation,
2255 PSA_KEY_DERIVATION_INPUT_SEED,
2256 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002257 }
Janos Follathba3fab92019-06-11 14:50:16 +01002258
2259 status = psa_key_derivation_input_key( &operation,
2260 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002261 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002262
Gilles Peskineea0fb492018-07-12 17:17:20 +02002263 if( policy_alg == exercise_alg &&
2264 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002265 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002266 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002267 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002268
2269exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002270 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002271 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002272 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002273}
2274/* END_CASE */
2275
2276/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002277void agreement_key_policy( int policy_usage,
2278 int policy_alg,
2279 int key_type_arg,
2280 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002281 int exercise_alg,
2282 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002283{
Ronald Cron5425a212020-08-04 14:58:35 +02002284 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002286 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002287 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002288 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002289 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002290
Gilles Peskine8817f612018-12-18 00:18:46 +01002291 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002292
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002293 psa_set_key_usage_flags( &attributes, policy_usage );
2294 psa_set_key_algorithm( &attributes, policy_alg );
2295 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002296
Gilles Peskine049c7532019-05-15 20:22:09 +02002297 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002298 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002299
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002300 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002301 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002302
Steven Cooremance48e852020-10-05 16:02:45 +02002303 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002304
2305exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002306 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002307 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002308 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002309}
2310/* END_CASE */
2311
2312/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002313void key_policy_alg2( int key_type_arg, data_t *key_data,
2314 int usage_arg, int alg_arg, int alg2_arg )
2315{
Ronald Cron5425a212020-08-04 14:58:35 +02002316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002317 psa_key_type_t key_type = key_type_arg;
2318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2319 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2320 psa_key_usage_t usage = usage_arg;
2321 psa_algorithm_t alg = alg_arg;
2322 psa_algorithm_t alg2 = alg2_arg;
2323
2324 PSA_ASSERT( psa_crypto_init( ) );
2325
2326 psa_set_key_usage_flags( &attributes, usage );
2327 psa_set_key_algorithm( &attributes, alg );
2328 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2329 psa_set_key_type( &attributes, key_type );
2330 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002331 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002332
Ronald Cron5425a212020-08-04 14:58:35 +02002333 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002334 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2335 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2336 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2337
Ronald Cron5425a212020-08-04 14:58:35 +02002338 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002339 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002340 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002341 goto exit;
2342
2343exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002344 /*
2345 * Key attributes may have been returned by psa_get_key_attributes()
2346 * thus reset them as required.
2347 */
2348 psa_reset_key_attributes( &got_attributes );
2349
Ronald Cron5425a212020-08-04 14:58:35 +02002350 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002351 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002352}
2353/* END_CASE */
2354
2355/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002356void raw_agreement_key_policy( int policy_usage,
2357 int policy_alg,
2358 int key_type_arg,
2359 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002360 int exercise_alg,
2361 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002362{
Ronald Cron5425a212020-08-04 14:58:35 +02002363 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002365 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002366 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002367 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002368 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002369
2370 PSA_ASSERT( psa_crypto_init( ) );
2371
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002372 psa_set_key_usage_flags( &attributes, policy_usage );
2373 psa_set_key_algorithm( &attributes, policy_alg );
2374 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002375
Gilles Peskine049c7532019-05-15 20:22:09 +02002376 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002377 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002378
Ronald Cron5425a212020-08-04 14:58:35 +02002379 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002380
Steven Cooremance48e852020-10-05 16:02:45 +02002381 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002382
2383exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002384 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002385 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002386 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002387}
2388/* END_CASE */
2389
2390/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002391void copy_success( int source_usage_arg,
2392 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002393 int type_arg, data_t *material,
2394 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002395 int target_usage_arg,
2396 int target_alg_arg, int target_alg2_arg,
2397 int expected_usage_arg,
2398 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002399{
Gilles Peskineca25db92019-04-19 11:43:08 +02002400 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2401 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002402 psa_key_usage_t expected_usage = expected_usage_arg;
2403 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002404 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002405 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2406 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002407 uint8_t *export_buffer = NULL;
2408
Gilles Peskine57ab7212019-01-28 13:03:09 +01002409 PSA_ASSERT( psa_crypto_init( ) );
2410
Gilles Peskineca25db92019-04-19 11:43:08 +02002411 /* Prepare the source key. */
2412 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2413 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002414 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002415 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002416 PSA_ASSERT( psa_import_key( &source_attributes,
2417 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002418 &source_key ) );
2419 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002420
Gilles Peskineca25db92019-04-19 11:43:08 +02002421 /* Prepare the target attributes. */
2422 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002423 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002424 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002425 /* Set volatile lifetime to reset the key identifier to 0. */
2426 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2427 }
2428
Gilles Peskineca25db92019-04-19 11:43:08 +02002429 if( target_usage_arg != -1 )
2430 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2431 if( target_alg_arg != -1 )
2432 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002433 if( target_alg2_arg != -1 )
2434 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002435
2436 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002437 PSA_ASSERT( psa_copy_key( source_key,
2438 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002439
2440 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002441 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002442
2443 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002444 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002445 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2446 psa_get_key_type( &target_attributes ) );
2447 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2448 psa_get_key_bits( &target_attributes ) );
2449 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2450 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002451 TEST_EQUAL( expected_alg2,
2452 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002453 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2454 {
2455 size_t length;
2456 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002457 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002458 material->len, &length ) );
2459 ASSERT_COMPARE( material->x, material->len,
2460 export_buffer, length );
2461 }
Ronald Cron5425a212020-08-04 14:58:35 +02002462 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002463 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002464 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002465 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002466
Ronald Cron5425a212020-08-04 14:58:35 +02002467 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468
2469exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002470 /*
2471 * Source and target key attributes may have been returned by
2472 * psa_get_key_attributes() thus reset them as required.
2473 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002474 psa_reset_key_attributes( &source_attributes );
2475 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002476
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002477 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002478 mbedtls_free( export_buffer );
2479}
2480/* END_CASE */
2481
2482/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002483void copy_fail( int source_usage_arg,
2484 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002485 int type_arg, data_t *material,
2486 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002487 int target_usage_arg,
2488 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002489 int expected_status_arg )
2490{
2491 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2492 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002493 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2494 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002495
2496 PSA_ASSERT( psa_crypto_init( ) );
2497
2498 /* Prepare the source key. */
2499 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2500 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002501 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002502 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002503 PSA_ASSERT( psa_import_key( &source_attributes,
2504 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002505 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002506
2507 /* Prepare the target attributes. */
2508 psa_set_key_type( &target_attributes, target_type_arg );
2509 psa_set_key_bits( &target_attributes, target_bits_arg );
2510 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2511 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002512 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002513
2514 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002515 TEST_EQUAL( psa_copy_key( source_key,
2516 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002517 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002518
Ronald Cron5425a212020-08-04 14:58:35 +02002519 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002520
Gilles Peskine4a644642019-05-03 17:14:08 +02002521exit:
2522 psa_reset_key_attributes( &source_attributes );
2523 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002524 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002525}
2526/* END_CASE */
2527
2528/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002529void hash_operation_init( )
2530{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002531 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002532 /* Test each valid way of initializing the object, except for `= {0}`, as
2533 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2534 * though it's OK by the C standard. We could test for this, but we'd need
2535 * to supress the Clang warning for the test. */
2536 psa_hash_operation_t func = psa_hash_operation_init( );
2537 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2538 psa_hash_operation_t zero;
2539
2540 memset( &zero, 0, sizeof( zero ) );
2541
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002542 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002543 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2544 PSA_ERROR_BAD_STATE );
2545 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2546 PSA_ERROR_BAD_STATE );
2547 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2548 PSA_ERROR_BAD_STATE );
2549
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002550 /* A default hash operation should be abortable without error. */
2551 PSA_ASSERT( psa_hash_abort( &func ) );
2552 PSA_ASSERT( psa_hash_abort( &init ) );
2553 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002554}
2555/* END_CASE */
2556
2557/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002558void hash_setup( int alg_arg,
2559 int expected_status_arg )
2560{
2561 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002562 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002563 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002564 psa_status_t status;
2565
Gilles Peskine8817f612018-12-18 00:18:46 +01002566 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002567
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002568 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002569 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002570
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002571 /* Whether setup succeeded or failed, abort must succeed. */
2572 PSA_ASSERT( psa_hash_abort( &operation ) );
2573
2574 /* If setup failed, reproduce the failure, so as to
2575 * test the resulting state of the operation object. */
2576 if( status != PSA_SUCCESS )
2577 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2578
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002579 /* Now the operation object should be reusable. */
2580#if defined(KNOWN_SUPPORTED_HASH_ALG)
2581 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2582 PSA_ASSERT( psa_hash_abort( &operation ) );
2583#endif
2584
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002585exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002586 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002587}
2588/* END_CASE */
2589
2590/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002591void hash_compute_fail( int alg_arg, data_t *input,
2592 int output_size_arg, int expected_status_arg )
2593{
2594 psa_algorithm_t alg = alg_arg;
2595 uint8_t *output = NULL;
2596 size_t output_size = output_size_arg;
2597 size_t output_length = INVALID_EXPORT_LENGTH;
2598 psa_status_t expected_status = expected_status_arg;
2599 psa_status_t status;
2600
2601 ASSERT_ALLOC( output, output_size );
2602
2603 PSA_ASSERT( psa_crypto_init( ) );
2604
2605 status = psa_hash_compute( alg, input->x, input->len,
2606 output, output_size, &output_length );
2607 TEST_EQUAL( status, expected_status );
2608 TEST_ASSERT( output_length <= output_size );
2609
2610exit:
2611 mbedtls_free( output );
2612 PSA_DONE( );
2613}
2614/* END_CASE */
2615
2616/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002617void hash_compare_fail( int alg_arg, data_t *input,
2618 data_t *reference_hash,
2619 int expected_status_arg )
2620{
2621 psa_algorithm_t alg = alg_arg;
2622 psa_status_t expected_status = expected_status_arg;
2623 psa_status_t status;
2624
2625 PSA_ASSERT( psa_crypto_init( ) );
2626
2627 status = psa_hash_compare( alg, input->x, input->len,
2628 reference_hash->x, reference_hash->len );
2629 TEST_EQUAL( status, expected_status );
2630
2631exit:
2632 PSA_DONE( );
2633}
2634/* END_CASE */
2635
2636/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002637void hash_compute_compare( int alg_arg, data_t *input,
2638 data_t *expected_output )
2639{
2640 psa_algorithm_t alg = alg_arg;
2641 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2642 size_t output_length = INVALID_EXPORT_LENGTH;
2643 size_t i;
2644
2645 PSA_ASSERT( psa_crypto_init( ) );
2646
2647 /* Compute with tight buffer */
2648 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002649 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002650 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002651 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002652 ASSERT_COMPARE( output, output_length,
2653 expected_output->x, expected_output->len );
2654
2655 /* Compute with larger buffer */
2656 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2657 output, sizeof( output ),
2658 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002659 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002660 ASSERT_COMPARE( output, output_length,
2661 expected_output->x, expected_output->len );
2662
2663 /* Compare with correct hash */
2664 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2665 output, output_length ) );
2666
2667 /* Compare with trailing garbage */
2668 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2669 output, output_length + 1 ),
2670 PSA_ERROR_INVALID_SIGNATURE );
2671
2672 /* Compare with truncated hash */
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 corrupted value */
2678 for( i = 0; i < output_length; i++ )
2679 {
2680 test_set_step( i );
2681 output[i] ^= 1;
2682 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2683 output, output_length ),
2684 PSA_ERROR_INVALID_SIGNATURE );
2685 output[i] ^= 1;
2686 }
2687
2688exit:
2689 PSA_DONE( );
2690}
2691/* END_CASE */
2692
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002693/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002694void hash_bad_order( )
2695{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002696 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002697 unsigned char input[] = "";
2698 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002699 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002700 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2701 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2702 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002703 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002704 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002705 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002706
Gilles Peskine8817f612018-12-18 00:18:46 +01002707 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002708
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002709 /* Call setup twice in a row. */
2710 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2711 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2712 PSA_ERROR_BAD_STATE );
2713 PSA_ASSERT( psa_hash_abort( &operation ) );
2714
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002715 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002716 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002717 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002718 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002719
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002720 /* Call update after finish. */
2721 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2722 PSA_ASSERT( psa_hash_finish( &operation,
2723 hash, sizeof( hash ), &hash_len ) );
2724 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002725 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002726 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002727
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002728 /* Call verify without calling setup beforehand. */
2729 TEST_EQUAL( psa_hash_verify( &operation,
2730 valid_hash, sizeof( valid_hash ) ),
2731 PSA_ERROR_BAD_STATE );
2732 PSA_ASSERT( psa_hash_abort( &operation ) );
2733
2734 /* Call verify after finish. */
2735 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2736 PSA_ASSERT( psa_hash_finish( &operation,
2737 hash, sizeof( hash ), &hash_len ) );
2738 TEST_EQUAL( psa_hash_verify( &operation,
2739 valid_hash, sizeof( valid_hash ) ),
2740 PSA_ERROR_BAD_STATE );
2741 PSA_ASSERT( psa_hash_abort( &operation ) );
2742
2743 /* Call verify twice in a row. */
2744 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2745 PSA_ASSERT( psa_hash_verify( &operation,
2746 valid_hash, sizeof( valid_hash ) ) );
2747 TEST_EQUAL( psa_hash_verify( &operation,
2748 valid_hash, sizeof( valid_hash ) ),
2749 PSA_ERROR_BAD_STATE );
2750 PSA_ASSERT( psa_hash_abort( &operation ) );
2751
2752 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002753 TEST_EQUAL( psa_hash_finish( &operation,
2754 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002755 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002756 PSA_ASSERT( psa_hash_abort( &operation ) );
2757
2758 /* Call finish twice in a row. */
2759 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2760 PSA_ASSERT( psa_hash_finish( &operation,
2761 hash, sizeof( hash ), &hash_len ) );
2762 TEST_EQUAL( psa_hash_finish( &operation,
2763 hash, sizeof( hash ), &hash_len ),
2764 PSA_ERROR_BAD_STATE );
2765 PSA_ASSERT( psa_hash_abort( &operation ) );
2766
2767 /* Call finish after calling verify. */
2768 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2769 PSA_ASSERT( psa_hash_verify( &operation,
2770 valid_hash, sizeof( valid_hash ) ) );
2771 TEST_EQUAL( psa_hash_finish( &operation,
2772 hash, sizeof( hash ), &hash_len ),
2773 PSA_ERROR_BAD_STATE );
2774 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002775
2776exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002777 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002778}
2779/* END_CASE */
2780
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002781/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002782void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002783{
2784 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002785 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2786 * appended to it */
2787 unsigned char hash[] = {
2788 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2789 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2790 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002791 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002792 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002793
Gilles Peskine8817f612018-12-18 00:18:46 +01002794 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002795
itayzafrir27e69452018-11-01 14:26:34 +02002796 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002797 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002798 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002799 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002800
itayzafrir27e69452018-11-01 14:26:34 +02002801 /* psa_hash_verify with a non-matching hash */
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 + 1, expected_size ),
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 hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002807 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002808 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002809 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002810
itayzafrirec93d302018-10-18 18:01:10 +03002811exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002812 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002813}
2814/* END_CASE */
2815
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002816/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2817void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002818{
2819 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002820 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002821 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002822 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002823 size_t hash_len;
2824
Gilles Peskine8817f612018-12-18 00:18:46 +01002825 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002826
itayzafrir58028322018-10-25 10:22:01 +03002827 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002828 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002829 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002830 hash, expected_size - 1, &hash_len ),
2831 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002832
2833exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002834 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002835}
2836/* END_CASE */
2837
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002838/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2839void hash_clone_source_state( )
2840{
2841 psa_algorithm_t alg = PSA_ALG_SHA_256;
2842 unsigned char hash[PSA_HASH_MAX_SIZE];
2843 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2844 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2845 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2846 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2847 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2848 size_t hash_len;
2849
2850 PSA_ASSERT( psa_crypto_init( ) );
2851 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2852
2853 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2854 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2855 PSA_ASSERT( psa_hash_finish( &op_finished,
2856 hash, sizeof( hash ), &hash_len ) );
2857 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2858 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2859
2860 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2861 PSA_ERROR_BAD_STATE );
2862
2863 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2864 PSA_ASSERT( psa_hash_finish( &op_init,
2865 hash, sizeof( hash ), &hash_len ) );
2866 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2867 PSA_ASSERT( psa_hash_finish( &op_finished,
2868 hash, sizeof( hash ), &hash_len ) );
2869 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2870 PSA_ASSERT( psa_hash_finish( &op_aborted,
2871 hash, sizeof( hash ), &hash_len ) );
2872
2873exit:
2874 psa_hash_abort( &op_source );
2875 psa_hash_abort( &op_init );
2876 psa_hash_abort( &op_setup );
2877 psa_hash_abort( &op_finished );
2878 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002879 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002880}
2881/* END_CASE */
2882
2883/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2884void hash_clone_target_state( )
2885{
2886 psa_algorithm_t alg = PSA_ALG_SHA_256;
2887 unsigned char hash[PSA_HASH_MAX_SIZE];
2888 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2889 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2890 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2891 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2892 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2893 size_t hash_len;
2894
2895 PSA_ASSERT( psa_crypto_init( ) );
2896
2897 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2898 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2899 PSA_ASSERT( psa_hash_finish( &op_finished,
2900 hash, sizeof( hash ), &hash_len ) );
2901 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2902 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2903
2904 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2905 PSA_ASSERT( psa_hash_finish( &op_target,
2906 hash, sizeof( hash ), &hash_len ) );
2907
2908 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2909 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2910 PSA_ERROR_BAD_STATE );
2911 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2912 PSA_ERROR_BAD_STATE );
2913
2914exit:
2915 psa_hash_abort( &op_target );
2916 psa_hash_abort( &op_init );
2917 psa_hash_abort( &op_setup );
2918 psa_hash_abort( &op_finished );
2919 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002920 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002921}
2922/* END_CASE */
2923
itayzafrir58028322018-10-25 10:22:01 +03002924/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002925void mac_operation_init( )
2926{
Jaeden Amero252ef282019-02-15 14:05:35 +00002927 const uint8_t input[1] = { 0 };
2928
Jaeden Amero769ce272019-01-04 11:48:03 +00002929 /* Test each valid way of initializing the object, except for `= {0}`, as
2930 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2931 * though it's OK by the C standard. We could test for this, but we'd need
2932 * to supress the Clang warning for the test. */
2933 psa_mac_operation_t func = psa_mac_operation_init( );
2934 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2935 psa_mac_operation_t zero;
2936
2937 memset( &zero, 0, sizeof( zero ) );
2938
Jaeden Amero252ef282019-02-15 14:05:35 +00002939 /* A freshly-initialized MAC operation should not be usable. */
2940 TEST_EQUAL( psa_mac_update( &func,
2941 input, sizeof( input ) ),
2942 PSA_ERROR_BAD_STATE );
2943 TEST_EQUAL( psa_mac_update( &init,
2944 input, sizeof( input ) ),
2945 PSA_ERROR_BAD_STATE );
2946 TEST_EQUAL( psa_mac_update( &zero,
2947 input, sizeof( input ) ),
2948 PSA_ERROR_BAD_STATE );
2949
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002950 /* A default MAC operation should be abortable without error. */
2951 PSA_ASSERT( psa_mac_abort( &func ) );
2952 PSA_ASSERT( psa_mac_abort( &init ) );
2953 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002954}
2955/* END_CASE */
2956
2957/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002958void mac_setup( int key_type_arg,
2959 data_t *key,
2960 int alg_arg,
2961 int expected_status_arg )
2962{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002963 psa_key_type_t key_type = key_type_arg;
2964 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002965 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002966 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002967 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2968#if defined(KNOWN_SUPPORTED_MAC_ALG)
2969 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2970#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002971
Gilles Peskine8817f612018-12-18 00:18:46 +01002972 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002973
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002974 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2975 &operation, &status ) )
2976 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002977 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002978
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002979 /* The operation object should be reusable. */
2980#if defined(KNOWN_SUPPORTED_MAC_ALG)
2981 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2982 smoke_test_key_data,
2983 sizeof( smoke_test_key_data ),
2984 KNOWN_SUPPORTED_MAC_ALG,
2985 &operation, &status ) )
2986 goto exit;
2987 TEST_EQUAL( status, PSA_SUCCESS );
2988#endif
2989
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002990exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002991 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002992}
2993/* END_CASE */
2994
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002995/* 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 +00002996void mac_bad_order( )
2997{
Ronald Cron5425a212020-08-04 14:58:35 +02002998 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002999 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3000 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003001 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003002 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3003 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3004 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003005 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003006 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3007 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3008 size_t sign_mac_length = 0;
3009 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3010 const uint8_t verify_mac[] = {
3011 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3012 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3013 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3014
3015 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003016 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003017 psa_set_key_algorithm( &attributes, alg );
3018 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003019
Ronald Cron5425a212020-08-04 14:58:35 +02003020 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3021 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003022
Jaeden Amero252ef282019-02-15 14:05:35 +00003023 /* Call update without calling setup beforehand. */
3024 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3025 PSA_ERROR_BAD_STATE );
3026 PSA_ASSERT( psa_mac_abort( &operation ) );
3027
3028 /* Call sign finish without calling setup beforehand. */
3029 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3030 &sign_mac_length),
3031 PSA_ERROR_BAD_STATE );
3032 PSA_ASSERT( psa_mac_abort( &operation ) );
3033
3034 /* Call verify finish without calling setup beforehand. */
3035 TEST_EQUAL( psa_mac_verify_finish( &operation,
3036 verify_mac, sizeof( verify_mac ) ),
3037 PSA_ERROR_BAD_STATE );
3038 PSA_ASSERT( psa_mac_abort( &operation ) );
3039
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003040 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003041 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
3042 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003043 PSA_ERROR_BAD_STATE );
3044 PSA_ASSERT( psa_mac_abort( &operation ) );
3045
Jaeden Amero252ef282019-02-15 14:05:35 +00003046 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003047 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003048 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3049 PSA_ASSERT( psa_mac_sign_finish( &operation,
3050 sign_mac, sizeof( sign_mac ),
3051 &sign_mac_length ) );
3052 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3053 PSA_ERROR_BAD_STATE );
3054 PSA_ASSERT( psa_mac_abort( &operation ) );
3055
3056 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003057 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003058 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3059 PSA_ASSERT( psa_mac_verify_finish( &operation,
3060 verify_mac, sizeof( verify_mac ) ) );
3061 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3062 PSA_ERROR_BAD_STATE );
3063 PSA_ASSERT( psa_mac_abort( &operation ) );
3064
3065 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003066 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003067 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3068 PSA_ASSERT( psa_mac_sign_finish( &operation,
3069 sign_mac, sizeof( sign_mac ),
3070 &sign_mac_length ) );
3071 TEST_EQUAL( psa_mac_sign_finish( &operation,
3072 sign_mac, sizeof( sign_mac ),
3073 &sign_mac_length ),
3074 PSA_ERROR_BAD_STATE );
3075 PSA_ASSERT( psa_mac_abort( &operation ) );
3076
3077 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003078 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003079 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3080 PSA_ASSERT( psa_mac_verify_finish( &operation,
3081 verify_mac, sizeof( verify_mac ) ) );
3082 TEST_EQUAL( psa_mac_verify_finish( &operation,
3083 verify_mac, sizeof( verify_mac ) ),
3084 PSA_ERROR_BAD_STATE );
3085 PSA_ASSERT( psa_mac_abort( &operation ) );
3086
3087 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003088 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003089 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3090 TEST_EQUAL( psa_mac_verify_finish( &operation,
3091 verify_mac, sizeof( verify_mac ) ),
3092 PSA_ERROR_BAD_STATE );
3093 PSA_ASSERT( psa_mac_abort( &operation ) );
3094
3095 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003096 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003097 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3098 TEST_EQUAL( psa_mac_sign_finish( &operation,
3099 sign_mac, sizeof( sign_mac ),
3100 &sign_mac_length ),
3101 PSA_ERROR_BAD_STATE );
3102 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003103
Ronald Cron5425a212020-08-04 14:58:35 +02003104 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003105
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003106exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003107 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003108}
3109/* END_CASE */
3110
3111/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003112void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003113 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003114 int alg_arg,
3115 data_t *input,
3116 data_t *expected_mac )
3117{
Ronald Cron5425a212020-08-04 14:58:35 +02003118 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003119 psa_key_type_t key_type = key_type_arg;
3120 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003121 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003123 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003124 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003125 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003126 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003127 const size_t output_sizes_to_test[] = {
3128 0,
3129 1,
3130 expected_mac->len - 1,
3131 expected_mac->len,
3132 expected_mac->len + 1,
3133 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003134
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003135 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003136 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003137 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003138
Gilles Peskine8817f612018-12-18 00:18:46 +01003139 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003140
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003142 psa_set_key_algorithm( &attributes, alg );
3143 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003144
Ronald Cron5425a212020-08-04 14:58:35 +02003145 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3146 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003147
Gilles Peskine8b356b52020-08-25 23:44:59 +02003148 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3149 {
3150 const size_t output_size = output_sizes_to_test[i];
3151 psa_status_t expected_status =
3152 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3153 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003154
Gilles Peskine8b356b52020-08-25 23:44:59 +02003155 test_set_step( output_size );
3156 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003157
Gilles Peskine8b356b52020-08-25 23:44:59 +02003158 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003159 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003160 PSA_ASSERT( psa_mac_update( &operation,
3161 input->x, input->len ) );
3162 TEST_EQUAL( psa_mac_sign_finish( &operation,
3163 actual_mac, output_size,
3164 &mac_length ),
3165 expected_status );
3166 PSA_ASSERT( psa_mac_abort( &operation ) );
3167
3168 if( expected_status == PSA_SUCCESS )
3169 {
3170 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3171 actual_mac, mac_length );
3172 }
3173 mbedtls_free( actual_mac );
3174 actual_mac = NULL;
3175 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003176
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003177exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003178 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003179 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003180 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003181 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003182}
3183/* END_CASE */
3184
3185/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003186void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003187 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003188 int alg_arg,
3189 data_t *input,
3190 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003191{
Ronald Cron5425a212020-08-04 14:58:35 +02003192 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003193 psa_key_type_t key_type = key_type_arg;
3194 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003195 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003197 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003198
Gilles Peskine69c12672018-06-28 00:07:19 +02003199 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3200
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003202
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003203 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003204 psa_set_key_algorithm( &attributes, alg );
3205 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003206
Ronald Cron5425a212020-08-04 14:58:35 +02003207 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3208 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003209
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003210 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003211 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003212 PSA_ASSERT( psa_mac_update( &operation,
3213 input->x, input->len ) );
3214 PSA_ASSERT( psa_mac_verify_finish( &operation,
3215 expected_mac->x,
3216 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003217
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003218 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003219 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003220 PSA_ASSERT( psa_mac_update( &operation,
3221 input->x, input->len ) );
3222 TEST_EQUAL( psa_mac_verify_finish( &operation,
3223 expected_mac->x,
3224 expected_mac->len - 1 ),
3225 PSA_ERROR_INVALID_SIGNATURE );
3226
3227 /* Test a MAC that's too long. */
3228 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3229 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003230 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003231 PSA_ASSERT( psa_mac_update( &operation,
3232 input->x, input->len ) );
3233 TEST_EQUAL( psa_mac_verify_finish( &operation,
3234 perturbed_mac,
3235 expected_mac->len + 1 ),
3236 PSA_ERROR_INVALID_SIGNATURE );
3237
3238 /* Test changing one byte. */
3239 for( size_t i = 0; i < expected_mac->len; i++ )
3240 {
3241 test_set_step( i );
3242 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003243 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003244 PSA_ASSERT( psa_mac_update( &operation,
3245 input->x, input->len ) );
3246 TEST_EQUAL( psa_mac_verify_finish( &operation,
3247 perturbed_mac,
3248 expected_mac->len ),
3249 PSA_ERROR_INVALID_SIGNATURE );
3250 perturbed_mac[i] ^= 1;
3251 }
3252
Gilles Peskine8c9def32018-02-08 10:02:12 +01003253exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003254 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003255 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003256 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003257 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003258}
3259/* END_CASE */
3260
3261/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003262void cipher_operation_init( )
3263{
Jaeden Ameroab439972019-02-15 14:12:05 +00003264 const uint8_t input[1] = { 0 };
3265 unsigned char output[1] = { 0 };
3266 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003267 /* Test each valid way of initializing the object, except for `= {0}`, as
3268 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3269 * though it's OK by the C standard. We could test for this, but we'd need
3270 * to supress the Clang warning for the test. */
3271 psa_cipher_operation_t func = psa_cipher_operation_init( );
3272 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3273 psa_cipher_operation_t zero;
3274
3275 memset( &zero, 0, sizeof( zero ) );
3276
Jaeden Ameroab439972019-02-15 14:12:05 +00003277 /* A freshly-initialized cipher operation should not be usable. */
3278 TEST_EQUAL( psa_cipher_update( &func,
3279 input, sizeof( input ),
3280 output, sizeof( output ),
3281 &output_length ),
3282 PSA_ERROR_BAD_STATE );
3283 TEST_EQUAL( psa_cipher_update( &init,
3284 input, sizeof( input ),
3285 output, sizeof( output ),
3286 &output_length ),
3287 PSA_ERROR_BAD_STATE );
3288 TEST_EQUAL( psa_cipher_update( &zero,
3289 input, sizeof( input ),
3290 output, sizeof( output ),
3291 &output_length ),
3292 PSA_ERROR_BAD_STATE );
3293
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003294 /* A default cipher operation should be abortable without error. */
3295 PSA_ASSERT( psa_cipher_abort( &func ) );
3296 PSA_ASSERT( psa_cipher_abort( &init ) );
3297 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003298}
3299/* END_CASE */
3300
3301/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003302void cipher_setup( int key_type_arg,
3303 data_t *key,
3304 int alg_arg,
3305 int expected_status_arg )
3306{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003307 psa_key_type_t key_type = key_type_arg;
3308 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003309 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003310 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003311 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003312#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003313 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3314#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003315
Gilles Peskine8817f612018-12-18 00:18:46 +01003316 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003317
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003318 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3319 &operation, &status ) )
3320 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003321 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003322
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003323 /* The operation object should be reusable. */
3324#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3325 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3326 smoke_test_key_data,
3327 sizeof( smoke_test_key_data ),
3328 KNOWN_SUPPORTED_CIPHER_ALG,
3329 &operation, &status ) )
3330 goto exit;
3331 TEST_EQUAL( status, PSA_SUCCESS );
3332#endif
3333
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003334exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003335 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003336 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003337}
3338/* END_CASE */
3339
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003340/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003341void cipher_bad_order( )
3342{
Ronald Cron5425a212020-08-04 14:58:35 +02003343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003344 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3345 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003347 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003348 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003349 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003350 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3351 0xaa, 0xaa, 0xaa, 0xaa };
3352 const uint8_t text[] = {
3353 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3354 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003355 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003356 size_t length = 0;
3357
3358 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003359 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3360 psa_set_key_algorithm( &attributes, alg );
3361 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003362 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3363 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003364
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003365 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003366 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3367 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003368 PSA_ERROR_BAD_STATE );
3369 PSA_ASSERT( psa_cipher_abort( &operation ) );
3370
3371 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003372 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3373 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003374 PSA_ERROR_BAD_STATE );
3375 PSA_ASSERT( psa_cipher_abort( &operation ) );
3376
Jaeden Ameroab439972019-02-15 14:12:05 +00003377 /* Generate an IV without calling setup beforehand. */
3378 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3379 buffer, sizeof( buffer ),
3380 &length ),
3381 PSA_ERROR_BAD_STATE );
3382 PSA_ASSERT( psa_cipher_abort( &operation ) );
3383
3384 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003385 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003386 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3387 buffer, sizeof( buffer ),
3388 &length ) );
3389 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3390 buffer, sizeof( buffer ),
3391 &length ),
3392 PSA_ERROR_BAD_STATE );
3393 PSA_ASSERT( psa_cipher_abort( &operation ) );
3394
3395 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003396 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003397 PSA_ASSERT( psa_cipher_set_iv( &operation,
3398 iv, sizeof( iv ) ) );
3399 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3400 buffer, sizeof( buffer ),
3401 &length ),
3402 PSA_ERROR_BAD_STATE );
3403 PSA_ASSERT( psa_cipher_abort( &operation ) );
3404
3405 /* Set an IV without calling setup beforehand. */
3406 TEST_EQUAL( psa_cipher_set_iv( &operation,
3407 iv, sizeof( iv ) ),
3408 PSA_ERROR_BAD_STATE );
3409 PSA_ASSERT( psa_cipher_abort( &operation ) );
3410
3411 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003412 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003413 PSA_ASSERT( psa_cipher_set_iv( &operation,
3414 iv, sizeof( iv ) ) );
3415 TEST_EQUAL( psa_cipher_set_iv( &operation,
3416 iv, sizeof( iv ) ),
3417 PSA_ERROR_BAD_STATE );
3418 PSA_ASSERT( psa_cipher_abort( &operation ) );
3419
3420 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003421 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003422 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3423 buffer, sizeof( buffer ),
3424 &length ) );
3425 TEST_EQUAL( psa_cipher_set_iv( &operation,
3426 iv, sizeof( iv ) ),
3427 PSA_ERROR_BAD_STATE );
3428 PSA_ASSERT( psa_cipher_abort( &operation ) );
3429
3430 /* Call update without calling setup beforehand. */
3431 TEST_EQUAL( psa_cipher_update( &operation,
3432 text, sizeof( text ),
3433 buffer, sizeof( buffer ),
3434 &length ),
3435 PSA_ERROR_BAD_STATE );
3436 PSA_ASSERT( psa_cipher_abort( &operation ) );
3437
3438 /* Call update without an IV where an IV is required. */
3439 TEST_EQUAL( psa_cipher_update( &operation,
3440 text, sizeof( text ),
3441 buffer, sizeof( buffer ),
3442 &length ),
3443 PSA_ERROR_BAD_STATE );
3444 PSA_ASSERT( psa_cipher_abort( &operation ) );
3445
3446 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003447 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003448 PSA_ASSERT( psa_cipher_set_iv( &operation,
3449 iv, sizeof( iv ) ) );
3450 PSA_ASSERT( psa_cipher_finish( &operation,
3451 buffer, sizeof( buffer ), &length ) );
3452 TEST_EQUAL( psa_cipher_update( &operation,
3453 text, sizeof( text ),
3454 buffer, sizeof( buffer ),
3455 &length ),
3456 PSA_ERROR_BAD_STATE );
3457 PSA_ASSERT( psa_cipher_abort( &operation ) );
3458
3459 /* Call finish without calling setup beforehand. */
3460 TEST_EQUAL( psa_cipher_finish( &operation,
3461 buffer, sizeof( buffer ), &length ),
3462 PSA_ERROR_BAD_STATE );
3463 PSA_ASSERT( psa_cipher_abort( &operation ) );
3464
3465 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003466 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003467 /* Not calling update means we are encrypting an empty buffer, which is OK
3468 * for cipher modes with padding. */
3469 TEST_EQUAL( psa_cipher_finish( &operation,
3470 buffer, sizeof( buffer ), &length ),
3471 PSA_ERROR_BAD_STATE );
3472 PSA_ASSERT( psa_cipher_abort( &operation ) );
3473
3474 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003475 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003476 PSA_ASSERT( psa_cipher_set_iv( &operation,
3477 iv, sizeof( iv ) ) );
3478 PSA_ASSERT( psa_cipher_finish( &operation,
3479 buffer, sizeof( buffer ), &length ) );
3480 TEST_EQUAL( psa_cipher_finish( &operation,
3481 buffer, sizeof( buffer ), &length ),
3482 PSA_ERROR_BAD_STATE );
3483 PSA_ASSERT( psa_cipher_abort( &operation ) );
3484
Ronald Cron5425a212020-08-04 14:58:35 +02003485 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003486
Jaeden Ameroab439972019-02-15 14:12:05 +00003487exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003488 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003489 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003490}
3491/* END_CASE */
3492
3493/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003494void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003495 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003496 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003497 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003498{
Ronald Cron5425a212020-08-04 14:58:35 +02003499 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003500 psa_status_t status;
3501 psa_key_type_t key_type = key_type_arg;
3502 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003503 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003504 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003505 size_t output_buffer_size = 0;
3506 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003507 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003508 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003509 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003510
Gilles Peskine8817f612018-12-18 00:18:46 +01003511 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003512
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003513 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3514 psa_set_key_algorithm( &attributes, alg );
3515 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003516
Ronald Cron5425a212020-08-04 14:58:35 +02003517 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3518 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003519
Ronald Cron5425a212020-08-04 14:58:35 +02003520 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003521
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003522 if( iv->len > 0 )
3523 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003524 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003525 }
3526
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003527 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003528 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003529 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003530
Gilles Peskine8817f612018-12-18 00:18:46 +01003531 PSA_ASSERT( psa_cipher_update( &operation,
3532 input->x, input->len,
3533 output, output_buffer_size,
3534 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003535 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003536 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003537 output + total_output_length,
3538 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003539 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003540 total_output_length += function_output_length;
3541
Gilles Peskinefe11b722018-12-18 00:24:04 +01003542 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003543 if( expected_status == PSA_SUCCESS )
3544 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003545 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003546 ASSERT_COMPARE( expected_output->x, expected_output->len,
3547 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003548 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003549
Gilles Peskine50e586b2018-06-08 14:28:46 +02003550exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003551 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003552 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003553 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003554 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003555}
3556/* END_CASE */
3557
3558/* BEGIN_CASE */
3559void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003560 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003561 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003562 int first_part_size_arg,
3563 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003564 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003565{
Ronald Cron5425a212020-08-04 14:58:35 +02003566 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003567 psa_key_type_t key_type = key_type_arg;
3568 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003569 size_t first_part_size = first_part_size_arg;
3570 size_t output1_length = output1_length_arg;
3571 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003572 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003573 size_t output_buffer_size = 0;
3574 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003575 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003576 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003577 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003578
Gilles Peskine8817f612018-12-18 00:18:46 +01003579 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003580
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003581 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3582 psa_set_key_algorithm( &attributes, alg );
3583 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003584
Ronald Cron5425a212020-08-04 14:58:35 +02003585 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3586 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003587
Ronald Cron5425a212020-08-04 14:58:35 +02003588 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003589
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003590 if( iv->len > 0 )
3591 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003592 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003593 }
3594
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003595 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003596 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003597 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003598
Gilles Peskinee0866522019-02-19 19:44:00 +01003599 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003600 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3601 output, output_buffer_size,
3602 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003603 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003604 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 PSA_ASSERT( psa_cipher_update( &operation,
3606 input->x + first_part_size,
3607 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003608 output + total_output_length,
3609 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003610 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003611 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003612 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003614 output + total_output_length,
3615 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003616 &function_output_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_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003619
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003620 ASSERT_COMPARE( expected_output->x, expected_output->len,
3621 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003622
3623exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003624 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003625 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003626 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003627 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003628}
3629/* END_CASE */
3630
3631/* BEGIN_CASE */
3632void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003633 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003634 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003635 int first_part_size_arg,
3636 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003637 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003638{
Ronald Cron5425a212020-08-04 14:58:35 +02003639 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003640 psa_key_type_t key_type = key_type_arg;
3641 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003642 size_t first_part_size = first_part_size_arg;
3643 size_t output1_length = output1_length_arg;
3644 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003645 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003646 size_t output_buffer_size = 0;
3647 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003648 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003649 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003650 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003651
Gilles Peskine8817f612018-12-18 00:18:46 +01003652 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003653
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003654 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3655 psa_set_key_algorithm( &attributes, alg );
3656 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003657
Ronald Cron5425a212020-08-04 14:58:35 +02003658 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3659 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003660
Ronald Cron5425a212020-08-04 14:58:35 +02003661 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003662
Steven Cooreman177deba2020-09-07 17:14:14 +02003663 if( iv->len > 0 )
3664 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003665 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003666 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003667
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003668 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003669 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003670 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003671
Gilles Peskinee0866522019-02-19 19:44:00 +01003672 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003673 PSA_ASSERT( psa_cipher_update( &operation,
3674 input->x, first_part_size,
3675 output, output_buffer_size,
3676 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003677 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003678 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003679 PSA_ASSERT( psa_cipher_update( &operation,
3680 input->x + first_part_size,
3681 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003682 output + total_output_length,
3683 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003684 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003685 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003686 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003687 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003688 output + total_output_length,
3689 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003690 &function_output_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_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003693
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003694 ASSERT_COMPARE( expected_output->x, expected_output->len,
3695 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003696
3697exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003698 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003699 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003701 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003702}
3703/* END_CASE */
3704
Gilles Peskine50e586b2018-06-08 14:28:46 +02003705/* BEGIN_CASE */
3706void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003707 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003708 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003709 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003710{
Ronald Cron5425a212020-08-04 14:58:35 +02003711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003712 psa_status_t status;
3713 psa_key_type_t key_type = key_type_arg;
3714 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003715 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003716 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003717 size_t output_buffer_size = 0;
3718 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003719 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003720 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003722
Gilles Peskine8817f612018-12-18 00:18:46 +01003723 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003724
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003725 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3726 psa_set_key_algorithm( &attributes, alg );
3727 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003728
Ronald Cron5425a212020-08-04 14:58:35 +02003729 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3730 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003731
Ronald Cron5425a212020-08-04 14:58:35 +02003732 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003733
Steven Cooreman177deba2020-09-07 17:14:14 +02003734 if( iv->len > 0 )
3735 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003736 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003737 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003738
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003739 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003740 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003741 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003742
Gilles Peskine8817f612018-12-18 00:18:46 +01003743 PSA_ASSERT( psa_cipher_update( &operation,
3744 input->x, input->len,
3745 output, output_buffer_size,
3746 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003747 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003748 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003749 output + total_output_length,
3750 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003751 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003752 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003753 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003754
3755 if( expected_status == PSA_SUCCESS )
3756 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003757 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003758 ASSERT_COMPARE( expected_output->x, expected_output->len,
3759 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003760 }
3761
Gilles Peskine50e586b2018-06-08 14:28:46 +02003762exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003763 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003764 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003765 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003766 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003767}
3768/* END_CASE */
3769
Gilles Peskine50e586b2018-06-08 14:28:46 +02003770/* BEGIN_CASE */
3771void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003772 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003773 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003774{
Ronald Cron5425a212020-08-04 14:58:35 +02003775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003776 psa_key_type_t key_type = key_type_arg;
3777 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003778 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003779 size_t iv_size = 16;
3780 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003781 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003782 size_t output1_size = 0;
3783 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003784 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003785 size_t output2_size = 0;
3786 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003787 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003788 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3789 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003791
Gilles Peskine8817f612018-12-18 00:18:46 +01003792 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003793
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003794 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3795 psa_set_key_algorithm( &attributes, alg );
3796 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003797
Ronald Cron5425a212020-08-04 14:58:35 +02003798 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3799 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003800
Ronald Cron5425a212020-08-04 14:58:35 +02003801 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3802 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003803
Steven Cooreman177deba2020-09-07 17:14:14 +02003804 if( alg != PSA_ALG_ECB_NO_PADDING )
3805 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003806 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3807 iv, iv_size,
3808 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003809 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003810 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003811 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003812 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003813
Gilles Peskine8817f612018-12-18 00:18:46 +01003814 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3815 output1, output1_size,
3816 &output1_length ) );
3817 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003818 output1 + output1_length,
3819 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003820 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003821
Gilles Peskine048b7f02018-06-08 14:20:49 +02003822 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003823
Gilles Peskine8817f612018-12-18 00:18:46 +01003824 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003825
3826 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003827 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003828
Steven Cooreman177deba2020-09-07 17:14:14 +02003829 if( iv_length > 0 )
3830 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003831 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3832 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003833 }
3834
Gilles Peskine8817f612018-12-18 00:18:46 +01003835 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3836 output2, output2_size,
3837 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003838 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003839 PSA_ASSERT( psa_cipher_finish( &operation2,
3840 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003841 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003842 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003843
Gilles Peskine048b7f02018-06-08 14:20:49 +02003844 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003845
Gilles Peskine8817f612018-12-18 00:18:46 +01003846 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003847
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003848 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003849
3850exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003851 psa_cipher_abort( &operation1 );
3852 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003853 mbedtls_free( output1 );
3854 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003855 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003856 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003857}
3858/* END_CASE */
3859
3860/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003861void cipher_verify_output_multipart( int alg_arg,
3862 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003863 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003864 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003865 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003866{
Ronald Cron5425a212020-08-04 14:58:35 +02003867 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003868 psa_key_type_t key_type = key_type_arg;
3869 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003870 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003871 unsigned char iv[16] = {0};
3872 size_t iv_size = 16;
3873 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003874 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003875 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003876 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003877 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003878 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003879 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003880 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003881 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3882 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003883 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003884
Gilles Peskine8817f612018-12-18 00:18:46 +01003885 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003886
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003887 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3888 psa_set_key_algorithm( &attributes, alg );
3889 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003890
Ronald Cron5425a212020-08-04 14:58:35 +02003891 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3892 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003893
Ronald Cron5425a212020-08-04 14:58:35 +02003894 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3895 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003896
Steven Cooreman177deba2020-09-07 17:14:14 +02003897 if( alg != PSA_ALG_ECB_NO_PADDING )
3898 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003899 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3900 iv, iv_size,
3901 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003902 }
3903
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003904 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003905 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003906 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003907
Gilles Peskinee0866522019-02-19 19:44:00 +01003908 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003909
Gilles Peskine8817f612018-12-18 00:18:46 +01003910 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3911 output1, output1_buffer_size,
3912 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003913 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003914
Gilles Peskine8817f612018-12-18 00:18:46 +01003915 PSA_ASSERT( psa_cipher_update( &operation1,
3916 input->x + first_part_size,
3917 input->len - first_part_size,
3918 output1, output1_buffer_size,
3919 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003920 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003921
Gilles Peskine8817f612018-12-18 00:18:46 +01003922 PSA_ASSERT( psa_cipher_finish( &operation1,
3923 output1 + output1_length,
3924 output1_buffer_size - output1_length,
3925 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003926 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003927
Gilles Peskine8817f612018-12-18 00:18:46 +01003928 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003929
Gilles Peskine048b7f02018-06-08 14:20:49 +02003930 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003931 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003932
Steven Cooreman177deba2020-09-07 17:14:14 +02003933 if( iv_length > 0 )
3934 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003935 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3936 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003937 }
Moran Pekerded84402018-06-06 16:36:50 +03003938
Gilles Peskine8817f612018-12-18 00:18:46 +01003939 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3940 output2, output2_buffer_size,
3941 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003942 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003943
Gilles Peskine8817f612018-12-18 00:18:46 +01003944 PSA_ASSERT( psa_cipher_update( &operation2,
3945 output1 + first_part_size,
3946 output1_length - first_part_size,
3947 output2, output2_buffer_size,
3948 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003949 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003950
Gilles Peskine8817f612018-12-18 00:18:46 +01003951 PSA_ASSERT( psa_cipher_finish( &operation2,
3952 output2 + output2_length,
3953 output2_buffer_size - output2_length,
3954 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003955 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003956
Gilles Peskine8817f612018-12-18 00:18:46 +01003957 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003958
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003959 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003960
3961exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003962 psa_cipher_abort( &operation1 );
3963 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003964 mbedtls_free( output1 );
3965 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003966 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003967 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003968}
3969/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003970
Gilles Peskine20035e32018-02-03 22:44:14 +01003971/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003972void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003973 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003974 data_t *nonce,
3975 data_t *additional_data,
3976 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003977 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003978{
Ronald Cron5425a212020-08-04 14:58:35 +02003979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003980 psa_key_type_t key_type = key_type_arg;
3981 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003982 unsigned char *output_data = NULL;
3983 size_t output_size = 0;
3984 size_t output_length = 0;
3985 unsigned char *output_data2 = NULL;
3986 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003987 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003988 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003990
Gilles Peskine4abf7412018-06-18 16:35:34 +02003991 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003992 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3993 * should be exact. */
3994 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3995 TEST_EQUAL( output_size,
3996 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003997 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003998
Gilles Peskine8817f612018-12-18 00:18:46 +01003999 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004000
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004001 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4002 psa_set_key_algorithm( &attributes, alg );
4003 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004004
Gilles Peskine049c7532019-05-15 20:22:09 +02004005 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004006 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004007
Ronald Cron5425a212020-08-04 14:58:35 +02004008 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004009 nonce->x, nonce->len,
4010 additional_data->x,
4011 additional_data->len,
4012 input_data->x, input_data->len,
4013 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004014 &output_length ),
4015 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004016
4017 if( PSA_SUCCESS == expected_result )
4018 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004019 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004020
Gilles Peskine003a4a92019-05-14 16:09:40 +02004021 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4022 * should be exact. */
4023 TEST_EQUAL( input_data->len,
4024 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
4025
Ronald Cron5425a212020-08-04 14:58:35 +02004026 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004027 nonce->x, nonce->len,
4028 additional_data->x,
4029 additional_data->len,
4030 output_data, output_length,
4031 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004032 &output_length2 ),
4033 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004034
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004035 ASSERT_COMPARE( input_data->x, input_data->len,
4036 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004037 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004038
Gilles Peskinea1cac842018-06-11 19:33:02 +02004039exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004040 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004041 mbedtls_free( output_data );
4042 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004043 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004044}
4045/* END_CASE */
4046
4047/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004048void aead_encrypt( int key_type_arg, data_t *key_data,
4049 int alg_arg,
4050 data_t *nonce,
4051 data_t *additional_data,
4052 data_t *input_data,
4053 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004054{
Ronald Cron5425a212020-08-04 14:58:35 +02004055 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004056 psa_key_type_t key_type = key_type_arg;
4057 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004058 unsigned char *output_data = NULL;
4059 size_t output_size = 0;
4060 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004061 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004063
Gilles Peskine4abf7412018-06-18 16:35:34 +02004064 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004065 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4066 * should be exact. */
4067 TEST_EQUAL( output_size,
4068 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004069 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004070
Gilles Peskine8817f612018-12-18 00:18:46 +01004071 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004072
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004073 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4074 psa_set_key_algorithm( &attributes, alg );
4075 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004076
Gilles Peskine049c7532019-05-15 20:22:09 +02004077 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004078 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004079
Ronald Cron5425a212020-08-04 14:58:35 +02004080 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004081 nonce->x, nonce->len,
4082 additional_data->x, additional_data->len,
4083 input_data->x, input_data->len,
4084 output_data, output_size,
4085 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004086
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004087 ASSERT_COMPARE( expected_result->x, expected_result->len,
4088 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004089
Gilles Peskinea1cac842018-06-11 19:33:02 +02004090exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004091 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004092 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004093 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004094}
4095/* END_CASE */
4096
4097/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004098void aead_decrypt( int key_type_arg, data_t *key_data,
4099 int alg_arg,
4100 data_t *nonce,
4101 data_t *additional_data,
4102 data_t *input_data,
4103 data_t *expected_data,
4104 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004105{
Ronald Cron5425a212020-08-04 14:58:35 +02004106 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004107 psa_key_type_t key_type = key_type_arg;
4108 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004109 unsigned char *output_data = NULL;
4110 size_t output_size = 0;
4111 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004112 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004114 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004115
Gilles Peskine003a4a92019-05-14 16:09:40 +02004116 output_size = input_data->len - tag_length;
4117 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4118 * should be exact. */
4119 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4120 TEST_EQUAL( output_size,
4121 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004122 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004123
Gilles Peskine8817f612018-12-18 00:18:46 +01004124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004126 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4127 psa_set_key_algorithm( &attributes, alg );
4128 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004129
Gilles Peskine049c7532019-05-15 20:22:09 +02004130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004131 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004132
Ronald Cron5425a212020-08-04 14:58:35 +02004133 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004134 nonce->x, nonce->len,
4135 additional_data->x,
4136 additional_data->len,
4137 input_data->x, input_data->len,
4138 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004139 &output_length ),
4140 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004141
Gilles Peskine2d277862018-06-18 15:41:12 +02004142 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004143 ASSERT_COMPARE( expected_data->x, expected_data->len,
4144 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004145
Gilles Peskinea1cac842018-06-11 19:33:02 +02004146exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004147 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004148 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004149 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004150}
4151/* END_CASE */
4152
4153/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004154void signature_size( int type_arg,
4155 int bits,
4156 int alg_arg,
4157 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004158{
4159 psa_key_type_t type = type_arg;
4160 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004161 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004162
Gilles Peskinefe11b722018-12-18 00:24:04 +01004163 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004164#if defined(MBEDTLS_TEST_DEPRECATED)
4165 TEST_EQUAL( actual_size,
4166 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4167#endif /* MBEDTLS_TEST_DEPRECATED */
4168
Gilles Peskinee59236f2018-01-27 23:32:46 +01004169exit:
4170 ;
4171}
4172/* END_CASE */
4173
4174/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004175void sign_deterministic( int key_type_arg, data_t *key_data,
4176 int alg_arg, data_t *input_data,
4177 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004178{
Ronald Cron5425a212020-08-04 14:58:35 +02004179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004180 psa_key_type_t key_type = key_type_arg;
4181 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004182 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004183 unsigned char *signature = NULL;
4184 size_t signature_size;
4185 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004186 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004187
Gilles Peskine8817f612018-12-18 00:18:46 +01004188 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004189
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004190 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004191 psa_set_key_algorithm( &attributes, alg );
4192 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004193
Gilles Peskine049c7532019-05-15 20:22:09 +02004194 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004195 &key ) );
4196 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004197 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004198
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004199 /* Allocate a buffer which has the size advertized by the
4200 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004201 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004202 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004203 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004204 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004205 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004206
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004207 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004208 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004209 input_data->x, input_data->len,
4210 signature, signature_size,
4211 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004212 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004213 ASSERT_COMPARE( output_data->x, output_data->len,
4214 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004215
Gilles Peskine0627f982019-11-26 19:12:16 +01004216#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004217 memset( signature, 0, signature_size );
4218 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004219 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004220 input_data->x, input_data->len,
4221 signature, signature_size,
4222 &signature_length ) );
4223 ASSERT_COMPARE( output_data->x, output_data->len,
4224 signature, signature_length );
4225#endif /* MBEDTLS_TEST_DEPRECATED */
4226
Gilles Peskine20035e32018-02-03 22:44:14 +01004227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004228 /*
4229 * Key attributes may have been returned by psa_get_key_attributes()
4230 * thus reset them as required.
4231 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004233
Ronald Cron5425a212020-08-04 14:58:35 +02004234 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004235 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004236 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004237}
4238/* END_CASE */
4239
4240/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004241void sign_fail( int key_type_arg, data_t *key_data,
4242 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004243 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004244{
Ronald Cron5425a212020-08-04 14:58:35 +02004245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004246 psa_key_type_t key_type = key_type_arg;
4247 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004248 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004249 psa_status_t actual_status;
4250 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004251 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004252 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004254
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004255 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004256
Gilles Peskine8817f612018-12-18 00:18:46 +01004257 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004258
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004259 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004260 psa_set_key_algorithm( &attributes, alg );
4261 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004262
Gilles Peskine049c7532019-05-15 20:22:09 +02004263 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004264 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004265
Ronald Cron5425a212020-08-04 14:58:35 +02004266 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004267 input_data->x, input_data->len,
4268 signature, signature_size,
4269 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004270 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004271 /* The value of *signature_length is unspecified on error, but
4272 * whatever it is, it should be less than signature_size, so that
4273 * if the caller tries to read *signature_length bytes without
4274 * checking the error code then they don't overflow a buffer. */
4275 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004276
Gilles Peskine895242b2019-11-29 12:15:40 +01004277#if defined(MBEDTLS_TEST_DEPRECATED)
4278 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004279 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004280 input_data->x, input_data->len,
4281 signature, signature_size,
4282 &signature_length ),
4283 expected_status );
4284 TEST_ASSERT( signature_length <= signature_size );
4285#endif /* MBEDTLS_TEST_DEPRECATED */
4286
Gilles Peskine20035e32018-02-03 22:44:14 +01004287exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004288 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004289 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004290 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004291 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004292}
4293/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004294
4295/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004296void sign_verify( int key_type_arg, data_t *key_data,
4297 int alg_arg, data_t *input_data )
4298{
Ronald Cron5425a212020-08-04 14:58:35 +02004299 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004300 psa_key_type_t key_type = key_type_arg;
4301 psa_algorithm_t alg = alg_arg;
4302 size_t key_bits;
4303 unsigned char *signature = NULL;
4304 size_t signature_size;
4305 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004307
Gilles Peskine8817f612018-12-18 00:18:46 +01004308 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004309
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004310 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004311 psa_set_key_algorithm( &attributes, alg );
4312 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004313
Gilles Peskine049c7532019-05-15 20:22:09 +02004314 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004315 &key ) );
4316 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004317 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004318
4319 /* Allocate a buffer which has the size advertized by the
4320 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004321 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004322 key_bits, alg );
4323 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004324 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004325 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004326
4327 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004328 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004329 input_data->x, input_data->len,
4330 signature, signature_size,
4331 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004332 /* Check that the signature length looks sensible. */
4333 TEST_ASSERT( signature_length <= signature_size );
4334 TEST_ASSERT( signature_length > 0 );
4335
4336 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004337 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004338 input_data->x, input_data->len,
4339 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004340
4341 if( input_data->len != 0 )
4342 {
4343 /* Flip a bit in the input and verify that the signature is now
4344 * detected as invalid. Flip a bit at the beginning, not at the end,
4345 * because ECDSA may ignore the last few bits of the input. */
4346 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004347 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004348 input_data->x, input_data->len,
4349 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004350 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004351 }
4352
4353exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004354 /*
4355 * Key attributes may have been returned by psa_get_key_attributes()
4356 * thus reset them as required.
4357 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004358 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004359
Ronald Cron5425a212020-08-04 14:58:35 +02004360 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004361 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004362 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004363}
4364/* END_CASE */
4365
4366/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004367void asymmetric_verify( int key_type_arg, data_t *key_data,
4368 int alg_arg, data_t *hash_data,
4369 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004370{
Ronald Cron5425a212020-08-04 14:58:35 +02004371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004372 psa_key_type_t key_type = key_type_arg;
4373 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004375
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004376 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004377
Gilles Peskine8817f612018-12-18 00:18:46 +01004378 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004379
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004380 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004381 psa_set_key_algorithm( &attributes, alg );
4382 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004383
Gilles Peskine049c7532019-05-15 20:22:09 +02004384 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004385 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004386
Ronald Cron5425a212020-08-04 14:58:35 +02004387 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004388 hash_data->x, hash_data->len,
4389 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004390
4391#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004392 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004393 hash_data->x, hash_data->len,
4394 signature_data->x,
4395 signature_data->len ) );
4396
4397#endif /* MBEDTLS_TEST_DEPRECATED */
4398
itayzafrir5c753392018-05-08 11:18:38 +03004399exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004400 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004401 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004402 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004403}
4404/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004405
4406/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004407void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4408 int alg_arg, data_t *hash_data,
4409 data_t *signature_data,
4410 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004411{
Ronald Cron5425a212020-08-04 14:58:35 +02004412 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004413 psa_key_type_t key_type = key_type_arg;
4414 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004415 psa_status_t actual_status;
4416 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004417 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004418
Gilles Peskine8817f612018-12-18 00:18:46 +01004419 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004420
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004421 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004422 psa_set_key_algorithm( &attributes, alg );
4423 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004424
Gilles Peskine049c7532019-05-15 20:22:09 +02004425 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004426 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004427
Ronald Cron5425a212020-08-04 14:58:35 +02004428 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004429 hash_data->x, hash_data->len,
4430 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004431 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004432
Gilles Peskine895242b2019-11-29 12:15:40 +01004433#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004434 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004435 hash_data->x, hash_data->len,
4436 signature_data->x, signature_data->len ),
4437 expected_status );
4438#endif /* MBEDTLS_TEST_DEPRECATED */
4439
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004440exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004441 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004442 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004443 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004444}
4445/* END_CASE */
4446
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004447/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004448void asymmetric_encrypt( int key_type_arg,
4449 data_t *key_data,
4450 int alg_arg,
4451 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004452 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004453 int expected_output_length_arg,
4454 int expected_status_arg )
4455{
Ronald Cron5425a212020-08-04 14:58:35 +02004456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004457 psa_key_type_t key_type = key_type_arg;
4458 psa_algorithm_t alg = alg_arg;
4459 size_t expected_output_length = expected_output_length_arg;
4460 size_t key_bits;
4461 unsigned char *output = NULL;
4462 size_t output_size;
4463 size_t output_length = ~0;
4464 psa_status_t actual_status;
4465 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004466 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004467
Gilles Peskine8817f612018-12-18 00:18:46 +01004468 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004469
Gilles Peskine656896e2018-06-29 19:12:28 +02004470 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004471 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4472 psa_set_key_algorithm( &attributes, alg );
4473 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004474 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004475 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004476
4477 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004478 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004479 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004480 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004481 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004482
4483 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004484 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004485 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004486 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004487 output, output_size,
4488 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004489 TEST_EQUAL( actual_status, expected_status );
4490 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004491
Gilles Peskine68428122018-06-30 18:42:41 +02004492 /* If the label is empty, the test framework puts a non-null pointer
4493 * in label->x. Test that a null pointer works as well. */
4494 if( label->len == 0 )
4495 {
4496 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004497 if( output_size != 0 )
4498 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004499 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004500 input_data->x, input_data->len,
4501 NULL, label->len,
4502 output, output_size,
4503 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004504 TEST_EQUAL( actual_status, expected_status );
4505 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004506 }
4507
Gilles Peskine656896e2018-06-29 19:12:28 +02004508exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004509 /*
4510 * Key attributes may have been returned by psa_get_key_attributes()
4511 * thus reset them as required.
4512 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004513 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004514
Ronald Cron5425a212020-08-04 14:58:35 +02004515 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004516 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004517 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004518}
4519/* END_CASE */
4520
4521/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004522void asymmetric_encrypt_decrypt( int key_type_arg,
4523 data_t *key_data,
4524 int alg_arg,
4525 data_t *input_data,
4526 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004527{
Ronald Cron5425a212020-08-04 14:58:35 +02004528 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004529 psa_key_type_t key_type = key_type_arg;
4530 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004531 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004532 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004533 size_t output_size;
4534 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004535 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004536 size_t output2_size;
4537 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004539
Gilles Peskine8817f612018-12-18 00:18:46 +01004540 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004541
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004542 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4543 psa_set_key_algorithm( &attributes, alg );
4544 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004545
Gilles Peskine049c7532019-05-15 20:22:09 +02004546 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004547 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004548
4549 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004550 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004551 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004552 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004553 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004554 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004555 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004556
Gilles Peskineeebd7382018-06-08 18:11:54 +02004557 /* We test encryption by checking that encrypt-then-decrypt gives back
4558 * the original plaintext because of the non-optional random
4559 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004560 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004561 input_data->x, input_data->len,
4562 label->x, label->len,
4563 output, output_size,
4564 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004565 /* We don't know what ciphertext length to expect, but check that
4566 * it looks sensible. */
4567 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004568
Ronald Cron5425a212020-08-04 14:58:35 +02004569 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004570 output, output_length,
4571 label->x, label->len,
4572 output2, output2_size,
4573 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004574 ASSERT_COMPARE( input_data->x, input_data->len,
4575 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004576
4577exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004578 /*
4579 * Key attributes may have been returned by psa_get_key_attributes()
4580 * thus reset them as required.
4581 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004582 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004583
Ronald Cron5425a212020-08-04 14:58:35 +02004584 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004585 mbedtls_free( output );
4586 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004587 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004588}
4589/* END_CASE */
4590
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004591/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004592void asymmetric_decrypt( int key_type_arg,
4593 data_t *key_data,
4594 int alg_arg,
4595 data_t *input_data,
4596 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004597 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004598{
Ronald Cron5425a212020-08-04 14:58:35 +02004599 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004600 psa_key_type_t key_type = key_type_arg;
4601 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004602 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004603 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004604 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004606
Jaeden Amero412654a2019-02-06 12:57:46 +00004607 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004608 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004609
Gilles Peskine8817f612018-12-18 00:18:46 +01004610 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004611
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004612 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4613 psa_set_key_algorithm( &attributes, alg );
4614 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004615
Gilles Peskine049c7532019-05-15 20:22:09 +02004616 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004617 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004618
Ronald Cron5425a212020-08-04 14:58:35 +02004619 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004620 input_data->x, input_data->len,
4621 label->x, label->len,
4622 output,
4623 output_size,
4624 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004625 ASSERT_COMPARE( expected_data->x, expected_data->len,
4626 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004627
Gilles Peskine68428122018-06-30 18:42:41 +02004628 /* If the label is empty, the test framework puts a non-null pointer
4629 * in label->x. Test that a null pointer works as well. */
4630 if( label->len == 0 )
4631 {
4632 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004633 if( output_size != 0 )
4634 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004635 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004636 input_data->x, input_data->len,
4637 NULL, label->len,
4638 output,
4639 output_size,
4640 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004641 ASSERT_COMPARE( expected_data->x, expected_data->len,
4642 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004643 }
4644
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004645exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004646 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004647 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004648 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004649 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004650}
4651/* END_CASE */
4652
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004653/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004654void asymmetric_decrypt_fail( int key_type_arg,
4655 data_t *key_data,
4656 int alg_arg,
4657 data_t *input_data,
4658 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004659 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004660 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004661{
Ronald Cron5425a212020-08-04 14:58:35 +02004662 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004663 psa_key_type_t key_type = key_type_arg;
4664 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004665 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004666 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004667 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004668 psa_status_t actual_status;
4669 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004671
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004672 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004673
Gilles Peskine8817f612018-12-18 00:18:46 +01004674 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004675
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004676 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4677 psa_set_key_algorithm( &attributes, alg );
4678 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004679
Gilles Peskine049c7532019-05-15 20:22:09 +02004680 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004681 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004682
Ronald Cron5425a212020-08-04 14:58:35 +02004683 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004684 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004685 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004686 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004687 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004688 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004689 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004690
Gilles Peskine68428122018-06-30 18:42:41 +02004691 /* If the label is empty, the test framework puts a non-null pointer
4692 * in label->x. Test that a null pointer works as well. */
4693 if( label->len == 0 )
4694 {
4695 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004696 if( output_size != 0 )
4697 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004698 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004699 input_data->x, input_data->len,
4700 NULL, label->len,
4701 output, output_size,
4702 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004703 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004704 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004705 }
4706
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004707exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004708 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004709 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004710 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004711 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004712}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004713/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004714
4715/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004716void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004717{
4718 /* Test each valid way of initializing the object, except for `= {0}`, as
4719 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4720 * though it's OK by the C standard. We could test for this, but we'd need
4721 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004722 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004723 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4724 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4725 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004726
4727 memset( &zero, 0, sizeof( zero ) );
4728
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004729 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004730 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004731 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004732 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004733 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004734 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004735 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004736
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004738 PSA_ASSERT( psa_key_derivation_abort(&func) );
4739 PSA_ASSERT( psa_key_derivation_abort(&init) );
4740 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004741}
4742/* END_CASE */
4743
Janos Follath16de4a42019-06-13 16:32:24 +01004744/* BEGIN_CASE */
4745void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004746{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004747 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004748 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004750
Gilles Peskine8817f612018-12-18 00:18:46 +01004751 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004752
Janos Follath16de4a42019-06-13 16:32:24 +01004753 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004754 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004755
4756exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004757 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004758 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004759}
4760/* END_CASE */
4761
Janos Follathaf3c2a02019-06-12 12:34:34 +01004762/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004763void derive_set_capacity( int alg_arg, int capacity_arg,
4764 int expected_status_arg )
4765{
4766 psa_algorithm_t alg = alg_arg;
4767 size_t capacity = capacity_arg;
4768 psa_status_t expected_status = expected_status_arg;
4769 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4770
4771 PSA_ASSERT( psa_crypto_init( ) );
4772
4773 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4774
4775 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4776 expected_status );
4777
4778exit:
4779 psa_key_derivation_abort( &operation );
4780 PSA_DONE( );
4781}
4782/* END_CASE */
4783
4784/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004785void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004786 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004787 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004788 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004789 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004790 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004791 int expected_status_arg3,
4792 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004793{
4794 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004795 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4796 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004797 psa_status_t expected_statuses[] = {expected_status_arg1,
4798 expected_status_arg2,
4799 expected_status_arg3};
4800 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004801 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4802 MBEDTLS_SVC_KEY_ID_INIT,
4803 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004804 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4806 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004807 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004808 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004809 psa_status_t expected_output_status = expected_output_status_arg;
4810 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004811
4812 PSA_ASSERT( psa_crypto_init( ) );
4813
4814 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4815 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004816
4817 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4818
4819 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4820 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004821 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004822 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004823 psa_set_key_type( &attributes, key_types[i] );
4824 PSA_ASSERT( psa_import_key( &attributes,
4825 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004826 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004827 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4828 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4829 {
4830 // When taking a private key as secret input, use key agreement
4831 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004832 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004833 expected_statuses[i] );
4834 }
4835 else
4836 {
4837 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004838 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004839 expected_statuses[i] );
4840 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004841 }
4842 else
4843 {
4844 TEST_EQUAL( psa_key_derivation_input_bytes(
4845 &operation, steps[i],
4846 inputs[i]->x, inputs[i]->len ),
4847 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004848 }
4849 }
4850
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004851 if( output_key_type != PSA_KEY_TYPE_NONE )
4852 {
4853 psa_reset_key_attributes( &attributes );
4854 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4855 psa_set_key_bits( &attributes, 8 );
4856 actual_output_status =
4857 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004858 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004859 }
4860 else
4861 {
4862 uint8_t buffer[1];
4863 actual_output_status =
4864 psa_key_derivation_output_bytes( &operation,
4865 buffer, sizeof( buffer ) );
4866 }
4867 TEST_EQUAL( actual_output_status, expected_output_status );
4868
Janos Follathaf3c2a02019-06-12 12:34:34 +01004869exit:
4870 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004871 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4872 psa_destroy_key( keys[i] );
4873 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004874 PSA_DONE( );
4875}
4876/* END_CASE */
4877
Janos Follathd958bb72019-07-03 15:02:16 +01004878/* BEGIN_CASE */
4879void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004880{
Janos Follathd958bb72019-07-03 15:02:16 +01004881 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004882 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004883 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004884 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004885 unsigned char input1[] = "Input 1";
4886 size_t input1_length = sizeof( input1 );
4887 unsigned char input2[] = "Input 2";
4888 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004889 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004890 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004891 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4892 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4893 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004895
Gilles Peskine8817f612018-12-18 00:18:46 +01004896 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004897
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004898 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4899 psa_set_key_algorithm( &attributes, alg );
4900 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004901
Gilles Peskine73676cb2019-05-15 20:15:10 +02004902 PSA_ASSERT( psa_import_key( &attributes,
4903 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004904 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004905
4906 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004907 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004908 input1, input1_length,
4909 input2, input2_length,
4910 capacity ) )
4911 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004912
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004914 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004915 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004916
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004918
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004920 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004921
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004922exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004923 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004924 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004925 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004926}
4927/* END_CASE */
4928
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004929/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004930void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004931{
4932 uint8_t output_buffer[16];
4933 size_t buffer_size = 16;
4934 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004936
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004937 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4938 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004939 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004940
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004941 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004942 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004943
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004944 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004945
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004946 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4947 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004948 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004949
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004950 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004951 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004952
4953exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004955}
4956/* END_CASE */
4957
4958/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004959void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004960 int step1_arg, data_t *input1,
4961 int step2_arg, data_t *input2,
4962 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004963 int requested_capacity_arg,
4964 data_t *expected_output1,
4965 data_t *expected_output2 )
4966{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004967 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004968 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4969 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004970 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4971 MBEDTLS_SVC_KEY_ID_INIT,
4972 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004973 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004974 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004975 uint8_t *expected_outputs[2] =
4976 {expected_output1->x, expected_output2->x};
4977 size_t output_sizes[2] =
4978 {expected_output1->len, expected_output2->len};
4979 size_t output_buffer_size = 0;
4980 uint8_t *output_buffer = NULL;
4981 size_t expected_capacity;
4982 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004983 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004984 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004985 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004986
4987 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4988 {
4989 if( output_sizes[i] > output_buffer_size )
4990 output_buffer_size = output_sizes[i];
4991 if( output_sizes[i] == 0 )
4992 expected_outputs[i] = NULL;
4993 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004994 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004995 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004996
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004997 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4998 psa_set_key_algorithm( &attributes, alg );
4999 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005000
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005001 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005002 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5003 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5004 requested_capacity ) );
5005 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005006 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005007 switch( steps[i] )
5008 {
5009 case 0:
5010 break;
5011 case PSA_KEY_DERIVATION_INPUT_SECRET:
5012 PSA_ASSERT( psa_import_key( &attributes,
5013 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005014 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005015 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005016 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005017 break;
5018 default:
5019 PSA_ASSERT( psa_key_derivation_input_bytes(
5020 &operation, steps[i],
5021 inputs[i]->x, inputs[i]->len ) );
5022 break;
5023 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005024 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005025
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005026 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005027 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005028 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005029 expected_capacity = requested_capacity;
5030
5031 /* Expansion phase. */
5032 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5033 {
5034 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005035 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005036 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005037 if( expected_capacity == 0 && output_sizes[i] == 0 )
5038 {
5039 /* Reading 0 bytes when 0 bytes are available can go either way. */
5040 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005041 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005042 continue;
5043 }
5044 else if( expected_capacity == 0 ||
5045 output_sizes[i] > expected_capacity )
5046 {
5047 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005048 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005049 expected_capacity = 0;
5050 continue;
5051 }
5052 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005053 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005054 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005055 ASSERT_COMPARE( output_buffer, output_sizes[i],
5056 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005057 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005058 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005059 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005060 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005061 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005062 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005064
5065exit:
5066 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005067 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005068 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5069 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005070 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005071}
5072/* END_CASE */
5073
5074/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005075void derive_full( int alg_arg,
5076 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005077 data_t *input1,
5078 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005079 int requested_capacity_arg )
5080{
Ronald Cron5425a212020-08-04 14:58:35 +02005081 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005082 psa_algorithm_t alg = alg_arg;
5083 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005084 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005085 unsigned char output_buffer[16];
5086 size_t expected_capacity = requested_capacity;
5087 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005089
Gilles Peskine8817f612018-12-18 00:18:46 +01005090 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005091
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005092 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5093 psa_set_key_algorithm( &attributes, alg );
5094 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005095
Gilles Peskine049c7532019-05-15 20:22:09 +02005096 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005097 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005098
Ronald Cron5425a212020-08-04 14:58:35 +02005099 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005100 input1->x, input1->len,
5101 input2->x, input2->len,
5102 requested_capacity ) )
5103 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005104
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005105 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005106 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005107 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005108
5109 /* Expansion phase. */
5110 while( current_capacity > 0 )
5111 {
5112 size_t read_size = sizeof( output_buffer );
5113 if( read_size > current_capacity )
5114 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005116 output_buffer,
5117 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005118 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005119 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005120 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005121 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005122 }
5123
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005124 /* Check that the operation refuses to go over capacity. */
5125 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005126 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005127
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005129
5130exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005131 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005132 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005133 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005134}
5135/* END_CASE */
5136
Janos Follathe60c9052019-07-03 13:51:30 +01005137/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005138void derive_key_exercise( int alg_arg,
5139 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005140 data_t *input1,
5141 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005142 int derived_type_arg,
5143 int derived_bits_arg,
5144 int derived_usage_arg,
5145 int derived_alg_arg )
5146{
Ronald Cron5425a212020-08-04 14:58:35 +02005147 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5148 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005149 psa_algorithm_t alg = alg_arg;
5150 psa_key_type_t derived_type = derived_type_arg;
5151 size_t derived_bits = derived_bits_arg;
5152 psa_key_usage_t derived_usage = derived_usage_arg;
5153 psa_algorithm_t derived_alg = derived_alg_arg;
5154 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005157 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005158
Gilles Peskine8817f612018-12-18 00:18:46 +01005159 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005160
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005161 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5162 psa_set_key_algorithm( &attributes, alg );
5163 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005164 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005165 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005166
5167 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005168 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005169 input1->x, input1->len,
5170 input2->x, input2->len, capacity ) )
5171 goto exit;
5172
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005173 psa_set_key_usage_flags( &attributes, derived_usage );
5174 psa_set_key_algorithm( &attributes, derived_alg );
5175 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005176 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005177 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005178 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005179
5180 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005181 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005182 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5183 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005184
5185 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005186 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005187 goto exit;
5188
5189exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005190 /*
5191 * Key attributes may have been returned by psa_get_key_attributes()
5192 * thus reset them as required.
5193 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005194 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005195
5196 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005197 psa_destroy_key( base_key );
5198 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005199 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005200}
5201/* END_CASE */
5202
Janos Follath42fd8882019-07-03 14:17:09 +01005203/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005204void derive_key_export( int alg_arg,
5205 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005206 data_t *input1,
5207 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005208 int bytes1_arg,
5209 int bytes2_arg )
5210{
Ronald Cron5425a212020-08-04 14:58:35 +02005211 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5212 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005213 psa_algorithm_t alg = alg_arg;
5214 size_t bytes1 = bytes1_arg;
5215 size_t bytes2 = bytes2_arg;
5216 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005218 uint8_t *output_buffer = NULL;
5219 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005220 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5221 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005222 size_t length;
5223
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005224 ASSERT_ALLOC( output_buffer, capacity );
5225 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005226 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005227
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005228 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5229 psa_set_key_algorithm( &base_attributes, alg );
5230 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005231 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005232 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005233
5234 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005235 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005236 input1->x, input1->len,
5237 input2->x, input2->len, capacity ) )
5238 goto exit;
5239
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005240 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005241 output_buffer,
5242 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005243 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005244
5245 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005246 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005247 input1->x, input1->len,
5248 input2->x, input2->len, capacity ) )
5249 goto exit;
5250
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005251 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5252 psa_set_key_algorithm( &derived_attributes, 0 );
5253 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005254 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005256 &derived_key ) );
5257 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005258 export_buffer, bytes1,
5259 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005260 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005261 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005262 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005264 &derived_key ) );
5265 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005266 export_buffer + bytes1, bytes2,
5267 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005268 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005269
5270 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005271 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5272 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005273
5274exit:
5275 mbedtls_free( output_buffer );
5276 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005278 psa_destroy_key( base_key );
5279 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005280 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005281}
5282/* END_CASE */
5283
5284/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005285void derive_key( int alg_arg,
5286 data_t *key_data, data_t *input1, data_t *input2,
5287 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005288 int expected_status_arg,
5289 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005290{
Ronald Cron5425a212020-08-04 14:58:35 +02005291 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5292 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005293 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005294 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005295 size_t bits = bits_arg;
5296 psa_status_t expected_status = expected_status_arg;
5297 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5298 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5299 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5300
5301 PSA_ASSERT( psa_crypto_init( ) );
5302
5303 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5304 psa_set_key_algorithm( &base_attributes, alg );
5305 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5306 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005307 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005308
Ronald Cron5425a212020-08-04 14:58:35 +02005309 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005310 input1->x, input1->len,
5311 input2->x, input2->len, SIZE_MAX ) )
5312 goto exit;
5313
5314 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5315 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005316 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005317 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005318
5319 psa_status_t status =
5320 psa_key_derivation_output_key( &derived_attributes,
5321 &operation,
5322 &derived_key );
5323 if( is_large_output > 0 )
5324 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5325 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005326
5327exit:
5328 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005329 psa_destroy_key( base_key );
5330 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005331 PSA_DONE( );
5332}
5333/* END_CASE */
5334
5335/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005336void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005337 int our_key_type_arg, int our_key_alg_arg,
5338 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005339 int expected_status_arg )
5340{
Ronald Cron5425a212020-08-04 14:58:35 +02005341 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005342 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005343 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005344 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005345 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005347 psa_status_t expected_status = expected_status_arg;
5348 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005349
Gilles Peskine8817f612018-12-18 00:18:46 +01005350 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005351
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005353 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005354 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005355 PSA_ASSERT( psa_import_key( &attributes,
5356 our_key_data->x, our_key_data->len,
5357 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005358
Gilles Peskine77f40d82019-04-11 21:27:06 +02005359 /* The tests currently include inputs that should fail at either step.
5360 * Test cases that fail at the setup step should be changed to call
5361 * key_derivation_setup instead, and this function should be renamed
5362 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005363 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005364 if( status == PSA_SUCCESS )
5365 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005366 TEST_EQUAL( psa_key_derivation_key_agreement(
5367 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5368 our_key,
5369 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005370 expected_status );
5371 }
5372 else
5373 {
5374 TEST_ASSERT( status == expected_status );
5375 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005376
5377exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005378 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005379 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005380 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005381}
5382/* END_CASE */
5383
5384/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005385void raw_key_agreement( int alg_arg,
5386 int our_key_type_arg, data_t *our_key_data,
5387 data_t *peer_key_data,
5388 data_t *expected_output )
5389{
Ronald Cron5425a212020-08-04 14:58:35 +02005390 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005391 psa_algorithm_t alg = alg_arg;
5392 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005394 unsigned char *output = NULL;
5395 size_t output_length = ~0;
5396
5397 ASSERT_ALLOC( output, expected_output->len );
5398 PSA_ASSERT( psa_crypto_init( ) );
5399
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005400 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5401 psa_set_key_algorithm( &attributes, alg );
5402 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005403 PSA_ASSERT( psa_import_key( &attributes,
5404 our_key_data->x, our_key_data->len,
5405 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005406
Gilles Peskinebe697d82019-05-16 18:00:41 +02005407 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5408 peer_key_data->x, peer_key_data->len,
5409 output, expected_output->len,
5410 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005411 ASSERT_COMPARE( output, output_length,
5412 expected_output->x, expected_output->len );
5413
5414exit:
5415 mbedtls_free( output );
5416 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005417 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005418}
5419/* END_CASE */
5420
5421/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005422void key_agreement_capacity( int alg_arg,
5423 int our_key_type_arg, data_t *our_key_data,
5424 data_t *peer_key_data,
5425 int expected_capacity_arg )
5426{
Ronald Cron5425a212020-08-04 14:58:35 +02005427 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005428 psa_algorithm_t alg = alg_arg;
5429 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005430 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005432 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005433 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005434
Gilles Peskine8817f612018-12-18 00:18:46 +01005435 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005436
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005437 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5438 psa_set_key_algorithm( &attributes, alg );
5439 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005440 PSA_ASSERT( psa_import_key( &attributes,
5441 our_key_data->x, our_key_data->len,
5442 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005443
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005444 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005445 PSA_ASSERT( psa_key_derivation_key_agreement(
5446 &operation,
5447 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5448 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005449 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5450 {
5451 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005453 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005454 NULL, 0 ) );
5455 }
Gilles Peskine59685592018-09-18 12:11:34 +02005456
Gilles Peskinebf491972018-10-25 22:36:12 +02005457 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005458 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005459 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005460 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005461
Gilles Peskinebf491972018-10-25 22:36:12 +02005462 /* Test the actual capacity by reading the output. */
5463 while( actual_capacity > sizeof( output ) )
5464 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005465 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005466 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005467 actual_capacity -= sizeof( output );
5468 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005469 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005470 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005471 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005472 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005473
Gilles Peskine59685592018-09-18 12:11:34 +02005474exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005475 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005476 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005477 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005478}
5479/* END_CASE */
5480
5481/* BEGIN_CASE */
5482void key_agreement_output( int alg_arg,
5483 int our_key_type_arg, data_t *our_key_data,
5484 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005485 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005486{
Ronald Cron5425a212020-08-04 14:58:35 +02005487 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005488 psa_algorithm_t alg = alg_arg;
5489 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005490 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005491 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005492 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005493
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005494 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5495 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005496
Gilles Peskine8817f612018-12-18 00:18:46 +01005497 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005498
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005499 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5500 psa_set_key_algorithm( &attributes, alg );
5501 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005502 PSA_ASSERT( psa_import_key( &attributes,
5503 our_key_data->x, our_key_data->len,
5504 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005505
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005506 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005507 PSA_ASSERT( psa_key_derivation_key_agreement(
5508 &operation,
5509 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5510 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005511 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5512 {
5513 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005514 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005515 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005516 NULL, 0 ) );
5517 }
Gilles Peskine59685592018-09-18 12:11:34 +02005518
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005519 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005520 actual_output,
5521 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005522 ASSERT_COMPARE( actual_output, expected_output1->len,
5523 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005524 if( expected_output2->len != 0 )
5525 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005526 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005527 actual_output,
5528 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005529 ASSERT_COMPARE( actual_output, expected_output2->len,
5530 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005531 }
Gilles Peskine59685592018-09-18 12:11:34 +02005532
5533exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005534 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005535 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005536 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005537 mbedtls_free( actual_output );
5538}
5539/* END_CASE */
5540
5541/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005542void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005543{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005544 size_t bytes = bytes_arg;
5545 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005546 unsigned char *output = NULL;
5547 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005548 size_t i;
5549 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005550
Simon Butcher49f8e312020-03-03 15:51:50 +00005551 TEST_ASSERT( bytes_arg >= 0 );
5552
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005553 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5554 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005555 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005556
Gilles Peskine8817f612018-12-18 00:18:46 +01005557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005558
Gilles Peskinea50d7392018-06-21 10:22:13 +02005559 /* Run several times, to ensure that every output byte will be
5560 * nonzero at least once with overwhelming probability
5561 * (2^(-8*number_of_runs)). */
5562 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005563 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005564 if( bytes != 0 )
5565 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005566 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005567
5568 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005569 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5570 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005571
5572 for( i = 0; i < bytes; i++ )
5573 {
5574 if( output[i] != 0 )
5575 ++changed[i];
5576 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005577 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005578
5579 /* Check that every byte was changed to nonzero at least once. This
5580 * validates that psa_generate_random is overwriting every byte of
5581 * the output buffer. */
5582 for( i = 0; i < bytes; i++ )
5583 {
5584 TEST_ASSERT( changed[i] != 0 );
5585 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005586
5587exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005588 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005589 mbedtls_free( output );
5590 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005591}
5592/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005593
5594/* BEGIN_CASE */
5595void generate_key( int type_arg,
5596 int bits_arg,
5597 int usage_arg,
5598 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005599 int expected_status_arg,
5600 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005601{
Ronald Cron5425a212020-08-04 14:58:35 +02005602 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005603 psa_key_type_t type = type_arg;
5604 psa_key_usage_t usage = usage_arg;
5605 size_t bits = bits_arg;
5606 psa_algorithm_t alg = alg_arg;
5607 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005609 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005610
Gilles Peskine8817f612018-12-18 00:18:46 +01005611 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005612
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005613 psa_set_key_usage_flags( &attributes, usage );
5614 psa_set_key_algorithm( &attributes, alg );
5615 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005616 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005617
5618 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005619 psa_status_t status = psa_generate_key( &attributes, &key );
5620
5621 if( is_large_key > 0 )
5622 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5623 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005624 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005625 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005626
5627 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005628 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005629 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5630 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005631
Gilles Peskine818ca122018-06-20 18:16:48 +02005632 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005633 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005634 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005635
5636exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005637 /*
5638 * Key attributes may have been returned by psa_get_key_attributes()
5639 * thus reset them as required.
5640 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005641 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005642
Ronald Cron5425a212020-08-04 14:58:35 +02005643 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005644 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005645}
5646/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005647
Gilles Peskinee56e8782019-04-26 17:34:02 +02005648/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5649void generate_key_rsa( int bits_arg,
5650 data_t *e_arg,
5651 int expected_status_arg )
5652{
Ronald Cron5425a212020-08-04 14:58:35 +02005653 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005654 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005655 size_t bits = bits_arg;
5656 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5657 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5658 psa_status_t expected_status = expected_status_arg;
5659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5660 uint8_t *exported = NULL;
5661 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005662 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005663 size_t exported_length = SIZE_MAX;
5664 uint8_t *e_read_buffer = NULL;
5665 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005666 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005667 size_t e_read_length = SIZE_MAX;
5668
5669 if( e_arg->len == 0 ||
5670 ( e_arg->len == 3 &&
5671 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5672 {
5673 is_default_public_exponent = 1;
5674 e_read_size = 0;
5675 }
5676 ASSERT_ALLOC( e_read_buffer, e_read_size );
5677 ASSERT_ALLOC( exported, exported_size );
5678
5679 PSA_ASSERT( psa_crypto_init( ) );
5680
5681 psa_set_key_usage_flags( &attributes, usage );
5682 psa_set_key_algorithm( &attributes, alg );
5683 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5684 e_arg->x, e_arg->len ) );
5685 psa_set_key_bits( &attributes, bits );
5686
5687 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005688 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005689 if( expected_status != PSA_SUCCESS )
5690 goto exit;
5691
5692 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005693 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005694 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5695 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5696 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5697 e_read_buffer, e_read_size,
5698 &e_read_length ) );
5699 if( is_default_public_exponent )
5700 TEST_EQUAL( e_read_length, 0 );
5701 else
5702 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5703
5704 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005705 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005706 goto exit;
5707
5708 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005709 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005710 exported, exported_size,
5711 &exported_length ) );
5712 {
5713 uint8_t *p = exported;
5714 uint8_t *end = exported + exported_length;
5715 size_t len;
5716 /* RSAPublicKey ::= SEQUENCE {
5717 * modulus INTEGER, -- n
5718 * publicExponent INTEGER } -- e
5719 */
5720 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005721 MBEDTLS_ASN1_SEQUENCE |
5722 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005723 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5724 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5725 MBEDTLS_ASN1_INTEGER ) );
5726 if( len >= 1 && p[0] == 0 )
5727 {
5728 ++p;
5729 --len;
5730 }
5731 if( e_arg->len == 0 )
5732 {
5733 TEST_EQUAL( len, 3 );
5734 TEST_EQUAL( p[0], 1 );
5735 TEST_EQUAL( p[1], 0 );
5736 TEST_EQUAL( p[2], 1 );
5737 }
5738 else
5739 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5740 }
5741
5742exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005743 /*
5744 * Key attributes may have been returned by psa_get_key_attributes() or
5745 * set by psa_set_key_domain_parameters() thus reset them as required.
5746 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005747 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005748
Ronald Cron5425a212020-08-04 14:58:35 +02005749 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005750 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005751 mbedtls_free( e_read_buffer );
5752 mbedtls_free( exported );
5753}
5754/* END_CASE */
5755
Darryl Greend49a4992018-06-18 17:27:26 +01005756/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005757void persistent_key_load_key_from_storage( data_t *data,
5758 int type_arg, int bits_arg,
5759 int usage_flags_arg, int alg_arg,
5760 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005761{
Ronald Cron71016a92020-08-28 19:01:50 +02005762 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005763 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5765 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005766 psa_key_type_t type = type_arg;
5767 size_t bits = bits_arg;
5768 psa_key_usage_t usage_flags = usage_flags_arg;
5769 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005770 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005771 unsigned char *first_export = NULL;
5772 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005773 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005774 size_t first_exported_length;
5775 size_t second_exported_length;
5776
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005777 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5778 {
5779 ASSERT_ALLOC( first_export, export_size );
5780 ASSERT_ALLOC( second_export, export_size );
5781 }
Darryl Greend49a4992018-06-18 17:27:26 +01005782
Gilles Peskine8817f612018-12-18 00:18:46 +01005783 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005784
Gilles Peskinec87af662019-05-15 16:12:22 +02005785 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005786 psa_set_key_usage_flags( &attributes, usage_flags );
5787 psa_set_key_algorithm( &attributes, alg );
5788 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005789 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005790
Darryl Green0c6575a2018-11-07 16:05:30 +00005791 switch( generation_method )
5792 {
5793 case IMPORT_KEY:
5794 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005795 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005796 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005797 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005798
Darryl Green0c6575a2018-11-07 16:05:30 +00005799 case GENERATE_KEY:
5800 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005801 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005802 break;
5803
5804 case DERIVE_KEY:
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005805#if PSA_WANT_ALG_HKDF && PSA_WANT_ALG_SHA_256
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005806 {
5807 /* Create base key */
5808 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5809 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5810 psa_set_key_usage_flags( &base_attributes,
5811 PSA_KEY_USAGE_DERIVE );
5812 psa_set_key_algorithm( &base_attributes, derive_alg );
5813 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005814 PSA_ASSERT( psa_import_key( &base_attributes,
5815 data->x, data->len,
5816 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005817 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005818 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005819 PSA_ASSERT( psa_key_derivation_input_key(
5820 &operation,
5821 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005822 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005823 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005824 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005825 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5826 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005827 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005828 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005829 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005830 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005831 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005832#else
5833 TEST_ASSUME( ! "KDF not supported in this configuration" );
5834#endif
5835 break;
5836
5837 default:
5838 TEST_ASSERT( ! "generation_method not implemented in test" );
5839 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005840 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005841 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005842
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005843 /* Export the key if permitted by the key policy. */
5844 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5845 {
Ronald Cron5425a212020-08-04 14:58:35 +02005846 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005847 first_export, export_size,
5848 &first_exported_length ) );
5849 if( generation_method == IMPORT_KEY )
5850 ASSERT_COMPARE( data->x, data->len,
5851 first_export, first_exported_length );
5852 }
Darryl Greend49a4992018-06-18 17:27:26 +01005853
5854 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005855 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005856 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005857 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005858
Darryl Greend49a4992018-06-18 17:27:26 +01005859 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005860 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005861 TEST_ASSERT( mbedtls_svc_key_id_equal(
5862 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005863 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5864 PSA_KEY_LIFETIME_PERSISTENT );
5865 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5866 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5867 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5868 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005869
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005870 /* Export the key again if permitted by the key policy. */
5871 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005872 {
Ronald Cron5425a212020-08-04 14:58:35 +02005873 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005874 second_export, export_size,
5875 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005876 ASSERT_COMPARE( first_export, first_exported_length,
5877 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005878 }
5879
5880 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005881 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005882 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005883
5884exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005885 /*
5886 * Key attributes may have been returned by psa_get_key_attributes()
5887 * thus reset them as required.
5888 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005889 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005890
Darryl Greend49a4992018-06-18 17:27:26 +01005891 mbedtls_free( first_export );
5892 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005893 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005894 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005895 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005896 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005897}
5898/* END_CASE */