blob: 93f41b56deb791fbf978a4a15638cef4920f93b8 [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 );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003877 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003879
Gilles Peskine4abf7412018-06-18 16:35:34 +02003880 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003881 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3882 * should be exact. */
3883 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3884 TEST_EQUAL( output_size,
3885 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003886 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003887
Gilles Peskine8817f612018-12-18 00:18:46 +01003888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003889
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003890 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3891 psa_set_key_algorithm( &attributes, alg );
3892 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003893
Gilles Peskine049c7532019-05-15 20:22:09 +02003894 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003895 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003896
Ronald Cron5425a212020-08-04 14:58:35 +02003897 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003898 nonce->x, nonce->len,
3899 additional_data->x,
3900 additional_data->len,
3901 input_data->x, input_data->len,
3902 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003903 &output_length ),
3904 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003905
3906 if( PSA_SUCCESS == expected_result )
3907 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003908 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003909
Gilles Peskine003a4a92019-05-14 16:09:40 +02003910 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3911 * should be exact. */
3912 TEST_EQUAL( input_data->len,
3913 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3914
Ronald Cron5425a212020-08-04 14:58:35 +02003915 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003916 nonce->x, nonce->len,
3917 additional_data->x,
3918 additional_data->len,
3919 output_data, output_length,
3920 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003921 &output_length2 ),
3922 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003923
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003924 ASSERT_COMPARE( input_data->x, input_data->len,
3925 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003926 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003927
Gilles Peskinea1cac842018-06-11 19:33:02 +02003928exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003929 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003930 mbedtls_free( output_data );
3931 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003932 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003933}
3934/* END_CASE */
3935
3936/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003937void aead_encrypt( int key_type_arg, data_t *key_data,
3938 int alg_arg,
3939 data_t *nonce,
3940 data_t *additional_data,
3941 data_t *input_data,
3942 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003943{
Ronald Cron5425a212020-08-04 14:58:35 +02003944 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003945 psa_key_type_t key_type = key_type_arg;
3946 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003947 unsigned char *output_data = NULL;
3948 size_t output_size = 0;
3949 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003950 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003951 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003952 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003953
Gilles Peskine4abf7412018-06-18 16:35:34 +02003954 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003955 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3956 * should be exact. */
3957 TEST_EQUAL( output_size,
3958 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003959 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003960
Gilles Peskine8817f612018-12-18 00:18:46 +01003961 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003962
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3964 psa_set_key_algorithm( &attributes, alg );
3965 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003966
Gilles Peskine049c7532019-05-15 20:22:09 +02003967 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003968 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003969
Steven Cooremand588ea12021-01-11 19:36:04 +01003970 status = psa_aead_encrypt( key, alg,
3971 nonce->x, nonce->len,
3972 additional_data->x, additional_data->len,
3973 input_data->x, input_data->len,
3974 output_data, output_size,
3975 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003976
Ronald Cron28a45ed2021-02-09 20:35:42 +01003977 /* If the operation is not supported, just skip and not fail in case the
3978 * encryption involves a common limitation of cryptography hardwares and
3979 * an alternative implementation. */
3980 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003981 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003982 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3983 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003984 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003985
3986 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003987 ASSERT_COMPARE( expected_result->x, expected_result->len,
3988 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003989
Gilles Peskinea1cac842018-06-11 19:33:02 +02003990exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003991 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003992 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003993 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003994}
3995/* END_CASE */
3996
3997/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003998void aead_decrypt( int key_type_arg, data_t *key_data,
3999 int alg_arg,
4000 data_t *nonce,
4001 data_t *additional_data,
4002 data_t *input_data,
4003 data_t *expected_data,
4004 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004005{
Ronald Cron5425a212020-08-04 14:58:35 +02004006 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004007 psa_key_type_t key_type = key_type_arg;
4008 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004009 unsigned char *output_data = NULL;
4010 size_t output_size = 0;
4011 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004012 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004014 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004015 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004016
Gilles Peskine003a4a92019-05-14 16:09:40 +02004017 output_size = input_data->len - tag_length;
4018 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4019 * should be exact. */
4020 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4021 TEST_EQUAL( output_size,
4022 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004023 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004024
Gilles Peskine8817f612018-12-18 00:18:46 +01004025 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004026
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004027 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4028 psa_set_key_algorithm( &attributes, alg );
4029 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004030
Gilles Peskine049c7532019-05-15 20:22:09 +02004031 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004032 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004033
Steven Cooremand588ea12021-01-11 19:36:04 +01004034 status = psa_aead_decrypt( key, alg,
4035 nonce->x, nonce->len,
4036 additional_data->x,
4037 additional_data->len,
4038 input_data->x, input_data->len,
4039 output_data, output_size,
4040 &output_length );
4041
Ronald Cron28a45ed2021-02-09 20:35:42 +01004042 /* If the operation is not supported, just skip and not fail in case the
4043 * decryption involves a common limitation of cryptography hardwares and
4044 * an alternative implementation. */
4045 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004046 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004047 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4048 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004049 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004050
4051 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004052
Gilles Peskine2d277862018-06-18 15:41:12 +02004053 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004054 ASSERT_COMPARE( expected_data->x, expected_data->len,
4055 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004056
Gilles Peskinea1cac842018-06-11 19:33:02 +02004057exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004058 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004059 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004060 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004061}
4062/* END_CASE */
4063
4064/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004065void signature_size( int type_arg,
4066 int bits,
4067 int alg_arg,
4068 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004069{
4070 psa_key_type_t type = type_arg;
4071 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004072 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004073
Gilles Peskinefe11b722018-12-18 00:24:04 +01004074 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004075#if defined(MBEDTLS_TEST_DEPRECATED)
4076 TEST_EQUAL( actual_size,
4077 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4078#endif /* MBEDTLS_TEST_DEPRECATED */
4079
Gilles Peskinee59236f2018-01-27 23:32:46 +01004080exit:
4081 ;
4082}
4083/* END_CASE */
4084
4085/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004086void sign_deterministic( int key_type_arg, data_t *key_data,
4087 int alg_arg, data_t *input_data,
4088 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004089{
Ronald Cron5425a212020-08-04 14:58:35 +02004090 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004091 psa_key_type_t key_type = key_type_arg;
4092 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004093 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004094 unsigned char *signature = NULL;
4095 size_t signature_size;
4096 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004098
Gilles Peskine8817f612018-12-18 00:18:46 +01004099 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004100
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004101 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004102 psa_set_key_algorithm( &attributes, alg );
4103 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004104
Gilles Peskine049c7532019-05-15 20:22:09 +02004105 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004106 &key ) );
4107 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004108 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004109
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004110 /* Allocate a buffer which has the size advertized by the
4111 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004112 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004113 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004114 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004115 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004116 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004117
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004118 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004119 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004120 input_data->x, input_data->len,
4121 signature, signature_size,
4122 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004123 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004124 ASSERT_COMPARE( output_data->x, output_data->len,
4125 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004126
Gilles Peskine0627f982019-11-26 19:12:16 +01004127#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004128 memset( signature, 0, signature_size );
4129 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004130 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004131 input_data->x, input_data->len,
4132 signature, signature_size,
4133 &signature_length ) );
4134 ASSERT_COMPARE( output_data->x, output_data->len,
4135 signature, signature_length );
4136#endif /* MBEDTLS_TEST_DEPRECATED */
4137
Gilles Peskine20035e32018-02-03 22:44:14 +01004138exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004139 /*
4140 * Key attributes may have been returned by psa_get_key_attributes()
4141 * thus reset them as required.
4142 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004143 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004144
Ronald Cron5425a212020-08-04 14:58:35 +02004145 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004146 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004147 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004148}
4149/* END_CASE */
4150
4151/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004152void sign_fail( int key_type_arg, data_t *key_data,
4153 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004154 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004155{
Ronald Cron5425a212020-08-04 14:58:35 +02004156 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004157 psa_key_type_t key_type = key_type_arg;
4158 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004159 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004160 psa_status_t actual_status;
4161 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004162 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004163 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004165
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004166 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004167
Gilles Peskine8817f612018-12-18 00:18:46 +01004168 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004169
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004170 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004171 psa_set_key_algorithm( &attributes, alg );
4172 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004173
Gilles Peskine049c7532019-05-15 20:22:09 +02004174 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004175 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004176
Ronald Cron5425a212020-08-04 14:58:35 +02004177 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004178 input_data->x, input_data->len,
4179 signature, signature_size,
4180 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004181 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004182 /* The value of *signature_length is unspecified on error, but
4183 * whatever it is, it should be less than signature_size, so that
4184 * if the caller tries to read *signature_length bytes without
4185 * checking the error code then they don't overflow a buffer. */
4186 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004187
Gilles Peskine895242b2019-11-29 12:15:40 +01004188#if defined(MBEDTLS_TEST_DEPRECATED)
4189 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004190 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004191 input_data->x, input_data->len,
4192 signature, signature_size,
4193 &signature_length ),
4194 expected_status );
4195 TEST_ASSERT( signature_length <= signature_size );
4196#endif /* MBEDTLS_TEST_DEPRECATED */
4197
Gilles Peskine20035e32018-02-03 22:44:14 +01004198exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004199 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004200 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004201 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004202 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004203}
4204/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004205
4206/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004207void sign_verify( int key_type_arg, data_t *key_data,
4208 int alg_arg, data_t *input_data )
4209{
Ronald Cron5425a212020-08-04 14:58:35 +02004210 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004211 psa_key_type_t key_type = key_type_arg;
4212 psa_algorithm_t alg = alg_arg;
4213 size_t key_bits;
4214 unsigned char *signature = NULL;
4215 size_t signature_size;
4216 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004218
Gilles Peskine8817f612018-12-18 00:18:46 +01004219 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004220
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004221 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004222 psa_set_key_algorithm( &attributes, alg );
4223 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004224
Gilles Peskine049c7532019-05-15 20:22:09 +02004225 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004226 &key ) );
4227 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004228 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004229
4230 /* Allocate a buffer which has the size advertized by the
4231 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004232 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004233 key_bits, alg );
4234 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004235 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004236 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004237
4238 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004239 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004240 input_data->x, input_data->len,
4241 signature, signature_size,
4242 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004243 /* Check that the signature length looks sensible. */
4244 TEST_ASSERT( signature_length <= signature_size );
4245 TEST_ASSERT( signature_length > 0 );
4246
4247 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004248 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004249 input_data->x, input_data->len,
4250 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004251
4252 if( input_data->len != 0 )
4253 {
4254 /* Flip a bit in the input and verify that the signature is now
4255 * detected as invalid. Flip a bit at the beginning, not at the end,
4256 * because ECDSA may ignore the last few bits of the input. */
4257 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004258 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004259 input_data->x, input_data->len,
4260 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004261 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004262 }
4263
4264exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004265 /*
4266 * Key attributes may have been returned by psa_get_key_attributes()
4267 * thus reset them as required.
4268 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004269 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004270
Ronald Cron5425a212020-08-04 14:58:35 +02004271 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004272 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004273 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004274}
4275/* END_CASE */
4276
4277/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004278void asymmetric_verify( int key_type_arg, data_t *key_data,
4279 int alg_arg, data_t *hash_data,
4280 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004281{
Ronald Cron5425a212020-08-04 14:58:35 +02004282 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004283 psa_key_type_t key_type = key_type_arg;
4284 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004286
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004287 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004288
Gilles Peskine8817f612018-12-18 00:18:46 +01004289 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004290
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004291 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004292 psa_set_key_algorithm( &attributes, alg );
4293 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004294
Gilles Peskine049c7532019-05-15 20:22:09 +02004295 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004296 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004297
Ronald Cron5425a212020-08-04 14:58:35 +02004298 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004299 hash_data->x, hash_data->len,
4300 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004301
4302#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004303 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004304 hash_data->x, hash_data->len,
4305 signature_data->x,
4306 signature_data->len ) );
4307
4308#endif /* MBEDTLS_TEST_DEPRECATED */
4309
itayzafrir5c753392018-05-08 11:18:38 +03004310exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004311 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004312 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004313 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004314}
4315/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004316
4317/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004318void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4319 int alg_arg, data_t *hash_data,
4320 data_t *signature_data,
4321 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004322{
Ronald Cron5425a212020-08-04 14:58:35 +02004323 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004324 psa_key_type_t key_type = key_type_arg;
4325 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004326 psa_status_t actual_status;
4327 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004328 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004329
Gilles Peskine8817f612018-12-18 00:18:46 +01004330 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004331
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004332 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004333 psa_set_key_algorithm( &attributes, alg );
4334 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004335
Gilles Peskine049c7532019-05-15 20:22:09 +02004336 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004337 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004338
Ronald Cron5425a212020-08-04 14:58:35 +02004339 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004340 hash_data->x, hash_data->len,
4341 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004342 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004343
Gilles Peskine895242b2019-11-29 12:15:40 +01004344#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004345 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004346 hash_data->x, hash_data->len,
4347 signature_data->x, signature_data->len ),
4348 expected_status );
4349#endif /* MBEDTLS_TEST_DEPRECATED */
4350
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004351exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004352 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004353 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004354 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004355}
4356/* END_CASE */
4357
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004358/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004359void asymmetric_encrypt( int key_type_arg,
4360 data_t *key_data,
4361 int alg_arg,
4362 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004363 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004364 int expected_output_length_arg,
4365 int expected_status_arg )
4366{
Ronald Cron5425a212020-08-04 14:58:35 +02004367 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004368 psa_key_type_t key_type = key_type_arg;
4369 psa_algorithm_t alg = alg_arg;
4370 size_t expected_output_length = expected_output_length_arg;
4371 size_t key_bits;
4372 unsigned char *output = NULL;
4373 size_t output_size;
4374 size_t output_length = ~0;
4375 psa_status_t actual_status;
4376 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004378
Gilles Peskine8817f612018-12-18 00:18:46 +01004379 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004380
Gilles Peskine656896e2018-06-29 19:12:28 +02004381 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004382 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4383 psa_set_key_algorithm( &attributes, alg );
4384 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004385 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004386 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004387
4388 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004389 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004390 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004391 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004392 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004393
4394 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004395 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004396 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004397 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004398 output, output_size,
4399 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004400 TEST_EQUAL( actual_status, expected_status );
4401 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004402
Gilles Peskine68428122018-06-30 18:42:41 +02004403 /* If the label is empty, the test framework puts a non-null pointer
4404 * in label->x. Test that a null pointer works as well. */
4405 if( label->len == 0 )
4406 {
4407 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004408 if( output_size != 0 )
4409 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004410 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004411 input_data->x, input_data->len,
4412 NULL, label->len,
4413 output, output_size,
4414 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004415 TEST_EQUAL( actual_status, expected_status );
4416 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004417 }
4418
Gilles Peskine656896e2018-06-29 19:12:28 +02004419exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004420 /*
4421 * Key attributes may have been returned by psa_get_key_attributes()
4422 * thus reset them as required.
4423 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004424 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004425
Ronald Cron5425a212020-08-04 14:58:35 +02004426 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004427 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004428 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004429}
4430/* END_CASE */
4431
4432/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004433void asymmetric_encrypt_decrypt( int key_type_arg,
4434 data_t *key_data,
4435 int alg_arg,
4436 data_t *input_data,
4437 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004438{
Ronald Cron5425a212020-08-04 14:58:35 +02004439 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004440 psa_key_type_t key_type = key_type_arg;
4441 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004442 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004443 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004444 size_t output_size;
4445 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004446 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004447 size_t output2_size;
4448 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004450
Gilles Peskine8817f612018-12-18 00:18:46 +01004451 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004452
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004453 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4454 psa_set_key_algorithm( &attributes, alg );
4455 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004456
Gilles Peskine049c7532019-05-15 20:22:09 +02004457 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004458 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004459
4460 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004461 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004462 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004463 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004464 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004465 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004466 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004467
Gilles Peskineeebd7382018-06-08 18:11:54 +02004468 /* We test encryption by checking that encrypt-then-decrypt gives back
4469 * the original plaintext because of the non-optional random
4470 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004471 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004472 input_data->x, input_data->len,
4473 label->x, label->len,
4474 output, output_size,
4475 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004476 /* We don't know what ciphertext length to expect, but check that
4477 * it looks sensible. */
4478 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004479
Ronald Cron5425a212020-08-04 14:58:35 +02004480 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004481 output, output_length,
4482 label->x, label->len,
4483 output2, output2_size,
4484 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004485 ASSERT_COMPARE( input_data->x, input_data->len,
4486 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004487
4488exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004489 /*
4490 * Key attributes may have been returned by psa_get_key_attributes()
4491 * thus reset them as required.
4492 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004493 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004494
Ronald Cron5425a212020-08-04 14:58:35 +02004495 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004496 mbedtls_free( output );
4497 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004498 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004499}
4500/* END_CASE */
4501
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004502/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004503void asymmetric_decrypt( int key_type_arg,
4504 data_t *key_data,
4505 int alg_arg,
4506 data_t *input_data,
4507 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004508 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004509{
Ronald Cron5425a212020-08-04 14:58:35 +02004510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004511 psa_key_type_t key_type = key_type_arg;
4512 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004513 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004514 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004515 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004517
Jaeden Amero412654a2019-02-06 12:57:46 +00004518 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004519 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004520
Gilles Peskine8817f612018-12-18 00:18:46 +01004521 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004522
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004523 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4524 psa_set_key_algorithm( &attributes, alg );
4525 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004526
Gilles Peskine049c7532019-05-15 20:22:09 +02004527 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004528 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004529
Ronald Cron5425a212020-08-04 14:58:35 +02004530 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004531 input_data->x, input_data->len,
4532 label->x, label->len,
4533 output,
4534 output_size,
4535 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004536 ASSERT_COMPARE( expected_data->x, expected_data->len,
4537 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004538
Gilles Peskine68428122018-06-30 18:42:41 +02004539 /* If the label is empty, the test framework puts a non-null pointer
4540 * in label->x. Test that a null pointer works as well. */
4541 if( label->len == 0 )
4542 {
4543 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004544 if( output_size != 0 )
4545 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004546 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004547 input_data->x, input_data->len,
4548 NULL, label->len,
4549 output,
4550 output_size,
4551 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004552 ASSERT_COMPARE( expected_data->x, expected_data->len,
4553 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004554 }
4555
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004556exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004557 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004558 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004559 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004560 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004561}
4562/* END_CASE */
4563
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004564/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004565void asymmetric_decrypt_fail( int key_type_arg,
4566 data_t *key_data,
4567 int alg_arg,
4568 data_t *input_data,
4569 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004570 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004571 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004572{
Ronald Cron5425a212020-08-04 14:58:35 +02004573 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004574 psa_key_type_t key_type = key_type_arg;
4575 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004576 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004577 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004578 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004579 psa_status_t actual_status;
4580 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004581 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004582
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004583 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004584
Gilles Peskine8817f612018-12-18 00:18:46 +01004585 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004586
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004587 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4588 psa_set_key_algorithm( &attributes, alg );
4589 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004590
Gilles Peskine049c7532019-05-15 20:22:09 +02004591 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004592 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004593
Ronald Cron5425a212020-08-04 14:58:35 +02004594 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004595 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004596 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004597 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004598 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004599 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004600 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004601
Gilles Peskine68428122018-06-30 18:42:41 +02004602 /* If the label is empty, the test framework puts a non-null pointer
4603 * in label->x. Test that a null pointer works as well. */
4604 if( label->len == 0 )
4605 {
4606 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004607 if( output_size != 0 )
4608 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004609 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004610 input_data->x, input_data->len,
4611 NULL, label->len,
4612 output, output_size,
4613 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004614 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004615 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004616 }
4617
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004618exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004619 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004620 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004621 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004622 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004623}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004624/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004625
4626/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004627void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004628{
4629 /* Test each valid way of initializing the object, except for `= {0}`, as
4630 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4631 * though it's OK by the C standard. We could test for this, but we'd need
4632 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004633 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004634 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4635 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4636 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004637
4638 memset( &zero, 0, sizeof( zero ) );
4639
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004640 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004641 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004642 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004643 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004644 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004645 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004646 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004647
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004649 PSA_ASSERT( psa_key_derivation_abort(&func) );
4650 PSA_ASSERT( psa_key_derivation_abort(&init) );
4651 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004652}
4653/* END_CASE */
4654
Janos Follath16de4a42019-06-13 16:32:24 +01004655/* BEGIN_CASE */
4656void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004657{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004658 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004659 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004660 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004661
Gilles Peskine8817f612018-12-18 00:18:46 +01004662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004663
Janos Follath16de4a42019-06-13 16:32:24 +01004664 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004665 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004666
4667exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004669 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004670}
4671/* END_CASE */
4672
Janos Follathaf3c2a02019-06-12 12:34:34 +01004673/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004674void derive_set_capacity( int alg_arg, int capacity_arg,
4675 int expected_status_arg )
4676{
4677 psa_algorithm_t alg = alg_arg;
4678 size_t capacity = capacity_arg;
4679 psa_status_t expected_status = expected_status_arg;
4680 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4681
4682 PSA_ASSERT( psa_crypto_init( ) );
4683
4684 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4685
4686 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4687 expected_status );
4688
4689exit:
4690 psa_key_derivation_abort( &operation );
4691 PSA_DONE( );
4692}
4693/* END_CASE */
4694
4695/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004696void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004697 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004698 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004699 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004700 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004701 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004702 int expected_status_arg3,
4703 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004704{
4705 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004706 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4707 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004708 psa_status_t expected_statuses[] = {expected_status_arg1,
4709 expected_status_arg2,
4710 expected_status_arg3};
4711 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004712 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4713 MBEDTLS_SVC_KEY_ID_INIT,
4714 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004715 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4716 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4717 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004718 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004719 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004720 psa_status_t expected_output_status = expected_output_status_arg;
4721 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004722
4723 PSA_ASSERT( psa_crypto_init( ) );
4724
4725 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4726 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004727
4728 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4729
4730 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4731 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004732 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004733 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004734 psa_set_key_type( &attributes, key_types[i] );
4735 PSA_ASSERT( psa_import_key( &attributes,
4736 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004737 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004738 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4739 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4740 {
4741 // When taking a private key as secret input, use key agreement
4742 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004743 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004744 expected_statuses[i] );
4745 }
4746 else
4747 {
4748 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004749 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004750 expected_statuses[i] );
4751 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004752 }
4753 else
4754 {
4755 TEST_EQUAL( psa_key_derivation_input_bytes(
4756 &operation, steps[i],
4757 inputs[i]->x, inputs[i]->len ),
4758 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004759 }
4760 }
4761
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004762 if( output_key_type != PSA_KEY_TYPE_NONE )
4763 {
4764 psa_reset_key_attributes( &attributes );
4765 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4766 psa_set_key_bits( &attributes, 8 );
4767 actual_output_status =
4768 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004769 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004770 }
4771 else
4772 {
4773 uint8_t buffer[1];
4774 actual_output_status =
4775 psa_key_derivation_output_bytes( &operation,
4776 buffer, sizeof( buffer ) );
4777 }
4778 TEST_EQUAL( actual_output_status, expected_output_status );
4779
Janos Follathaf3c2a02019-06-12 12:34:34 +01004780exit:
4781 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004782 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4783 psa_destroy_key( keys[i] );
4784 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004785 PSA_DONE( );
4786}
4787/* END_CASE */
4788
Janos Follathd958bb72019-07-03 15:02:16 +01004789/* BEGIN_CASE */
4790void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004791{
Janos Follathd958bb72019-07-03 15:02:16 +01004792 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004794 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004795 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004796 unsigned char input1[] = "Input 1";
4797 size_t input1_length = sizeof( input1 );
4798 unsigned char input2[] = "Input 2";
4799 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004800 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004801 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004802 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4803 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4804 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004806
Gilles Peskine8817f612018-12-18 00:18:46 +01004807 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004808
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004809 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4810 psa_set_key_algorithm( &attributes, alg );
4811 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004812
Gilles Peskine73676cb2019-05-15 20:15:10 +02004813 PSA_ASSERT( psa_import_key( &attributes,
4814 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004815 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004816
4817 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004818 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004819 input1, input1_length,
4820 input2, input2_length,
4821 capacity ) )
4822 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004823
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004825 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004826 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004827
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004829
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004831 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004832
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004833exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004834 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004835 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004836 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004837}
4838/* END_CASE */
4839
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004840/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004841void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004842{
4843 uint8_t output_buffer[16];
4844 size_t buffer_size = 16;
4845 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004846 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004847
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004848 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4849 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004850 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004851
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004853 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004854
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004855 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004856
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004857 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4858 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004859 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004860
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004861 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004862 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004863
4864exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004866}
4867/* END_CASE */
4868
4869/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004870void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004871 int step1_arg, data_t *input1,
4872 int step2_arg, data_t *input2,
4873 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004874 int requested_capacity_arg,
4875 data_t *expected_output1,
4876 data_t *expected_output2 )
4877{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004878 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004879 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4880 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004881 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4882 MBEDTLS_SVC_KEY_ID_INIT,
4883 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004884 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004885 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004886 uint8_t *expected_outputs[2] =
4887 {expected_output1->x, expected_output2->x};
4888 size_t output_sizes[2] =
4889 {expected_output1->len, expected_output2->len};
4890 size_t output_buffer_size = 0;
4891 uint8_t *output_buffer = NULL;
4892 size_t expected_capacity;
4893 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004895 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004896 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004897
4898 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4899 {
4900 if( output_sizes[i] > output_buffer_size )
4901 output_buffer_size = output_sizes[i];
4902 if( output_sizes[i] == 0 )
4903 expected_outputs[i] = NULL;
4904 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004905 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004906 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004907
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004908 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4909 psa_set_key_algorithm( &attributes, alg );
4910 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004911
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004912 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004913 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4914 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4915 requested_capacity ) );
4916 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004917 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004918 switch( steps[i] )
4919 {
4920 case 0:
4921 break;
4922 case PSA_KEY_DERIVATION_INPUT_SECRET:
4923 PSA_ASSERT( psa_import_key( &attributes,
4924 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004925 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004926 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004927 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004928 break;
4929 default:
4930 PSA_ASSERT( psa_key_derivation_input_bytes(
4931 &operation, steps[i],
4932 inputs[i]->x, inputs[i]->len ) );
4933 break;
4934 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004935 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004936
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004937 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004938 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004939 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004940 expected_capacity = requested_capacity;
4941
4942 /* Expansion phase. */
4943 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4944 {
4945 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004946 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004947 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004948 if( expected_capacity == 0 && output_sizes[i] == 0 )
4949 {
4950 /* Reading 0 bytes when 0 bytes are available can go either way. */
4951 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004952 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004953 continue;
4954 }
4955 else if( expected_capacity == 0 ||
4956 output_sizes[i] > expected_capacity )
4957 {
4958 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004959 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004960 expected_capacity = 0;
4961 continue;
4962 }
4963 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004964 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004965 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004966 ASSERT_COMPARE( output_buffer, output_sizes[i],
4967 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004969 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004971 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004972 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004973 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004974 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004975
4976exit:
4977 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004978 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004979 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4980 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004981 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004982}
4983/* END_CASE */
4984
4985/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004986void derive_full( int alg_arg,
4987 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004988 data_t *input1,
4989 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004990 int requested_capacity_arg )
4991{
Ronald Cron5425a212020-08-04 14:58:35 +02004992 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004993 psa_algorithm_t alg = alg_arg;
4994 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004995 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004996 unsigned char output_buffer[16];
4997 size_t expected_capacity = requested_capacity;
4998 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005000
Gilles Peskine8817f612018-12-18 00:18:46 +01005001 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005002
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005003 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5004 psa_set_key_algorithm( &attributes, alg );
5005 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005006
Gilles Peskine049c7532019-05-15 20:22:09 +02005007 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005008 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005009
Ronald Cron5425a212020-08-04 14:58:35 +02005010 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005011 input1->x, input1->len,
5012 input2->x, input2->len,
5013 requested_capacity ) )
5014 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005015
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005016 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005017 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005018 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005019
5020 /* Expansion phase. */
5021 while( current_capacity > 0 )
5022 {
5023 size_t read_size = sizeof( output_buffer );
5024 if( read_size > current_capacity )
5025 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005026 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005027 output_buffer,
5028 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005029 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005030 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005031 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005032 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005033 }
5034
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005035 /* Check that the operation refuses to go over capacity. */
5036 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005037 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005038
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005039 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005040
5041exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005042 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005043 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005044 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005045}
5046/* END_CASE */
5047
Janos Follathe60c9052019-07-03 13:51:30 +01005048/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005049void derive_key_exercise( int alg_arg,
5050 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005051 data_t *input1,
5052 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005053 int derived_type_arg,
5054 int derived_bits_arg,
5055 int derived_usage_arg,
5056 int derived_alg_arg )
5057{
Ronald Cron5425a212020-08-04 14:58:35 +02005058 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5059 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005060 psa_algorithm_t alg = alg_arg;
5061 psa_key_type_t derived_type = derived_type_arg;
5062 size_t derived_bits = derived_bits_arg;
5063 psa_key_usage_t derived_usage = derived_usage_arg;
5064 psa_algorithm_t derived_alg = derived_alg_arg;
5065 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005066 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005068 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005069
Gilles Peskine8817f612018-12-18 00:18:46 +01005070 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005071
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005072 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5073 psa_set_key_algorithm( &attributes, alg );
5074 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005075 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005076 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005077
5078 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005079 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005080 input1->x, input1->len,
5081 input2->x, input2->len, capacity ) )
5082 goto exit;
5083
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005084 psa_set_key_usage_flags( &attributes, derived_usage );
5085 psa_set_key_algorithm( &attributes, derived_alg );
5086 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005087 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005088 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005089 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005090
5091 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005092 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005093 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5094 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005095
5096 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005097 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005098 goto exit;
5099
5100exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005101 /*
5102 * Key attributes may have been returned by psa_get_key_attributes()
5103 * thus reset them as required.
5104 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005105 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005106
5107 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005108 psa_destroy_key( base_key );
5109 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005110 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005111}
5112/* END_CASE */
5113
Janos Follath42fd8882019-07-03 14:17:09 +01005114/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005115void derive_key_export( int alg_arg,
5116 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005117 data_t *input1,
5118 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005119 int bytes1_arg,
5120 int bytes2_arg )
5121{
Ronald Cron5425a212020-08-04 14:58:35 +02005122 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5123 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005124 psa_algorithm_t alg = alg_arg;
5125 size_t bytes1 = bytes1_arg;
5126 size_t bytes2 = bytes2_arg;
5127 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005129 uint8_t *output_buffer = NULL;
5130 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005131 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5132 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005133 size_t length;
5134
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005135 ASSERT_ALLOC( output_buffer, capacity );
5136 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005137 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005138
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005139 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5140 psa_set_key_algorithm( &base_attributes, alg );
5141 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005142 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005143 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005144
5145 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005146 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005147 input1->x, input1->len,
5148 input2->x, input2->len, capacity ) )
5149 goto exit;
5150
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005152 output_buffer,
5153 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005154 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005155
5156 /* Derive the same output again, but this time store it in key objects. */
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 Peskineff5f0e72019-04-18 12:53:30 +02005162 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5163 psa_set_key_algorithm( &derived_attributes, 0 );
5164 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005165 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005166 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005167 &derived_key ) );
5168 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005169 export_buffer, bytes1,
5170 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005171 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005172 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005173 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005175 &derived_key ) );
5176 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005177 export_buffer + bytes1, bytes2,
5178 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005179 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005180
5181 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005182 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5183 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005184
5185exit:
5186 mbedtls_free( output_buffer );
5187 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005188 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005189 psa_destroy_key( base_key );
5190 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005191 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005192}
5193/* END_CASE */
5194
5195/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005196void derive_key( int alg_arg,
5197 data_t *key_data, data_t *input1, data_t *input2,
5198 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005199 int expected_status_arg,
5200 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005201{
Ronald Cron5425a212020-08-04 14:58:35 +02005202 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5203 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005204 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005205 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005206 size_t bits = bits_arg;
5207 psa_status_t expected_status = expected_status_arg;
5208 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5209 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5210 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5211
5212 PSA_ASSERT( psa_crypto_init( ) );
5213
5214 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5215 psa_set_key_algorithm( &base_attributes, alg );
5216 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5217 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005218 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005219
Ronald Cron5425a212020-08-04 14:58:35 +02005220 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005221 input1->x, input1->len,
5222 input2->x, input2->len, SIZE_MAX ) )
5223 goto exit;
5224
5225 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5226 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005227 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005228 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005229
5230 psa_status_t status =
5231 psa_key_derivation_output_key( &derived_attributes,
5232 &operation,
5233 &derived_key );
5234 if( is_large_output > 0 )
5235 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5236 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005237
5238exit:
5239 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005240 psa_destroy_key( base_key );
5241 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005242 PSA_DONE( );
5243}
5244/* END_CASE */
5245
5246/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005247void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005248 int our_key_type_arg, int our_key_alg_arg,
5249 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005250 int expected_status_arg )
5251{
Ronald Cron5425a212020-08-04 14:58:35 +02005252 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005253 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005254 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005255 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005258 psa_status_t expected_status = expected_status_arg;
5259 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005260
Gilles Peskine8817f612018-12-18 00:18:46 +01005261 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005262
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005263 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005264 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005265 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005266 PSA_ASSERT( psa_import_key( &attributes,
5267 our_key_data->x, our_key_data->len,
5268 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005269
Gilles Peskine77f40d82019-04-11 21:27:06 +02005270 /* The tests currently include inputs that should fail at either step.
5271 * Test cases that fail at the setup step should be changed to call
5272 * key_derivation_setup instead, and this function should be renamed
5273 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005275 if( status == PSA_SUCCESS )
5276 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005277 TEST_EQUAL( psa_key_derivation_key_agreement(
5278 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5279 our_key,
5280 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005281 expected_status );
5282 }
5283 else
5284 {
5285 TEST_ASSERT( status == expected_status );
5286 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005287
5288exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005290 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005291 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005292}
5293/* END_CASE */
5294
5295/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005296void raw_key_agreement( int alg_arg,
5297 int our_key_type_arg, data_t *our_key_data,
5298 data_t *peer_key_data,
5299 data_t *expected_output )
5300{
Ronald Cron5425a212020-08-04 14:58:35 +02005301 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005302 psa_algorithm_t alg = alg_arg;
5303 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005305 unsigned char *output = NULL;
5306 size_t output_length = ~0;
5307
5308 ASSERT_ALLOC( output, expected_output->len );
5309 PSA_ASSERT( psa_crypto_init( ) );
5310
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005311 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5312 psa_set_key_algorithm( &attributes, alg );
5313 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005314 PSA_ASSERT( psa_import_key( &attributes,
5315 our_key_data->x, our_key_data->len,
5316 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005317
Gilles Peskinebe697d82019-05-16 18:00:41 +02005318 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5319 peer_key_data->x, peer_key_data->len,
5320 output, expected_output->len,
5321 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005322 ASSERT_COMPARE( output, output_length,
5323 expected_output->x, expected_output->len );
5324
5325exit:
5326 mbedtls_free( output );
5327 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005328 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005329}
5330/* END_CASE */
5331
5332/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005333void key_agreement_capacity( int alg_arg,
5334 int our_key_type_arg, data_t *our_key_data,
5335 data_t *peer_key_data,
5336 int expected_capacity_arg )
5337{
Ronald Cron5425a212020-08-04 14:58:35 +02005338 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005339 psa_algorithm_t alg = alg_arg;
5340 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005343 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005344 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005345
Gilles Peskine8817f612018-12-18 00:18:46 +01005346 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005347
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5349 psa_set_key_algorithm( &attributes, alg );
5350 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005351 PSA_ASSERT( psa_import_key( &attributes,
5352 our_key_data->x, our_key_data->len,
5353 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005354
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005355 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005356 PSA_ASSERT( psa_key_derivation_key_agreement(
5357 &operation,
5358 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5359 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005360 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5361 {
5362 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005363 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005364 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005365 NULL, 0 ) );
5366 }
Gilles Peskine59685592018-09-18 12:11:34 +02005367
Gilles Peskinebf491972018-10-25 22:36:12 +02005368 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005369 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005370 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005371 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005372
Gilles Peskinebf491972018-10-25 22:36:12 +02005373 /* Test the actual capacity by reading the output. */
5374 while( actual_capacity > sizeof( output ) )
5375 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005376 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005377 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005378 actual_capacity -= sizeof( output );
5379 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005380 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005381 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005382 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005383 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005384
Gilles Peskine59685592018-09-18 12:11:34 +02005385exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005386 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005387 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005388 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005389}
5390/* END_CASE */
5391
5392/* BEGIN_CASE */
5393void key_agreement_output( int alg_arg,
5394 int our_key_type_arg, data_t *our_key_data,
5395 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005396 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005397{
Ronald Cron5425a212020-08-04 14:58:35 +02005398 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005399 psa_algorithm_t alg = alg_arg;
5400 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005401 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005403 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005404
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005405 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5406 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005407
Gilles Peskine8817f612018-12-18 00:18:46 +01005408 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005409
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005410 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5411 psa_set_key_algorithm( &attributes, alg );
5412 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005413 PSA_ASSERT( psa_import_key( &attributes,
5414 our_key_data->x, our_key_data->len,
5415 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005416
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005417 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005418 PSA_ASSERT( psa_key_derivation_key_agreement(
5419 &operation,
5420 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5421 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005422 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5423 {
5424 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005425 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005426 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005427 NULL, 0 ) );
5428 }
Gilles Peskine59685592018-09-18 12:11:34 +02005429
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005430 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005431 actual_output,
5432 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005433 ASSERT_COMPARE( actual_output, expected_output1->len,
5434 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005435 if( expected_output2->len != 0 )
5436 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005438 actual_output,
5439 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005440 ASSERT_COMPARE( actual_output, expected_output2->len,
5441 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005442 }
Gilles Peskine59685592018-09-18 12:11:34 +02005443
5444exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005445 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005446 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005447 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005448 mbedtls_free( actual_output );
5449}
5450/* END_CASE */
5451
5452/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005453void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005454{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005455 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005456 unsigned char *output = NULL;
5457 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005458 size_t i;
5459 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005460
Simon Butcher49f8e312020-03-03 15:51:50 +00005461 TEST_ASSERT( bytes_arg >= 0 );
5462
Gilles Peskine91892022021-02-08 19:50:26 +01005463 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005464 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005465
Gilles Peskine8817f612018-12-18 00:18:46 +01005466 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005467
Gilles Peskinea50d7392018-06-21 10:22:13 +02005468 /* Run several times, to ensure that every output byte will be
5469 * nonzero at least once with overwhelming probability
5470 * (2^(-8*number_of_runs)). */
5471 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005472 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005473 if( bytes != 0 )
5474 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005475 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005476
Gilles Peskinea50d7392018-06-21 10:22:13 +02005477 for( i = 0; i < bytes; i++ )
5478 {
5479 if( output[i] != 0 )
5480 ++changed[i];
5481 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005482 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005483
5484 /* Check that every byte was changed to nonzero at least once. This
5485 * validates that psa_generate_random is overwriting every byte of
5486 * the output buffer. */
5487 for( i = 0; i < bytes; i++ )
5488 {
5489 TEST_ASSERT( changed[i] != 0 );
5490 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005491
5492exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005493 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005494 mbedtls_free( output );
5495 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005496}
5497/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005498
5499/* BEGIN_CASE */
5500void generate_key( int type_arg,
5501 int bits_arg,
5502 int usage_arg,
5503 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005504 int expected_status_arg,
5505 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005506{
Ronald Cron5425a212020-08-04 14:58:35 +02005507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005508 psa_key_type_t type = type_arg;
5509 psa_key_usage_t usage = usage_arg;
5510 size_t bits = bits_arg;
5511 psa_algorithm_t alg = alg_arg;
5512 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005514 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005515
Gilles Peskine8817f612018-12-18 00:18:46 +01005516 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005517
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005518 psa_set_key_usage_flags( &attributes, usage );
5519 psa_set_key_algorithm( &attributes, alg );
5520 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005521 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005522
5523 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005524 psa_status_t status = psa_generate_key( &attributes, &key );
5525
5526 if( is_large_key > 0 )
5527 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5528 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005529 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005530 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005531
5532 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005533 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005534 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5535 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005536
Gilles Peskine818ca122018-06-20 18:16:48 +02005537 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005538 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005539 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005540
5541exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005542 /*
5543 * Key attributes may have been returned by psa_get_key_attributes()
5544 * thus reset them as required.
5545 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005546 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005547
Ronald Cron5425a212020-08-04 14:58:35 +02005548 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005549 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005550}
5551/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005552
Gilles Peskinee56e8782019-04-26 17:34:02 +02005553/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5554void generate_key_rsa( int bits_arg,
5555 data_t *e_arg,
5556 int expected_status_arg )
5557{
Ronald Cron5425a212020-08-04 14:58:35 +02005558 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005559 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005560 size_t bits = bits_arg;
5561 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5562 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5563 psa_status_t expected_status = expected_status_arg;
5564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5565 uint8_t *exported = NULL;
5566 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005567 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005568 size_t exported_length = SIZE_MAX;
5569 uint8_t *e_read_buffer = NULL;
5570 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005571 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005572 size_t e_read_length = SIZE_MAX;
5573
5574 if( e_arg->len == 0 ||
5575 ( e_arg->len == 3 &&
5576 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5577 {
5578 is_default_public_exponent = 1;
5579 e_read_size = 0;
5580 }
5581 ASSERT_ALLOC( e_read_buffer, e_read_size );
5582 ASSERT_ALLOC( exported, exported_size );
5583
5584 PSA_ASSERT( psa_crypto_init( ) );
5585
5586 psa_set_key_usage_flags( &attributes, usage );
5587 psa_set_key_algorithm( &attributes, alg );
5588 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5589 e_arg->x, e_arg->len ) );
5590 psa_set_key_bits( &attributes, bits );
5591
5592 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005593 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005594 if( expected_status != PSA_SUCCESS )
5595 goto exit;
5596
5597 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005598 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005599 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5600 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5601 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5602 e_read_buffer, e_read_size,
5603 &e_read_length ) );
5604 if( is_default_public_exponent )
5605 TEST_EQUAL( e_read_length, 0 );
5606 else
5607 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5608
5609 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005610 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005611 goto exit;
5612
5613 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005614 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005615 exported, exported_size,
5616 &exported_length ) );
5617 {
5618 uint8_t *p = exported;
5619 uint8_t *end = exported + exported_length;
5620 size_t len;
5621 /* RSAPublicKey ::= SEQUENCE {
5622 * modulus INTEGER, -- n
5623 * publicExponent INTEGER } -- e
5624 */
5625 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005626 MBEDTLS_ASN1_SEQUENCE |
5627 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005628 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5629 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5630 MBEDTLS_ASN1_INTEGER ) );
5631 if( len >= 1 && p[0] == 0 )
5632 {
5633 ++p;
5634 --len;
5635 }
5636 if( e_arg->len == 0 )
5637 {
5638 TEST_EQUAL( len, 3 );
5639 TEST_EQUAL( p[0], 1 );
5640 TEST_EQUAL( p[1], 0 );
5641 TEST_EQUAL( p[2], 1 );
5642 }
5643 else
5644 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5645 }
5646
5647exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005648 /*
5649 * Key attributes may have been returned by psa_get_key_attributes() or
5650 * set by psa_set_key_domain_parameters() thus reset them as required.
5651 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005652 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005653
Ronald Cron5425a212020-08-04 14:58:35 +02005654 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005655 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005656 mbedtls_free( e_read_buffer );
5657 mbedtls_free( exported );
5658}
5659/* END_CASE */
5660
Darryl Greend49a4992018-06-18 17:27:26 +01005661/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005662void persistent_key_load_key_from_storage( data_t *data,
5663 int type_arg, int bits_arg,
5664 int usage_flags_arg, int alg_arg,
5665 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005666{
Ronald Cron71016a92020-08-28 19:01:50 +02005667 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5670 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005671 psa_key_type_t type = type_arg;
5672 size_t bits = bits_arg;
5673 psa_key_usage_t usage_flags = usage_flags_arg;
5674 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005675 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005676 unsigned char *first_export = NULL;
5677 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005678 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005679 size_t first_exported_length;
5680 size_t second_exported_length;
5681
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005682 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5683 {
5684 ASSERT_ALLOC( first_export, export_size );
5685 ASSERT_ALLOC( second_export, export_size );
5686 }
Darryl Greend49a4992018-06-18 17:27:26 +01005687
Gilles Peskine8817f612018-12-18 00:18:46 +01005688 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005689
Gilles Peskinec87af662019-05-15 16:12:22 +02005690 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005691 psa_set_key_usage_flags( &attributes, usage_flags );
5692 psa_set_key_algorithm( &attributes, alg );
5693 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005694 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005695
Darryl Green0c6575a2018-11-07 16:05:30 +00005696 switch( generation_method )
5697 {
5698 case IMPORT_KEY:
5699 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005700 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005701 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005702 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005703
Darryl Green0c6575a2018-11-07 16:05:30 +00005704 case GENERATE_KEY:
5705 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005706 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005707 break;
5708
5709 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005710#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005711 {
5712 /* Create base key */
5713 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5714 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5715 psa_set_key_usage_flags( &base_attributes,
5716 PSA_KEY_USAGE_DERIVE );
5717 psa_set_key_algorithm( &base_attributes, derive_alg );
5718 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005719 PSA_ASSERT( psa_import_key( &base_attributes,
5720 data->x, data->len,
5721 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005722 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005724 PSA_ASSERT( psa_key_derivation_input_key(
5725 &operation,
5726 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005727 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005728 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005729 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005730 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5731 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005732 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005733 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005734 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005735 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005736 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005737#else
5738 TEST_ASSUME( ! "KDF not supported in this configuration" );
5739#endif
5740 break;
5741
5742 default:
5743 TEST_ASSERT( ! "generation_method not implemented in test" );
5744 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005745 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005746 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005747
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005748 /* Export the key if permitted by the key policy. */
5749 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5750 {
Ronald Cron5425a212020-08-04 14:58:35 +02005751 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005752 first_export, export_size,
5753 &first_exported_length ) );
5754 if( generation_method == IMPORT_KEY )
5755 ASSERT_COMPARE( data->x, data->len,
5756 first_export, first_exported_length );
5757 }
Darryl Greend49a4992018-06-18 17:27:26 +01005758
5759 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005760 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005761 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005762 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005763
Darryl Greend49a4992018-06-18 17:27:26 +01005764 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005765 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005766 TEST_ASSERT( mbedtls_svc_key_id_equal(
5767 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005768 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5769 PSA_KEY_LIFETIME_PERSISTENT );
5770 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5771 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5772 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5773 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005774
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005775 /* Export the key again if permitted by the key policy. */
5776 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005777 {
Ronald Cron5425a212020-08-04 14:58:35 +02005778 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005779 second_export, export_size,
5780 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005781 ASSERT_COMPARE( first_export, first_exported_length,
5782 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005783 }
5784
5785 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005786 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005787 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005788
5789exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005790 /*
5791 * Key attributes may have been returned by psa_get_key_attributes()
5792 * thus reset them as required.
5793 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005794 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005795
Darryl Greend49a4992018-06-18 17:27:26 +01005796 mbedtls_free( first_export );
5797 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005798 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005799 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005800 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005801 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005802}
5803/* END_CASE */