blob: 82797681ef943876c4d6fac0c7d6415d7c0f6daa [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
Ronald Cron02c78b72020-05-27 09:22:32 +020012#include "test/psa_crypto_helpers.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskinec744d992019-07-30 17:26:54 +020014/* Tests that require more than 128kB of RAM plus change have this symbol
15 * as a dependency. Currently we always define this symbol, so the tests
16 * are always executed. In the future we should make this conditional
17 * so that tests that require a lot of memory are skipped on constrained
18 * platforms. */
Gilles Peskine49232e82019-08-07 11:01:30 +020019#define HAVE_RAM_AVAILABLE_128K
Gilles Peskinec744d992019-07-30 17:26:54 +020020
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020021#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020022#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020023
Jaeden Amerof24c7f82018-06-27 17:20:43 +010024/** An invalid export length that will never be set by psa_export_key(). */
25static const size_t INVALID_EXPORT_LENGTH = ~0U;
26
Gilles Peskinef426e0f2019-02-25 17:42:03 +010027/* A hash algorithm that is known to be supported.
28 *
29 * This is used in some smoke tests.
30 */
31#if defined(MBEDTLS_MD2_C)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
33#elif defined(MBEDTLS_MD4_C)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
35#elif defined(MBEDTLS_MD5_C)
36#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
37/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
38 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
39 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
40 * implausible anyway. */
41#elif defined(MBEDTLS_SHA1_C)
42#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
43#elif defined(MBEDTLS_SHA256_C)
44#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
45#elif defined(MBEDTLS_SHA512_C)
46#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
47#elif defined(MBEDTLS_SHA3_C)
48#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
49#else
50#undef KNOWN_SUPPORTED_HASH_ALG
51#endif
52
53/* A block cipher that is known to be supported.
54 *
55 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
56 */
57#if defined(MBEDTLS_AES_C)
58#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
59#elif defined(MBEDTLS_ARIA_C)
60#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
61#elif defined(MBEDTLS_CAMELLIA_C)
62#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
63#undef KNOWN_SUPPORTED_BLOCK_CIPHER
64#endif
65
66/* A MAC mode that is known to be supported.
67 *
68 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
69 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
70 *
71 * This is used in some smoke tests.
72 */
73#if defined(KNOWN_SUPPORTED_HASH_ALG)
74#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
75#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
76#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
77#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
78#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
79#else
80#undef KNOWN_SUPPORTED_MAC_ALG
81#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
82#endif
83
84/* A cipher algorithm and key type that are known to be supported.
85 *
86 * This is used in some smoke tests.
87 */
88#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
89#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
90#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
91#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
92#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
93#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
94#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
95#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
96#else
97#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
98#endif
99#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
100#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
101#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
102#elif defined(MBEDTLS_RC4_C)
103#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
104#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
105#else
106#undef KNOWN_SUPPORTED_CIPHER_ALG
107#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
108#endif
109
Gilles Peskine667c1112019-12-03 19:03:20 +0100110#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
111int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
112{
113 /* At the moment, anything that isn't a built-in lifetime is either
114 * a secure element or unassigned. */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200115 return( ( ! PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) &&
Gilles Peskine667c1112019-12-03 19:03:20 +0100116 lifetime != PSA_KEY_LIFETIME_PERSISTENT );
117}
118#else
119int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
120{
121 (void) lifetime;
122 return( 0 );
123}
124#endif
125
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200126/** Test if a buffer contains a constant byte value.
127 *
128 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200129 *
130 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200131 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200132 * \param size Size of the buffer in bytes.
133 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200134 * \return 1 if the buffer is all-bits-zero.
135 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200136 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200137static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200138{
139 size_t i;
140 for( i = 0; i < size; i++ )
141 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200142 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200143 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200144 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200145 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200146}
Gilles Peskine818ca122018-06-20 18:16:48 +0200147
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200148/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
149static int asn1_write_10x( unsigned char **p,
150 unsigned char *start,
151 size_t bits,
152 unsigned char x )
153{
154 int ret;
155 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200156 if( bits == 0 )
157 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
158 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200159 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300160 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200161 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
162 *p -= len;
163 ( *p )[len-1] = x;
164 if( bits % 8 == 0 )
165 ( *p )[1] |= 1;
166 else
167 ( *p )[0] |= 1 << ( bits % 8 );
168 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
169 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
170 MBEDTLS_ASN1_INTEGER ) );
171 return( len );
172}
173
174static int construct_fake_rsa_key( unsigned char *buffer,
175 size_t buffer_size,
176 unsigned char **p,
177 size_t bits,
178 int keypair )
179{
180 size_t half_bits = ( bits + 1 ) / 2;
181 int ret;
182 int len = 0;
183 /* Construct something that looks like a DER encoding of
184 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
185 * RSAPrivateKey ::= SEQUENCE {
186 * version Version,
187 * modulus INTEGER, -- n
188 * publicExponent INTEGER, -- e
189 * privateExponent INTEGER, -- d
190 * prime1 INTEGER, -- p
191 * prime2 INTEGER, -- q
192 * exponent1 INTEGER, -- d mod (p-1)
193 * exponent2 INTEGER, -- d mod (q-1)
194 * coefficient INTEGER, -- (inverse of q) mod p
195 * otherPrimeInfos OtherPrimeInfos OPTIONAL
196 * }
197 * Or, for a public key, the same structure with only
198 * version, modulus and publicExponent.
199 */
200 *p = buffer + buffer_size;
201 if( keypair )
202 {
203 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
204 asn1_write_10x( p, buffer, half_bits, 1 ) );
205 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
206 asn1_write_10x( p, buffer, half_bits, 1 ) );
207 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
208 asn1_write_10x( p, buffer, half_bits, 1 ) );
209 MBEDTLS_ASN1_CHK_ADD( len, /* q */
210 asn1_write_10x( p, buffer, half_bits, 1 ) );
211 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
212 asn1_write_10x( p, buffer, half_bits, 3 ) );
213 MBEDTLS_ASN1_CHK_ADD( len, /* d */
214 asn1_write_10x( p, buffer, bits, 1 ) );
215 }
216 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
217 asn1_write_10x( p, buffer, 17, 1 ) );
218 MBEDTLS_ASN1_CHK_ADD( len, /* n */
219 asn1_write_10x( p, buffer, bits, 1 ) );
220 if( keypair )
221 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
222 mbedtls_asn1_write_int( p, buffer, 0 ) );
223 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
224 {
225 const unsigned char tag =
226 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
227 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
228 }
229 return( len );
230}
231
Ronald Cron5425a212020-08-04 14:58:35 +0200232int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100233{
234 int ok = 0;
235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
236 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200237 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100238 psa_key_type_t type;
239 psa_key_type_t bits;
240
241 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
242 lifetime = psa_get_key_lifetime( &attributes );
243 id = psa_get_key_id( &attributes );
244 type = psa_get_key_type( &attributes );
245 bits = psa_get_key_bits( &attributes );
246
247 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200248 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200249 {
250 TEST_ASSERT(
251 ( PSA_KEY_ID_VOLATILE_MIN <=
252 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
253 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
254 PSA_KEY_ID_VOLATILE_MAX ) );
255 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100256 else
257 {
258 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200259 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
260 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100261 }
262#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
263 /* randomly-generated 64-bit constant, should never appear in test data */
264 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
265 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
266 if( lifetime_is_secure_element( lifetime ) )
267 {
268 /* Mbed Crypto currently always exposes the slot number to
269 * applications. This is not mandated by the PSA specification
270 * and may change in future versions. */
271 TEST_EQUAL( status, 0 );
272 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
273 }
274 else
275 {
276 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
277 }
278#endif
279
280 /* Type and size */
281 TEST_ASSERT( type != 0 );
282 TEST_ASSERT( bits != 0 );
283 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
284 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
285 TEST_ASSERT( bits % 8 == 0 );
286
287 /* MAX macros concerning specific key types */
288 if( PSA_KEY_TYPE_IS_ECC( type ) )
289 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
290 else if( PSA_KEY_TYPE_IS_RSA( type ) )
291 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
292 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) <= PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE );
293
294 ok = 1;
295
296exit:
297 psa_reset_key_attributes( &attributes );
298 return( ok );
299}
300
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100301int exercise_mac_setup( psa_key_type_t key_type,
302 const unsigned char *key_bytes,
303 size_t key_length,
304 psa_algorithm_t alg,
305 psa_mac_operation_t *operation,
306 psa_status_t *status )
307{
Ronald Cron5425a212020-08-04 14:58:35 +0200308 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200309 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100310
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100311 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200312 psa_set_key_algorithm( &attributes, alg );
313 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200314 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100315
Ronald Cron5425a212020-08-04 14:58:35 +0200316 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100317 /* Whether setup succeeded or failed, abort must succeed. */
318 PSA_ASSERT( psa_mac_abort( operation ) );
319 /* If setup failed, reproduce the failure, so that the caller can
320 * test the resulting state of the operation object. */
321 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100322 {
Ronald Cron5425a212020-08-04 14:58:35 +0200323 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100324 }
325
Ronald Cron5425a212020-08-04 14:58:35 +0200326 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100327 return( 1 );
328
329exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200330 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100331 return( 0 );
332}
333
334int exercise_cipher_setup( psa_key_type_t key_type,
335 const unsigned char *key_bytes,
336 size_t key_length,
337 psa_algorithm_t alg,
338 psa_cipher_operation_t *operation,
339 psa_status_t *status )
340{
Ronald Cron5425a212020-08-04 14:58:35 +0200341 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100343
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200344 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
345 psa_set_key_algorithm( &attributes, alg );
346 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200347 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100348
Ronald Cron5425a212020-08-04 14:58:35 +0200349 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100350 /* Whether setup succeeded or failed, abort must succeed. */
351 PSA_ASSERT( psa_cipher_abort( operation ) );
352 /* If setup failed, reproduce the failure, so that the caller can
353 * test the resulting state of the operation object. */
354 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100355 {
Ronald Cron5425a212020-08-04 14:58:35 +0200356 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100357 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100358 }
359
Ronald Cron5425a212020-08-04 14:58:35 +0200360 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100361 return( 1 );
362
363exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200364 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100365 return( 0 );
366}
367
Ronald Cron5425a212020-08-04 14:58:35 +0200368static int exercise_mac_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200369 psa_key_usage_t usage,
370 psa_algorithm_t alg )
371{
Jaeden Amero769ce272019-01-04 11:48:03 +0000372 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200373 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200374 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200375 size_t mac_length = sizeof( mac );
376
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100377 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200378 {
Ronald Cron5425a212020-08-04 14:58:35 +0200379 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100380 PSA_ASSERT( psa_mac_update( &operation,
381 input, sizeof( input ) ) );
382 PSA_ASSERT( psa_mac_sign_finish( &operation,
383 mac, sizeof( mac ),
384 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200385 }
386
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100387 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200388 {
389 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100390 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200391 PSA_SUCCESS :
392 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200393 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100394 PSA_ASSERT( psa_mac_update( &operation,
395 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100396 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
397 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200398 }
399
400 return( 1 );
401
402exit:
403 psa_mac_abort( &operation );
404 return( 0 );
405}
406
Ronald Cron5425a212020-08-04 14:58:35 +0200407static int exercise_cipher_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200408 psa_key_usage_t usage,
409 psa_algorithm_t alg )
410{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000411 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200412 unsigned char iv[16] = {0};
413 size_t iv_length = sizeof( iv );
414 const unsigned char plaintext[16] = "Hello, world...";
415 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
416 size_t ciphertext_length = sizeof( ciphertext );
417 unsigned char decrypted[sizeof( ciphertext )];
418 size_t part_length;
419
420 if( usage & PSA_KEY_USAGE_ENCRYPT )
421 {
Ronald Cron5425a212020-08-04 14:58:35 +0200422 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100423 PSA_ASSERT( psa_cipher_generate_iv( &operation,
424 iv, sizeof( iv ),
425 &iv_length ) );
426 PSA_ASSERT( psa_cipher_update( &operation,
427 plaintext, sizeof( plaintext ),
428 ciphertext, sizeof( ciphertext ),
429 &ciphertext_length ) );
430 PSA_ASSERT( psa_cipher_finish( &operation,
431 ciphertext + ciphertext_length,
432 sizeof( ciphertext ) - ciphertext_length,
433 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200434 ciphertext_length += part_length;
435 }
436
437 if( usage & PSA_KEY_USAGE_DECRYPT )
438 {
439 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200440 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200441 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
442 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200444 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200445 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
446 * have this macro yet. */
447 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
448 psa_get_key_type( &attributes ) );
449 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200450 }
Ronald Cron5425a212020-08-04 14:58:35 +0200451 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100452 PSA_ASSERT( psa_cipher_set_iv( &operation,
453 iv, iv_length ) );
454 PSA_ASSERT( psa_cipher_update( &operation,
455 ciphertext, ciphertext_length,
456 decrypted, sizeof( decrypted ),
457 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200458 status = psa_cipher_finish( &operation,
459 decrypted + part_length,
460 sizeof( decrypted ) - part_length,
461 &part_length );
462 /* For a stream cipher, all inputs are valid. For a block cipher,
463 * if the input is some aribtrary data rather than an actual
464 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200465 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200466 TEST_ASSERT( status == PSA_SUCCESS ||
467 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200468 else
469 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200470 }
471
472 return( 1 );
473
474exit:
475 psa_cipher_abort( &operation );
476 return( 0 );
477}
478
Ronald Cron5425a212020-08-04 14:58:35 +0200479static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200480 psa_key_usage_t usage,
481 psa_algorithm_t alg )
482{
483 unsigned char nonce[16] = {0};
484 size_t nonce_length = sizeof( nonce );
485 unsigned char plaintext[16] = "Hello, world...";
486 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
487 size_t ciphertext_length = sizeof( ciphertext );
488 size_t plaintext_length = sizeof( ciphertext );
489
490 if( usage & PSA_KEY_USAGE_ENCRYPT )
491 {
Ronald Cron5425a212020-08-04 14:58:35 +0200492 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100493 nonce, nonce_length,
494 NULL, 0,
495 plaintext, sizeof( plaintext ),
496 ciphertext, sizeof( ciphertext ),
497 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200498 }
499
500 if( usage & PSA_KEY_USAGE_DECRYPT )
501 {
502 psa_status_t verify_status =
503 ( usage & PSA_KEY_USAGE_ENCRYPT ?
504 PSA_SUCCESS :
505 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200506 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100507 nonce, nonce_length,
508 NULL, 0,
509 ciphertext, ciphertext_length,
510 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100511 &plaintext_length ),
512 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200513 }
514
515 return( 1 );
516
517exit:
518 return( 0 );
519}
520
Ronald Cron5425a212020-08-04 14:58:35 +0200521static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200522 psa_key_usage_t usage,
523 psa_algorithm_t alg )
524{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200525 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
526 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100527 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200528 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100529 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
530
531 /* If the policy allows signing with any hash, just pick one. */
532 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
533 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100534#if defined(KNOWN_SUPPORTED_HASH_ALG)
535 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
536 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100537#else
538 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100539 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100540#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100541 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200542
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100543 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200544 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200545 /* Some algorithms require the payload to have the size of
546 * the hash encoded in the algorithm. Use this input size
547 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200548 if( hash_alg != 0 )
549 payload_length = PSA_HASH_SIZE( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200550 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100551 payload, payload_length,
552 signature, sizeof( signature ),
553 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200554 }
555
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100556 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200557 {
558 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100559 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200560 PSA_SUCCESS :
561 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200562 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100563 payload, payload_length,
564 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100565 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200566 }
567
568 return( 1 );
569
570exit:
571 return( 0 );
572}
573
Ronald Cron5425a212020-08-04 14:58:35 +0200574static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200575 psa_key_usage_t usage,
576 psa_algorithm_t alg )
577{
578 unsigned char plaintext[256] = "Hello, world...";
579 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
580 size_t ciphertext_length = sizeof( ciphertext );
581 size_t plaintext_length = 16;
582
583 if( usage & PSA_KEY_USAGE_ENCRYPT )
584 {
Ronald Cron5425a212020-08-04 14:58:35 +0200585 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100586 plaintext, plaintext_length,
587 NULL, 0,
588 ciphertext, sizeof( ciphertext ),
589 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200590 }
591
592 if( usage & PSA_KEY_USAGE_DECRYPT )
593 {
594 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200595 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200596 ciphertext, ciphertext_length,
597 NULL, 0,
598 plaintext, sizeof( plaintext ),
599 &plaintext_length );
600 TEST_ASSERT( status == PSA_SUCCESS ||
601 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
602 ( status == PSA_ERROR_INVALID_ARGUMENT ||
603 status == PSA_ERROR_INVALID_PADDING ) ) );
604 }
605
606 return( 1 );
607
608exit:
609 return( 0 );
610}
Gilles Peskine02b75072018-07-01 22:31:34 +0200611
Janos Follathf2815ea2019-07-03 12:41:36 +0100612static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200613 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100614 psa_algorithm_t alg,
615 unsigned char* input1, size_t input1_length,
616 unsigned char* input2, size_t input2_length,
617 size_t capacity )
618{
619 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
620 if( PSA_ALG_IS_HKDF( alg ) )
621 {
622 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
623 PSA_KEY_DERIVATION_INPUT_SALT,
624 input1, input1_length ) );
625 PSA_ASSERT( psa_key_derivation_input_key( operation,
626 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200627 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100628 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
629 PSA_KEY_DERIVATION_INPUT_INFO,
630 input2,
631 input2_length ) );
632 }
633 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
634 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
635 {
636 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
637 PSA_KEY_DERIVATION_INPUT_SEED,
638 input1, input1_length ) );
639 PSA_ASSERT( psa_key_derivation_input_key( operation,
640 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200641 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100642 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
643 PSA_KEY_DERIVATION_INPUT_LABEL,
644 input2, input2_length ) );
645 }
646 else
647 {
648 TEST_ASSERT( ! "Key derivation algorithm not supported" );
649 }
650
Gilles Peskinec744d992019-07-30 17:26:54 +0200651 if( capacity != SIZE_MAX )
652 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100653
654 return( 1 );
655
656exit:
657 return( 0 );
658}
659
660
Ronald Cron5425a212020-08-04 14:58:35 +0200661static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200662 psa_key_usage_t usage,
663 psa_algorithm_t alg )
664{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200665 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100666 unsigned char input1[] = "Input 1";
667 size_t input1_length = sizeof( input1 );
668 unsigned char input2[] = "Input 2";
669 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200670 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100671 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200672
673 if( usage & PSA_KEY_USAGE_DERIVE )
674 {
Ronald Cron5425a212020-08-04 14:58:35 +0200675 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100676 input1, input1_length,
677 input2, input2_length, capacity ) )
678 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200679
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200680 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200681 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100682 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200683 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200684 }
685
686 return( 1 );
687
688exit:
689 return( 0 );
690}
691
Gilles Peskinec7998b72018-11-07 18:45:02 +0100692/* We need two keys to exercise key agreement. Exercise the
693 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200694static psa_status_t key_agreement_with_self(
695 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200696 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100697{
698 psa_key_type_t private_key_type;
699 psa_key_type_t public_key_type;
700 size_t key_bits;
701 uint8_t *public_key = NULL;
702 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200703 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200704 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
705 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200706 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100708
Ronald Cron5425a212020-08-04 14:58:35 +0200709 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200710 private_key_type = psa_get_key_type( &attributes );
711 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200712 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100713 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
714 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200715 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100716 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100717
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200718 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200719 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200720 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100721exit:
722 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200723 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100724 return( status );
725}
726
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200727/* We need two keys to exercise key agreement. Exercise the
728 * private key against its own public key. */
729static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200730 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200731{
732 psa_key_type_t private_key_type;
733 psa_key_type_t public_key_type;
734 size_t key_bits;
735 uint8_t *public_key = NULL;
736 size_t public_key_length;
737 uint8_t output[1024];
738 size_t output_length;
739 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200740 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
741 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200742 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200744
Ronald Cron5425a212020-08-04 14:58:35 +0200745 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200746 private_key_type = psa_get_key_type( &attributes );
747 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200748 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200749 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
750 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200751 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200752 public_key, public_key_length,
753 &public_key_length ) );
754
Ronald Cron5425a212020-08-04 14:58:35 +0200755 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200756 public_key, public_key_length,
757 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200758exit:
759 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200760 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200761 return( status );
762}
763
Ronald Cron5425a212020-08-04 14:58:35 +0200764static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200765 psa_key_usage_t usage,
766 psa_algorithm_t alg )
767{
768 int ok = 0;
769
770 if( usage & PSA_KEY_USAGE_DERIVE )
771 {
772 /* We need two keys to exercise key agreement. Exercise the
773 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200774 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200775 }
776 ok = 1;
777
778exit:
779 return( ok );
780}
781
Ronald Cron5425a212020-08-04 14:58:35 +0200782static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200783 psa_key_usage_t usage,
784 psa_algorithm_t alg )
785{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200786 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200787 unsigned char output[1];
788 int ok = 0;
789
790 if( usage & PSA_KEY_USAGE_DERIVE )
791 {
792 /* We need two keys to exercise key agreement. Exercise the
793 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200794 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200795 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200796 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200797 output,
798 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200799 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200800 }
801 ok = 1;
802
803exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200804 return( ok );
805}
806
Jaeden Amerof7dca862019-06-27 17:31:33 +0100807int asn1_skip_integer( unsigned char **p, const unsigned char *end,
808 size_t min_bits, size_t max_bits,
809 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200810{
811 size_t len;
812 size_t actual_bits;
813 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100814 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100815 MBEDTLS_ASN1_INTEGER ),
816 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200817
818 /* Check if the retrieved length doesn't extend the actual buffer's size.
819 * It is assumed here, that end >= p, which validates casting to size_t. */
820 TEST_ASSERT( len <= (size_t)( end - *p) );
821
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200822 /* Tolerate a slight departure from DER encoding:
823 * - 0 may be represented by an empty string or a 1-byte string.
824 * - The sign bit may be used as a value bit. */
825 if( ( len == 1 && ( *p )[0] == 0 ) ||
826 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
827 {
828 ++( *p );
829 --len;
830 }
831 if( min_bits == 0 && len == 0 )
832 return( 1 );
833 msb = ( *p )[0];
834 TEST_ASSERT( msb != 0 );
835 actual_bits = 8 * ( len - 1 );
836 while( msb != 0 )
837 {
838 msb >>= 1;
839 ++actual_bits;
840 }
841 TEST_ASSERT( actual_bits >= min_bits );
842 TEST_ASSERT( actual_bits <= max_bits );
843 if( must_be_odd )
844 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
845 *p += len;
846 return( 1 );
847exit:
848 return( 0 );
849}
850
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200851static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
852 uint8_t *exported, size_t exported_length )
853{
854 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100855 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200856 else
857 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200858
859#if defined(MBEDTLS_DES_C)
860 if( type == PSA_KEY_TYPE_DES )
861 {
862 /* Check the parity bits. */
863 unsigned i;
864 for( i = 0; i < bits / 8; i++ )
865 {
866 unsigned bit_count = 0;
867 unsigned m;
868 for( m = 1; m <= 0x100; m <<= 1 )
869 {
870 if( exported[i] & m )
871 ++bit_count;
872 }
873 TEST_ASSERT( bit_count % 2 != 0 );
874 }
875 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200876 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200877#endif
878
879#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200880 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200881 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200882 uint8_t *p = exported;
883 uint8_t *end = exported + exported_length;
884 size_t len;
885 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200886 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200887 * modulus INTEGER, -- n
888 * publicExponent INTEGER, -- e
889 * privateExponent INTEGER, -- d
890 * prime1 INTEGER, -- p
891 * prime2 INTEGER, -- q
892 * exponent1 INTEGER, -- d mod (p-1)
893 * exponent2 INTEGER, -- d mod (q-1)
894 * coefficient INTEGER, -- (inverse of q) mod p
895 * }
896 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100897 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
898 MBEDTLS_ASN1_SEQUENCE |
899 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
900 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200901 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
902 goto exit;
903 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
904 goto exit;
905 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
906 goto exit;
907 /* Require d to be at least half the size of n. */
908 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
909 goto exit;
910 /* Require p and q to be at most half the size of n, rounded up. */
911 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
912 goto exit;
913 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
914 goto exit;
915 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
916 goto exit;
917 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
918 goto exit;
919 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
920 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100921 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100922 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200923 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200924#endif /* MBEDTLS_RSA_C */
925
926#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200927 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200928 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100929 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100930 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100931 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200932 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200933#endif /* MBEDTLS_ECP_C */
934
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200935 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
936 {
937 uint8_t *p = exported;
938 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200939#if defined(MBEDTLS_RSA_C)
940 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
941 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100942 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200943 /* RSAPublicKey ::= SEQUENCE {
944 * modulus INTEGER, -- n
945 * publicExponent INTEGER } -- e
946 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100947 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
948 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100949 MBEDTLS_ASN1_CONSTRUCTED ),
950 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100951 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200952 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
953 goto exit;
954 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
955 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100956 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200957 }
958 else
959#endif /* MBEDTLS_RSA_C */
960#if defined(MBEDTLS_ECP_C)
961 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
962 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200963 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
964 {
965 /* The representation of an ECC Montgomery public key is
966 * the raw compressed point */
967 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
968 }
969 else
970 {
971 /* The representation of an ECC Weierstrass public key is:
972 * - The byte 0x04;
973 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
974 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
975 * - where m is the bit size associated with the curve.
976 */
977 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
978 TEST_EQUAL( p[0], 4 );
979 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200980 }
981 else
982#endif /* MBEDTLS_ECP_C */
983 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100984 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200985 mbedtls_snprintf( message, sizeof( message ),
986 "No sanity check for public key type=0x%08lx",
987 (unsigned long) type );
988 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200989 (void) p;
990 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200991 return( 0 );
992 }
993 }
994 else
995
996 {
997 /* No sanity checks for other types */
998 }
999
1000 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001001
1002exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001003 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001004}
1005
Ronald Cron5425a212020-08-04 14:58:35 +02001006static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001007 psa_key_usage_t usage )
1008{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001009 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001010 uint8_t *exported = NULL;
1011 size_t exported_size = 0;
1012 size_t exported_length = 0;
1013 int ok = 0;
1014
Ronald Cron5425a212020-08-04 14:58:35 +02001015 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001016
1017 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001018 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001019 {
Ronald Cron5425a212020-08-04 14:58:35 +02001020 TEST_EQUAL( psa_export_key( key, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001021 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001022 ok = 1;
1023 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001024 }
1025
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001026 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
1027 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001028 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001029
Ronald Cron5425a212020-08-04 14:58:35 +02001030 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001031 exported, exported_size,
1032 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001033 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1034 psa_get_key_bits( &attributes ),
1035 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001036
1037exit:
1038 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001039 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001040 return( ok );
1041}
1042
Ronald Cron5425a212020-08-04 14:58:35 +02001043static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001044{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001045 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001046 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001047 uint8_t *exported = NULL;
1048 size_t exported_size = 0;
1049 size_t exported_length = 0;
1050 int ok = 0;
1051
Ronald Cron5425a212020-08-04 14:58:35 +02001052 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001053 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001054 {
Ronald Cron5425a212020-08-04 14:58:35 +02001055 TEST_EQUAL( psa_export_public_key( key, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001056 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +02001057 return( 1 );
1058 }
1059
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001060 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001061 psa_get_key_type( &attributes ) );
1062 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
1063 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001064 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001065
Ronald Cron5425a212020-08-04 14:58:35 +02001066 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001067 exported, exported_size,
1068 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001069 ok = exported_key_sanity_check( public_type,
1070 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001071 exported, exported_length );
1072
1073exit:
1074 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001075 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001076 return( ok );
1077}
1078
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001079/** Do smoke tests on a key.
1080 *
1081 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1082 * sign/verify, or derivation) that is permitted according to \p usage.
1083 * \p usage and \p alg should correspond to the expected policy on the
1084 * key.
1085 *
1086 * Export the key if permitted by \p usage, and check that the output
1087 * looks sensible. If \p usage forbids export, check that
1088 * \p psa_export_key correctly rejects the attempt. If the key is
1089 * asymmetric, also check \p psa_export_public_key.
1090 *
1091 * If the key fails the tests, this function calls the test framework's
1092 * `test_fail` function and returns false. Otherwise this function returns
1093 * true. Therefore it should be used as follows:
1094 * ```
1095 * if( ! exercise_key( ... ) ) goto exit;
1096 * ```
1097 *
Ronald Cron5425a212020-08-04 14:58:35 +02001098 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001099 * \p alg.
1100 * \param usage The usage flags to assume.
1101 * \param alg The algorithm to exercise.
1102 *
1103 * \retval 0 The key failed the smoke tests.
1104 * \retval 1 The key passed the smoke tests.
1105 */
Ronald Cron5425a212020-08-04 14:58:35 +02001106static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001107 psa_key_usage_t usage,
1108 psa_algorithm_t alg )
1109{
1110 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001111
Ronald Cron5425a212020-08-04 14:58:35 +02001112 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001113 return( 0 );
1114
Gilles Peskine02b75072018-07-01 22:31:34 +02001115 if( alg == 0 )
1116 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1117 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001118 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001119 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001120 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001121 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001122 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001123 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001124 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001125 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001126 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001127 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001128 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001129 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001130 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001131 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001132 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001133 else
1134 {
1135 char message[40];
1136 mbedtls_snprintf( message, sizeof( message ),
1137 "No code to exercise alg=0x%08lx",
1138 (unsigned long) alg );
1139 test_fail( message, __LINE__, __FILE__ );
1140 ok = 0;
1141 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001142
Ronald Cron5425a212020-08-04 14:58:35 +02001143 ok = ok && exercise_export_key( key, usage );
1144 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001145
Gilles Peskine02b75072018-07-01 22:31:34 +02001146 return( ok );
1147}
1148
Gilles Peskine10df3412018-10-25 22:35:43 +02001149static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1150 psa_algorithm_t alg )
1151{
1152 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1153 {
1154 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001155 PSA_KEY_USAGE_VERIFY_HASH :
1156 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001157 }
1158 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1159 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1160 {
1161 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1162 PSA_KEY_USAGE_ENCRYPT :
1163 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1164 }
1165 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1166 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1167 {
1168 return( PSA_KEY_USAGE_DERIVE );
1169 }
1170 else
1171 {
1172 return( 0 );
1173 }
1174
1175}
Darryl Green0c6575a2018-11-07 16:05:30 +00001176
Ronald Cron5425a212020-08-04 14:58:35 +02001177static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001178{
1179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001180 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001181 uint8_t buffer[1];
1182 size_t length;
1183 int ok = 0;
1184
Ronald Cronecfb2372020-07-23 17:13:42 +02001185 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1187 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1188 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001189 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001190 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001191 TEST_EQUAL(
1192 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1193 TEST_EQUAL(
1194 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001195 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001196 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1197 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1198 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1199 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1200
Ronald Cron5425a212020-08-04 14:58:35 +02001201 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001202 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001203 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001204 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001205 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001206
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001207 ok = 1;
1208
1209exit:
1210 psa_reset_key_attributes( &attributes );
1211 return( ok );
1212}
1213
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001214/* Assert that a key isn't reported as having a slot number. */
1215#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1216#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1217 do \
1218 { \
1219 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1220 TEST_EQUAL( psa_get_key_slot_number( \
1221 attributes, \
1222 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1223 PSA_ERROR_INVALID_ARGUMENT ); \
1224 } \
1225 while( 0 )
1226#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1227#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1228 ( (void) 0 )
1229#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1230
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001231/* An overapproximation of the amount of storage needed for a key of the
1232 * given type and with the given content. The API doesn't make it easy
1233 * to find a good value for the size. The current implementation doesn't
1234 * care about the value anyway. */
1235#define KEY_BITS_FROM_DATA( type, data ) \
1236 ( data )->len
1237
Darryl Green0c6575a2018-11-07 16:05:30 +00001238typedef enum {
1239 IMPORT_KEY = 0,
1240 GENERATE_KEY = 1,
1241 DERIVE_KEY = 2
1242} generate_method;
1243
Gilles Peskinee59236f2018-01-27 23:32:46 +01001244/* END_HEADER */
1245
1246/* BEGIN_DEPENDENCIES
1247 * depends_on:MBEDTLS_PSA_CRYPTO_C
1248 * END_DEPENDENCIES
1249 */
1250
1251/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001252void static_checks( )
1253{
1254 size_t max_truncated_mac_size =
1255 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1256
1257 /* Check that the length for a truncated MAC always fits in the algorithm
1258 * encoding. The shifted mask is the maximum truncated value. The
1259 * untruncated algorithm may be one byte larger. */
1260 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001261
1262#if defined(MBEDTLS_TEST_DEPRECATED)
1263 /* Check deprecated constants. */
1264 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1265 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1266 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1267 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1268 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1269 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1270 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1271 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001272
Paul Elliott8ff510a2020-06-02 17:19:28 +01001273 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1274 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1275 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1276 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1277 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1278 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1279 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1280 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1281 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1282 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1283 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1284 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1285 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1286 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1287 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1288 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1289 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1290 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1291 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1292 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1293 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1294 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1295 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1296 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1297 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1298 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1299 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1300 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1301 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1302 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1303
1304 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1305 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1306 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1308 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1309 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1310 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1311 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001312
Paul Elliott75e27032020-06-03 15:17:39 +01001313 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1314 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1315 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1316 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1317 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1318
1319 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1320 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001321#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001322}
1323/* END_CASE */
1324
1325/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001326void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001327 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001328 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329{
Gilles Peskine4747d192019-04-17 15:05:45 +02001330 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001331 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001332 psa_key_lifetime_t lifetime = lifetime_arg;
1333 psa_key_usage_t usage_flags = usage_flags_arg;
1334 psa_algorithm_t alg = alg_arg;
1335 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001336 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001337
Ronald Cronecfb2372020-07-23 17:13:42 +02001338 TEST_EQUAL(
1339 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1340 TEST_EQUAL(
1341 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001342 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1343 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1344 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1345 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001346 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001347
Gilles Peskinec87af662019-05-15 16:12:22 +02001348 psa_set_key_id( &attributes, id );
1349 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001350 psa_set_key_usage_flags( &attributes, usage_flags );
1351 psa_set_key_algorithm( &attributes, alg );
1352 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001353 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001354
Ronald Cronecfb2372020-07-23 17:13:42 +02001355 TEST_ASSERT( mbedtls_svc_key_id_equal(
1356 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001357 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1358 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1359 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1360 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001361 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001362
1363 psa_reset_key_attributes( &attributes );
1364
Ronald Cronecfb2372020-07-23 17:13:42 +02001365 TEST_EQUAL(
1366 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1367 TEST_EQUAL(
1368 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001369 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1370 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1371 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1372 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001373 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001374}
1375/* END_CASE */
1376
1377/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001378void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1379 int id2_arg, int owner_id2_arg,
1380 int expected_id_arg, int expected_owner_id_arg,
1381 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001382{
1383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001384 mbedtls_svc_key_id_t id1 =
1385 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001386 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001387 mbedtls_svc_key_id_t id2 =
1388 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001389 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001390 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001391 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1392
1393 if( id1_arg != -1 )
1394 psa_set_key_id( &attributes, id1 );
1395 if( lifetime_arg != -1 )
1396 psa_set_key_lifetime( &attributes, lifetime );
1397 if( id2_arg != -1 )
1398 psa_set_key_id( &attributes, id2 );
1399
Ronald Cronecfb2372020-07-23 17:13:42 +02001400 TEST_ASSERT( mbedtls_svc_key_id_equal(
1401 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001402 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1403}
1404/* END_CASE */
1405
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001406/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1407void slot_number_attribute( )
1408{
1409 psa_key_slot_number_t slot_number = 0xdeadbeef;
1410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1411
1412 /* Initially, there is no slot number. */
1413 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1414 PSA_ERROR_INVALID_ARGUMENT );
1415
1416 /* Test setting a slot number. */
1417 psa_set_key_slot_number( &attributes, 0 );
1418 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1419 TEST_EQUAL( slot_number, 0 );
1420
1421 /* Test changing the slot number. */
1422 psa_set_key_slot_number( &attributes, 42 );
1423 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1424 TEST_EQUAL( slot_number, 42 );
1425
1426 /* Test clearing the slot number. */
1427 psa_clear_key_slot_number( &attributes );
1428 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1429 PSA_ERROR_INVALID_ARGUMENT );
1430
1431 /* Clearing again should have no effect. */
1432 psa_clear_key_slot_number( &attributes );
1433 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1434 PSA_ERROR_INVALID_ARGUMENT );
1435
1436 /* Test that reset clears the slot number. */
1437 psa_set_key_slot_number( &attributes, 42 );
1438 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1439 TEST_EQUAL( slot_number, 42 );
1440 psa_reset_key_attributes( &attributes );
1441 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1442 PSA_ERROR_INVALID_ARGUMENT );
1443}
1444/* END_CASE */
1445
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001446/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001447void import_with_policy( int type_arg,
1448 int usage_arg, int alg_arg,
1449 int expected_status_arg )
1450{
1451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1452 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001454 psa_key_type_t type = type_arg;
1455 psa_key_usage_t usage = usage_arg;
1456 psa_algorithm_t alg = alg_arg;
1457 psa_status_t expected_status = expected_status_arg;
1458 const uint8_t key_material[16] = {0};
1459 psa_status_t status;
1460
1461 PSA_ASSERT( psa_crypto_init( ) );
1462
1463 psa_set_key_type( &attributes, type );
1464 psa_set_key_usage_flags( &attributes, usage );
1465 psa_set_key_algorithm( &attributes, alg );
1466
1467 status = psa_import_key( &attributes,
1468 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001469 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001470 TEST_EQUAL( status, expected_status );
1471 if( status != PSA_SUCCESS )
1472 goto exit;
1473
Ronald Cron5425a212020-08-04 14:58:35 +02001474 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001475 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1476 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1477 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001478 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001479
Ronald Cron5425a212020-08-04 14:58:35 +02001480 PSA_ASSERT( psa_destroy_key( key ) );
1481 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001482
1483exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001484 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001485 psa_reset_key_attributes( &got_attributes );
1486 PSA_DONE( );
1487}
1488/* END_CASE */
1489
1490/* BEGIN_CASE */
1491void import_with_data( data_t *data, int type_arg,
1492 int attr_bits_arg,
1493 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001494{
1495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1496 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001497 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001498 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001499 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001500 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001501 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001502
Gilles Peskine8817f612018-12-18 00:18:46 +01001503 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001504
Gilles Peskine4747d192019-04-17 15:05:45 +02001505 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001506 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001507
Ronald Cron5425a212020-08-04 14:58:35 +02001508 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001509 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001510 if( status != PSA_SUCCESS )
1511 goto exit;
1512
Ronald Cron5425a212020-08-04 14:58:35 +02001513 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001514 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001515 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001516 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001517 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001518
Ronald Cron5425a212020-08-04 14:58:35 +02001519 PSA_ASSERT( psa_destroy_key( key ) );
1520 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001521
1522exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001523 psa_destroy_key( key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001524 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001525 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001526}
1527/* END_CASE */
1528
1529/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001530void import_large_key( int type_arg, int byte_size_arg,
1531 int expected_status_arg )
1532{
1533 psa_key_type_t type = type_arg;
1534 size_t byte_size = byte_size_arg;
1535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1536 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001537 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001538 psa_status_t status;
1539 uint8_t *buffer = NULL;
1540 size_t buffer_size = byte_size + 1;
1541 size_t n;
1542
1543 /* It would be better to skip the test than fail it if the allocation
1544 * fails, but the test framework doesn't support this yet. */
1545 ASSERT_ALLOC( buffer, buffer_size );
1546 memset( buffer, 'K', byte_size );
1547
1548 PSA_ASSERT( psa_crypto_init( ) );
1549
1550 /* Try importing the key */
1551 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1552 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001553 status = psa_import_key( &attributes, buffer, byte_size, &key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001554 TEST_EQUAL( status, expected_status );
1555
1556 if( status == PSA_SUCCESS )
1557 {
Ronald Cron5425a212020-08-04 14:58:35 +02001558 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001559 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1560 TEST_EQUAL( psa_get_key_bits( &attributes ),
1561 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001562 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001563 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001564 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001565 for( n = 0; n < byte_size; n++ )
1566 TEST_EQUAL( buffer[n], 'K' );
1567 for( n = byte_size; n < buffer_size; n++ )
1568 TEST_EQUAL( buffer[n], 0 );
1569 }
1570
1571exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001572 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001573 PSA_DONE( );
1574 mbedtls_free( buffer );
1575}
1576/* END_CASE */
1577
1578/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001579void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1580{
Ronald Cron5425a212020-08-04 14:58:35 +02001581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001582 size_t bits = bits_arg;
1583 psa_status_t expected_status = expected_status_arg;
1584 psa_status_t status;
1585 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001586 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001587 size_t buffer_size = /* Slight overapproximations */
1588 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001589 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001590 unsigned char *p;
1591 int ret;
1592 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001594
Gilles Peskine8817f612018-12-18 00:18:46 +01001595 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001596 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001597
1598 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1599 bits, keypair ) ) >= 0 );
1600 length = ret;
1601
1602 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001603 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001604 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001605 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001606
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001607 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001608 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001609
1610exit:
1611 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001612 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001613}
1614/* END_CASE */
1615
1616/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001617void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001618 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001619 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001620 int expected_bits,
1621 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001622 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001623 int canonical_input )
1624{
Ronald Cron5425a212020-08-04 14:58:35 +02001625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001626 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001627 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001628 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001629 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001630 unsigned char *exported = NULL;
1631 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001632 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001633 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001634 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001636 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001637
Moran Pekercb088e72018-07-17 17:36:59 +03001638 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001639 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001640 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001641 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001642 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001643
Gilles Peskine4747d192019-04-17 15:05:45 +02001644 psa_set_key_usage_flags( &attributes, usage_arg );
1645 psa_set_key_algorithm( &attributes, alg );
1646 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001647
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001648 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001649 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001650
1651 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001652 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001653 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1654 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001655 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001656
1657 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001658 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001659 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001660
1661 /* The exported length must be set by psa_export_key() to a value between 0
1662 * and export_size. On errors, the exported length must be 0. */
1663 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1664 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1665 TEST_ASSERT( exported_length <= export_size );
1666
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001667 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001668 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001669 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001670 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001671 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001672 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001673 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001674
Ronald Cron5425a212020-08-04 14:58:35 +02001675 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001676 goto exit;
1677
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001678 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001679 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001680 else
1681 {
Ronald Cron5425a212020-08-04 14:58:35 +02001682 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001683 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001684 &key2 ) );
1685 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001686 reexported,
1687 export_size,
1688 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001689 ASSERT_COMPARE( exported, exported_length,
1690 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001691 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001692 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001693 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001694
1695destroy:
1696 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001697 PSA_ASSERT( psa_destroy_key( key ) );
1698 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001699
1700exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001701 mbedtls_free( exported );
1702 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001703 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001704 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001705}
1706/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001707
Moran Pekerf709f4a2018-06-06 17:26:04 +03001708/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001709void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001710 int type_arg,
1711 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001712 int export_size_delta,
1713 int expected_export_status_arg,
1714 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001715{
Ronald Cron5425a212020-08-04 14:58:35 +02001716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001717 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001718 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001719 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001720 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001721 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001722 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001723 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001725
Gilles Peskine8817f612018-12-18 00:18:46 +01001726 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001727
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1729 psa_set_key_algorithm( &attributes, alg );
1730 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001731
1732 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001733 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001734
Gilles Peskine49c25912018-10-29 15:15:31 +01001735 /* Export the public key */
1736 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001737 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001738 exported, export_size,
1739 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001740 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001741 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001742 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001743 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001744 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001745 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001746 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001747 TEST_ASSERT( expected_public_key->len <=
1748 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001749 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1750 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001751 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001752
1753exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001754 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001755 psa_destroy_key( key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001756 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001757 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001758}
1759/* END_CASE */
1760
Gilles Peskine20035e32018-02-03 22:44:14 +01001761/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001762void import_and_exercise_key( data_t *data,
1763 int type_arg,
1764 int bits_arg,
1765 int alg_arg )
1766{
Ronald Cron5425a212020-08-04 14:58:35 +02001767 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001768 psa_key_type_t type = type_arg;
1769 size_t bits = bits_arg;
1770 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001771 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001773 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001774
Gilles Peskine8817f612018-12-18 00:18:46 +01001775 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001776
Gilles Peskine4747d192019-04-17 15:05:45 +02001777 psa_set_key_usage_flags( &attributes, usage );
1778 psa_set_key_algorithm( &attributes, alg );
1779 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001780
1781 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001782 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001783
1784 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001785 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001786 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1787 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001788
1789 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001790 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001791 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001792
Ronald Cron5425a212020-08-04 14:58:35 +02001793 PSA_ASSERT( psa_destroy_key( key ) );
1794 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001795
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001796exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001797 psa_destroy_key( key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001798 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001799 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001800}
1801/* END_CASE */
1802
1803/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001804void effective_key_attributes( int type_arg, int expected_type_arg,
1805 int bits_arg, int expected_bits_arg,
1806 int usage_arg, int expected_usage_arg,
1807 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001808{
Ronald Cron5425a212020-08-04 14:58:35 +02001809 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001810 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001811 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001812 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001813 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001814 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001815 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001816 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001817 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001819
Gilles Peskine8817f612018-12-18 00:18:46 +01001820 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001821
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001822 psa_set_key_usage_flags( &attributes, usage );
1823 psa_set_key_algorithm( &attributes, alg );
1824 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001825 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001826
Ronald Cron5425a212020-08-04 14:58:35 +02001827 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001828 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001829
Ronald Cron5425a212020-08-04 14:58:35 +02001830 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001831 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1832 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1833 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1834 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001835
1836exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001837 psa_destroy_key( key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001838 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001839 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001840}
1841/* END_CASE */
1842
1843/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001844void check_key_policy( int type_arg, int bits_arg,
1845 int usage_arg, int alg_arg )
1846{
1847 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1848 usage_arg, usage_arg, alg_arg, alg_arg );
1849 goto exit;
1850}
1851/* END_CASE */
1852
1853/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001854void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001855{
1856 /* Test each valid way of initializing the object, except for `= {0}`, as
1857 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1858 * though it's OK by the C standard. We could test for this, but we'd need
1859 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001860 psa_key_attributes_t func = psa_key_attributes_init( );
1861 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1862 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001863
1864 memset( &zero, 0, sizeof( zero ) );
1865
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001866 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1867 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1868 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001869
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001870 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1871 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1872 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1873
1874 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1875 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1876 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1877
1878 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1879 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1880 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1881
1882 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1883 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1884 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001885}
1886/* END_CASE */
1887
1888/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001889void mac_key_policy( int policy_usage,
1890 int policy_alg,
1891 int key_type,
1892 data_t *key_data,
1893 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001894{
Ronald Cron5425a212020-08-04 14:58:35 +02001895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001897 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001898 psa_status_t status;
1899 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001900
Gilles Peskine8817f612018-12-18 00:18:46 +01001901 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001902
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001903 psa_set_key_usage_flags( &attributes, policy_usage );
1904 psa_set_key_algorithm( &attributes, policy_alg );
1905 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001906
Gilles Peskine049c7532019-05-15 20:22:09 +02001907 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001908 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001909
Ronald Cron5425a212020-08-04 14:58:35 +02001910 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001912 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001913 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001915 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001916 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001917
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001919 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001921 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001922 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001923 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001924 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001925
1926exit:
1927 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001928 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001929 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001930}
1931/* END_CASE */
1932
1933/* BEGIN_CASE */
1934void cipher_key_policy( int policy_usage,
1935 int policy_alg,
1936 int key_type,
1937 data_t *key_data,
1938 int exercise_alg )
1939{
Ronald Cron5425a212020-08-04 14:58:35 +02001940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001942 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001943 psa_status_t status;
1944
Gilles Peskine8817f612018-12-18 00:18:46 +01001945 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001947 psa_set_key_usage_flags( &attributes, policy_usage );
1948 psa_set_key_algorithm( &attributes, policy_alg );
1949 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001950
Gilles Peskine049c7532019-05-15 20:22:09 +02001951 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001952 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953
Ronald Cron5425a212020-08-04 14:58:35 +02001954 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955 if( policy_alg == exercise_alg &&
1956 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001957 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001959 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001960 psa_cipher_abort( &operation );
1961
Ronald Cron5425a212020-08-04 14:58:35 +02001962 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001963 if( policy_alg == exercise_alg &&
1964 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001965 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001966 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001967 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001968
1969exit:
1970 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001971 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001972 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001973}
1974/* END_CASE */
1975
1976/* BEGIN_CASE */
1977void aead_key_policy( int policy_usage,
1978 int policy_alg,
1979 int key_type,
1980 data_t *key_data,
1981 int nonce_length_arg,
1982 int tag_length_arg,
1983 int exercise_alg )
1984{
Ronald Cron5425a212020-08-04 14:58:35 +02001985 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001987 psa_status_t status;
1988 unsigned char nonce[16] = {0};
1989 size_t nonce_length = nonce_length_arg;
1990 unsigned char tag[16];
1991 size_t tag_length = tag_length_arg;
1992 size_t output_length;
1993
1994 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1995 TEST_ASSERT( tag_length <= sizeof( tag ) );
1996
Gilles Peskine8817f612018-12-18 00:18:46 +01001997 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001998
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001999 psa_set_key_usage_flags( &attributes, policy_usage );
2000 psa_set_key_algorithm( &attributes, policy_alg );
2001 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002
Gilles Peskine049c7532019-05-15 20:22:09 +02002003 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002004 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002005
Ronald Cron5425a212020-08-04 14:58:35 +02002006 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002007 nonce, nonce_length,
2008 NULL, 0,
2009 NULL, 0,
2010 tag, tag_length,
2011 &output_length );
2012 if( policy_alg == exercise_alg &&
2013 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002014 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002015 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002016 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017
2018 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002019 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020 nonce, nonce_length,
2021 NULL, 0,
2022 tag, tag_length,
2023 NULL, 0,
2024 &output_length );
2025 if( policy_alg == exercise_alg &&
2026 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002027 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002029 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030
2031exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002032 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002033 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034}
2035/* END_CASE */
2036
2037/* BEGIN_CASE */
2038void asymmetric_encryption_key_policy( int policy_usage,
2039 int policy_alg,
2040 int key_type,
2041 data_t *key_data,
2042 int exercise_alg )
2043{
Ronald Cron5425a212020-08-04 14:58:35 +02002044 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002045 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046 psa_status_t status;
2047 size_t key_bits;
2048 size_t buffer_length;
2049 unsigned char *buffer = NULL;
2050 size_t output_length;
2051
Gilles Peskine8817f612018-12-18 00:18:46 +01002052 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002053
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002054 psa_set_key_usage_flags( &attributes, policy_usage );
2055 psa_set_key_algorithm( &attributes, policy_alg );
2056 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002057
Gilles Peskine049c7532019-05-15 20:22:09 +02002058 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002059 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002060
Ronald Cron5425a212020-08-04 14:58:35 +02002061 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002062 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002063 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2064 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002065 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066
Ronald Cron5425a212020-08-04 14:58:35 +02002067 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002068 NULL, 0,
2069 NULL, 0,
2070 buffer, buffer_length,
2071 &output_length );
2072 if( policy_alg == exercise_alg &&
2073 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002074 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002076 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002078 if( buffer_length != 0 )
2079 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002080 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081 buffer, buffer_length,
2082 NULL, 0,
2083 buffer, buffer_length,
2084 &output_length );
2085 if( policy_alg == exercise_alg &&
2086 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002087 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002088 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002089 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090
2091exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002092 psa_destroy_key( key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002093 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002094 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002095 mbedtls_free( buffer );
2096}
2097/* END_CASE */
2098
2099/* BEGIN_CASE */
2100void asymmetric_signature_key_policy( int policy_usage,
2101 int policy_alg,
2102 int key_type,
2103 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002104 int exercise_alg,
2105 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002106{
Ronald Cron5425a212020-08-04 14:58:35 +02002107 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002109 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002110 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2111 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2112 * compatible with the policy and `payload_length_arg` is supposed to be
2113 * a valid input length to sign. If `payload_length_arg <= 0`,
2114 * `exercise_alg` is supposed to be forbidden by the policy. */
2115 int compatible_alg = payload_length_arg > 0;
2116 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002117 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118 size_t signature_length;
2119
Gilles Peskine8817f612018-12-18 00:18:46 +01002120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002122 psa_set_key_usage_flags( &attributes, policy_usage );
2123 psa_set_key_algorithm( &attributes, policy_alg );
2124 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
Gilles Peskine049c7532019-05-15 20:22:09 +02002126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002127 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128
Ronald Cron5425a212020-08-04 14:58:35 +02002129 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002130 payload, payload_length,
2131 signature, sizeof( signature ),
2132 &signature_length );
2133 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002134 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002135 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002136 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002137
2138 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002139 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002140 payload, payload_length,
2141 signature, sizeof( signature ) );
2142 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002143 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
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 Peskined5b33222018-06-18 22:20:03 +02002146
2147exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002148 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002149 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002150}
2151/* END_CASE */
2152
Janos Follathba3fab92019-06-11 14:50:16 +01002153/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002154void derive_key_policy( int policy_usage,
2155 int policy_alg,
2156 int key_type,
2157 data_t *key_data,
2158 int exercise_alg )
2159{
Ronald Cron5425a212020-08-04 14:58:35 +02002160 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002161 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002162 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002163 psa_status_t status;
2164
Gilles Peskine8817f612018-12-18 00:18:46 +01002165 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002166
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002167 psa_set_key_usage_flags( &attributes, policy_usage );
2168 psa_set_key_algorithm( &attributes, policy_alg );
2169 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002170
Gilles Peskine049c7532019-05-15 20:22:09 +02002171 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002172 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002173
Janos Follathba3fab92019-06-11 14:50:16 +01002174 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2175
2176 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2177 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002178 {
Janos Follathba3fab92019-06-11 14:50:16 +01002179 PSA_ASSERT( psa_key_derivation_input_bytes(
2180 &operation,
2181 PSA_KEY_DERIVATION_INPUT_SEED,
2182 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002183 }
Janos Follathba3fab92019-06-11 14:50:16 +01002184
2185 status = psa_key_derivation_input_key( &operation,
2186 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002187 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002188
Gilles Peskineea0fb492018-07-12 17:17:20 +02002189 if( policy_alg == exercise_alg &&
2190 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002191 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002192 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002193 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002194
2195exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002196 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002197 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002198 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002199}
2200/* END_CASE */
2201
2202/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002203void agreement_key_policy( int policy_usage,
2204 int policy_alg,
2205 int key_type_arg,
2206 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002207 int exercise_alg,
2208 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002209{
Ronald Cron5425a212020-08-04 14:58:35 +02002210 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002212 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002213 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002214 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002215 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002216
Gilles Peskine8817f612018-12-18 00:18:46 +01002217 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002218
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002219 psa_set_key_usage_flags( &attributes, policy_usage );
2220 psa_set_key_algorithm( &attributes, policy_alg );
2221 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002222
Gilles Peskine049c7532019-05-15 20:22:09 +02002223 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002224 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002225
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002226 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002227 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002228
Steven Cooremance48e852020-10-05 16:02:45 +02002229 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002230
2231exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002232 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002233 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002234 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002235}
2236/* END_CASE */
2237
2238/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002239void key_policy_alg2( int key_type_arg, data_t *key_data,
2240 int usage_arg, int alg_arg, int alg2_arg )
2241{
Ronald Cron5425a212020-08-04 14:58:35 +02002242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002243 psa_key_type_t key_type = key_type_arg;
2244 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2245 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2246 psa_key_usage_t usage = usage_arg;
2247 psa_algorithm_t alg = alg_arg;
2248 psa_algorithm_t alg2 = alg2_arg;
2249
2250 PSA_ASSERT( psa_crypto_init( ) );
2251
2252 psa_set_key_usage_flags( &attributes, usage );
2253 psa_set_key_algorithm( &attributes, alg );
2254 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2255 psa_set_key_type( &attributes, key_type );
2256 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002257 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002258
Ronald Cron5425a212020-08-04 14:58:35 +02002259 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002260 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2261 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2262 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2263
Ronald Cron5425a212020-08-04 14:58:35 +02002264 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002265 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002266 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002267 goto exit;
2268
2269exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002270 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002271 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002272}
2273/* END_CASE */
2274
2275/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002276void raw_agreement_key_policy( int policy_usage,
2277 int policy_alg,
2278 int key_type_arg,
2279 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002280 int exercise_alg,
2281 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002282{
Ronald Cron5425a212020-08-04 14:58:35 +02002283 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002284 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002285 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002286 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002287 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002288 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002289
2290 PSA_ASSERT( psa_crypto_init( ) );
2291
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002292 psa_set_key_usage_flags( &attributes, policy_usage );
2293 psa_set_key_algorithm( &attributes, policy_alg );
2294 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002295
Gilles Peskine049c7532019-05-15 20:22:09 +02002296 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002297 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002298
Ronald Cron5425a212020-08-04 14:58:35 +02002299 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002300
Steven Cooremance48e852020-10-05 16:02:45 +02002301 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002302
2303exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002304 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002305 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002306 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002307}
2308/* END_CASE */
2309
2310/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002311void copy_success( int source_usage_arg,
2312 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002313 int type_arg, data_t *material,
2314 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002315 int target_usage_arg,
2316 int target_alg_arg, int target_alg2_arg,
2317 int expected_usage_arg,
2318 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002319{
Gilles Peskineca25db92019-04-19 11:43:08 +02002320 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2321 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002322 psa_key_usage_t expected_usage = expected_usage_arg;
2323 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002324 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002325 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2326 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002327 uint8_t *export_buffer = NULL;
2328
Gilles Peskine57ab7212019-01-28 13:03:09 +01002329 PSA_ASSERT( psa_crypto_init( ) );
2330
Gilles Peskineca25db92019-04-19 11:43:08 +02002331 /* Prepare the source key. */
2332 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2333 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002334 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002335 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002336 PSA_ASSERT( psa_import_key( &source_attributes,
2337 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002338 &source_key ) );
2339 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002340
Gilles Peskineca25db92019-04-19 11:43:08 +02002341 /* Prepare the target attributes. */
2342 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002343 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002344 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002345 /* Set volatile lifetime to reset the key identifier to 0. */
2346 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2347 }
2348
Gilles Peskineca25db92019-04-19 11:43:08 +02002349 if( target_usage_arg != -1 )
2350 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2351 if( target_alg_arg != -1 )
2352 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002353 if( target_alg2_arg != -1 )
2354 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002355
2356 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002357 PSA_ASSERT( psa_copy_key( source_key,
2358 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002359
2360 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002361 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002362
2363 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002364 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002365 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2366 psa_get_key_type( &target_attributes ) );
2367 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2368 psa_get_key_bits( &target_attributes ) );
2369 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2370 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002371 TEST_EQUAL( expected_alg2,
2372 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002373 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2374 {
2375 size_t length;
2376 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002377 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002378 material->len, &length ) );
2379 ASSERT_COMPARE( material->x, material->len,
2380 export_buffer, length );
2381 }
Ronald Cron5425a212020-08-04 14:58:35 +02002382 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002383 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002384 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002385 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002386
Ronald Cron5425a212020-08-04 14:58:35 +02002387 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002388
2389exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002390 psa_reset_key_attributes( &source_attributes );
2391 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002392 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002393 mbedtls_free( export_buffer );
2394}
2395/* END_CASE */
2396
2397/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002398void copy_fail( int source_usage_arg,
2399 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002400 int type_arg, data_t *material,
2401 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002402 int target_usage_arg,
2403 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002404 int expected_status_arg )
2405{
2406 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2407 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002408 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2409 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002410
2411 PSA_ASSERT( psa_crypto_init( ) );
2412
2413 /* Prepare the source key. */
2414 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2415 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002416 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002417 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002418 PSA_ASSERT( psa_import_key( &source_attributes,
2419 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002420 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002421
2422 /* Prepare the target attributes. */
2423 psa_set_key_type( &target_attributes, target_type_arg );
2424 psa_set_key_bits( &target_attributes, target_bits_arg );
2425 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2426 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002427 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002428
2429 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002430 TEST_EQUAL( psa_copy_key( source_key,
2431 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002432 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002433
Ronald Cron5425a212020-08-04 14:58:35 +02002434 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002435
Gilles Peskine4a644642019-05-03 17:14:08 +02002436exit:
2437 psa_reset_key_attributes( &source_attributes );
2438 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002439 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002440}
2441/* END_CASE */
2442
2443/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002444void hash_operation_init( )
2445{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002446 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002447 /* Test each valid way of initializing the object, except for `= {0}`, as
2448 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2449 * though it's OK by the C standard. We could test for this, but we'd need
2450 * to supress the Clang warning for the test. */
2451 psa_hash_operation_t func = psa_hash_operation_init( );
2452 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2453 psa_hash_operation_t zero;
2454
2455 memset( &zero, 0, sizeof( zero ) );
2456
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002457 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002458 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2459 PSA_ERROR_BAD_STATE );
2460 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2461 PSA_ERROR_BAD_STATE );
2462 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2463 PSA_ERROR_BAD_STATE );
2464
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002465 /* A default hash operation should be abortable without error. */
2466 PSA_ASSERT( psa_hash_abort( &func ) );
2467 PSA_ASSERT( psa_hash_abort( &init ) );
2468 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002469}
2470/* END_CASE */
2471
2472/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002473void hash_setup( int alg_arg,
2474 int expected_status_arg )
2475{
2476 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002477 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002478 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002479 psa_status_t status;
2480
Gilles Peskine8817f612018-12-18 00:18:46 +01002481 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002482
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002483 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002484 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002485
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002486 /* Whether setup succeeded or failed, abort must succeed. */
2487 PSA_ASSERT( psa_hash_abort( &operation ) );
2488
2489 /* If setup failed, reproduce the failure, so as to
2490 * test the resulting state of the operation object. */
2491 if( status != PSA_SUCCESS )
2492 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2493
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002494 /* Now the operation object should be reusable. */
2495#if defined(KNOWN_SUPPORTED_HASH_ALG)
2496 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2497 PSA_ASSERT( psa_hash_abort( &operation ) );
2498#endif
2499
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002500exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002501 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002502}
2503/* END_CASE */
2504
2505/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002506void hash_compute_fail( int alg_arg, data_t *input,
2507 int output_size_arg, int expected_status_arg )
2508{
2509 psa_algorithm_t alg = alg_arg;
2510 uint8_t *output = NULL;
2511 size_t output_size = output_size_arg;
2512 size_t output_length = INVALID_EXPORT_LENGTH;
2513 psa_status_t expected_status = expected_status_arg;
2514 psa_status_t status;
2515
2516 ASSERT_ALLOC( output, output_size );
2517
2518 PSA_ASSERT( psa_crypto_init( ) );
2519
2520 status = psa_hash_compute( alg, input->x, input->len,
2521 output, output_size, &output_length );
2522 TEST_EQUAL( status, expected_status );
2523 TEST_ASSERT( output_length <= output_size );
2524
2525exit:
2526 mbedtls_free( output );
2527 PSA_DONE( );
2528}
2529/* END_CASE */
2530
2531/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002532void hash_compare_fail( int alg_arg, data_t *input,
2533 data_t *reference_hash,
2534 int expected_status_arg )
2535{
2536 psa_algorithm_t alg = alg_arg;
2537 psa_status_t expected_status = expected_status_arg;
2538 psa_status_t status;
2539
2540 PSA_ASSERT( psa_crypto_init( ) );
2541
2542 status = psa_hash_compare( alg, input->x, input->len,
2543 reference_hash->x, reference_hash->len );
2544 TEST_EQUAL( status, expected_status );
2545
2546exit:
2547 PSA_DONE( );
2548}
2549/* END_CASE */
2550
2551/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002552void hash_compute_compare( int alg_arg, data_t *input,
2553 data_t *expected_output )
2554{
2555 psa_algorithm_t alg = alg_arg;
2556 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2557 size_t output_length = INVALID_EXPORT_LENGTH;
2558 size_t i;
2559
2560 PSA_ASSERT( psa_crypto_init( ) );
2561
2562 /* Compute with tight buffer */
2563 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2564 output, PSA_HASH_SIZE( alg ),
2565 &output_length ) );
2566 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2567 ASSERT_COMPARE( output, output_length,
2568 expected_output->x, expected_output->len );
2569
2570 /* Compute with larger buffer */
2571 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2572 output, sizeof( output ),
2573 &output_length ) );
2574 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2575 ASSERT_COMPARE( output, output_length,
2576 expected_output->x, expected_output->len );
2577
2578 /* Compare with correct hash */
2579 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2580 output, output_length ) );
2581
2582 /* Compare with trailing garbage */
2583 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2584 output, output_length + 1 ),
2585 PSA_ERROR_INVALID_SIGNATURE );
2586
2587 /* Compare with truncated hash */
2588 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2589 output, output_length - 1 ),
2590 PSA_ERROR_INVALID_SIGNATURE );
2591
2592 /* Compare with corrupted value */
2593 for( i = 0; i < output_length; i++ )
2594 {
2595 test_set_step( i );
2596 output[i] ^= 1;
2597 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2598 output, output_length ),
2599 PSA_ERROR_INVALID_SIGNATURE );
2600 output[i] ^= 1;
2601 }
2602
2603exit:
2604 PSA_DONE( );
2605}
2606/* END_CASE */
2607
2608/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002609void hash_bad_order( )
2610{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002611 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002612 unsigned char input[] = "";
2613 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002614 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002615 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2616 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2617 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002618 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002619 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002620 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002621
Gilles Peskine8817f612018-12-18 00:18:46 +01002622 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002623
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002624 /* Call setup twice in a row. */
2625 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2626 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2627 PSA_ERROR_BAD_STATE );
2628 PSA_ASSERT( psa_hash_abort( &operation ) );
2629
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002630 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002631 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002632 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002633 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002634
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002635 /* Call update after finish. */
2636 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2637 PSA_ASSERT( psa_hash_finish( &operation,
2638 hash, sizeof( hash ), &hash_len ) );
2639 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002640 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002641 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002642
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002643 /* Call verify without calling setup beforehand. */
2644 TEST_EQUAL( psa_hash_verify( &operation,
2645 valid_hash, sizeof( valid_hash ) ),
2646 PSA_ERROR_BAD_STATE );
2647 PSA_ASSERT( psa_hash_abort( &operation ) );
2648
2649 /* Call verify after finish. */
2650 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2651 PSA_ASSERT( psa_hash_finish( &operation,
2652 hash, sizeof( hash ), &hash_len ) );
2653 TEST_EQUAL( psa_hash_verify( &operation,
2654 valid_hash, sizeof( valid_hash ) ),
2655 PSA_ERROR_BAD_STATE );
2656 PSA_ASSERT( psa_hash_abort( &operation ) );
2657
2658 /* Call verify twice in a row. */
2659 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2660 PSA_ASSERT( psa_hash_verify( &operation,
2661 valid_hash, sizeof( valid_hash ) ) );
2662 TEST_EQUAL( psa_hash_verify( &operation,
2663 valid_hash, sizeof( valid_hash ) ),
2664 PSA_ERROR_BAD_STATE );
2665 PSA_ASSERT( psa_hash_abort( &operation ) );
2666
2667 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002668 TEST_EQUAL( psa_hash_finish( &operation,
2669 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002670 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002671 PSA_ASSERT( psa_hash_abort( &operation ) );
2672
2673 /* Call finish twice in a row. */
2674 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2675 PSA_ASSERT( psa_hash_finish( &operation,
2676 hash, sizeof( hash ), &hash_len ) );
2677 TEST_EQUAL( psa_hash_finish( &operation,
2678 hash, sizeof( hash ), &hash_len ),
2679 PSA_ERROR_BAD_STATE );
2680 PSA_ASSERT( psa_hash_abort( &operation ) );
2681
2682 /* Call finish after calling verify. */
2683 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2684 PSA_ASSERT( psa_hash_verify( &operation,
2685 valid_hash, sizeof( valid_hash ) ) );
2686 TEST_EQUAL( psa_hash_finish( &operation,
2687 hash, sizeof( hash ), &hash_len ),
2688 PSA_ERROR_BAD_STATE );
2689 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002690
2691exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002692 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002693}
2694/* END_CASE */
2695
itayzafrir27e69452018-11-01 14:26:34 +02002696/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2697void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002698{
2699 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002700 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2701 * appended to it */
2702 unsigned char hash[] = {
2703 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2704 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2705 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002706 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002707 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002708
Gilles Peskine8817f612018-12-18 00:18:46 +01002709 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002710
itayzafrir27e69452018-11-01 14:26:34 +02002711 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002713 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002714 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002715
itayzafrir27e69452018-11-01 14:26:34 +02002716 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002718 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002719 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002720
itayzafrir27e69452018-11-01 14:26:34 +02002721 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002722 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002723 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002724 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002725
itayzafrirec93d302018-10-18 18:01:10 +03002726exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002727 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002728}
2729/* END_CASE */
2730
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002731/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2732void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002733{
2734 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002735 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002736 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002737 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002738 size_t hash_len;
2739
Gilles Peskine8817f612018-12-18 00:18:46 +01002740 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002741
itayzafrir58028322018-10-25 10:22:01 +03002742 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002743 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002744 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002745 hash, expected_size - 1, &hash_len ),
2746 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002747
2748exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002749 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002750}
2751/* END_CASE */
2752
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002753/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2754void hash_clone_source_state( )
2755{
2756 psa_algorithm_t alg = PSA_ALG_SHA_256;
2757 unsigned char hash[PSA_HASH_MAX_SIZE];
2758 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2759 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2760 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2761 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2762 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2763 size_t hash_len;
2764
2765 PSA_ASSERT( psa_crypto_init( ) );
2766 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2767
2768 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2769 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2770 PSA_ASSERT( psa_hash_finish( &op_finished,
2771 hash, sizeof( hash ), &hash_len ) );
2772 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2773 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2774
2775 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2776 PSA_ERROR_BAD_STATE );
2777
2778 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2779 PSA_ASSERT( psa_hash_finish( &op_init,
2780 hash, sizeof( hash ), &hash_len ) );
2781 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2782 PSA_ASSERT( psa_hash_finish( &op_finished,
2783 hash, sizeof( hash ), &hash_len ) );
2784 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2785 PSA_ASSERT( psa_hash_finish( &op_aborted,
2786 hash, sizeof( hash ), &hash_len ) );
2787
2788exit:
2789 psa_hash_abort( &op_source );
2790 psa_hash_abort( &op_init );
2791 psa_hash_abort( &op_setup );
2792 psa_hash_abort( &op_finished );
2793 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002794 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002795}
2796/* END_CASE */
2797
2798/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2799void hash_clone_target_state( )
2800{
2801 psa_algorithm_t alg = PSA_ALG_SHA_256;
2802 unsigned char hash[PSA_HASH_MAX_SIZE];
2803 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2804 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2805 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2806 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2807 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2808 size_t hash_len;
2809
2810 PSA_ASSERT( psa_crypto_init( ) );
2811
2812 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2813 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2814 PSA_ASSERT( psa_hash_finish( &op_finished,
2815 hash, sizeof( hash ), &hash_len ) );
2816 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2817 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2818
2819 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2820 PSA_ASSERT( psa_hash_finish( &op_target,
2821 hash, sizeof( hash ), &hash_len ) );
2822
2823 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2824 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2825 PSA_ERROR_BAD_STATE );
2826 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2827 PSA_ERROR_BAD_STATE );
2828
2829exit:
2830 psa_hash_abort( &op_target );
2831 psa_hash_abort( &op_init );
2832 psa_hash_abort( &op_setup );
2833 psa_hash_abort( &op_finished );
2834 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002835 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002836}
2837/* END_CASE */
2838
itayzafrir58028322018-10-25 10:22:01 +03002839/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002840void mac_operation_init( )
2841{
Jaeden Amero252ef282019-02-15 14:05:35 +00002842 const uint8_t input[1] = { 0 };
2843
Jaeden Amero769ce272019-01-04 11:48:03 +00002844 /* Test each valid way of initializing the object, except for `= {0}`, as
2845 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2846 * though it's OK by the C standard. We could test for this, but we'd need
2847 * to supress the Clang warning for the test. */
2848 psa_mac_operation_t func = psa_mac_operation_init( );
2849 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2850 psa_mac_operation_t zero;
2851
2852 memset( &zero, 0, sizeof( zero ) );
2853
Jaeden Amero252ef282019-02-15 14:05:35 +00002854 /* A freshly-initialized MAC operation should not be usable. */
2855 TEST_EQUAL( psa_mac_update( &func,
2856 input, sizeof( input ) ),
2857 PSA_ERROR_BAD_STATE );
2858 TEST_EQUAL( psa_mac_update( &init,
2859 input, sizeof( input ) ),
2860 PSA_ERROR_BAD_STATE );
2861 TEST_EQUAL( psa_mac_update( &zero,
2862 input, sizeof( input ) ),
2863 PSA_ERROR_BAD_STATE );
2864
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002865 /* A default MAC operation should be abortable without error. */
2866 PSA_ASSERT( psa_mac_abort( &func ) );
2867 PSA_ASSERT( psa_mac_abort( &init ) );
2868 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002869}
2870/* END_CASE */
2871
2872/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002873void mac_setup( int key_type_arg,
2874 data_t *key,
2875 int alg_arg,
2876 int expected_status_arg )
2877{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002878 psa_key_type_t key_type = key_type_arg;
2879 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002880 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002881 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002882 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2883#if defined(KNOWN_SUPPORTED_MAC_ALG)
2884 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2885#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002886
Gilles Peskine8817f612018-12-18 00:18:46 +01002887 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002888
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002889 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2890 &operation, &status ) )
2891 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002892 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002893
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002894 /* The operation object should be reusable. */
2895#if defined(KNOWN_SUPPORTED_MAC_ALG)
2896 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2897 smoke_test_key_data,
2898 sizeof( smoke_test_key_data ),
2899 KNOWN_SUPPORTED_MAC_ALG,
2900 &operation, &status ) )
2901 goto exit;
2902 TEST_EQUAL( status, PSA_SUCCESS );
2903#endif
2904
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002905exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002906 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002907}
2908/* END_CASE */
2909
2910/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002911void mac_bad_order( )
2912{
Ronald Cron5425a212020-08-04 14:58:35 +02002913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002914 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2915 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002916 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002917 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2918 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2919 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002920 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002921 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2922 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2923 size_t sign_mac_length = 0;
2924 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2925 const uint8_t verify_mac[] = {
2926 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2927 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2928 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2929
2930 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002931 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002932 psa_set_key_algorithm( &attributes, alg );
2933 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002934
Ronald Cron5425a212020-08-04 14:58:35 +02002935 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2936 &key ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002937
Jaeden Amero252ef282019-02-15 14:05:35 +00002938 /* Call update without calling setup beforehand. */
2939 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2940 PSA_ERROR_BAD_STATE );
2941 PSA_ASSERT( psa_mac_abort( &operation ) );
2942
2943 /* Call sign finish without calling setup beforehand. */
2944 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2945 &sign_mac_length),
2946 PSA_ERROR_BAD_STATE );
2947 PSA_ASSERT( psa_mac_abort( &operation ) );
2948
2949 /* Call verify finish without calling setup beforehand. */
2950 TEST_EQUAL( psa_mac_verify_finish( &operation,
2951 verify_mac, sizeof( verify_mac ) ),
2952 PSA_ERROR_BAD_STATE );
2953 PSA_ASSERT( psa_mac_abort( &operation ) );
2954
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002955 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002956 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2957 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002958 PSA_ERROR_BAD_STATE );
2959 PSA_ASSERT( psa_mac_abort( &operation ) );
2960
Jaeden Amero252ef282019-02-15 14:05:35 +00002961 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002962 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002963 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2964 PSA_ASSERT( psa_mac_sign_finish( &operation,
2965 sign_mac, sizeof( sign_mac ),
2966 &sign_mac_length ) );
2967 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2968 PSA_ERROR_BAD_STATE );
2969 PSA_ASSERT( psa_mac_abort( &operation ) );
2970
2971 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002972 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002973 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2974 PSA_ASSERT( psa_mac_verify_finish( &operation,
2975 verify_mac, sizeof( verify_mac ) ) );
2976 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2977 PSA_ERROR_BAD_STATE );
2978 PSA_ASSERT( psa_mac_abort( &operation ) );
2979
2980 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002981 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002982 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2983 PSA_ASSERT( psa_mac_sign_finish( &operation,
2984 sign_mac, sizeof( sign_mac ),
2985 &sign_mac_length ) );
2986 TEST_EQUAL( psa_mac_sign_finish( &operation,
2987 sign_mac, sizeof( sign_mac ),
2988 &sign_mac_length ),
2989 PSA_ERROR_BAD_STATE );
2990 PSA_ASSERT( psa_mac_abort( &operation ) );
2991
2992 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002993 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002994 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2995 PSA_ASSERT( psa_mac_verify_finish( &operation,
2996 verify_mac, sizeof( verify_mac ) ) );
2997 TEST_EQUAL( psa_mac_verify_finish( &operation,
2998 verify_mac, sizeof( verify_mac ) ),
2999 PSA_ERROR_BAD_STATE );
3000 PSA_ASSERT( psa_mac_abort( &operation ) );
3001
3002 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003003 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003004 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3005 TEST_EQUAL( psa_mac_verify_finish( &operation,
3006 verify_mac, sizeof( verify_mac ) ),
3007 PSA_ERROR_BAD_STATE );
3008 PSA_ASSERT( psa_mac_abort( &operation ) );
3009
3010 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003011 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003012 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3013 TEST_EQUAL( psa_mac_sign_finish( &operation,
3014 sign_mac, sizeof( sign_mac ),
3015 &sign_mac_length ),
3016 PSA_ERROR_BAD_STATE );
3017 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003018
Ronald Cron5425a212020-08-04 14:58:35 +02003019 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003020
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003021exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003022 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003023}
3024/* END_CASE */
3025
3026/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003027void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003028 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003029 int alg_arg,
3030 data_t *input,
3031 data_t *expected_mac )
3032{
Ronald Cron5425a212020-08-04 14:58:35 +02003033 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003034 psa_key_type_t key_type = key_type_arg;
3035 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003036 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003038 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003039 size_t mac_buffer_size =
Ronald Cron5425a212020-08-04 14:58:35 +02003040 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003041 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003042 const size_t output_sizes_to_test[] = {
3043 0,
3044 1,
3045 expected_mac->len - 1,
3046 expected_mac->len,
3047 expected_mac->len + 1,
3048 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003049
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003050 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
Gilles Peskine3d404d62020-08-25 23:47:36 +02003051 /* We expect PSA_MAC_FINAL_SIZE to be exact. */
3052 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003053
Gilles Peskine8817f612018-12-18 00:18:46 +01003054 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003055
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003056 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003057 psa_set_key_algorithm( &attributes, alg );
3058 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003059
Ronald Cron5425a212020-08-04 14:58:35 +02003060 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3061 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003062
Gilles Peskine8b356b52020-08-25 23:44:59 +02003063 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3064 {
3065 const size_t output_size = output_sizes_to_test[i];
3066 psa_status_t expected_status =
3067 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3068 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003069
Gilles Peskine8b356b52020-08-25 23:44:59 +02003070 test_set_step( output_size );
3071 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003072
Gilles Peskine8b356b52020-08-25 23:44:59 +02003073 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003074 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003075 PSA_ASSERT( psa_mac_update( &operation,
3076 input->x, input->len ) );
3077 TEST_EQUAL( psa_mac_sign_finish( &operation,
3078 actual_mac, output_size,
3079 &mac_length ),
3080 expected_status );
3081 PSA_ASSERT( psa_mac_abort( &operation ) );
3082
3083 if( expected_status == PSA_SUCCESS )
3084 {
3085 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3086 actual_mac, mac_length );
3087 }
3088 mbedtls_free( actual_mac );
3089 actual_mac = NULL;
3090 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003091
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003092exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003093 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003094 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003095 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003096 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003097}
3098/* END_CASE */
3099
3100/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003101void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003102 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003103 int alg_arg,
3104 data_t *input,
3105 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003106{
Ronald Cron5425a212020-08-04 14:58:35 +02003107 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003108 psa_key_type_t key_type = key_type_arg;
3109 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003110 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003112 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003113
Gilles Peskine69c12672018-06-28 00:07:19 +02003114 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3115
Gilles Peskine8817f612018-12-18 00:18:46 +01003116 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003117
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003118 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003119 psa_set_key_algorithm( &attributes, alg );
3120 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003121
Ronald Cron5425a212020-08-04 14:58:35 +02003122 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3123 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003124
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003125 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003126 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003127 PSA_ASSERT( psa_mac_update( &operation,
3128 input->x, input->len ) );
3129 PSA_ASSERT( psa_mac_verify_finish( &operation,
3130 expected_mac->x,
3131 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003132
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003133 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003134 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003135 PSA_ASSERT( psa_mac_update( &operation,
3136 input->x, input->len ) );
3137 TEST_EQUAL( psa_mac_verify_finish( &operation,
3138 expected_mac->x,
3139 expected_mac->len - 1 ),
3140 PSA_ERROR_INVALID_SIGNATURE );
3141
3142 /* Test a MAC that's too long. */
3143 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3144 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003145 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003146 PSA_ASSERT( psa_mac_update( &operation,
3147 input->x, input->len ) );
3148 TEST_EQUAL( psa_mac_verify_finish( &operation,
3149 perturbed_mac,
3150 expected_mac->len + 1 ),
3151 PSA_ERROR_INVALID_SIGNATURE );
3152
3153 /* Test changing one byte. */
3154 for( size_t i = 0; i < expected_mac->len; i++ )
3155 {
3156 test_set_step( i );
3157 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003158 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003159 PSA_ASSERT( psa_mac_update( &operation,
3160 input->x, input->len ) );
3161 TEST_EQUAL( psa_mac_verify_finish( &operation,
3162 perturbed_mac,
3163 expected_mac->len ),
3164 PSA_ERROR_INVALID_SIGNATURE );
3165 perturbed_mac[i] ^= 1;
3166 }
3167
Gilles Peskine8c9def32018-02-08 10:02:12 +01003168exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003169 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003170 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003171 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003172 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003173}
3174/* END_CASE */
3175
3176/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003177void cipher_operation_init( )
3178{
Jaeden Ameroab439972019-02-15 14:12:05 +00003179 const uint8_t input[1] = { 0 };
3180 unsigned char output[1] = { 0 };
3181 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003182 /* Test each valid way of initializing the object, except for `= {0}`, as
3183 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3184 * though it's OK by the C standard. We could test for this, but we'd need
3185 * to supress the Clang warning for the test. */
3186 psa_cipher_operation_t func = psa_cipher_operation_init( );
3187 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3188 psa_cipher_operation_t zero;
3189
3190 memset( &zero, 0, sizeof( zero ) );
3191
Jaeden Ameroab439972019-02-15 14:12:05 +00003192 /* A freshly-initialized cipher operation should not be usable. */
3193 TEST_EQUAL( psa_cipher_update( &func,
3194 input, sizeof( input ),
3195 output, sizeof( output ),
3196 &output_length ),
3197 PSA_ERROR_BAD_STATE );
3198 TEST_EQUAL( psa_cipher_update( &init,
3199 input, sizeof( input ),
3200 output, sizeof( output ),
3201 &output_length ),
3202 PSA_ERROR_BAD_STATE );
3203 TEST_EQUAL( psa_cipher_update( &zero,
3204 input, sizeof( input ),
3205 output, sizeof( output ),
3206 &output_length ),
3207 PSA_ERROR_BAD_STATE );
3208
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003209 /* A default cipher operation should be abortable without error. */
3210 PSA_ASSERT( psa_cipher_abort( &func ) );
3211 PSA_ASSERT( psa_cipher_abort( &init ) );
3212 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003213}
3214/* END_CASE */
3215
3216/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003217void cipher_setup( int key_type_arg,
3218 data_t *key,
3219 int alg_arg,
3220 int expected_status_arg )
3221{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003222 psa_key_type_t key_type = key_type_arg;
3223 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003224 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003225 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003226 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003227#if defined(KNOWN_SUPPORTED_MAC_ALG)
3228 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3229#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003230
Gilles Peskine8817f612018-12-18 00:18:46 +01003231 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003232
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003233 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3234 &operation, &status ) )
3235 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003236 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003237
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003238 /* The operation object should be reusable. */
3239#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3240 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3241 smoke_test_key_data,
3242 sizeof( smoke_test_key_data ),
3243 KNOWN_SUPPORTED_CIPHER_ALG,
3244 &operation, &status ) )
3245 goto exit;
3246 TEST_EQUAL( status, PSA_SUCCESS );
3247#endif
3248
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003249exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003250 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003251 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003252}
3253/* END_CASE */
3254
3255/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003256void cipher_bad_order( )
3257{
Ronald Cron5425a212020-08-04 14:58:35 +02003258 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003259 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3260 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003261 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003262 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3263 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003264 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003265 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3266 0xaa, 0xaa, 0xaa, 0xaa };
3267 const uint8_t text[] = {
3268 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3269 0xbb, 0xbb, 0xbb, 0xbb };
3270 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3271 size_t length = 0;
3272
3273 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003274 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3275 psa_set_key_algorithm( &attributes, alg );
3276 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003277 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3278 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003279
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003280 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003281 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3282 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003283 PSA_ERROR_BAD_STATE );
3284 PSA_ASSERT( psa_cipher_abort( &operation ) );
3285
3286 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003287 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3288 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003289 PSA_ERROR_BAD_STATE );
3290 PSA_ASSERT( psa_cipher_abort( &operation ) );
3291
Jaeden Ameroab439972019-02-15 14:12:05 +00003292 /* Generate an IV without calling setup beforehand. */
3293 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3294 buffer, sizeof( buffer ),
3295 &length ),
3296 PSA_ERROR_BAD_STATE );
3297 PSA_ASSERT( psa_cipher_abort( &operation ) );
3298
3299 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003300 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003301 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3302 buffer, sizeof( buffer ),
3303 &length ) );
3304 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3305 buffer, sizeof( buffer ),
3306 &length ),
3307 PSA_ERROR_BAD_STATE );
3308 PSA_ASSERT( psa_cipher_abort( &operation ) );
3309
3310 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003311 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003312 PSA_ASSERT( psa_cipher_set_iv( &operation,
3313 iv, sizeof( iv ) ) );
3314 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3315 buffer, sizeof( buffer ),
3316 &length ),
3317 PSA_ERROR_BAD_STATE );
3318 PSA_ASSERT( psa_cipher_abort( &operation ) );
3319
3320 /* Set an IV without calling setup beforehand. */
3321 TEST_EQUAL( psa_cipher_set_iv( &operation,
3322 iv, sizeof( iv ) ),
3323 PSA_ERROR_BAD_STATE );
3324 PSA_ASSERT( psa_cipher_abort( &operation ) );
3325
3326 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003327 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003328 PSA_ASSERT( psa_cipher_set_iv( &operation,
3329 iv, sizeof( iv ) ) );
3330 TEST_EQUAL( psa_cipher_set_iv( &operation,
3331 iv, sizeof( iv ) ),
3332 PSA_ERROR_BAD_STATE );
3333 PSA_ASSERT( psa_cipher_abort( &operation ) );
3334
3335 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003336 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003337 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3338 buffer, sizeof( buffer ),
3339 &length ) );
3340 TEST_EQUAL( psa_cipher_set_iv( &operation,
3341 iv, sizeof( iv ) ),
3342 PSA_ERROR_BAD_STATE );
3343 PSA_ASSERT( psa_cipher_abort( &operation ) );
3344
3345 /* Call update without calling setup beforehand. */
3346 TEST_EQUAL( psa_cipher_update( &operation,
3347 text, sizeof( text ),
3348 buffer, sizeof( buffer ),
3349 &length ),
3350 PSA_ERROR_BAD_STATE );
3351 PSA_ASSERT( psa_cipher_abort( &operation ) );
3352
3353 /* Call update without an IV where an IV is required. */
3354 TEST_EQUAL( psa_cipher_update( &operation,
3355 text, sizeof( text ),
3356 buffer, sizeof( buffer ),
3357 &length ),
3358 PSA_ERROR_BAD_STATE );
3359 PSA_ASSERT( psa_cipher_abort( &operation ) );
3360
3361 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003362 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003363 PSA_ASSERT( psa_cipher_set_iv( &operation,
3364 iv, sizeof( iv ) ) );
3365 PSA_ASSERT( psa_cipher_finish( &operation,
3366 buffer, sizeof( buffer ), &length ) );
3367 TEST_EQUAL( psa_cipher_update( &operation,
3368 text, sizeof( text ),
3369 buffer, sizeof( buffer ),
3370 &length ),
3371 PSA_ERROR_BAD_STATE );
3372 PSA_ASSERT( psa_cipher_abort( &operation ) );
3373
3374 /* Call finish without calling setup beforehand. */
3375 TEST_EQUAL( psa_cipher_finish( &operation,
3376 buffer, sizeof( buffer ), &length ),
3377 PSA_ERROR_BAD_STATE );
3378 PSA_ASSERT( psa_cipher_abort( &operation ) );
3379
3380 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003381 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003382 /* Not calling update means we are encrypting an empty buffer, which is OK
3383 * for cipher modes with padding. */
3384 TEST_EQUAL( psa_cipher_finish( &operation,
3385 buffer, sizeof( buffer ), &length ),
3386 PSA_ERROR_BAD_STATE );
3387 PSA_ASSERT( psa_cipher_abort( &operation ) );
3388
3389 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003390 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003391 PSA_ASSERT( psa_cipher_set_iv( &operation,
3392 iv, sizeof( iv ) ) );
3393 PSA_ASSERT( psa_cipher_finish( &operation,
3394 buffer, sizeof( buffer ), &length ) );
3395 TEST_EQUAL( psa_cipher_finish( &operation,
3396 buffer, sizeof( buffer ), &length ),
3397 PSA_ERROR_BAD_STATE );
3398 PSA_ASSERT( psa_cipher_abort( &operation ) );
3399
Ronald Cron5425a212020-08-04 14:58:35 +02003400 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003401
Jaeden Ameroab439972019-02-15 14:12:05 +00003402exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003403 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003404 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003405}
3406/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003407
Gilles Peskine50e586b2018-06-08 14:28:46 +02003408/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003409void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003410 data_t *key_data, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003411 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003412 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003413{
Ronald Cron5425a212020-08-04 14:58:35 +02003414 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003415 psa_status_t status;
3416 psa_key_type_t key_type = key_type_arg;
3417 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003418 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003419 unsigned char *output = NULL;
3420 size_t output_buffer_size = 0;
3421 size_t function_output_length = 0;
3422 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003423 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003424 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003425
Gilles Peskine8817f612018-12-18 00:18:46 +01003426 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003427
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003428 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3429 psa_set_key_algorithm( &attributes, alg );
3430 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003431
Ronald Cron5425a212020-08-04 14:58:35 +02003432 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3433 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003434
Ronald Cron5425a212020-08-04 14:58:35 +02003435 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003436
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003437 if( iv->len > 0 )
3438 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003439 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003440 }
3441
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003442 output_buffer_size = ( (size_t) input->len +
3443 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003444 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003445
Gilles Peskine8817f612018-12-18 00:18:46 +01003446 PSA_ASSERT( psa_cipher_update( &operation,
3447 input->x, input->len,
3448 output, output_buffer_size,
3449 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003450 total_output_length += function_output_length;
3451 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003452 output + total_output_length,
3453 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003454 &function_output_length );
3455 total_output_length += function_output_length;
3456
Gilles Peskinefe11b722018-12-18 00:24:04 +01003457 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003458 if( expected_status == PSA_SUCCESS )
3459 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003460 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003461 ASSERT_COMPARE( expected_output->x, expected_output->len,
3462 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003463 }
3464
3465exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003466 psa_cipher_abort( &operation );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003467 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003468 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003469 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003470}
3471/* END_CASE */
3472
3473/* BEGIN_CASE */
3474void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003475 data_t *key_data, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003476 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003477 int first_part_size_arg,
3478 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003479 data_t *expected_output )
3480{
Ronald Cron5425a212020-08-04 14:58:35 +02003481 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003482 psa_key_type_t key_type = key_type_arg;
3483 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003484 size_t first_part_size = first_part_size_arg;
3485 size_t output1_length = output1_length_arg;
3486 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003487 unsigned char *output = NULL;
3488 size_t output_buffer_size = 0;
3489 size_t function_output_length = 0;
3490 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003491 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003492 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003493
Gilles Peskine8817f612018-12-18 00:18:46 +01003494 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003495
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003496 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3497 psa_set_key_algorithm( &attributes, alg );
3498 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003499
Ronald Cron5425a212020-08-04 14:58:35 +02003500 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3501 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003502
Ronald Cron5425a212020-08-04 14:58:35 +02003503 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003504
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003505 if( iv->len > 0 )
3506 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003507 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003508 }
3509
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003510 output_buffer_size = ( (size_t) input->len +
3511 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003512 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003513
Gilles Peskinee0866522019-02-19 19:44:00 +01003514 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003515 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3516 output, output_buffer_size,
3517 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003518 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003519 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003520 PSA_ASSERT( psa_cipher_update( &operation,
3521 input->x + first_part_size,
3522 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003523 output + total_output_length,
3524 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003525 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003526 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003527 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003528 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003529 output + total_output_length,
3530 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003531 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003532 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003533 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003534
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003535 ASSERT_COMPARE( expected_output->x, expected_output->len,
3536 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003537
3538exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003539 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003540 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003541 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003542 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003543}
3544/* END_CASE */
3545
3546/* BEGIN_CASE */
3547void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003548 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003549 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003550 int first_part_size_arg,
3551 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003552 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003553{
Ronald Cron5425a212020-08-04 14:58:35 +02003554 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003555 psa_key_type_t key_type = key_type_arg;
3556 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003557 size_t first_part_size = first_part_size_arg;
3558 size_t output1_length = output1_length_arg;
3559 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003560 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003561 size_t output_buffer_size = 0;
3562 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003563 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003564 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003565 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003566
Gilles Peskine8817f612018-12-18 00:18:46 +01003567 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003568
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003569 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3570 psa_set_key_algorithm( &attributes, alg );
3571 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003572
Ronald Cron5425a212020-08-04 14:58:35 +02003573 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3574 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003575
Ronald Cron5425a212020-08-04 14:58:35 +02003576 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003577
Steven Cooreman177deba2020-09-07 17:14:14 +02003578 if( iv->len > 0 )
3579 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003580 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003581 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003582
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003583 output_buffer_size = ( (size_t) input->len +
3584 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003585 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003586
Gilles Peskinee0866522019-02-19 19:44:00 +01003587 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003588 PSA_ASSERT( psa_cipher_update( &operation,
3589 input->x, first_part_size,
3590 output, output_buffer_size,
3591 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003592 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003593 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003594 PSA_ASSERT( psa_cipher_update( &operation,
3595 input->x + first_part_size,
3596 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003597 output + total_output_length,
3598 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003599 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003600 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003601 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003602 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003603 output + total_output_length,
3604 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003606 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003607 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003608
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003609 ASSERT_COMPARE( expected_output->x, expected_output->len,
3610 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003611
3612exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003613 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003614 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003615 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003616 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003617}
3618/* END_CASE */
3619
Gilles Peskine50e586b2018-06-08 14:28:46 +02003620/* BEGIN_CASE */
3621void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003622 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003623 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003624 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003625{
Ronald Cron5425a212020-08-04 14:58:35 +02003626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003627 psa_status_t status;
3628 psa_key_type_t key_type = key_type_arg;
3629 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003630 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003631 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003632 size_t output_buffer_size = 0;
3633 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003634 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003635 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003636 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003637
Gilles Peskine8817f612018-12-18 00:18:46 +01003638 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003639
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003640 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3641 psa_set_key_algorithm( &attributes, alg );
3642 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003643
Ronald Cron5425a212020-08-04 14:58:35 +02003644 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3645 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003646
Ronald Cron5425a212020-08-04 14:58:35 +02003647 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003648
Steven Cooreman177deba2020-09-07 17:14:14 +02003649 if( iv->len > 0 )
3650 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003651 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003652 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003653
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003654 output_buffer_size = ( (size_t) input->len +
3655 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003656 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003657
Gilles Peskine8817f612018-12-18 00:18:46 +01003658 PSA_ASSERT( psa_cipher_update( &operation,
3659 input->x, input->len,
3660 output, output_buffer_size,
3661 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003662 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003663 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003664 output + total_output_length,
3665 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003666 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003667 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003668 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003669
3670 if( expected_status == PSA_SUCCESS )
3671 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003672 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003673 ASSERT_COMPARE( expected_output->x, expected_output->len,
3674 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003675 }
3676
Gilles Peskine50e586b2018-06-08 14:28:46 +02003677exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003678 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003679 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003680 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003681 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003682}
3683/* END_CASE */
3684
Gilles Peskine50e586b2018-06-08 14:28:46 +02003685/* BEGIN_CASE */
3686void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003687 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003688 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003689{
Ronald Cron5425a212020-08-04 14:58:35 +02003690 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003691 psa_key_type_t key_type = key_type_arg;
3692 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003693 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003694 size_t iv_size = 16;
3695 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003696 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003697 size_t output1_size = 0;
3698 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003699 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003700 size_t output2_size = 0;
3701 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003702 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003703 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3704 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003706
Gilles Peskine8817f612018-12-18 00:18:46 +01003707 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003708
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003709 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3710 psa_set_key_algorithm( &attributes, alg );
3711 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003712
Ronald Cron5425a212020-08-04 14:58:35 +02003713 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3714 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003715
Ronald Cron5425a212020-08-04 14:58:35 +02003716 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3717 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003718
Steven Cooreman177deba2020-09-07 17:14:14 +02003719 if( alg != PSA_ALG_ECB_NO_PADDING )
3720 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003721 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3722 iv, iv_size,
3723 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003724 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003725 output1_size = ( (size_t) input->len +
3726 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003727 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003728
Gilles Peskine8817f612018-12-18 00:18:46 +01003729 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3730 output1, output1_size,
3731 &output1_length ) );
3732 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003733 output1 + output1_length,
3734 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003735 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003736
Gilles Peskine048b7f02018-06-08 14:20:49 +02003737 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003738
Gilles Peskine8817f612018-12-18 00:18:46 +01003739 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003740
3741 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003742 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003743
Steven Cooreman177deba2020-09-07 17:14:14 +02003744 if( iv_length > 0 )
3745 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003746 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3747 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003748 }
3749
Gilles Peskine8817f612018-12-18 00:18:46 +01003750 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3751 output2, output2_size,
3752 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003753 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003754 PSA_ASSERT( psa_cipher_finish( &operation2,
3755 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003756 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003757 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003758
Gilles Peskine048b7f02018-06-08 14:20:49 +02003759 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003760
Gilles Peskine8817f612018-12-18 00:18:46 +01003761 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003762
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003763 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003764
3765exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003766 psa_cipher_abort( &operation1 );
3767 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003768 mbedtls_free( output1 );
3769 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003770 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003771 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003772}
3773/* END_CASE */
3774
3775/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003776void cipher_verify_output_multipart( int alg_arg,
3777 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003778 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003779 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003780 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003781{
Ronald Cron5425a212020-08-04 14:58:35 +02003782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003783 psa_key_type_t key_type = key_type_arg;
3784 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003785 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003786 unsigned char iv[16] = {0};
3787 size_t iv_size = 16;
3788 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003789 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003790 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003791 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003792 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003793 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003794 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003795 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003796 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3797 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003799
Gilles Peskine8817f612018-12-18 00:18:46 +01003800 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003801
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003802 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3803 psa_set_key_algorithm( &attributes, alg );
3804 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003805
Ronald Cron5425a212020-08-04 14:58:35 +02003806 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3807 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003808
Ronald Cron5425a212020-08-04 14:58:35 +02003809 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3810 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003811
Steven Cooreman177deba2020-09-07 17:14:14 +02003812 if( alg != PSA_ALG_ECB_NO_PADDING )
3813 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003814 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3815 iv, iv_size,
3816 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003817 }
3818
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003819 output1_buffer_size = ( (size_t) input->len +
3820 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003821 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003822
Gilles Peskinee0866522019-02-19 19:44:00 +01003823 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003824
Gilles Peskine8817f612018-12-18 00:18:46 +01003825 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3826 output1, output1_buffer_size,
3827 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003828 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003829
Gilles Peskine8817f612018-12-18 00:18:46 +01003830 PSA_ASSERT( psa_cipher_update( &operation1,
3831 input->x + first_part_size,
3832 input->len - first_part_size,
3833 output1, output1_buffer_size,
3834 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003835 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003836
Gilles Peskine8817f612018-12-18 00:18:46 +01003837 PSA_ASSERT( psa_cipher_finish( &operation1,
3838 output1 + output1_length,
3839 output1_buffer_size - output1_length,
3840 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003841 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003842
Gilles Peskine8817f612018-12-18 00:18:46 +01003843 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003844
Gilles Peskine048b7f02018-06-08 14:20:49 +02003845 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003846 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003847
Steven Cooreman177deba2020-09-07 17:14:14 +02003848 if( iv_length > 0 )
3849 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003850 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3851 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003852 }
Moran Pekerded84402018-06-06 16:36:50 +03003853
Gilles Peskine8817f612018-12-18 00:18:46 +01003854 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3855 output2, output2_buffer_size,
3856 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003857 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003858
Gilles Peskine8817f612018-12-18 00:18:46 +01003859 PSA_ASSERT( psa_cipher_update( &operation2,
3860 output1 + first_part_size,
3861 output1_length - first_part_size,
3862 output2, output2_buffer_size,
3863 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003864 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003865
Gilles Peskine8817f612018-12-18 00:18:46 +01003866 PSA_ASSERT( psa_cipher_finish( &operation2,
3867 output2 + output2_length,
3868 output2_buffer_size - output2_length,
3869 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003870 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003871
Gilles Peskine8817f612018-12-18 00:18:46 +01003872 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003873
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003874 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003875
3876exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003877 psa_cipher_abort( &operation1 );
3878 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003879 mbedtls_free( output1 );
3880 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003881 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003882 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003883}
3884/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003885
Gilles Peskine20035e32018-02-03 22:44:14 +01003886/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003887void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003888 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003889 data_t *nonce,
3890 data_t *additional_data,
3891 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003892 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003893{
Ronald Cron5425a212020-08-04 14:58:35 +02003894 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003895 psa_key_type_t key_type = key_type_arg;
3896 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003897 unsigned char *output_data = NULL;
3898 size_t output_size = 0;
3899 size_t output_length = 0;
3900 unsigned char *output_data2 = NULL;
3901 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003902 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003903 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003904 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003905
Gilles Peskine4abf7412018-06-18 16:35:34 +02003906 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003907 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3908 * should be exact. */
3909 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3910 TEST_EQUAL( output_size,
3911 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003912 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003913
Gilles Peskine8817f612018-12-18 00:18:46 +01003914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003915
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3917 psa_set_key_algorithm( &attributes, alg );
3918 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003919
Gilles Peskine049c7532019-05-15 20:22:09 +02003920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003921 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003922
Ronald Cron5425a212020-08-04 14:58:35 +02003923 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003924 nonce->x, nonce->len,
3925 additional_data->x,
3926 additional_data->len,
3927 input_data->x, input_data->len,
3928 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003929 &output_length ),
3930 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003931
3932 if( PSA_SUCCESS == expected_result )
3933 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003934 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003935
Gilles Peskine003a4a92019-05-14 16:09:40 +02003936 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3937 * should be exact. */
3938 TEST_EQUAL( input_data->len,
3939 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3940
Ronald Cron5425a212020-08-04 14:58:35 +02003941 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003942 nonce->x, nonce->len,
3943 additional_data->x,
3944 additional_data->len,
3945 output_data, output_length,
3946 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003947 &output_length2 ),
3948 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003949
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003950 ASSERT_COMPARE( input_data->x, input_data->len,
3951 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003952 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003953
Gilles Peskinea1cac842018-06-11 19:33:02 +02003954exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003955 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003956 mbedtls_free( output_data );
3957 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003958 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003959}
3960/* END_CASE */
3961
3962/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003963void aead_encrypt( int key_type_arg, data_t *key_data,
3964 int alg_arg,
3965 data_t *nonce,
3966 data_t *additional_data,
3967 data_t *input_data,
3968 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003969{
Ronald Cron5425a212020-08-04 14:58:35 +02003970 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003971 psa_key_type_t key_type = key_type_arg;
3972 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003973 unsigned char *output_data = NULL;
3974 size_t output_size = 0;
3975 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003976 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003977 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003978
Gilles Peskine4abf7412018-06-18 16:35:34 +02003979 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003980 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3981 * should be exact. */
3982 TEST_EQUAL( output_size,
3983 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003984 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003985
Gilles Peskine8817f612018-12-18 00:18:46 +01003986 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003987
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003988 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3989 psa_set_key_algorithm( &attributes, alg );
3990 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003991
Gilles Peskine049c7532019-05-15 20:22:09 +02003992 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003993 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003994
Ronald Cron5425a212020-08-04 14:58:35 +02003995 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003996 nonce->x, nonce->len,
3997 additional_data->x, additional_data->len,
3998 input_data->x, input_data->len,
3999 output_data, output_size,
4000 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004001
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004002 ASSERT_COMPARE( expected_result->x, expected_result->len,
4003 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004004
Gilles Peskinea1cac842018-06-11 19:33:02 +02004005exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004006 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004007 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004008 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004009}
4010/* END_CASE */
4011
4012/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004013void aead_decrypt( int key_type_arg, data_t *key_data,
4014 int alg_arg,
4015 data_t *nonce,
4016 data_t *additional_data,
4017 data_t *input_data,
4018 data_t *expected_data,
4019 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004020{
Ronald Cron5425a212020-08-04 14:58:35 +02004021 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004022 psa_key_type_t key_type = key_type_arg;
4023 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004024 unsigned char *output_data = NULL;
4025 size_t output_size = 0;
4026 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004027 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004029 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004030
Gilles Peskine003a4a92019-05-14 16:09:40 +02004031 output_size = input_data->len - tag_length;
4032 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4033 * should be exact. */
4034 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4035 TEST_EQUAL( output_size,
4036 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004037 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004038
Gilles Peskine8817f612018-12-18 00:18:46 +01004039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004040
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004041 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4042 psa_set_key_algorithm( &attributes, alg );
4043 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004044
Gilles Peskine049c7532019-05-15 20:22:09 +02004045 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004046 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004047
Ronald Cron5425a212020-08-04 14:58:35 +02004048 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004049 nonce->x, nonce->len,
4050 additional_data->x,
4051 additional_data->len,
4052 input_data->x, input_data->len,
4053 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004054 &output_length ),
4055 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004056
Gilles Peskine2d277862018-06-18 15:41:12 +02004057 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004058 ASSERT_COMPARE( expected_data->x, expected_data->len,
4059 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004060
Gilles Peskinea1cac842018-06-11 19:33:02 +02004061exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004062 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004063 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004064 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004065}
4066/* END_CASE */
4067
4068/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004069void signature_size( int type_arg,
4070 int bits,
4071 int alg_arg,
4072 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004073{
4074 psa_key_type_t type = type_arg;
4075 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004076 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004077
Gilles Peskinefe11b722018-12-18 00:24:04 +01004078 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004079#if defined(MBEDTLS_TEST_DEPRECATED)
4080 TEST_EQUAL( actual_size,
4081 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4082#endif /* MBEDTLS_TEST_DEPRECATED */
4083
Gilles Peskinee59236f2018-01-27 23:32:46 +01004084exit:
4085 ;
4086}
4087/* END_CASE */
4088
4089/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004090void sign_deterministic( int key_type_arg, data_t *key_data,
4091 int alg_arg, data_t *input_data,
4092 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004093{
Ronald Cron5425a212020-08-04 14:58:35 +02004094 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004095 psa_key_type_t key_type = key_type_arg;
4096 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004097 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004098 unsigned char *signature = NULL;
4099 size_t signature_size;
4100 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004102
Gilles Peskine8817f612018-12-18 00:18:46 +01004103 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004104
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004105 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004106 psa_set_key_algorithm( &attributes, alg );
4107 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004108
Gilles Peskine049c7532019-05-15 20:22:09 +02004109 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004110 &key ) );
4111 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004112 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004113
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004114 /* Allocate a buffer which has the size advertized by the
4115 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004116 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004117 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004118 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004119 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004120 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004121
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004122 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004123 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004124 input_data->x, input_data->len,
4125 signature, signature_size,
4126 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004127 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004128 ASSERT_COMPARE( output_data->x, output_data->len,
4129 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004130
Gilles Peskine0627f982019-11-26 19:12:16 +01004131#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004132 memset( signature, 0, signature_size );
4133 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004134 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004135 input_data->x, input_data->len,
4136 signature, signature_size,
4137 &signature_length ) );
4138 ASSERT_COMPARE( output_data->x, output_data->len,
4139 signature, signature_length );
4140#endif /* MBEDTLS_TEST_DEPRECATED */
4141
Gilles Peskine20035e32018-02-03 22:44:14 +01004142exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004143 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004144 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004145 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004146 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004147}
4148/* END_CASE */
4149
4150/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004151void sign_fail( int key_type_arg, data_t *key_data,
4152 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004153 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004154{
Ronald Cron5425a212020-08-04 14:58:35 +02004155 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004156 psa_key_type_t key_type = key_type_arg;
4157 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004158 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004159 psa_status_t actual_status;
4160 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004161 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004162 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004164
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004165 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004166
Gilles Peskine8817f612018-12-18 00:18:46 +01004167 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004168
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004169 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004170 psa_set_key_algorithm( &attributes, alg );
4171 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004172
Gilles Peskine049c7532019-05-15 20:22:09 +02004173 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004174 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004175
Ronald Cron5425a212020-08-04 14:58:35 +02004176 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004177 input_data->x, input_data->len,
4178 signature, signature_size,
4179 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004180 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004181 /* The value of *signature_length is unspecified on error, but
4182 * whatever it is, it should be less than signature_size, so that
4183 * if the caller tries to read *signature_length bytes without
4184 * checking the error code then they don't overflow a buffer. */
4185 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004186
Gilles Peskine895242b2019-11-29 12:15:40 +01004187#if defined(MBEDTLS_TEST_DEPRECATED)
4188 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004189 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004190 input_data->x, input_data->len,
4191 signature, signature_size,
4192 &signature_length ),
4193 expected_status );
4194 TEST_ASSERT( signature_length <= signature_size );
4195#endif /* MBEDTLS_TEST_DEPRECATED */
4196
Gilles Peskine20035e32018-02-03 22:44:14 +01004197exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004198 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004199 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004200 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004201 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004202}
4203/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004204
4205/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004206void sign_verify( int key_type_arg, data_t *key_data,
4207 int alg_arg, data_t *input_data )
4208{
Ronald Cron5425a212020-08-04 14:58:35 +02004209 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004210 psa_key_type_t key_type = key_type_arg;
4211 psa_algorithm_t alg = alg_arg;
4212 size_t key_bits;
4213 unsigned char *signature = NULL;
4214 size_t signature_size;
4215 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004216 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004217
Gilles Peskine8817f612018-12-18 00:18:46 +01004218 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004219
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004221 psa_set_key_algorithm( &attributes, alg );
4222 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004223
Gilles Peskine049c7532019-05-15 20:22:09 +02004224 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004225 &key ) );
4226 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004227 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004228
4229 /* Allocate a buffer which has the size advertized by the
4230 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004231 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004232 key_bits, alg );
4233 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004234 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004235 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004236
4237 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004238 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004239 input_data->x, input_data->len,
4240 signature, signature_size,
4241 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004242 /* Check that the signature length looks sensible. */
4243 TEST_ASSERT( signature_length <= signature_size );
4244 TEST_ASSERT( signature_length > 0 );
4245
4246 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004247 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004248 input_data->x, input_data->len,
4249 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004250
4251 if( input_data->len != 0 )
4252 {
4253 /* Flip a bit in the input and verify that the signature is now
4254 * detected as invalid. Flip a bit at the beginning, not at the end,
4255 * because ECDSA may ignore the last few bits of the input. */
4256 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004257 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004258 input_data->x, input_data->len,
4259 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004260 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004261 }
4262
4263exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004264 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004265 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004266 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004267 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004268}
4269/* END_CASE */
4270
4271/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004272void asymmetric_verify( int key_type_arg, data_t *key_data,
4273 int alg_arg, data_t *hash_data,
4274 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004275{
Ronald Cron5425a212020-08-04 14:58:35 +02004276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004277 psa_key_type_t key_type = key_type_arg;
4278 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004280
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004281 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004282
Gilles Peskine8817f612018-12-18 00:18:46 +01004283 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004284
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004285 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004286 psa_set_key_algorithm( &attributes, alg );
4287 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004288
Gilles Peskine049c7532019-05-15 20:22:09 +02004289 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004290 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004291
Ronald Cron5425a212020-08-04 14:58:35 +02004292 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004293 hash_data->x, hash_data->len,
4294 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004295
4296#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004297 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004298 hash_data->x, hash_data->len,
4299 signature_data->x,
4300 signature_data->len ) );
4301
4302#endif /* MBEDTLS_TEST_DEPRECATED */
4303
itayzafrir5c753392018-05-08 11:18:38 +03004304exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004305 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004306 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004307 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004308}
4309/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004310
4311/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004312void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4313 int alg_arg, data_t *hash_data,
4314 data_t *signature_data,
4315 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004316{
Ronald Cron5425a212020-08-04 14:58:35 +02004317 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004318 psa_key_type_t key_type = key_type_arg;
4319 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004320 psa_status_t actual_status;
4321 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004323
Gilles Peskine8817f612018-12-18 00:18:46 +01004324 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004325
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004326 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004327 psa_set_key_algorithm( &attributes, alg );
4328 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004329
Gilles Peskine049c7532019-05-15 20:22:09 +02004330 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004331 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004332
Ronald Cron5425a212020-08-04 14:58:35 +02004333 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004334 hash_data->x, hash_data->len,
4335 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004336 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004337
Gilles Peskine895242b2019-11-29 12:15:40 +01004338#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004339 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004340 hash_data->x, hash_data->len,
4341 signature_data->x, signature_data->len ),
4342 expected_status );
4343#endif /* MBEDTLS_TEST_DEPRECATED */
4344
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004345exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004346 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004347 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004348 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004349}
4350/* END_CASE */
4351
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004352/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004353void asymmetric_encrypt( int key_type_arg,
4354 data_t *key_data,
4355 int alg_arg,
4356 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004357 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004358 int expected_output_length_arg,
4359 int expected_status_arg )
4360{
Ronald Cron5425a212020-08-04 14:58:35 +02004361 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004362 psa_key_type_t key_type = key_type_arg;
4363 psa_algorithm_t alg = alg_arg;
4364 size_t expected_output_length = expected_output_length_arg;
4365 size_t key_bits;
4366 unsigned char *output = NULL;
4367 size_t output_size;
4368 size_t output_length = ~0;
4369 psa_status_t actual_status;
4370 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004371 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004372
Gilles Peskine8817f612018-12-18 00:18:46 +01004373 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004374
Gilles Peskine656896e2018-06-29 19:12:28 +02004375 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004376 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4377 psa_set_key_algorithm( &attributes, alg );
4378 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004379 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004380 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004381
4382 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004383 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004384 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004385 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004386 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004387
4388 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004389 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004390 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004391 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004392 output, output_size,
4393 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004394 TEST_EQUAL( actual_status, expected_status );
4395 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004396
Gilles Peskine68428122018-06-30 18:42:41 +02004397 /* If the label is empty, the test framework puts a non-null pointer
4398 * in label->x. Test that a null pointer works as well. */
4399 if( label->len == 0 )
4400 {
4401 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004402 if( output_size != 0 )
4403 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004404 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004405 input_data->x, input_data->len,
4406 NULL, label->len,
4407 output, output_size,
4408 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004409 TEST_EQUAL( actual_status, expected_status );
4410 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004411 }
4412
Gilles Peskine656896e2018-06-29 19:12:28 +02004413exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004414 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004415 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004416 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004417 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004418}
4419/* END_CASE */
4420
4421/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004422void asymmetric_encrypt_decrypt( int key_type_arg,
4423 data_t *key_data,
4424 int alg_arg,
4425 data_t *input_data,
4426 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004427{
Ronald Cron5425a212020-08-04 14:58:35 +02004428 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004429 psa_key_type_t key_type = key_type_arg;
4430 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004431 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004432 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004433 size_t output_size;
4434 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004435 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004436 size_t output2_size;
4437 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004439
Gilles Peskine8817f612018-12-18 00:18:46 +01004440 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004441
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004442 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4443 psa_set_key_algorithm( &attributes, alg );
4444 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004445
Gilles Peskine049c7532019-05-15 20:22:09 +02004446 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004447 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004448
4449 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004450 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004451 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004452 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004453 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004454 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004455 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004456
Gilles Peskineeebd7382018-06-08 18:11:54 +02004457 /* We test encryption by checking that encrypt-then-decrypt gives back
4458 * the original plaintext because of the non-optional random
4459 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004460 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004461 input_data->x, input_data->len,
4462 label->x, label->len,
4463 output, output_size,
4464 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004465 /* We don't know what ciphertext length to expect, but check that
4466 * it looks sensible. */
4467 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004468
Ronald Cron5425a212020-08-04 14:58:35 +02004469 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004470 output, output_length,
4471 label->x, label->len,
4472 output2, output2_size,
4473 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004474 ASSERT_COMPARE( input_data->x, input_data->len,
4475 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004476
4477exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004478 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004479 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004480 mbedtls_free( output );
4481 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004482 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004483}
4484/* END_CASE */
4485
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004486/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004487void asymmetric_decrypt( int key_type_arg,
4488 data_t *key_data,
4489 int alg_arg,
4490 data_t *input_data,
4491 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004492 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004493{
Ronald Cron5425a212020-08-04 14:58:35 +02004494 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004495 psa_key_type_t key_type = key_type_arg;
4496 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004497 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004498 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004499 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004501
Jaeden Amero412654a2019-02-06 12:57:46 +00004502 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004503 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004504
Gilles Peskine8817f612018-12-18 00:18:46 +01004505 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004506
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004507 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4508 psa_set_key_algorithm( &attributes, alg );
4509 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004510
Gilles Peskine049c7532019-05-15 20:22:09 +02004511 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004512 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004513
Ronald Cron5425a212020-08-04 14:58:35 +02004514 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004515 input_data->x, input_data->len,
4516 label->x, label->len,
4517 output,
4518 output_size,
4519 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004520 ASSERT_COMPARE( expected_data->x, expected_data->len,
4521 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004522
Gilles Peskine68428122018-06-30 18:42:41 +02004523 /* If the label is empty, the test framework puts a non-null pointer
4524 * in label->x. Test that a null pointer works as well. */
4525 if( label->len == 0 )
4526 {
4527 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004528 if( output_size != 0 )
4529 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004530 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004531 input_data->x, input_data->len,
4532 NULL, label->len,
4533 output,
4534 output_size,
4535 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004536 ASSERT_COMPARE( expected_data->x, expected_data->len,
4537 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004538 }
4539
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004540exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004541 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004542 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004543 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004544 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004545}
4546/* END_CASE */
4547
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004548/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004549void asymmetric_decrypt_fail( int key_type_arg,
4550 data_t *key_data,
4551 int alg_arg,
4552 data_t *input_data,
4553 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004554 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004555 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004556{
Ronald Cron5425a212020-08-04 14:58:35 +02004557 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004558 psa_key_type_t key_type = key_type_arg;
4559 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004560 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004561 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004562 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004563 psa_status_t actual_status;
4564 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004565 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004566
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004567 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004568
Gilles Peskine8817f612018-12-18 00:18:46 +01004569 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004570
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4572 psa_set_key_algorithm( &attributes, alg );
4573 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004574
Gilles Peskine049c7532019-05-15 20:22:09 +02004575 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004576 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004577
Ronald Cron5425a212020-08-04 14:58:35 +02004578 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004579 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004580 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004581 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004582 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004583 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004584 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004585
Gilles Peskine68428122018-06-30 18:42:41 +02004586 /* If the label is empty, the test framework puts a non-null pointer
4587 * in label->x. Test that a null pointer works as well. */
4588 if( label->len == 0 )
4589 {
4590 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004591 if( output_size != 0 )
4592 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004593 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004594 input_data->x, input_data->len,
4595 NULL, label->len,
4596 output, output_size,
4597 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004598 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004599 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004600 }
4601
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004602exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004603 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004604 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004605 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004606 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004607}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004608/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004609
4610/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004611void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004612{
4613 /* Test each valid way of initializing the object, except for `= {0}`, as
4614 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4615 * though it's OK by the C standard. We could test for this, but we'd need
4616 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004617 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004618 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4619 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4620 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004621
4622 memset( &zero, 0, sizeof( zero ) );
4623
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004624 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004625 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004626 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004627 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004628 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004629 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004630 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004631
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004632 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004633 PSA_ASSERT( psa_key_derivation_abort(&func) );
4634 PSA_ASSERT( psa_key_derivation_abort(&init) );
4635 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004636}
4637/* END_CASE */
4638
Janos Follath16de4a42019-06-13 16:32:24 +01004639/* BEGIN_CASE */
4640void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004641{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004642 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004643 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004645
Gilles Peskine8817f612018-12-18 00:18:46 +01004646 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004647
Janos Follath16de4a42019-06-13 16:32:24 +01004648 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004649 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004650
4651exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004652 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004653 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004654}
4655/* END_CASE */
4656
Janos Follathaf3c2a02019-06-12 12:34:34 +01004657/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004658void derive_set_capacity( int alg_arg, int capacity_arg,
4659 int expected_status_arg )
4660{
4661 psa_algorithm_t alg = alg_arg;
4662 size_t capacity = capacity_arg;
4663 psa_status_t expected_status = expected_status_arg;
4664 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4665
4666 PSA_ASSERT( psa_crypto_init( ) );
4667
4668 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4669
4670 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4671 expected_status );
4672
4673exit:
4674 psa_key_derivation_abort( &operation );
4675 PSA_DONE( );
4676}
4677/* END_CASE */
4678
4679/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004680void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004681 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004682 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004683 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004684 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004685 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004686 int expected_status_arg3,
4687 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004688{
4689 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004690 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4691 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004692 psa_status_t expected_statuses[] = {expected_status_arg1,
4693 expected_status_arg2,
4694 expected_status_arg3};
4695 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004696 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4697 MBEDTLS_SVC_KEY_ID_INIT,
4698 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004699 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4701 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004702 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004703 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004704 psa_status_t expected_output_status = expected_output_status_arg;
4705 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004706
4707 PSA_ASSERT( psa_crypto_init( ) );
4708
4709 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4710 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004711
4712 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4713
4714 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4715 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004716 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004717 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004718 psa_set_key_type( &attributes, key_types[i] );
4719 PSA_ASSERT( psa_import_key( &attributes,
4720 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004721 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004722 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4723 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4724 {
4725 // When taking a private key as secret input, use key agreement
4726 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004727 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004728 expected_statuses[i] );
4729 }
4730 else
4731 {
4732 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004733 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004734 expected_statuses[i] );
4735 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004736 }
4737 else
4738 {
4739 TEST_EQUAL( psa_key_derivation_input_bytes(
4740 &operation, steps[i],
4741 inputs[i]->x, inputs[i]->len ),
4742 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004743 }
4744 }
4745
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004746 if( output_key_type != PSA_KEY_TYPE_NONE )
4747 {
4748 psa_reset_key_attributes( &attributes );
4749 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4750 psa_set_key_bits( &attributes, 8 );
4751 actual_output_status =
4752 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004753 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004754 }
4755 else
4756 {
4757 uint8_t buffer[1];
4758 actual_output_status =
4759 psa_key_derivation_output_bytes( &operation,
4760 buffer, sizeof( buffer ) );
4761 }
4762 TEST_EQUAL( actual_output_status, expected_output_status );
4763
Janos Follathaf3c2a02019-06-12 12:34:34 +01004764exit:
4765 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004766 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4767 psa_destroy_key( keys[i] );
4768 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004769 PSA_DONE( );
4770}
4771/* END_CASE */
4772
Janos Follathd958bb72019-07-03 15:02:16 +01004773/* BEGIN_CASE */
4774void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004775{
Janos Follathd958bb72019-07-03 15:02:16 +01004776 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004777 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004778 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004779 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004780 unsigned char input1[] = "Input 1";
4781 size_t input1_length = sizeof( input1 );
4782 unsigned char input2[] = "Input 2";
4783 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004784 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004785 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004786 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4787 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4788 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004790
Gilles Peskine8817f612018-12-18 00:18:46 +01004791 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004792
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004793 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4794 psa_set_key_algorithm( &attributes, alg );
4795 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004796
Gilles Peskine73676cb2019-05-15 20:15:10 +02004797 PSA_ASSERT( psa_import_key( &attributes,
4798 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004799 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004800
4801 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004802 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004803 input1, input1_length,
4804 input2, input2_length,
4805 capacity ) )
4806 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004807
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004809 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004810 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004811
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004812 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004813
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004814 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004815 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004816
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004817exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004818 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004819 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004820 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004821}
4822/* END_CASE */
4823
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004824/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004825void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004826{
4827 uint8_t output_buffer[16];
4828 size_t buffer_size = 16;
4829 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004831
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004832 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4833 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004834 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004835
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004837 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004838
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004840
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004841 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4842 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004843 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004844
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004846 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004847
4848exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004850}
4851/* END_CASE */
4852
4853/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004854void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004855 int step1_arg, data_t *input1,
4856 int step2_arg, data_t *input2,
4857 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004858 int requested_capacity_arg,
4859 data_t *expected_output1,
4860 data_t *expected_output2 )
4861{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004862 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004863 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4864 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004865 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4866 MBEDTLS_SVC_KEY_ID_INIT,
4867 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004868 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004869 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004870 uint8_t *expected_outputs[2] =
4871 {expected_output1->x, expected_output2->x};
4872 size_t output_sizes[2] =
4873 {expected_output1->len, expected_output2->len};
4874 size_t output_buffer_size = 0;
4875 uint8_t *output_buffer = NULL;
4876 size_t expected_capacity;
4877 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004879 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004880 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004881
4882 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4883 {
4884 if( output_sizes[i] > output_buffer_size )
4885 output_buffer_size = output_sizes[i];
4886 if( output_sizes[i] == 0 )
4887 expected_outputs[i] = NULL;
4888 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004889 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004891
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004892 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4893 psa_set_key_algorithm( &attributes, alg );
4894 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004895
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004896 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004897 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4898 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4899 requested_capacity ) );
4900 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004901 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004902 switch( steps[i] )
4903 {
4904 case 0:
4905 break;
4906 case PSA_KEY_DERIVATION_INPUT_SECRET:
4907 PSA_ASSERT( psa_import_key( &attributes,
4908 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004909 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004910 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004911 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004912 break;
4913 default:
4914 PSA_ASSERT( psa_key_derivation_input_bytes(
4915 &operation, steps[i],
4916 inputs[i]->x, inputs[i]->len ) );
4917 break;
4918 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004919 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004920
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004921 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004922 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004923 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004924 expected_capacity = requested_capacity;
4925
4926 /* Expansion phase. */
4927 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4928 {
4929 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004930 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004931 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004932 if( expected_capacity == 0 && output_sizes[i] == 0 )
4933 {
4934 /* Reading 0 bytes when 0 bytes are available can go either way. */
4935 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004936 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004937 continue;
4938 }
4939 else if( expected_capacity == 0 ||
4940 output_sizes[i] > expected_capacity )
4941 {
4942 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004943 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004944 expected_capacity = 0;
4945 continue;
4946 }
4947 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004948 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004949 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004950 ASSERT_COMPARE( output_buffer, output_sizes[i],
4951 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004952 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004953 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004955 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004956 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004957 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004959
4960exit:
4961 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004963 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4964 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004965 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004966}
4967/* END_CASE */
4968
4969/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004970void derive_full( int alg_arg,
4971 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004972 data_t *input1,
4973 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004974 int requested_capacity_arg )
4975{
Ronald Cron5425a212020-08-04 14:58:35 +02004976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004977 psa_algorithm_t alg = alg_arg;
4978 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004980 unsigned char output_buffer[16];
4981 size_t expected_capacity = requested_capacity;
4982 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004983 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004984
Gilles Peskine8817f612018-12-18 00:18:46 +01004985 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004986
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004987 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4988 psa_set_key_algorithm( &attributes, alg );
4989 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004990
Gilles Peskine049c7532019-05-15 20:22:09 +02004991 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004992 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004993
Ronald Cron5425a212020-08-04 14:58:35 +02004994 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01004995 input1->x, input1->len,
4996 input2->x, input2->len,
4997 requested_capacity ) )
4998 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004999
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005000 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005001 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005002 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005003
5004 /* Expansion phase. */
5005 while( current_capacity > 0 )
5006 {
5007 size_t read_size = sizeof( output_buffer );
5008 if( read_size > current_capacity )
5009 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005010 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005011 output_buffer,
5012 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005013 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005014 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005015 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005016 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005017 }
5018
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005019 /* Check that the operation refuses to go over capacity. */
5020 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005021 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005022
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005023 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005024
5025exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005026 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005027 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005028 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005029}
5030/* END_CASE */
5031
Janos Follathe60c9052019-07-03 13:51:30 +01005032/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005033void derive_key_exercise( int alg_arg,
5034 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005035 data_t *input1,
5036 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005037 int derived_type_arg,
5038 int derived_bits_arg,
5039 int derived_usage_arg,
5040 int derived_alg_arg )
5041{
Ronald Cron5425a212020-08-04 14:58:35 +02005042 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5043 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005044 psa_algorithm_t alg = alg_arg;
5045 psa_key_type_t derived_type = derived_type_arg;
5046 size_t derived_bits = derived_bits_arg;
5047 psa_key_usage_t derived_usage = derived_usage_arg;
5048 psa_algorithm_t derived_alg = derived_alg_arg;
5049 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005051 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005052 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005053
Gilles Peskine8817f612018-12-18 00:18:46 +01005054 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005055
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005056 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5057 psa_set_key_algorithm( &attributes, alg );
5058 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005059 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005060 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005061
5062 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005063 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005064 input1->x, input1->len,
5065 input2->x, input2->len, capacity ) )
5066 goto exit;
5067
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005068 psa_set_key_usage_flags( &attributes, derived_usage );
5069 psa_set_key_algorithm( &attributes, derived_alg );
5070 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005071 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005073 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005074
5075 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005076 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005077 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5078 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005079
5080 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005081 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005082 goto exit;
5083
5084exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005085 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005086 psa_reset_key_attributes( &got_attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005087 psa_destroy_key( base_key );
5088 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005089 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005090}
5091/* END_CASE */
5092
Janos Follath42fd8882019-07-03 14:17:09 +01005093/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005094void derive_key_export( int alg_arg,
5095 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005096 data_t *input1,
5097 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005098 int bytes1_arg,
5099 int bytes2_arg )
5100{
Ronald Cron5425a212020-08-04 14:58:35 +02005101 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5102 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005103 psa_algorithm_t alg = alg_arg;
5104 size_t bytes1 = bytes1_arg;
5105 size_t bytes2 = bytes2_arg;
5106 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005107 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005108 uint8_t *output_buffer = NULL;
5109 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005110 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5111 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005112 size_t length;
5113
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005114 ASSERT_ALLOC( output_buffer, capacity );
5115 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005116 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005117
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005118 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5119 psa_set_key_algorithm( &base_attributes, alg );
5120 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005121 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005122 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005123
5124 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005125 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005126 input1->x, input1->len,
5127 input2->x, input2->len, capacity ) )
5128 goto exit;
5129
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005130 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005131 output_buffer,
5132 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005133 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005134
5135 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005136 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005137 input1->x, input1->len,
5138 input2->x, input2->len, capacity ) )
5139 goto exit;
5140
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005141 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5142 psa_set_key_algorithm( &derived_attributes, 0 );
5143 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005144 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005145 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005146 &derived_key ) );
5147 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005148 export_buffer, bytes1,
5149 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005150 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005151 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005152 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005153 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005154 &derived_key ) );
5155 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005156 export_buffer + bytes1, bytes2,
5157 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005158 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005159
5160 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005161 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5162 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005163
5164exit:
5165 mbedtls_free( output_buffer );
5166 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005167 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005168 psa_destroy_key( base_key );
5169 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005170 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005171}
5172/* END_CASE */
5173
5174/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005175void derive_key( int alg_arg,
5176 data_t *key_data, data_t *input1, data_t *input2,
5177 int type_arg, int bits_arg,
5178 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005179{
Ronald Cron5425a212020-08-04 14:58:35 +02005180 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5181 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005182 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005183 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005184 size_t bits = bits_arg;
5185 psa_status_t expected_status = expected_status_arg;
5186 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5187 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5188 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5189
5190 PSA_ASSERT( psa_crypto_init( ) );
5191
5192 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5193 psa_set_key_algorithm( &base_attributes, alg );
5194 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5195 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005196 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005197
Ronald Cron5425a212020-08-04 14:58:35 +02005198 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005199 input1->x, input1->len,
5200 input2->x, input2->len, SIZE_MAX ) )
5201 goto exit;
5202
5203 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5204 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005205 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005206 psa_set_key_bits( &derived_attributes, bits );
5207 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005208 &derived_key ),
Gilles Peskinec744d992019-07-30 17:26:54 +02005209 expected_status );
5210
5211exit:
5212 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005213 psa_destroy_key( base_key );
5214 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005215 PSA_DONE( );
5216}
5217/* END_CASE */
5218
5219/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005220void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005221 int our_key_type_arg, int our_key_alg_arg,
5222 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005223 int expected_status_arg )
5224{
Ronald Cron5425a212020-08-04 14:58:35 +02005225 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005226 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005227 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005228 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005229 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005231 psa_status_t expected_status = expected_status_arg;
5232 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005233
Gilles Peskine8817f612018-12-18 00:18:46 +01005234 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005235
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005236 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005237 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005238 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005239 PSA_ASSERT( psa_import_key( &attributes,
5240 our_key_data->x, our_key_data->len,
5241 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005242
Gilles Peskine77f40d82019-04-11 21:27:06 +02005243 /* The tests currently include inputs that should fail at either step.
5244 * Test cases that fail at the setup step should be changed to call
5245 * key_derivation_setup instead, and this function should be renamed
5246 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005247 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005248 if( status == PSA_SUCCESS )
5249 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005250 TEST_EQUAL( psa_key_derivation_key_agreement(
5251 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5252 our_key,
5253 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005254 expected_status );
5255 }
5256 else
5257 {
5258 TEST_ASSERT( status == expected_status );
5259 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005260
5261exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005262 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005263 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005264 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005265}
5266/* END_CASE */
5267
5268/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005269void raw_key_agreement( int alg_arg,
5270 int our_key_type_arg, data_t *our_key_data,
5271 data_t *peer_key_data,
5272 data_t *expected_output )
5273{
Ronald Cron5425a212020-08-04 14:58:35 +02005274 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005275 psa_algorithm_t alg = alg_arg;
5276 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005278 unsigned char *output = NULL;
5279 size_t output_length = ~0;
5280
5281 ASSERT_ALLOC( output, expected_output->len );
5282 PSA_ASSERT( psa_crypto_init( ) );
5283
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005284 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5285 psa_set_key_algorithm( &attributes, alg );
5286 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005287 PSA_ASSERT( psa_import_key( &attributes,
5288 our_key_data->x, our_key_data->len,
5289 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005290
Gilles Peskinebe697d82019-05-16 18:00:41 +02005291 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5292 peer_key_data->x, peer_key_data->len,
5293 output, expected_output->len,
5294 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005295 ASSERT_COMPARE( output, output_length,
5296 expected_output->x, expected_output->len );
5297
5298exit:
5299 mbedtls_free( output );
5300 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005301 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005302}
5303/* END_CASE */
5304
5305/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005306void key_agreement_capacity( int alg_arg,
5307 int our_key_type_arg, data_t *our_key_data,
5308 data_t *peer_key_data,
5309 int expected_capacity_arg )
5310{
Ronald Cron5425a212020-08-04 14:58:35 +02005311 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005312 psa_algorithm_t alg = alg_arg;
5313 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005314 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005316 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005317 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005318
Gilles Peskine8817f612018-12-18 00:18:46 +01005319 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005320
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005321 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5322 psa_set_key_algorithm( &attributes, alg );
5323 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005324 PSA_ASSERT( psa_import_key( &attributes,
5325 our_key_data->x, our_key_data->len,
5326 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005327
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005328 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005329 PSA_ASSERT( psa_key_derivation_key_agreement(
5330 &operation,
5331 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5332 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005333 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5334 {
5335 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005337 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005338 NULL, 0 ) );
5339 }
Gilles Peskine59685592018-09-18 12:11:34 +02005340
Gilles Peskinebf491972018-10-25 22:36:12 +02005341 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005342 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005343 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005344 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005345
Gilles Peskinebf491972018-10-25 22:36:12 +02005346 /* Test the actual capacity by reading the output. */
5347 while( actual_capacity > sizeof( output ) )
5348 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005349 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005350 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005351 actual_capacity -= sizeof( output );
5352 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005353 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005354 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005355 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005356 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005357
Gilles Peskine59685592018-09-18 12:11:34 +02005358exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005359 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005360 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005361 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005362}
5363/* END_CASE */
5364
5365/* BEGIN_CASE */
5366void key_agreement_output( int alg_arg,
5367 int our_key_type_arg, data_t *our_key_data,
5368 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005369 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005370{
Ronald Cron5425a212020-08-04 14:58:35 +02005371 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005372 psa_algorithm_t alg = alg_arg;
5373 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005374 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005376 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005377
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005378 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5379 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005380
Gilles Peskine8817f612018-12-18 00:18:46 +01005381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005382
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005383 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5384 psa_set_key_algorithm( &attributes, alg );
5385 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005386 PSA_ASSERT( psa_import_key( &attributes,
5387 our_key_data->x, our_key_data->len,
5388 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005389
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005390 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005391 PSA_ASSERT( psa_key_derivation_key_agreement(
5392 &operation,
5393 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5394 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005395 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5396 {
5397 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005398 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005399 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005400 NULL, 0 ) );
5401 }
Gilles Peskine59685592018-09-18 12:11:34 +02005402
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005403 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005404 actual_output,
5405 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005406 ASSERT_COMPARE( actual_output, expected_output1->len,
5407 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005408 if( expected_output2->len != 0 )
5409 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005410 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005411 actual_output,
5412 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005413 ASSERT_COMPARE( actual_output, expected_output2->len,
5414 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005415 }
Gilles Peskine59685592018-09-18 12:11:34 +02005416
5417exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005418 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005419 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005420 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005421 mbedtls_free( actual_output );
5422}
5423/* END_CASE */
5424
5425/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005426void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005427{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005428 size_t bytes = bytes_arg;
5429 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005430 unsigned char *output = NULL;
5431 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005432 size_t i;
5433 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005434
Simon Butcher49f8e312020-03-03 15:51:50 +00005435 TEST_ASSERT( bytes_arg >= 0 );
5436
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005437 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5438 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005439 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005440
Gilles Peskine8817f612018-12-18 00:18:46 +01005441 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005442
Gilles Peskinea50d7392018-06-21 10:22:13 +02005443 /* Run several times, to ensure that every output byte will be
5444 * nonzero at least once with overwhelming probability
5445 * (2^(-8*number_of_runs)). */
5446 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005447 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005448 if( bytes != 0 )
5449 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005450 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005451
5452 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005453 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5454 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005455
5456 for( i = 0; i < bytes; i++ )
5457 {
5458 if( output[i] != 0 )
5459 ++changed[i];
5460 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005461 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005462
5463 /* Check that every byte was changed to nonzero at least once. This
5464 * validates that psa_generate_random is overwriting every byte of
5465 * the output buffer. */
5466 for( i = 0; i < bytes; i++ )
5467 {
5468 TEST_ASSERT( changed[i] != 0 );
5469 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005470
5471exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005472 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005473 mbedtls_free( output );
5474 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005475}
5476/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005477
5478/* BEGIN_CASE */
5479void generate_key( int type_arg,
5480 int bits_arg,
5481 int usage_arg,
5482 int alg_arg,
5483 int expected_status_arg )
5484{
Ronald Cron5425a212020-08-04 14:58:35 +02005485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005486 psa_key_type_t type = type_arg;
5487 psa_key_usage_t usage = usage_arg;
5488 size_t bits = bits_arg;
5489 psa_algorithm_t alg = alg_arg;
5490 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005491 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005492 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005493
Gilles Peskine8817f612018-12-18 00:18:46 +01005494 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005495
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005496 psa_set_key_usage_flags( &attributes, usage );
5497 psa_set_key_algorithm( &attributes, alg );
5498 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005499 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005500
5501 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005502 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005503 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005504 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005505
5506 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005507 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005508 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5509 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005510
Gilles Peskine818ca122018-06-20 18:16:48 +02005511 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005512 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005513 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005514
5515exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005516 psa_reset_key_attributes( &got_attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005517 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005518 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005519}
5520/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005521
Gilles Peskinee56e8782019-04-26 17:34:02 +02005522/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5523void generate_key_rsa( int bits_arg,
5524 data_t *e_arg,
5525 int expected_status_arg )
5526{
Ronald Cron5425a212020-08-04 14:58:35 +02005527 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005528 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005529 size_t bits = bits_arg;
5530 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5531 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5532 psa_status_t expected_status = expected_status_arg;
5533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5534 uint8_t *exported = NULL;
5535 size_t exported_size =
5536 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5537 size_t exported_length = SIZE_MAX;
5538 uint8_t *e_read_buffer = NULL;
5539 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005540 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005541 size_t e_read_length = SIZE_MAX;
5542
5543 if( e_arg->len == 0 ||
5544 ( e_arg->len == 3 &&
5545 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5546 {
5547 is_default_public_exponent = 1;
5548 e_read_size = 0;
5549 }
5550 ASSERT_ALLOC( e_read_buffer, e_read_size );
5551 ASSERT_ALLOC( exported, exported_size );
5552
5553 PSA_ASSERT( psa_crypto_init( ) );
5554
5555 psa_set_key_usage_flags( &attributes, usage );
5556 psa_set_key_algorithm( &attributes, alg );
5557 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5558 e_arg->x, e_arg->len ) );
5559 psa_set_key_bits( &attributes, bits );
5560
5561 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005562 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005563 if( expected_status != PSA_SUCCESS )
5564 goto exit;
5565
5566 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005567 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005568 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5569 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5570 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5571 e_read_buffer, e_read_size,
5572 &e_read_length ) );
5573 if( is_default_public_exponent )
5574 TEST_EQUAL( e_read_length, 0 );
5575 else
5576 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5577
5578 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005579 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005580 goto exit;
5581
5582 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005583 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005584 exported, exported_size,
5585 &exported_length ) );
5586 {
5587 uint8_t *p = exported;
5588 uint8_t *end = exported + exported_length;
5589 size_t len;
5590 /* RSAPublicKey ::= SEQUENCE {
5591 * modulus INTEGER, -- n
5592 * publicExponent INTEGER } -- e
5593 */
5594 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005595 MBEDTLS_ASN1_SEQUENCE |
5596 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005597 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5598 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5599 MBEDTLS_ASN1_INTEGER ) );
5600 if( len >= 1 && p[0] == 0 )
5601 {
5602 ++p;
5603 --len;
5604 }
5605 if( e_arg->len == 0 )
5606 {
5607 TEST_EQUAL( len, 3 );
5608 TEST_EQUAL( p[0], 1 );
5609 TEST_EQUAL( p[1], 0 );
5610 TEST_EQUAL( p[2], 1 );
5611 }
5612 else
5613 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5614 }
5615
5616exit:
5617 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005618 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005619 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005620 mbedtls_free( e_read_buffer );
5621 mbedtls_free( exported );
5622}
5623/* END_CASE */
5624
Darryl Greend49a4992018-06-18 17:27:26 +01005625/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005626void persistent_key_load_key_from_storage( data_t *data,
5627 int type_arg, int bits_arg,
5628 int usage_flags_arg, int alg_arg,
5629 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005630{
Ronald Cron71016a92020-08-28 19:01:50 +02005631 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005632 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005633 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5634 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005635 psa_key_type_t type = type_arg;
5636 size_t bits = bits_arg;
5637 psa_key_usage_t usage_flags = usage_flags_arg;
5638 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005639 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005640 unsigned char *first_export = NULL;
5641 unsigned char *second_export = NULL;
5642 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5643 size_t first_exported_length;
5644 size_t second_exported_length;
5645
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005646 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5647 {
5648 ASSERT_ALLOC( first_export, export_size );
5649 ASSERT_ALLOC( second_export, export_size );
5650 }
Darryl Greend49a4992018-06-18 17:27:26 +01005651
Gilles Peskine8817f612018-12-18 00:18:46 +01005652 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005653
Gilles Peskinec87af662019-05-15 16:12:22 +02005654 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005655 psa_set_key_usage_flags( &attributes, usage_flags );
5656 psa_set_key_algorithm( &attributes, alg );
5657 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005658 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005659
Darryl Green0c6575a2018-11-07 16:05:30 +00005660 switch( generation_method )
5661 {
5662 case IMPORT_KEY:
5663 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005664 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005665 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005666 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005667
Darryl Green0c6575a2018-11-07 16:05:30 +00005668 case GENERATE_KEY:
5669 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005670 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005671 break;
5672
5673 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005674 {
5675 /* Create base key */
5676 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5677 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5678 psa_set_key_usage_flags( &base_attributes,
5679 PSA_KEY_USAGE_DERIVE );
5680 psa_set_key_algorithm( &base_attributes, derive_alg );
5681 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005682 PSA_ASSERT( psa_import_key( &base_attributes,
5683 data->x, data->len,
5684 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005685 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005687 PSA_ASSERT( psa_key_derivation_input_key(
5688 &operation,
5689 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005690 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005691 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005692 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005693 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5694 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005695 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005696 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005697 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005698 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005699 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005700 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005701 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005702 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005703
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005704 /* Export the key if permitted by the key policy. */
5705 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5706 {
Ronald Cron5425a212020-08-04 14:58:35 +02005707 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005708 first_export, export_size,
5709 &first_exported_length ) );
5710 if( generation_method == IMPORT_KEY )
5711 ASSERT_COMPARE( data->x, data->len,
5712 first_export, first_exported_length );
5713 }
Darryl Greend49a4992018-06-18 17:27:26 +01005714
5715 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005716 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005717 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005718 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005719
Darryl Greend49a4992018-06-18 17:27:26 +01005720 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005721 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005722 TEST_ASSERT( mbedtls_svc_key_id_equal(
5723 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005724 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5725 PSA_KEY_LIFETIME_PERSISTENT );
5726 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5727 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5728 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5729 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005730
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005731 /* Export the key again if permitted by the key policy. */
5732 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005733 {
Ronald Cron5425a212020-08-04 14:58:35 +02005734 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005735 second_export, export_size,
5736 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005737 ASSERT_COMPARE( first_export, first_exported_length,
5738 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005739 }
5740
5741 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005742 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005743 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005744
5745exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005746 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005747 mbedtls_free( first_export );
5748 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005749 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005750 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005751 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005752 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005753}
5754/* END_CASE */