blob: 7661925b456f7aa7de824d146bb59adf46952b41 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Ronald Cron28a45ed2021-02-09 20:35:42 +010015#include "test/psa_crypto_helpers.h"
16
Jaeden Amerof24c7f82018-06-27 17:20:43 +010017/** An invalid export length that will never be set by psa_export_key(). */
18static const size_t INVALID_EXPORT_LENGTH = ~0U;
19
Gilles Peskinef426e0f2019-02-25 17:42:03 +010020/* A hash algorithm that is known to be supported.
21 *
22 * This is used in some smoke tests.
23 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010024#if defined(PSA_WANT_ALG_MD2)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010025#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
Gilles Peskined6dc40c2021-01-12 12:55:31 +010026#elif defined(PSA_WANT_ALG_MD4)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010027#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
Gilles Peskined6dc40c2021-01-12 12:55:31 +010028#elif defined(PSA_WANT_ALG_MD5)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010029#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
30/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
31 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
32 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
33 * implausible anyway. */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010034#elif defined(PSA_WANT_ALG_SHA_1)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010035#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
Gilles Peskined6dc40c2021-01-12 12:55:31 +010036#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010037#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
Gilles Peskined6dc40c2021-01-12 12:55:31 +010038#elif defined(PSA_WANT_ALG_SHA_384)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010039#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
Gilles Peskined6dc40c2021-01-12 12:55:31 +010040#elif defined(PSA_WANT_ALG_SHA_512)
41#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
42#elif defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010043#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
44#else
45#undef KNOWN_SUPPORTED_HASH_ALG
46#endif
47
48/* A block cipher that is known to be supported.
49 *
50 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
51 */
52#if defined(MBEDTLS_AES_C)
53#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
54#elif defined(MBEDTLS_ARIA_C)
55#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
56#elif defined(MBEDTLS_CAMELLIA_C)
57#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
58#undef KNOWN_SUPPORTED_BLOCK_CIPHER
59#endif
60
61/* A MAC mode that is known to be supported.
62 *
63 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
64 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
65 *
66 * This is used in some smoke tests.
67 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010068#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010069#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
70#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
71#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
72#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
73#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
74#else
75#undef KNOWN_SUPPORTED_MAC_ALG
76#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
77#endif
78
79/* A cipher algorithm and key type that are known to be supported.
80 *
81 * This is used in some smoke tests.
82 */
83#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
84#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
85#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
86#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
87#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
89#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
90#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
91#else
92#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
93#endif
94#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
95#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
96#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
97#elif defined(MBEDTLS_RC4_C)
98#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
99#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
100#else
101#undef KNOWN_SUPPORTED_CIPHER_ALG
102#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
103#endif
104
Gilles Peskine667c1112019-12-03 19:03:20 +0100105#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100106int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
Gilles Peskine667c1112019-12-03 19:03:20 +0100107{
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100108 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
109 PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100110}
111#else
112int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
113{
114 (void) lifetime;
115 return( 0 );
116}
117#endif
118
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200119/** Test if a buffer contains a constant byte value.
120 *
121 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200122 *
123 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200124 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200125 * \param size Size of the buffer in bytes.
126 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200127 * \return 1 if the buffer is all-bits-zero.
128 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200129 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200130static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200131{
132 size_t i;
133 for( i = 0; i < size; i++ )
134 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200135 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200136 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200137 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200138 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200139}
Gilles Peskine818ca122018-06-20 18:16:48 +0200140
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200141/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
142static int asn1_write_10x( unsigned char **p,
143 unsigned char *start,
144 size_t bits,
145 unsigned char x )
146{
147 int ret;
148 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200149 if( bits == 0 )
150 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
151 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200152 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300153 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200154 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
155 *p -= len;
156 ( *p )[len-1] = x;
157 if( bits % 8 == 0 )
158 ( *p )[1] |= 1;
159 else
160 ( *p )[0] |= 1 << ( bits % 8 );
161 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
162 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
163 MBEDTLS_ASN1_INTEGER ) );
164 return( len );
165}
166
167static int construct_fake_rsa_key( unsigned char *buffer,
168 size_t buffer_size,
169 unsigned char **p,
170 size_t bits,
171 int keypair )
172{
173 size_t half_bits = ( bits + 1 ) / 2;
174 int ret;
175 int len = 0;
176 /* Construct something that looks like a DER encoding of
177 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
178 * RSAPrivateKey ::= SEQUENCE {
179 * version Version,
180 * modulus INTEGER, -- n
181 * publicExponent INTEGER, -- e
182 * privateExponent INTEGER, -- d
183 * prime1 INTEGER, -- p
184 * prime2 INTEGER, -- q
185 * exponent1 INTEGER, -- d mod (p-1)
186 * exponent2 INTEGER, -- d mod (q-1)
187 * coefficient INTEGER, -- (inverse of q) mod p
188 * otherPrimeInfos OtherPrimeInfos OPTIONAL
189 * }
190 * Or, for a public key, the same structure with only
191 * version, modulus and publicExponent.
192 */
193 *p = buffer + buffer_size;
194 if( keypair )
195 {
196 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
197 asn1_write_10x( p, buffer, half_bits, 1 ) );
198 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
199 asn1_write_10x( p, buffer, half_bits, 1 ) );
200 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
201 asn1_write_10x( p, buffer, half_bits, 1 ) );
202 MBEDTLS_ASN1_CHK_ADD( len, /* q */
203 asn1_write_10x( p, buffer, half_bits, 1 ) );
204 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
205 asn1_write_10x( p, buffer, half_bits, 3 ) );
206 MBEDTLS_ASN1_CHK_ADD( len, /* d */
207 asn1_write_10x( p, buffer, bits, 1 ) );
208 }
209 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
210 asn1_write_10x( p, buffer, 17, 1 ) );
211 MBEDTLS_ASN1_CHK_ADD( len, /* n */
212 asn1_write_10x( p, buffer, bits, 1 ) );
213 if( keypair )
214 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
215 mbedtls_asn1_write_int( p, buffer, 0 ) );
216 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
217 {
218 const unsigned char tag =
219 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
220 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
221 }
222 return( len );
223}
224
Ronald Cron5425a212020-08-04 14:58:35 +0200225int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100226{
227 int ok = 0;
228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
229 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200230 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100231 psa_key_type_t type;
232 psa_key_type_t bits;
233
234 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
235 lifetime = psa_get_key_lifetime( &attributes );
236 id = psa_get_key_id( &attributes );
237 type = psa_get_key_type( &attributes );
238 bits = psa_get_key_bits( &attributes );
239
240 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200241 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200242 {
243 TEST_ASSERT(
244 ( PSA_KEY_ID_VOLATILE_MIN <=
245 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
246 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
247 PSA_KEY_ID_VOLATILE_MAX ) );
248 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100249 else
250 {
251 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200252 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
253 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100254 }
255#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
256 /* randomly-generated 64-bit constant, should never appear in test data */
257 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
258 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100259 if( lifetime_is_dynamic_secure_element( lifetime ) )
Gilles Peskine667c1112019-12-03 19:03:20 +0100260 {
261 /* Mbed Crypto currently always exposes the slot number to
262 * applications. This is not mandated by the PSA specification
263 * and may change in future versions. */
264 TEST_EQUAL( status, 0 );
265 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
266 }
267 else
268 {
269 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
270 }
271#endif
272
273 /* Type and size */
274 TEST_ASSERT( type != 0 );
275 TEST_ASSERT( bits != 0 );
276 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
277 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
278 TEST_ASSERT( bits % 8 == 0 );
279
280 /* MAX macros concerning specific key types */
281 if( PSA_KEY_TYPE_IS_ECC( type ) )
282 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
283 else if( PSA_KEY_TYPE_IS_RSA( type ) )
284 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100285 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100286
287 ok = 1;
288
289exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100290 /*
291 * Key attributes may have been returned by psa_get_key_attributes()
292 * thus reset them as required.
293 */
Gilles Peskine667c1112019-12-03 19:03:20 +0100294 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100295
Gilles Peskine667c1112019-12-03 19:03:20 +0100296 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. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100445 iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200446 psa_get_key_type( &attributes ) );
447 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100448 psa_reset_key_attributes( &attributes );
Gilles Peskine818ca122018-06-20 18:16:48 +0200449 }
Ronald Cron5425a212020-08-04 14:58:35 +0200450 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100451 PSA_ASSERT( psa_cipher_set_iv( &operation,
452 iv, iv_length ) );
453 PSA_ASSERT( psa_cipher_update( &operation,
454 ciphertext, ciphertext_length,
455 decrypted, sizeof( decrypted ),
456 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200457 status = psa_cipher_finish( &operation,
458 decrypted + part_length,
459 sizeof( decrypted ) - part_length,
460 &part_length );
461 /* For a stream cipher, all inputs are valid. For a block cipher,
462 * if the input is some aribtrary data rather than an actual
463 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200464 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200465 TEST_ASSERT( status == PSA_SUCCESS ||
466 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200467 else
468 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200469 }
470
471 return( 1 );
472
473exit:
474 psa_cipher_abort( &operation );
475 return( 0 );
476}
477
Ronald Cron5425a212020-08-04 14:58:35 +0200478static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200479 psa_key_usage_t usage,
480 psa_algorithm_t alg )
481{
482 unsigned char nonce[16] = {0};
483 size_t nonce_length = sizeof( nonce );
484 unsigned char plaintext[16] = "Hello, world...";
485 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
486 size_t ciphertext_length = sizeof( ciphertext );
487 size_t plaintext_length = sizeof( ciphertext );
488
Steven Cooreman2f099132021-01-11 20:33:45 +0100489 /* Default IV length for AES-GCM is 12 bytes */
Bence Szépkútia63b20d2020-12-16 11:36:46 +0100490 if( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ==
491 PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ) )
Steven Cooreman50f1f5e2021-01-25 10:26:49 +0100492 {
Steven Cooreman2f099132021-01-11 20:33:45 +0100493 nonce_length = 12;
Steven Cooreman50f1f5e2021-01-25 10:26:49 +0100494 }
Steven Cooreman2f099132021-01-11 20:33:45 +0100495
Gilles Peskine818ca122018-06-20 18:16:48 +0200496 if( usage & PSA_KEY_USAGE_ENCRYPT )
497 {
Ronald Cron5425a212020-08-04 14:58:35 +0200498 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100499 nonce, nonce_length,
500 NULL, 0,
501 plaintext, sizeof( plaintext ),
502 ciphertext, sizeof( ciphertext ),
503 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200504 }
505
506 if( usage & PSA_KEY_USAGE_DECRYPT )
507 {
508 psa_status_t verify_status =
509 ( usage & PSA_KEY_USAGE_ENCRYPT ?
510 PSA_SUCCESS :
511 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200512 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100513 nonce, nonce_length,
514 NULL, 0,
515 ciphertext, ciphertext_length,
516 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100517 &plaintext_length ),
518 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200519 }
520
521 return( 1 );
522
523exit:
524 return( 0 );
525}
526
Ronald Cron5425a212020-08-04 14:58:35 +0200527static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200528 psa_key_usage_t usage,
529 psa_algorithm_t alg )
530{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200531 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
532 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100533 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200534 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100535 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
536
537 /* If the policy allows signing with any hash, just pick one. */
538 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
539 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100540#if defined(KNOWN_SUPPORTED_HASH_ALG)
541 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
542 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100543#else
Chris Jones9634bb12021-01-20 15:56:42 +0000544 mbedtls_test_fail( "No hash algorithm for hash-and-sign testing",
545 __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100546 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100547#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100548 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200549
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100550 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200551 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200552 /* Some algorithms require the payload to have the size of
553 * the hash encoded in the algorithm. Use this input size
554 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200555 if( hash_alg != 0 )
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100556 payload_length = PSA_HASH_LENGTH( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200557 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100558 payload, payload_length,
559 signature, sizeof( signature ),
560 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200561 }
562
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100563 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200564 {
565 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100566 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200567 PSA_SUCCESS :
568 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200569 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100570 payload, payload_length,
571 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100572 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200573 }
574
575 return( 1 );
576
577exit:
578 return( 0 );
579}
580
Ronald Cron5425a212020-08-04 14:58:35 +0200581static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200582 psa_key_usage_t usage,
583 psa_algorithm_t alg )
584{
585 unsigned char plaintext[256] = "Hello, world...";
586 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
587 size_t ciphertext_length = sizeof( ciphertext );
588 size_t plaintext_length = 16;
589
590 if( usage & PSA_KEY_USAGE_ENCRYPT )
591 {
Ronald Cron5425a212020-08-04 14:58:35 +0200592 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100593 plaintext, plaintext_length,
594 NULL, 0,
595 ciphertext, sizeof( ciphertext ),
596 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200597 }
598
599 if( usage & PSA_KEY_USAGE_DECRYPT )
600 {
601 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200602 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200603 ciphertext, ciphertext_length,
604 NULL, 0,
605 plaintext, sizeof( plaintext ),
606 &plaintext_length );
607 TEST_ASSERT( status == PSA_SUCCESS ||
608 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
609 ( status == PSA_ERROR_INVALID_ARGUMENT ||
610 status == PSA_ERROR_INVALID_PADDING ) ) );
611 }
612
613 return( 1 );
614
615exit:
616 return( 0 );
617}
Gilles Peskine02b75072018-07-01 22:31:34 +0200618
Janos Follathf2815ea2019-07-03 12:41:36 +0100619static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200620 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100621 psa_algorithm_t alg,
622 unsigned char* input1, size_t input1_length,
623 unsigned char* input2, size_t input2_length,
624 size_t capacity )
625{
626 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
627 if( PSA_ALG_IS_HKDF( alg ) )
628 {
629 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
630 PSA_KEY_DERIVATION_INPUT_SALT,
631 input1, input1_length ) );
632 PSA_ASSERT( psa_key_derivation_input_key( operation,
633 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200634 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100635 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
636 PSA_KEY_DERIVATION_INPUT_INFO,
637 input2,
638 input2_length ) );
639 }
640 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
641 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
642 {
643 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
644 PSA_KEY_DERIVATION_INPUT_SEED,
645 input1, input1_length ) );
646 PSA_ASSERT( psa_key_derivation_input_key( operation,
647 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200648 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100649 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
650 PSA_KEY_DERIVATION_INPUT_LABEL,
651 input2, input2_length ) );
652 }
653 else
654 {
655 TEST_ASSERT( ! "Key derivation algorithm not supported" );
656 }
657
Gilles Peskinec744d992019-07-30 17:26:54 +0200658 if( capacity != SIZE_MAX )
659 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100660
661 return( 1 );
662
663exit:
664 return( 0 );
665}
666
667
Ronald Cron5425a212020-08-04 14:58:35 +0200668static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200669 psa_key_usage_t usage,
670 psa_algorithm_t alg )
671{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200672 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100673 unsigned char input1[] = "Input 1";
674 size_t input1_length = sizeof( input1 );
675 unsigned char input2[] = "Input 2";
676 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200677 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100678 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200679
680 if( usage & PSA_KEY_USAGE_DERIVE )
681 {
Ronald Cron5425a212020-08-04 14:58:35 +0200682 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100683 input1, input1_length,
684 input2, input2_length, capacity ) )
685 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200686
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200687 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200688 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100689 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200690 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200691 }
692
693 return( 1 );
694
695exit:
696 return( 0 );
697}
698
Gilles Peskinec7998b72018-11-07 18:45:02 +0100699/* We need two keys to exercise key agreement. Exercise the
700 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200701static psa_status_t key_agreement_with_self(
702 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200703 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100704{
705 psa_key_type_t private_key_type;
706 psa_key_type_t public_key_type;
707 size_t key_bits;
708 uint8_t *public_key = NULL;
709 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200710 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200711 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
712 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200713 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200714 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100715
Ronald Cron5425a212020-08-04 14:58:35 +0200716 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200717 private_key_type = psa_get_key_type( &attributes );
718 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200719 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100720 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100721 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200722 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100723 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100724
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200725 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200726 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200727 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100728exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100729 /*
730 * Key attributes may have been returned by psa_get_key_attributes()
731 * thus reset them as required.
732 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200733 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100734
735 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100736 return( status );
737}
738
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200739/* We need two keys to exercise key agreement. Exercise the
740 * private key against its own public key. */
741static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200742 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200743{
744 psa_key_type_t private_key_type;
745 psa_key_type_t public_key_type;
746 size_t key_bits;
747 uint8_t *public_key = NULL;
748 size_t public_key_length;
749 uint8_t output[1024];
750 size_t output_length;
751 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200752 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
753 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200754 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200755 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200756
Ronald Cron5425a212020-08-04 14:58:35 +0200757 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200758 private_key_type = psa_get_key_type( &attributes );
759 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200760 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100761 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200762 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200763 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200764 public_key, public_key_length,
765 &public_key_length ) );
766
Ronald Cron5425a212020-08-04 14:58:35 +0200767 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200768 public_key, public_key_length,
769 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200770exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100771 /*
772 * Key attributes may have been returned by psa_get_key_attributes()
773 * thus reset them as required.
774 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200775 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100776
777 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200778 return( status );
779}
780
Ronald Cron5425a212020-08-04 14:58:35 +0200781static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200782 psa_key_usage_t usage,
783 psa_algorithm_t alg )
784{
785 int ok = 0;
786
787 if( usage & PSA_KEY_USAGE_DERIVE )
788 {
789 /* We need two keys to exercise key agreement. Exercise the
790 * private key against its own public key. */
Ronald Cron5425a212020-08-04 14:58:35 +0200791 PSA_ASSERT( raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200792 }
793 ok = 1;
794
795exit:
796 return( ok );
797}
798
Ronald Cron5425a212020-08-04 14:58:35 +0200799static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200800 psa_key_usage_t usage,
801 psa_algorithm_t alg )
802{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200803 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200804 unsigned char output[1];
805 int ok = 0;
806
807 if( usage & PSA_KEY_USAGE_DERIVE )
808 {
809 /* We need two keys to exercise key agreement. Exercise the
810 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200811 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200812 PSA_ASSERT( key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200813 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200814 output,
815 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200816 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200817 }
818 ok = 1;
819
820exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200821 return( ok );
822}
823
Jaeden Amerof7dca862019-06-27 17:31:33 +0100824int asn1_skip_integer( unsigned char **p, const unsigned char *end,
825 size_t min_bits, size_t max_bits,
826 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200827{
828 size_t len;
829 size_t actual_bits;
830 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100831 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100832 MBEDTLS_ASN1_INTEGER ),
833 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200834
835 /* Check if the retrieved length doesn't extend the actual buffer's size.
836 * It is assumed here, that end >= p, which validates casting to size_t. */
837 TEST_ASSERT( len <= (size_t)( end - *p) );
838
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200839 /* Tolerate a slight departure from DER encoding:
840 * - 0 may be represented by an empty string or a 1-byte string.
841 * - The sign bit may be used as a value bit. */
842 if( ( len == 1 && ( *p )[0] == 0 ) ||
843 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
844 {
845 ++( *p );
846 --len;
847 }
848 if( min_bits == 0 && len == 0 )
849 return( 1 );
850 msb = ( *p )[0];
851 TEST_ASSERT( msb != 0 );
852 actual_bits = 8 * ( len - 1 );
853 while( msb != 0 )
854 {
855 msb >>= 1;
856 ++actual_bits;
857 }
858 TEST_ASSERT( actual_bits >= min_bits );
859 TEST_ASSERT( actual_bits <= max_bits );
860 if( must_be_odd )
861 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
862 *p += len;
863 return( 1 );
864exit:
865 return( 0 );
866}
867
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200868static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
869 uint8_t *exported, size_t exported_length )
870{
871 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100872 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200873 else
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100874 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200875
876#if defined(MBEDTLS_DES_C)
877 if( type == PSA_KEY_TYPE_DES )
878 {
879 /* Check the parity bits. */
880 unsigned i;
881 for( i = 0; i < bits / 8; i++ )
882 {
883 unsigned bit_count = 0;
884 unsigned m;
885 for( m = 1; m <= 0x100; m <<= 1 )
886 {
887 if( exported[i] & m )
888 ++bit_count;
889 }
890 TEST_ASSERT( bit_count % 2 != 0 );
891 }
892 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200893 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200894#endif
895
896#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200897 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200898 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200899 uint8_t *p = exported;
900 uint8_t *end = exported + exported_length;
901 size_t len;
902 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200903 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200904 * modulus INTEGER, -- n
905 * publicExponent INTEGER, -- e
906 * privateExponent INTEGER, -- d
907 * prime1 INTEGER, -- p
908 * prime2 INTEGER, -- q
909 * exponent1 INTEGER, -- d mod (p-1)
910 * exponent2 INTEGER, -- d mod (q-1)
911 * coefficient INTEGER, -- (inverse of q) mod p
912 * }
913 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100914 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
915 MBEDTLS_ASN1_SEQUENCE |
916 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
917 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200918 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
919 goto exit;
920 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
921 goto exit;
922 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
923 goto exit;
924 /* Require d to be at least half the size of n. */
925 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
926 goto exit;
927 /* Require p and q to be at most half the size of n, rounded up. */
928 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
929 goto exit;
930 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
931 goto exit;
932 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
933 goto exit;
934 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
935 goto exit;
936 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
937 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100938 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100939 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200940 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200941#endif /* MBEDTLS_RSA_C */
942
943#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200944 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200945 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100946 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100947 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100948 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200949 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200950#endif /* MBEDTLS_ECP_C */
951
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200952 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
953 {
954 uint8_t *p = exported;
955 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200956#if defined(MBEDTLS_RSA_C)
957 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
958 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100959 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200960 /* RSAPublicKey ::= SEQUENCE {
961 * modulus INTEGER, -- n
962 * publicExponent INTEGER } -- e
963 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100964 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
965 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100966 MBEDTLS_ASN1_CONSTRUCTED ),
967 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100968 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200969 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
970 goto exit;
971 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
972 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100973 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200974 }
975 else
976#endif /* MBEDTLS_RSA_C */
977#if defined(MBEDTLS_ECP_C)
978 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
979 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200980 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
981 {
982 /* The representation of an ECC Montgomery public key is
983 * the raw compressed point */
984 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
985 }
986 else
987 {
988 /* The representation of an ECC Weierstrass public key is:
989 * - The byte 0x04;
990 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
991 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
992 * - where m is the bit size associated with the curve.
993 */
994 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
995 TEST_EQUAL( p[0], 4 );
996 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200997 }
998 else
999#endif /* MBEDTLS_ECP_C */
1000 {
Jaeden Amero594a3302018-10-26 17:07:22 +01001001 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001002 mbedtls_snprintf( message, sizeof( message ),
1003 "No sanity check for public key type=0x%08lx",
1004 (unsigned long) type );
Chris Jones9634bb12021-01-20 15:56:42 +00001005 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +02001006 (void) p;
1007 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001008 return( 0 );
1009 }
1010 }
1011 else
1012
1013 {
1014 /* No sanity checks for other types */
1015 }
1016
1017 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001018
1019exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001020 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001021}
1022
Ronald Cron5425a212020-08-04 14:58:35 +02001023static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001024 psa_key_usage_t usage )
1025{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001027 uint8_t *exported = NULL;
1028 size_t exported_size = 0;
1029 size_t exported_length = 0;
1030 int ok = 0;
1031
Ronald Cron5425a212020-08-04 14:58:35 +02001032 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001033
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001034 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1035 psa_get_key_type( &attributes ),
1036 psa_get_key_bits( &attributes ) );
1037 ASSERT_ALLOC( exported, exported_size );
1038
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001039 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001040 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001041 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001042 TEST_EQUAL( psa_export_key( key, exported,
1043 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001044 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001045 ok = 1;
1046 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001047 }
1048
Ronald Cron5425a212020-08-04 14:58:35 +02001049 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001050 exported, exported_size,
1051 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001052 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1053 psa_get_key_bits( &attributes ),
1054 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001055
1056exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001057 /*
1058 * Key attributes may have been returned by psa_get_key_attributes()
1059 * thus reset them as required.
1060 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001061 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001062
1063 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001064 return( ok );
1065}
1066
Ronald Cron5425a212020-08-04 14:58:35 +02001067static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001068{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001069 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001070 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001071 uint8_t *exported = NULL;
1072 size_t exported_size = 0;
1073 size_t exported_length = 0;
1074 int ok = 0;
1075
Ronald Cron5425a212020-08-04 14:58:35 +02001076 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001077 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001078 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001079 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1080 psa_get_key_type( &attributes ),
1081 psa_get_key_bits( &attributes ) );
1082 ASSERT_ALLOC( exported, exported_size );
1083
1084 TEST_EQUAL( psa_export_public_key( key, exported,
1085 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001086 PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001087 ok = 1;
1088 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001089 }
1090
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001091 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001092 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001093 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1094 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001095 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001096
Ronald Cron5425a212020-08-04 14:58:35 +02001097 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001098 exported, exported_size,
1099 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001100 ok = exported_key_sanity_check( public_type,
1101 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001102 exported, exported_length );
1103
1104exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001105 /*
1106 * Key attributes may have been returned by psa_get_key_attributes()
1107 * thus reset them as required.
1108 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001109 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001110
1111 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001112 return( ok );
1113}
1114
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001115/** Do smoke tests on a key.
1116 *
1117 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1118 * sign/verify, or derivation) that is permitted according to \p usage.
1119 * \p usage and \p alg should correspond to the expected policy on the
1120 * key.
1121 *
1122 * Export the key if permitted by \p usage, and check that the output
1123 * looks sensible. If \p usage forbids export, check that
1124 * \p psa_export_key correctly rejects the attempt. If the key is
1125 * asymmetric, also check \p psa_export_public_key.
1126 *
1127 * If the key fails the tests, this function calls the test framework's
Chris Jones9634bb12021-01-20 15:56:42 +00001128 * `mbedtls_test_fail` function and returns false. Otherwise this function
1129 * returns true. Therefore it should be used as follows:
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001130 * ```
1131 * if( ! exercise_key( ... ) ) goto exit;
1132 * ```
1133 *
Ronald Cron5425a212020-08-04 14:58:35 +02001134 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001135 * \p alg.
1136 * \param usage The usage flags to assume.
1137 * \param alg The algorithm to exercise.
1138 *
1139 * \retval 0 The key failed the smoke tests.
1140 * \retval 1 The key passed the smoke tests.
1141 */
Ronald Cron5425a212020-08-04 14:58:35 +02001142static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001143 psa_key_usage_t usage,
1144 psa_algorithm_t alg )
1145{
1146 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001147
Ronald Cron5425a212020-08-04 14:58:35 +02001148 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001149 return( 0 );
1150
Gilles Peskine02b75072018-07-01 22:31:34 +02001151 if( alg == 0 )
1152 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1153 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001154 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001155 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001156 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001157 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001158 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001159 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001160 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001161 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001162 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001163 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001164 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001165 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001166 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001167 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001168 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001169 else
1170 {
1171 char message[40];
1172 mbedtls_snprintf( message, sizeof( message ),
1173 "No code to exercise alg=0x%08lx",
1174 (unsigned long) alg );
Chris Jones9634bb12021-01-20 15:56:42 +00001175 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskine02b75072018-07-01 22:31:34 +02001176 ok = 0;
1177 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001178
Ronald Cron5425a212020-08-04 14:58:35 +02001179 ok = ok && exercise_export_key( key, usage );
1180 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001181
Gilles Peskine02b75072018-07-01 22:31:34 +02001182 return( ok );
1183}
1184
Gilles Peskine10df3412018-10-25 22:35:43 +02001185static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1186 psa_algorithm_t alg )
1187{
1188 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1189 {
1190 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001191 PSA_KEY_USAGE_VERIFY_HASH :
1192 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001193 }
1194 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1195 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1196 {
1197 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1198 PSA_KEY_USAGE_ENCRYPT :
1199 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1200 }
1201 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1202 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1203 {
1204 return( PSA_KEY_USAGE_DERIVE );
1205 }
1206 else
1207 {
1208 return( 0 );
1209 }
1210
1211}
Darryl Green0c6575a2018-11-07 16:05:30 +00001212
Ronald Cron5425a212020-08-04 14:58:35 +02001213static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001214{
1215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001216 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001217 uint8_t buffer[1];
1218 size_t length;
1219 int ok = 0;
1220
Ronald Cronecfb2372020-07-23 17:13:42 +02001221 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001222 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1223 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1224 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001225 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001226 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001227 TEST_EQUAL(
1228 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1229 TEST_EQUAL(
1230 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001231 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001232 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1233 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1234 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1235 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1236
Ronald Cron5425a212020-08-04 14:58:35 +02001237 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001238 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001239 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001240 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001241 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001242
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001243 ok = 1;
1244
1245exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001246 /*
1247 * Key attributes may have been returned by psa_get_key_attributes()
1248 * thus reset them as required.
1249 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001250 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001251
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001252 return( ok );
1253}
1254
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001255/* Assert that a key isn't reported as having a slot number. */
1256#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1257#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1258 do \
1259 { \
1260 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1261 TEST_EQUAL( psa_get_key_slot_number( \
1262 attributes, \
1263 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1264 PSA_ERROR_INVALID_ARGUMENT ); \
1265 } \
1266 while( 0 )
1267#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1268#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1269 ( (void) 0 )
1270#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1271
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001272/* An overapproximation of the amount of storage needed for a key of the
1273 * given type and with the given content. The API doesn't make it easy
1274 * to find a good value for the size. The current implementation doesn't
1275 * care about the value anyway. */
1276#define KEY_BITS_FROM_DATA( type, data ) \
1277 ( data )->len
1278
Darryl Green0c6575a2018-11-07 16:05:30 +00001279typedef enum {
1280 IMPORT_KEY = 0,
1281 GENERATE_KEY = 1,
1282 DERIVE_KEY = 2
1283} generate_method;
1284
Gilles Peskinee59236f2018-01-27 23:32:46 +01001285/* END_HEADER */
1286
1287/* BEGIN_DEPENDENCIES
1288 * depends_on:MBEDTLS_PSA_CRYPTO_C
1289 * END_DEPENDENCIES
1290 */
1291
1292/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001293void static_checks( )
1294{
1295 size_t max_truncated_mac_size =
1296 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1297
1298 /* Check that the length for a truncated MAC always fits in the algorithm
1299 * encoding. The shifted mask is the maximum truncated value. The
1300 * untruncated algorithm may be one byte larger. */
1301 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001302
1303#if defined(MBEDTLS_TEST_DEPRECATED)
1304 /* Check deprecated constants. */
1305 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1306 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1307 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1308 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1309 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1310 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1311 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1312 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001313
Paul Elliott8ff510a2020-06-02 17:19:28 +01001314 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1326 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1328 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1329 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1330 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1331 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1332 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1333 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1334 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1335 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1336 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1337 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1338 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1339 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1340 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1341 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1342 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1343 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1344
1345 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1346 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1347 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1348 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1349 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1350 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1351 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1352 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001353
Paul Elliott75e27032020-06-03 15:17:39 +01001354 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1355 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1356 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1357 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1358 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1359
1360 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1361 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001362#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001363}
1364/* END_CASE */
1365
1366/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001367void import_with_policy( int type_arg,
1368 int usage_arg, int alg_arg,
1369 int expected_status_arg )
1370{
1371 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1372 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001374 psa_key_type_t type = type_arg;
1375 psa_key_usage_t usage = usage_arg;
1376 psa_algorithm_t alg = alg_arg;
1377 psa_status_t expected_status = expected_status_arg;
1378 const uint8_t key_material[16] = {0};
1379 psa_status_t status;
1380
1381 PSA_ASSERT( psa_crypto_init( ) );
1382
1383 psa_set_key_type( &attributes, type );
1384 psa_set_key_usage_flags( &attributes, usage );
1385 psa_set_key_algorithm( &attributes, alg );
1386
1387 status = psa_import_key( &attributes,
1388 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001389 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001390 TEST_EQUAL( status, expected_status );
1391 if( status != PSA_SUCCESS )
1392 goto exit;
1393
Ronald Cron5425a212020-08-04 14:58:35 +02001394 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001395 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1396 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1397 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001398 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001399
Ronald Cron5425a212020-08-04 14:58:35 +02001400 PSA_ASSERT( psa_destroy_key( key ) );
1401 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001402
1403exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001404 /*
1405 * Key attributes may have been returned by psa_get_key_attributes()
1406 * thus reset them as required.
1407 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001408 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001409
1410 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001411 PSA_DONE( );
1412}
1413/* END_CASE */
1414
1415/* BEGIN_CASE */
1416void import_with_data( data_t *data, int type_arg,
1417 int attr_bits_arg,
1418 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001419{
1420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1421 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001422 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001423 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001424 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001425 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001426 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001427
Gilles Peskine8817f612018-12-18 00:18:46 +01001428 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001429
Gilles Peskine4747d192019-04-17 15:05:45 +02001430 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001431 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001432
Ronald Cron5425a212020-08-04 14:58:35 +02001433 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001434 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001435 if( status != PSA_SUCCESS )
1436 goto exit;
1437
Ronald Cron5425a212020-08-04 14:58:35 +02001438 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001439 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001440 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001441 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001442 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001443
Ronald Cron5425a212020-08-04 14:58:35 +02001444 PSA_ASSERT( psa_destroy_key( key ) );
1445 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001446
1447exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001448 /*
1449 * Key attributes may have been returned by psa_get_key_attributes()
1450 * thus reset them as required.
1451 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001452 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001453
1454 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001455 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001456}
1457/* END_CASE */
1458
1459/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001460void import_large_key( int type_arg, int byte_size_arg,
1461 int expected_status_arg )
1462{
1463 psa_key_type_t type = type_arg;
1464 size_t byte_size = byte_size_arg;
1465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1466 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001467 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001468 psa_status_t status;
1469 uint8_t *buffer = NULL;
1470 size_t buffer_size = byte_size + 1;
1471 size_t n;
1472
Steven Cooreman69967ce2021-01-18 18:01:08 +01001473 /* Skip the test case if the target running the test cannot
1474 * accomodate large keys due to heap size constraints */
1475 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001476 memset( buffer, 'K', byte_size );
1477
1478 PSA_ASSERT( psa_crypto_init( ) );
1479
1480 /* Try importing the key */
1481 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1482 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001483 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001484 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001485 TEST_EQUAL( status, expected_status );
1486
1487 if( status == PSA_SUCCESS )
1488 {
Ronald Cron5425a212020-08-04 14:58:35 +02001489 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001490 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1491 TEST_EQUAL( psa_get_key_bits( &attributes ),
1492 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001493 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001494 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001495 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001496 for( n = 0; n < byte_size; n++ )
1497 TEST_EQUAL( buffer[n], 'K' );
1498 for( n = byte_size; n < buffer_size; n++ )
1499 TEST_EQUAL( buffer[n], 0 );
1500 }
1501
1502exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001503 /*
1504 * Key attributes may have been returned by psa_get_key_attributes()
1505 * thus reset them as required.
1506 */
1507 psa_reset_key_attributes( &attributes );
1508
Ronald Cron5425a212020-08-04 14:58:35 +02001509 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001510 PSA_DONE( );
1511 mbedtls_free( buffer );
1512}
1513/* END_CASE */
1514
1515/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001516void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1517{
Ronald Cron5425a212020-08-04 14:58:35 +02001518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001519 size_t bits = bits_arg;
1520 psa_status_t expected_status = expected_status_arg;
1521 psa_status_t status;
1522 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001523 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001524 size_t buffer_size = /* Slight overapproximations */
1525 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001526 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001527 unsigned char *p;
1528 int ret;
1529 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001530 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001531
Gilles Peskine8817f612018-12-18 00:18:46 +01001532 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001533 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001534
1535 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1536 bits, keypair ) ) >= 0 );
1537 length = ret;
1538
1539 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001540 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001541 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001542 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001543
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001544 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001545 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001546
1547exit:
1548 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001549 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001550}
1551/* END_CASE */
1552
1553/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001554void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001555 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001556 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001557 int expected_bits,
1558 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001559 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001560 int canonical_input )
1561{
Ronald Cron5425a212020-08-04 14:58:35 +02001562 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001563 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001564 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001565 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001566 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567 unsigned char *exported = NULL;
1568 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001569 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001570 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001571 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001572 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001573 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001574
Moran Pekercb088e72018-07-17 17:36:59 +03001575 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001576 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001577 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001578 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001579 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001580
Gilles Peskine4747d192019-04-17 15:05:45 +02001581 psa_set_key_usage_flags( &attributes, usage_arg );
1582 psa_set_key_algorithm( &attributes, alg );
1583 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001584
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001585 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001586 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587
1588 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001589 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001590 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1591 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001592 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001593
1594 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001595 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001596 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001597
1598 /* The exported length must be set by psa_export_key() to a value between 0
1599 * and export_size. On errors, the exported length must be 0. */
1600 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1601 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1602 TEST_ASSERT( exported_length <= export_size );
1603
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001604 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001605 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001606 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001607 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001608 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001609 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001610 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001611
Ronald Cron5425a212020-08-04 14:58:35 +02001612 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001613 goto exit;
1614
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001615 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001616 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001617 else
1618 {
Ronald Cron5425a212020-08-04 14:58:35 +02001619 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001620 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001621 &key2 ) );
1622 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001623 reexported,
1624 export_size,
1625 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001626 ASSERT_COMPARE( exported, exported_length,
1627 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001628 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001629 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001630 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001631
1632destroy:
1633 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001634 PSA_ASSERT( psa_destroy_key( key ) );
1635 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001636
1637exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001638 /*
1639 * Key attributes may have been returned by psa_get_key_attributes()
1640 * thus reset them as required.
1641 */
1642 psa_reset_key_attributes( &got_attributes );
1643
itayzafrir3e02b3b2018-06-12 17:06:52 +03001644 mbedtls_free( exported );
1645 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001646 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001647}
1648/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001649
Moran Pekerf709f4a2018-06-06 17:26:04 +03001650/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001651void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001652 int type_arg,
1653 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001654 int export_size_delta,
1655 int expected_export_status_arg,
1656 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001657{
Ronald Cron5425a212020-08-04 14:58:35 +02001658 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001659 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001660 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001661 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001662 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001663 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001664 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001665 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001667
Gilles Peskine8817f612018-12-18 00:18:46 +01001668 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001669
Gilles Peskine4747d192019-04-17 15:05:45 +02001670 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1671 psa_set_key_algorithm( &attributes, alg );
1672 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001673
1674 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001675 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001676
Gilles Peskine49c25912018-10-29 15:15:31 +01001677 /* Export the public key */
1678 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001679 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001680 exported, export_size,
1681 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001682 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001683 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001684 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001685 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001686 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001687 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001688 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001689 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001690 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001691 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1692 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001693 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001694
1695exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001696 /*
1697 * Key attributes may have been returned by psa_get_key_attributes()
1698 * thus reset them as required.
1699 */
1700 psa_reset_key_attributes( &attributes );
1701
itayzafrir3e02b3b2018-06-12 17:06:52 +03001702 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001703 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001704 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001705}
1706/* END_CASE */
1707
Gilles Peskine20035e32018-02-03 22:44:14 +01001708/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001709void import_and_exercise_key( data_t *data,
1710 int type_arg,
1711 int bits_arg,
1712 int alg_arg )
1713{
Ronald Cron5425a212020-08-04 14:58:35 +02001714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001715 psa_key_type_t type = type_arg;
1716 size_t bits = bits_arg;
1717 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001718 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001719 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001720 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001721
Gilles Peskine8817f612018-12-18 00:18:46 +01001722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001723
Gilles Peskine4747d192019-04-17 15:05:45 +02001724 psa_set_key_usage_flags( &attributes, usage );
1725 psa_set_key_algorithm( &attributes, alg );
1726 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001727
1728 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001729 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001730
1731 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001732 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001733 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1734 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001735
1736 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001737 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001738 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001739
Ronald Cron5425a212020-08-04 14:58:35 +02001740 PSA_ASSERT( psa_destroy_key( key ) );
1741 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001742
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001743exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001744 /*
1745 * Key attributes may have been returned by psa_get_key_attributes()
1746 * thus reset them as required.
1747 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001748 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001749
1750 psa_reset_key_attributes( &attributes );
1751 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001752 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001753}
1754/* END_CASE */
1755
1756/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001757void effective_key_attributes( int type_arg, int expected_type_arg,
1758 int bits_arg, int expected_bits_arg,
1759 int usage_arg, int expected_usage_arg,
1760 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001761{
Ronald Cron5425a212020-08-04 14:58:35 +02001762 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001763 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001764 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001765 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001766 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001767 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001768 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001769 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001770 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001772
Gilles Peskine8817f612018-12-18 00:18:46 +01001773 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001774
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001775 psa_set_key_usage_flags( &attributes, usage );
1776 psa_set_key_algorithm( &attributes, alg );
1777 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001778 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001779
Ronald Cron5425a212020-08-04 14:58:35 +02001780 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001781 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001782
Ronald Cron5425a212020-08-04 14:58:35 +02001783 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001784 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1785 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1786 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1787 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001788
1789exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001790 /*
1791 * Key attributes may have been returned by psa_get_key_attributes()
1792 * thus reset them as required.
1793 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001794 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001795
1796 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001797 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001798}
1799/* END_CASE */
1800
1801/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001802void check_key_policy( int type_arg, int bits_arg,
1803 int usage_arg, int alg_arg )
1804{
1805 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1806 usage_arg, usage_arg, alg_arg, alg_arg );
1807 goto exit;
1808}
1809/* END_CASE */
1810
1811/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001812void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001813{
1814 /* Test each valid way of initializing the object, except for `= {0}`, as
1815 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1816 * though it's OK by the C standard. We could test for this, but we'd need
1817 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001818 psa_key_attributes_t func = psa_key_attributes_init( );
1819 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1820 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001821
1822 memset( &zero, 0, sizeof( zero ) );
1823
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001824 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1825 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1826 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001827
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001828 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1829 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1830 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1831
1832 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1833 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1834 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1835
1836 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1837 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1838 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1839
1840 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1841 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1842 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001843}
1844/* END_CASE */
1845
1846/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001847void mac_key_policy( int policy_usage,
1848 int policy_alg,
1849 int key_type,
1850 data_t *key_data,
1851 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001852{
Ronald Cron5425a212020-08-04 14:58:35 +02001853 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001854 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001855 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001856 psa_status_t status;
1857 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001858
Gilles Peskine8817f612018-12-18 00:18:46 +01001859 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001860
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001861 psa_set_key_usage_flags( &attributes, policy_usage );
1862 psa_set_key_algorithm( &attributes, policy_alg );
1863 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001864
Gilles Peskine049c7532019-05-15 20:22:09 +02001865 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001866 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001867
Ronald Cron5425a212020-08-04 14:58:35 +02001868 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001869 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001870 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001871 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001872 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001873 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001874 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001875
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001876 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001877 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001878 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001879 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001880 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001881 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001882 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001883
1884exit:
1885 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001886 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001887 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001888}
1889/* END_CASE */
1890
1891/* BEGIN_CASE */
1892void cipher_key_policy( int policy_usage,
1893 int policy_alg,
1894 int key_type,
1895 data_t *key_data,
1896 int exercise_alg )
1897{
Ronald Cron5425a212020-08-04 14:58:35 +02001898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001899 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001900 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001901 psa_status_t status;
1902
Gilles Peskine8817f612018-12-18 00:18:46 +01001903 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001904
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001905 psa_set_key_usage_flags( &attributes, policy_usage );
1906 psa_set_key_algorithm( &attributes, policy_alg );
1907 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001908
Gilles Peskine049c7532019-05-15 20:22:09 +02001909 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001910 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911
Ronald Cron5425a212020-08-04 14:58:35 +02001912 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001913 if( policy_alg == exercise_alg &&
1914 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001915 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001916 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001917 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918 psa_cipher_abort( &operation );
1919
Ronald Cron5425a212020-08-04 14:58:35 +02001920 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921 if( policy_alg == exercise_alg &&
1922 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001923 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001925 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001926
1927exit:
1928 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001929 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001930 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931}
1932/* END_CASE */
1933
1934/* BEGIN_CASE */
1935void aead_key_policy( int policy_usage,
1936 int policy_alg,
1937 int key_type,
1938 data_t *key_data,
1939 int nonce_length_arg,
1940 int tag_length_arg,
1941 int exercise_alg )
1942{
Ronald Cron5425a212020-08-04 14:58:35 +02001943 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001944 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001945 psa_status_t status;
1946 unsigned char nonce[16] = {0};
1947 size_t nonce_length = nonce_length_arg;
1948 unsigned char tag[16];
1949 size_t tag_length = tag_length_arg;
1950 size_t output_length;
1951
1952 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1953 TEST_ASSERT( tag_length <= sizeof( tag ) );
1954
Gilles Peskine8817f612018-12-18 00:18:46 +01001955 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001957 psa_set_key_usage_flags( &attributes, policy_usage );
1958 psa_set_key_algorithm( &attributes, policy_alg );
1959 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001960
Gilles Peskine049c7532019-05-15 20:22:09 +02001961 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001962 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001963
Ronald Cron5425a212020-08-04 14:58:35 +02001964 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001965 nonce, nonce_length,
1966 NULL, 0,
1967 NULL, 0,
1968 tag, tag_length,
1969 &output_length );
1970 if( policy_alg == exercise_alg &&
1971 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001972 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001973 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001974 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001975
1976 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001977 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001978 nonce, nonce_length,
1979 NULL, 0,
1980 tag, tag_length,
1981 NULL, 0,
1982 &output_length );
1983 if( policy_alg == exercise_alg &&
1984 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001985 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001987 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001988
1989exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001990 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001991 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001992}
1993/* END_CASE */
1994
1995/* BEGIN_CASE */
1996void asymmetric_encryption_key_policy( int policy_usage,
1997 int policy_alg,
1998 int key_type,
1999 data_t *key_data,
2000 int exercise_alg )
2001{
Ronald Cron5425a212020-08-04 14:58:35 +02002002 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004 psa_status_t status;
2005 size_t key_bits;
2006 size_t buffer_length;
2007 unsigned char *buffer = NULL;
2008 size_t output_length;
2009
Gilles Peskine8817f612018-12-18 00:18:46 +01002010 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002011
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002012 psa_set_key_usage_flags( &attributes, policy_usage );
2013 psa_set_key_algorithm( &attributes, policy_alg );
2014 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002015
Gilles Peskine049c7532019-05-15 20:22:09 +02002016 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002017 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002018
Ronald Cron5425a212020-08-04 14:58:35 +02002019 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002020 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2022 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002023 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024
Ronald Cron5425a212020-08-04 14:58:35 +02002025 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002026 NULL, 0,
2027 NULL, 0,
2028 buffer, buffer_length,
2029 &output_length );
2030 if( policy_alg == exercise_alg &&
2031 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002032 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002034 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002036 if( buffer_length != 0 )
2037 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002038 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039 buffer, buffer_length,
2040 NULL, 0,
2041 buffer, buffer_length,
2042 &output_length );
2043 if( policy_alg == exercise_alg &&
2044 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002045 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002047 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002048
2049exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002050 /*
2051 * Key attributes may have been returned by psa_get_key_attributes()
2052 * thus reset them as required.
2053 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002054 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002055
2056 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002057 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002058 mbedtls_free( buffer );
2059}
2060/* END_CASE */
2061
2062/* BEGIN_CASE */
2063void asymmetric_signature_key_policy( int policy_usage,
2064 int policy_alg,
2065 int key_type,
2066 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002067 int exercise_alg,
2068 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002069{
Ronald Cron5425a212020-08-04 14:58:35 +02002070 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002072 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002073 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2074 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2075 * compatible with the policy and `payload_length_arg` is supposed to be
2076 * a valid input length to sign. If `payload_length_arg <= 0`,
2077 * `exercise_alg` is supposed to be forbidden by the policy. */
2078 int compatible_alg = payload_length_arg > 0;
2079 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002080 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081 size_t signature_length;
2082
Gilles Peskine8817f612018-12-18 00:18:46 +01002083 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002085 psa_set_key_usage_flags( &attributes, policy_usage );
2086 psa_set_key_algorithm( &attributes, policy_alg );
2087 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002088
Gilles Peskine049c7532019-05-15 20:22:09 +02002089 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002090 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002091
Ronald Cron5425a212020-08-04 14:58:35 +02002092 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002093 payload, payload_length,
2094 signature, sizeof( signature ),
2095 &signature_length );
2096 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002097 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002098 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002099 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100
2101 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002102 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002103 payload, payload_length,
2104 signature, sizeof( signature ) );
2105 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002106 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002107 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002108 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002109
2110exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002111 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002112 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002113}
2114/* END_CASE */
2115
Janos Follathba3fab92019-06-11 14:50:16 +01002116/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002117void derive_key_policy( int policy_usage,
2118 int policy_alg,
2119 int key_type,
2120 data_t *key_data,
2121 int exercise_alg )
2122{
Ronald Cron5425a212020-08-04 14:58:35 +02002123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002125 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002126 psa_status_t status;
2127
Gilles Peskine8817f612018-12-18 00:18:46 +01002128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002130 psa_set_key_usage_flags( &attributes, policy_usage );
2131 psa_set_key_algorithm( &attributes, policy_alg );
2132 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002133
Gilles Peskine049c7532019-05-15 20:22:09 +02002134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002135 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002136
Janos Follathba3fab92019-06-11 14:50:16 +01002137 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2138
2139 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2140 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002141 {
Janos Follathba3fab92019-06-11 14:50:16 +01002142 PSA_ASSERT( psa_key_derivation_input_bytes(
2143 &operation,
2144 PSA_KEY_DERIVATION_INPUT_SEED,
2145 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002146 }
Janos Follathba3fab92019-06-11 14:50:16 +01002147
2148 status = psa_key_derivation_input_key( &operation,
2149 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002150 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002151
Gilles Peskineea0fb492018-07-12 17:17:20 +02002152 if( policy_alg == exercise_alg &&
2153 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002154 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002155 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002156 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002157
2158exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002159 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002160 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002161 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002162}
2163/* END_CASE */
2164
2165/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002166void agreement_key_policy( int policy_usage,
2167 int policy_alg,
2168 int key_type_arg,
2169 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002170 int exercise_alg,
2171 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002172{
Ronald Cron5425a212020-08-04 14:58:35 +02002173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002175 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002176 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002177 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002178 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002179
Gilles Peskine8817f612018-12-18 00:18:46 +01002180 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002181
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002182 psa_set_key_usage_flags( &attributes, policy_usage );
2183 psa_set_key_algorithm( &attributes, policy_alg );
2184 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002185
Gilles Peskine049c7532019-05-15 20:22:09 +02002186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002187 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002188
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002189 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002190 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002191
Steven Cooremance48e852020-10-05 16:02:45 +02002192 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002193
2194exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002195 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002196 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002197 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002198}
2199/* END_CASE */
2200
2201/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002202void key_policy_alg2( int key_type_arg, data_t *key_data,
2203 int usage_arg, int alg_arg, int alg2_arg )
2204{
Ronald Cron5425a212020-08-04 14:58:35 +02002205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002206 psa_key_type_t key_type = key_type_arg;
2207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2208 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2209 psa_key_usage_t usage = usage_arg;
2210 psa_algorithm_t alg = alg_arg;
2211 psa_algorithm_t alg2 = alg2_arg;
2212
2213 PSA_ASSERT( psa_crypto_init( ) );
2214
2215 psa_set_key_usage_flags( &attributes, usage );
2216 psa_set_key_algorithm( &attributes, alg );
2217 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2218 psa_set_key_type( &attributes, key_type );
2219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002220 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002221
Ronald Cron5425a212020-08-04 14:58:35 +02002222 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002223 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2224 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2225 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2226
Ronald Cron5425a212020-08-04 14:58:35 +02002227 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002228 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002229 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002230 goto exit;
2231
2232exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002233 /*
2234 * Key attributes may have been returned by psa_get_key_attributes()
2235 * thus reset them as required.
2236 */
2237 psa_reset_key_attributes( &got_attributes );
2238
Ronald Cron5425a212020-08-04 14:58:35 +02002239 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002240 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002241}
2242/* END_CASE */
2243
2244/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002245void raw_agreement_key_policy( int policy_usage,
2246 int policy_alg,
2247 int key_type_arg,
2248 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002249 int exercise_alg,
2250 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002251{
Ronald Cron5425a212020-08-04 14:58:35 +02002252 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002254 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002255 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002256 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002257 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002258
2259 PSA_ASSERT( psa_crypto_init( ) );
2260
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002261 psa_set_key_usage_flags( &attributes, policy_usage );
2262 psa_set_key_algorithm( &attributes, policy_alg );
2263 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002264
Gilles Peskine049c7532019-05-15 20:22:09 +02002265 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002266 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002267
Ronald Cron5425a212020-08-04 14:58:35 +02002268 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002269
Steven Cooremance48e852020-10-05 16:02:45 +02002270 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002271
2272exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002273 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002274 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002275 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002276}
2277/* END_CASE */
2278
2279/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002280void copy_success( int source_usage_arg,
2281 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002282 int type_arg, data_t *material,
2283 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002284 int target_usage_arg,
2285 int target_alg_arg, int target_alg2_arg,
2286 int expected_usage_arg,
2287 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002288{
Gilles Peskineca25db92019-04-19 11:43:08 +02002289 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2290 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002291 psa_key_usage_t expected_usage = expected_usage_arg;
2292 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002293 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002294 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2295 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002296 uint8_t *export_buffer = NULL;
2297
Gilles Peskine57ab7212019-01-28 13:03:09 +01002298 PSA_ASSERT( psa_crypto_init( ) );
2299
Gilles Peskineca25db92019-04-19 11:43:08 +02002300 /* Prepare the source key. */
2301 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2302 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002303 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002304 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002305 PSA_ASSERT( psa_import_key( &source_attributes,
2306 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002307 &source_key ) );
2308 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002309
Gilles Peskineca25db92019-04-19 11:43:08 +02002310 /* Prepare the target attributes. */
2311 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002312 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002313 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002314 /* Set volatile lifetime to reset the key identifier to 0. */
2315 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2316 }
2317
Gilles Peskineca25db92019-04-19 11:43:08 +02002318 if( target_usage_arg != -1 )
2319 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2320 if( target_alg_arg != -1 )
2321 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002322 if( target_alg2_arg != -1 )
2323 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002324
2325 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002326 PSA_ASSERT( psa_copy_key( source_key,
2327 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002328
2329 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002330 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002331
2332 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002333 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002334 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2335 psa_get_key_type( &target_attributes ) );
2336 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2337 psa_get_key_bits( &target_attributes ) );
2338 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2339 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002340 TEST_EQUAL( expected_alg2,
2341 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002342 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2343 {
2344 size_t length;
2345 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002346 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002347 material->len, &length ) );
2348 ASSERT_COMPARE( material->x, material->len,
2349 export_buffer, length );
2350 }
Ronald Cron5425a212020-08-04 14:58:35 +02002351 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002352 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002353 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002354 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002355
Ronald Cron5425a212020-08-04 14:58:35 +02002356 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002357
2358exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002359 /*
2360 * Source and target key attributes may have been returned by
2361 * psa_get_key_attributes() thus reset them as required.
2362 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002363 psa_reset_key_attributes( &source_attributes );
2364 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002365
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002366 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002367 mbedtls_free( export_buffer );
2368}
2369/* END_CASE */
2370
2371/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002372void copy_fail( int source_usage_arg,
2373 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002374 int type_arg, data_t *material,
2375 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002376 int target_usage_arg,
2377 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002378 int expected_status_arg )
2379{
2380 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2381 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002382 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2383 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002384
2385 PSA_ASSERT( psa_crypto_init( ) );
2386
2387 /* Prepare the source key. */
2388 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2389 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002390 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002391 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002392 PSA_ASSERT( psa_import_key( &source_attributes,
2393 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002394 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002395
2396 /* Prepare the target attributes. */
2397 psa_set_key_type( &target_attributes, target_type_arg );
2398 psa_set_key_bits( &target_attributes, target_bits_arg );
2399 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2400 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002401 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002402
2403 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002404 TEST_EQUAL( psa_copy_key( source_key,
2405 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002406 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002407
Ronald Cron5425a212020-08-04 14:58:35 +02002408 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002409
Gilles Peskine4a644642019-05-03 17:14:08 +02002410exit:
2411 psa_reset_key_attributes( &source_attributes );
2412 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002413 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002414}
2415/* END_CASE */
2416
2417/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002418void hash_operation_init( )
2419{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002420 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002421 /* Test each valid way of initializing the object, except for `= {0}`, as
2422 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2423 * though it's OK by the C standard. We could test for this, but we'd need
2424 * to supress the Clang warning for the test. */
2425 psa_hash_operation_t func = psa_hash_operation_init( );
2426 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2427 psa_hash_operation_t zero;
2428
2429 memset( &zero, 0, sizeof( zero ) );
2430
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002431 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002432 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2433 PSA_ERROR_BAD_STATE );
2434 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2435 PSA_ERROR_BAD_STATE );
2436 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2437 PSA_ERROR_BAD_STATE );
2438
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002439 /* A default hash operation should be abortable without error. */
2440 PSA_ASSERT( psa_hash_abort( &func ) );
2441 PSA_ASSERT( psa_hash_abort( &init ) );
2442 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002443}
2444/* END_CASE */
2445
2446/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002447void hash_setup( int alg_arg,
2448 int expected_status_arg )
2449{
2450 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002451 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002452 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002453 psa_status_t status;
2454
Gilles Peskine8817f612018-12-18 00:18:46 +01002455 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002456
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002457 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002458 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002459
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002460 /* Whether setup succeeded or failed, abort must succeed. */
2461 PSA_ASSERT( psa_hash_abort( &operation ) );
2462
2463 /* If setup failed, reproduce the failure, so as to
2464 * test the resulting state of the operation object. */
2465 if( status != PSA_SUCCESS )
2466 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2467
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002468 /* Now the operation object should be reusable. */
2469#if defined(KNOWN_SUPPORTED_HASH_ALG)
2470 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2471 PSA_ASSERT( psa_hash_abort( &operation ) );
2472#endif
2473
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002474exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002475 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476}
2477/* END_CASE */
2478
2479/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002480void hash_compute_fail( int alg_arg, data_t *input,
2481 int output_size_arg, int expected_status_arg )
2482{
2483 psa_algorithm_t alg = alg_arg;
2484 uint8_t *output = NULL;
2485 size_t output_size = output_size_arg;
2486 size_t output_length = INVALID_EXPORT_LENGTH;
2487 psa_status_t expected_status = expected_status_arg;
2488 psa_status_t status;
2489
2490 ASSERT_ALLOC( output, output_size );
2491
2492 PSA_ASSERT( psa_crypto_init( ) );
2493
2494 status = psa_hash_compute( alg, input->x, input->len,
2495 output, output_size, &output_length );
2496 TEST_EQUAL( status, expected_status );
2497 TEST_ASSERT( output_length <= output_size );
2498
2499exit:
2500 mbedtls_free( output );
2501 PSA_DONE( );
2502}
2503/* END_CASE */
2504
2505/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002506void hash_compare_fail( int alg_arg, data_t *input,
2507 data_t *reference_hash,
2508 int expected_status_arg )
2509{
2510 psa_algorithm_t alg = alg_arg;
2511 psa_status_t expected_status = expected_status_arg;
2512 psa_status_t status;
2513
2514 PSA_ASSERT( psa_crypto_init( ) );
2515
2516 status = psa_hash_compare( alg, input->x, input->len,
2517 reference_hash->x, reference_hash->len );
2518 TEST_EQUAL( status, expected_status );
2519
2520exit:
2521 PSA_DONE( );
2522}
2523/* END_CASE */
2524
2525/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002526void hash_compute_compare( int alg_arg, data_t *input,
2527 data_t *expected_output )
2528{
2529 psa_algorithm_t alg = alg_arg;
2530 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2531 size_t output_length = INVALID_EXPORT_LENGTH;
2532 size_t i;
2533
2534 PSA_ASSERT( psa_crypto_init( ) );
2535
2536 /* Compute with tight buffer */
2537 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002538 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002539 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002540 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002541 ASSERT_COMPARE( output, output_length,
2542 expected_output->x, expected_output->len );
2543
2544 /* Compute with larger buffer */
2545 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2546 output, sizeof( output ),
2547 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002548 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002549 ASSERT_COMPARE( output, output_length,
2550 expected_output->x, expected_output->len );
2551
2552 /* Compare with correct hash */
2553 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2554 output, output_length ) );
2555
2556 /* Compare with trailing garbage */
2557 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2558 output, output_length + 1 ),
2559 PSA_ERROR_INVALID_SIGNATURE );
2560
2561 /* Compare with truncated hash */
2562 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2563 output, output_length - 1 ),
2564 PSA_ERROR_INVALID_SIGNATURE );
2565
2566 /* Compare with corrupted value */
2567 for( i = 0; i < output_length; i++ )
2568 {
Chris Jones9634bb12021-01-20 15:56:42 +00002569 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002570 output[i] ^= 1;
2571 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2572 output, output_length ),
2573 PSA_ERROR_INVALID_SIGNATURE );
2574 output[i] ^= 1;
2575 }
2576
2577exit:
2578 PSA_DONE( );
2579}
2580/* END_CASE */
2581
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002582/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002583void hash_bad_order( )
2584{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002585 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002586 unsigned char input[] = "";
2587 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002588 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002589 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2590 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2591 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002592 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002593 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002594 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002595
Gilles Peskine8817f612018-12-18 00:18:46 +01002596 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002597
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002598 /* Call setup twice in a row. */
2599 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2600 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2601 PSA_ERROR_BAD_STATE );
2602 PSA_ASSERT( psa_hash_abort( &operation ) );
2603
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002604 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002605 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002606 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002607 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002608
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002609 /* Call update after finish. */
2610 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2611 PSA_ASSERT( psa_hash_finish( &operation,
2612 hash, sizeof( hash ), &hash_len ) );
2613 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002614 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002615 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002616
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002617 /* Call verify without calling setup beforehand. */
2618 TEST_EQUAL( psa_hash_verify( &operation,
2619 valid_hash, sizeof( valid_hash ) ),
2620 PSA_ERROR_BAD_STATE );
2621 PSA_ASSERT( psa_hash_abort( &operation ) );
2622
2623 /* Call verify after finish. */
2624 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2625 PSA_ASSERT( psa_hash_finish( &operation,
2626 hash, sizeof( hash ), &hash_len ) );
2627 TEST_EQUAL( psa_hash_verify( &operation,
2628 valid_hash, sizeof( valid_hash ) ),
2629 PSA_ERROR_BAD_STATE );
2630 PSA_ASSERT( psa_hash_abort( &operation ) );
2631
2632 /* Call verify twice in a row. */
2633 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2634 PSA_ASSERT( psa_hash_verify( &operation,
2635 valid_hash, sizeof( valid_hash ) ) );
2636 TEST_EQUAL( psa_hash_verify( &operation,
2637 valid_hash, sizeof( valid_hash ) ),
2638 PSA_ERROR_BAD_STATE );
2639 PSA_ASSERT( psa_hash_abort( &operation ) );
2640
2641 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002642 TEST_EQUAL( psa_hash_finish( &operation,
2643 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002644 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002645 PSA_ASSERT( psa_hash_abort( &operation ) );
2646
2647 /* Call finish twice in a row. */
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_finish( &operation,
2652 hash, sizeof( hash ), &hash_len ),
2653 PSA_ERROR_BAD_STATE );
2654 PSA_ASSERT( psa_hash_abort( &operation ) );
2655
2656 /* Call finish after calling verify. */
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_finish( &operation,
2661 hash, sizeof( hash ), &hash_len ),
2662 PSA_ERROR_BAD_STATE );
2663 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002664
2665exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002666 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002667}
2668/* END_CASE */
2669
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002670/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002671void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002672{
2673 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002674 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2675 * appended to it */
2676 unsigned char hash[] = {
2677 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2678 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2679 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002680 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002681 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002682
Gilles Peskine8817f612018-12-18 00:18:46 +01002683 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002684
itayzafrir27e69452018-11-01 14:26:34 +02002685 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002686 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002687 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002688 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002689
itayzafrir27e69452018-11-01 14:26:34 +02002690 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002691 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002692 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002693 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002694
itayzafrir27e69452018-11-01 14:26:34 +02002695 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002696 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002697 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002698 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002699
itayzafrirec93d302018-10-18 18:01:10 +03002700exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002701 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002702}
2703/* END_CASE */
2704
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002705/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2706void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002707{
2708 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002709 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002710 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002711 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002712 size_t hash_len;
2713
Gilles Peskine8817f612018-12-18 00:18:46 +01002714 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002715
itayzafrir58028322018-10-25 10:22:01 +03002716 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002718 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002719 hash, expected_size - 1, &hash_len ),
2720 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002721
2722exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002723 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002724}
2725/* END_CASE */
2726
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002727/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2728void hash_clone_source_state( )
2729{
2730 psa_algorithm_t alg = PSA_ALG_SHA_256;
2731 unsigned char hash[PSA_HASH_MAX_SIZE];
2732 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2733 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2734 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2735 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2736 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2737 size_t hash_len;
2738
2739 PSA_ASSERT( psa_crypto_init( ) );
2740 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2741
2742 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2743 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2744 PSA_ASSERT( psa_hash_finish( &op_finished,
2745 hash, sizeof( hash ), &hash_len ) );
2746 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2747 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2748
2749 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2750 PSA_ERROR_BAD_STATE );
2751
2752 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2753 PSA_ASSERT( psa_hash_finish( &op_init,
2754 hash, sizeof( hash ), &hash_len ) );
2755 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2756 PSA_ASSERT( psa_hash_finish( &op_finished,
2757 hash, sizeof( hash ), &hash_len ) );
2758 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2759 PSA_ASSERT( psa_hash_finish( &op_aborted,
2760 hash, sizeof( hash ), &hash_len ) );
2761
2762exit:
2763 psa_hash_abort( &op_source );
2764 psa_hash_abort( &op_init );
2765 psa_hash_abort( &op_setup );
2766 psa_hash_abort( &op_finished );
2767 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002768 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002769}
2770/* END_CASE */
2771
2772/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2773void hash_clone_target_state( )
2774{
2775 psa_algorithm_t alg = PSA_ALG_SHA_256;
2776 unsigned char hash[PSA_HASH_MAX_SIZE];
2777 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2778 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2779 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2780 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2781 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2782 size_t hash_len;
2783
2784 PSA_ASSERT( psa_crypto_init( ) );
2785
2786 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2787 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2788 PSA_ASSERT( psa_hash_finish( &op_finished,
2789 hash, sizeof( hash ), &hash_len ) );
2790 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2791 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2792
2793 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2794 PSA_ASSERT( psa_hash_finish( &op_target,
2795 hash, sizeof( hash ), &hash_len ) );
2796
2797 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2798 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2799 PSA_ERROR_BAD_STATE );
2800 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2801 PSA_ERROR_BAD_STATE );
2802
2803exit:
2804 psa_hash_abort( &op_target );
2805 psa_hash_abort( &op_init );
2806 psa_hash_abort( &op_setup );
2807 psa_hash_abort( &op_finished );
2808 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002809 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002810}
2811/* END_CASE */
2812
itayzafrir58028322018-10-25 10:22:01 +03002813/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002814void mac_operation_init( )
2815{
Jaeden Amero252ef282019-02-15 14:05:35 +00002816 const uint8_t input[1] = { 0 };
2817
Jaeden Amero769ce272019-01-04 11:48:03 +00002818 /* Test each valid way of initializing the object, except for `= {0}`, as
2819 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2820 * though it's OK by the C standard. We could test for this, but we'd need
2821 * to supress the Clang warning for the test. */
2822 psa_mac_operation_t func = psa_mac_operation_init( );
2823 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2824 psa_mac_operation_t zero;
2825
2826 memset( &zero, 0, sizeof( zero ) );
2827
Jaeden Amero252ef282019-02-15 14:05:35 +00002828 /* A freshly-initialized MAC operation should not be usable. */
2829 TEST_EQUAL( psa_mac_update( &func,
2830 input, sizeof( input ) ),
2831 PSA_ERROR_BAD_STATE );
2832 TEST_EQUAL( psa_mac_update( &init,
2833 input, sizeof( input ) ),
2834 PSA_ERROR_BAD_STATE );
2835 TEST_EQUAL( psa_mac_update( &zero,
2836 input, sizeof( input ) ),
2837 PSA_ERROR_BAD_STATE );
2838
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002839 /* A default MAC operation should be abortable without error. */
2840 PSA_ASSERT( psa_mac_abort( &func ) );
2841 PSA_ASSERT( psa_mac_abort( &init ) );
2842 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002843}
2844/* END_CASE */
2845
2846/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002847void mac_setup( int key_type_arg,
2848 data_t *key,
2849 int alg_arg,
2850 int expected_status_arg )
2851{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002852 psa_key_type_t key_type = key_type_arg;
2853 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002854 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002855 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002856 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2857#if defined(KNOWN_SUPPORTED_MAC_ALG)
2858 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2859#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002860
Gilles Peskine8817f612018-12-18 00:18:46 +01002861 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002862
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002863 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2864 &operation, &status ) )
2865 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002866 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002867
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002868 /* The operation object should be reusable. */
2869#if defined(KNOWN_SUPPORTED_MAC_ALG)
2870 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2871 smoke_test_key_data,
2872 sizeof( smoke_test_key_data ),
2873 KNOWN_SUPPORTED_MAC_ALG,
2874 &operation, &status ) )
2875 goto exit;
2876 TEST_EQUAL( status, PSA_SUCCESS );
2877#endif
2878
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002879exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002880 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002881}
2882/* END_CASE */
2883
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002884/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00002885void mac_bad_order( )
2886{
Ronald Cron5425a212020-08-04 14:58:35 +02002887 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002888 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2889 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002890 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002891 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2892 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2893 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002895 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2896 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2897 size_t sign_mac_length = 0;
2898 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2899 const uint8_t verify_mac[] = {
2900 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2901 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2902 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2903
2904 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002905 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002906 psa_set_key_algorithm( &attributes, alg );
2907 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002908
Ronald Cron5425a212020-08-04 14:58:35 +02002909 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2910 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002911
Jaeden Amero252ef282019-02-15 14:05:35 +00002912 /* Call update without calling setup beforehand. */
2913 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2914 PSA_ERROR_BAD_STATE );
2915 PSA_ASSERT( psa_mac_abort( &operation ) );
2916
2917 /* Call sign finish without calling setup beforehand. */
2918 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2919 &sign_mac_length),
2920 PSA_ERROR_BAD_STATE );
2921 PSA_ASSERT( psa_mac_abort( &operation ) );
2922
2923 /* Call verify finish without calling setup beforehand. */
2924 TEST_EQUAL( psa_mac_verify_finish( &operation,
2925 verify_mac, sizeof( verify_mac ) ),
2926 PSA_ERROR_BAD_STATE );
2927 PSA_ASSERT( psa_mac_abort( &operation ) );
2928
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002929 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002930 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2931 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002932 PSA_ERROR_BAD_STATE );
2933 PSA_ASSERT( psa_mac_abort( &operation ) );
2934
Jaeden Amero252ef282019-02-15 14:05:35 +00002935 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002936 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002937 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2938 PSA_ASSERT( psa_mac_sign_finish( &operation,
2939 sign_mac, sizeof( sign_mac ),
2940 &sign_mac_length ) );
2941 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2942 PSA_ERROR_BAD_STATE );
2943 PSA_ASSERT( psa_mac_abort( &operation ) );
2944
2945 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002946 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002947 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2948 PSA_ASSERT( psa_mac_verify_finish( &operation,
2949 verify_mac, sizeof( verify_mac ) ) );
2950 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2951 PSA_ERROR_BAD_STATE );
2952 PSA_ASSERT( psa_mac_abort( &operation ) );
2953
2954 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002955 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002956 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2957 PSA_ASSERT( psa_mac_sign_finish( &operation,
2958 sign_mac, sizeof( sign_mac ),
2959 &sign_mac_length ) );
2960 TEST_EQUAL( psa_mac_sign_finish( &operation,
2961 sign_mac, sizeof( sign_mac ),
2962 &sign_mac_length ),
2963 PSA_ERROR_BAD_STATE );
2964 PSA_ASSERT( psa_mac_abort( &operation ) );
2965
2966 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002967 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002968 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2969 PSA_ASSERT( psa_mac_verify_finish( &operation,
2970 verify_mac, sizeof( verify_mac ) ) );
2971 TEST_EQUAL( psa_mac_verify_finish( &operation,
2972 verify_mac, sizeof( verify_mac ) ),
2973 PSA_ERROR_BAD_STATE );
2974 PSA_ASSERT( psa_mac_abort( &operation ) );
2975
2976 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002977 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002978 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2979 TEST_EQUAL( psa_mac_verify_finish( &operation,
2980 verify_mac, sizeof( verify_mac ) ),
2981 PSA_ERROR_BAD_STATE );
2982 PSA_ASSERT( psa_mac_abort( &operation ) );
2983
2984 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002985 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002986 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2987 TEST_EQUAL( psa_mac_sign_finish( &operation,
2988 sign_mac, sizeof( sign_mac ),
2989 &sign_mac_length ),
2990 PSA_ERROR_BAD_STATE );
2991 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002992
Ronald Cron5425a212020-08-04 14:58:35 +02002993 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002994
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002995exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002996 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002997}
2998/* END_CASE */
2999
3000/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003001void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003002 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003003 int alg_arg,
3004 data_t *input,
3005 data_t *expected_mac )
3006{
Ronald Cron5425a212020-08-04 14:58:35 +02003007 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003008 psa_key_type_t key_type = key_type_arg;
3009 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003010 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003012 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003013 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003014 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003015 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003016 const size_t output_sizes_to_test[] = {
3017 0,
3018 1,
3019 expected_mac->len - 1,
3020 expected_mac->len,
3021 expected_mac->len + 1,
3022 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003023
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003024 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003025 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003026 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003027
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003029
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003030 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003031 psa_set_key_algorithm( &attributes, alg );
3032 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003033
Ronald Cron5425a212020-08-04 14:58:35 +02003034 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3035 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003036
Gilles Peskine8b356b52020-08-25 23:44:59 +02003037 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3038 {
3039 const size_t output_size = output_sizes_to_test[i];
3040 psa_status_t expected_status =
3041 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3042 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003043
Chris Jones9634bb12021-01-20 15:56:42 +00003044 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003045 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003046
Gilles Peskine8b356b52020-08-25 23:44:59 +02003047 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003048 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003049 PSA_ASSERT( psa_mac_update( &operation,
3050 input->x, input->len ) );
3051 TEST_EQUAL( psa_mac_sign_finish( &operation,
3052 actual_mac, output_size,
3053 &mac_length ),
3054 expected_status );
3055 PSA_ASSERT( psa_mac_abort( &operation ) );
3056
3057 if( expected_status == PSA_SUCCESS )
3058 {
3059 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3060 actual_mac, mac_length );
3061 }
3062 mbedtls_free( actual_mac );
3063 actual_mac = NULL;
3064 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003065
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003066exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003067 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003068 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003069 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003070 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003071}
3072/* END_CASE */
3073
3074/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003075void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003076 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003077 int alg_arg,
3078 data_t *input,
3079 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003080{
Ronald Cron5425a212020-08-04 14:58:35 +02003081 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003082 psa_key_type_t key_type = key_type_arg;
3083 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003084 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003086 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003087
Gilles Peskine69c12672018-06-28 00:07:19 +02003088 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3089
Gilles Peskine8817f612018-12-18 00:18:46 +01003090 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003092 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003093 psa_set_key_algorithm( &attributes, alg );
3094 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003095
Ronald Cron5425a212020-08-04 14:58:35 +02003096 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3097 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003098
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003099 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003100 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003101 PSA_ASSERT( psa_mac_update( &operation,
3102 input->x, input->len ) );
3103 PSA_ASSERT( psa_mac_verify_finish( &operation,
3104 expected_mac->x,
3105 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003106
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003107 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003108 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003109 PSA_ASSERT( psa_mac_update( &operation,
3110 input->x, input->len ) );
3111 TEST_EQUAL( psa_mac_verify_finish( &operation,
3112 expected_mac->x,
3113 expected_mac->len - 1 ),
3114 PSA_ERROR_INVALID_SIGNATURE );
3115
3116 /* Test a MAC that's too long. */
3117 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3118 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003119 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003120 PSA_ASSERT( psa_mac_update( &operation,
3121 input->x, input->len ) );
3122 TEST_EQUAL( psa_mac_verify_finish( &operation,
3123 perturbed_mac,
3124 expected_mac->len + 1 ),
3125 PSA_ERROR_INVALID_SIGNATURE );
3126
3127 /* Test changing one byte. */
3128 for( size_t i = 0; i < expected_mac->len; i++ )
3129 {
Chris Jones9634bb12021-01-20 15:56:42 +00003130 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003131 perturbed_mac[i] ^= 1;
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 perturbed_mac,
3137 expected_mac->len ),
3138 PSA_ERROR_INVALID_SIGNATURE );
3139 perturbed_mac[i] ^= 1;
3140 }
3141
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003143 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003144 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003145 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003146 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003147}
3148/* END_CASE */
3149
3150/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003151void cipher_operation_init( )
3152{
Jaeden Ameroab439972019-02-15 14:12:05 +00003153 const uint8_t input[1] = { 0 };
3154 unsigned char output[1] = { 0 };
3155 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003156 /* Test each valid way of initializing the object, except for `= {0}`, as
3157 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3158 * though it's OK by the C standard. We could test for this, but we'd need
3159 * to supress the Clang warning for the test. */
3160 psa_cipher_operation_t func = psa_cipher_operation_init( );
3161 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3162 psa_cipher_operation_t zero;
3163
3164 memset( &zero, 0, sizeof( zero ) );
3165
Jaeden Ameroab439972019-02-15 14:12:05 +00003166 /* A freshly-initialized cipher operation should not be usable. */
3167 TEST_EQUAL( psa_cipher_update( &func,
3168 input, sizeof( input ),
3169 output, sizeof( output ),
3170 &output_length ),
3171 PSA_ERROR_BAD_STATE );
3172 TEST_EQUAL( psa_cipher_update( &init,
3173 input, sizeof( input ),
3174 output, sizeof( output ),
3175 &output_length ),
3176 PSA_ERROR_BAD_STATE );
3177 TEST_EQUAL( psa_cipher_update( &zero,
3178 input, sizeof( input ),
3179 output, sizeof( output ),
3180 &output_length ),
3181 PSA_ERROR_BAD_STATE );
3182
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003183 /* A default cipher operation should be abortable without error. */
3184 PSA_ASSERT( psa_cipher_abort( &func ) );
3185 PSA_ASSERT( psa_cipher_abort( &init ) );
3186 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003187}
3188/* END_CASE */
3189
3190/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003191void cipher_setup( int key_type_arg,
3192 data_t *key,
3193 int alg_arg,
3194 int expected_status_arg )
3195{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003196 psa_key_type_t key_type = key_type_arg;
3197 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003198 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003199 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003200 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003201#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003202 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3203#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003204
Gilles Peskine8817f612018-12-18 00:18:46 +01003205 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003206
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003207 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3208 &operation, &status ) )
3209 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003210 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003211
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003212 /* The operation object should be reusable. */
3213#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3214 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3215 smoke_test_key_data,
3216 sizeof( smoke_test_key_data ),
3217 KNOWN_SUPPORTED_CIPHER_ALG,
3218 &operation, &status ) )
3219 goto exit;
3220 TEST_EQUAL( status, PSA_SUCCESS );
3221#endif
3222
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003223exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003224 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003225 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003226}
3227/* END_CASE */
3228
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003229/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003230void cipher_bad_order( )
3231{
Ronald Cron5425a212020-08-04 14:58:35 +02003232 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003233 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3234 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003236 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003237 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003238 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003239 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3240 0xaa, 0xaa, 0xaa, 0xaa };
3241 const uint8_t text[] = {
3242 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3243 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003244 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003245 size_t length = 0;
3246
3247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003248 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3249 psa_set_key_algorithm( &attributes, alg );
3250 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003251 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3252 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003253
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003254 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003255 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3256 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003257 PSA_ERROR_BAD_STATE );
3258 PSA_ASSERT( psa_cipher_abort( &operation ) );
3259
3260 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003261 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3262 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003263 PSA_ERROR_BAD_STATE );
3264 PSA_ASSERT( psa_cipher_abort( &operation ) );
3265
Jaeden Ameroab439972019-02-15 14:12:05 +00003266 /* Generate an IV without calling setup beforehand. */
3267 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3268 buffer, sizeof( buffer ),
3269 &length ),
3270 PSA_ERROR_BAD_STATE );
3271 PSA_ASSERT( psa_cipher_abort( &operation ) );
3272
3273 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003274 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003275 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3276 buffer, sizeof( buffer ),
3277 &length ) );
3278 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3279 buffer, sizeof( buffer ),
3280 &length ),
3281 PSA_ERROR_BAD_STATE );
3282 PSA_ASSERT( psa_cipher_abort( &operation ) );
3283
3284 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003285 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003286 PSA_ASSERT( psa_cipher_set_iv( &operation,
3287 iv, sizeof( iv ) ) );
3288 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3289 buffer, sizeof( buffer ),
3290 &length ),
3291 PSA_ERROR_BAD_STATE );
3292 PSA_ASSERT( psa_cipher_abort( &operation ) );
3293
3294 /* Set an IV without calling setup beforehand. */
3295 TEST_EQUAL( psa_cipher_set_iv( &operation,
3296 iv, sizeof( iv ) ),
3297 PSA_ERROR_BAD_STATE );
3298 PSA_ASSERT( psa_cipher_abort( &operation ) );
3299
3300 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003301 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003302 PSA_ASSERT( psa_cipher_set_iv( &operation,
3303 iv, sizeof( iv ) ) );
3304 TEST_EQUAL( psa_cipher_set_iv( &operation,
3305 iv, sizeof( iv ) ),
3306 PSA_ERROR_BAD_STATE );
3307 PSA_ASSERT( psa_cipher_abort( &operation ) );
3308
3309 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003310 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003311 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3312 buffer, sizeof( buffer ),
3313 &length ) );
3314 TEST_EQUAL( psa_cipher_set_iv( &operation,
3315 iv, sizeof( iv ) ),
3316 PSA_ERROR_BAD_STATE );
3317 PSA_ASSERT( psa_cipher_abort( &operation ) );
3318
3319 /* Call update without calling setup beforehand. */
3320 TEST_EQUAL( psa_cipher_update( &operation,
3321 text, sizeof( text ),
3322 buffer, sizeof( buffer ),
3323 &length ),
3324 PSA_ERROR_BAD_STATE );
3325 PSA_ASSERT( psa_cipher_abort( &operation ) );
3326
3327 /* Call update without an IV where an IV is required. */
3328 TEST_EQUAL( psa_cipher_update( &operation,
3329 text, sizeof( text ),
3330 buffer, sizeof( buffer ),
3331 &length ),
3332 PSA_ERROR_BAD_STATE );
3333 PSA_ASSERT( psa_cipher_abort( &operation ) );
3334
3335 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003336 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003337 PSA_ASSERT( psa_cipher_set_iv( &operation,
3338 iv, sizeof( iv ) ) );
3339 PSA_ASSERT( psa_cipher_finish( &operation,
3340 buffer, sizeof( buffer ), &length ) );
3341 TEST_EQUAL( psa_cipher_update( &operation,
3342 text, sizeof( text ),
3343 buffer, sizeof( buffer ),
3344 &length ),
3345 PSA_ERROR_BAD_STATE );
3346 PSA_ASSERT( psa_cipher_abort( &operation ) );
3347
3348 /* Call finish without calling setup beforehand. */
3349 TEST_EQUAL( psa_cipher_finish( &operation,
3350 buffer, sizeof( buffer ), &length ),
3351 PSA_ERROR_BAD_STATE );
3352 PSA_ASSERT( psa_cipher_abort( &operation ) );
3353
3354 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003355 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003356 /* Not calling update means we are encrypting an empty buffer, which is OK
3357 * for cipher modes with padding. */
3358 TEST_EQUAL( psa_cipher_finish( &operation,
3359 buffer, sizeof( buffer ), &length ),
3360 PSA_ERROR_BAD_STATE );
3361 PSA_ASSERT( psa_cipher_abort( &operation ) );
3362
3363 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003364 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003365 PSA_ASSERT( psa_cipher_set_iv( &operation,
3366 iv, sizeof( iv ) ) );
3367 PSA_ASSERT( psa_cipher_finish( &operation,
3368 buffer, sizeof( buffer ), &length ) );
3369 TEST_EQUAL( psa_cipher_finish( &operation,
3370 buffer, sizeof( buffer ), &length ),
3371 PSA_ERROR_BAD_STATE );
3372 PSA_ASSERT( psa_cipher_abort( &operation ) );
3373
Ronald Cron5425a212020-08-04 14:58:35 +02003374 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003375
Jaeden Ameroab439972019-02-15 14:12:05 +00003376exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003377 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003378 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003379}
3380/* END_CASE */
3381
3382/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003383void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003384 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003385 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003386 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003387{
Ronald Cron5425a212020-08-04 14:58:35 +02003388 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003389 psa_status_t status;
3390 psa_key_type_t key_type = key_type_arg;
3391 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003392 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003393 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003394 size_t output_buffer_size = 0;
3395 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003396 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003397 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003399
Gilles Peskine8817f612018-12-18 00:18:46 +01003400 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003401
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003402 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3403 psa_set_key_algorithm( &attributes, alg );
3404 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003405
Ronald Cron5425a212020-08-04 14:58:35 +02003406 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3407 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003408
Ronald Cron5425a212020-08-04 14:58:35 +02003409 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003410
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003411 if( iv->len > 0 )
3412 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003413 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003414 }
3415
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003416 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003417 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003418 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003419
Gilles Peskine8817f612018-12-18 00:18:46 +01003420 PSA_ASSERT( psa_cipher_update( &operation,
3421 input->x, input->len,
3422 output, output_buffer_size,
3423 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003424 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003425 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003426 output + total_output_length,
3427 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003428 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003429 total_output_length += function_output_length;
3430
Gilles Peskinefe11b722018-12-18 00:24:04 +01003431 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003432 if( expected_status == PSA_SUCCESS )
3433 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003434 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003435 ASSERT_COMPARE( expected_output->x, expected_output->len,
3436 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003437 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003438
Gilles Peskine50e586b2018-06-08 14:28:46 +02003439exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003440 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003441 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003442 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003443 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003444}
3445/* END_CASE */
3446
3447/* BEGIN_CASE */
3448void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003449 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003450 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003451 int first_part_size_arg,
3452 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003453 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003454{
Ronald Cron5425a212020-08-04 14:58:35 +02003455 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003456 psa_key_type_t key_type = key_type_arg;
3457 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003458 size_t first_part_size = first_part_size_arg;
3459 size_t output1_length = output1_length_arg;
3460 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003461 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003462 size_t output_buffer_size = 0;
3463 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003464 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003465 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003466 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003467
Gilles Peskine8817f612018-12-18 00:18:46 +01003468 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003469
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003470 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3471 psa_set_key_algorithm( &attributes, alg );
3472 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003473
Ronald Cron5425a212020-08-04 14:58:35 +02003474 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3475 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003476
Ronald Cron5425a212020-08-04 14:58:35 +02003477 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003478
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003479 if( iv->len > 0 )
3480 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003481 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003482 }
3483
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003484 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003485 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003486 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003487
Gilles Peskinee0866522019-02-19 19:44:00 +01003488 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003489 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3490 output, output_buffer_size,
3491 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003492 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003493 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003494 PSA_ASSERT( psa_cipher_update( &operation,
3495 input->x + first_part_size,
3496 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003497 output + total_output_length,
3498 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003499 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003500 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003501 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003502 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003503 output + total_output_length,
3504 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003505 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003506 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003507 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003508
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003509 ASSERT_COMPARE( expected_output->x, expected_output->len,
3510 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003511
3512exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003513 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003514 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003515 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003516 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003517}
3518/* END_CASE */
3519
3520/* BEGIN_CASE */
3521void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003522 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003523 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003524 int first_part_size_arg,
3525 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003526 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003527{
Ronald Cron5425a212020-08-04 14:58:35 +02003528 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003529 psa_key_type_t key_type = key_type_arg;
3530 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003531 size_t first_part_size = first_part_size_arg;
3532 size_t output1_length = output1_length_arg;
3533 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003534 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003535 size_t output_buffer_size = 0;
3536 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003537 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003538 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003540
Gilles Peskine8817f612018-12-18 00:18:46 +01003541 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003542
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003543 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3544 psa_set_key_algorithm( &attributes, alg );
3545 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003546
Ronald Cron5425a212020-08-04 14:58:35 +02003547 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3548 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003549
Ronald Cron5425a212020-08-04 14:58:35 +02003550 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003551
Steven Cooreman177deba2020-09-07 17:14:14 +02003552 if( iv->len > 0 )
3553 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003554 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003555 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003556
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003557 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003558 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003559 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003560
Gilles Peskinee0866522019-02-19 19:44:00 +01003561 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003562 PSA_ASSERT( psa_cipher_update( &operation,
3563 input->x, first_part_size,
3564 output, output_buffer_size,
3565 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003566 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003567 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003568 PSA_ASSERT( psa_cipher_update( &operation,
3569 input->x + first_part_size,
3570 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003571 output + total_output_length,
3572 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003573 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003574 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003575 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003576 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003577 output + total_output_length,
3578 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003579 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003580 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003582
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003583 ASSERT_COMPARE( expected_output->x, expected_output->len,
3584 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003585
3586exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003587 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003588 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003589 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003590 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003591}
3592/* END_CASE */
3593
Gilles Peskine50e586b2018-06-08 14:28:46 +02003594/* BEGIN_CASE */
3595void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003596 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003597 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003598 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003599{
Ronald Cron5425a212020-08-04 14:58:35 +02003600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003601 psa_status_t status;
3602 psa_key_type_t key_type = key_type_arg;
3603 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003604 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003605 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003606 size_t output_buffer_size = 0;
3607 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003608 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003609 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003611
Gilles Peskine8817f612018-12-18 00:18:46 +01003612 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003613
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003614 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3615 psa_set_key_algorithm( &attributes, alg );
3616 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003617
Ronald Cron5425a212020-08-04 14:58:35 +02003618 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3619 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003620
Ronald Cron5425a212020-08-04 14:58:35 +02003621 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003622
Steven Cooreman177deba2020-09-07 17:14:14 +02003623 if( iv->len > 0 )
3624 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003625 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003626 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003627
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003628 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003629 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003630 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003631
Gilles Peskine8817f612018-12-18 00:18:46 +01003632 PSA_ASSERT( psa_cipher_update( &operation,
3633 input->x, input->len,
3634 output, output_buffer_size,
3635 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003636 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003637 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003638 output + total_output_length,
3639 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003640 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003641 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003642 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003643
3644 if( expected_status == PSA_SUCCESS )
3645 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003646 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003647 ASSERT_COMPARE( expected_output->x, expected_output->len,
3648 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003649 }
3650
Gilles Peskine50e586b2018-06-08 14:28:46 +02003651exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003652 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003653 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003654 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003655 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003656}
3657/* END_CASE */
3658
Gilles Peskine50e586b2018-06-08 14:28:46 +02003659/* BEGIN_CASE */
3660void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003661 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003662 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003663{
Ronald Cron5425a212020-08-04 14:58:35 +02003664 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003665 psa_key_type_t key_type = key_type_arg;
3666 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003667 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003668 size_t iv_size = 16;
3669 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003670 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003671 size_t output1_size = 0;
3672 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003673 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003674 size_t output2_size = 0;
3675 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003676 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003677 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3678 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003679 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003680
Gilles Peskine8817f612018-12-18 00:18:46 +01003681 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003682
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003683 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3684 psa_set_key_algorithm( &attributes, alg );
3685 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003686
Ronald Cron5425a212020-08-04 14:58:35 +02003687 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3688 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003689
Ronald Cron5425a212020-08-04 14:58:35 +02003690 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3691 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003692
Steven Cooreman177deba2020-09-07 17:14:14 +02003693 if( alg != PSA_ALG_ECB_NO_PADDING )
3694 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003695 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3696 iv, iv_size,
3697 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003698 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003699 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003700 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003701 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003702
Gilles Peskine8817f612018-12-18 00:18:46 +01003703 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3704 output1, output1_size,
3705 &output1_length ) );
3706 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003707 output1 + output1_length,
3708 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003709 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003710
Gilles Peskine048b7f02018-06-08 14:20:49 +02003711 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003712
Gilles Peskine8817f612018-12-18 00:18:46 +01003713 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003714
3715 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003716 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003717
Steven Cooreman177deba2020-09-07 17:14:14 +02003718 if( iv_length > 0 )
3719 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003720 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3721 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003722 }
3723
Gilles Peskine8817f612018-12-18 00:18:46 +01003724 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3725 output2, output2_size,
3726 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003727 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003728 PSA_ASSERT( psa_cipher_finish( &operation2,
3729 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003730 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003731 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003732
Gilles Peskine048b7f02018-06-08 14:20:49 +02003733 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003734
Gilles Peskine8817f612018-12-18 00:18:46 +01003735 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003736
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003737 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003738
3739exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003740 psa_cipher_abort( &operation1 );
3741 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003742 mbedtls_free( output1 );
3743 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003744 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003745 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003746}
3747/* END_CASE */
3748
3749/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003750void cipher_verify_output_multipart( int alg_arg,
3751 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003752 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003753 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003754 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003755{
Ronald Cron5425a212020-08-04 14:58:35 +02003756 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003757 psa_key_type_t key_type = key_type_arg;
3758 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003759 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003760 unsigned char iv[16] = {0};
3761 size_t iv_size = 16;
3762 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003763 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003764 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003765 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003766 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003767 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003768 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003769 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003770 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3771 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003773
Gilles Peskine8817f612018-12-18 00:18:46 +01003774 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003775
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003776 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3777 psa_set_key_algorithm( &attributes, alg );
3778 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003779
Ronald Cron5425a212020-08-04 14:58:35 +02003780 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3781 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003782
Ronald Cron5425a212020-08-04 14:58:35 +02003783 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3784 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003785
Steven Cooreman177deba2020-09-07 17:14:14 +02003786 if( alg != PSA_ALG_ECB_NO_PADDING )
3787 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003788 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3789 iv, iv_size,
3790 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003791 }
3792
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003793 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003794 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003795 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003796
Gilles Peskinee0866522019-02-19 19:44:00 +01003797 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003798
Gilles Peskine8817f612018-12-18 00:18:46 +01003799 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3800 output1, output1_buffer_size,
3801 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003802 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003803
Gilles Peskine8817f612018-12-18 00:18:46 +01003804 PSA_ASSERT( psa_cipher_update( &operation1,
3805 input->x + first_part_size,
3806 input->len - first_part_size,
3807 output1, output1_buffer_size,
3808 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003809 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003810
Gilles Peskine8817f612018-12-18 00:18:46 +01003811 PSA_ASSERT( psa_cipher_finish( &operation1,
3812 output1 + output1_length,
3813 output1_buffer_size - output1_length,
3814 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003815 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003816
Gilles Peskine8817f612018-12-18 00:18:46 +01003817 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003818
Gilles Peskine048b7f02018-06-08 14:20:49 +02003819 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003820 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003821
Steven Cooreman177deba2020-09-07 17:14:14 +02003822 if( iv_length > 0 )
3823 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003824 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3825 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003826 }
Moran Pekerded84402018-06-06 16:36:50 +03003827
Gilles Peskine8817f612018-12-18 00:18:46 +01003828 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3829 output2, output2_buffer_size,
3830 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003831 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003832
Gilles Peskine8817f612018-12-18 00:18:46 +01003833 PSA_ASSERT( psa_cipher_update( &operation2,
3834 output1 + first_part_size,
3835 output1_length - first_part_size,
3836 output2, output2_buffer_size,
3837 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003838 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003839
Gilles Peskine8817f612018-12-18 00:18:46 +01003840 PSA_ASSERT( psa_cipher_finish( &operation2,
3841 output2 + output2_length,
3842 output2_buffer_size - output2_length,
3843 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003844 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003845
Gilles Peskine8817f612018-12-18 00:18:46 +01003846 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003847
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003848 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003849
3850exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003851 psa_cipher_abort( &operation1 );
3852 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003853 mbedtls_free( output1 );
3854 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003855 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003856 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003857}
3858/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003859
Gilles Peskine20035e32018-02-03 22:44:14 +01003860/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003861void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003862 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003863 data_t *nonce,
3864 data_t *additional_data,
3865 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003866 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003867{
Ronald Cron5425a212020-08-04 14:58:35 +02003868 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003869 psa_key_type_t key_type = key_type_arg;
3870 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003871 unsigned char *output_data = NULL;
3872 size_t output_size = 0;
3873 size_t output_length = 0;
3874 unsigned char *output_data2 = NULL;
3875 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003876 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Steven Cooremanf49478b2021-02-15 15:19:25 +01003877 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003878 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003880
Gilles Peskine4abf7412018-06-18 16:35:34 +02003881 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003882 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3883 * should be exact. */
3884 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3885 TEST_EQUAL( output_size,
3886 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003887 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003888
Gilles Peskine8817f612018-12-18 00:18:46 +01003889 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003890
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003891 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3892 psa_set_key_algorithm( &attributes, alg );
3893 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003894
Gilles Peskine049c7532019-05-15 20:22:09 +02003895 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003896 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003897
Steven Cooremanf49478b2021-02-15 15:19:25 +01003898 status = psa_aead_encrypt( key, alg,
3899 nonce->x, nonce->len,
3900 additional_data->x,
3901 additional_data->len,
3902 input_data->x, input_data->len,
3903 output_data, output_size,
3904 &output_length );
3905
3906 /* If the operation is not supported, just skip and not fail in case the
3907 * encryption involves a common limitation of cryptography hardwares and
3908 * an alternative implementation. */
3909 if( status == PSA_ERROR_NOT_SUPPORTED )
3910 {
3911 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3912 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3913 }
3914
3915 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003916
3917 if( PSA_SUCCESS == expected_result )
3918 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003919 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003920
Gilles Peskine003a4a92019-05-14 16:09:40 +02003921 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3922 * should be exact. */
3923 TEST_EQUAL( input_data->len,
3924 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3925
Ronald Cron5425a212020-08-04 14:58:35 +02003926 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003927 nonce->x, nonce->len,
3928 additional_data->x,
3929 additional_data->len,
3930 output_data, output_length,
3931 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003932 &output_length2 ),
3933 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003934
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003935 ASSERT_COMPARE( input_data->x, input_data->len,
3936 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003937 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003938
Gilles Peskinea1cac842018-06-11 19:33:02 +02003939exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003940 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003941 mbedtls_free( output_data );
3942 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003943 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003944}
3945/* END_CASE */
3946
3947/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003948void aead_encrypt( int key_type_arg, data_t *key_data,
3949 int alg_arg,
3950 data_t *nonce,
3951 data_t *additional_data,
3952 data_t *input_data,
3953 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003954{
Ronald Cron5425a212020-08-04 14:58:35 +02003955 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003956 psa_key_type_t key_type = key_type_arg;
3957 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003958 unsigned char *output_data = NULL;
3959 size_t output_size = 0;
3960 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003961 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003963 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003964
Gilles Peskine4abf7412018-06-18 16:35:34 +02003965 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003966 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3967 * should be exact. */
3968 TEST_EQUAL( output_size,
3969 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003970 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003971
Gilles Peskine8817f612018-12-18 00:18:46 +01003972 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003973
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003974 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3975 psa_set_key_algorithm( &attributes, alg );
3976 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003977
Gilles Peskine049c7532019-05-15 20:22:09 +02003978 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003979 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003980
Steven Cooremand588ea12021-01-11 19:36:04 +01003981 status = psa_aead_encrypt( key, alg,
3982 nonce->x, nonce->len,
3983 additional_data->x, additional_data->len,
3984 input_data->x, input_data->len,
3985 output_data, output_size,
3986 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003987
Ronald Cron28a45ed2021-02-09 20:35:42 +01003988 /* If the operation is not supported, just skip and not fail in case the
3989 * encryption involves a common limitation of cryptography hardwares and
3990 * an alternative implementation. */
3991 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003992 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003993 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3994 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003995 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003996
3997 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003998 ASSERT_COMPARE( expected_result->x, expected_result->len,
3999 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004000
Gilles Peskinea1cac842018-06-11 19:33:02 +02004001exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004002 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004003 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004004 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004005}
4006/* END_CASE */
4007
4008/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004009void aead_decrypt( int key_type_arg, data_t *key_data,
4010 int alg_arg,
4011 data_t *nonce,
4012 data_t *additional_data,
4013 data_t *input_data,
4014 data_t *expected_data,
4015 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004016{
Ronald Cron5425a212020-08-04 14:58:35 +02004017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004018 psa_key_type_t key_type = key_type_arg;
4019 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004020 unsigned char *output_data = NULL;
4021 size_t output_size = 0;
4022 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004023 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004025 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004026 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004027
Gilles Peskine003a4a92019-05-14 16:09:40 +02004028 output_size = input_data->len - tag_length;
4029 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4030 * should be exact. */
4031 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4032 TEST_EQUAL( output_size,
4033 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004034 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004035
Gilles Peskine8817f612018-12-18 00:18:46 +01004036 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004037
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004038 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4039 psa_set_key_algorithm( &attributes, alg );
4040 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004041
Gilles Peskine049c7532019-05-15 20:22:09 +02004042 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004043 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004044
Steven Cooremand588ea12021-01-11 19:36:04 +01004045 status = psa_aead_decrypt( key, alg,
4046 nonce->x, nonce->len,
4047 additional_data->x,
4048 additional_data->len,
4049 input_data->x, input_data->len,
4050 output_data, output_size,
4051 &output_length );
4052
Ronald Cron28a45ed2021-02-09 20:35:42 +01004053 /* If the operation is not supported, just skip and not fail in case the
4054 * decryption involves a common limitation of cryptography hardwares and
4055 * an alternative implementation. */
4056 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004057 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004058 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4059 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004060 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004061
4062 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004063
Gilles Peskine2d277862018-06-18 15:41:12 +02004064 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004065 ASSERT_COMPARE( expected_data->x, expected_data->len,
4066 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004067
Gilles Peskinea1cac842018-06-11 19:33:02 +02004068exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004069 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004070 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004071 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004072}
4073/* END_CASE */
4074
4075/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004076void signature_size( int type_arg,
4077 int bits,
4078 int alg_arg,
4079 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004080{
4081 psa_key_type_t type = type_arg;
4082 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004083 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004084
Gilles Peskinefe11b722018-12-18 00:24:04 +01004085 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004086#if defined(MBEDTLS_TEST_DEPRECATED)
4087 TEST_EQUAL( actual_size,
4088 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4089#endif /* MBEDTLS_TEST_DEPRECATED */
4090
Gilles Peskinee59236f2018-01-27 23:32:46 +01004091exit:
4092 ;
4093}
4094/* END_CASE */
4095
4096/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004097void sign_deterministic( int key_type_arg, data_t *key_data,
4098 int alg_arg, data_t *input_data,
4099 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004100{
Ronald Cron5425a212020-08-04 14:58:35 +02004101 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004102 psa_key_type_t key_type = key_type_arg;
4103 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004104 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004105 unsigned char *signature = NULL;
4106 size_t signature_size;
4107 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004109
Gilles Peskine8817f612018-12-18 00:18:46 +01004110 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004111
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004112 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004113 psa_set_key_algorithm( &attributes, alg );
4114 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004115
Gilles Peskine049c7532019-05-15 20:22:09 +02004116 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004117 &key ) );
4118 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004119 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004120
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004121 /* Allocate a buffer which has the size advertized by the
4122 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004123 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004124 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004125 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004126 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004127 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004128
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004129 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004130 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004131 input_data->x, input_data->len,
4132 signature, signature_size,
4133 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004134 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004135 ASSERT_COMPARE( output_data->x, output_data->len,
4136 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004137
Gilles Peskine0627f982019-11-26 19:12:16 +01004138#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004139 memset( signature, 0, signature_size );
4140 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004141 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004142 input_data->x, input_data->len,
4143 signature, signature_size,
4144 &signature_length ) );
4145 ASSERT_COMPARE( output_data->x, output_data->len,
4146 signature, signature_length );
4147#endif /* MBEDTLS_TEST_DEPRECATED */
4148
Gilles Peskine20035e32018-02-03 22:44:14 +01004149exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004150 /*
4151 * Key attributes may have been returned by psa_get_key_attributes()
4152 * thus reset them as required.
4153 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004154 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004155
Ronald Cron5425a212020-08-04 14:58:35 +02004156 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004157 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004158 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004159}
4160/* END_CASE */
4161
4162/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004163void sign_fail( int key_type_arg, data_t *key_data,
4164 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004165 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004166{
Ronald Cron5425a212020-08-04 14:58:35 +02004167 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004168 psa_key_type_t key_type = key_type_arg;
4169 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004170 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004171 psa_status_t actual_status;
4172 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004173 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004174 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004176
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004177 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004178
Gilles Peskine8817f612018-12-18 00:18:46 +01004179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004180
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004181 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004182 psa_set_key_algorithm( &attributes, alg );
4183 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004184
Gilles Peskine049c7532019-05-15 20:22:09 +02004185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004186 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004187
Ronald Cron5425a212020-08-04 14:58:35 +02004188 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004189 input_data->x, input_data->len,
4190 signature, signature_size,
4191 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004192 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004193 /* The value of *signature_length is unspecified on error, but
4194 * whatever it is, it should be less than signature_size, so that
4195 * if the caller tries to read *signature_length bytes without
4196 * checking the error code then they don't overflow a buffer. */
4197 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004198
Gilles Peskine895242b2019-11-29 12:15:40 +01004199#if defined(MBEDTLS_TEST_DEPRECATED)
4200 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004201 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004202 input_data->x, input_data->len,
4203 signature, signature_size,
4204 &signature_length ),
4205 expected_status );
4206 TEST_ASSERT( signature_length <= signature_size );
4207#endif /* MBEDTLS_TEST_DEPRECATED */
4208
Gilles Peskine20035e32018-02-03 22:44:14 +01004209exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004210 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004211 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004212 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004213 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004214}
4215/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004216
4217/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004218void sign_verify( int key_type_arg, data_t *key_data,
4219 int alg_arg, data_t *input_data )
4220{
Ronald Cron5425a212020-08-04 14:58:35 +02004221 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004222 psa_key_type_t key_type = key_type_arg;
4223 psa_algorithm_t alg = alg_arg;
4224 size_t key_bits;
4225 unsigned char *signature = NULL;
4226 size_t signature_size;
4227 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004229
Gilles Peskine8817f612018-12-18 00:18:46 +01004230 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004231
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004232 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004233 psa_set_key_algorithm( &attributes, alg );
4234 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004235
Gilles Peskine049c7532019-05-15 20:22:09 +02004236 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004237 &key ) );
4238 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004239 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004240
4241 /* Allocate a buffer which has the size advertized by the
4242 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004243 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004244 key_bits, alg );
4245 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004246 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004247 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004248
4249 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004250 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004251 input_data->x, input_data->len,
4252 signature, signature_size,
4253 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004254 /* Check that the signature length looks sensible. */
4255 TEST_ASSERT( signature_length <= signature_size );
4256 TEST_ASSERT( signature_length > 0 );
4257
4258 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004259 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004260 input_data->x, input_data->len,
4261 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004262
4263 if( input_data->len != 0 )
4264 {
4265 /* Flip a bit in the input and verify that the signature is now
4266 * detected as invalid. Flip a bit at the beginning, not at the end,
4267 * because ECDSA may ignore the last few bits of the input. */
4268 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004269 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004270 input_data->x, input_data->len,
4271 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004272 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004273 }
4274
4275exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004276 /*
4277 * Key attributes may have been returned by psa_get_key_attributes()
4278 * thus reset them as required.
4279 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004280 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004281
Ronald Cron5425a212020-08-04 14:58:35 +02004282 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004283 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004284 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004285}
4286/* END_CASE */
4287
4288/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004289void asymmetric_verify( int key_type_arg, data_t *key_data,
4290 int alg_arg, data_t *hash_data,
4291 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004292{
Ronald Cron5425a212020-08-04 14:58:35 +02004293 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004294 psa_key_type_t key_type = key_type_arg;
4295 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004296 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004297
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004298 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004299
Gilles Peskine8817f612018-12-18 00:18:46 +01004300 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004301
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004302 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004303 psa_set_key_algorithm( &attributes, alg );
4304 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004305
Gilles Peskine049c7532019-05-15 20:22:09 +02004306 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004307 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004308
Ronald Cron5425a212020-08-04 14:58:35 +02004309 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004310 hash_data->x, hash_data->len,
4311 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004312
4313#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004314 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004315 hash_data->x, hash_data->len,
4316 signature_data->x,
4317 signature_data->len ) );
4318
4319#endif /* MBEDTLS_TEST_DEPRECATED */
4320
itayzafrir5c753392018-05-08 11:18:38 +03004321exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004322 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004323 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004324 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004325}
4326/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004327
4328/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004329void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4330 int alg_arg, data_t *hash_data,
4331 data_t *signature_data,
4332 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004333{
Ronald Cron5425a212020-08-04 14:58:35 +02004334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004335 psa_key_type_t key_type = key_type_arg;
4336 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004337 psa_status_t actual_status;
4338 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340
Gilles Peskine8817f612018-12-18 00:18:46 +01004341 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004342
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004343 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004344 psa_set_key_algorithm( &attributes, alg );
4345 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004346
Gilles Peskine049c7532019-05-15 20:22:09 +02004347 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004348 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004349
Ronald Cron5425a212020-08-04 14:58:35 +02004350 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004351 hash_data->x, hash_data->len,
4352 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004353 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004354
Gilles Peskine895242b2019-11-29 12:15:40 +01004355#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004356 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004357 hash_data->x, hash_data->len,
4358 signature_data->x, signature_data->len ),
4359 expected_status );
4360#endif /* MBEDTLS_TEST_DEPRECATED */
4361
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004362exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004363 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004364 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004365 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004366}
4367/* END_CASE */
4368
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004369/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004370void asymmetric_encrypt( int key_type_arg,
4371 data_t *key_data,
4372 int alg_arg,
4373 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004374 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004375 int expected_output_length_arg,
4376 int expected_status_arg )
4377{
Ronald Cron5425a212020-08-04 14:58:35 +02004378 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004379 psa_key_type_t key_type = key_type_arg;
4380 psa_algorithm_t alg = alg_arg;
4381 size_t expected_output_length = expected_output_length_arg;
4382 size_t key_bits;
4383 unsigned char *output = NULL;
4384 size_t output_size;
4385 size_t output_length = ~0;
4386 psa_status_t actual_status;
4387 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004388 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004389
Gilles Peskine8817f612018-12-18 00:18:46 +01004390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004391
Gilles Peskine656896e2018-06-29 19:12:28 +02004392 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004393 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4394 psa_set_key_algorithm( &attributes, alg );
4395 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004396 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004397 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004398
4399 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004400 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004401 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004402 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004403 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004404
4405 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004406 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004407 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004408 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004409 output, output_size,
4410 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004411 TEST_EQUAL( actual_status, expected_status );
4412 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004413
Gilles Peskine68428122018-06-30 18:42:41 +02004414 /* If the label is empty, the test framework puts a non-null pointer
4415 * in label->x. Test that a null pointer works as well. */
4416 if( label->len == 0 )
4417 {
4418 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004419 if( output_size != 0 )
4420 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004421 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004422 input_data->x, input_data->len,
4423 NULL, label->len,
4424 output, output_size,
4425 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004426 TEST_EQUAL( actual_status, expected_status );
4427 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004428 }
4429
Gilles Peskine656896e2018-06-29 19:12:28 +02004430exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004431 /*
4432 * Key attributes may have been returned by psa_get_key_attributes()
4433 * thus reset them as required.
4434 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004435 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004436
Ronald Cron5425a212020-08-04 14:58:35 +02004437 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004438 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004439 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004440}
4441/* END_CASE */
4442
4443/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004444void asymmetric_encrypt_decrypt( int key_type_arg,
4445 data_t *key_data,
4446 int alg_arg,
4447 data_t *input_data,
4448 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004449{
Ronald Cron5425a212020-08-04 14:58:35 +02004450 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004451 psa_key_type_t key_type = key_type_arg;
4452 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004453 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004454 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004455 size_t output_size;
4456 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004457 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004458 size_t output2_size;
4459 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004461
Gilles Peskine8817f612018-12-18 00:18:46 +01004462 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004463
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004464 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4465 psa_set_key_algorithm( &attributes, alg );
4466 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004467
Gilles Peskine049c7532019-05-15 20:22:09 +02004468 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004469 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004470
4471 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004472 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004473 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004474 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004475 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004476 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004477 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004478
Gilles Peskineeebd7382018-06-08 18:11:54 +02004479 /* We test encryption by checking that encrypt-then-decrypt gives back
4480 * the original plaintext because of the non-optional random
4481 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004482 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004483 input_data->x, input_data->len,
4484 label->x, label->len,
4485 output, output_size,
4486 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004487 /* We don't know what ciphertext length to expect, but check that
4488 * it looks sensible. */
4489 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004490
Ronald Cron5425a212020-08-04 14:58:35 +02004491 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004492 output, output_length,
4493 label->x, label->len,
4494 output2, output2_size,
4495 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004496 ASSERT_COMPARE( input_data->x, input_data->len,
4497 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004498
4499exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004500 /*
4501 * Key attributes may have been returned by psa_get_key_attributes()
4502 * thus reset them as required.
4503 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004504 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004505
Ronald Cron5425a212020-08-04 14:58:35 +02004506 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004507 mbedtls_free( output );
4508 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004509 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004510}
4511/* END_CASE */
4512
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004513/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004514void asymmetric_decrypt( int key_type_arg,
4515 data_t *key_data,
4516 int alg_arg,
4517 data_t *input_data,
4518 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004519 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004520{
Ronald Cron5425a212020-08-04 14:58:35 +02004521 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004522 psa_key_type_t key_type = key_type_arg;
4523 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004524 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004525 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004526 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004528
Jaeden Amero412654a2019-02-06 12:57:46 +00004529 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004530 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004531
Gilles Peskine8817f612018-12-18 00:18:46 +01004532 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004533
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004534 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4535 psa_set_key_algorithm( &attributes, alg );
4536 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004537
Gilles Peskine049c7532019-05-15 20:22:09 +02004538 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004539 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004540
Ronald Cron5425a212020-08-04 14:58:35 +02004541 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004542 input_data->x, input_data->len,
4543 label->x, label->len,
4544 output,
4545 output_size,
4546 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004547 ASSERT_COMPARE( expected_data->x, expected_data->len,
4548 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004549
Gilles Peskine68428122018-06-30 18:42:41 +02004550 /* If the label is empty, the test framework puts a non-null pointer
4551 * in label->x. Test that a null pointer works as well. */
4552 if( label->len == 0 )
4553 {
4554 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004555 if( output_size != 0 )
4556 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004557 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004558 input_data->x, input_data->len,
4559 NULL, label->len,
4560 output,
4561 output_size,
4562 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004563 ASSERT_COMPARE( expected_data->x, expected_data->len,
4564 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004565 }
4566
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004567exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004568 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004569 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004570 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004571 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004572}
4573/* END_CASE */
4574
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004575/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004576void asymmetric_decrypt_fail( int key_type_arg,
4577 data_t *key_data,
4578 int alg_arg,
4579 data_t *input_data,
4580 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004581 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004582 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004583{
Ronald Cron5425a212020-08-04 14:58:35 +02004584 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004585 psa_key_type_t key_type = key_type_arg;
4586 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004587 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004588 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004589 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004590 psa_status_t actual_status;
4591 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004592 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004593
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004594 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004595
Gilles Peskine8817f612018-12-18 00:18:46 +01004596 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004597
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004598 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4599 psa_set_key_algorithm( &attributes, alg );
4600 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004601
Gilles Peskine049c7532019-05-15 20:22:09 +02004602 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004603 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004604
Ronald Cron5425a212020-08-04 14:58:35 +02004605 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004606 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004607 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004608 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004609 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004610 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004611 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004612
Gilles Peskine68428122018-06-30 18:42:41 +02004613 /* If the label is empty, the test framework puts a non-null pointer
4614 * in label->x. Test that a null pointer works as well. */
4615 if( label->len == 0 )
4616 {
4617 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004618 if( output_size != 0 )
4619 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004620 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004621 input_data->x, input_data->len,
4622 NULL, label->len,
4623 output, output_size,
4624 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004625 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004626 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004627 }
4628
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004629exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004630 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004631 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004632 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004633 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004634}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004635/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004636
4637/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004638void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004639{
4640 /* Test each valid way of initializing the object, except for `= {0}`, as
4641 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4642 * though it's OK by the C standard. We could test for this, but we'd need
4643 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004644 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004645 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4646 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4647 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004648
4649 memset( &zero, 0, sizeof( zero ) );
4650
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004651 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004652 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004653 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004654 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004655 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004656 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004657 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004658
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004660 PSA_ASSERT( psa_key_derivation_abort(&func) );
4661 PSA_ASSERT( psa_key_derivation_abort(&init) );
4662 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004663}
4664/* END_CASE */
4665
Janos Follath16de4a42019-06-13 16:32:24 +01004666/* BEGIN_CASE */
4667void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004668{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004669 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004670 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004671 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004672
Gilles Peskine8817f612018-12-18 00:18:46 +01004673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004674
Janos Follath16de4a42019-06-13 16:32:24 +01004675 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004676 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004677
4678exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004679 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004680 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004681}
4682/* END_CASE */
4683
Janos Follathaf3c2a02019-06-12 12:34:34 +01004684/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004685void derive_set_capacity( int alg_arg, int capacity_arg,
4686 int expected_status_arg )
4687{
4688 psa_algorithm_t alg = alg_arg;
4689 size_t capacity = capacity_arg;
4690 psa_status_t expected_status = expected_status_arg;
4691 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4692
4693 PSA_ASSERT( psa_crypto_init( ) );
4694
4695 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4696
4697 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4698 expected_status );
4699
4700exit:
4701 psa_key_derivation_abort( &operation );
4702 PSA_DONE( );
4703}
4704/* END_CASE */
4705
4706/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004707void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004708 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004709 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004710 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004711 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004712 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004713 int expected_status_arg3,
4714 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004715{
4716 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004717 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4718 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004719 psa_status_t expected_statuses[] = {expected_status_arg1,
4720 expected_status_arg2,
4721 expected_status_arg3};
4722 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004723 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4724 MBEDTLS_SVC_KEY_ID_INIT,
4725 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004726 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4727 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4728 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004729 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004730 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004731 psa_status_t expected_output_status = expected_output_status_arg;
4732 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004733
4734 PSA_ASSERT( psa_crypto_init( ) );
4735
4736 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4737 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004738
4739 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4740
4741 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4742 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004743 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004744 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004745 psa_set_key_type( &attributes, key_types[i] );
4746 PSA_ASSERT( psa_import_key( &attributes,
4747 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004748 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004749 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4750 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4751 {
4752 // When taking a private key as secret input, use key agreement
4753 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004754 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004755 expected_statuses[i] );
4756 }
4757 else
4758 {
4759 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004760 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004761 expected_statuses[i] );
4762 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004763 }
4764 else
4765 {
4766 TEST_EQUAL( psa_key_derivation_input_bytes(
4767 &operation, steps[i],
4768 inputs[i]->x, inputs[i]->len ),
4769 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004770 }
4771 }
4772
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004773 if( output_key_type != PSA_KEY_TYPE_NONE )
4774 {
4775 psa_reset_key_attributes( &attributes );
4776 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4777 psa_set_key_bits( &attributes, 8 );
4778 actual_output_status =
4779 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004780 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004781 }
4782 else
4783 {
4784 uint8_t buffer[1];
4785 actual_output_status =
4786 psa_key_derivation_output_bytes( &operation,
4787 buffer, sizeof( buffer ) );
4788 }
4789 TEST_EQUAL( actual_output_status, expected_output_status );
4790
Janos Follathaf3c2a02019-06-12 12:34:34 +01004791exit:
4792 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004793 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4794 psa_destroy_key( keys[i] );
4795 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004796 PSA_DONE( );
4797}
4798/* END_CASE */
4799
Janos Follathd958bb72019-07-03 15:02:16 +01004800/* BEGIN_CASE */
4801void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004802{
Janos Follathd958bb72019-07-03 15:02:16 +01004803 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004804 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004805 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004807 unsigned char input1[] = "Input 1";
4808 size_t input1_length = sizeof( input1 );
4809 unsigned char input2[] = "Input 2";
4810 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004811 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004812 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004813 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4814 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4815 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004817
Gilles Peskine8817f612018-12-18 00:18:46 +01004818 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004819
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004820 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4821 psa_set_key_algorithm( &attributes, alg );
4822 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004823
Gilles Peskine73676cb2019-05-15 20:15:10 +02004824 PSA_ASSERT( psa_import_key( &attributes,
4825 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004826 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004827
4828 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004829 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004830 input1, input1_length,
4831 input2, input2_length,
4832 capacity ) )
4833 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004834
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004836 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004837 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004838
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004840
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004841 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004842 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004843
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004844exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004846 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004847 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004848}
4849/* END_CASE */
4850
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004851/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004852void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004853{
4854 uint8_t output_buffer[16];
4855 size_t buffer_size = 16;
4856 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004857 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004858
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004859 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4860 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004861 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004862
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004863 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004864 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004865
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004867
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004868 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4869 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004870 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004871
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004872 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004873 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004874
4875exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004876 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004877}
4878/* END_CASE */
4879
4880/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004881void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004882 int step1_arg, data_t *input1,
4883 int step2_arg, data_t *input2,
4884 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004885 int requested_capacity_arg,
4886 data_t *expected_output1,
4887 data_t *expected_output2 )
4888{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004889 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004890 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4891 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004892 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4893 MBEDTLS_SVC_KEY_ID_INIT,
4894 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004895 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004897 uint8_t *expected_outputs[2] =
4898 {expected_output1->x, expected_output2->x};
4899 size_t output_sizes[2] =
4900 {expected_output1->len, expected_output2->len};
4901 size_t output_buffer_size = 0;
4902 uint8_t *output_buffer = NULL;
4903 size_t expected_capacity;
4904 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004906 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004907 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004908
4909 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4910 {
4911 if( output_sizes[i] > output_buffer_size )
4912 output_buffer_size = output_sizes[i];
4913 if( output_sizes[i] == 0 )
4914 expected_outputs[i] = NULL;
4915 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004916 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004917 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004918
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004919 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4920 psa_set_key_algorithm( &attributes, alg );
4921 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004922
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004923 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004924 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4925 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4926 requested_capacity ) );
4927 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004928 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004929 switch( steps[i] )
4930 {
4931 case 0:
4932 break;
4933 case PSA_KEY_DERIVATION_INPUT_SECRET:
4934 PSA_ASSERT( psa_import_key( &attributes,
4935 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004936 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004937 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004938 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004939 break;
4940 default:
4941 PSA_ASSERT( psa_key_derivation_input_bytes(
4942 &operation, steps[i],
4943 inputs[i]->x, inputs[i]->len ) );
4944 break;
4945 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004946 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004947
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004948 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004949 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004950 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004951 expected_capacity = requested_capacity;
4952
4953 /* Expansion phase. */
4954 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4955 {
4956 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004957 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004958 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004959 if( expected_capacity == 0 && output_sizes[i] == 0 )
4960 {
4961 /* Reading 0 bytes when 0 bytes are available can go either way. */
4962 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004963 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004964 continue;
4965 }
4966 else if( expected_capacity == 0 ||
4967 output_sizes[i] > expected_capacity )
4968 {
4969 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004970 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004971 expected_capacity = 0;
4972 continue;
4973 }
4974 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004975 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004976 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004977 ASSERT_COMPARE( output_buffer, output_sizes[i],
4978 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004980 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004982 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004983 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004984 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004985 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004986
4987exit:
4988 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004989 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004990 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4991 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004992 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004993}
4994/* END_CASE */
4995
4996/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004997void derive_full( int alg_arg,
4998 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004999 data_t *input1,
5000 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005001 int requested_capacity_arg )
5002{
Ronald Cron5425a212020-08-04 14:58:35 +02005003 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005004 psa_algorithm_t alg = alg_arg;
5005 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005006 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005007 unsigned char output_buffer[16];
5008 size_t expected_capacity = requested_capacity;
5009 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005011
Gilles Peskine8817f612018-12-18 00:18:46 +01005012 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005013
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005014 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5015 psa_set_key_algorithm( &attributes, alg );
5016 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005017
Gilles Peskine049c7532019-05-15 20:22:09 +02005018 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005019 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005020
Ronald Cron5425a212020-08-04 14:58:35 +02005021 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005022 input1->x, input1->len,
5023 input2->x, input2->len,
5024 requested_capacity ) )
5025 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005026
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005027 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005028 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005029 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005030
5031 /* Expansion phase. */
5032 while( current_capacity > 0 )
5033 {
5034 size_t read_size = sizeof( output_buffer );
5035 if( read_size > current_capacity )
5036 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005037 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005038 output_buffer,
5039 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005040 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005041 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005042 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005043 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005044 }
5045
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 /* Check that the operation refuses to go over capacity. */
5047 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005048 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005049
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005051
5052exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005054 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005055 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005056}
5057/* END_CASE */
5058
Janos Follathe60c9052019-07-03 13:51:30 +01005059/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005060void derive_key_exercise( int alg_arg,
5061 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005062 data_t *input1,
5063 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005064 int derived_type_arg,
5065 int derived_bits_arg,
5066 int derived_usage_arg,
5067 int derived_alg_arg )
5068{
Ronald Cron5425a212020-08-04 14:58:35 +02005069 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5070 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005071 psa_algorithm_t alg = alg_arg;
5072 psa_key_type_t derived_type = derived_type_arg;
5073 size_t derived_bits = derived_bits_arg;
5074 psa_key_usage_t derived_usage = derived_usage_arg;
5075 psa_algorithm_t derived_alg = derived_alg_arg;
5076 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005077 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005078 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005079 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005080
Gilles Peskine8817f612018-12-18 00:18:46 +01005081 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005082
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005083 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5084 psa_set_key_algorithm( &attributes, alg );
5085 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005086 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005087 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005088
5089 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005090 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005091 input1->x, input1->len,
5092 input2->x, input2->len, capacity ) )
5093 goto exit;
5094
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005095 psa_set_key_usage_flags( &attributes, derived_usage );
5096 psa_set_key_algorithm( &attributes, derived_alg );
5097 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005098 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005100 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005101
5102 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005103 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005104 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5105 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005106
5107 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005108 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005109 goto exit;
5110
5111exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005112 /*
5113 * Key attributes may have been returned by psa_get_key_attributes()
5114 * thus reset them as required.
5115 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005116 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005117
5118 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005119 psa_destroy_key( base_key );
5120 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005121 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005122}
5123/* END_CASE */
5124
Janos Follath42fd8882019-07-03 14:17:09 +01005125/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005126void derive_key_export( int alg_arg,
5127 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005128 data_t *input1,
5129 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005130 int bytes1_arg,
5131 int bytes2_arg )
5132{
Ronald Cron5425a212020-08-04 14:58:35 +02005133 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5134 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005135 psa_algorithm_t alg = alg_arg;
5136 size_t bytes1 = bytes1_arg;
5137 size_t bytes2 = bytes2_arg;
5138 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005140 uint8_t *output_buffer = NULL;
5141 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005142 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5143 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005144 size_t length;
5145
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005146 ASSERT_ALLOC( output_buffer, capacity );
5147 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005148 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005149
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005150 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5151 psa_set_key_algorithm( &base_attributes, alg );
5152 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005153 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005154 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005155
5156 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005157 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005158 input1->x, input1->len,
5159 input2->x, input2->len, capacity ) )
5160 goto exit;
5161
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005163 output_buffer,
5164 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005165 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005166
5167 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005168 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005169 input1->x, input1->len,
5170 input2->x, input2->len, capacity ) )
5171 goto exit;
5172
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005173 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5174 psa_set_key_algorithm( &derived_attributes, 0 );
5175 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005176 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005177 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005178 &derived_key ) );
5179 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005180 export_buffer, bytes1,
5181 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005182 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005183 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005184 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005185 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005186 &derived_key ) );
5187 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005188 export_buffer + bytes1, bytes2,
5189 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005190 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005191
5192 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005193 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5194 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005195
5196exit:
5197 mbedtls_free( output_buffer );
5198 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005200 psa_destroy_key( base_key );
5201 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005202 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005203}
5204/* END_CASE */
5205
5206/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005207void derive_key( int alg_arg,
5208 data_t *key_data, data_t *input1, data_t *input2,
5209 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005210 int expected_status_arg,
5211 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005212{
Ronald Cron5425a212020-08-04 14:58:35 +02005213 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5214 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005215 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005216 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005217 size_t bits = bits_arg;
5218 psa_status_t expected_status = expected_status_arg;
5219 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5220 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5221 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5222
5223 PSA_ASSERT( psa_crypto_init( ) );
5224
5225 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5226 psa_set_key_algorithm( &base_attributes, alg );
5227 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5228 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005229 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005230
Ronald Cron5425a212020-08-04 14:58:35 +02005231 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005232 input1->x, input1->len,
5233 input2->x, input2->len, SIZE_MAX ) )
5234 goto exit;
5235
5236 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5237 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005238 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005239 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005240
5241 psa_status_t status =
5242 psa_key_derivation_output_key( &derived_attributes,
5243 &operation,
5244 &derived_key );
5245 if( is_large_output > 0 )
5246 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5247 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005248
5249exit:
5250 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005251 psa_destroy_key( base_key );
5252 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005253 PSA_DONE( );
5254}
5255/* END_CASE */
5256
5257/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005258void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005259 int our_key_type_arg, int our_key_alg_arg,
5260 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005261 int expected_status_arg )
5262{
Ronald Cron5425a212020-08-04 14:58:35 +02005263 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005264 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005265 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005266 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005269 psa_status_t expected_status = expected_status_arg;
5270 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005271
Gilles Peskine8817f612018-12-18 00:18:46 +01005272 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005273
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005274 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005275 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005276 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005277 PSA_ASSERT( psa_import_key( &attributes,
5278 our_key_data->x, our_key_data->len,
5279 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005280
Gilles Peskine77f40d82019-04-11 21:27:06 +02005281 /* The tests currently include inputs that should fail at either step.
5282 * Test cases that fail at the setup step should be changed to call
5283 * key_derivation_setup instead, and this function should be renamed
5284 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005285 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005286 if( status == PSA_SUCCESS )
5287 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005288 TEST_EQUAL( psa_key_derivation_key_agreement(
5289 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5290 our_key,
5291 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005292 expected_status );
5293 }
5294 else
5295 {
5296 TEST_ASSERT( status == expected_status );
5297 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005298
5299exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005301 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005302 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005303}
5304/* END_CASE */
5305
5306/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005307void raw_key_agreement( int alg_arg,
5308 int our_key_type_arg, data_t *our_key_data,
5309 data_t *peer_key_data,
5310 data_t *expected_output )
5311{
Ronald Cron5425a212020-08-04 14:58:35 +02005312 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005313 psa_algorithm_t alg = alg_arg;
5314 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005316 unsigned char *output = NULL;
5317 size_t output_length = ~0;
5318
5319 ASSERT_ALLOC( output, expected_output->len );
5320 PSA_ASSERT( psa_crypto_init( ) );
5321
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005322 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5323 psa_set_key_algorithm( &attributes, alg );
5324 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005325 PSA_ASSERT( psa_import_key( &attributes,
5326 our_key_data->x, our_key_data->len,
5327 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005328
Gilles Peskinebe697d82019-05-16 18:00:41 +02005329 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5330 peer_key_data->x, peer_key_data->len,
5331 output, expected_output->len,
5332 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005333 ASSERT_COMPARE( output, output_length,
5334 expected_output->x, expected_output->len );
5335
5336exit:
5337 mbedtls_free( output );
5338 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005339 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005340}
5341/* END_CASE */
5342
5343/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005344void key_agreement_capacity( int alg_arg,
5345 int our_key_type_arg, data_t *our_key_data,
5346 data_t *peer_key_data,
5347 int expected_capacity_arg )
5348{
Ronald Cron5425a212020-08-04 14:58:35 +02005349 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005350 psa_algorithm_t alg = alg_arg;
5351 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005352 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005354 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005355 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005356
Gilles Peskine8817f612018-12-18 00:18:46 +01005357 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005358
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005359 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5360 psa_set_key_algorithm( &attributes, alg );
5361 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005362 PSA_ASSERT( psa_import_key( &attributes,
5363 our_key_data->x, our_key_data->len,
5364 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005365
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005366 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005367 PSA_ASSERT( psa_key_derivation_key_agreement(
5368 &operation,
5369 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5370 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005371 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5372 {
5373 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005374 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005375 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005376 NULL, 0 ) );
5377 }
Gilles Peskine59685592018-09-18 12:11:34 +02005378
Gilles Peskinebf491972018-10-25 22:36:12 +02005379 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005380 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005381 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005382 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005383
Gilles Peskinebf491972018-10-25 22:36:12 +02005384 /* Test the actual capacity by reading the output. */
5385 while( actual_capacity > sizeof( output ) )
5386 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005387 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005388 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005389 actual_capacity -= sizeof( output );
5390 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005391 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005392 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005394 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005395
Gilles Peskine59685592018-09-18 12:11:34 +02005396exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005397 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005398 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005399 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005400}
5401/* END_CASE */
5402
5403/* BEGIN_CASE */
5404void key_agreement_output( int alg_arg,
5405 int our_key_type_arg, data_t *our_key_data,
5406 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005407 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005408{
Ronald Cron5425a212020-08-04 14:58:35 +02005409 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005410 psa_algorithm_t alg = alg_arg;
5411 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005412 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005413 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005414 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005415
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005416 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5417 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005418
Gilles Peskine8817f612018-12-18 00:18:46 +01005419 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005420
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005421 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5422 psa_set_key_algorithm( &attributes, alg );
5423 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005424 PSA_ASSERT( psa_import_key( &attributes,
5425 our_key_data->x, our_key_data->len,
5426 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005427
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005428 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005429 PSA_ASSERT( psa_key_derivation_key_agreement(
5430 &operation,
5431 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5432 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005433 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5434 {
5435 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005436 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005437 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005438 NULL, 0 ) );
5439 }
Gilles Peskine59685592018-09-18 12:11:34 +02005440
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005441 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005442 actual_output,
5443 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005444 ASSERT_COMPARE( actual_output, expected_output1->len,
5445 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005446 if( expected_output2->len != 0 )
5447 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005448 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005449 actual_output,
5450 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005451 ASSERT_COMPARE( actual_output, expected_output2->len,
5452 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005453 }
Gilles Peskine59685592018-09-18 12:11:34 +02005454
5455exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005456 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005457 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005458 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005459 mbedtls_free( actual_output );
5460}
5461/* END_CASE */
5462
5463/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005464void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005465{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005466 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005467 unsigned char *output = NULL;
5468 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005469 size_t i;
5470 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005471
Simon Butcher49f8e312020-03-03 15:51:50 +00005472 TEST_ASSERT( bytes_arg >= 0 );
5473
Gilles Peskine91892022021-02-08 19:50:26 +01005474 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005475 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005476
Gilles Peskine8817f612018-12-18 00:18:46 +01005477 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005478
Gilles Peskinea50d7392018-06-21 10:22:13 +02005479 /* Run several times, to ensure that every output byte will be
5480 * nonzero at least once with overwhelming probability
5481 * (2^(-8*number_of_runs)). */
5482 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005483 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005484 if( bytes != 0 )
5485 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005486 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005487
Gilles Peskinea50d7392018-06-21 10:22:13 +02005488 for( i = 0; i < bytes; i++ )
5489 {
5490 if( output[i] != 0 )
5491 ++changed[i];
5492 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005493 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005494
5495 /* Check that every byte was changed to nonzero at least once. This
5496 * validates that psa_generate_random is overwriting every byte of
5497 * the output buffer. */
5498 for( i = 0; i < bytes; i++ )
5499 {
5500 TEST_ASSERT( changed[i] != 0 );
5501 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005502
5503exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005504 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005505 mbedtls_free( output );
5506 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005507}
5508/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005509
5510/* BEGIN_CASE */
5511void generate_key( int type_arg,
5512 int bits_arg,
5513 int usage_arg,
5514 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005515 int expected_status_arg,
5516 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005517{
Ronald Cron5425a212020-08-04 14:58:35 +02005518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005519 psa_key_type_t type = type_arg;
5520 psa_key_usage_t usage = usage_arg;
5521 size_t bits = bits_arg;
5522 psa_algorithm_t alg = alg_arg;
5523 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005524 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005525 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005526
Gilles Peskine8817f612018-12-18 00:18:46 +01005527 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005528
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005529 psa_set_key_usage_flags( &attributes, usage );
5530 psa_set_key_algorithm( &attributes, alg );
5531 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005532 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005533
5534 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005535 psa_status_t status = psa_generate_key( &attributes, &key );
5536
5537 if( is_large_key > 0 )
5538 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5539 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005540 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005541 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005542
5543 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005544 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005545 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5546 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005547
Gilles Peskine818ca122018-06-20 18:16:48 +02005548 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005549 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005550 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005551
5552exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005553 /*
5554 * Key attributes may have been returned by psa_get_key_attributes()
5555 * thus reset them as required.
5556 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005557 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005558
Ronald Cron5425a212020-08-04 14:58:35 +02005559 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005560 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005561}
5562/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005563
Gilles Peskinee56e8782019-04-26 17:34:02 +02005564/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5565void generate_key_rsa( int bits_arg,
5566 data_t *e_arg,
5567 int expected_status_arg )
5568{
Ronald Cron5425a212020-08-04 14:58:35 +02005569 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005570 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005571 size_t bits = bits_arg;
5572 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5573 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5574 psa_status_t expected_status = expected_status_arg;
5575 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5576 uint8_t *exported = NULL;
5577 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005578 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005579 size_t exported_length = SIZE_MAX;
5580 uint8_t *e_read_buffer = NULL;
5581 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005582 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005583 size_t e_read_length = SIZE_MAX;
5584
5585 if( e_arg->len == 0 ||
5586 ( e_arg->len == 3 &&
5587 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5588 {
5589 is_default_public_exponent = 1;
5590 e_read_size = 0;
5591 }
5592 ASSERT_ALLOC( e_read_buffer, e_read_size );
5593 ASSERT_ALLOC( exported, exported_size );
5594
5595 PSA_ASSERT( psa_crypto_init( ) );
5596
5597 psa_set_key_usage_flags( &attributes, usage );
5598 psa_set_key_algorithm( &attributes, alg );
5599 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5600 e_arg->x, e_arg->len ) );
5601 psa_set_key_bits( &attributes, bits );
5602
5603 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005604 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005605 if( expected_status != PSA_SUCCESS )
5606 goto exit;
5607
5608 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005609 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005610 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5611 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5612 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5613 e_read_buffer, e_read_size,
5614 &e_read_length ) );
5615 if( is_default_public_exponent )
5616 TEST_EQUAL( e_read_length, 0 );
5617 else
5618 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5619
5620 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005621 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005622 goto exit;
5623
5624 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005625 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005626 exported, exported_size,
5627 &exported_length ) );
5628 {
5629 uint8_t *p = exported;
5630 uint8_t *end = exported + exported_length;
5631 size_t len;
5632 /* RSAPublicKey ::= SEQUENCE {
5633 * modulus INTEGER, -- n
5634 * publicExponent INTEGER } -- e
5635 */
5636 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005637 MBEDTLS_ASN1_SEQUENCE |
5638 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005639 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5640 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5641 MBEDTLS_ASN1_INTEGER ) );
5642 if( len >= 1 && p[0] == 0 )
5643 {
5644 ++p;
5645 --len;
5646 }
5647 if( e_arg->len == 0 )
5648 {
5649 TEST_EQUAL( len, 3 );
5650 TEST_EQUAL( p[0], 1 );
5651 TEST_EQUAL( p[1], 0 );
5652 TEST_EQUAL( p[2], 1 );
5653 }
5654 else
5655 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5656 }
5657
5658exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005659 /*
5660 * Key attributes may have been returned by psa_get_key_attributes() or
5661 * set by psa_set_key_domain_parameters() thus reset them as required.
5662 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005663 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005664
Ronald Cron5425a212020-08-04 14:58:35 +02005665 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005666 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005667 mbedtls_free( e_read_buffer );
5668 mbedtls_free( exported );
5669}
5670/* END_CASE */
5671
Darryl Greend49a4992018-06-18 17:27:26 +01005672/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005673void persistent_key_load_key_from_storage( data_t *data,
5674 int type_arg, int bits_arg,
5675 int usage_flags_arg, int alg_arg,
5676 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005677{
Ronald Cron71016a92020-08-28 19:01:50 +02005678 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005679 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005680 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5681 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005682 psa_key_type_t type = type_arg;
5683 size_t bits = bits_arg;
5684 psa_key_usage_t usage_flags = usage_flags_arg;
5685 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005687 unsigned char *first_export = NULL;
5688 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005689 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005690 size_t first_exported_length;
5691 size_t second_exported_length;
5692
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005693 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5694 {
5695 ASSERT_ALLOC( first_export, export_size );
5696 ASSERT_ALLOC( second_export, export_size );
5697 }
Darryl Greend49a4992018-06-18 17:27:26 +01005698
Gilles Peskine8817f612018-12-18 00:18:46 +01005699 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005700
Gilles Peskinec87af662019-05-15 16:12:22 +02005701 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005702 psa_set_key_usage_flags( &attributes, usage_flags );
5703 psa_set_key_algorithm( &attributes, alg );
5704 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005705 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005706
Darryl Green0c6575a2018-11-07 16:05:30 +00005707 switch( generation_method )
5708 {
5709 case IMPORT_KEY:
5710 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005711 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005712 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005713 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005714
Darryl Green0c6575a2018-11-07 16:05:30 +00005715 case GENERATE_KEY:
5716 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005717 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005718 break;
5719
5720 case DERIVE_KEY:
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005721#if PSA_WANT_ALG_HKDF && PSA_WANT_ALG_SHA_256
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005722 {
5723 /* Create base key */
5724 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5725 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5726 psa_set_key_usage_flags( &base_attributes,
5727 PSA_KEY_USAGE_DERIVE );
5728 psa_set_key_algorithm( &base_attributes, derive_alg );
5729 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005730 PSA_ASSERT( psa_import_key( &base_attributes,
5731 data->x, data->len,
5732 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005733 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005734 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005735 PSA_ASSERT( psa_key_derivation_input_key(
5736 &operation,
5737 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005738 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005739 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005740 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005741 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5742 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005743 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005745 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005746 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005747 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005748#else
5749 TEST_ASSUME( ! "KDF not supported in this configuration" );
5750#endif
5751 break;
5752
5753 default:
5754 TEST_ASSERT( ! "generation_method not implemented in test" );
5755 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005756 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005757 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005758
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005759 /* Export the key if permitted by the key policy. */
5760 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5761 {
Ronald Cron5425a212020-08-04 14:58:35 +02005762 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005763 first_export, export_size,
5764 &first_exported_length ) );
5765 if( generation_method == IMPORT_KEY )
5766 ASSERT_COMPARE( data->x, data->len,
5767 first_export, first_exported_length );
5768 }
Darryl Greend49a4992018-06-18 17:27:26 +01005769
5770 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005771 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005772 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005773 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005774
Darryl Greend49a4992018-06-18 17:27:26 +01005775 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005776 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005777 TEST_ASSERT( mbedtls_svc_key_id_equal(
5778 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005779 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5780 PSA_KEY_LIFETIME_PERSISTENT );
5781 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5782 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5783 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5784 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005785
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005786 /* Export the key again if permitted by the key policy. */
5787 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005788 {
Ronald Cron5425a212020-08-04 14:58:35 +02005789 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005790 second_export, export_size,
5791 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005792 ASSERT_COMPARE( first_export, first_exported_length,
5793 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005794 }
5795
5796 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005797 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005798 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005799
5800exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005801 /*
5802 * Key attributes may have been returned by psa_get_key_attributes()
5803 * thus reset them as required.
5804 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005805 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005806
Darryl Greend49a4992018-06-18 17:27:26 +01005807 mbedtls_free( first_export );
5808 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005809 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005810 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005811 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005812 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005813}
5814/* END_CASE */