blob: b03df3d4b1be6a26ae03c79b01dcf7921da647c7 [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)
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100111int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
Gilles Peskine667c1112019-12-03 19:03:20 +0100112{
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100113 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
114 PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100115}
116#else
117int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
118{
119 (void) lifetime;
120 return( 0 );
121}
122#endif
123
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200124/** Test if a buffer contains a constant byte value.
125 *
126 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200127 *
128 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200129 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200130 * \param size Size of the buffer in bytes.
131 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200132 * \return 1 if the buffer is all-bits-zero.
133 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200134 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200135static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200136{
137 size_t i;
138 for( i = 0; i < size; i++ )
139 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200140 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200141 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200142 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200143 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200144}
Gilles Peskine818ca122018-06-20 18:16:48 +0200145
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200146/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
147static int asn1_write_10x( unsigned char **p,
148 unsigned char *start,
149 size_t bits,
150 unsigned char x )
151{
152 int ret;
153 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200154 if( bits == 0 )
155 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
156 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200157 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300158 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200159 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
160 *p -= len;
161 ( *p )[len-1] = x;
162 if( bits % 8 == 0 )
163 ( *p )[1] |= 1;
164 else
165 ( *p )[0] |= 1 << ( bits % 8 );
166 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
167 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
168 MBEDTLS_ASN1_INTEGER ) );
169 return( len );
170}
171
172static int construct_fake_rsa_key( unsigned char *buffer,
173 size_t buffer_size,
174 unsigned char **p,
175 size_t bits,
176 int keypair )
177{
178 size_t half_bits = ( bits + 1 ) / 2;
179 int ret;
180 int len = 0;
181 /* Construct something that looks like a DER encoding of
182 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
183 * RSAPrivateKey ::= SEQUENCE {
184 * version Version,
185 * modulus INTEGER, -- n
186 * publicExponent INTEGER, -- e
187 * privateExponent INTEGER, -- d
188 * prime1 INTEGER, -- p
189 * prime2 INTEGER, -- q
190 * exponent1 INTEGER, -- d mod (p-1)
191 * exponent2 INTEGER, -- d mod (q-1)
192 * coefficient INTEGER, -- (inverse of q) mod p
193 * otherPrimeInfos OtherPrimeInfos OPTIONAL
194 * }
195 * Or, for a public key, the same structure with only
196 * version, modulus and publicExponent.
197 */
198 *p = buffer + buffer_size;
199 if( keypair )
200 {
201 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
202 asn1_write_10x( p, buffer, half_bits, 1 ) );
203 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
204 asn1_write_10x( p, buffer, half_bits, 1 ) );
205 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
206 asn1_write_10x( p, buffer, half_bits, 1 ) );
207 MBEDTLS_ASN1_CHK_ADD( len, /* q */
208 asn1_write_10x( p, buffer, half_bits, 1 ) );
209 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
210 asn1_write_10x( p, buffer, half_bits, 3 ) );
211 MBEDTLS_ASN1_CHK_ADD( len, /* d */
212 asn1_write_10x( p, buffer, bits, 1 ) );
213 }
214 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
215 asn1_write_10x( p, buffer, 17, 1 ) );
216 MBEDTLS_ASN1_CHK_ADD( len, /* n */
217 asn1_write_10x( p, buffer, bits, 1 ) );
218 if( keypair )
219 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
220 mbedtls_asn1_write_int( p, buffer, 0 ) );
221 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
222 {
223 const unsigned char tag =
224 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
225 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
226 }
227 return( len );
228}
229
Ronald Cron5425a212020-08-04 14:58:35 +0200230int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100231{
232 int ok = 0;
233 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
234 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200235 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100236 psa_key_type_t type;
237 psa_key_type_t bits;
238
239 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
240 lifetime = psa_get_key_lifetime( &attributes );
241 id = psa_get_key_id( &attributes );
242 type = psa_get_key_type( &attributes );
243 bits = psa_get_key_bits( &attributes );
244
245 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200246 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200247 {
248 TEST_ASSERT(
249 ( PSA_KEY_ID_VOLATILE_MIN <=
250 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
251 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
252 PSA_KEY_ID_VOLATILE_MAX ) );
253 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100254 else
255 {
256 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200257 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
258 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100259 }
260#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
261 /* randomly-generated 64-bit constant, should never appear in test data */
262 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
263 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100264 if( lifetime_is_dynamic_secure_element( lifetime ) )
Gilles Peskine667c1112019-12-03 19:03:20 +0100265 {
266 /* Mbed Crypto currently always exposes the slot number to
267 * applications. This is not mandated by the PSA specification
268 * and may change in future versions. */
269 TEST_EQUAL( status, 0 );
270 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
271 }
272 else
273 {
274 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
275 }
276#endif
277
278 /* Type and size */
279 TEST_ASSERT( type != 0 );
280 TEST_ASSERT( bits != 0 );
281 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
282 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
283 TEST_ASSERT( bits % 8 == 0 );
284
285 /* MAX macros concerning specific key types */
286 if( PSA_KEY_TYPE_IS_ECC( type ) )
287 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
288 else if( PSA_KEY_TYPE_IS_RSA( type ) )
289 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
290 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) <= PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE );
291
292 ok = 1;
293
294exit:
295 psa_reset_key_attributes( &attributes );
296 return( ok );
297}
298
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100299int exercise_mac_setup( psa_key_type_t key_type,
300 const unsigned char *key_bytes,
301 size_t key_length,
302 psa_algorithm_t alg,
303 psa_mac_operation_t *operation,
304 psa_status_t *status )
305{
Ronald Cron5425a212020-08-04 14:58:35 +0200306 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200307 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100308
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100309 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200310 psa_set_key_algorithm( &attributes, alg );
311 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200312 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100313
Ronald Cron5425a212020-08-04 14:58:35 +0200314 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100315 /* Whether setup succeeded or failed, abort must succeed. */
316 PSA_ASSERT( psa_mac_abort( operation ) );
317 /* If setup failed, reproduce the failure, so that the caller can
318 * test the resulting state of the operation object. */
319 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100320 {
Ronald Cron5425a212020-08-04 14:58:35 +0200321 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100322 }
323
Ronald Cron5425a212020-08-04 14:58:35 +0200324 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100325 return( 1 );
326
327exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200328 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100329 return( 0 );
330}
331
332int exercise_cipher_setup( psa_key_type_t key_type,
333 const unsigned char *key_bytes,
334 size_t key_length,
335 psa_algorithm_t alg,
336 psa_cipher_operation_t *operation,
337 psa_status_t *status )
338{
Ronald Cron5425a212020-08-04 14:58:35 +0200339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100341
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
343 psa_set_key_algorithm( &attributes, alg );
344 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200345 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100346
Ronald Cron5425a212020-08-04 14:58:35 +0200347 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100348 /* Whether setup succeeded or failed, abort must succeed. */
349 PSA_ASSERT( psa_cipher_abort( operation ) );
350 /* If setup failed, reproduce the failure, so that the caller can
351 * test the resulting state of the operation object. */
352 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100353 {
Ronald Cron5425a212020-08-04 14:58:35 +0200354 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100355 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100356 }
357
Ronald Cron5425a212020-08-04 14:58:35 +0200358 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100359 return( 1 );
360
361exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200362 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100363 return( 0 );
364}
365
Ronald Cron5425a212020-08-04 14:58:35 +0200366static int exercise_mac_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200367 psa_key_usage_t usage,
368 psa_algorithm_t alg )
369{
Jaeden Amero769ce272019-01-04 11:48:03 +0000370 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200371 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200372 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200373 size_t mac_length = sizeof( mac );
374
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100375 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200376 {
Ronald Cron5425a212020-08-04 14:58:35 +0200377 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100378 PSA_ASSERT( psa_mac_update( &operation,
379 input, sizeof( input ) ) );
380 PSA_ASSERT( psa_mac_sign_finish( &operation,
381 mac, sizeof( mac ),
382 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200383 }
384
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100385 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200386 {
387 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100388 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200389 PSA_SUCCESS :
390 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200391 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100392 PSA_ASSERT( psa_mac_update( &operation,
393 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100394 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
395 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200396 }
397
398 return( 1 );
399
400exit:
401 psa_mac_abort( &operation );
402 return( 0 );
403}
404
Ronald Cron5425a212020-08-04 14:58:35 +0200405static int exercise_cipher_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200406 psa_key_usage_t usage,
407 psa_algorithm_t alg )
408{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000409 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200410 unsigned char iv[16] = {0};
411 size_t iv_length = sizeof( iv );
412 const unsigned char plaintext[16] = "Hello, world...";
413 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
414 size_t ciphertext_length = sizeof( ciphertext );
415 unsigned char decrypted[sizeof( ciphertext )];
416 size_t part_length;
417
418 if( usage & PSA_KEY_USAGE_ENCRYPT )
419 {
Ronald Cron5425a212020-08-04 14:58:35 +0200420 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100421 PSA_ASSERT( psa_cipher_generate_iv( &operation,
422 iv, sizeof( iv ),
423 &iv_length ) );
424 PSA_ASSERT( psa_cipher_update( &operation,
425 plaintext, sizeof( plaintext ),
426 ciphertext, sizeof( ciphertext ),
427 &ciphertext_length ) );
428 PSA_ASSERT( psa_cipher_finish( &operation,
429 ciphertext + ciphertext_length,
430 sizeof( ciphertext ) - ciphertext_length,
431 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200432 ciphertext_length += part_length;
433 }
434
435 if( usage & PSA_KEY_USAGE_DECRYPT )
436 {
437 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200438 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200439 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
440 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200442 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200443 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
444 * have this macro yet. */
445 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
446 psa_get_key_type( &attributes ) );
447 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200448 }
Ronald Cron5425a212020-08-04 14:58:35 +0200449 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100450 PSA_ASSERT( psa_cipher_set_iv( &operation,
451 iv, iv_length ) );
452 PSA_ASSERT( psa_cipher_update( &operation,
453 ciphertext, ciphertext_length,
454 decrypted, sizeof( decrypted ),
455 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200456 status = psa_cipher_finish( &operation,
457 decrypted + part_length,
458 sizeof( decrypted ) - part_length,
459 &part_length );
460 /* For a stream cipher, all inputs are valid. For a block cipher,
461 * if the input is some aribtrary data rather than an actual
462 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200463 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200464 TEST_ASSERT( status == PSA_SUCCESS ||
465 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200466 else
467 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200468 }
469
470 return( 1 );
471
472exit:
473 psa_cipher_abort( &operation );
474 return( 0 );
475}
476
Ronald Cron5425a212020-08-04 14:58:35 +0200477static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200478 psa_key_usage_t usage,
479 psa_algorithm_t alg )
480{
481 unsigned char nonce[16] = {0};
482 size_t nonce_length = sizeof( nonce );
483 unsigned char plaintext[16] = "Hello, world...";
484 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
485 size_t ciphertext_length = sizeof( ciphertext );
486 size_t plaintext_length = sizeof( ciphertext );
487
488 if( usage & PSA_KEY_USAGE_ENCRYPT )
489 {
Ronald Cron5425a212020-08-04 14:58:35 +0200490 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100491 nonce, nonce_length,
492 NULL, 0,
493 plaintext, sizeof( plaintext ),
494 ciphertext, sizeof( ciphertext ),
495 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200496 }
497
498 if( usage & PSA_KEY_USAGE_DECRYPT )
499 {
500 psa_status_t verify_status =
501 ( usage & PSA_KEY_USAGE_ENCRYPT ?
502 PSA_SUCCESS :
503 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200504 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100505 nonce, nonce_length,
506 NULL, 0,
507 ciphertext, ciphertext_length,
508 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100509 &plaintext_length ),
510 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200511 }
512
513 return( 1 );
514
515exit:
516 return( 0 );
517}
518
Ronald Cron5425a212020-08-04 14:58:35 +0200519static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200520 psa_key_usage_t usage,
521 psa_algorithm_t alg )
522{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200523 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
524 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100525 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200526 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100527 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
528
529 /* If the policy allows signing with any hash, just pick one. */
530 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
531 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100532#if defined(KNOWN_SUPPORTED_HASH_ALG)
533 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
534 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100535#else
536 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100537 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100538#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100539 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200540
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100541 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200542 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200543 /* Some algorithms require the payload to have the size of
544 * the hash encoded in the algorithm. Use this input size
545 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200546 if( hash_alg != 0 )
547 payload_length = PSA_HASH_SIZE( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200548 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100549 payload, payload_length,
550 signature, sizeof( signature ),
551 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200552 }
553
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100554 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200555 {
556 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100557 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200558 PSA_SUCCESS :
559 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200560 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100561 payload, payload_length,
562 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100563 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200564 }
565
566 return( 1 );
567
568exit:
569 return( 0 );
570}
571
Ronald Cron5425a212020-08-04 14:58:35 +0200572static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200573 psa_key_usage_t usage,
574 psa_algorithm_t alg )
575{
576 unsigned char plaintext[256] = "Hello, world...";
577 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
578 size_t ciphertext_length = sizeof( ciphertext );
579 size_t plaintext_length = 16;
580
581 if( usage & PSA_KEY_USAGE_ENCRYPT )
582 {
Ronald Cron5425a212020-08-04 14:58:35 +0200583 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100584 plaintext, plaintext_length,
585 NULL, 0,
586 ciphertext, sizeof( ciphertext ),
587 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200588 }
589
590 if( usage & PSA_KEY_USAGE_DECRYPT )
591 {
592 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200593 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200594 ciphertext, ciphertext_length,
595 NULL, 0,
596 plaintext, sizeof( plaintext ),
597 &plaintext_length );
598 TEST_ASSERT( status == PSA_SUCCESS ||
599 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
600 ( status == PSA_ERROR_INVALID_ARGUMENT ||
601 status == PSA_ERROR_INVALID_PADDING ) ) );
602 }
603
604 return( 1 );
605
606exit:
607 return( 0 );
608}
Gilles Peskine02b75072018-07-01 22:31:34 +0200609
Janos Follathf2815ea2019-07-03 12:41:36 +0100610static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200611 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100612 psa_algorithm_t alg,
613 unsigned char* input1, size_t input1_length,
614 unsigned char* input2, size_t input2_length,
615 size_t capacity )
616{
617 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
618 if( PSA_ALG_IS_HKDF( alg ) )
619 {
620 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
621 PSA_KEY_DERIVATION_INPUT_SALT,
622 input1, input1_length ) );
623 PSA_ASSERT( psa_key_derivation_input_key( operation,
624 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200625 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100626 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
627 PSA_KEY_DERIVATION_INPUT_INFO,
628 input2,
629 input2_length ) );
630 }
631 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
632 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
633 {
634 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
635 PSA_KEY_DERIVATION_INPUT_SEED,
636 input1, input1_length ) );
637 PSA_ASSERT( psa_key_derivation_input_key( operation,
638 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200639 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100640 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
641 PSA_KEY_DERIVATION_INPUT_LABEL,
642 input2, input2_length ) );
643 }
644 else
645 {
646 TEST_ASSERT( ! "Key derivation algorithm not supported" );
647 }
648
Gilles Peskinec744d992019-07-30 17:26:54 +0200649 if( capacity != SIZE_MAX )
650 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100651
652 return( 1 );
653
654exit:
655 return( 0 );
656}
657
658
Ronald Cron5425a212020-08-04 14:58:35 +0200659static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200660 psa_key_usage_t usage,
661 psa_algorithm_t alg )
662{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200663 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100664 unsigned char input1[] = "Input 1";
665 size_t input1_length = sizeof( input1 );
666 unsigned char input2[] = "Input 2";
667 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200668 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100669 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200670
671 if( usage & PSA_KEY_USAGE_DERIVE )
672 {
Ronald Cron5425a212020-08-04 14:58:35 +0200673 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100674 input1, input1_length,
675 input2, input2_length, capacity ) )
676 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200677
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200678 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200679 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100680 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200681 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200682 }
683
684 return( 1 );
685
686exit:
687 return( 0 );
688}
689
Gilles Peskinec7998b72018-11-07 18:45:02 +0100690/* We need two keys to exercise key agreement. Exercise the
691 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200692static psa_status_t key_agreement_with_self(
693 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200694 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100695{
696 psa_key_type_t private_key_type;
697 psa_key_type_t public_key_type;
698 size_t key_bits;
699 uint8_t *public_key = NULL;
700 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200701 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200702 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
703 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200704 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100706
Ronald Cron5425a212020-08-04 14:58:35 +0200707 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200708 private_key_type = psa_get_key_type( &attributes );
709 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200710 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100711 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
712 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200713 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100714 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100715
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200716 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200717 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200718 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100719exit:
720 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200721 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100722 return( status );
723}
724
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200725/* We need two keys to exercise key agreement. Exercise the
726 * private key against its own public key. */
727static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200728 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200729{
730 psa_key_type_t private_key_type;
731 psa_key_type_t public_key_type;
732 size_t key_bits;
733 uint8_t *public_key = NULL;
734 size_t public_key_length;
735 uint8_t output[1024];
736 size_t output_length;
737 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200738 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
739 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200740 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200741 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200742
Ronald Cron5425a212020-08-04 14:58:35 +0200743 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200744 private_key_type = psa_get_key_type( &attributes );
745 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200746 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200747 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
748 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200749 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200750 public_key, public_key_length,
751 &public_key_length ) );
752
Ronald Cron5425a212020-08-04 14:58:35 +0200753 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200754 public_key, public_key_length,
755 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200756exit:
757 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200758 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200759 return( status );
760}
761
Ronald Cron5425a212020-08-04 14:58:35 +0200762static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200763 psa_key_usage_t usage,
764 psa_algorithm_t alg )
765{
766 int ok = 0;
767
768 if( usage & PSA_KEY_USAGE_DERIVE )
769 {
770 /* We need two keys to exercise key agreement. Exercise the
771 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200772 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200773 }
774 ok = 1;
775
776exit:
777 return( ok );
778}
779
Ronald Cron5425a212020-08-04 14:58:35 +0200780static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200781 psa_key_usage_t usage,
782 psa_algorithm_t alg )
783{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200784 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200785 unsigned char output[1];
786 int ok = 0;
787
788 if( usage & PSA_KEY_USAGE_DERIVE )
789 {
790 /* We need two keys to exercise key agreement. Exercise the
791 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200792 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200793 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200794 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200795 output,
796 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200797 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200798 }
799 ok = 1;
800
801exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200802 return( ok );
803}
804
Jaeden Amerof7dca862019-06-27 17:31:33 +0100805int asn1_skip_integer( unsigned char **p, const unsigned char *end,
806 size_t min_bits, size_t max_bits,
807 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200808{
809 size_t len;
810 size_t actual_bits;
811 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100812 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100813 MBEDTLS_ASN1_INTEGER ),
814 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200815
816 /* Check if the retrieved length doesn't extend the actual buffer's size.
817 * It is assumed here, that end >= p, which validates casting to size_t. */
818 TEST_ASSERT( len <= (size_t)( end - *p) );
819
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200820 /* Tolerate a slight departure from DER encoding:
821 * - 0 may be represented by an empty string or a 1-byte string.
822 * - The sign bit may be used as a value bit. */
823 if( ( len == 1 && ( *p )[0] == 0 ) ||
824 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
825 {
826 ++( *p );
827 --len;
828 }
829 if( min_bits == 0 && len == 0 )
830 return( 1 );
831 msb = ( *p )[0];
832 TEST_ASSERT( msb != 0 );
833 actual_bits = 8 * ( len - 1 );
834 while( msb != 0 )
835 {
836 msb >>= 1;
837 ++actual_bits;
838 }
839 TEST_ASSERT( actual_bits >= min_bits );
840 TEST_ASSERT( actual_bits <= max_bits );
841 if( must_be_odd )
842 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
843 *p += len;
844 return( 1 );
845exit:
846 return( 0 );
847}
848
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200849static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
850 uint8_t *exported, size_t exported_length )
851{
852 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100853 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200854 else
855 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200856
857#if defined(MBEDTLS_DES_C)
858 if( type == PSA_KEY_TYPE_DES )
859 {
860 /* Check the parity bits. */
861 unsigned i;
862 for( i = 0; i < bits / 8; i++ )
863 {
864 unsigned bit_count = 0;
865 unsigned m;
866 for( m = 1; m <= 0x100; m <<= 1 )
867 {
868 if( exported[i] & m )
869 ++bit_count;
870 }
871 TEST_ASSERT( bit_count % 2 != 0 );
872 }
873 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200874 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200875#endif
876
877#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200878 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200879 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200880 uint8_t *p = exported;
881 uint8_t *end = exported + exported_length;
882 size_t len;
883 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200884 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200885 * modulus INTEGER, -- n
886 * publicExponent INTEGER, -- e
887 * privateExponent INTEGER, -- d
888 * prime1 INTEGER, -- p
889 * prime2 INTEGER, -- q
890 * exponent1 INTEGER, -- d mod (p-1)
891 * exponent2 INTEGER, -- d mod (q-1)
892 * coefficient INTEGER, -- (inverse of q) mod p
893 * }
894 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100895 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
896 MBEDTLS_ASN1_SEQUENCE |
897 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
898 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200899 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
900 goto exit;
901 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
902 goto exit;
903 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
904 goto exit;
905 /* Require d to be at least half the size of n. */
906 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
907 goto exit;
908 /* Require p and q to be at most half the size of n, rounded up. */
909 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
910 goto exit;
911 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
912 goto exit;
913 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
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;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100919 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100920 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200921 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200922#endif /* MBEDTLS_RSA_C */
923
924#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200925 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200926 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100927 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100928 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100929 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200930 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200931#endif /* MBEDTLS_ECP_C */
932
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200933 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
934 {
935 uint8_t *p = exported;
936 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200937#if defined(MBEDTLS_RSA_C)
938 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
939 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100940 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200941 /* RSAPublicKey ::= SEQUENCE {
942 * modulus INTEGER, -- n
943 * publicExponent INTEGER } -- e
944 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
946 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100947 MBEDTLS_ASN1_CONSTRUCTED ),
948 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100949 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200950 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
951 goto exit;
952 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
953 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100954 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200955 }
956 else
957#endif /* MBEDTLS_RSA_C */
958#if defined(MBEDTLS_ECP_C)
959 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
960 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200961 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
962 {
963 /* The representation of an ECC Montgomery public key is
964 * the raw compressed point */
965 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
966 }
967 else
968 {
969 /* The representation of an ECC Weierstrass public key is:
970 * - The byte 0x04;
971 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
972 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
973 * - where m is the bit size associated with the curve.
974 */
975 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
976 TEST_EQUAL( p[0], 4 );
977 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200978 }
979 else
980#endif /* MBEDTLS_ECP_C */
981 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100982 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200983 mbedtls_snprintf( message, sizeof( message ),
984 "No sanity check for public key type=0x%08lx",
985 (unsigned long) type );
986 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200987 (void) p;
988 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200989 return( 0 );
990 }
991 }
992 else
993
994 {
995 /* No sanity checks for other types */
996 }
997
998 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200999
1000exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001001 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001002}
1003
Ronald Cron5425a212020-08-04 14:58:35 +02001004static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001005 psa_key_usage_t usage )
1006{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001008 uint8_t *exported = NULL;
1009 size_t exported_size = 0;
1010 size_t exported_length = 0;
1011 int ok = 0;
1012
Ronald Cron5425a212020-08-04 14:58:35 +02001013 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001014
1015 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001017 {
Ronald Cron5425a212020-08-04 14:58:35 +02001018 TEST_EQUAL( psa_export_key( key, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001019 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001020 ok = 1;
1021 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001022 }
1023
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001024 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
1025 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001026 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001027
Ronald Cron5425a212020-08-04 14:58:35 +02001028 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001029 exported, exported_size,
1030 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001031 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1032 psa_get_key_bits( &attributes ),
1033 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001034
1035exit:
1036 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001037 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001038 return( ok );
1039}
1040
Ronald Cron5425a212020-08-04 14:58:35 +02001041static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001042{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001044 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001045 uint8_t *exported = NULL;
1046 size_t exported_size = 0;
1047 size_t exported_length = 0;
1048 int ok = 0;
1049
Ronald Cron5425a212020-08-04 14:58:35 +02001050 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001051 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001052 {
Ronald Cron5425a212020-08-04 14:58:35 +02001053 TEST_EQUAL( psa_export_public_key( key, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001054 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +02001055 return( 1 );
1056 }
1057
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001058 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001059 psa_get_key_type( &attributes ) );
1060 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
1061 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001062 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001063
Ronald Cron5425a212020-08-04 14:58:35 +02001064 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001065 exported, exported_size,
1066 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001067 ok = exported_key_sanity_check( public_type,
1068 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001069 exported, exported_length );
1070
1071exit:
1072 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001073 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001074 return( ok );
1075}
1076
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001077/** Do smoke tests on a key.
1078 *
1079 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1080 * sign/verify, or derivation) that is permitted according to \p usage.
1081 * \p usage and \p alg should correspond to the expected policy on the
1082 * key.
1083 *
1084 * Export the key if permitted by \p usage, and check that the output
1085 * looks sensible. If \p usage forbids export, check that
1086 * \p psa_export_key correctly rejects the attempt. If the key is
1087 * asymmetric, also check \p psa_export_public_key.
1088 *
1089 * If the key fails the tests, this function calls the test framework's
1090 * `test_fail` function and returns false. Otherwise this function returns
1091 * true. Therefore it should be used as follows:
1092 * ```
1093 * if( ! exercise_key( ... ) ) goto exit;
1094 * ```
1095 *
Ronald Cron5425a212020-08-04 14:58:35 +02001096 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001097 * \p alg.
1098 * \param usage The usage flags to assume.
1099 * \param alg The algorithm to exercise.
1100 *
1101 * \retval 0 The key failed the smoke tests.
1102 * \retval 1 The key passed the smoke tests.
1103 */
Ronald Cron5425a212020-08-04 14:58:35 +02001104static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001105 psa_key_usage_t usage,
1106 psa_algorithm_t alg )
1107{
1108 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001109
Ronald Cron5425a212020-08-04 14:58:35 +02001110 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001111 return( 0 );
1112
Gilles Peskine02b75072018-07-01 22:31:34 +02001113 if( alg == 0 )
1114 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1115 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001116 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001117 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001118 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001119 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001120 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001121 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001122 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001123 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001124 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001125 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001126 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001127 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001128 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001129 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001130 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001131 else
1132 {
1133 char message[40];
1134 mbedtls_snprintf( message, sizeof( message ),
1135 "No code to exercise alg=0x%08lx",
1136 (unsigned long) alg );
1137 test_fail( message, __LINE__, __FILE__ );
1138 ok = 0;
1139 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001140
Ronald Cron5425a212020-08-04 14:58:35 +02001141 ok = ok && exercise_export_key( key, usage );
1142 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001143
Gilles Peskine02b75072018-07-01 22:31:34 +02001144 return( ok );
1145}
1146
Gilles Peskine10df3412018-10-25 22:35:43 +02001147static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1148 psa_algorithm_t alg )
1149{
1150 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1151 {
1152 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001153 PSA_KEY_USAGE_VERIFY_HASH :
1154 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001155 }
1156 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1157 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1158 {
1159 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1160 PSA_KEY_USAGE_ENCRYPT :
1161 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1162 }
1163 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1164 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1165 {
1166 return( PSA_KEY_USAGE_DERIVE );
1167 }
1168 else
1169 {
1170 return( 0 );
1171 }
1172
1173}
Darryl Green0c6575a2018-11-07 16:05:30 +00001174
Ronald Cron5425a212020-08-04 14:58:35 +02001175static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001176{
1177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001178 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001179 uint8_t buffer[1];
1180 size_t length;
1181 int ok = 0;
1182
Ronald Cronecfb2372020-07-23 17:13:42 +02001183 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001184 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1185 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1186 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001187 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001188 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001189 TEST_EQUAL(
1190 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1191 TEST_EQUAL(
1192 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001193 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001194 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1195 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1196 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1197 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1198
Ronald Cron5425a212020-08-04 14:58:35 +02001199 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001200 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001201 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001202 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001203 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001204
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001205 ok = 1;
1206
1207exit:
1208 psa_reset_key_attributes( &attributes );
1209 return( ok );
1210}
1211
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001212/* Assert that a key isn't reported as having a slot number. */
1213#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1214#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1215 do \
1216 { \
1217 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1218 TEST_EQUAL( psa_get_key_slot_number( \
1219 attributes, \
1220 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1221 PSA_ERROR_INVALID_ARGUMENT ); \
1222 } \
1223 while( 0 )
1224#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1225#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1226 ( (void) 0 )
1227#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1228
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001229/* An overapproximation of the amount of storage needed for a key of the
1230 * given type and with the given content. The API doesn't make it easy
1231 * to find a good value for the size. The current implementation doesn't
1232 * care about the value anyway. */
1233#define KEY_BITS_FROM_DATA( type, data ) \
1234 ( data )->len
1235
Darryl Green0c6575a2018-11-07 16:05:30 +00001236typedef enum {
1237 IMPORT_KEY = 0,
1238 GENERATE_KEY = 1,
1239 DERIVE_KEY = 2
1240} generate_method;
1241
Gilles Peskinee59236f2018-01-27 23:32:46 +01001242/* END_HEADER */
1243
1244/* BEGIN_DEPENDENCIES
1245 * depends_on:MBEDTLS_PSA_CRYPTO_C
1246 * END_DEPENDENCIES
1247 */
1248
1249/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001250void static_checks( )
1251{
1252 size_t max_truncated_mac_size =
1253 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1254
1255 /* Check that the length for a truncated MAC always fits in the algorithm
1256 * encoding. The shifted mask is the maximum truncated value. The
1257 * untruncated algorithm may be one byte larger. */
1258 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001259
1260#if defined(MBEDTLS_TEST_DEPRECATED)
1261 /* Check deprecated constants. */
1262 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1263 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1264 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1265 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1266 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1267 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1268 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1269 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001270
Paul Elliott8ff510a2020-06-02 17:19:28 +01001271 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1272 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1273 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1274 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1275 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1276 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1277 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1278 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1279 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1280 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1281 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1282 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1283 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1284 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1285 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1286 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1287 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1288 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1289 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1290 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1291 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1292 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1293 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1294 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1295 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1296 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1297 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1298 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1299 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1300 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1301
1302 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1303 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1304 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1305 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1306 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1308 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1309 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001310
Paul Elliott75e27032020-06-03 15:17:39 +01001311 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1312 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1313 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1314 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1315 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1316
1317 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1318 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001319#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001320}
1321/* END_CASE */
1322
1323/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001324void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001325 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001326 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327{
Gilles Peskine4747d192019-04-17 15:05:45 +02001328 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001329 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001330 psa_key_lifetime_t lifetime = lifetime_arg;
1331 psa_key_usage_t usage_flags = usage_flags_arg;
1332 psa_algorithm_t alg = alg_arg;
1333 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001334 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001335
Ronald Cronecfb2372020-07-23 17:13:42 +02001336 TEST_EQUAL(
1337 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1338 TEST_EQUAL(
1339 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001340 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1341 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1342 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1343 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001344 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001345
Gilles Peskinec87af662019-05-15 16:12:22 +02001346 psa_set_key_id( &attributes, id );
1347 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001348 psa_set_key_usage_flags( &attributes, usage_flags );
1349 psa_set_key_algorithm( &attributes, alg );
1350 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001351 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001352
Ronald Cronecfb2372020-07-23 17:13:42 +02001353 TEST_ASSERT( mbedtls_svc_key_id_equal(
1354 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001355 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1356 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1357 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1358 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001359 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001360
1361 psa_reset_key_attributes( &attributes );
1362
Ronald Cronecfb2372020-07-23 17:13:42 +02001363 TEST_EQUAL(
1364 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1365 TEST_EQUAL(
1366 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001367 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1368 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1369 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1370 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001371 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001372}
1373/* END_CASE */
1374
1375/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001376void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1377 int id2_arg, int owner_id2_arg,
1378 int expected_id_arg, int expected_owner_id_arg,
1379 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001380{
1381 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001382 mbedtls_svc_key_id_t id1 =
1383 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001384 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001385 mbedtls_svc_key_id_t id2 =
1386 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001387 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001388 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001389 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1390
1391 if( id1_arg != -1 )
1392 psa_set_key_id( &attributes, id1 );
1393 if( lifetime_arg != -1 )
1394 psa_set_key_lifetime( &attributes, lifetime );
1395 if( id2_arg != -1 )
1396 psa_set_key_id( &attributes, id2 );
1397
Ronald Cronecfb2372020-07-23 17:13:42 +02001398 TEST_ASSERT( mbedtls_svc_key_id_equal(
1399 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001400 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1401}
1402/* END_CASE */
1403
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001404/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1405void slot_number_attribute( )
1406{
1407 psa_key_slot_number_t slot_number = 0xdeadbeef;
1408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1409
1410 /* Initially, there is no slot number. */
1411 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1412 PSA_ERROR_INVALID_ARGUMENT );
1413
1414 /* Test setting a slot number. */
1415 psa_set_key_slot_number( &attributes, 0 );
1416 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1417 TEST_EQUAL( slot_number, 0 );
1418
1419 /* Test changing the slot number. */
1420 psa_set_key_slot_number( &attributes, 42 );
1421 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1422 TEST_EQUAL( slot_number, 42 );
1423
1424 /* Test clearing the slot number. */
1425 psa_clear_key_slot_number( &attributes );
1426 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1427 PSA_ERROR_INVALID_ARGUMENT );
1428
1429 /* Clearing again should have no effect. */
1430 psa_clear_key_slot_number( &attributes );
1431 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1432 PSA_ERROR_INVALID_ARGUMENT );
1433
1434 /* Test that reset clears the slot number. */
1435 psa_set_key_slot_number( &attributes, 42 );
1436 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1437 TEST_EQUAL( slot_number, 42 );
1438 psa_reset_key_attributes( &attributes );
1439 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1440 PSA_ERROR_INVALID_ARGUMENT );
1441}
1442/* END_CASE */
1443
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001444/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001445void import_with_policy( int type_arg,
1446 int usage_arg, int alg_arg,
1447 int expected_status_arg )
1448{
1449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1450 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001451 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001452 psa_key_type_t type = type_arg;
1453 psa_key_usage_t usage = usage_arg;
1454 psa_algorithm_t alg = alg_arg;
1455 psa_status_t expected_status = expected_status_arg;
1456 const uint8_t key_material[16] = {0};
1457 psa_status_t status;
1458
1459 PSA_ASSERT( psa_crypto_init( ) );
1460
1461 psa_set_key_type( &attributes, type );
1462 psa_set_key_usage_flags( &attributes, usage );
1463 psa_set_key_algorithm( &attributes, alg );
1464
1465 status = psa_import_key( &attributes,
1466 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001467 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001468 TEST_EQUAL( status, expected_status );
1469 if( status != PSA_SUCCESS )
1470 goto exit;
1471
Ronald Cron5425a212020-08-04 14:58:35 +02001472 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001473 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1474 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1475 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001476 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001477
Ronald Cron5425a212020-08-04 14:58:35 +02001478 PSA_ASSERT( psa_destroy_key( key ) );
1479 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001480
1481exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001482 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001483 psa_reset_key_attributes( &got_attributes );
1484 PSA_DONE( );
1485}
1486/* END_CASE */
1487
1488/* BEGIN_CASE */
1489void import_with_data( data_t *data, int type_arg,
1490 int attr_bits_arg,
1491 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001492{
1493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1494 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001495 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001496 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001497 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001498 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001499 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001500
Gilles Peskine8817f612018-12-18 00:18:46 +01001501 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001502
Gilles Peskine4747d192019-04-17 15:05:45 +02001503 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001504 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001505
Ronald Cron5425a212020-08-04 14:58:35 +02001506 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001507 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001508 if( status != PSA_SUCCESS )
1509 goto exit;
1510
Ronald Cron5425a212020-08-04 14:58:35 +02001511 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001512 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001513 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001514 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001515 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001516
Ronald Cron5425a212020-08-04 14:58:35 +02001517 PSA_ASSERT( psa_destroy_key( key ) );
1518 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001519
1520exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001521 psa_destroy_key( key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001522 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001523 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524}
1525/* END_CASE */
1526
1527/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001528void import_large_key( int type_arg, int byte_size_arg,
1529 int expected_status_arg )
1530{
1531 psa_key_type_t type = type_arg;
1532 size_t byte_size = byte_size_arg;
1533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1534 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001535 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001536 psa_status_t status;
1537 uint8_t *buffer = NULL;
1538 size_t buffer_size = byte_size + 1;
1539 size_t n;
1540
1541 /* It would be better to skip the test than fail it if the allocation
1542 * fails, but the test framework doesn't support this yet. */
1543 ASSERT_ALLOC( buffer, buffer_size );
1544 memset( buffer, 'K', byte_size );
1545
1546 PSA_ASSERT( psa_crypto_init( ) );
1547
1548 /* Try importing the key */
1549 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1550 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001551 status = psa_import_key( &attributes, buffer, byte_size, &key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001552 TEST_EQUAL( status, expected_status );
1553
1554 if( status == PSA_SUCCESS )
1555 {
Ronald Cron5425a212020-08-04 14:58:35 +02001556 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001557 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1558 TEST_EQUAL( psa_get_key_bits( &attributes ),
1559 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001560 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001561 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001562 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001563 for( n = 0; n < byte_size; n++ )
1564 TEST_EQUAL( buffer[n], 'K' );
1565 for( n = byte_size; n < buffer_size; n++ )
1566 TEST_EQUAL( buffer[n], 0 );
1567 }
1568
1569exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001570 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001571 PSA_DONE( );
1572 mbedtls_free( buffer );
1573}
1574/* END_CASE */
1575
1576/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001577void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1578{
Ronald Cron5425a212020-08-04 14:58:35 +02001579 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001580 size_t bits = bits_arg;
1581 psa_status_t expected_status = expected_status_arg;
1582 psa_status_t status;
1583 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001584 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001585 size_t buffer_size = /* Slight overapproximations */
1586 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001587 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001588 unsigned char *p;
1589 int ret;
1590 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001592
Gilles Peskine8817f612018-12-18 00:18:46 +01001593 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001594 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001595
1596 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1597 bits, keypair ) ) >= 0 );
1598 length = ret;
1599
1600 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001601 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001602 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001603 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001604
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001605 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001606 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001607
1608exit:
1609 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001610 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001611}
1612/* END_CASE */
1613
1614/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001615void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001616 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001617 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001618 int expected_bits,
1619 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001620 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001621 int canonical_input )
1622{
Ronald Cron5425a212020-08-04 14:58:35 +02001623 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001624 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001625 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001626 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001627 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001628 unsigned char *exported = NULL;
1629 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001630 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001631 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001632 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001634 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001635
Moran Pekercb088e72018-07-17 17:36:59 +03001636 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001637 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001638 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001639 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001640 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001641
Gilles Peskine4747d192019-04-17 15:05:45 +02001642 psa_set_key_usage_flags( &attributes, usage_arg );
1643 psa_set_key_algorithm( &attributes, alg );
1644 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001645
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001646 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001647 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001648
1649 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001650 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001651 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1652 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001653 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001654
1655 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001656 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001657 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001658
1659 /* The exported length must be set by psa_export_key() to a value between 0
1660 * and export_size. On errors, the exported length must be 0. */
1661 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1662 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1663 TEST_ASSERT( exported_length <= export_size );
1664
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001665 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001666 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001667 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001668 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001669 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001670 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001671 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001672
Ronald Cron5425a212020-08-04 14:58:35 +02001673 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001674 goto exit;
1675
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001676 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001677 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001678 else
1679 {
Ronald Cron5425a212020-08-04 14:58:35 +02001680 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001681 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001682 &key2 ) );
1683 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001684 reexported,
1685 export_size,
1686 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001687 ASSERT_COMPARE( exported, exported_length,
1688 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001689 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001690 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001691 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001692
1693destroy:
1694 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001695 PSA_ASSERT( psa_destroy_key( key ) );
1696 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001697
1698exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001699 mbedtls_free( exported );
1700 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001701 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001702 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001703}
1704/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001705
Moran Pekerf709f4a2018-06-06 17:26:04 +03001706/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001707void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001708 int type_arg,
1709 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001710 int export_size_delta,
1711 int expected_export_status_arg,
1712 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001713{
Ronald Cron5425a212020-08-04 14:58:35 +02001714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001715 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001716 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001717 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001718 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001719 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001720 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001721 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001723
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001725
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1727 psa_set_key_algorithm( &attributes, alg );
1728 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001729
1730 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001731 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001732
Gilles Peskine49c25912018-10-29 15:15:31 +01001733 /* Export the public key */
1734 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001735 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001736 exported, export_size,
1737 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001738 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001739 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001740 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001741 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001742 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001743 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001744 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001745 TEST_ASSERT( expected_public_key->len <=
1746 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001747 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1748 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001749 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001750
1751exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001752 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001753 psa_destroy_key( key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001754 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001755 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001756}
1757/* END_CASE */
1758
Gilles Peskine20035e32018-02-03 22:44:14 +01001759/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001760void import_and_exercise_key( data_t *data,
1761 int type_arg,
1762 int bits_arg,
1763 int alg_arg )
1764{
Ronald Cron5425a212020-08-04 14:58:35 +02001765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001766 psa_key_type_t type = type_arg;
1767 size_t bits = bits_arg;
1768 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001769 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001771 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001772
Gilles Peskine8817f612018-12-18 00:18:46 +01001773 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001774
Gilles Peskine4747d192019-04-17 15:05:45 +02001775 psa_set_key_usage_flags( &attributes, usage );
1776 psa_set_key_algorithm( &attributes, alg );
1777 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001778
1779 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001780 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001781
1782 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001783 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001784 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1785 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001786
1787 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001788 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001789 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001790
Ronald Cron5425a212020-08-04 14:58:35 +02001791 PSA_ASSERT( psa_destroy_key( key ) );
1792 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001793
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001794exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001795 psa_destroy_key( key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001796 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001797 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001798}
1799/* END_CASE */
1800
1801/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001802void effective_key_attributes( int type_arg, int expected_type_arg,
1803 int bits_arg, int expected_bits_arg,
1804 int usage_arg, int expected_usage_arg,
1805 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001806{
Ronald Cron5425a212020-08-04 14:58:35 +02001807 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001808 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001809 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001810 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001811 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001812 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001813 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001814 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001815 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001817
Gilles Peskine8817f612018-12-18 00:18:46 +01001818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001819
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001820 psa_set_key_usage_flags( &attributes, usage );
1821 psa_set_key_algorithm( &attributes, alg );
1822 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001823 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001824
Ronald Cron5425a212020-08-04 14:58:35 +02001825 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001826 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001827
Ronald Cron5425a212020-08-04 14:58:35 +02001828 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001829 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1830 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1831 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1832 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001833
1834exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001835 psa_destroy_key( key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001836 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001837 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001838}
1839/* END_CASE */
1840
1841/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001842void check_key_policy( int type_arg, int bits_arg,
1843 int usage_arg, int alg_arg )
1844{
1845 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1846 usage_arg, usage_arg, alg_arg, alg_arg );
1847 goto exit;
1848}
1849/* END_CASE */
1850
1851/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001852void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001853{
1854 /* Test each valid way of initializing the object, except for `= {0}`, as
1855 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1856 * though it's OK by the C standard. We could test for this, but we'd need
1857 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001858 psa_key_attributes_t func = psa_key_attributes_init( );
1859 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1860 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001861
1862 memset( &zero, 0, sizeof( zero ) );
1863
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001864 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1865 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1866 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001867
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001868 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1869 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1870 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1871
1872 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1873 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1874 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1875
1876 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1877 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1878 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1879
1880 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1881 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1882 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001883}
1884/* END_CASE */
1885
1886/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001887void mac_key_policy( int policy_usage,
1888 int policy_alg,
1889 int key_type,
1890 data_t *key_data,
1891 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001892{
Ronald Cron5425a212020-08-04 14:58:35 +02001893 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001895 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001896 psa_status_t status;
1897 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001898
Gilles Peskine8817f612018-12-18 00:18:46 +01001899 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001900
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001901 psa_set_key_usage_flags( &attributes, policy_usage );
1902 psa_set_key_algorithm( &attributes, policy_alg );
1903 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001904
Gilles Peskine049c7532019-05-15 20:22:09 +02001905 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001906 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001907
Ronald Cron5425a212020-08-04 14:58:35 +02001908 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001909 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001910 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001911 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001912 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001913 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001915
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001916 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001917 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001919 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001920 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001922 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001923
1924exit:
1925 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001926 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001927 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001928}
1929/* END_CASE */
1930
1931/* BEGIN_CASE */
1932void cipher_key_policy( int policy_usage,
1933 int policy_alg,
1934 int key_type,
1935 data_t *key_data,
1936 int exercise_alg )
1937{
Ronald Cron5425a212020-08-04 14:58:35 +02001938 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001940 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941 psa_status_t status;
1942
Gilles Peskine8817f612018-12-18 00:18:46 +01001943 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001944
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001945 psa_set_key_usage_flags( &attributes, policy_usage );
1946 psa_set_key_algorithm( &attributes, policy_alg );
1947 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001948
Gilles Peskine049c7532019-05-15 20:22:09 +02001949 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001950 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001951
Ronald Cron5425a212020-08-04 14:58:35 +02001952 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953 if( policy_alg == exercise_alg &&
1954 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001955 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001957 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958 psa_cipher_abort( &operation );
1959
Ronald Cron5425a212020-08-04 14:58:35 +02001960 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001961 if( policy_alg == exercise_alg &&
1962 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001963 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001964 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001965 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001966
1967exit:
1968 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001969 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001970 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001971}
1972/* END_CASE */
1973
1974/* BEGIN_CASE */
1975void aead_key_policy( int policy_usage,
1976 int policy_alg,
1977 int key_type,
1978 data_t *key_data,
1979 int nonce_length_arg,
1980 int tag_length_arg,
1981 int exercise_alg )
1982{
Ronald Cron5425a212020-08-04 14:58:35 +02001983 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001984 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985 psa_status_t status;
1986 unsigned char nonce[16] = {0};
1987 size_t nonce_length = nonce_length_arg;
1988 unsigned char tag[16];
1989 size_t tag_length = tag_length_arg;
1990 size_t output_length;
1991
1992 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1993 TEST_ASSERT( tag_length <= sizeof( tag ) );
1994
Gilles Peskine8817f612018-12-18 00:18:46 +01001995 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001996
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001997 psa_set_key_usage_flags( &attributes, policy_usage );
1998 psa_set_key_algorithm( &attributes, policy_alg );
1999 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002000
Gilles Peskine049c7532019-05-15 20:22:09 +02002001 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002002 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003
Ronald Cron5425a212020-08-04 14:58:35 +02002004 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002005 nonce, nonce_length,
2006 NULL, 0,
2007 NULL, 0,
2008 tag, tag_length,
2009 &output_length );
2010 if( policy_alg == exercise_alg &&
2011 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002012 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002014 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002015
2016 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002017 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002018 nonce, nonce_length,
2019 NULL, 0,
2020 tag, tag_length,
2021 NULL, 0,
2022 &output_length );
2023 if( policy_alg == exercise_alg &&
2024 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002025 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002026 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002027 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028
2029exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002030 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002031 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032}
2033/* END_CASE */
2034
2035/* BEGIN_CASE */
2036void asymmetric_encryption_key_policy( int policy_usage,
2037 int policy_alg,
2038 int key_type,
2039 data_t *key_data,
2040 int exercise_alg )
2041{
Ronald Cron5425a212020-08-04 14:58:35 +02002042 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002044 psa_status_t status;
2045 size_t key_bits;
2046 size_t buffer_length;
2047 unsigned char *buffer = NULL;
2048 size_t output_length;
2049
Gilles Peskine8817f612018-12-18 00:18:46 +01002050 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002051
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002052 psa_set_key_usage_flags( &attributes, policy_usage );
2053 psa_set_key_algorithm( &attributes, policy_alg );
2054 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002055
Gilles Peskine049c7532019-05-15 20:22:09 +02002056 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002057 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002058
Ronald Cron5425a212020-08-04 14:58:35 +02002059 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002060 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2062 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002063 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002064
Ronald Cron5425a212020-08-04 14:58:35 +02002065 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066 NULL, 0,
2067 NULL, 0,
2068 buffer, buffer_length,
2069 &output_length );
2070 if( policy_alg == exercise_alg &&
2071 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002072 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002074 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002076 if( buffer_length != 0 )
2077 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002078 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079 buffer, buffer_length,
2080 NULL, 0,
2081 buffer, buffer_length,
2082 &output_length );
2083 if( policy_alg == exercise_alg &&
2084 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002085 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002086 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002087 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002088
2089exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002090 psa_destroy_key( key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002091 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002092 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002093 mbedtls_free( buffer );
2094}
2095/* END_CASE */
2096
2097/* BEGIN_CASE */
2098void asymmetric_signature_key_policy( int policy_usage,
2099 int policy_alg,
2100 int key_type,
2101 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002102 int exercise_alg,
2103 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002104{
Ronald Cron5425a212020-08-04 14:58:35 +02002105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002107 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002108 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2109 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2110 * compatible with the policy and `payload_length_arg` is supposed to be
2111 * a valid input length to sign. If `payload_length_arg <= 0`,
2112 * `exercise_alg` is supposed to be forbidden by the policy. */
2113 int compatible_alg = payload_length_arg > 0;
2114 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002115 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002116 size_t signature_length;
2117
Gilles Peskine8817f612018-12-18 00:18:46 +01002118 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002119
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002120 psa_set_key_usage_flags( &attributes, policy_usage );
2121 psa_set_key_algorithm( &attributes, policy_alg );
2122 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123
Gilles Peskine049c7532019-05-15 20:22:09 +02002124 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002125 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002126
Ronald Cron5425a212020-08-04 14:58:35 +02002127 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002128 payload, payload_length,
2129 signature, sizeof( signature ),
2130 &signature_length );
2131 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002132 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002133 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002134 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002135
2136 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002137 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002138 payload, payload_length,
2139 signature, sizeof( signature ) );
2140 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002141 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002142 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002143 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002144
2145exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002146 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002147 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002148}
2149/* END_CASE */
2150
Janos Follathba3fab92019-06-11 14:50:16 +01002151/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002152void derive_key_policy( int policy_usage,
2153 int policy_alg,
2154 int key_type,
2155 data_t *key_data,
2156 int exercise_alg )
2157{
Ronald Cron5425a212020-08-04 14:58:35 +02002158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002160 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002161 psa_status_t status;
2162
Gilles Peskine8817f612018-12-18 00:18:46 +01002163 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002164
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002165 psa_set_key_usage_flags( &attributes, policy_usage );
2166 psa_set_key_algorithm( &attributes, policy_alg );
2167 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002168
Gilles Peskine049c7532019-05-15 20:22:09 +02002169 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002170 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002171
Janos Follathba3fab92019-06-11 14:50:16 +01002172 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2173
2174 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2175 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002176 {
Janos Follathba3fab92019-06-11 14:50:16 +01002177 PSA_ASSERT( psa_key_derivation_input_bytes(
2178 &operation,
2179 PSA_KEY_DERIVATION_INPUT_SEED,
2180 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002181 }
Janos Follathba3fab92019-06-11 14:50:16 +01002182
2183 status = psa_key_derivation_input_key( &operation,
2184 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002185 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002186
Gilles Peskineea0fb492018-07-12 17:17:20 +02002187 if( policy_alg == exercise_alg &&
2188 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002189 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002190 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002191 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002192
2193exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002194 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002195 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002196 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002197}
2198/* END_CASE */
2199
2200/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002201void agreement_key_policy( int policy_usage,
2202 int policy_alg,
2203 int key_type_arg,
2204 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002205 int exercise_alg,
2206 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002207{
Ronald Cron5425a212020-08-04 14:58:35 +02002208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002210 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002211 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002212 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002213 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002214
Gilles Peskine8817f612018-12-18 00:18:46 +01002215 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002216
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002217 psa_set_key_usage_flags( &attributes, policy_usage );
2218 psa_set_key_algorithm( &attributes, policy_alg );
2219 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002220
Gilles Peskine049c7532019-05-15 20:22:09 +02002221 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002222 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002223
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002224 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002225 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002226
Steven Cooremance48e852020-10-05 16:02:45 +02002227 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002228
2229exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002230 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002231 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002232 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002233}
2234/* END_CASE */
2235
2236/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002237void key_policy_alg2( int key_type_arg, data_t *key_data,
2238 int usage_arg, int alg_arg, int alg2_arg )
2239{
Ronald Cron5425a212020-08-04 14:58:35 +02002240 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002241 psa_key_type_t key_type = key_type_arg;
2242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2243 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2244 psa_key_usage_t usage = usage_arg;
2245 psa_algorithm_t alg = alg_arg;
2246 psa_algorithm_t alg2 = alg2_arg;
2247
2248 PSA_ASSERT( psa_crypto_init( ) );
2249
2250 psa_set_key_usage_flags( &attributes, usage );
2251 psa_set_key_algorithm( &attributes, alg );
2252 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2253 psa_set_key_type( &attributes, key_type );
2254 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002255 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002256
Ronald Cron5425a212020-08-04 14:58:35 +02002257 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002258 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2259 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2260 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2261
Ronald Cron5425a212020-08-04 14:58:35 +02002262 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002263 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002264 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002265 goto exit;
2266
2267exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002268 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002269 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002270}
2271/* END_CASE */
2272
2273/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002274void raw_agreement_key_policy( int policy_usage,
2275 int policy_alg,
2276 int key_type_arg,
2277 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002278 int exercise_alg,
2279 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002280{
Ronald Cron5425a212020-08-04 14:58:35 +02002281 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002283 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002284 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002285 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002286 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002287
2288 PSA_ASSERT( psa_crypto_init( ) );
2289
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002290 psa_set_key_usage_flags( &attributes, policy_usage );
2291 psa_set_key_algorithm( &attributes, policy_alg );
2292 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002293
Gilles Peskine049c7532019-05-15 20:22:09 +02002294 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002295 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002296
Ronald Cron5425a212020-08-04 14:58:35 +02002297 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002298
Steven Cooremance48e852020-10-05 16:02:45 +02002299 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002300
2301exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002302 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002303 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002304 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002305}
2306/* END_CASE */
2307
2308/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002309void copy_success( int source_usage_arg,
2310 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002311 int type_arg, data_t *material,
2312 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002313 int target_usage_arg,
2314 int target_alg_arg, int target_alg2_arg,
2315 int expected_usage_arg,
2316 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002317{
Gilles Peskineca25db92019-04-19 11:43:08 +02002318 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2319 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002320 psa_key_usage_t expected_usage = expected_usage_arg;
2321 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002322 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002323 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2324 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002325 uint8_t *export_buffer = NULL;
2326
Gilles Peskine57ab7212019-01-28 13:03:09 +01002327 PSA_ASSERT( psa_crypto_init( ) );
2328
Gilles Peskineca25db92019-04-19 11:43:08 +02002329 /* Prepare the source key. */
2330 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2331 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002332 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002333 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002334 PSA_ASSERT( psa_import_key( &source_attributes,
2335 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002336 &source_key ) );
2337 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002338
Gilles Peskineca25db92019-04-19 11:43:08 +02002339 /* Prepare the target attributes. */
2340 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002341 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002342 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002343 /* Set volatile lifetime to reset the key identifier to 0. */
2344 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2345 }
2346
Gilles Peskineca25db92019-04-19 11:43:08 +02002347 if( target_usage_arg != -1 )
2348 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2349 if( target_alg_arg != -1 )
2350 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002351 if( target_alg2_arg != -1 )
2352 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002353
2354 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002355 PSA_ASSERT( psa_copy_key( source_key,
2356 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002357
2358 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002359 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002360
2361 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002362 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002363 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2364 psa_get_key_type( &target_attributes ) );
2365 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2366 psa_get_key_bits( &target_attributes ) );
2367 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2368 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002369 TEST_EQUAL( expected_alg2,
2370 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002371 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2372 {
2373 size_t length;
2374 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002375 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002376 material->len, &length ) );
2377 ASSERT_COMPARE( material->x, material->len,
2378 export_buffer, length );
2379 }
Ronald Cron5425a212020-08-04 14:58:35 +02002380 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002381 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002382 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002383 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002384
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002386
2387exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002388 psa_reset_key_attributes( &source_attributes );
2389 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002390 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002391 mbedtls_free( export_buffer );
2392}
2393/* END_CASE */
2394
2395/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002396void copy_fail( int source_usage_arg,
2397 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002398 int type_arg, data_t *material,
2399 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002400 int target_usage_arg,
2401 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002402 int expected_status_arg )
2403{
2404 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2405 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002406 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2407 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002408
2409 PSA_ASSERT( psa_crypto_init( ) );
2410
2411 /* Prepare the source key. */
2412 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2413 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002414 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002415 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002416 PSA_ASSERT( psa_import_key( &source_attributes,
2417 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002418 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002419
2420 /* Prepare the target attributes. */
2421 psa_set_key_type( &target_attributes, target_type_arg );
2422 psa_set_key_bits( &target_attributes, target_bits_arg );
2423 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2424 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002425 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002426
2427 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002428 TEST_EQUAL( psa_copy_key( source_key,
2429 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002430 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002431
Ronald Cron5425a212020-08-04 14:58:35 +02002432 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002433
Gilles Peskine4a644642019-05-03 17:14:08 +02002434exit:
2435 psa_reset_key_attributes( &source_attributes );
2436 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002437 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002438}
2439/* END_CASE */
2440
2441/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002442void hash_operation_init( )
2443{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002444 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002445 /* Test each valid way of initializing the object, except for `= {0}`, as
2446 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2447 * though it's OK by the C standard. We could test for this, but we'd need
2448 * to supress the Clang warning for the test. */
2449 psa_hash_operation_t func = psa_hash_operation_init( );
2450 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2451 psa_hash_operation_t zero;
2452
2453 memset( &zero, 0, sizeof( zero ) );
2454
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002455 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002456 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2457 PSA_ERROR_BAD_STATE );
2458 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2459 PSA_ERROR_BAD_STATE );
2460 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2461 PSA_ERROR_BAD_STATE );
2462
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002463 /* A default hash operation should be abortable without error. */
2464 PSA_ASSERT( psa_hash_abort( &func ) );
2465 PSA_ASSERT( psa_hash_abort( &init ) );
2466 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002467}
2468/* END_CASE */
2469
2470/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471void hash_setup( int alg_arg,
2472 int expected_status_arg )
2473{
2474 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002475 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002476 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002477 psa_status_t status;
2478
Gilles Peskine8817f612018-12-18 00:18:46 +01002479 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002480
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002481 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002482 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002483
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002484 /* Whether setup succeeded or failed, abort must succeed. */
2485 PSA_ASSERT( psa_hash_abort( &operation ) );
2486
2487 /* If setup failed, reproduce the failure, so as to
2488 * test the resulting state of the operation object. */
2489 if( status != PSA_SUCCESS )
2490 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2491
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002492 /* Now the operation object should be reusable. */
2493#if defined(KNOWN_SUPPORTED_HASH_ALG)
2494 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2495 PSA_ASSERT( psa_hash_abort( &operation ) );
2496#endif
2497
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002498exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002499 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002500}
2501/* END_CASE */
2502
2503/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002504void hash_compute_fail( int alg_arg, data_t *input,
2505 int output_size_arg, int expected_status_arg )
2506{
2507 psa_algorithm_t alg = alg_arg;
2508 uint8_t *output = NULL;
2509 size_t output_size = output_size_arg;
2510 size_t output_length = INVALID_EXPORT_LENGTH;
2511 psa_status_t expected_status = expected_status_arg;
2512 psa_status_t status;
2513
2514 ASSERT_ALLOC( output, output_size );
2515
2516 PSA_ASSERT( psa_crypto_init( ) );
2517
2518 status = psa_hash_compute( alg, input->x, input->len,
2519 output, output_size, &output_length );
2520 TEST_EQUAL( status, expected_status );
2521 TEST_ASSERT( output_length <= output_size );
2522
2523exit:
2524 mbedtls_free( output );
2525 PSA_DONE( );
2526}
2527/* END_CASE */
2528
2529/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002530void hash_compare_fail( int alg_arg, data_t *input,
2531 data_t *reference_hash,
2532 int expected_status_arg )
2533{
2534 psa_algorithm_t alg = alg_arg;
2535 psa_status_t expected_status = expected_status_arg;
2536 psa_status_t status;
2537
2538 PSA_ASSERT( psa_crypto_init( ) );
2539
2540 status = psa_hash_compare( alg, input->x, input->len,
2541 reference_hash->x, reference_hash->len );
2542 TEST_EQUAL( status, expected_status );
2543
2544exit:
2545 PSA_DONE( );
2546}
2547/* END_CASE */
2548
2549/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002550void hash_compute_compare( int alg_arg, data_t *input,
2551 data_t *expected_output )
2552{
2553 psa_algorithm_t alg = alg_arg;
2554 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2555 size_t output_length = INVALID_EXPORT_LENGTH;
2556 size_t i;
2557
2558 PSA_ASSERT( psa_crypto_init( ) );
2559
2560 /* Compute with tight buffer */
2561 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2562 output, PSA_HASH_SIZE( alg ),
2563 &output_length ) );
2564 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2565 ASSERT_COMPARE( output, output_length,
2566 expected_output->x, expected_output->len );
2567
2568 /* Compute with larger buffer */
2569 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2570 output, sizeof( output ),
2571 &output_length ) );
2572 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2573 ASSERT_COMPARE( output, output_length,
2574 expected_output->x, expected_output->len );
2575
2576 /* Compare with correct hash */
2577 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2578 output, output_length ) );
2579
2580 /* Compare with trailing garbage */
2581 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2582 output, output_length + 1 ),
2583 PSA_ERROR_INVALID_SIGNATURE );
2584
2585 /* Compare with truncated hash */
2586 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2587 output, output_length - 1 ),
2588 PSA_ERROR_INVALID_SIGNATURE );
2589
2590 /* Compare with corrupted value */
2591 for( i = 0; i < output_length; i++ )
2592 {
2593 test_set_step( i );
2594 output[i] ^= 1;
2595 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2596 output, output_length ),
2597 PSA_ERROR_INVALID_SIGNATURE );
2598 output[i] ^= 1;
2599 }
2600
2601exit:
2602 PSA_DONE( );
2603}
2604/* END_CASE */
2605
2606/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002607void hash_bad_order( )
2608{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002609 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002610 unsigned char input[] = "";
2611 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002612 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002613 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2614 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2615 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002616 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002617 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002618 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002619
Gilles Peskine8817f612018-12-18 00:18:46 +01002620 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002621
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002622 /* Call setup twice in a row. */
2623 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2624 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2625 PSA_ERROR_BAD_STATE );
2626 PSA_ASSERT( psa_hash_abort( &operation ) );
2627
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002628 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002629 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002630 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002631 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002632
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002633 /* Call update after finish. */
2634 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2635 PSA_ASSERT( psa_hash_finish( &operation,
2636 hash, sizeof( hash ), &hash_len ) );
2637 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002638 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002639 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002640
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002641 /* Call verify without calling setup beforehand. */
2642 TEST_EQUAL( psa_hash_verify( &operation,
2643 valid_hash, sizeof( valid_hash ) ),
2644 PSA_ERROR_BAD_STATE );
2645 PSA_ASSERT( psa_hash_abort( &operation ) );
2646
2647 /* Call verify after finish. */
2648 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2649 PSA_ASSERT( psa_hash_finish( &operation,
2650 hash, sizeof( hash ), &hash_len ) );
2651 TEST_EQUAL( psa_hash_verify( &operation,
2652 valid_hash, sizeof( valid_hash ) ),
2653 PSA_ERROR_BAD_STATE );
2654 PSA_ASSERT( psa_hash_abort( &operation ) );
2655
2656 /* Call verify twice in a row. */
2657 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2658 PSA_ASSERT( psa_hash_verify( &operation,
2659 valid_hash, sizeof( valid_hash ) ) );
2660 TEST_EQUAL( psa_hash_verify( &operation,
2661 valid_hash, sizeof( valid_hash ) ),
2662 PSA_ERROR_BAD_STATE );
2663 PSA_ASSERT( psa_hash_abort( &operation ) );
2664
2665 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002666 TEST_EQUAL( psa_hash_finish( &operation,
2667 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002668 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002669 PSA_ASSERT( psa_hash_abort( &operation ) );
2670
2671 /* Call finish twice in a row. */
2672 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2673 PSA_ASSERT( psa_hash_finish( &operation,
2674 hash, sizeof( hash ), &hash_len ) );
2675 TEST_EQUAL( psa_hash_finish( &operation,
2676 hash, sizeof( hash ), &hash_len ),
2677 PSA_ERROR_BAD_STATE );
2678 PSA_ASSERT( psa_hash_abort( &operation ) );
2679
2680 /* Call finish after calling verify. */
2681 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2682 PSA_ASSERT( psa_hash_verify( &operation,
2683 valid_hash, sizeof( valid_hash ) ) );
2684 TEST_EQUAL( psa_hash_finish( &operation,
2685 hash, sizeof( hash ), &hash_len ),
2686 PSA_ERROR_BAD_STATE );
2687 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002688
2689exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002690 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002691}
2692/* END_CASE */
2693
itayzafrir27e69452018-11-01 14:26:34 +02002694/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2695void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002696{
2697 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002698 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2699 * appended to it */
2700 unsigned char hash[] = {
2701 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2702 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2703 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002704 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002705 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002706
Gilles Peskine8817f612018-12-18 00:18:46 +01002707 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002708
itayzafrir27e69452018-11-01 14:26:34 +02002709 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002710 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002711 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002712 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002713
itayzafrir27e69452018-11-01 14:26:34 +02002714 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002715 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002716 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002717 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002718
itayzafrir27e69452018-11-01 14:26:34 +02002719 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002720 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002721 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002722 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002723
itayzafrirec93d302018-10-18 18:01:10 +03002724exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002725 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002726}
2727/* END_CASE */
2728
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002729/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2730void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002731{
2732 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002733 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002734 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002735 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002736 size_t hash_len;
2737
Gilles Peskine8817f612018-12-18 00:18:46 +01002738 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002739
itayzafrir58028322018-10-25 10:22:01 +03002740 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002741 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002742 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002743 hash, expected_size - 1, &hash_len ),
2744 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002745
2746exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002747 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002748}
2749/* END_CASE */
2750
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002751/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2752void hash_clone_source_state( )
2753{
2754 psa_algorithm_t alg = PSA_ALG_SHA_256;
2755 unsigned char hash[PSA_HASH_MAX_SIZE];
2756 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2757 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2758 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2759 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2760 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2761 size_t hash_len;
2762
2763 PSA_ASSERT( psa_crypto_init( ) );
2764 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2765
2766 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2767 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2768 PSA_ASSERT( psa_hash_finish( &op_finished,
2769 hash, sizeof( hash ), &hash_len ) );
2770 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2771 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2772
2773 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2774 PSA_ERROR_BAD_STATE );
2775
2776 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2777 PSA_ASSERT( psa_hash_finish( &op_init,
2778 hash, sizeof( hash ), &hash_len ) );
2779 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2780 PSA_ASSERT( psa_hash_finish( &op_finished,
2781 hash, sizeof( hash ), &hash_len ) );
2782 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2783 PSA_ASSERT( psa_hash_finish( &op_aborted,
2784 hash, sizeof( hash ), &hash_len ) );
2785
2786exit:
2787 psa_hash_abort( &op_source );
2788 psa_hash_abort( &op_init );
2789 psa_hash_abort( &op_setup );
2790 psa_hash_abort( &op_finished );
2791 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002792 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002793}
2794/* END_CASE */
2795
2796/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2797void hash_clone_target_state( )
2798{
2799 psa_algorithm_t alg = PSA_ALG_SHA_256;
2800 unsigned char hash[PSA_HASH_MAX_SIZE];
2801 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2802 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2803 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2804 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2805 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2806 size_t hash_len;
2807
2808 PSA_ASSERT( psa_crypto_init( ) );
2809
2810 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2811 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2812 PSA_ASSERT( psa_hash_finish( &op_finished,
2813 hash, sizeof( hash ), &hash_len ) );
2814 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2815 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2816
2817 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2818 PSA_ASSERT( psa_hash_finish( &op_target,
2819 hash, sizeof( hash ), &hash_len ) );
2820
2821 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2822 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2823 PSA_ERROR_BAD_STATE );
2824 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2825 PSA_ERROR_BAD_STATE );
2826
2827exit:
2828 psa_hash_abort( &op_target );
2829 psa_hash_abort( &op_init );
2830 psa_hash_abort( &op_setup );
2831 psa_hash_abort( &op_finished );
2832 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002833 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002834}
2835/* END_CASE */
2836
itayzafrir58028322018-10-25 10:22:01 +03002837/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002838void mac_operation_init( )
2839{
Jaeden Amero252ef282019-02-15 14:05:35 +00002840 const uint8_t input[1] = { 0 };
2841
Jaeden Amero769ce272019-01-04 11:48:03 +00002842 /* Test each valid way of initializing the object, except for `= {0}`, as
2843 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2844 * though it's OK by the C standard. We could test for this, but we'd need
2845 * to supress the Clang warning for the test. */
2846 psa_mac_operation_t func = psa_mac_operation_init( );
2847 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2848 psa_mac_operation_t zero;
2849
2850 memset( &zero, 0, sizeof( zero ) );
2851
Jaeden Amero252ef282019-02-15 14:05:35 +00002852 /* A freshly-initialized MAC operation should not be usable. */
2853 TEST_EQUAL( psa_mac_update( &func,
2854 input, sizeof( input ) ),
2855 PSA_ERROR_BAD_STATE );
2856 TEST_EQUAL( psa_mac_update( &init,
2857 input, sizeof( input ) ),
2858 PSA_ERROR_BAD_STATE );
2859 TEST_EQUAL( psa_mac_update( &zero,
2860 input, sizeof( input ) ),
2861 PSA_ERROR_BAD_STATE );
2862
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002863 /* A default MAC operation should be abortable without error. */
2864 PSA_ASSERT( psa_mac_abort( &func ) );
2865 PSA_ASSERT( psa_mac_abort( &init ) );
2866 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002867}
2868/* END_CASE */
2869
2870/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002871void mac_setup( int key_type_arg,
2872 data_t *key,
2873 int alg_arg,
2874 int expected_status_arg )
2875{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002876 psa_key_type_t key_type = key_type_arg;
2877 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002878 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002879 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002880 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2881#if defined(KNOWN_SUPPORTED_MAC_ALG)
2882 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2883#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002884
Gilles Peskine8817f612018-12-18 00:18:46 +01002885 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002886
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002887 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2888 &operation, &status ) )
2889 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002890 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002891
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002892 /* The operation object should be reusable. */
2893#if defined(KNOWN_SUPPORTED_MAC_ALG)
2894 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2895 smoke_test_key_data,
2896 sizeof( smoke_test_key_data ),
2897 KNOWN_SUPPORTED_MAC_ALG,
2898 &operation, &status ) )
2899 goto exit;
2900 TEST_EQUAL( status, PSA_SUCCESS );
2901#endif
2902
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002903exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002904 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002905}
2906/* END_CASE */
2907
2908/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002909void mac_bad_order( )
2910{
Ronald Cron5425a212020-08-04 14:58:35 +02002911 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002912 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2913 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002914 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002915 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2916 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2917 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002918 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002919 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2920 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2921 size_t sign_mac_length = 0;
2922 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2923 const uint8_t verify_mac[] = {
2924 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2925 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2926 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2927
2928 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002929 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002930 psa_set_key_algorithm( &attributes, alg );
2931 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002932
Ronald Cron5425a212020-08-04 14:58:35 +02002933 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2934 &key ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002935
Jaeden Amero252ef282019-02-15 14:05:35 +00002936 /* Call update without calling setup beforehand. */
2937 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2938 PSA_ERROR_BAD_STATE );
2939 PSA_ASSERT( psa_mac_abort( &operation ) );
2940
2941 /* Call sign finish without calling setup beforehand. */
2942 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2943 &sign_mac_length),
2944 PSA_ERROR_BAD_STATE );
2945 PSA_ASSERT( psa_mac_abort( &operation ) );
2946
2947 /* Call verify finish without calling setup beforehand. */
2948 TEST_EQUAL( psa_mac_verify_finish( &operation,
2949 verify_mac, sizeof( verify_mac ) ),
2950 PSA_ERROR_BAD_STATE );
2951 PSA_ASSERT( psa_mac_abort( &operation ) );
2952
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002953 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002954 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2955 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002956 PSA_ERROR_BAD_STATE );
2957 PSA_ASSERT( psa_mac_abort( &operation ) );
2958
Jaeden Amero252ef282019-02-15 14:05:35 +00002959 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002960 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002961 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2962 PSA_ASSERT( psa_mac_sign_finish( &operation,
2963 sign_mac, sizeof( sign_mac ),
2964 &sign_mac_length ) );
2965 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2966 PSA_ERROR_BAD_STATE );
2967 PSA_ASSERT( psa_mac_abort( &operation ) );
2968
2969 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002970 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002971 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2972 PSA_ASSERT( psa_mac_verify_finish( &operation,
2973 verify_mac, sizeof( verify_mac ) ) );
2974 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2975 PSA_ERROR_BAD_STATE );
2976 PSA_ASSERT( psa_mac_abort( &operation ) );
2977
2978 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002979 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002980 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2981 PSA_ASSERT( psa_mac_sign_finish( &operation,
2982 sign_mac, sizeof( sign_mac ),
2983 &sign_mac_length ) );
2984 TEST_EQUAL( psa_mac_sign_finish( &operation,
2985 sign_mac, sizeof( sign_mac ),
2986 &sign_mac_length ),
2987 PSA_ERROR_BAD_STATE );
2988 PSA_ASSERT( psa_mac_abort( &operation ) );
2989
2990 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002991 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002992 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2993 PSA_ASSERT( psa_mac_verify_finish( &operation,
2994 verify_mac, sizeof( verify_mac ) ) );
2995 TEST_EQUAL( psa_mac_verify_finish( &operation,
2996 verify_mac, sizeof( verify_mac ) ),
2997 PSA_ERROR_BAD_STATE );
2998 PSA_ASSERT( psa_mac_abort( &operation ) );
2999
3000 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003001 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003002 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3003 TEST_EQUAL( psa_mac_verify_finish( &operation,
3004 verify_mac, sizeof( verify_mac ) ),
3005 PSA_ERROR_BAD_STATE );
3006 PSA_ASSERT( psa_mac_abort( &operation ) );
3007
3008 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003009 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003010 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3011 TEST_EQUAL( psa_mac_sign_finish( &operation,
3012 sign_mac, sizeof( sign_mac ),
3013 &sign_mac_length ),
3014 PSA_ERROR_BAD_STATE );
3015 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003016
Ronald Cron5425a212020-08-04 14:58:35 +02003017 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003018
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003019exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003020 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003021}
3022/* END_CASE */
3023
3024/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003025void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003026 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003027 int alg_arg,
3028 data_t *input,
3029 data_t *expected_mac )
3030{
Ronald Cron5425a212020-08-04 14:58:35 +02003031 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003032 psa_key_type_t key_type = key_type_arg;
3033 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003034 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003036 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003037 size_t mac_buffer_size =
Ronald Cron5425a212020-08-04 14:58:35 +02003038 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003039 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003040 const size_t output_sizes_to_test[] = {
3041 0,
3042 1,
3043 expected_mac->len - 1,
3044 expected_mac->len,
3045 expected_mac->len + 1,
3046 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003047
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003048 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
Gilles Peskine3d404d62020-08-25 23:47:36 +02003049 /* We expect PSA_MAC_FINAL_SIZE to be exact. */
3050 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003051
Gilles Peskine8817f612018-12-18 00:18:46 +01003052 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003053
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003054 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003055 psa_set_key_algorithm( &attributes, alg );
3056 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003057
Ronald Cron5425a212020-08-04 14:58:35 +02003058 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3059 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003060
Gilles Peskine8b356b52020-08-25 23:44:59 +02003061 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3062 {
3063 const size_t output_size = output_sizes_to_test[i];
3064 psa_status_t expected_status =
3065 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3066 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003067
Gilles Peskine8b356b52020-08-25 23:44:59 +02003068 test_set_step( output_size );
3069 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003070
Gilles Peskine8b356b52020-08-25 23:44:59 +02003071 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003072 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003073 PSA_ASSERT( psa_mac_update( &operation,
3074 input->x, input->len ) );
3075 TEST_EQUAL( psa_mac_sign_finish( &operation,
3076 actual_mac, output_size,
3077 &mac_length ),
3078 expected_status );
3079 PSA_ASSERT( psa_mac_abort( &operation ) );
3080
3081 if( expected_status == PSA_SUCCESS )
3082 {
3083 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3084 actual_mac, mac_length );
3085 }
3086 mbedtls_free( actual_mac );
3087 actual_mac = NULL;
3088 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003089
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003090exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003091 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003092 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003093 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003094 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003095}
3096/* END_CASE */
3097
3098/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003099void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003100 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003101 int alg_arg,
3102 data_t *input,
3103 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003104{
Ronald Cron5425a212020-08-04 14:58:35 +02003105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003106 psa_key_type_t key_type = key_type_arg;
3107 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003108 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003110 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003111
Gilles Peskine69c12672018-06-28 00:07:19 +02003112 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3113
Gilles Peskine8817f612018-12-18 00:18:46 +01003114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003115
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003116 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003117 psa_set_key_algorithm( &attributes, alg );
3118 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003119
Ronald Cron5425a212020-08-04 14:58:35 +02003120 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3121 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003122
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003123 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003124 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003125 PSA_ASSERT( psa_mac_update( &operation,
3126 input->x, input->len ) );
3127 PSA_ASSERT( psa_mac_verify_finish( &operation,
3128 expected_mac->x,
3129 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003130
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003131 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003132 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003133 PSA_ASSERT( psa_mac_update( &operation,
3134 input->x, input->len ) );
3135 TEST_EQUAL( psa_mac_verify_finish( &operation,
3136 expected_mac->x,
3137 expected_mac->len - 1 ),
3138 PSA_ERROR_INVALID_SIGNATURE );
3139
3140 /* Test a MAC that's too long. */
3141 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3142 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003143 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003144 PSA_ASSERT( psa_mac_update( &operation,
3145 input->x, input->len ) );
3146 TEST_EQUAL( psa_mac_verify_finish( &operation,
3147 perturbed_mac,
3148 expected_mac->len + 1 ),
3149 PSA_ERROR_INVALID_SIGNATURE );
3150
3151 /* Test changing one byte. */
3152 for( size_t i = 0; i < expected_mac->len; i++ )
3153 {
3154 test_set_step( i );
3155 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003156 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003157 PSA_ASSERT( psa_mac_update( &operation,
3158 input->x, input->len ) );
3159 TEST_EQUAL( psa_mac_verify_finish( &operation,
3160 perturbed_mac,
3161 expected_mac->len ),
3162 PSA_ERROR_INVALID_SIGNATURE );
3163 perturbed_mac[i] ^= 1;
3164 }
3165
Gilles Peskine8c9def32018-02-08 10:02:12 +01003166exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003167 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003168 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003169 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003170 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003171}
3172/* END_CASE */
3173
3174/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003175void cipher_operation_init( )
3176{
Jaeden Ameroab439972019-02-15 14:12:05 +00003177 const uint8_t input[1] = { 0 };
3178 unsigned char output[1] = { 0 };
3179 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003180 /* Test each valid way of initializing the object, except for `= {0}`, as
3181 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3182 * though it's OK by the C standard. We could test for this, but we'd need
3183 * to supress the Clang warning for the test. */
3184 psa_cipher_operation_t func = psa_cipher_operation_init( );
3185 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3186 psa_cipher_operation_t zero;
3187
3188 memset( &zero, 0, sizeof( zero ) );
3189
Jaeden Ameroab439972019-02-15 14:12:05 +00003190 /* A freshly-initialized cipher operation should not be usable. */
3191 TEST_EQUAL( psa_cipher_update( &func,
3192 input, sizeof( input ),
3193 output, sizeof( output ),
3194 &output_length ),
3195 PSA_ERROR_BAD_STATE );
3196 TEST_EQUAL( psa_cipher_update( &init,
3197 input, sizeof( input ),
3198 output, sizeof( output ),
3199 &output_length ),
3200 PSA_ERROR_BAD_STATE );
3201 TEST_EQUAL( psa_cipher_update( &zero,
3202 input, sizeof( input ),
3203 output, sizeof( output ),
3204 &output_length ),
3205 PSA_ERROR_BAD_STATE );
3206
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003207 /* A default cipher operation should be abortable without error. */
3208 PSA_ASSERT( psa_cipher_abort( &func ) );
3209 PSA_ASSERT( psa_cipher_abort( &init ) );
3210 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003211}
3212/* END_CASE */
3213
3214/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003215void cipher_setup( int key_type_arg,
3216 data_t *key,
3217 int alg_arg,
3218 int expected_status_arg )
3219{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003220 psa_key_type_t key_type = key_type_arg;
3221 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003222 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003223 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003224 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003225#if defined(KNOWN_SUPPORTED_MAC_ALG)
3226 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3227#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003228
Gilles Peskine8817f612018-12-18 00:18:46 +01003229 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003230
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003231 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3232 &operation, &status ) )
3233 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003234 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003235
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003236 /* The operation object should be reusable. */
3237#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3238 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3239 smoke_test_key_data,
3240 sizeof( smoke_test_key_data ),
3241 KNOWN_SUPPORTED_CIPHER_ALG,
3242 &operation, &status ) )
3243 goto exit;
3244 TEST_EQUAL( status, PSA_SUCCESS );
3245#endif
3246
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003247exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003248 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003249 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003250}
3251/* END_CASE */
3252
3253/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003254void cipher_bad_order( )
3255{
Ronald Cron5425a212020-08-04 14:58:35 +02003256 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003257 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3258 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003259 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003260 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3261 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003262 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003263 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3264 0xaa, 0xaa, 0xaa, 0xaa };
3265 const uint8_t text[] = {
3266 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3267 0xbb, 0xbb, 0xbb, 0xbb };
3268 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3269 size_t length = 0;
3270
3271 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003272 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3273 psa_set_key_algorithm( &attributes, alg );
3274 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003275 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3276 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003277
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003278 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003279 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3280 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003281 PSA_ERROR_BAD_STATE );
3282 PSA_ASSERT( psa_cipher_abort( &operation ) );
3283
3284 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003285 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3286 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003287 PSA_ERROR_BAD_STATE );
3288 PSA_ASSERT( psa_cipher_abort( &operation ) );
3289
Jaeden Ameroab439972019-02-15 14:12:05 +00003290 /* Generate an IV without calling setup beforehand. */
3291 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3292 buffer, sizeof( buffer ),
3293 &length ),
3294 PSA_ERROR_BAD_STATE );
3295 PSA_ASSERT( psa_cipher_abort( &operation ) );
3296
3297 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003298 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003299 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3300 buffer, sizeof( buffer ),
3301 &length ) );
3302 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3303 buffer, sizeof( buffer ),
3304 &length ),
3305 PSA_ERROR_BAD_STATE );
3306 PSA_ASSERT( psa_cipher_abort( &operation ) );
3307
3308 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003309 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003310 PSA_ASSERT( psa_cipher_set_iv( &operation,
3311 iv, sizeof( iv ) ) );
3312 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3313 buffer, sizeof( buffer ),
3314 &length ),
3315 PSA_ERROR_BAD_STATE );
3316 PSA_ASSERT( psa_cipher_abort( &operation ) );
3317
3318 /* Set an IV without calling setup beforehand. */
3319 TEST_EQUAL( psa_cipher_set_iv( &operation,
3320 iv, sizeof( iv ) ),
3321 PSA_ERROR_BAD_STATE );
3322 PSA_ASSERT( psa_cipher_abort( &operation ) );
3323
3324 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003325 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003326 PSA_ASSERT( psa_cipher_set_iv( &operation,
3327 iv, sizeof( iv ) ) );
3328 TEST_EQUAL( psa_cipher_set_iv( &operation,
3329 iv, sizeof( iv ) ),
3330 PSA_ERROR_BAD_STATE );
3331 PSA_ASSERT( psa_cipher_abort( &operation ) );
3332
3333 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003334 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003335 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3336 buffer, sizeof( buffer ),
3337 &length ) );
3338 TEST_EQUAL( psa_cipher_set_iv( &operation,
3339 iv, sizeof( iv ) ),
3340 PSA_ERROR_BAD_STATE );
3341 PSA_ASSERT( psa_cipher_abort( &operation ) );
3342
3343 /* Call update without calling setup beforehand. */
3344 TEST_EQUAL( psa_cipher_update( &operation,
3345 text, sizeof( text ),
3346 buffer, sizeof( buffer ),
3347 &length ),
3348 PSA_ERROR_BAD_STATE );
3349 PSA_ASSERT( psa_cipher_abort( &operation ) );
3350
3351 /* Call update without an IV where an IV is required. */
3352 TEST_EQUAL( psa_cipher_update( &operation,
3353 text, sizeof( text ),
3354 buffer, sizeof( buffer ),
3355 &length ),
3356 PSA_ERROR_BAD_STATE );
3357 PSA_ASSERT( psa_cipher_abort( &operation ) );
3358
3359 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003360 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003361 PSA_ASSERT( psa_cipher_set_iv( &operation,
3362 iv, sizeof( iv ) ) );
3363 PSA_ASSERT( psa_cipher_finish( &operation,
3364 buffer, sizeof( buffer ), &length ) );
3365 TEST_EQUAL( psa_cipher_update( &operation,
3366 text, sizeof( text ),
3367 buffer, sizeof( buffer ),
3368 &length ),
3369 PSA_ERROR_BAD_STATE );
3370 PSA_ASSERT( psa_cipher_abort( &operation ) );
3371
3372 /* Call finish without calling setup beforehand. */
3373 TEST_EQUAL( psa_cipher_finish( &operation,
3374 buffer, sizeof( buffer ), &length ),
3375 PSA_ERROR_BAD_STATE );
3376 PSA_ASSERT( psa_cipher_abort( &operation ) );
3377
3378 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003379 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003380 /* Not calling update means we are encrypting an empty buffer, which is OK
3381 * for cipher modes with padding. */
3382 TEST_EQUAL( psa_cipher_finish( &operation,
3383 buffer, sizeof( buffer ), &length ),
3384 PSA_ERROR_BAD_STATE );
3385 PSA_ASSERT( psa_cipher_abort( &operation ) );
3386
3387 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003388 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003389 PSA_ASSERT( psa_cipher_set_iv( &operation,
3390 iv, sizeof( iv ) ) );
3391 PSA_ASSERT( psa_cipher_finish( &operation,
3392 buffer, sizeof( buffer ), &length ) );
3393 TEST_EQUAL( psa_cipher_finish( &operation,
3394 buffer, sizeof( buffer ), &length ),
3395 PSA_ERROR_BAD_STATE );
3396 PSA_ASSERT( psa_cipher_abort( &operation ) );
3397
Ronald Cron5425a212020-08-04 14:58:35 +02003398 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003399
Jaeden Ameroab439972019-02-15 14:12:05 +00003400exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003401 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003402 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003403}
3404/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003405
Gilles Peskine50e586b2018-06-08 14:28:46 +02003406/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003407void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003408 data_t *key_data, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003409 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003410 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003411{
Ronald Cron5425a212020-08-04 14:58:35 +02003412 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003413 psa_status_t status;
3414 psa_key_type_t key_type = key_type_arg;
3415 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003416 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003417 unsigned char *output = NULL;
3418 size_t output_buffer_size = 0;
3419 size_t function_output_length = 0;
3420 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003421 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003423
Gilles Peskine8817f612018-12-18 00:18:46 +01003424 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003425
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003426 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3427 psa_set_key_algorithm( &attributes, alg );
3428 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003429
Ronald Cron5425a212020-08-04 14:58:35 +02003430 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3431 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003432
Ronald Cron5425a212020-08-04 14:58:35 +02003433 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003434
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003435 if( iv->len > 0 )
3436 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003437 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003438 }
3439
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003440 output_buffer_size = ( (size_t) input->len +
3441 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003442 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003443
Gilles Peskine8817f612018-12-18 00:18:46 +01003444 PSA_ASSERT( psa_cipher_update( &operation,
3445 input->x, input->len,
3446 output, output_buffer_size,
3447 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003448 total_output_length += function_output_length;
3449 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003450 output + total_output_length,
3451 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003452 &function_output_length );
3453 total_output_length += function_output_length;
3454
Gilles Peskinefe11b722018-12-18 00:24:04 +01003455 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003456 if( expected_status == PSA_SUCCESS )
3457 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003458 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003459 ASSERT_COMPARE( expected_output->x, expected_output->len,
3460 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003461 }
3462
3463exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003464 psa_cipher_abort( &operation );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003465 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003466 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003467 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003468}
3469/* END_CASE */
3470
3471/* BEGIN_CASE */
3472void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003473 data_t *key_data, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003474 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003475 int first_part_size_arg,
3476 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003477 data_t *expected_output )
3478{
Ronald Cron5425a212020-08-04 14:58:35 +02003479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003480 psa_key_type_t key_type = key_type_arg;
3481 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003482 size_t first_part_size = first_part_size_arg;
3483 size_t output1_length = output1_length_arg;
3484 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003485 unsigned char *output = NULL;
3486 size_t output_buffer_size = 0;
3487 size_t function_output_length = 0;
3488 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003489 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003491
Gilles Peskine8817f612018-12-18 00:18:46 +01003492 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003493
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003494 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3495 psa_set_key_algorithm( &attributes, alg );
3496 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003497
Ronald Cron5425a212020-08-04 14:58:35 +02003498 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3499 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003500
Ronald Cron5425a212020-08-04 14:58:35 +02003501 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003502
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003503 if( iv->len > 0 )
3504 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003505 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003506 }
3507
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003508 output_buffer_size = ( (size_t) input->len +
3509 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003510 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003511
Gilles Peskinee0866522019-02-19 19:44:00 +01003512 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003513 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3514 output, output_buffer_size,
3515 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003516 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003517 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003518 PSA_ASSERT( psa_cipher_update( &operation,
3519 input->x + first_part_size,
3520 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003521 output + total_output_length,
3522 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003523 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003524 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003525 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003526 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003527 output + total_output_length,
3528 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003529 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003530 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003531 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003532
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003533 ASSERT_COMPARE( expected_output->x, expected_output->len,
3534 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003535
3536exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003537 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003538 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003539 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003540 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003541}
3542/* END_CASE */
3543
3544/* BEGIN_CASE */
3545void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003546 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003547 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003548 int first_part_size_arg,
3549 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003550 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003551{
Ronald Cron5425a212020-08-04 14:58:35 +02003552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003553 psa_key_type_t key_type = key_type_arg;
3554 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003555 size_t first_part_size = first_part_size_arg;
3556 size_t output1_length = output1_length_arg;
3557 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003558 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003559 size_t output_buffer_size = 0;
3560 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003561 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003562 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003564
Gilles Peskine8817f612018-12-18 00:18:46 +01003565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003566
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003567 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3568 psa_set_key_algorithm( &attributes, alg );
3569 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003570
Ronald Cron5425a212020-08-04 14:58:35 +02003571 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3572 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003573
Ronald Cron5425a212020-08-04 14:58:35 +02003574 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003575
Steven Cooreman177deba2020-09-07 17:14:14 +02003576 if( iv->len > 0 )
3577 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003578 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003579 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003580
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003581 output_buffer_size = ( (size_t) input->len +
3582 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003583 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003584
Gilles Peskinee0866522019-02-19 19:44:00 +01003585 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003586 PSA_ASSERT( psa_cipher_update( &operation,
3587 input->x, first_part_size,
3588 output, output_buffer_size,
3589 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003590 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003591 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003592 PSA_ASSERT( psa_cipher_update( &operation,
3593 input->x + first_part_size,
3594 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003595 output + total_output_length,
3596 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003597 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003598 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003599 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003600 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003601 output + total_output_length,
3602 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003603 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003604 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003606
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003607 ASSERT_COMPARE( expected_output->x, expected_output->len,
3608 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003609
3610exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003611 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003612 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003613 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003614 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003615}
3616/* END_CASE */
3617
Gilles Peskine50e586b2018-06-08 14:28:46 +02003618/* BEGIN_CASE */
3619void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003620 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003621 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003622 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003623{
Ronald Cron5425a212020-08-04 14:58:35 +02003624 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003625 psa_status_t status;
3626 psa_key_type_t key_type = key_type_arg;
3627 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003628 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003629 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003630 size_t output_buffer_size = 0;
3631 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003632 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003633 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003635
Gilles Peskine8817f612018-12-18 00:18:46 +01003636 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003637
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003638 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3639 psa_set_key_algorithm( &attributes, alg );
3640 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003641
Ronald Cron5425a212020-08-04 14:58:35 +02003642 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3643 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003644
Ronald Cron5425a212020-08-04 14:58:35 +02003645 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003646
Steven Cooreman177deba2020-09-07 17:14:14 +02003647 if( iv->len > 0 )
3648 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003649 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003650 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003651
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003652 output_buffer_size = ( (size_t) input->len +
3653 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003654 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003655
Gilles Peskine8817f612018-12-18 00:18:46 +01003656 PSA_ASSERT( psa_cipher_update( &operation,
3657 input->x, input->len,
3658 output, output_buffer_size,
3659 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003660 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003661 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003662 output + total_output_length,
3663 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003664 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003665 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003666 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003667
3668 if( expected_status == PSA_SUCCESS )
3669 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003670 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003671 ASSERT_COMPARE( expected_output->x, expected_output->len,
3672 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003673 }
3674
Gilles Peskine50e586b2018-06-08 14:28:46 +02003675exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003676 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003677 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003678 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003679 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003680}
3681/* END_CASE */
3682
Gilles Peskine50e586b2018-06-08 14:28:46 +02003683/* BEGIN_CASE */
3684void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003685 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003686 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003687{
Ronald Cron5425a212020-08-04 14:58:35 +02003688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003689 psa_key_type_t key_type = key_type_arg;
3690 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003691 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003692 size_t iv_size = 16;
3693 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003694 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003695 size_t output1_size = 0;
3696 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003697 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003698 size_t output2_size = 0;
3699 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003700 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003701 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3702 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003704
Gilles Peskine8817f612018-12-18 00:18:46 +01003705 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003706
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003707 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3708 psa_set_key_algorithm( &attributes, alg );
3709 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003710
Ronald Cron5425a212020-08-04 14:58:35 +02003711 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3712 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003713
Ronald Cron5425a212020-08-04 14:58:35 +02003714 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3715 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003716
Steven Cooreman177deba2020-09-07 17:14:14 +02003717 if( alg != PSA_ALG_ECB_NO_PADDING )
3718 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003719 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3720 iv, iv_size,
3721 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003722 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003723 output1_size = ( (size_t) input->len +
3724 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003725 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003726
Gilles Peskine8817f612018-12-18 00:18:46 +01003727 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3728 output1, output1_size,
3729 &output1_length ) );
3730 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003731 output1 + output1_length,
3732 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003733 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003734
Gilles Peskine048b7f02018-06-08 14:20:49 +02003735 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003736
Gilles Peskine8817f612018-12-18 00:18:46 +01003737 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003738
3739 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003740 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003741
Steven Cooreman177deba2020-09-07 17:14:14 +02003742 if( iv_length > 0 )
3743 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003744 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3745 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003746 }
3747
Gilles Peskine8817f612018-12-18 00:18:46 +01003748 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3749 output2, output2_size,
3750 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003751 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003752 PSA_ASSERT( psa_cipher_finish( &operation2,
3753 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003754 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003755 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003756
Gilles Peskine048b7f02018-06-08 14:20:49 +02003757 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003758
Gilles Peskine8817f612018-12-18 00:18:46 +01003759 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003760
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003761 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003762
3763exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003764 psa_cipher_abort( &operation1 );
3765 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003766 mbedtls_free( output1 );
3767 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003768 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003769 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003770}
3771/* END_CASE */
3772
3773/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003774void cipher_verify_output_multipart( int alg_arg,
3775 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003776 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003777 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003778 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003779{
Ronald Cron5425a212020-08-04 14:58:35 +02003780 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003781 psa_key_type_t key_type = key_type_arg;
3782 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003783 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003784 unsigned char iv[16] = {0};
3785 size_t iv_size = 16;
3786 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003787 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003788 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003789 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003790 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003791 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003792 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003793 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003794 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3795 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003797
Gilles Peskine8817f612018-12-18 00:18:46 +01003798 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003799
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003800 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3801 psa_set_key_algorithm( &attributes, alg );
3802 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003803
Ronald Cron5425a212020-08-04 14:58:35 +02003804 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3805 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003806
Ronald Cron5425a212020-08-04 14:58:35 +02003807 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3808 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003809
Steven Cooreman177deba2020-09-07 17:14:14 +02003810 if( alg != PSA_ALG_ECB_NO_PADDING )
3811 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003812 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3813 iv, iv_size,
3814 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003815 }
3816
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003817 output1_buffer_size = ( (size_t) input->len +
3818 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003819 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003820
Gilles Peskinee0866522019-02-19 19:44:00 +01003821 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003822
Gilles Peskine8817f612018-12-18 00:18:46 +01003823 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3824 output1, output1_buffer_size,
3825 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003826 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003827
Gilles Peskine8817f612018-12-18 00:18:46 +01003828 PSA_ASSERT( psa_cipher_update( &operation1,
3829 input->x + first_part_size,
3830 input->len - first_part_size,
3831 output1, output1_buffer_size,
3832 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003833 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003834
Gilles Peskine8817f612018-12-18 00:18:46 +01003835 PSA_ASSERT( psa_cipher_finish( &operation1,
3836 output1 + output1_length,
3837 output1_buffer_size - output1_length,
3838 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003839 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003840
Gilles Peskine8817f612018-12-18 00:18:46 +01003841 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003842
Gilles Peskine048b7f02018-06-08 14:20:49 +02003843 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003844 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003845
Steven Cooreman177deba2020-09-07 17:14:14 +02003846 if( iv_length > 0 )
3847 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003848 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3849 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003850 }
Moran Pekerded84402018-06-06 16:36:50 +03003851
Gilles Peskine8817f612018-12-18 00:18:46 +01003852 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3853 output2, output2_buffer_size,
3854 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003855 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003856
Gilles Peskine8817f612018-12-18 00:18:46 +01003857 PSA_ASSERT( psa_cipher_update( &operation2,
3858 output1 + first_part_size,
3859 output1_length - first_part_size,
3860 output2, output2_buffer_size,
3861 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003862 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003863
Gilles Peskine8817f612018-12-18 00:18:46 +01003864 PSA_ASSERT( psa_cipher_finish( &operation2,
3865 output2 + output2_length,
3866 output2_buffer_size - output2_length,
3867 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003868 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003869
Gilles Peskine8817f612018-12-18 00:18:46 +01003870 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003871
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003872 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003873
3874exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003875 psa_cipher_abort( &operation1 );
3876 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003877 mbedtls_free( output1 );
3878 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003879 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003880 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003881}
3882/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003883
Gilles Peskine20035e32018-02-03 22:44:14 +01003884/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003885void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003886 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003887 data_t *nonce,
3888 data_t *additional_data,
3889 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003890 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003891{
Ronald Cron5425a212020-08-04 14:58:35 +02003892 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003893 psa_key_type_t key_type = key_type_arg;
3894 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003895 unsigned char *output_data = NULL;
3896 size_t output_size = 0;
3897 size_t output_length = 0;
3898 unsigned char *output_data2 = NULL;
3899 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003900 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003901 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003902 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003903
Gilles Peskine4abf7412018-06-18 16:35:34 +02003904 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003905 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3906 * should be exact. */
3907 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3908 TEST_EQUAL( output_size,
3909 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003910 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003911
Gilles Peskine8817f612018-12-18 00:18:46 +01003912 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003913
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003914 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3915 psa_set_key_algorithm( &attributes, alg );
3916 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003917
Gilles Peskine049c7532019-05-15 20:22:09 +02003918 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003919 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003920
Ronald Cron5425a212020-08-04 14:58:35 +02003921 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003922 nonce->x, nonce->len,
3923 additional_data->x,
3924 additional_data->len,
3925 input_data->x, input_data->len,
3926 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003927 &output_length ),
3928 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003929
3930 if( PSA_SUCCESS == expected_result )
3931 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003932 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003933
Gilles Peskine003a4a92019-05-14 16:09:40 +02003934 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3935 * should be exact. */
3936 TEST_EQUAL( input_data->len,
3937 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3938
Ronald Cron5425a212020-08-04 14:58:35 +02003939 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003940 nonce->x, nonce->len,
3941 additional_data->x,
3942 additional_data->len,
3943 output_data, output_length,
3944 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003945 &output_length2 ),
3946 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003947
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003948 ASSERT_COMPARE( input_data->x, input_data->len,
3949 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003950 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003951
Gilles Peskinea1cac842018-06-11 19:33:02 +02003952exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003953 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003954 mbedtls_free( output_data );
3955 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003956 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003957}
3958/* END_CASE */
3959
3960/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003961void aead_encrypt( int key_type_arg, data_t *key_data,
3962 int alg_arg,
3963 data_t *nonce,
3964 data_t *additional_data,
3965 data_t *input_data,
3966 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003967{
Ronald Cron5425a212020-08-04 14:58:35 +02003968 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003969 psa_key_type_t key_type = key_type_arg;
3970 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003971 unsigned char *output_data = NULL;
3972 size_t output_size = 0;
3973 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003974 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003975 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003976
Gilles Peskine4abf7412018-06-18 16:35:34 +02003977 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003978 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3979 * should be exact. */
3980 TEST_EQUAL( output_size,
3981 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003982 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003983
Gilles Peskine8817f612018-12-18 00:18:46 +01003984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003985
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003986 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3987 psa_set_key_algorithm( &attributes, alg );
3988 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003989
Gilles Peskine049c7532019-05-15 20:22:09 +02003990 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003991 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003992
Ronald Cron5425a212020-08-04 14:58:35 +02003993 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003994 nonce->x, nonce->len,
3995 additional_data->x, additional_data->len,
3996 input_data->x, input_data->len,
3997 output_data, output_size,
3998 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003999
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004000 ASSERT_COMPARE( expected_result->x, expected_result->len,
4001 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004002
Gilles Peskinea1cac842018-06-11 19:33:02 +02004003exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004004 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004005 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004006 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004007}
4008/* END_CASE */
4009
4010/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004011void aead_decrypt( int key_type_arg, data_t *key_data,
4012 int alg_arg,
4013 data_t *nonce,
4014 data_t *additional_data,
4015 data_t *input_data,
4016 data_t *expected_data,
4017 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004018{
Ronald Cron5425a212020-08-04 14:58:35 +02004019 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004020 psa_key_type_t key_type = key_type_arg;
4021 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004022 unsigned char *output_data = NULL;
4023 size_t output_size = 0;
4024 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004025 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004027 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004028
Gilles Peskine003a4a92019-05-14 16:09:40 +02004029 output_size = input_data->len - tag_length;
4030 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4031 * should be exact. */
4032 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4033 TEST_EQUAL( output_size,
4034 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004035 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004036
Gilles Peskine8817f612018-12-18 00:18:46 +01004037 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004038
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004039 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4040 psa_set_key_algorithm( &attributes, alg );
4041 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004042
Gilles Peskine049c7532019-05-15 20:22:09 +02004043 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004044 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004045
Ronald Cron5425a212020-08-04 14:58:35 +02004046 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004047 nonce->x, nonce->len,
4048 additional_data->x,
4049 additional_data->len,
4050 input_data->x, input_data->len,
4051 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004052 &output_length ),
4053 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004054
Gilles Peskine2d277862018-06-18 15:41:12 +02004055 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004056 ASSERT_COMPARE( expected_data->x, expected_data->len,
4057 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004058
Gilles Peskinea1cac842018-06-11 19:33:02 +02004059exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004060 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004061 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004062 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004063}
4064/* END_CASE */
4065
4066/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004067void signature_size( int type_arg,
4068 int bits,
4069 int alg_arg,
4070 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004071{
4072 psa_key_type_t type = type_arg;
4073 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004074 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004075
Gilles Peskinefe11b722018-12-18 00:24:04 +01004076 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004077#if defined(MBEDTLS_TEST_DEPRECATED)
4078 TEST_EQUAL( actual_size,
4079 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4080#endif /* MBEDTLS_TEST_DEPRECATED */
4081
Gilles Peskinee59236f2018-01-27 23:32:46 +01004082exit:
4083 ;
4084}
4085/* END_CASE */
4086
4087/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004088void sign_deterministic( int key_type_arg, data_t *key_data,
4089 int alg_arg, data_t *input_data,
4090 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004091{
Ronald Cron5425a212020-08-04 14:58:35 +02004092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004093 psa_key_type_t key_type = key_type_arg;
4094 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004095 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004096 unsigned char *signature = NULL;
4097 size_t signature_size;
4098 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004099 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004100
Gilles Peskine8817f612018-12-18 00:18:46 +01004101 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004102
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004103 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004104 psa_set_key_algorithm( &attributes, alg );
4105 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004106
Gilles Peskine049c7532019-05-15 20:22:09 +02004107 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004108 &key ) );
4109 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004110 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004111
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004112 /* Allocate a buffer which has the size advertized by the
4113 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004114 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004115 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004116 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004117 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004118 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004119
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004120 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004121 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004122 input_data->x, input_data->len,
4123 signature, signature_size,
4124 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004125 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004126 ASSERT_COMPARE( output_data->x, output_data->len,
4127 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004128
Gilles Peskine0627f982019-11-26 19:12:16 +01004129#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004130 memset( signature, 0, signature_size );
4131 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004132 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004133 input_data->x, input_data->len,
4134 signature, signature_size,
4135 &signature_length ) );
4136 ASSERT_COMPARE( output_data->x, output_data->len,
4137 signature, signature_length );
4138#endif /* MBEDTLS_TEST_DEPRECATED */
4139
Gilles Peskine20035e32018-02-03 22:44:14 +01004140exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004141 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004142 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004143 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004144 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004145}
4146/* END_CASE */
4147
4148/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004149void sign_fail( int key_type_arg, data_t *key_data,
4150 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004151 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004152{
Ronald Cron5425a212020-08-04 14:58:35 +02004153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004154 psa_key_type_t key_type = key_type_arg;
4155 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004156 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004157 psa_status_t actual_status;
4158 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004159 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004160 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004161 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004162
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004163 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004164
Gilles Peskine8817f612018-12-18 00:18:46 +01004165 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004166
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004167 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004168 psa_set_key_algorithm( &attributes, alg );
4169 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004170
Gilles Peskine049c7532019-05-15 20:22:09 +02004171 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004172 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004173
Ronald Cron5425a212020-08-04 14:58:35 +02004174 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004175 input_data->x, input_data->len,
4176 signature, signature_size,
4177 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004178 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004179 /* The value of *signature_length is unspecified on error, but
4180 * whatever it is, it should be less than signature_size, so that
4181 * if the caller tries to read *signature_length bytes without
4182 * checking the error code then they don't overflow a buffer. */
4183 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004184
Gilles Peskine895242b2019-11-29 12:15:40 +01004185#if defined(MBEDTLS_TEST_DEPRECATED)
4186 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004187 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004188 input_data->x, input_data->len,
4189 signature, signature_size,
4190 &signature_length ),
4191 expected_status );
4192 TEST_ASSERT( signature_length <= signature_size );
4193#endif /* MBEDTLS_TEST_DEPRECATED */
4194
Gilles Peskine20035e32018-02-03 22:44:14 +01004195exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004196 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004197 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004198 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004199 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004200}
4201/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004202
4203/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004204void sign_verify( int key_type_arg, data_t *key_data,
4205 int alg_arg, data_t *input_data )
4206{
Ronald Cron5425a212020-08-04 14:58:35 +02004207 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004208 psa_key_type_t key_type = key_type_arg;
4209 psa_algorithm_t alg = alg_arg;
4210 size_t key_bits;
4211 unsigned char *signature = NULL;
4212 size_t signature_size;
4213 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004215
Gilles Peskine8817f612018-12-18 00:18:46 +01004216 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004217
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004218 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004219 psa_set_key_algorithm( &attributes, alg );
4220 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004221
Gilles Peskine049c7532019-05-15 20:22:09 +02004222 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004223 &key ) );
4224 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004225 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004226
4227 /* Allocate a buffer which has the size advertized by the
4228 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004229 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004230 key_bits, alg );
4231 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004232 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004233 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004234
4235 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004236 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004237 input_data->x, input_data->len,
4238 signature, signature_size,
4239 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004240 /* Check that the signature length looks sensible. */
4241 TEST_ASSERT( signature_length <= signature_size );
4242 TEST_ASSERT( signature_length > 0 );
4243
4244 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004245 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004246 input_data->x, input_data->len,
4247 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004248
4249 if( input_data->len != 0 )
4250 {
4251 /* Flip a bit in the input and verify that the signature is now
4252 * detected as invalid. Flip a bit at the beginning, not at the end,
4253 * because ECDSA may ignore the last few bits of the input. */
4254 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004255 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004256 input_data->x, input_data->len,
4257 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004258 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004259 }
4260
4261exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004262 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004263 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004264 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004265 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004266}
4267/* END_CASE */
4268
4269/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004270void asymmetric_verify( int key_type_arg, data_t *key_data,
4271 int alg_arg, data_t *hash_data,
4272 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004273{
Ronald Cron5425a212020-08-04 14:58:35 +02004274 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004275 psa_key_type_t key_type = key_type_arg;
4276 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004278
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004279 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004280
Gilles Peskine8817f612018-12-18 00:18:46 +01004281 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004282
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004283 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004284 psa_set_key_algorithm( &attributes, alg );
4285 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004286
Gilles Peskine049c7532019-05-15 20:22:09 +02004287 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004288 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004289
Ronald Cron5425a212020-08-04 14:58:35 +02004290 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004291 hash_data->x, hash_data->len,
4292 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004293
4294#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004295 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004296 hash_data->x, hash_data->len,
4297 signature_data->x,
4298 signature_data->len ) );
4299
4300#endif /* MBEDTLS_TEST_DEPRECATED */
4301
itayzafrir5c753392018-05-08 11:18:38 +03004302exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004303 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004304 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004305 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004306}
4307/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004308
4309/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004310void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4311 int alg_arg, data_t *hash_data,
4312 data_t *signature_data,
4313 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004314{
Ronald Cron5425a212020-08-04 14:58:35 +02004315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004316 psa_key_type_t key_type = key_type_arg;
4317 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004318 psa_status_t actual_status;
4319 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004320 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004321
Gilles Peskine8817f612018-12-18 00:18:46 +01004322 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004323
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004324 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004325 psa_set_key_algorithm( &attributes, alg );
4326 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004327
Gilles Peskine049c7532019-05-15 20:22:09 +02004328 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004329 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004330
Ronald Cron5425a212020-08-04 14:58:35 +02004331 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004332 hash_data->x, hash_data->len,
4333 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004334 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004335
Gilles Peskine895242b2019-11-29 12:15:40 +01004336#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004337 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004338 hash_data->x, hash_data->len,
4339 signature_data->x, signature_data->len ),
4340 expected_status );
4341#endif /* MBEDTLS_TEST_DEPRECATED */
4342
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004343exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004344 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004345 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004346 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004347}
4348/* END_CASE */
4349
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004350/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004351void asymmetric_encrypt( int key_type_arg,
4352 data_t *key_data,
4353 int alg_arg,
4354 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004355 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004356 int expected_output_length_arg,
4357 int expected_status_arg )
4358{
Ronald Cron5425a212020-08-04 14:58:35 +02004359 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004360 psa_key_type_t key_type = key_type_arg;
4361 psa_algorithm_t alg = alg_arg;
4362 size_t expected_output_length = expected_output_length_arg;
4363 size_t key_bits;
4364 unsigned char *output = NULL;
4365 size_t output_size;
4366 size_t output_length = ~0;
4367 psa_status_t actual_status;
4368 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004370
Gilles Peskine8817f612018-12-18 00:18:46 +01004371 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004372
Gilles Peskine656896e2018-06-29 19:12:28 +02004373 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004374 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4375 psa_set_key_algorithm( &attributes, alg );
4376 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004377 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004378 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004379
4380 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004381 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004382 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004383 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004384 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004385
4386 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004387 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004388 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004389 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004390 output, output_size,
4391 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004392 TEST_EQUAL( actual_status, expected_status );
4393 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004394
Gilles Peskine68428122018-06-30 18:42:41 +02004395 /* If the label is empty, the test framework puts a non-null pointer
4396 * in label->x. Test that a null pointer works as well. */
4397 if( label->len == 0 )
4398 {
4399 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004400 if( output_size != 0 )
4401 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004402 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004403 input_data->x, input_data->len,
4404 NULL, label->len,
4405 output, output_size,
4406 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004407 TEST_EQUAL( actual_status, expected_status );
4408 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004409 }
4410
Gilles Peskine656896e2018-06-29 19:12:28 +02004411exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004412 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004413 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004414 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004415 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004416}
4417/* END_CASE */
4418
4419/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004420void asymmetric_encrypt_decrypt( int key_type_arg,
4421 data_t *key_data,
4422 int alg_arg,
4423 data_t *input_data,
4424 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004425{
Ronald Cron5425a212020-08-04 14:58:35 +02004426 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004427 psa_key_type_t key_type = key_type_arg;
4428 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004429 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004430 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004431 size_t output_size;
4432 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004433 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004434 size_t output2_size;
4435 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004437
Gilles Peskine8817f612018-12-18 00:18:46 +01004438 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004439
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004440 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4441 psa_set_key_algorithm( &attributes, alg );
4442 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004443
Gilles Peskine049c7532019-05-15 20:22:09 +02004444 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004445 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004446
4447 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004448 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004449 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004450 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004451 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004452 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004453 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004454
Gilles Peskineeebd7382018-06-08 18:11:54 +02004455 /* We test encryption by checking that encrypt-then-decrypt gives back
4456 * the original plaintext because of the non-optional random
4457 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004458 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004459 input_data->x, input_data->len,
4460 label->x, label->len,
4461 output, output_size,
4462 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004463 /* We don't know what ciphertext length to expect, but check that
4464 * it looks sensible. */
4465 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004466
Ronald Cron5425a212020-08-04 14:58:35 +02004467 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004468 output, output_length,
4469 label->x, label->len,
4470 output2, output2_size,
4471 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004472 ASSERT_COMPARE( input_data->x, input_data->len,
4473 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004474
4475exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004476 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004477 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004478 mbedtls_free( output );
4479 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004480 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004481}
4482/* END_CASE */
4483
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004484/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004485void asymmetric_decrypt( int key_type_arg,
4486 data_t *key_data,
4487 int alg_arg,
4488 data_t *input_data,
4489 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004490 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004491{
Ronald Cron5425a212020-08-04 14:58:35 +02004492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004493 psa_key_type_t key_type = key_type_arg;
4494 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004495 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004496 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004497 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004499
Jaeden Amero412654a2019-02-06 12:57:46 +00004500 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004501 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004502
Gilles Peskine8817f612018-12-18 00:18:46 +01004503 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004504
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004505 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4506 psa_set_key_algorithm( &attributes, alg );
4507 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004508
Gilles Peskine049c7532019-05-15 20:22:09 +02004509 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004510 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004511
Ronald Cron5425a212020-08-04 14:58:35 +02004512 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004513 input_data->x, input_data->len,
4514 label->x, label->len,
4515 output,
4516 output_size,
4517 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004518 ASSERT_COMPARE( expected_data->x, expected_data->len,
4519 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004520
Gilles Peskine68428122018-06-30 18:42:41 +02004521 /* If the label is empty, the test framework puts a non-null pointer
4522 * in label->x. Test that a null pointer works as well. */
4523 if( label->len == 0 )
4524 {
4525 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004526 if( output_size != 0 )
4527 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004528 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004529 input_data->x, input_data->len,
4530 NULL, label->len,
4531 output,
4532 output_size,
4533 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004534 ASSERT_COMPARE( expected_data->x, expected_data->len,
4535 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004536 }
4537
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004538exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004539 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004540 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004541 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004542 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004543}
4544/* END_CASE */
4545
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004546/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004547void asymmetric_decrypt_fail( int key_type_arg,
4548 data_t *key_data,
4549 int alg_arg,
4550 data_t *input_data,
4551 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004552 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004553 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004554{
Ronald Cron5425a212020-08-04 14:58:35 +02004555 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004556 psa_key_type_t key_type = key_type_arg;
4557 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004558 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004559 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004560 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004561 psa_status_t actual_status;
4562 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004564
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004565 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004566
Gilles Peskine8817f612018-12-18 00:18:46 +01004567 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004568
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004569 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4570 psa_set_key_algorithm( &attributes, alg );
4571 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004572
Gilles Peskine049c7532019-05-15 20:22:09 +02004573 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004574 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004575
Ronald Cron5425a212020-08-04 14:58:35 +02004576 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004577 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004578 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004579 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004580 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004581 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004582 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004583
Gilles Peskine68428122018-06-30 18:42:41 +02004584 /* If the label is empty, the test framework puts a non-null pointer
4585 * in label->x. Test that a null pointer works as well. */
4586 if( label->len == 0 )
4587 {
4588 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004589 if( output_size != 0 )
4590 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004591 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004592 input_data->x, input_data->len,
4593 NULL, label->len,
4594 output, output_size,
4595 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004596 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004597 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004598 }
4599
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004600exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004601 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004602 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004603 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004604 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004605}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004606/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004607
4608/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004609void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004610{
4611 /* Test each valid way of initializing the object, except for `= {0}`, as
4612 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4613 * though it's OK by the C standard. We could test for this, but we'd need
4614 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004615 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004616 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4617 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4618 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004619
4620 memset( &zero, 0, sizeof( zero ) );
4621
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004623 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004624 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004625 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004626 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004627 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004628 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004629
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004630 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004631 PSA_ASSERT( psa_key_derivation_abort(&func) );
4632 PSA_ASSERT( psa_key_derivation_abort(&init) );
4633 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004634}
4635/* END_CASE */
4636
Janos Follath16de4a42019-06-13 16:32:24 +01004637/* BEGIN_CASE */
4638void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004639{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004640 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004641 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004642 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004643
Gilles Peskine8817f612018-12-18 00:18:46 +01004644 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004645
Janos Follath16de4a42019-06-13 16:32:24 +01004646 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004647 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004648
4649exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004650 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004651 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004652}
4653/* END_CASE */
4654
Janos Follathaf3c2a02019-06-12 12:34:34 +01004655/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004656void derive_set_capacity( int alg_arg, int capacity_arg,
4657 int expected_status_arg )
4658{
4659 psa_algorithm_t alg = alg_arg;
4660 size_t capacity = capacity_arg;
4661 psa_status_t expected_status = expected_status_arg;
4662 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4663
4664 PSA_ASSERT( psa_crypto_init( ) );
4665
4666 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4667
4668 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4669 expected_status );
4670
4671exit:
4672 psa_key_derivation_abort( &operation );
4673 PSA_DONE( );
4674}
4675/* END_CASE */
4676
4677/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004678void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004679 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004680 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004681 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004682 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004683 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004684 int expected_status_arg3,
4685 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004686{
4687 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004688 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4689 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004690 psa_status_t expected_statuses[] = {expected_status_arg1,
4691 expected_status_arg2,
4692 expected_status_arg3};
4693 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004694 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4695 MBEDTLS_SVC_KEY_ID_INIT,
4696 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004697 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4699 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004700 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004701 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004702 psa_status_t expected_output_status = expected_output_status_arg;
4703 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004704
4705 PSA_ASSERT( psa_crypto_init( ) );
4706
4707 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4708 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004709
4710 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4711
4712 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4713 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004714 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004715 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004716 psa_set_key_type( &attributes, key_types[i] );
4717 PSA_ASSERT( psa_import_key( &attributes,
4718 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004719 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004720 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4721 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4722 {
4723 // When taking a private key as secret input, use key agreement
4724 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004725 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004726 expected_statuses[i] );
4727 }
4728 else
4729 {
4730 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004731 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004732 expected_statuses[i] );
4733 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004734 }
4735 else
4736 {
4737 TEST_EQUAL( psa_key_derivation_input_bytes(
4738 &operation, steps[i],
4739 inputs[i]->x, inputs[i]->len ),
4740 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004741 }
4742 }
4743
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004744 if( output_key_type != PSA_KEY_TYPE_NONE )
4745 {
4746 psa_reset_key_attributes( &attributes );
4747 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4748 psa_set_key_bits( &attributes, 8 );
4749 actual_output_status =
4750 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004751 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004752 }
4753 else
4754 {
4755 uint8_t buffer[1];
4756 actual_output_status =
4757 psa_key_derivation_output_bytes( &operation,
4758 buffer, sizeof( buffer ) );
4759 }
4760 TEST_EQUAL( actual_output_status, expected_output_status );
4761
Janos Follathaf3c2a02019-06-12 12:34:34 +01004762exit:
4763 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004764 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4765 psa_destroy_key( keys[i] );
4766 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004767 PSA_DONE( );
4768}
4769/* END_CASE */
4770
Janos Follathd958bb72019-07-03 15:02:16 +01004771/* BEGIN_CASE */
4772void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004773{
Janos Follathd958bb72019-07-03 15:02:16 +01004774 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004776 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004777 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004778 unsigned char input1[] = "Input 1";
4779 size_t input1_length = sizeof( input1 );
4780 unsigned char input2[] = "Input 2";
4781 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004782 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004783 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004784 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4785 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4786 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004788
Gilles Peskine8817f612018-12-18 00:18:46 +01004789 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004790
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004791 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4792 psa_set_key_algorithm( &attributes, alg );
4793 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004794
Gilles Peskine73676cb2019-05-15 20:15:10 +02004795 PSA_ASSERT( psa_import_key( &attributes,
4796 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004797 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004798
4799 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004800 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004801 input1, input1_length,
4802 input2, input2_length,
4803 capacity ) )
4804 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004805
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004807 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004808 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004809
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004811
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004812 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004813 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004814
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004815exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004816 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004817 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004818 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004819}
4820/* END_CASE */
4821
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004822/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004823void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004824{
4825 uint8_t output_buffer[16];
4826 size_t buffer_size = 16;
4827 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004829
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004830 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4831 output_buffer, buffer_size )
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 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004835 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004836
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004838
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004839 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4840 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004841 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004842
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004844 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004845
4846exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004847 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004848}
4849/* END_CASE */
4850
4851/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004852void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004853 int step1_arg, data_t *input1,
4854 int step2_arg, data_t *input2,
4855 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004856 int requested_capacity_arg,
4857 data_t *expected_output1,
4858 data_t *expected_output2 )
4859{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004860 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004861 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4862 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004863 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4864 MBEDTLS_SVC_KEY_ID_INIT,
4865 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004866 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004867 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004868 uint8_t *expected_outputs[2] =
4869 {expected_output1->x, expected_output2->x};
4870 size_t output_sizes[2] =
4871 {expected_output1->len, expected_output2->len};
4872 size_t output_buffer_size = 0;
4873 uint8_t *output_buffer = NULL;
4874 size_t expected_capacity;
4875 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004877 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004878 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004879
4880 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4881 {
4882 if( output_sizes[i] > output_buffer_size )
4883 output_buffer_size = output_sizes[i];
4884 if( output_sizes[i] == 0 )
4885 expected_outputs[i] = NULL;
4886 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004887 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004889
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004890 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4891 psa_set_key_algorithm( &attributes, alg );
4892 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004893
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004894 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004895 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4896 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4897 requested_capacity ) );
4898 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004899 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004900 switch( steps[i] )
4901 {
4902 case 0:
4903 break;
4904 case PSA_KEY_DERIVATION_INPUT_SECRET:
4905 PSA_ASSERT( psa_import_key( &attributes,
4906 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004907 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004908 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004909 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004910 break;
4911 default:
4912 PSA_ASSERT( psa_key_derivation_input_bytes(
4913 &operation, steps[i],
4914 inputs[i]->x, inputs[i]->len ) );
4915 break;
4916 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004917 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004918
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004920 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004921 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004922 expected_capacity = requested_capacity;
4923
4924 /* Expansion phase. */
4925 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4926 {
4927 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004929 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004930 if( expected_capacity == 0 && output_sizes[i] == 0 )
4931 {
4932 /* Reading 0 bytes when 0 bytes are available can go either way. */
4933 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004934 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004935 continue;
4936 }
4937 else if( expected_capacity == 0 ||
4938 output_sizes[i] > expected_capacity )
4939 {
4940 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004941 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004942 expected_capacity = 0;
4943 continue;
4944 }
4945 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004946 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004947 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004948 ASSERT_COMPARE( output_buffer, output_sizes[i],
4949 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004950 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004951 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004952 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004953 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004954 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004955 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004957
4958exit:
4959 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004961 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4962 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004963 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004964}
4965/* END_CASE */
4966
4967/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004968void derive_full( int alg_arg,
4969 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004970 data_t *input1,
4971 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004972 int requested_capacity_arg )
4973{
Ronald Cron5425a212020-08-04 14:58:35 +02004974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004975 psa_algorithm_t alg = alg_arg;
4976 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004978 unsigned char output_buffer[16];
4979 size_t expected_capacity = requested_capacity;
4980 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004981 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004982
Gilles Peskine8817f612018-12-18 00:18:46 +01004983 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004984
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004985 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4986 psa_set_key_algorithm( &attributes, alg );
4987 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004988
Gilles Peskine049c7532019-05-15 20:22:09 +02004989 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004990 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004991
Ronald Cron5425a212020-08-04 14:58:35 +02004992 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01004993 input1->x, input1->len,
4994 input2->x, input2->len,
4995 requested_capacity ) )
4996 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004997
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004998 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004999 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005000 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005001
5002 /* Expansion phase. */
5003 while( current_capacity > 0 )
5004 {
5005 size_t read_size = sizeof( output_buffer );
5006 if( read_size > current_capacity )
5007 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005009 output_buffer,
5010 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005011 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005012 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005013 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005014 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005015 }
5016
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005017 /* Check that the operation refuses to go over capacity. */
5018 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005019 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005020
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005022
5023exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005025 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005026 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005027}
5028/* END_CASE */
5029
Janos Follathe60c9052019-07-03 13:51:30 +01005030/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005031void derive_key_exercise( int alg_arg,
5032 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005033 data_t *input1,
5034 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005035 int derived_type_arg,
5036 int derived_bits_arg,
5037 int derived_usage_arg,
5038 int derived_alg_arg )
5039{
Ronald Cron5425a212020-08-04 14:58:35 +02005040 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5041 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005042 psa_algorithm_t alg = alg_arg;
5043 psa_key_type_t derived_type = derived_type_arg;
5044 size_t derived_bits = derived_bits_arg;
5045 psa_key_usage_t derived_usage = derived_usage_arg;
5046 psa_algorithm_t derived_alg = derived_alg_arg;
5047 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005048 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005049 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005050 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005051
Gilles Peskine8817f612018-12-18 00:18:46 +01005052 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005053
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005054 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5055 psa_set_key_algorithm( &attributes, alg );
5056 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005057 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005058 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005059
5060 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005061 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005062 input1->x, input1->len,
5063 input2->x, input2->len, capacity ) )
5064 goto exit;
5065
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005066 psa_set_key_usage_flags( &attributes, derived_usage );
5067 psa_set_key_algorithm( &attributes, derived_alg );
5068 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005069 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005070 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005071 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005072
5073 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005074 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005075 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5076 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005077
5078 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005079 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005080 goto exit;
5081
5082exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005083 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005084 psa_reset_key_attributes( &got_attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005085 psa_destroy_key( base_key );
5086 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005087 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005088}
5089/* END_CASE */
5090
Janos Follath42fd8882019-07-03 14:17:09 +01005091/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005092void derive_key_export( int alg_arg,
5093 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005094 data_t *input1,
5095 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005096 int bytes1_arg,
5097 int bytes2_arg )
5098{
Ronald Cron5425a212020-08-04 14:58:35 +02005099 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5100 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005101 psa_algorithm_t alg = alg_arg;
5102 size_t bytes1 = bytes1_arg;
5103 size_t bytes2 = bytes2_arg;
5104 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005105 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005106 uint8_t *output_buffer = NULL;
5107 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005108 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5109 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005110 size_t length;
5111
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005112 ASSERT_ALLOC( output_buffer, capacity );
5113 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005115
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005116 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5117 psa_set_key_algorithm( &base_attributes, alg );
5118 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005119 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005120 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005121
5122 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005123 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005124 input1->x, input1->len,
5125 input2->x, input2->len, capacity ) )
5126 goto exit;
5127
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005129 output_buffer,
5130 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005131 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005132
5133 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005134 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005135 input1->x, input1->len,
5136 input2->x, input2->len, capacity ) )
5137 goto exit;
5138
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005139 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5140 psa_set_key_algorithm( &derived_attributes, 0 );
5141 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005142 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005143 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005144 &derived_key ) );
5145 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005146 export_buffer, bytes1,
5147 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005148 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005149 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005150 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005152 &derived_key ) );
5153 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005154 export_buffer + bytes1, bytes2,
5155 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005156 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005157
5158 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005159 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5160 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005161
5162exit:
5163 mbedtls_free( output_buffer );
5164 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005165 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005166 psa_destroy_key( base_key );
5167 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005168 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005169}
5170/* END_CASE */
5171
5172/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005173void derive_key( int alg_arg,
5174 data_t *key_data, data_t *input1, data_t *input2,
5175 int type_arg, int bits_arg,
5176 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005177{
Ronald Cron5425a212020-08-04 14:58:35 +02005178 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5179 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005180 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005181 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005182 size_t bits = bits_arg;
5183 psa_status_t expected_status = expected_status_arg;
5184 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5185 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5186 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5187
5188 PSA_ASSERT( psa_crypto_init( ) );
5189
5190 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5191 psa_set_key_algorithm( &base_attributes, alg );
5192 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5193 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005194 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005195
Ronald Cron5425a212020-08-04 14:58:35 +02005196 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005197 input1->x, input1->len,
5198 input2->x, input2->len, SIZE_MAX ) )
5199 goto exit;
5200
5201 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5202 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005203 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005204 psa_set_key_bits( &derived_attributes, bits );
5205 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005206 &derived_key ),
Gilles Peskinec744d992019-07-30 17:26:54 +02005207 expected_status );
5208
5209exit:
5210 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005211 psa_destroy_key( base_key );
5212 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005213 PSA_DONE( );
5214}
5215/* END_CASE */
5216
5217/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005218void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005219 int our_key_type_arg, int our_key_alg_arg,
5220 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005221 int expected_status_arg )
5222{
Ronald Cron5425a212020-08-04 14:58:35 +02005223 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005224 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005225 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005226 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005227 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005229 psa_status_t expected_status = expected_status_arg;
5230 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005231
Gilles Peskine8817f612018-12-18 00:18:46 +01005232 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005233
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005234 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005235 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005236 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005237 PSA_ASSERT( psa_import_key( &attributes,
5238 our_key_data->x, our_key_data->len,
5239 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005240
Gilles Peskine77f40d82019-04-11 21:27:06 +02005241 /* The tests currently include inputs that should fail at either step.
5242 * Test cases that fail at the setup step should be changed to call
5243 * key_derivation_setup instead, and this function should be renamed
5244 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005246 if( status == PSA_SUCCESS )
5247 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005248 TEST_EQUAL( psa_key_derivation_key_agreement(
5249 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5250 our_key,
5251 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005252 expected_status );
5253 }
5254 else
5255 {
5256 TEST_ASSERT( status == expected_status );
5257 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005258
5259exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005261 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005262 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005263}
5264/* END_CASE */
5265
5266/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005267void raw_key_agreement( int alg_arg,
5268 int our_key_type_arg, data_t *our_key_data,
5269 data_t *peer_key_data,
5270 data_t *expected_output )
5271{
Ronald Cron5425a212020-08-04 14:58:35 +02005272 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005273 psa_algorithm_t alg = alg_arg;
5274 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005276 unsigned char *output = NULL;
5277 size_t output_length = ~0;
5278
5279 ASSERT_ALLOC( output, expected_output->len );
5280 PSA_ASSERT( psa_crypto_init( ) );
5281
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005282 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5283 psa_set_key_algorithm( &attributes, alg );
5284 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005285 PSA_ASSERT( psa_import_key( &attributes,
5286 our_key_data->x, our_key_data->len,
5287 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005288
Gilles Peskinebe697d82019-05-16 18:00:41 +02005289 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5290 peer_key_data->x, peer_key_data->len,
5291 output, expected_output->len,
5292 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005293 ASSERT_COMPARE( output, output_length,
5294 expected_output->x, expected_output->len );
5295
5296exit:
5297 mbedtls_free( output );
5298 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005299 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005300}
5301/* END_CASE */
5302
5303/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005304void key_agreement_capacity( int alg_arg,
5305 int our_key_type_arg, data_t *our_key_data,
5306 data_t *peer_key_data,
5307 int expected_capacity_arg )
5308{
Ronald Cron5425a212020-08-04 14:58:35 +02005309 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005310 psa_algorithm_t alg = alg_arg;
5311 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005312 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005314 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005315 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005316
Gilles Peskine8817f612018-12-18 00:18:46 +01005317 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005318
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005319 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5320 psa_set_key_algorithm( &attributes, alg );
5321 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005322 PSA_ASSERT( psa_import_key( &attributes,
5323 our_key_data->x, our_key_data->len,
5324 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005325
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005326 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005327 PSA_ASSERT( psa_key_derivation_key_agreement(
5328 &operation,
5329 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5330 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005331 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5332 {
5333 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005335 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005336 NULL, 0 ) );
5337 }
Gilles Peskine59685592018-09-18 12:11:34 +02005338
Gilles Peskinebf491972018-10-25 22:36:12 +02005339 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005340 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005342 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005343
Gilles Peskinebf491972018-10-25 22:36:12 +02005344 /* Test the actual capacity by reading the output. */
5345 while( actual_capacity > sizeof( output ) )
5346 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005348 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005349 actual_capacity -= sizeof( output );
5350 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005351 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005352 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005353 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005354 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005355
Gilles Peskine59685592018-09-18 12:11:34 +02005356exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005357 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005358 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005359 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005360}
5361/* END_CASE */
5362
5363/* BEGIN_CASE */
5364void key_agreement_output( int alg_arg,
5365 int our_key_type_arg, data_t *our_key_data,
5366 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005367 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005368{
Ronald Cron5425a212020-08-04 14:58:35 +02005369 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005370 psa_algorithm_t alg = alg_arg;
5371 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005372 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005374 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005375
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005376 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5377 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005378
Gilles Peskine8817f612018-12-18 00:18:46 +01005379 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005380
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005381 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5382 psa_set_key_algorithm( &attributes, alg );
5383 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005384 PSA_ASSERT( psa_import_key( &attributes,
5385 our_key_data->x, our_key_data->len,
5386 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005387
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005388 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005389 PSA_ASSERT( psa_key_derivation_key_agreement(
5390 &operation,
5391 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5392 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005393 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5394 {
5395 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005396 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005397 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005398 NULL, 0 ) );
5399 }
Gilles Peskine59685592018-09-18 12:11:34 +02005400
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005401 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005402 actual_output,
5403 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005404 ASSERT_COMPARE( actual_output, expected_output1->len,
5405 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005406 if( expected_output2->len != 0 )
5407 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005408 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005409 actual_output,
5410 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005411 ASSERT_COMPARE( actual_output, expected_output2->len,
5412 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005413 }
Gilles Peskine59685592018-09-18 12:11:34 +02005414
5415exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005416 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005417 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005418 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005419 mbedtls_free( actual_output );
5420}
5421/* END_CASE */
5422
5423/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005424void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005425{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005426 size_t bytes = bytes_arg;
5427 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005428 unsigned char *output = NULL;
5429 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005430 size_t i;
5431 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005432
Simon Butcher49f8e312020-03-03 15:51:50 +00005433 TEST_ASSERT( bytes_arg >= 0 );
5434
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005435 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5436 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005437 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005438
Gilles Peskine8817f612018-12-18 00:18:46 +01005439 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005440
Gilles Peskinea50d7392018-06-21 10:22:13 +02005441 /* Run several times, to ensure that every output byte will be
5442 * nonzero at least once with overwhelming probability
5443 * (2^(-8*number_of_runs)). */
5444 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005445 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005446 if( bytes != 0 )
5447 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005448 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005449
5450 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005451 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5452 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005453
5454 for( i = 0; i < bytes; i++ )
5455 {
5456 if( output[i] != 0 )
5457 ++changed[i];
5458 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005459 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005460
5461 /* Check that every byte was changed to nonzero at least once. This
5462 * validates that psa_generate_random is overwriting every byte of
5463 * the output buffer. */
5464 for( i = 0; i < bytes; i++ )
5465 {
5466 TEST_ASSERT( changed[i] != 0 );
5467 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005468
5469exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005470 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005471 mbedtls_free( output );
5472 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005473}
5474/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005475
5476/* BEGIN_CASE */
5477void generate_key( int type_arg,
5478 int bits_arg,
5479 int usage_arg,
5480 int alg_arg,
5481 int expected_status_arg )
5482{
Ronald Cron5425a212020-08-04 14:58:35 +02005483 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005484 psa_key_type_t type = type_arg;
5485 psa_key_usage_t usage = usage_arg;
5486 size_t bits = bits_arg;
5487 psa_algorithm_t alg = alg_arg;
5488 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005490 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005491
Gilles Peskine8817f612018-12-18 00:18:46 +01005492 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005493
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005494 psa_set_key_usage_flags( &attributes, usage );
5495 psa_set_key_algorithm( &attributes, alg );
5496 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005497 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005498
5499 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005500 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005501 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005502 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005503
5504 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005505 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005506 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5507 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005508
Gilles Peskine818ca122018-06-20 18:16:48 +02005509 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005510 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005511 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005512
5513exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005514 psa_reset_key_attributes( &got_attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005515 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005516 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005517}
5518/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005519
Gilles Peskinee56e8782019-04-26 17:34:02 +02005520/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5521void generate_key_rsa( int bits_arg,
5522 data_t *e_arg,
5523 int expected_status_arg )
5524{
Ronald Cron5425a212020-08-04 14:58:35 +02005525 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005526 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005527 size_t bits = bits_arg;
5528 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5529 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5530 psa_status_t expected_status = expected_status_arg;
5531 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5532 uint8_t *exported = NULL;
5533 size_t exported_size =
5534 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5535 size_t exported_length = SIZE_MAX;
5536 uint8_t *e_read_buffer = NULL;
5537 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005538 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005539 size_t e_read_length = SIZE_MAX;
5540
5541 if( e_arg->len == 0 ||
5542 ( e_arg->len == 3 &&
5543 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5544 {
5545 is_default_public_exponent = 1;
5546 e_read_size = 0;
5547 }
5548 ASSERT_ALLOC( e_read_buffer, e_read_size );
5549 ASSERT_ALLOC( exported, exported_size );
5550
5551 PSA_ASSERT( psa_crypto_init( ) );
5552
5553 psa_set_key_usage_flags( &attributes, usage );
5554 psa_set_key_algorithm( &attributes, alg );
5555 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5556 e_arg->x, e_arg->len ) );
5557 psa_set_key_bits( &attributes, bits );
5558
5559 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005560 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005561 if( expected_status != PSA_SUCCESS )
5562 goto exit;
5563
5564 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005565 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005566 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5567 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5568 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5569 e_read_buffer, e_read_size,
5570 &e_read_length ) );
5571 if( is_default_public_exponent )
5572 TEST_EQUAL( e_read_length, 0 );
5573 else
5574 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5575
5576 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005577 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005578 goto exit;
5579
5580 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005581 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005582 exported, exported_size,
5583 &exported_length ) );
5584 {
5585 uint8_t *p = exported;
5586 uint8_t *end = exported + exported_length;
5587 size_t len;
5588 /* RSAPublicKey ::= SEQUENCE {
5589 * modulus INTEGER, -- n
5590 * publicExponent INTEGER } -- e
5591 */
5592 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005593 MBEDTLS_ASN1_SEQUENCE |
5594 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005595 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5596 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5597 MBEDTLS_ASN1_INTEGER ) );
5598 if( len >= 1 && p[0] == 0 )
5599 {
5600 ++p;
5601 --len;
5602 }
5603 if( e_arg->len == 0 )
5604 {
5605 TEST_EQUAL( len, 3 );
5606 TEST_EQUAL( p[0], 1 );
5607 TEST_EQUAL( p[1], 0 );
5608 TEST_EQUAL( p[2], 1 );
5609 }
5610 else
5611 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5612 }
5613
5614exit:
5615 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005616 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005617 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005618 mbedtls_free( e_read_buffer );
5619 mbedtls_free( exported );
5620}
5621/* END_CASE */
5622
Darryl Greend49a4992018-06-18 17:27:26 +01005623/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005624void persistent_key_load_key_from_storage( data_t *data,
5625 int type_arg, int bits_arg,
5626 int usage_flags_arg, int alg_arg,
5627 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005628{
Ronald Cron71016a92020-08-28 19:01:50 +02005629 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005630 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005631 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5632 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005633 psa_key_type_t type = type_arg;
5634 size_t bits = bits_arg;
5635 psa_key_usage_t usage_flags = usage_flags_arg;
5636 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005637 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005638 unsigned char *first_export = NULL;
5639 unsigned char *second_export = NULL;
5640 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5641 size_t first_exported_length;
5642 size_t second_exported_length;
5643
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005644 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5645 {
5646 ASSERT_ALLOC( first_export, export_size );
5647 ASSERT_ALLOC( second_export, export_size );
5648 }
Darryl Greend49a4992018-06-18 17:27:26 +01005649
Gilles Peskine8817f612018-12-18 00:18:46 +01005650 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005651
Gilles Peskinec87af662019-05-15 16:12:22 +02005652 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005653 psa_set_key_usage_flags( &attributes, usage_flags );
5654 psa_set_key_algorithm( &attributes, alg );
5655 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005656 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005657
Darryl Green0c6575a2018-11-07 16:05:30 +00005658 switch( generation_method )
5659 {
5660 case IMPORT_KEY:
5661 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005662 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005663 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005664 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005665
Darryl Green0c6575a2018-11-07 16:05:30 +00005666 case GENERATE_KEY:
5667 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005668 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005669 break;
5670
5671 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005672 {
5673 /* Create base key */
5674 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5675 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5676 psa_set_key_usage_flags( &base_attributes,
5677 PSA_KEY_USAGE_DERIVE );
5678 psa_set_key_algorithm( &base_attributes, derive_alg );
5679 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005680 PSA_ASSERT( psa_import_key( &base_attributes,
5681 data->x, data->len,
5682 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005683 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005684 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005685 PSA_ASSERT( psa_key_derivation_input_key(
5686 &operation,
5687 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005688 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005689 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005690 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005691 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5692 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005693 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005694 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005695 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005696 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005697 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005698 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005699 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005700 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005701
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005702 /* Export the key if permitted by the key policy. */
5703 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5704 {
Ronald Cron5425a212020-08-04 14:58:35 +02005705 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005706 first_export, export_size,
5707 &first_exported_length ) );
5708 if( generation_method == IMPORT_KEY )
5709 ASSERT_COMPARE( data->x, data->len,
5710 first_export, first_exported_length );
5711 }
Darryl Greend49a4992018-06-18 17:27:26 +01005712
5713 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005714 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005715 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005716 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005717
Darryl Greend49a4992018-06-18 17:27:26 +01005718 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005719 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005720 TEST_ASSERT( mbedtls_svc_key_id_equal(
5721 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005722 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5723 PSA_KEY_LIFETIME_PERSISTENT );
5724 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5725 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5726 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5727 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005728
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005729 /* Export the key again if permitted by the key policy. */
5730 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005731 {
Ronald Cron5425a212020-08-04 14:58:35 +02005732 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005733 second_export, export_size,
5734 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005735 ASSERT_COMPARE( first_export, first_exported_length,
5736 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005737 }
5738
5739 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005740 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005741 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005742
5743exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005744 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005745 mbedtls_free( first_export );
5746 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005747 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005748 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005749 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005750 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005751}
5752/* END_CASE */