blob: 9b113b48ed3142bff84c37e5ee9bebfe917b3a69 [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. */
115 return( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
116 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 */
248 if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
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 )
2343 target_attributes = source_attributes;
2344 if( target_usage_arg != -1 )
2345 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2346 if( target_alg_arg != -1 )
2347 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002348 if( target_alg2_arg != -1 )
2349 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002350
2351 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002352 PSA_ASSERT( psa_copy_key( source_key,
2353 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002354
2355 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002356 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002357
2358 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002359 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002360 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2361 psa_get_key_type( &target_attributes ) );
2362 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2363 psa_get_key_bits( &target_attributes ) );
2364 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2365 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002366 TEST_EQUAL( expected_alg2,
2367 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002368 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2369 {
2370 size_t length;
2371 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002372 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002373 material->len, &length ) );
2374 ASSERT_COMPARE( material->x, material->len,
2375 export_buffer, length );
2376 }
Ronald Cron5425a212020-08-04 14:58:35 +02002377 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002378 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002379 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002380 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002381
Ronald Cron5425a212020-08-04 14:58:35 +02002382 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002383
2384exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002385 psa_reset_key_attributes( &source_attributes );
2386 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002387 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002388 mbedtls_free( export_buffer );
2389}
2390/* END_CASE */
2391
2392/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002393void copy_fail( int source_usage_arg,
2394 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002395 int type_arg, data_t *material,
2396 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002397 int target_usage_arg,
2398 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002399 int expected_status_arg )
2400{
2401 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2402 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002403 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2404 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002405
2406 PSA_ASSERT( psa_crypto_init( ) );
2407
2408 /* Prepare the source key. */
2409 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2410 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002411 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002412 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002413 PSA_ASSERT( psa_import_key( &source_attributes,
2414 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002415 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002416
2417 /* Prepare the target attributes. */
2418 psa_set_key_type( &target_attributes, target_type_arg );
2419 psa_set_key_bits( &target_attributes, target_bits_arg );
2420 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2421 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002422 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002423
2424 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002425 TEST_EQUAL( psa_copy_key( source_key,
2426 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002427 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002428
Ronald Cron5425a212020-08-04 14:58:35 +02002429 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002430
Gilles Peskine4a644642019-05-03 17:14:08 +02002431exit:
2432 psa_reset_key_attributes( &source_attributes );
2433 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002434 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002435}
2436/* END_CASE */
2437
2438/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002439void hash_operation_init( )
2440{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002441 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002442 /* Test each valid way of initializing the object, except for `= {0}`, as
2443 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2444 * though it's OK by the C standard. We could test for this, but we'd need
2445 * to supress the Clang warning for the test. */
2446 psa_hash_operation_t func = psa_hash_operation_init( );
2447 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2448 psa_hash_operation_t zero;
2449
2450 memset( &zero, 0, sizeof( zero ) );
2451
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002452 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002453 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2454 PSA_ERROR_BAD_STATE );
2455 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2456 PSA_ERROR_BAD_STATE );
2457 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2458 PSA_ERROR_BAD_STATE );
2459
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002460 /* A default hash operation should be abortable without error. */
2461 PSA_ASSERT( psa_hash_abort( &func ) );
2462 PSA_ASSERT( psa_hash_abort( &init ) );
2463 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002464}
2465/* END_CASE */
2466
2467/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002468void hash_setup( int alg_arg,
2469 int expected_status_arg )
2470{
2471 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002472 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002473 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002474 psa_status_t status;
2475
Gilles Peskine8817f612018-12-18 00:18:46 +01002476 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002477
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002478 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002479 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002480
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002481 /* Whether setup succeeded or failed, abort must succeed. */
2482 PSA_ASSERT( psa_hash_abort( &operation ) );
2483
2484 /* If setup failed, reproduce the failure, so as to
2485 * test the resulting state of the operation object. */
2486 if( status != PSA_SUCCESS )
2487 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2488
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002489 /* Now the operation object should be reusable. */
2490#if defined(KNOWN_SUPPORTED_HASH_ALG)
2491 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2492 PSA_ASSERT( psa_hash_abort( &operation ) );
2493#endif
2494
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002495exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002496 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002497}
2498/* END_CASE */
2499
2500/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002501void hash_compute_fail( int alg_arg, data_t *input,
2502 int output_size_arg, int expected_status_arg )
2503{
2504 psa_algorithm_t alg = alg_arg;
2505 uint8_t *output = NULL;
2506 size_t output_size = output_size_arg;
2507 size_t output_length = INVALID_EXPORT_LENGTH;
2508 psa_status_t expected_status = expected_status_arg;
2509 psa_status_t status;
2510
2511 ASSERT_ALLOC( output, output_size );
2512
2513 PSA_ASSERT( psa_crypto_init( ) );
2514
2515 status = psa_hash_compute( alg, input->x, input->len,
2516 output, output_size, &output_length );
2517 TEST_EQUAL( status, expected_status );
2518 TEST_ASSERT( output_length <= output_size );
2519
2520exit:
2521 mbedtls_free( output );
2522 PSA_DONE( );
2523}
2524/* END_CASE */
2525
2526/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002527void hash_compare_fail( int alg_arg, data_t *input,
2528 data_t *reference_hash,
2529 int expected_status_arg )
2530{
2531 psa_algorithm_t alg = alg_arg;
2532 psa_status_t expected_status = expected_status_arg;
2533 psa_status_t status;
2534
2535 PSA_ASSERT( psa_crypto_init( ) );
2536
2537 status = psa_hash_compare( alg, input->x, input->len,
2538 reference_hash->x, reference_hash->len );
2539 TEST_EQUAL( status, expected_status );
2540
2541exit:
2542 PSA_DONE( );
2543}
2544/* END_CASE */
2545
2546/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002547void hash_compute_compare( int alg_arg, data_t *input,
2548 data_t *expected_output )
2549{
2550 psa_algorithm_t alg = alg_arg;
2551 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2552 size_t output_length = INVALID_EXPORT_LENGTH;
2553 size_t i;
2554
2555 PSA_ASSERT( psa_crypto_init( ) );
2556
2557 /* Compute with tight buffer */
2558 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2559 output, PSA_HASH_SIZE( alg ),
2560 &output_length ) );
2561 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2562 ASSERT_COMPARE( output, output_length,
2563 expected_output->x, expected_output->len );
2564
2565 /* Compute with larger buffer */
2566 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2567 output, sizeof( output ),
2568 &output_length ) );
2569 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2570 ASSERT_COMPARE( output, output_length,
2571 expected_output->x, expected_output->len );
2572
2573 /* Compare with correct hash */
2574 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2575 output, output_length ) );
2576
2577 /* Compare with trailing garbage */
2578 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2579 output, output_length + 1 ),
2580 PSA_ERROR_INVALID_SIGNATURE );
2581
2582 /* Compare with truncated hash */
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 corrupted value */
2588 for( i = 0; i < output_length; i++ )
2589 {
2590 test_set_step( i );
2591 output[i] ^= 1;
2592 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2593 output, output_length ),
2594 PSA_ERROR_INVALID_SIGNATURE );
2595 output[i] ^= 1;
2596 }
2597
2598exit:
2599 PSA_DONE( );
2600}
2601/* END_CASE */
2602
2603/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002604void hash_bad_order( )
2605{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002606 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002607 unsigned char input[] = "";
2608 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002609 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002610 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2611 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2612 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002613 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002614 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002615 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002616
Gilles Peskine8817f612018-12-18 00:18:46 +01002617 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002618
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002619 /* Call setup twice in a row. */
2620 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2621 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2622 PSA_ERROR_BAD_STATE );
2623 PSA_ASSERT( psa_hash_abort( &operation ) );
2624
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002625 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002626 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002627 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002628 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002629
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002630 /* Call update after finish. */
2631 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2632 PSA_ASSERT( psa_hash_finish( &operation,
2633 hash, sizeof( hash ), &hash_len ) );
2634 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002635 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002636 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002637
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002638 /* Call verify without calling setup beforehand. */
2639 TEST_EQUAL( psa_hash_verify( &operation,
2640 valid_hash, sizeof( valid_hash ) ),
2641 PSA_ERROR_BAD_STATE );
2642 PSA_ASSERT( psa_hash_abort( &operation ) );
2643
2644 /* Call verify after finish. */
2645 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2646 PSA_ASSERT( psa_hash_finish( &operation,
2647 hash, sizeof( hash ), &hash_len ) );
2648 TEST_EQUAL( psa_hash_verify( &operation,
2649 valid_hash, sizeof( valid_hash ) ),
2650 PSA_ERROR_BAD_STATE );
2651 PSA_ASSERT( psa_hash_abort( &operation ) );
2652
2653 /* Call verify twice in a row. */
2654 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2655 PSA_ASSERT( psa_hash_verify( &operation,
2656 valid_hash, sizeof( valid_hash ) ) );
2657 TEST_EQUAL( psa_hash_verify( &operation,
2658 valid_hash, sizeof( valid_hash ) ),
2659 PSA_ERROR_BAD_STATE );
2660 PSA_ASSERT( psa_hash_abort( &operation ) );
2661
2662 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002663 TEST_EQUAL( psa_hash_finish( &operation,
2664 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002665 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002666 PSA_ASSERT( psa_hash_abort( &operation ) );
2667
2668 /* Call finish twice in a row. */
2669 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2670 PSA_ASSERT( psa_hash_finish( &operation,
2671 hash, sizeof( hash ), &hash_len ) );
2672 TEST_EQUAL( psa_hash_finish( &operation,
2673 hash, sizeof( hash ), &hash_len ),
2674 PSA_ERROR_BAD_STATE );
2675 PSA_ASSERT( psa_hash_abort( &operation ) );
2676
2677 /* Call finish after calling verify. */
2678 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2679 PSA_ASSERT( psa_hash_verify( &operation,
2680 valid_hash, sizeof( valid_hash ) ) );
2681 TEST_EQUAL( psa_hash_finish( &operation,
2682 hash, sizeof( hash ), &hash_len ),
2683 PSA_ERROR_BAD_STATE );
2684 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002685
2686exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002687 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002688}
2689/* END_CASE */
2690
itayzafrir27e69452018-11-01 14:26:34 +02002691/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2692void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002693{
2694 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002695 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2696 * appended to it */
2697 unsigned char hash[] = {
2698 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2699 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2700 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002701 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002702 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002703
Gilles Peskine8817f612018-12-18 00:18:46 +01002704 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002705
itayzafrir27e69452018-11-01 14:26:34 +02002706 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002707 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002708 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002709 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002710
itayzafrir27e69452018-11-01 14:26:34 +02002711 /* psa_hash_verify with a non-matching hash */
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 + 1, expected_size ),
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 hash longer than expected */
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, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002719 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002720
itayzafrirec93d302018-10-18 18:01:10 +03002721exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002722 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002723}
2724/* END_CASE */
2725
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002726/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2727void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002728{
2729 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002730 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002731 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002732 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002733 size_t hash_len;
2734
Gilles Peskine8817f612018-12-18 00:18:46 +01002735 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002736
itayzafrir58028322018-10-25 10:22:01 +03002737 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002738 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002739 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002740 hash, expected_size - 1, &hash_len ),
2741 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002742
2743exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002744 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002745}
2746/* END_CASE */
2747
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002748/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2749void hash_clone_source_state( )
2750{
2751 psa_algorithm_t alg = PSA_ALG_SHA_256;
2752 unsigned char hash[PSA_HASH_MAX_SIZE];
2753 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2754 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2755 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2756 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2757 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2758 size_t hash_len;
2759
2760 PSA_ASSERT( psa_crypto_init( ) );
2761 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2762
2763 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2764 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2765 PSA_ASSERT( psa_hash_finish( &op_finished,
2766 hash, sizeof( hash ), &hash_len ) );
2767 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2768 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2769
2770 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2771 PSA_ERROR_BAD_STATE );
2772
2773 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2774 PSA_ASSERT( psa_hash_finish( &op_init,
2775 hash, sizeof( hash ), &hash_len ) );
2776 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2777 PSA_ASSERT( psa_hash_finish( &op_finished,
2778 hash, sizeof( hash ), &hash_len ) );
2779 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2780 PSA_ASSERT( psa_hash_finish( &op_aborted,
2781 hash, sizeof( hash ), &hash_len ) );
2782
2783exit:
2784 psa_hash_abort( &op_source );
2785 psa_hash_abort( &op_init );
2786 psa_hash_abort( &op_setup );
2787 psa_hash_abort( &op_finished );
2788 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002789 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002790}
2791/* END_CASE */
2792
2793/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2794void hash_clone_target_state( )
2795{
2796 psa_algorithm_t alg = PSA_ALG_SHA_256;
2797 unsigned char hash[PSA_HASH_MAX_SIZE];
2798 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2799 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2800 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2801 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2802 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2803 size_t hash_len;
2804
2805 PSA_ASSERT( psa_crypto_init( ) );
2806
2807 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2808 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2809 PSA_ASSERT( psa_hash_finish( &op_finished,
2810 hash, sizeof( hash ), &hash_len ) );
2811 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2812 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2813
2814 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2815 PSA_ASSERT( psa_hash_finish( &op_target,
2816 hash, sizeof( hash ), &hash_len ) );
2817
2818 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2819 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2820 PSA_ERROR_BAD_STATE );
2821 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2822 PSA_ERROR_BAD_STATE );
2823
2824exit:
2825 psa_hash_abort( &op_target );
2826 psa_hash_abort( &op_init );
2827 psa_hash_abort( &op_setup );
2828 psa_hash_abort( &op_finished );
2829 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002830 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002831}
2832/* END_CASE */
2833
itayzafrir58028322018-10-25 10:22:01 +03002834/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002835void mac_operation_init( )
2836{
Jaeden Amero252ef282019-02-15 14:05:35 +00002837 const uint8_t input[1] = { 0 };
2838
Jaeden Amero769ce272019-01-04 11:48:03 +00002839 /* Test each valid way of initializing the object, except for `= {0}`, as
2840 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2841 * though it's OK by the C standard. We could test for this, but we'd need
2842 * to supress the Clang warning for the test. */
2843 psa_mac_operation_t func = psa_mac_operation_init( );
2844 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2845 psa_mac_operation_t zero;
2846
2847 memset( &zero, 0, sizeof( zero ) );
2848
Jaeden Amero252ef282019-02-15 14:05:35 +00002849 /* A freshly-initialized MAC operation should not be usable. */
2850 TEST_EQUAL( psa_mac_update( &func,
2851 input, sizeof( input ) ),
2852 PSA_ERROR_BAD_STATE );
2853 TEST_EQUAL( psa_mac_update( &init,
2854 input, sizeof( input ) ),
2855 PSA_ERROR_BAD_STATE );
2856 TEST_EQUAL( psa_mac_update( &zero,
2857 input, sizeof( input ) ),
2858 PSA_ERROR_BAD_STATE );
2859
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002860 /* A default MAC operation should be abortable without error. */
2861 PSA_ASSERT( psa_mac_abort( &func ) );
2862 PSA_ASSERT( psa_mac_abort( &init ) );
2863 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002864}
2865/* END_CASE */
2866
2867/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002868void mac_setup( int key_type_arg,
2869 data_t *key,
2870 int alg_arg,
2871 int expected_status_arg )
2872{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002873 psa_key_type_t key_type = key_type_arg;
2874 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002875 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002876 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002877 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2878#if defined(KNOWN_SUPPORTED_MAC_ALG)
2879 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2880#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002881
Gilles Peskine8817f612018-12-18 00:18:46 +01002882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002883
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002884 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2885 &operation, &status ) )
2886 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002887 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002888
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002889 /* The operation object should be reusable. */
2890#if defined(KNOWN_SUPPORTED_MAC_ALG)
2891 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2892 smoke_test_key_data,
2893 sizeof( smoke_test_key_data ),
2894 KNOWN_SUPPORTED_MAC_ALG,
2895 &operation, &status ) )
2896 goto exit;
2897 TEST_EQUAL( status, PSA_SUCCESS );
2898#endif
2899
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002900exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002901 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002902}
2903/* END_CASE */
2904
2905/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002906void mac_bad_order( )
2907{
Ronald Cron5425a212020-08-04 14:58:35 +02002908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002909 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2910 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002911 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002912 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2913 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2914 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002916 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2917 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2918 size_t sign_mac_length = 0;
2919 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2920 const uint8_t verify_mac[] = {
2921 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2922 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2923 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2924
2925 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002926 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002927 psa_set_key_algorithm( &attributes, alg );
2928 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002929
Ronald Cron5425a212020-08-04 14:58:35 +02002930 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2931 &key ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002932
Jaeden Amero252ef282019-02-15 14:05:35 +00002933 /* Call update without calling setup beforehand. */
2934 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2935 PSA_ERROR_BAD_STATE );
2936 PSA_ASSERT( psa_mac_abort( &operation ) );
2937
2938 /* Call sign finish without calling setup beforehand. */
2939 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2940 &sign_mac_length),
2941 PSA_ERROR_BAD_STATE );
2942 PSA_ASSERT( psa_mac_abort( &operation ) );
2943
2944 /* Call verify finish without calling setup beforehand. */
2945 TEST_EQUAL( psa_mac_verify_finish( &operation,
2946 verify_mac, sizeof( verify_mac ) ),
2947 PSA_ERROR_BAD_STATE );
2948 PSA_ASSERT( psa_mac_abort( &operation ) );
2949
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002950 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002951 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2952 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002953 PSA_ERROR_BAD_STATE );
2954 PSA_ASSERT( psa_mac_abort( &operation ) );
2955
Jaeden Amero252ef282019-02-15 14:05:35 +00002956 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002957 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002958 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2959 PSA_ASSERT( psa_mac_sign_finish( &operation,
2960 sign_mac, sizeof( sign_mac ),
2961 &sign_mac_length ) );
2962 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2963 PSA_ERROR_BAD_STATE );
2964 PSA_ASSERT( psa_mac_abort( &operation ) );
2965
2966 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002967 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002968 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2969 PSA_ASSERT( psa_mac_verify_finish( &operation,
2970 verify_mac, sizeof( verify_mac ) ) );
2971 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2972 PSA_ERROR_BAD_STATE );
2973 PSA_ASSERT( psa_mac_abort( &operation ) );
2974
2975 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002976 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002977 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2978 PSA_ASSERT( psa_mac_sign_finish( &operation,
2979 sign_mac, sizeof( sign_mac ),
2980 &sign_mac_length ) );
2981 TEST_EQUAL( psa_mac_sign_finish( &operation,
2982 sign_mac, sizeof( sign_mac ),
2983 &sign_mac_length ),
2984 PSA_ERROR_BAD_STATE );
2985 PSA_ASSERT( psa_mac_abort( &operation ) );
2986
2987 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002988 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002989 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2990 PSA_ASSERT( psa_mac_verify_finish( &operation,
2991 verify_mac, sizeof( verify_mac ) ) );
2992 TEST_EQUAL( psa_mac_verify_finish( &operation,
2993 verify_mac, sizeof( verify_mac ) ),
2994 PSA_ERROR_BAD_STATE );
2995 PSA_ASSERT( psa_mac_abort( &operation ) );
2996
2997 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002998 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002999 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3000 TEST_EQUAL( psa_mac_verify_finish( &operation,
3001 verify_mac, sizeof( verify_mac ) ),
3002 PSA_ERROR_BAD_STATE );
3003 PSA_ASSERT( psa_mac_abort( &operation ) );
3004
3005 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003006 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003007 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3008 TEST_EQUAL( psa_mac_sign_finish( &operation,
3009 sign_mac, sizeof( sign_mac ),
3010 &sign_mac_length ),
3011 PSA_ERROR_BAD_STATE );
3012 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003013
Ronald Cron5425a212020-08-04 14:58:35 +02003014 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003015
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003016exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003017 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003018}
3019/* END_CASE */
3020
3021/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003022void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003023 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003024 int alg_arg,
3025 data_t *input,
3026 data_t *expected_mac )
3027{
Ronald Cron5425a212020-08-04 14:58:35 +02003028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003029 psa_key_type_t key_type = key_type_arg;
3030 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003031 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003033 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003034 size_t mac_buffer_size =
Ronald Cron5425a212020-08-04 14:58:35 +02003035 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003036 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003037 const size_t output_sizes_to_test[] = {
3038 0,
3039 1,
3040 expected_mac->len - 1,
3041 expected_mac->len,
3042 expected_mac->len + 1,
3043 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003044
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003045 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
Gilles Peskine3d404d62020-08-25 23:47:36 +02003046 /* We expect PSA_MAC_FINAL_SIZE to be exact. */
3047 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003048
Gilles Peskine8817f612018-12-18 00:18:46 +01003049 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003050
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003051 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003052 psa_set_key_algorithm( &attributes, alg );
3053 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003054
Ronald Cron5425a212020-08-04 14:58:35 +02003055 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3056 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003057
Gilles Peskine8b356b52020-08-25 23:44:59 +02003058 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3059 {
3060 const size_t output_size = output_sizes_to_test[i];
3061 psa_status_t expected_status =
3062 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3063 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003064
Gilles Peskine8b356b52020-08-25 23:44:59 +02003065 test_set_step( output_size );
3066 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003067
Gilles Peskine8b356b52020-08-25 23:44:59 +02003068 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003069 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003070 PSA_ASSERT( psa_mac_update( &operation,
3071 input->x, input->len ) );
3072 TEST_EQUAL( psa_mac_sign_finish( &operation,
3073 actual_mac, output_size,
3074 &mac_length ),
3075 expected_status );
3076 PSA_ASSERT( psa_mac_abort( &operation ) );
3077
3078 if( expected_status == PSA_SUCCESS )
3079 {
3080 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3081 actual_mac, mac_length );
3082 }
3083 mbedtls_free( actual_mac );
3084 actual_mac = NULL;
3085 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003086
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003087exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003088 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003089 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003090 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003091 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003092}
3093/* END_CASE */
3094
3095/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003096void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003097 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003098 int alg_arg,
3099 data_t *input,
3100 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003101{
Ronald Cron5425a212020-08-04 14:58:35 +02003102 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003103 psa_key_type_t key_type = key_type_arg;
3104 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003105 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003107 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003108
Gilles Peskine69c12672018-06-28 00:07:19 +02003109 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3110
Gilles Peskine8817f612018-12-18 00:18:46 +01003111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003112
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003113 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003114 psa_set_key_algorithm( &attributes, alg );
3115 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003116
Ronald Cron5425a212020-08-04 14:58:35 +02003117 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3118 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003119
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003120 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003121 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003122 PSA_ASSERT( psa_mac_update( &operation,
3123 input->x, input->len ) );
3124 PSA_ASSERT( psa_mac_verify_finish( &operation,
3125 expected_mac->x,
3126 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003128 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003129 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003130 PSA_ASSERT( psa_mac_update( &operation,
3131 input->x, input->len ) );
3132 TEST_EQUAL( psa_mac_verify_finish( &operation,
3133 expected_mac->x,
3134 expected_mac->len - 1 ),
3135 PSA_ERROR_INVALID_SIGNATURE );
3136
3137 /* Test a MAC that's too long. */
3138 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3139 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003140 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003141 PSA_ASSERT( psa_mac_update( &operation,
3142 input->x, input->len ) );
3143 TEST_EQUAL( psa_mac_verify_finish( &operation,
3144 perturbed_mac,
3145 expected_mac->len + 1 ),
3146 PSA_ERROR_INVALID_SIGNATURE );
3147
3148 /* Test changing one byte. */
3149 for( size_t i = 0; i < expected_mac->len; i++ )
3150 {
3151 test_set_step( i );
3152 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003153 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003154 PSA_ASSERT( psa_mac_update( &operation,
3155 input->x, input->len ) );
3156 TEST_EQUAL( psa_mac_verify_finish( &operation,
3157 perturbed_mac,
3158 expected_mac->len ),
3159 PSA_ERROR_INVALID_SIGNATURE );
3160 perturbed_mac[i] ^= 1;
3161 }
3162
Gilles Peskine8c9def32018-02-08 10:02:12 +01003163exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003164 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003165 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003166 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003167 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003168}
3169/* END_CASE */
3170
3171/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003172void cipher_operation_init( )
3173{
Jaeden Ameroab439972019-02-15 14:12:05 +00003174 const uint8_t input[1] = { 0 };
3175 unsigned char output[1] = { 0 };
3176 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003177 /* Test each valid way of initializing the object, except for `= {0}`, as
3178 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3179 * though it's OK by the C standard. We could test for this, but we'd need
3180 * to supress the Clang warning for the test. */
3181 psa_cipher_operation_t func = psa_cipher_operation_init( );
3182 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3183 psa_cipher_operation_t zero;
3184
3185 memset( &zero, 0, sizeof( zero ) );
3186
Jaeden Ameroab439972019-02-15 14:12:05 +00003187 /* A freshly-initialized cipher operation should not be usable. */
3188 TEST_EQUAL( psa_cipher_update( &func,
3189 input, sizeof( input ),
3190 output, sizeof( output ),
3191 &output_length ),
3192 PSA_ERROR_BAD_STATE );
3193 TEST_EQUAL( psa_cipher_update( &init,
3194 input, sizeof( input ),
3195 output, sizeof( output ),
3196 &output_length ),
3197 PSA_ERROR_BAD_STATE );
3198 TEST_EQUAL( psa_cipher_update( &zero,
3199 input, sizeof( input ),
3200 output, sizeof( output ),
3201 &output_length ),
3202 PSA_ERROR_BAD_STATE );
3203
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003204 /* A default cipher operation should be abortable without error. */
3205 PSA_ASSERT( psa_cipher_abort( &func ) );
3206 PSA_ASSERT( psa_cipher_abort( &init ) );
3207 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003208}
3209/* END_CASE */
3210
3211/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003212void cipher_setup( int key_type_arg,
3213 data_t *key,
3214 int alg_arg,
3215 int expected_status_arg )
3216{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003217 psa_key_type_t key_type = key_type_arg;
3218 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003219 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003220 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003221 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003222#if defined(KNOWN_SUPPORTED_MAC_ALG)
3223 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3224#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003225
Gilles Peskine8817f612018-12-18 00:18:46 +01003226 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003227
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003228 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3229 &operation, &status ) )
3230 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003231 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003232
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003233 /* The operation object should be reusable. */
3234#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3235 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3236 smoke_test_key_data,
3237 sizeof( smoke_test_key_data ),
3238 KNOWN_SUPPORTED_CIPHER_ALG,
3239 &operation, &status ) )
3240 goto exit;
3241 TEST_EQUAL( status, PSA_SUCCESS );
3242#endif
3243
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003244exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003245 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003246 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003247}
3248/* END_CASE */
3249
3250/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003251void cipher_bad_order( )
3252{
Ronald Cron5425a212020-08-04 14:58:35 +02003253 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003254 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3255 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003257 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3258 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003259 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003260 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3261 0xaa, 0xaa, 0xaa, 0xaa };
3262 const uint8_t text[] = {
3263 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3264 0xbb, 0xbb, 0xbb, 0xbb };
3265 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3266 size_t length = 0;
3267
3268 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003269 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3270 psa_set_key_algorithm( &attributes, alg );
3271 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003272 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3273 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003274
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003275 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003276 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3277 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003278 PSA_ERROR_BAD_STATE );
3279 PSA_ASSERT( psa_cipher_abort( &operation ) );
3280
3281 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003282 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3283 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003284 PSA_ERROR_BAD_STATE );
3285 PSA_ASSERT( psa_cipher_abort( &operation ) );
3286
Jaeden Ameroab439972019-02-15 14:12:05 +00003287 /* Generate an IV without calling setup beforehand. */
3288 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3289 buffer, sizeof( buffer ),
3290 &length ),
3291 PSA_ERROR_BAD_STATE );
3292 PSA_ASSERT( psa_cipher_abort( &operation ) );
3293
3294 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003295 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003296 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3297 buffer, sizeof( buffer ),
3298 &length ) );
3299 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3300 buffer, sizeof( buffer ),
3301 &length ),
3302 PSA_ERROR_BAD_STATE );
3303 PSA_ASSERT( psa_cipher_abort( &operation ) );
3304
3305 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003306 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003307 PSA_ASSERT( psa_cipher_set_iv( &operation,
3308 iv, sizeof( iv ) ) );
3309 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3310 buffer, sizeof( buffer ),
3311 &length ),
3312 PSA_ERROR_BAD_STATE );
3313 PSA_ASSERT( psa_cipher_abort( &operation ) );
3314
3315 /* Set an IV without calling setup beforehand. */
3316 TEST_EQUAL( psa_cipher_set_iv( &operation,
3317 iv, sizeof( iv ) ),
3318 PSA_ERROR_BAD_STATE );
3319 PSA_ASSERT( psa_cipher_abort( &operation ) );
3320
3321 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003322 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003323 PSA_ASSERT( psa_cipher_set_iv( &operation,
3324 iv, sizeof( iv ) ) );
3325 TEST_EQUAL( psa_cipher_set_iv( &operation,
3326 iv, sizeof( iv ) ),
3327 PSA_ERROR_BAD_STATE );
3328 PSA_ASSERT( psa_cipher_abort( &operation ) );
3329
3330 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003331 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003332 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3333 buffer, sizeof( buffer ),
3334 &length ) );
3335 TEST_EQUAL( psa_cipher_set_iv( &operation,
3336 iv, sizeof( iv ) ),
3337 PSA_ERROR_BAD_STATE );
3338 PSA_ASSERT( psa_cipher_abort( &operation ) );
3339
3340 /* Call update without calling setup beforehand. */
3341 TEST_EQUAL( psa_cipher_update( &operation,
3342 text, sizeof( text ),
3343 buffer, sizeof( buffer ),
3344 &length ),
3345 PSA_ERROR_BAD_STATE );
3346 PSA_ASSERT( psa_cipher_abort( &operation ) );
3347
3348 /* Call update without an IV where an IV is required. */
3349 TEST_EQUAL( psa_cipher_update( &operation,
3350 text, sizeof( text ),
3351 buffer, sizeof( buffer ),
3352 &length ),
3353 PSA_ERROR_BAD_STATE );
3354 PSA_ASSERT( psa_cipher_abort( &operation ) );
3355
3356 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003357 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003358 PSA_ASSERT( psa_cipher_set_iv( &operation,
3359 iv, sizeof( iv ) ) );
3360 PSA_ASSERT( psa_cipher_finish( &operation,
3361 buffer, sizeof( buffer ), &length ) );
3362 TEST_EQUAL( psa_cipher_update( &operation,
3363 text, sizeof( text ),
3364 buffer, sizeof( buffer ),
3365 &length ),
3366 PSA_ERROR_BAD_STATE );
3367 PSA_ASSERT( psa_cipher_abort( &operation ) );
3368
3369 /* Call finish without calling setup beforehand. */
3370 TEST_EQUAL( psa_cipher_finish( &operation,
3371 buffer, sizeof( buffer ), &length ),
3372 PSA_ERROR_BAD_STATE );
3373 PSA_ASSERT( psa_cipher_abort( &operation ) );
3374
3375 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003376 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003377 /* Not calling update means we are encrypting an empty buffer, which is OK
3378 * for cipher modes with padding. */
3379 TEST_EQUAL( psa_cipher_finish( &operation,
3380 buffer, sizeof( buffer ), &length ),
3381 PSA_ERROR_BAD_STATE );
3382 PSA_ASSERT( psa_cipher_abort( &operation ) );
3383
3384 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003385 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003386 PSA_ASSERT( psa_cipher_set_iv( &operation,
3387 iv, sizeof( iv ) ) );
3388 PSA_ASSERT( psa_cipher_finish( &operation,
3389 buffer, sizeof( buffer ), &length ) );
3390 TEST_EQUAL( psa_cipher_finish( &operation,
3391 buffer, sizeof( buffer ), &length ),
3392 PSA_ERROR_BAD_STATE );
3393 PSA_ASSERT( psa_cipher_abort( &operation ) );
3394
Ronald Cron5425a212020-08-04 14:58:35 +02003395 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003396
Jaeden Ameroab439972019-02-15 14:12:05 +00003397exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003398 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003399 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003400}
3401/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003402
Gilles Peskine50e586b2018-06-08 14:28:46 +02003403/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003404void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003405 data_t *key_data, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003406 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003407 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003408{
Ronald Cron5425a212020-08-04 14:58:35 +02003409 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003410 psa_status_t status;
3411 psa_key_type_t key_type = key_type_arg;
3412 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003413 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003414 unsigned char *output = NULL;
3415 size_t output_buffer_size = 0;
3416 size_t function_output_length = 0;
3417 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003418 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003420
Gilles Peskine8817f612018-12-18 00:18:46 +01003421 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003422
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003423 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3424 psa_set_key_algorithm( &attributes, alg );
3425 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003426
Ronald Cron5425a212020-08-04 14:58:35 +02003427 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3428 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003429
Ronald Cron5425a212020-08-04 14:58:35 +02003430 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003431
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003432 if( iv->len > 0 )
3433 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003434 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003435 }
3436
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003437 output_buffer_size = ( (size_t) input->len +
3438 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003439 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003440
Gilles Peskine8817f612018-12-18 00:18:46 +01003441 PSA_ASSERT( psa_cipher_update( &operation,
3442 input->x, input->len,
3443 output, output_buffer_size,
3444 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003445 total_output_length += function_output_length;
3446 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003447 output + total_output_length,
3448 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003449 &function_output_length );
3450 total_output_length += function_output_length;
3451
Gilles Peskinefe11b722018-12-18 00:24:04 +01003452 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003453 if( expected_status == PSA_SUCCESS )
3454 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003455 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003456 ASSERT_COMPARE( expected_output->x, expected_output->len,
3457 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003458 }
3459
3460exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003461 psa_cipher_abort( &operation );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003462 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003463 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003464 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003465}
3466/* END_CASE */
3467
3468/* BEGIN_CASE */
3469void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003470 data_t *key_data, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003471 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003472 int first_part_size_arg,
3473 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003474 data_t *expected_output )
3475{
Ronald Cron5425a212020-08-04 14:58:35 +02003476 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003477 psa_key_type_t key_type = key_type_arg;
3478 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003479 size_t first_part_size = first_part_size_arg;
3480 size_t output1_length = output1_length_arg;
3481 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003482 unsigned char *output = NULL;
3483 size_t output_buffer_size = 0;
3484 size_t function_output_length = 0;
3485 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003486 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003488
Gilles Peskine8817f612018-12-18 00:18:46 +01003489 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003490
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003491 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3492 psa_set_key_algorithm( &attributes, alg );
3493 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003494
Ronald Cron5425a212020-08-04 14:58:35 +02003495 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3496 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003497
Ronald Cron5425a212020-08-04 14:58:35 +02003498 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003499
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003500 if( iv->len > 0 )
3501 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003502 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003503 }
3504
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003505 output_buffer_size = ( (size_t) input->len +
3506 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003507 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003508
Gilles Peskinee0866522019-02-19 19:44:00 +01003509 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003510 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3511 output, output_buffer_size,
3512 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003513 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003514 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003515 PSA_ASSERT( psa_cipher_update( &operation,
3516 input->x + first_part_size,
3517 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003518 output + total_output_length,
3519 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003520 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003521 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003522 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003523 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003524 output + total_output_length,
3525 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003526 &function_output_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_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003529
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003530 ASSERT_COMPARE( expected_output->x, expected_output->len,
3531 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003532
3533exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003534 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003535 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003536 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003537 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003538}
3539/* END_CASE */
3540
3541/* BEGIN_CASE */
3542void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003543 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003544 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003545 int first_part_size_arg,
3546 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003547 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003548{
Ronald Cron5425a212020-08-04 14:58:35 +02003549 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003550 psa_key_type_t key_type = key_type_arg;
3551 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003552 size_t first_part_size = first_part_size_arg;
3553 size_t output1_length = output1_length_arg;
3554 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003555 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003556 size_t output_buffer_size = 0;
3557 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003558 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003559 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003561
Gilles Peskine8817f612018-12-18 00:18:46 +01003562 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003563
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003564 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3565 psa_set_key_algorithm( &attributes, alg );
3566 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003567
Ronald Cron5425a212020-08-04 14:58:35 +02003568 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3569 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003570
Ronald Cron5425a212020-08-04 14:58:35 +02003571 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003572
Steven Cooreman177deba2020-09-07 17:14:14 +02003573 if( iv->len > 0 )
3574 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003575 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003576 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003577
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003578 output_buffer_size = ( (size_t) input->len +
3579 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003580 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003581
Gilles Peskinee0866522019-02-19 19:44:00 +01003582 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003583 PSA_ASSERT( psa_cipher_update( &operation,
3584 input->x, first_part_size,
3585 output, output_buffer_size,
3586 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003587 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003588 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003589 PSA_ASSERT( psa_cipher_update( &operation,
3590 input->x + first_part_size,
3591 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003592 output + total_output_length,
3593 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003594 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003595 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003596 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003597 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003598 output + total_output_length,
3599 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003600 &function_output_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_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003603
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003604 ASSERT_COMPARE( expected_output->x, expected_output->len,
3605 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003606
3607exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003608 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003609 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003610 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003611 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003612}
3613/* END_CASE */
3614
Gilles Peskine50e586b2018-06-08 14:28:46 +02003615/* BEGIN_CASE */
3616void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003617 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003618 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003619 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003620{
Ronald Cron5425a212020-08-04 14:58:35 +02003621 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003622 psa_status_t status;
3623 psa_key_type_t key_type = key_type_arg;
3624 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003625 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003626 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003627 size_t output_buffer_size = 0;
3628 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003629 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003630 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003632
Gilles Peskine8817f612018-12-18 00:18:46 +01003633 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003634
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003635 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3636 psa_set_key_algorithm( &attributes, alg );
3637 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003638
Ronald Cron5425a212020-08-04 14:58:35 +02003639 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3640 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003641
Ronald Cron5425a212020-08-04 14:58:35 +02003642 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003643
Steven Cooreman177deba2020-09-07 17:14:14 +02003644 if( iv->len > 0 )
3645 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003646 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003647 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003648
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003649 output_buffer_size = ( (size_t) input->len +
3650 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003651 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003652
Gilles Peskine8817f612018-12-18 00:18:46 +01003653 PSA_ASSERT( psa_cipher_update( &operation,
3654 input->x, input->len,
3655 output, output_buffer_size,
3656 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003657 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003658 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003659 output + total_output_length,
3660 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003661 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003662 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003663 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003664
3665 if( expected_status == PSA_SUCCESS )
3666 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003667 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003668 ASSERT_COMPARE( expected_output->x, expected_output->len,
3669 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003670 }
3671
Gilles Peskine50e586b2018-06-08 14:28:46 +02003672exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003673 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003674 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003675 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003676 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003677}
3678/* END_CASE */
3679
Gilles Peskine50e586b2018-06-08 14:28:46 +02003680/* BEGIN_CASE */
3681void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003682 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003683 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003684{
Ronald Cron5425a212020-08-04 14:58:35 +02003685 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003686 psa_key_type_t key_type = key_type_arg;
3687 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003688 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003689 size_t iv_size = 16;
3690 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003691 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003692 size_t output1_size = 0;
3693 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003694 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003695 size_t output2_size = 0;
3696 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003697 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003698 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3699 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003701
Gilles Peskine8817f612018-12-18 00:18:46 +01003702 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003703
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003704 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3705 psa_set_key_algorithm( &attributes, alg );
3706 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003707
Ronald Cron5425a212020-08-04 14:58:35 +02003708 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3709 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003710
Ronald Cron5425a212020-08-04 14:58:35 +02003711 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3712 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003713
Steven Cooreman177deba2020-09-07 17:14:14 +02003714 if( alg != PSA_ALG_ECB_NO_PADDING )
3715 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003716 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3717 iv, iv_size,
3718 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003719 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003720 output1_size = ( (size_t) input->len +
3721 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003722 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003723
Gilles Peskine8817f612018-12-18 00:18:46 +01003724 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3725 output1, output1_size,
3726 &output1_length ) );
3727 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003728 output1 + output1_length,
3729 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003730 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003731
Gilles Peskine048b7f02018-06-08 14:20:49 +02003732 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003733
Gilles Peskine8817f612018-12-18 00:18:46 +01003734 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003735
3736 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003737 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003738
Steven Cooreman177deba2020-09-07 17:14:14 +02003739 if( iv_length > 0 )
3740 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003741 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3742 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003743 }
3744
Gilles Peskine8817f612018-12-18 00:18:46 +01003745 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3746 output2, output2_size,
3747 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003748 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003749 PSA_ASSERT( psa_cipher_finish( &operation2,
3750 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003751 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003752 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003753
Gilles Peskine048b7f02018-06-08 14:20:49 +02003754 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003755
Gilles Peskine8817f612018-12-18 00:18:46 +01003756 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003757
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003758 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003759
3760exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003761 psa_cipher_abort( &operation1 );
3762 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003763 mbedtls_free( output1 );
3764 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003765 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003766 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003767}
3768/* END_CASE */
3769
3770/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003771void cipher_verify_output_multipart( int alg_arg,
3772 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003773 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003774 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003775 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003776{
Ronald Cron5425a212020-08-04 14:58:35 +02003777 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003778 psa_key_type_t key_type = key_type_arg;
3779 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003780 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003781 unsigned char iv[16] = {0};
3782 size_t iv_size = 16;
3783 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003784 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003785 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003786 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003787 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003788 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003789 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003790 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003791 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3792 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003793 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003794
Gilles Peskine8817f612018-12-18 00:18:46 +01003795 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003796
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003797 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3798 psa_set_key_algorithm( &attributes, alg );
3799 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003800
Ronald Cron5425a212020-08-04 14:58:35 +02003801 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3802 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003803
Ronald Cron5425a212020-08-04 14:58:35 +02003804 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3805 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003806
Steven Cooreman177deba2020-09-07 17:14:14 +02003807 if( alg != PSA_ALG_ECB_NO_PADDING )
3808 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003809 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3810 iv, iv_size,
3811 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003812 }
3813
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003814 output1_buffer_size = ( (size_t) input->len +
3815 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003816 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003817
Gilles Peskinee0866522019-02-19 19:44:00 +01003818 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003819
Gilles Peskine8817f612018-12-18 00:18:46 +01003820 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3821 output1, output1_buffer_size,
3822 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003823 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003824
Gilles Peskine8817f612018-12-18 00:18:46 +01003825 PSA_ASSERT( psa_cipher_update( &operation1,
3826 input->x + first_part_size,
3827 input->len - first_part_size,
3828 output1, output1_buffer_size,
3829 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003830 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003831
Gilles Peskine8817f612018-12-18 00:18:46 +01003832 PSA_ASSERT( psa_cipher_finish( &operation1,
3833 output1 + output1_length,
3834 output1_buffer_size - output1_length,
3835 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003836 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003837
Gilles Peskine8817f612018-12-18 00:18:46 +01003838 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003839
Gilles Peskine048b7f02018-06-08 14:20:49 +02003840 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003841 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003842
Steven Cooreman177deba2020-09-07 17:14:14 +02003843 if( iv_length > 0 )
3844 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003845 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3846 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003847 }
Moran Pekerded84402018-06-06 16:36:50 +03003848
Gilles Peskine8817f612018-12-18 00:18:46 +01003849 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3850 output2, output2_buffer_size,
3851 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003852 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003853
Gilles Peskine8817f612018-12-18 00:18:46 +01003854 PSA_ASSERT( psa_cipher_update( &operation2,
3855 output1 + first_part_size,
3856 output1_length - first_part_size,
3857 output2, output2_buffer_size,
3858 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003859 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003860
Gilles Peskine8817f612018-12-18 00:18:46 +01003861 PSA_ASSERT( psa_cipher_finish( &operation2,
3862 output2 + output2_length,
3863 output2_buffer_size - output2_length,
3864 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003865 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003866
Gilles Peskine8817f612018-12-18 00:18:46 +01003867 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003868
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003869 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003870
3871exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003872 psa_cipher_abort( &operation1 );
3873 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003874 mbedtls_free( output1 );
3875 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003876 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003877 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003878}
3879/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003880
Gilles Peskine20035e32018-02-03 22:44:14 +01003881/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003882void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003883 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003884 data_t *nonce,
3885 data_t *additional_data,
3886 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003887 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003888{
Ronald Cron5425a212020-08-04 14:58:35 +02003889 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003890 psa_key_type_t key_type = key_type_arg;
3891 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003892 unsigned char *output_data = NULL;
3893 size_t output_size = 0;
3894 size_t output_length = 0;
3895 unsigned char *output_data2 = NULL;
3896 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003897 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003898 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003899 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003900
Gilles Peskine4abf7412018-06-18 16:35:34 +02003901 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003902 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3903 * should be exact. */
3904 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3905 TEST_EQUAL( output_size,
3906 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003907 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003908
Gilles Peskine8817f612018-12-18 00:18:46 +01003909 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003910
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003911 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3912 psa_set_key_algorithm( &attributes, alg );
3913 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003914
Gilles Peskine049c7532019-05-15 20:22:09 +02003915 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003916 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003917
Ronald Cron5425a212020-08-04 14:58:35 +02003918 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003919 nonce->x, nonce->len,
3920 additional_data->x,
3921 additional_data->len,
3922 input_data->x, input_data->len,
3923 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003924 &output_length ),
3925 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003926
3927 if( PSA_SUCCESS == expected_result )
3928 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003929 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003930
Gilles Peskine003a4a92019-05-14 16:09:40 +02003931 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3932 * should be exact. */
3933 TEST_EQUAL( input_data->len,
3934 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3935
Ronald Cron5425a212020-08-04 14:58:35 +02003936 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003937 nonce->x, nonce->len,
3938 additional_data->x,
3939 additional_data->len,
3940 output_data, output_length,
3941 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003942 &output_length2 ),
3943 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003944
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003945 ASSERT_COMPARE( input_data->x, input_data->len,
3946 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003947 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003948
Gilles Peskinea1cac842018-06-11 19:33:02 +02003949exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003950 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003951 mbedtls_free( output_data );
3952 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003953 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003954}
3955/* END_CASE */
3956
3957/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003958void aead_encrypt( int key_type_arg, data_t *key_data,
3959 int alg_arg,
3960 data_t *nonce,
3961 data_t *additional_data,
3962 data_t *input_data,
3963 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003964{
Ronald Cron5425a212020-08-04 14:58:35 +02003965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003966 psa_key_type_t key_type = key_type_arg;
3967 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003968 unsigned char *output_data = NULL;
3969 size_t output_size = 0;
3970 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003971 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003972 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003973
Gilles Peskine4abf7412018-06-18 16:35:34 +02003974 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003975 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3976 * should be exact. */
3977 TEST_EQUAL( output_size,
3978 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003979 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003980
Gilles Peskine8817f612018-12-18 00:18:46 +01003981 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003982
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003983 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3984 psa_set_key_algorithm( &attributes, alg );
3985 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003986
Gilles Peskine049c7532019-05-15 20:22:09 +02003987 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003988 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003989
Ronald Cron5425a212020-08-04 14:58:35 +02003990 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003991 nonce->x, nonce->len,
3992 additional_data->x, additional_data->len,
3993 input_data->x, input_data->len,
3994 output_data, output_size,
3995 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003996
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003997 ASSERT_COMPARE( expected_result->x, expected_result->len,
3998 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003999
Gilles Peskinea1cac842018-06-11 19:33:02 +02004000exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004001 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004002 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004003 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004004}
4005/* END_CASE */
4006
4007/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004008void aead_decrypt( int key_type_arg, data_t *key_data,
4009 int alg_arg,
4010 data_t *nonce,
4011 data_t *additional_data,
4012 data_t *input_data,
4013 data_t *expected_data,
4014 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004015{
Ronald Cron5425a212020-08-04 14:58:35 +02004016 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004017 psa_key_type_t key_type = key_type_arg;
4018 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004019 unsigned char *output_data = NULL;
4020 size_t output_size = 0;
4021 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004022 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004024 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004025
Gilles Peskine003a4a92019-05-14 16:09:40 +02004026 output_size = input_data->len - tag_length;
4027 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4028 * should be exact. */
4029 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4030 TEST_EQUAL( output_size,
4031 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004032 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004033
Gilles Peskine8817f612018-12-18 00:18:46 +01004034 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004035
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004036 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4037 psa_set_key_algorithm( &attributes, alg );
4038 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004039
Gilles Peskine049c7532019-05-15 20:22:09 +02004040 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004041 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004042
Ronald Cron5425a212020-08-04 14:58:35 +02004043 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004044 nonce->x, nonce->len,
4045 additional_data->x,
4046 additional_data->len,
4047 input_data->x, input_data->len,
4048 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004049 &output_length ),
4050 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004051
Gilles Peskine2d277862018-06-18 15:41:12 +02004052 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004053 ASSERT_COMPARE( expected_data->x, expected_data->len,
4054 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004055
Gilles Peskinea1cac842018-06-11 19:33:02 +02004056exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004057 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004058 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004059 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004060}
4061/* END_CASE */
4062
4063/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004064void signature_size( int type_arg,
4065 int bits,
4066 int alg_arg,
4067 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004068{
4069 psa_key_type_t type = type_arg;
4070 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004071 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004072
Gilles Peskinefe11b722018-12-18 00:24:04 +01004073 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004074#if defined(MBEDTLS_TEST_DEPRECATED)
4075 TEST_EQUAL( actual_size,
4076 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4077#endif /* MBEDTLS_TEST_DEPRECATED */
4078
Gilles Peskinee59236f2018-01-27 23:32:46 +01004079exit:
4080 ;
4081}
4082/* END_CASE */
4083
4084/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004085void sign_deterministic( int key_type_arg, data_t *key_data,
4086 int alg_arg, data_t *input_data,
4087 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004088{
Ronald Cron5425a212020-08-04 14:58:35 +02004089 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004090 psa_key_type_t key_type = key_type_arg;
4091 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004092 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004093 unsigned char *signature = NULL;
4094 size_t signature_size;
4095 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004097
Gilles Peskine8817f612018-12-18 00:18:46 +01004098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004099
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004101 psa_set_key_algorithm( &attributes, alg );
4102 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004103
Gilles Peskine049c7532019-05-15 20:22:09 +02004104 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004105 &key ) );
4106 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004107 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004108
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004109 /* Allocate a buffer which has the size advertized by the
4110 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004111 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004112 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004113 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004114 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004115 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004116
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004117 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004118 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004119 input_data->x, input_data->len,
4120 signature, signature_size,
4121 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004122 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004123 ASSERT_COMPARE( output_data->x, output_data->len,
4124 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004125
Gilles Peskine0627f982019-11-26 19:12:16 +01004126#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004127 memset( signature, 0, signature_size );
4128 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004129 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004130 input_data->x, input_data->len,
4131 signature, signature_size,
4132 &signature_length ) );
4133 ASSERT_COMPARE( output_data->x, output_data->len,
4134 signature, signature_length );
4135#endif /* MBEDTLS_TEST_DEPRECATED */
4136
Gilles Peskine20035e32018-02-03 22:44:14 +01004137exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004138 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004139 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004140 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004141 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004142}
4143/* END_CASE */
4144
4145/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004146void sign_fail( int key_type_arg, data_t *key_data,
4147 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004148 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004149{
Ronald Cron5425a212020-08-04 14:58:35 +02004150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004151 psa_key_type_t key_type = key_type_arg;
4152 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004153 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004154 psa_status_t actual_status;
4155 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004156 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004157 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004159
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004160 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004161
Gilles Peskine8817f612018-12-18 00:18:46 +01004162 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004163
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004164 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004165 psa_set_key_algorithm( &attributes, alg );
4166 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004167
Gilles Peskine049c7532019-05-15 20:22:09 +02004168 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004169 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004170
Ronald Cron5425a212020-08-04 14:58:35 +02004171 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004172 input_data->x, input_data->len,
4173 signature, signature_size,
4174 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004175 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004176 /* The value of *signature_length is unspecified on error, but
4177 * whatever it is, it should be less than signature_size, so that
4178 * if the caller tries to read *signature_length bytes without
4179 * checking the error code then they don't overflow a buffer. */
4180 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004181
Gilles Peskine895242b2019-11-29 12:15:40 +01004182#if defined(MBEDTLS_TEST_DEPRECATED)
4183 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004184 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004185 input_data->x, input_data->len,
4186 signature, signature_size,
4187 &signature_length ),
4188 expected_status );
4189 TEST_ASSERT( signature_length <= signature_size );
4190#endif /* MBEDTLS_TEST_DEPRECATED */
4191
Gilles Peskine20035e32018-02-03 22:44:14 +01004192exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004193 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004194 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004195 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004196 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004197}
4198/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004199
4200/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004201void sign_verify( int key_type_arg, data_t *key_data,
4202 int alg_arg, data_t *input_data )
4203{
Ronald Cron5425a212020-08-04 14:58:35 +02004204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004205 psa_key_type_t key_type = key_type_arg;
4206 psa_algorithm_t alg = alg_arg;
4207 size_t key_bits;
4208 unsigned char *signature = NULL;
4209 size_t signature_size;
4210 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004212
Gilles Peskine8817f612018-12-18 00:18:46 +01004213 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004214
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004215 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004216 psa_set_key_algorithm( &attributes, alg );
4217 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004218
Gilles Peskine049c7532019-05-15 20:22:09 +02004219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004220 &key ) );
4221 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004222 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004223
4224 /* Allocate a buffer which has the size advertized by the
4225 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004226 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004227 key_bits, alg );
4228 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004229 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004230 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004231
4232 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004233 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004234 input_data->x, input_data->len,
4235 signature, signature_size,
4236 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004237 /* Check that the signature length looks sensible. */
4238 TEST_ASSERT( signature_length <= signature_size );
4239 TEST_ASSERT( signature_length > 0 );
4240
4241 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004242 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004243 input_data->x, input_data->len,
4244 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004245
4246 if( input_data->len != 0 )
4247 {
4248 /* Flip a bit in the input and verify that the signature is now
4249 * detected as invalid. Flip a bit at the beginning, not at the end,
4250 * because ECDSA may ignore the last few bits of the input. */
4251 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004252 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004253 input_data->x, input_data->len,
4254 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004255 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004256 }
4257
4258exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004259 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004260 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004261 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004262 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004263}
4264/* END_CASE */
4265
4266/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004267void asymmetric_verify( int key_type_arg, data_t *key_data,
4268 int alg_arg, data_t *hash_data,
4269 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004270{
Ronald Cron5425a212020-08-04 14:58:35 +02004271 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004272 psa_key_type_t key_type = key_type_arg;
4273 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004274 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004275
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004276 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004277
Gilles Peskine8817f612018-12-18 00:18:46 +01004278 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004279
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004280 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004281 psa_set_key_algorithm( &attributes, alg );
4282 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004283
Gilles Peskine049c7532019-05-15 20:22:09 +02004284 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004285 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004286
Ronald Cron5425a212020-08-04 14:58:35 +02004287 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004288 hash_data->x, hash_data->len,
4289 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004290
4291#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004292 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004293 hash_data->x, hash_data->len,
4294 signature_data->x,
4295 signature_data->len ) );
4296
4297#endif /* MBEDTLS_TEST_DEPRECATED */
4298
itayzafrir5c753392018-05-08 11:18:38 +03004299exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004300 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004301 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004302 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004303}
4304/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004305
4306/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004307void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4308 int alg_arg, data_t *hash_data,
4309 data_t *signature_data,
4310 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004311{
Ronald Cron5425a212020-08-04 14:58:35 +02004312 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004313 psa_key_type_t key_type = key_type_arg;
4314 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004315 psa_status_t actual_status;
4316 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004318
Gilles Peskine8817f612018-12-18 00:18:46 +01004319 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004320
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004321 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004322 psa_set_key_algorithm( &attributes, alg );
4323 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004324
Gilles Peskine049c7532019-05-15 20:22:09 +02004325 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004326 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004327
Ronald Cron5425a212020-08-04 14:58:35 +02004328 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004329 hash_data->x, hash_data->len,
4330 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004331 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004332
Gilles Peskine895242b2019-11-29 12:15:40 +01004333#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004334 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004335 hash_data->x, hash_data->len,
4336 signature_data->x, signature_data->len ),
4337 expected_status );
4338#endif /* MBEDTLS_TEST_DEPRECATED */
4339
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004341 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004342 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004343 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004344}
4345/* END_CASE */
4346
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004347/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004348void asymmetric_encrypt( int key_type_arg,
4349 data_t *key_data,
4350 int alg_arg,
4351 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004352 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004353 int expected_output_length_arg,
4354 int expected_status_arg )
4355{
Ronald Cron5425a212020-08-04 14:58:35 +02004356 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004357 psa_key_type_t key_type = key_type_arg;
4358 psa_algorithm_t alg = alg_arg;
4359 size_t expected_output_length = expected_output_length_arg;
4360 size_t key_bits;
4361 unsigned char *output = NULL;
4362 size_t output_size;
4363 size_t output_length = ~0;
4364 psa_status_t actual_status;
4365 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004366 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004367
Gilles Peskine8817f612018-12-18 00:18:46 +01004368 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004369
Gilles Peskine656896e2018-06-29 19:12:28 +02004370 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004371 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4372 psa_set_key_algorithm( &attributes, alg );
4373 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004374 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004375 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004376
4377 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004378 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004379 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004380 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004381 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004382
4383 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004384 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004385 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004386 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004387 output, output_size,
4388 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004389 TEST_EQUAL( actual_status, expected_status );
4390 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004391
Gilles Peskine68428122018-06-30 18:42:41 +02004392 /* If the label is empty, the test framework puts a non-null pointer
4393 * in label->x. Test that a null pointer works as well. */
4394 if( label->len == 0 )
4395 {
4396 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004397 if( output_size != 0 )
4398 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004399 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004400 input_data->x, input_data->len,
4401 NULL, label->len,
4402 output, output_size,
4403 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004404 TEST_EQUAL( actual_status, expected_status );
4405 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004406 }
4407
Gilles Peskine656896e2018-06-29 19:12:28 +02004408exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004409 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004410 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004411 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004412 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004413}
4414/* END_CASE */
4415
4416/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004417void asymmetric_encrypt_decrypt( int key_type_arg,
4418 data_t *key_data,
4419 int alg_arg,
4420 data_t *input_data,
4421 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004422{
Ronald Cron5425a212020-08-04 14:58:35 +02004423 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004424 psa_key_type_t key_type = key_type_arg;
4425 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004426 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004427 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004428 size_t output_size;
4429 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004430 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004431 size_t output2_size;
4432 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004434
Gilles Peskine8817f612018-12-18 00:18:46 +01004435 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004436
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004437 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4438 psa_set_key_algorithm( &attributes, alg );
4439 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004440
Gilles Peskine049c7532019-05-15 20:22:09 +02004441 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004442 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004443
4444 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004445 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004446 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004447 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004448 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004449 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004450 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004451
Gilles Peskineeebd7382018-06-08 18:11:54 +02004452 /* We test encryption by checking that encrypt-then-decrypt gives back
4453 * the original plaintext because of the non-optional random
4454 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004455 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004456 input_data->x, input_data->len,
4457 label->x, label->len,
4458 output, output_size,
4459 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004460 /* We don't know what ciphertext length to expect, but check that
4461 * it looks sensible. */
4462 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004463
Ronald Cron5425a212020-08-04 14:58:35 +02004464 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004465 output, output_length,
4466 label->x, label->len,
4467 output2, output2_size,
4468 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004469 ASSERT_COMPARE( input_data->x, input_data->len,
4470 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004471
4472exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004473 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004474 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004475 mbedtls_free( output );
4476 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004477 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004478}
4479/* END_CASE */
4480
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004481/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004482void asymmetric_decrypt( int key_type_arg,
4483 data_t *key_data,
4484 int alg_arg,
4485 data_t *input_data,
4486 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004487 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004488{
Ronald Cron5425a212020-08-04 14:58:35 +02004489 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004490 psa_key_type_t key_type = key_type_arg;
4491 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004492 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004493 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004494 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004496
Jaeden Amero412654a2019-02-06 12:57:46 +00004497 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004498 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004499
Gilles Peskine8817f612018-12-18 00:18:46 +01004500 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004501
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004502 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4503 psa_set_key_algorithm( &attributes, alg );
4504 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004505
Gilles Peskine049c7532019-05-15 20:22:09 +02004506 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004507 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004508
Ronald Cron5425a212020-08-04 14:58:35 +02004509 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004510 input_data->x, input_data->len,
4511 label->x, label->len,
4512 output,
4513 output_size,
4514 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004515 ASSERT_COMPARE( expected_data->x, expected_data->len,
4516 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004517
Gilles Peskine68428122018-06-30 18:42:41 +02004518 /* If the label is empty, the test framework puts a non-null pointer
4519 * in label->x. Test that a null pointer works as well. */
4520 if( label->len == 0 )
4521 {
4522 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004523 if( output_size != 0 )
4524 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004525 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004526 input_data->x, input_data->len,
4527 NULL, label->len,
4528 output,
4529 output_size,
4530 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004531 ASSERT_COMPARE( expected_data->x, expected_data->len,
4532 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004533 }
4534
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004535exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004536 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004537 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004538 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004539 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004540}
4541/* END_CASE */
4542
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004543/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004544void asymmetric_decrypt_fail( int key_type_arg,
4545 data_t *key_data,
4546 int alg_arg,
4547 data_t *input_data,
4548 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004549 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004550 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004551{
Ronald Cron5425a212020-08-04 14:58:35 +02004552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004553 psa_key_type_t key_type = key_type_arg;
4554 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004555 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004556 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004557 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004558 psa_status_t actual_status;
4559 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004561
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004562 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004563
Gilles Peskine8817f612018-12-18 00:18:46 +01004564 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004565
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004566 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4567 psa_set_key_algorithm( &attributes, alg );
4568 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004569
Gilles Peskine049c7532019-05-15 20:22:09 +02004570 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004571 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004572
Ronald Cron5425a212020-08-04 14:58:35 +02004573 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004574 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004575 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004576 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004577 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004578 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004579 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004580
Gilles Peskine68428122018-06-30 18:42:41 +02004581 /* If the label is empty, the test framework puts a non-null pointer
4582 * in label->x. Test that a null pointer works as well. */
4583 if( label->len == 0 )
4584 {
4585 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004586 if( output_size != 0 )
4587 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004588 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004589 input_data->x, input_data->len,
4590 NULL, label->len,
4591 output, output_size,
4592 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004593 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004594 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004595 }
4596
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004597exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004598 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004599 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004600 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004601 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004602}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004603/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004604
4605/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004606void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004607{
4608 /* Test each valid way of initializing the object, except for `= {0}`, as
4609 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4610 * though it's OK by the C standard. We could test for this, but we'd need
4611 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004612 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004613 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4614 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4615 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004616
4617 memset( &zero, 0, sizeof( zero ) );
4618
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004619 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004620 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004621 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004622 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004623 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004624 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004625 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004626
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004627 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004628 PSA_ASSERT( psa_key_derivation_abort(&func) );
4629 PSA_ASSERT( psa_key_derivation_abort(&init) );
4630 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004631}
4632/* END_CASE */
4633
Janos Follath16de4a42019-06-13 16:32:24 +01004634/* BEGIN_CASE */
4635void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004636{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004637 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004638 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004639 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004640
Gilles Peskine8817f612018-12-18 00:18:46 +01004641 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004642
Janos Follath16de4a42019-06-13 16:32:24 +01004643 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004644 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004645
4646exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004648 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004649}
4650/* END_CASE */
4651
Janos Follathaf3c2a02019-06-12 12:34:34 +01004652/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004653void derive_set_capacity( int alg_arg, int capacity_arg,
4654 int expected_status_arg )
4655{
4656 psa_algorithm_t alg = alg_arg;
4657 size_t capacity = capacity_arg;
4658 psa_status_t expected_status = expected_status_arg;
4659 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4660
4661 PSA_ASSERT( psa_crypto_init( ) );
4662
4663 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4664
4665 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4666 expected_status );
4667
4668exit:
4669 psa_key_derivation_abort( &operation );
4670 PSA_DONE( );
4671}
4672/* END_CASE */
4673
4674/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004675void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004676 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004677 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004678 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004679 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004680 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004681 int expected_status_arg3,
4682 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004683{
4684 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004685 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4686 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004687 psa_status_t expected_statuses[] = {expected_status_arg1,
4688 expected_status_arg2,
4689 expected_status_arg3};
4690 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004691 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4692 MBEDTLS_SVC_KEY_ID_INIT,
4693 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004694 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4696 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004697 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004698 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004699 psa_status_t expected_output_status = expected_output_status_arg;
4700 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004701
4702 PSA_ASSERT( psa_crypto_init( ) );
4703
4704 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4705 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004706
4707 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4708
4709 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4710 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004711 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004712 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004713 psa_set_key_type( &attributes, key_types[i] );
4714 PSA_ASSERT( psa_import_key( &attributes,
4715 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004716 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004717 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4718 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4719 {
4720 // When taking a private key as secret input, use key agreement
4721 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004722 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004723 expected_statuses[i] );
4724 }
4725 else
4726 {
4727 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004728 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004729 expected_statuses[i] );
4730 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004731 }
4732 else
4733 {
4734 TEST_EQUAL( psa_key_derivation_input_bytes(
4735 &operation, steps[i],
4736 inputs[i]->x, inputs[i]->len ),
4737 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004738 }
4739 }
4740
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004741 if( output_key_type != PSA_KEY_TYPE_NONE )
4742 {
4743 psa_reset_key_attributes( &attributes );
4744 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4745 psa_set_key_bits( &attributes, 8 );
4746 actual_output_status =
4747 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004748 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004749 }
4750 else
4751 {
4752 uint8_t buffer[1];
4753 actual_output_status =
4754 psa_key_derivation_output_bytes( &operation,
4755 buffer, sizeof( buffer ) );
4756 }
4757 TEST_EQUAL( actual_output_status, expected_output_status );
4758
Janos Follathaf3c2a02019-06-12 12:34:34 +01004759exit:
4760 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004761 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4762 psa_destroy_key( keys[i] );
4763 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004764 PSA_DONE( );
4765}
4766/* END_CASE */
4767
Janos Follathd958bb72019-07-03 15:02:16 +01004768/* BEGIN_CASE */
4769void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004770{
Janos Follathd958bb72019-07-03 15:02:16 +01004771 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004772 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004773 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004774 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004775 unsigned char input1[] = "Input 1";
4776 size_t input1_length = sizeof( input1 );
4777 unsigned char input2[] = "Input 2";
4778 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004779 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004780 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004781 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4782 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4783 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004784 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004785
Gilles Peskine8817f612018-12-18 00:18:46 +01004786 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004787
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004788 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4789 psa_set_key_algorithm( &attributes, alg );
4790 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004791
Gilles Peskine73676cb2019-05-15 20:15:10 +02004792 PSA_ASSERT( psa_import_key( &attributes,
4793 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004794 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004795
4796 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004797 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004798 input1, input1_length,
4799 input2, input2_length,
4800 capacity ) )
4801 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004802
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004803 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004804 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004805 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004806
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004808
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004809 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004810 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004811
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004812exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004813 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004814 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004815 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004816}
4817/* END_CASE */
4818
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004819/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004820void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004821{
4822 uint8_t output_buffer[16];
4823 size_t buffer_size = 16;
4824 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004825 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004826
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004827 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4828 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004829 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004830
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004832 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004833
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004834 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004835
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004836 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4837 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004838 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004839
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004840 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004841 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004842
4843exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004844 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004845}
4846/* END_CASE */
4847
4848/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004849void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004850 int step1_arg, data_t *input1,
4851 int step2_arg, data_t *input2,
4852 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004853 int requested_capacity_arg,
4854 data_t *expected_output1,
4855 data_t *expected_output2 )
4856{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004857 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004858 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4859 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004860 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4861 MBEDTLS_SVC_KEY_ID_INIT,
4862 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004863 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004865 uint8_t *expected_outputs[2] =
4866 {expected_output1->x, expected_output2->x};
4867 size_t output_sizes[2] =
4868 {expected_output1->len, expected_output2->len};
4869 size_t output_buffer_size = 0;
4870 uint8_t *output_buffer = NULL;
4871 size_t expected_capacity;
4872 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004874 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004875 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004876
4877 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4878 {
4879 if( output_sizes[i] > output_buffer_size )
4880 output_buffer_size = output_sizes[i];
4881 if( output_sizes[i] == 0 )
4882 expected_outputs[i] = NULL;
4883 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004884 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004885 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004886
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004887 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4888 psa_set_key_algorithm( &attributes, alg );
4889 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004890
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004891 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004892 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4893 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4894 requested_capacity ) );
4895 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004896 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004897 switch( steps[i] )
4898 {
4899 case 0:
4900 break;
4901 case PSA_KEY_DERIVATION_INPUT_SECRET:
4902 PSA_ASSERT( psa_import_key( &attributes,
4903 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004904 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004905 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004906 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004907 break;
4908 default:
4909 PSA_ASSERT( psa_key_derivation_input_bytes(
4910 &operation, steps[i],
4911 inputs[i]->x, inputs[i]->len ) );
4912 break;
4913 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004914 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004915
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004917 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004918 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004919 expected_capacity = requested_capacity;
4920
4921 /* Expansion phase. */
4922 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4923 {
4924 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004926 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004927 if( expected_capacity == 0 && output_sizes[i] == 0 )
4928 {
4929 /* Reading 0 bytes when 0 bytes are available can go either way. */
4930 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004931 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004932 continue;
4933 }
4934 else if( expected_capacity == 0 ||
4935 output_sizes[i] > expected_capacity )
4936 {
4937 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004938 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004939 expected_capacity = 0;
4940 continue;
4941 }
4942 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004943 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004944 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004945 ASSERT_COMPARE( output_buffer, output_sizes[i],
4946 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004948 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004950 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004951 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004952 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004954
4955exit:
4956 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004957 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004958 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4959 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004960 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004961}
4962/* END_CASE */
4963
4964/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004965void derive_full( int alg_arg,
4966 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004967 data_t *input1,
4968 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004969 int requested_capacity_arg )
4970{
Ronald Cron5425a212020-08-04 14:58:35 +02004971 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004972 psa_algorithm_t alg = alg_arg;
4973 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004974 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004975 unsigned char output_buffer[16];
4976 size_t expected_capacity = requested_capacity;
4977 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004978 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004979
Gilles Peskine8817f612018-12-18 00:18:46 +01004980 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004981
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004982 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4983 psa_set_key_algorithm( &attributes, alg );
4984 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004985
Gilles Peskine049c7532019-05-15 20:22:09 +02004986 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004987 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004988
Ronald Cron5425a212020-08-04 14:58:35 +02004989 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01004990 input1->x, input1->len,
4991 input2->x, input2->len,
4992 requested_capacity ) )
4993 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004994
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004995 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004996 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004997 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004998
4999 /* Expansion phase. */
5000 while( current_capacity > 0 )
5001 {
5002 size_t read_size = sizeof( output_buffer );
5003 if( read_size > current_capacity )
5004 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005005 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005006 output_buffer,
5007 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005008 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005010 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005011 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005012 }
5013
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005014 /* Check that the operation refuses to go over capacity. */
5015 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005016 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005017
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005018 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005019
5020exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005022 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005023 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005024}
5025/* END_CASE */
5026
Janos Follathe60c9052019-07-03 13:51:30 +01005027/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005028void derive_key_exercise( int alg_arg,
5029 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005030 data_t *input1,
5031 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005032 int derived_type_arg,
5033 int derived_bits_arg,
5034 int derived_usage_arg,
5035 int derived_alg_arg )
5036{
Ronald Cron5425a212020-08-04 14:58:35 +02005037 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5038 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005039 psa_algorithm_t alg = alg_arg;
5040 psa_key_type_t derived_type = derived_type_arg;
5041 size_t derived_bits = derived_bits_arg;
5042 psa_key_usage_t derived_usage = derived_usage_arg;
5043 psa_algorithm_t derived_alg = derived_alg_arg;
5044 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005047 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005048
Gilles Peskine8817f612018-12-18 00:18:46 +01005049 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005050
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005051 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5052 psa_set_key_algorithm( &attributes, alg );
5053 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005054 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005055 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005056
5057 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005058 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005059 input1->x, input1->len,
5060 input2->x, input2->len, capacity ) )
5061 goto exit;
5062
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005063 psa_set_key_usage_flags( &attributes, derived_usage );
5064 psa_set_key_algorithm( &attributes, derived_alg );
5065 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005066 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005067 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005068 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005069
5070 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005071 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005072 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5073 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005074
5075 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005076 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005077 goto exit;
5078
5079exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005080 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005081 psa_reset_key_attributes( &got_attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005082 psa_destroy_key( base_key );
5083 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005084 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005085}
5086/* END_CASE */
5087
Janos Follath42fd8882019-07-03 14:17:09 +01005088/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005089void derive_key_export( int alg_arg,
5090 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005091 data_t *input1,
5092 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005093 int bytes1_arg,
5094 int bytes2_arg )
5095{
Ronald Cron5425a212020-08-04 14:58:35 +02005096 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5097 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005098 psa_algorithm_t alg = alg_arg;
5099 size_t bytes1 = bytes1_arg;
5100 size_t bytes2 = bytes2_arg;
5101 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005102 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005103 uint8_t *output_buffer = NULL;
5104 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005105 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5106 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005107 size_t length;
5108
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005109 ASSERT_ALLOC( output_buffer, capacity );
5110 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005112
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005113 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5114 psa_set_key_algorithm( &base_attributes, alg );
5115 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005116 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005117 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005118
5119 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005120 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005121 input1->x, input1->len,
5122 input2->x, input2->len, capacity ) )
5123 goto exit;
5124
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005126 output_buffer,
5127 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005129
5130 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005131 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005132 input1->x, input1->len,
5133 input2->x, input2->len, capacity ) )
5134 goto exit;
5135
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005136 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5137 psa_set_key_algorithm( &derived_attributes, 0 );
5138 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005139 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005141 &derived_key ) );
5142 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005143 export_buffer, bytes1,
5144 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005145 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005146 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005147 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005149 &derived_key ) );
5150 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005151 export_buffer + bytes1, bytes2,
5152 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005153 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005154
5155 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005156 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5157 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005158
5159exit:
5160 mbedtls_free( output_buffer );
5161 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005163 psa_destroy_key( base_key );
5164 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005165 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005166}
5167/* END_CASE */
5168
5169/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005170void derive_key( int alg_arg,
5171 data_t *key_data, data_t *input1, data_t *input2,
5172 int type_arg, int bits_arg,
5173 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005174{
Ronald Cron5425a212020-08-04 14:58:35 +02005175 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5176 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005177 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005178 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005179 size_t bits = bits_arg;
5180 psa_status_t expected_status = expected_status_arg;
5181 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5182 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5183 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5184
5185 PSA_ASSERT( psa_crypto_init( ) );
5186
5187 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5188 psa_set_key_algorithm( &base_attributes, alg );
5189 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5190 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005191 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005192
Ronald Cron5425a212020-08-04 14:58:35 +02005193 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005194 input1->x, input1->len,
5195 input2->x, input2->len, SIZE_MAX ) )
5196 goto exit;
5197
5198 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5199 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005200 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005201 psa_set_key_bits( &derived_attributes, bits );
5202 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005203 &derived_key ),
Gilles Peskinec744d992019-07-30 17:26:54 +02005204 expected_status );
5205
5206exit:
5207 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005208 psa_destroy_key( base_key );
5209 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005210 PSA_DONE( );
5211}
5212/* END_CASE */
5213
5214/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005215void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005216 int our_key_type_arg, int our_key_alg_arg,
5217 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005218 int expected_status_arg )
5219{
Ronald Cron5425a212020-08-04 14:58:35 +02005220 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005221 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005222 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005223 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005224 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005225 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005226 psa_status_t expected_status = expected_status_arg;
5227 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005228
Gilles Peskine8817f612018-12-18 00:18:46 +01005229 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005230
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005231 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005232 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005233 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005234 PSA_ASSERT( psa_import_key( &attributes,
5235 our_key_data->x, our_key_data->len,
5236 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005237
Gilles Peskine77f40d82019-04-11 21:27:06 +02005238 /* The tests currently include inputs that should fail at either step.
5239 * Test cases that fail at the setup step should be changed to call
5240 * key_derivation_setup instead, and this function should be renamed
5241 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005242 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005243 if( status == PSA_SUCCESS )
5244 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005245 TEST_EQUAL( psa_key_derivation_key_agreement(
5246 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5247 our_key,
5248 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005249 expected_status );
5250 }
5251 else
5252 {
5253 TEST_ASSERT( status == expected_status );
5254 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005255
5256exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005258 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005259 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005260}
5261/* END_CASE */
5262
5263/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005264void raw_key_agreement( int alg_arg,
5265 int our_key_type_arg, data_t *our_key_data,
5266 data_t *peer_key_data,
5267 data_t *expected_output )
5268{
Ronald Cron5425a212020-08-04 14:58:35 +02005269 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005270 psa_algorithm_t alg = alg_arg;
5271 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005273 unsigned char *output = NULL;
5274 size_t output_length = ~0;
5275
5276 ASSERT_ALLOC( output, expected_output->len );
5277 PSA_ASSERT( psa_crypto_init( ) );
5278
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005279 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5280 psa_set_key_algorithm( &attributes, alg );
5281 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005282 PSA_ASSERT( psa_import_key( &attributes,
5283 our_key_data->x, our_key_data->len,
5284 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005285
Gilles Peskinebe697d82019-05-16 18:00:41 +02005286 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5287 peer_key_data->x, peer_key_data->len,
5288 output, expected_output->len,
5289 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005290 ASSERT_COMPARE( output, output_length,
5291 expected_output->x, expected_output->len );
5292
5293exit:
5294 mbedtls_free( output );
5295 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005296 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005297}
5298/* END_CASE */
5299
5300/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005301void key_agreement_capacity( int alg_arg,
5302 int our_key_type_arg, data_t *our_key_data,
5303 data_t *peer_key_data,
5304 int expected_capacity_arg )
5305{
Ronald Cron5425a212020-08-04 14:58:35 +02005306 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005307 psa_algorithm_t alg = alg_arg;
5308 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005310 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005311 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005312 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005313
Gilles Peskine8817f612018-12-18 00:18:46 +01005314 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005315
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005316 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5317 psa_set_key_algorithm( &attributes, alg );
5318 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005319 PSA_ASSERT( psa_import_key( &attributes,
5320 our_key_data->x, our_key_data->len,
5321 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005322
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005323 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005324 PSA_ASSERT( psa_key_derivation_key_agreement(
5325 &operation,
5326 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5327 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005328 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5329 {
5330 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005332 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005333 NULL, 0 ) );
5334 }
Gilles Peskine59685592018-09-18 12:11:34 +02005335
Gilles Peskinebf491972018-10-25 22:36:12 +02005336 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005337 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005339 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005340
Gilles Peskinebf491972018-10-25 22:36:12 +02005341 /* Test the actual capacity by reading the output. */
5342 while( actual_capacity > sizeof( output ) )
5343 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005345 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005346 actual_capacity -= sizeof( output );
5347 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005349 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005351 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005352
Gilles Peskine59685592018-09-18 12:11:34 +02005353exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005354 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005355 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005356 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005357}
5358/* END_CASE */
5359
5360/* BEGIN_CASE */
5361void key_agreement_output( int alg_arg,
5362 int our_key_type_arg, data_t *our_key_data,
5363 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005364 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005365{
Ronald Cron5425a212020-08-04 14:58:35 +02005366 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005367 psa_algorithm_t alg = alg_arg;
5368 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005369 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005371 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005372
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005373 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5374 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005375
Gilles Peskine8817f612018-12-18 00:18:46 +01005376 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005377
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005378 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5379 psa_set_key_algorithm( &attributes, alg );
5380 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005381 PSA_ASSERT( psa_import_key( &attributes,
5382 our_key_data->x, our_key_data->len,
5383 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005384
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005385 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005386 PSA_ASSERT( psa_key_derivation_key_agreement(
5387 &operation,
5388 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5389 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005390 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5391 {
5392 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005394 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005395 NULL, 0 ) );
5396 }
Gilles Peskine59685592018-09-18 12:11:34 +02005397
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005398 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005399 actual_output,
5400 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005401 ASSERT_COMPARE( actual_output, expected_output1->len,
5402 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005403 if( expected_output2->len != 0 )
5404 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005405 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005406 actual_output,
5407 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005408 ASSERT_COMPARE( actual_output, expected_output2->len,
5409 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005410 }
Gilles Peskine59685592018-09-18 12:11:34 +02005411
5412exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005413 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005414 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005415 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005416 mbedtls_free( actual_output );
5417}
5418/* END_CASE */
5419
5420/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005421void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005422{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005423 size_t bytes = bytes_arg;
5424 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005425 unsigned char *output = NULL;
5426 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005427 size_t i;
5428 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005429
Simon Butcher49f8e312020-03-03 15:51:50 +00005430 TEST_ASSERT( bytes_arg >= 0 );
5431
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005432 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5433 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005434 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005435
Gilles Peskine8817f612018-12-18 00:18:46 +01005436 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005437
Gilles Peskinea50d7392018-06-21 10:22:13 +02005438 /* Run several times, to ensure that every output byte will be
5439 * nonzero at least once with overwhelming probability
5440 * (2^(-8*number_of_runs)). */
5441 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005442 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005443 if( bytes != 0 )
5444 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005445 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005446
5447 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005448 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5449 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005450
5451 for( i = 0; i < bytes; i++ )
5452 {
5453 if( output[i] != 0 )
5454 ++changed[i];
5455 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005456 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005457
5458 /* Check that every byte was changed to nonzero at least once. This
5459 * validates that psa_generate_random is overwriting every byte of
5460 * the output buffer. */
5461 for( i = 0; i < bytes; i++ )
5462 {
5463 TEST_ASSERT( changed[i] != 0 );
5464 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005465
5466exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005467 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005468 mbedtls_free( output );
5469 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005470}
5471/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005472
5473/* BEGIN_CASE */
5474void generate_key( int type_arg,
5475 int bits_arg,
5476 int usage_arg,
5477 int alg_arg,
5478 int expected_status_arg )
5479{
Ronald Cron5425a212020-08-04 14:58:35 +02005480 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005481 psa_key_type_t type = type_arg;
5482 psa_key_usage_t usage = usage_arg;
5483 size_t bits = bits_arg;
5484 psa_algorithm_t alg = alg_arg;
5485 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005487 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005488
Gilles Peskine8817f612018-12-18 00:18:46 +01005489 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005490
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005491 psa_set_key_usage_flags( &attributes, usage );
5492 psa_set_key_algorithm( &attributes, alg );
5493 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005494 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005495
5496 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005497 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005498 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005499 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005500
5501 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005502 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005503 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5504 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005505
Gilles Peskine818ca122018-06-20 18:16:48 +02005506 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005507 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005508 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005509
5510exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005511 psa_reset_key_attributes( &got_attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005512 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005513 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005514}
5515/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005516
Gilles Peskinee56e8782019-04-26 17:34:02 +02005517/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5518void generate_key_rsa( int bits_arg,
5519 data_t *e_arg,
5520 int expected_status_arg )
5521{
Ronald Cron5425a212020-08-04 14:58:35 +02005522 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005523 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005524 size_t bits = bits_arg;
5525 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5526 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5527 psa_status_t expected_status = expected_status_arg;
5528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5529 uint8_t *exported = NULL;
5530 size_t exported_size =
5531 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5532 size_t exported_length = SIZE_MAX;
5533 uint8_t *e_read_buffer = NULL;
5534 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005535 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005536 size_t e_read_length = SIZE_MAX;
5537
5538 if( e_arg->len == 0 ||
5539 ( e_arg->len == 3 &&
5540 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5541 {
5542 is_default_public_exponent = 1;
5543 e_read_size = 0;
5544 }
5545 ASSERT_ALLOC( e_read_buffer, e_read_size );
5546 ASSERT_ALLOC( exported, exported_size );
5547
5548 PSA_ASSERT( psa_crypto_init( ) );
5549
5550 psa_set_key_usage_flags( &attributes, usage );
5551 psa_set_key_algorithm( &attributes, alg );
5552 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5553 e_arg->x, e_arg->len ) );
5554 psa_set_key_bits( &attributes, bits );
5555
5556 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005557 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005558 if( expected_status != PSA_SUCCESS )
5559 goto exit;
5560
5561 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005562 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005563 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5564 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5565 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5566 e_read_buffer, e_read_size,
5567 &e_read_length ) );
5568 if( is_default_public_exponent )
5569 TEST_EQUAL( e_read_length, 0 );
5570 else
5571 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5572
5573 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005574 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005575 goto exit;
5576
5577 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005578 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005579 exported, exported_size,
5580 &exported_length ) );
5581 {
5582 uint8_t *p = exported;
5583 uint8_t *end = exported + exported_length;
5584 size_t len;
5585 /* RSAPublicKey ::= SEQUENCE {
5586 * modulus INTEGER, -- n
5587 * publicExponent INTEGER } -- e
5588 */
5589 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005590 MBEDTLS_ASN1_SEQUENCE |
5591 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005592 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5593 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5594 MBEDTLS_ASN1_INTEGER ) );
5595 if( len >= 1 && p[0] == 0 )
5596 {
5597 ++p;
5598 --len;
5599 }
5600 if( e_arg->len == 0 )
5601 {
5602 TEST_EQUAL( len, 3 );
5603 TEST_EQUAL( p[0], 1 );
5604 TEST_EQUAL( p[1], 0 );
5605 TEST_EQUAL( p[2], 1 );
5606 }
5607 else
5608 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5609 }
5610
5611exit:
5612 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005613 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005614 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005615 mbedtls_free( e_read_buffer );
5616 mbedtls_free( exported );
5617}
5618/* END_CASE */
5619
Darryl Greend49a4992018-06-18 17:27:26 +01005620/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005621void persistent_key_load_key_from_storage( data_t *data,
5622 int type_arg, int bits_arg,
5623 int usage_flags_arg, int alg_arg,
5624 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005625{
Ronald Cron71016a92020-08-28 19:01:50 +02005626 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005627 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5629 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005630 psa_key_type_t type = type_arg;
5631 size_t bits = bits_arg;
5632 psa_key_usage_t usage_flags = usage_flags_arg;
5633 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005634 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005635 unsigned char *first_export = NULL;
5636 unsigned char *second_export = NULL;
5637 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5638 size_t first_exported_length;
5639 size_t second_exported_length;
5640
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005641 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5642 {
5643 ASSERT_ALLOC( first_export, export_size );
5644 ASSERT_ALLOC( second_export, export_size );
5645 }
Darryl Greend49a4992018-06-18 17:27:26 +01005646
Gilles Peskine8817f612018-12-18 00:18:46 +01005647 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005648
Gilles Peskinec87af662019-05-15 16:12:22 +02005649 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005650 psa_set_key_usage_flags( &attributes, usage_flags );
5651 psa_set_key_algorithm( &attributes, alg );
5652 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005653 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005654
Darryl Green0c6575a2018-11-07 16:05:30 +00005655 switch( generation_method )
5656 {
5657 case IMPORT_KEY:
5658 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005659 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005660 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005661 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005662
Darryl Green0c6575a2018-11-07 16:05:30 +00005663 case GENERATE_KEY:
5664 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005665 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005666 break;
5667
5668 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005669 {
5670 /* Create base key */
5671 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5672 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5673 psa_set_key_usage_flags( &base_attributes,
5674 PSA_KEY_USAGE_DERIVE );
5675 psa_set_key_algorithm( &base_attributes, derive_alg );
5676 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005677 PSA_ASSERT( psa_import_key( &base_attributes,
5678 data->x, data->len,
5679 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005680 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005681 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005682 PSA_ASSERT( psa_key_derivation_input_key(
5683 &operation,
5684 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005685 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005687 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005688 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5689 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005690 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005691 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005692 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005693 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005694 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005695 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005696 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005697 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005698
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005699 /* Export the key if permitted by the key policy. */
5700 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5701 {
Ronald Cron5425a212020-08-04 14:58:35 +02005702 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005703 first_export, export_size,
5704 &first_exported_length ) );
5705 if( generation_method == IMPORT_KEY )
5706 ASSERT_COMPARE( data->x, data->len,
5707 first_export, first_exported_length );
5708 }
Darryl Greend49a4992018-06-18 17:27:26 +01005709
5710 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005711 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005712 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005713 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005714
Darryl Greend49a4992018-06-18 17:27:26 +01005715 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005716 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005717 TEST_ASSERT( mbedtls_svc_key_id_equal(
5718 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005719 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5720 PSA_KEY_LIFETIME_PERSISTENT );
5721 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5722 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5723 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5724 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005725
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005726 /* Export the key again if permitted by the key policy. */
5727 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005728 {
Ronald Cron5425a212020-08-04 14:58:35 +02005729 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005730 second_export, export_size,
5731 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005732 ASSERT_COMPARE( first_export, first_exported_length,
5733 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005734 }
5735
5736 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005737 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005738 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005739
5740exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005741 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005742 mbedtls_free( first_export );
5743 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005745 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005746 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005747 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005748}
5749/* END_CASE */