blob: 37cc5514a3417c02ea60ebf6b358b6fa4d12b943 [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)
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100106static int 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}
Gilles Peskine667c1112019-12-03 19:03:20 +0100111#endif
112
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200113/** Test if a buffer contains a constant byte value.
114 *
115 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200116 *
117 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200118 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200119 * \param size Size of the buffer in bytes.
120 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200121 * \return 1 if the buffer is all-bits-zero.
122 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200123 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200124static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200125{
126 size_t i;
127 for( i = 0; i < size; i++ )
128 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200129 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200130 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200131 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200132 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200133}
Gilles Peskine818ca122018-06-20 18:16:48 +0200134
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200135/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
136static int asn1_write_10x( unsigned char **p,
137 unsigned char *start,
138 size_t bits,
139 unsigned char x )
140{
141 int ret;
142 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200143 if( bits == 0 )
144 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
145 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200146 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300147 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200148 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
149 *p -= len;
150 ( *p )[len-1] = x;
151 if( bits % 8 == 0 )
152 ( *p )[1] |= 1;
153 else
154 ( *p )[0] |= 1 << ( bits % 8 );
155 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
156 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
157 MBEDTLS_ASN1_INTEGER ) );
158 return( len );
159}
160
161static int construct_fake_rsa_key( unsigned char *buffer,
162 size_t buffer_size,
163 unsigned char **p,
164 size_t bits,
165 int keypair )
166{
167 size_t half_bits = ( bits + 1 ) / 2;
168 int ret;
169 int len = 0;
170 /* Construct something that looks like a DER encoding of
171 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
172 * RSAPrivateKey ::= SEQUENCE {
173 * version Version,
174 * modulus INTEGER, -- n
175 * publicExponent INTEGER, -- e
176 * privateExponent INTEGER, -- d
177 * prime1 INTEGER, -- p
178 * prime2 INTEGER, -- q
179 * exponent1 INTEGER, -- d mod (p-1)
180 * exponent2 INTEGER, -- d mod (q-1)
181 * coefficient INTEGER, -- (inverse of q) mod p
182 * otherPrimeInfos OtherPrimeInfos OPTIONAL
183 * }
184 * Or, for a public key, the same structure with only
185 * version, modulus and publicExponent.
186 */
187 *p = buffer + buffer_size;
188 if( keypair )
189 {
190 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
191 asn1_write_10x( p, buffer, half_bits, 1 ) );
192 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
193 asn1_write_10x( p, buffer, half_bits, 1 ) );
194 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
195 asn1_write_10x( p, buffer, half_bits, 1 ) );
196 MBEDTLS_ASN1_CHK_ADD( len, /* q */
197 asn1_write_10x( p, buffer, half_bits, 1 ) );
198 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
199 asn1_write_10x( p, buffer, half_bits, 3 ) );
200 MBEDTLS_ASN1_CHK_ADD( len, /* d */
201 asn1_write_10x( p, buffer, bits, 1 ) );
202 }
203 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
204 asn1_write_10x( p, buffer, 17, 1 ) );
205 MBEDTLS_ASN1_CHK_ADD( len, /* n */
206 asn1_write_10x( p, buffer, bits, 1 ) );
207 if( keypair )
208 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
209 mbedtls_asn1_write_int( p, buffer, 0 ) );
210 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
211 {
212 const unsigned char tag =
213 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
214 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
215 }
216 return( len );
217}
218
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100219static int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100220{
221 int ok = 0;
222 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
223 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200224 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100225 psa_key_type_t type;
226 psa_key_type_t bits;
227
228 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
229 lifetime = psa_get_key_lifetime( &attributes );
230 id = psa_get_key_id( &attributes );
231 type = psa_get_key_type( &attributes );
232 bits = psa_get_key_bits( &attributes );
233
234 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200235 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200236 {
237 TEST_ASSERT(
238 ( PSA_KEY_ID_VOLATILE_MIN <=
239 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
240 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
241 PSA_KEY_ID_VOLATILE_MAX ) );
242 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100243 else
244 {
245 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200246 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
247 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100248 }
249#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
250 /* randomly-generated 64-bit constant, should never appear in test data */
251 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
252 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100253 if( lifetime_is_dynamic_secure_element( lifetime ) )
Gilles Peskine667c1112019-12-03 19:03:20 +0100254 {
255 /* Mbed Crypto currently always exposes the slot number to
256 * applications. This is not mandated by the PSA specification
257 * and may change in future versions. */
258 TEST_EQUAL( status, 0 );
259 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
260 }
261 else
262 {
263 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
264 }
265#endif
266
267 /* Type and size */
268 TEST_ASSERT( type != 0 );
269 TEST_ASSERT( bits != 0 );
270 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
271 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
272 TEST_ASSERT( bits % 8 == 0 );
273
274 /* MAX macros concerning specific key types */
275 if( PSA_KEY_TYPE_IS_ECC( type ) )
276 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
277 else if( PSA_KEY_TYPE_IS_RSA( type ) )
278 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100279 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100280
281 ok = 1;
282
283exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100284 /*
285 * Key attributes may have been returned by psa_get_key_attributes()
286 * thus reset them as required.
287 */
Gilles Peskine667c1112019-12-03 19:03:20 +0100288 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100289
Gilles Peskine667c1112019-12-03 19:03:20 +0100290 return( ok );
291}
292
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100293int exercise_mac_setup( psa_key_type_t key_type,
294 const unsigned char *key_bytes,
295 size_t key_length,
296 psa_algorithm_t alg,
297 psa_mac_operation_t *operation,
298 psa_status_t *status )
299{
Ronald Cron5425a212020-08-04 14:58:35 +0200300 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200301 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100302
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100303 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200304 psa_set_key_algorithm( &attributes, alg );
305 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200306 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100307
Ronald Cron5425a212020-08-04 14:58:35 +0200308 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100309 /* Whether setup succeeded or failed, abort must succeed. */
310 PSA_ASSERT( psa_mac_abort( operation ) );
311 /* If setup failed, reproduce the failure, so that the caller can
312 * test the resulting state of the operation object. */
313 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100314 {
Ronald Cron5425a212020-08-04 14:58:35 +0200315 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100316 }
317
Ronald Cron5425a212020-08-04 14:58:35 +0200318 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100319 return( 1 );
320
321exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200322 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100323 return( 0 );
324}
325
326int exercise_cipher_setup( psa_key_type_t key_type,
327 const unsigned char *key_bytes,
328 size_t key_length,
329 psa_algorithm_t alg,
330 psa_cipher_operation_t *operation,
331 psa_status_t *status )
332{
Ronald Cron5425a212020-08-04 14:58:35 +0200333 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100335
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200336 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
337 psa_set_key_algorithm( &attributes, alg );
338 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200339 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100340
Ronald Cron5425a212020-08-04 14:58:35 +0200341 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100342 /* Whether setup succeeded or failed, abort must succeed. */
343 PSA_ASSERT( psa_cipher_abort( operation ) );
344 /* If setup failed, reproduce the failure, so that the caller can
345 * test the resulting state of the operation object. */
346 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100347 {
Ronald Cron5425a212020-08-04 14:58:35 +0200348 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100349 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100350 }
351
Ronald Cron5425a212020-08-04 14:58:35 +0200352 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100353 return( 1 );
354
355exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200356 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100357 return( 0 );
358}
359
Ronald Cron5425a212020-08-04 14:58:35 +0200360static int exercise_mac_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200361 psa_key_usage_t usage,
362 psa_algorithm_t alg )
363{
Jaeden Amero769ce272019-01-04 11:48:03 +0000364 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200365 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200366 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200367 size_t mac_length = sizeof( mac );
368
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100369 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200370 {
Ronald Cron5425a212020-08-04 14:58:35 +0200371 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100372 PSA_ASSERT( psa_mac_update( &operation,
373 input, sizeof( input ) ) );
374 PSA_ASSERT( psa_mac_sign_finish( &operation,
375 mac, sizeof( mac ),
376 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200377 }
378
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100379 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200380 {
381 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100382 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200383 PSA_SUCCESS :
384 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200385 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100386 PSA_ASSERT( psa_mac_update( &operation,
387 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100388 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
389 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200390 }
391
392 return( 1 );
393
394exit:
395 psa_mac_abort( &operation );
396 return( 0 );
397}
398
Ronald Cron5425a212020-08-04 14:58:35 +0200399static int exercise_cipher_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200400 psa_key_usage_t usage,
401 psa_algorithm_t alg )
402{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000403 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200404 unsigned char iv[16] = {0};
405 size_t iv_length = sizeof( iv );
406 const unsigned char plaintext[16] = "Hello, world...";
407 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
408 size_t ciphertext_length = sizeof( ciphertext );
409 unsigned char decrypted[sizeof( ciphertext )];
410 size_t part_length;
411
412 if( usage & PSA_KEY_USAGE_ENCRYPT )
413 {
Ronald Cron5425a212020-08-04 14:58:35 +0200414 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100415 PSA_ASSERT( psa_cipher_generate_iv( &operation,
416 iv, sizeof( iv ),
417 &iv_length ) );
418 PSA_ASSERT( psa_cipher_update( &operation,
419 plaintext, sizeof( plaintext ),
420 ciphertext, sizeof( ciphertext ),
421 &ciphertext_length ) );
422 PSA_ASSERT( psa_cipher_finish( &operation,
423 ciphertext + ciphertext_length,
424 sizeof( ciphertext ) - ciphertext_length,
425 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200426 ciphertext_length += part_length;
427 }
428
429 if( usage & PSA_KEY_USAGE_DECRYPT )
430 {
431 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200432 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200433 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
434 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200436 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200437 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
438 * have this macro yet. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100439 iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200440 psa_get_key_type( &attributes ) );
441 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100442 psa_reset_key_attributes( &attributes );
Gilles Peskine818ca122018-06-20 18:16:48 +0200443 }
Ronald Cron5425a212020-08-04 14:58:35 +0200444 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100445 PSA_ASSERT( psa_cipher_set_iv( &operation,
446 iv, iv_length ) );
447 PSA_ASSERT( psa_cipher_update( &operation,
448 ciphertext, ciphertext_length,
449 decrypted, sizeof( decrypted ),
450 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200451 status = psa_cipher_finish( &operation,
452 decrypted + part_length,
453 sizeof( decrypted ) - part_length,
454 &part_length );
455 /* For a stream cipher, all inputs are valid. For a block cipher,
456 * if the input is some aribtrary data rather than an actual
457 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200458 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200459 TEST_ASSERT( status == PSA_SUCCESS ||
460 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200461 else
462 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200463 }
464
465 return( 1 );
466
467exit:
468 psa_cipher_abort( &operation );
469 return( 0 );
470}
471
Ronald Cron5425a212020-08-04 14:58:35 +0200472static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200473 psa_key_usage_t usage,
474 psa_algorithm_t alg )
475{
476 unsigned char nonce[16] = {0};
477 size_t nonce_length = sizeof( nonce );
478 unsigned char plaintext[16] = "Hello, world...";
479 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
480 size_t ciphertext_length = sizeof( ciphertext );
481 size_t plaintext_length = sizeof( ciphertext );
482
Steven Cooreman2f099132021-01-11 20:33:45 +0100483 /* Default IV length for AES-GCM is 12 bytes */
Bence Szépkútia63b20d2020-12-16 11:36:46 +0100484 if( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ==
485 PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ) )
Steven Cooreman50f1f5e2021-01-25 10:26:49 +0100486 {
Steven Cooreman2f099132021-01-11 20:33:45 +0100487 nonce_length = 12;
Steven Cooreman50f1f5e2021-01-25 10:26:49 +0100488 }
Steven Cooreman2f099132021-01-11 20:33:45 +0100489
Gilles Peskine818ca122018-06-20 18:16:48 +0200490 if( usage & PSA_KEY_USAGE_ENCRYPT )
491 {
Ronald Cron5425a212020-08-04 14:58:35 +0200492 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100493 nonce, nonce_length,
494 NULL, 0,
495 plaintext, sizeof( plaintext ),
496 ciphertext, sizeof( ciphertext ),
497 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200498 }
499
500 if( usage & PSA_KEY_USAGE_DECRYPT )
501 {
502 psa_status_t verify_status =
503 ( usage & PSA_KEY_USAGE_ENCRYPT ?
504 PSA_SUCCESS :
505 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200506 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100507 nonce, nonce_length,
508 NULL, 0,
509 ciphertext, ciphertext_length,
510 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100511 &plaintext_length ),
512 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200513 }
514
515 return( 1 );
516
517exit:
518 return( 0 );
519}
520
Ronald Cron5425a212020-08-04 14:58:35 +0200521static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200522 psa_key_usage_t usage,
523 psa_algorithm_t alg )
524{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200525 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
526 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100527 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200528 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100529 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
530
531 /* If the policy allows signing with any hash, just pick one. */
532 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
533 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100534#if defined(KNOWN_SUPPORTED_HASH_ALG)
535 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
536 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100537#else
Chris Jones9634bb12021-01-20 15:56:42 +0000538 mbedtls_test_fail( "No hash algorithm for hash-and-sign testing",
539 __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100540 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100541#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100542 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200543
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100544 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200545 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200546 /* Some algorithms require the payload to have the size of
547 * the hash encoded in the algorithm. Use this input size
548 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200549 if( hash_alg != 0 )
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100550 payload_length = PSA_HASH_LENGTH( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200551 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100552 payload, payload_length,
553 signature, sizeof( signature ),
554 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200555 }
556
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100557 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200558 {
559 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100560 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200561 PSA_SUCCESS :
562 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200563 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100564 payload, payload_length,
565 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100566 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200567 }
568
569 return( 1 );
570
571exit:
572 return( 0 );
573}
574
Ronald Cron5425a212020-08-04 14:58:35 +0200575static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200576 psa_key_usage_t usage,
577 psa_algorithm_t alg )
578{
579 unsigned char plaintext[256] = "Hello, world...";
580 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
581 size_t ciphertext_length = sizeof( ciphertext );
582 size_t plaintext_length = 16;
583
584 if( usage & PSA_KEY_USAGE_ENCRYPT )
585 {
Ronald Cron5425a212020-08-04 14:58:35 +0200586 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100587 plaintext, plaintext_length,
588 NULL, 0,
589 ciphertext, sizeof( ciphertext ),
590 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200591 }
592
593 if( usage & PSA_KEY_USAGE_DECRYPT )
594 {
595 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200596 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200597 ciphertext, ciphertext_length,
598 NULL, 0,
599 plaintext, sizeof( plaintext ),
600 &plaintext_length );
601 TEST_ASSERT( status == PSA_SUCCESS ||
602 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
603 ( status == PSA_ERROR_INVALID_ARGUMENT ||
604 status == PSA_ERROR_INVALID_PADDING ) ) );
605 }
606
607 return( 1 );
608
609exit:
610 return( 0 );
611}
Gilles Peskine02b75072018-07-01 22:31:34 +0200612
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100613int mbedtls_test_psa_setup_key_derivation_wrap(
614 psa_key_derivation_operation_t* operation,
615 mbedtls_svc_key_id_t key,
616 psa_algorithm_t alg,
617 unsigned char* input1, size_t input1_length,
618 unsigned char* input2, size_t input2_length,
619 size_t capacity )
Janos Follathf2815ea2019-07-03 12:41:36 +0100620{
621 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
622 if( PSA_ALG_IS_HKDF( alg ) )
623 {
624 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
625 PSA_KEY_DERIVATION_INPUT_SALT,
626 input1, input1_length ) );
627 PSA_ASSERT( psa_key_derivation_input_key( operation,
628 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200629 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100630 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
631 PSA_KEY_DERIVATION_INPUT_INFO,
632 input2,
633 input2_length ) );
634 }
635 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
636 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
637 {
638 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
639 PSA_KEY_DERIVATION_INPUT_SEED,
640 input1, input1_length ) );
641 PSA_ASSERT( psa_key_derivation_input_key( operation,
642 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200643 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100644 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
645 PSA_KEY_DERIVATION_INPUT_LABEL,
646 input2, input2_length ) );
647 }
648 else
649 {
650 TEST_ASSERT( ! "Key derivation algorithm not supported" );
651 }
652
Gilles Peskinec744d992019-07-30 17:26:54 +0200653 if( capacity != SIZE_MAX )
654 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100655
656 return( 1 );
657
658exit:
659 return( 0 );
660}
661
662
Ronald Cron5425a212020-08-04 14:58:35 +0200663static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200664 psa_key_usage_t usage,
665 psa_algorithm_t alg )
666{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200667 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100668 unsigned char input1[] = "Input 1";
669 size_t input1_length = sizeof( input1 );
670 unsigned char input2[] = "Input 2";
671 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200672 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100673 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200674
675 if( usage & PSA_KEY_USAGE_DERIVE )
676 {
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100677 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
678 input1, input1_length,
679 input2, input2_length,
680 capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +0100681 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200682
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200683 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200684 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100685 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200686 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200687 }
688
689 return( 1 );
690
691exit:
692 return( 0 );
693}
694
Gilles Peskinec7998b72018-11-07 18:45:02 +0100695/* We need two keys to exercise key agreement. Exercise the
696 * private key against its own public key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100697psa_status_t mbedtls_test_psa_key_agreement_with_self(
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200698 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200699 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100700{
701 psa_key_type_t private_key_type;
702 psa_key_type_t public_key_type;
703 size_t key_bits;
704 uint8_t *public_key = NULL;
705 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200706 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200707 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
708 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200709 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200710 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100711
Ronald Cron5425a212020-08-04 14:58:35 +0200712 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200713 private_key_type = psa_get_key_type( &attributes );
714 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200715 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100716 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100717 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200718 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100719 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100720
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200721 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200722 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200723 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100724exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100725 /*
726 * Key attributes may have been returned by psa_get_key_attributes()
727 * thus reset them as required.
728 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200729 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100730
731 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100732 return( status );
733}
734
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200735/* We need two keys to exercise key agreement. Exercise the
736 * private key against its own public key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100737psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
738 psa_algorithm_t alg,
739 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200740{
741 psa_key_type_t private_key_type;
742 psa_key_type_t public_key_type;
743 size_t key_bits;
744 uint8_t *public_key = NULL;
745 size_t public_key_length;
746 uint8_t output[1024];
747 size_t output_length;
748 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200749 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
750 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200751 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200752 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200753
Ronald Cron5425a212020-08-04 14:58:35 +0200754 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200755 private_key_type = psa_get_key_type( &attributes );
756 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200757 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100758 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200759 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200760 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200761 public_key, public_key_length,
762 &public_key_length ) );
763
Ronald Cron5425a212020-08-04 14:58:35 +0200764 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200765 public_key, public_key_length,
766 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200767exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100768 /*
769 * Key attributes may have been returned by psa_get_key_attributes()
770 * thus reset them as required.
771 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200772 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100773
774 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200775 return( status );
776}
777
Ronald Cron5425a212020-08-04 14:58:35 +0200778static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200779 psa_key_usage_t usage,
780 psa_algorithm_t alg )
781{
782 int ok = 0;
783
784 if( usage & PSA_KEY_USAGE_DERIVE )
785 {
786 /* We need two keys to exercise key agreement. Exercise the
787 * private key against its own public key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100788 PSA_ASSERT( mbedtls_test_psa_raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200789 }
790 ok = 1;
791
792exit:
793 return( ok );
794}
795
Ronald Cron5425a212020-08-04 14:58:35 +0200796static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200797 psa_key_usage_t usage,
798 psa_algorithm_t alg )
799{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200800 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200801 unsigned char output[1];
802 int ok = 0;
803
804 if( usage & PSA_KEY_USAGE_DERIVE )
805 {
806 /* We need two keys to exercise key agreement. Exercise the
807 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200808 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100809 PSA_ASSERT( mbedtls_test_psa_key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200810 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200811 output,
812 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200813 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200814 }
815 ok = 1;
816
817exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200818 return( ok );
819}
820
Jaeden Amerof7dca862019-06-27 17:31:33 +0100821int asn1_skip_integer( unsigned char **p, const unsigned char *end,
822 size_t min_bits, size_t max_bits,
823 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200824{
825 size_t len;
826 size_t actual_bits;
827 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100828 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100829 MBEDTLS_ASN1_INTEGER ),
830 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200831
832 /* Check if the retrieved length doesn't extend the actual buffer's size.
833 * It is assumed here, that end >= p, which validates casting to size_t. */
834 TEST_ASSERT( len <= (size_t)( end - *p) );
835
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200836 /* Tolerate a slight departure from DER encoding:
837 * - 0 may be represented by an empty string or a 1-byte string.
838 * - The sign bit may be used as a value bit. */
839 if( ( len == 1 && ( *p )[0] == 0 ) ||
840 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
841 {
842 ++( *p );
843 --len;
844 }
845 if( min_bits == 0 && len == 0 )
846 return( 1 );
847 msb = ( *p )[0];
848 TEST_ASSERT( msb != 0 );
849 actual_bits = 8 * ( len - 1 );
850 while( msb != 0 )
851 {
852 msb >>= 1;
853 ++actual_bits;
854 }
855 TEST_ASSERT( actual_bits >= min_bits );
856 TEST_ASSERT( actual_bits <= max_bits );
857 if( must_be_odd )
858 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
859 *p += len;
860 return( 1 );
861exit:
862 return( 0 );
863}
864
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100865int mbedtls_test_psa_exported_key_sanity_check(
866 psa_key_type_t type, size_t bits,
867 uint8_t *exported, size_t exported_length )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200868{
869 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100870 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200871 else
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100872 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200873
874#if defined(MBEDTLS_DES_C)
875 if( type == PSA_KEY_TYPE_DES )
876 {
877 /* Check the parity bits. */
878 unsigned i;
879 for( i = 0; i < bits / 8; i++ )
880 {
881 unsigned bit_count = 0;
882 unsigned m;
883 for( m = 1; m <= 0x100; m <<= 1 )
884 {
885 if( exported[i] & m )
886 ++bit_count;
887 }
888 TEST_ASSERT( bit_count % 2 != 0 );
889 }
890 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200891 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200892#endif
893
894#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200895 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200896 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200897 uint8_t *p = exported;
898 uint8_t *end = exported + exported_length;
899 size_t len;
900 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200901 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200902 * modulus INTEGER, -- n
903 * publicExponent INTEGER, -- e
904 * privateExponent INTEGER, -- d
905 * prime1 INTEGER, -- p
906 * prime2 INTEGER, -- q
907 * exponent1 INTEGER, -- d mod (p-1)
908 * exponent2 INTEGER, -- d mod (q-1)
909 * coefficient INTEGER, -- (inverse of q) mod p
910 * }
911 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100912 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
913 MBEDTLS_ASN1_SEQUENCE |
914 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
915 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200916 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
917 goto exit;
918 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
919 goto exit;
920 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
921 goto exit;
922 /* Require d to be at least half the size of n. */
923 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
924 goto exit;
925 /* Require p and q to be at most half the size of n, rounded up. */
926 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
927 goto exit;
928 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
929 goto exit;
930 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
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;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100936 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100937 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200938 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200939#endif /* MBEDTLS_RSA_C */
940
941#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200942 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200943 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100944 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100946 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200947 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200948#endif /* MBEDTLS_ECP_C */
949
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200950 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
951 {
952 uint8_t *p = exported;
953 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200954#if defined(MBEDTLS_RSA_C)
955 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
956 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100957 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200958 /* RSAPublicKey ::= SEQUENCE {
959 * modulus INTEGER, -- n
960 * publicExponent INTEGER } -- e
961 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100962 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
963 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100964 MBEDTLS_ASN1_CONSTRUCTED ),
965 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100966 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200967 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
968 goto exit;
969 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
970 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100971 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200972 }
973 else
974#endif /* MBEDTLS_RSA_C */
975#if defined(MBEDTLS_ECP_C)
976 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
977 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200978 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
979 {
980 /* The representation of an ECC Montgomery public key is
981 * the raw compressed point */
982 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
983 }
984 else
985 {
986 /* The representation of an ECC Weierstrass public key is:
987 * - The byte 0x04;
988 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
989 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
990 * - where m is the bit size associated with the curve.
991 */
992 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
993 TEST_EQUAL( p[0], 4 );
994 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200995 }
996 else
997#endif /* MBEDTLS_ECP_C */
998 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100999 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001000 mbedtls_snprintf( message, sizeof( message ),
1001 "No sanity check for public key type=0x%08lx",
1002 (unsigned long) type );
Chris Jones9634bb12021-01-20 15:56:42 +00001003 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +02001004 (void) p;
1005 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001006 return( 0 );
1007 }
1008 }
1009 else
1010
1011 {
1012 /* No sanity checks for other types */
1013 }
1014
1015 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001016
1017exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001018 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001019}
1020
Ronald Cron5425a212020-08-04 14:58:35 +02001021static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001022 psa_key_usage_t usage )
1023{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001025 uint8_t *exported = NULL;
1026 size_t exported_size = 0;
1027 size_t exported_length = 0;
1028 int ok = 0;
1029
Ronald Cron5425a212020-08-04 14:58:35 +02001030 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001031
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001032 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1033 psa_get_key_type( &attributes ),
1034 psa_get_key_bits( &attributes ) );
1035 ASSERT_ALLOC( exported, exported_size );
1036
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001037 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001038 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001039 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001040 TEST_EQUAL( psa_export_key( key, exported,
1041 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001042 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001043 ok = 1;
1044 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001045 }
1046
Ronald Cron5425a212020-08-04 14:58:35 +02001047 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001048 exported, exported_size,
1049 &exported_length ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001050 ok = mbedtls_test_psa_exported_key_sanity_check(
1051 psa_get_key_type( &attributes ), psa_get_key_bits( &attributes ),
1052 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001053
1054exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001055 /*
1056 * Key attributes may have been returned by psa_get_key_attributes()
1057 * thus reset them as required.
1058 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001059 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001060
1061 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001062 return( ok );
1063}
1064
Ronald Cron5425a212020-08-04 14:58:35 +02001065static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001066{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001068 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001069 uint8_t *exported = NULL;
1070 size_t exported_size = 0;
1071 size_t exported_length = 0;
1072 int ok = 0;
1073
Ronald Cron5425a212020-08-04 14:58:35 +02001074 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001075 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001076 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001077 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1078 psa_get_key_type( &attributes ),
1079 psa_get_key_bits( &attributes ) );
1080 ASSERT_ALLOC( exported, exported_size );
1081
1082 TEST_EQUAL( psa_export_public_key( key, exported,
1083 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001084 PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001085 ok = 1;
1086 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001087 }
1088
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001089 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001090 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001091 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1092 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001093 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001094
Ronald Cron5425a212020-08-04 14:58:35 +02001095 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001096 exported, exported_size,
1097 &exported_length ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001098 ok = mbedtls_test_psa_exported_key_sanity_check(
1099 public_type, psa_get_key_bits( &attributes ),
1100 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001101
1102exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001103 /*
1104 * Key attributes may have been returned by psa_get_key_attributes()
1105 * thus reset them as required.
1106 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001107 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001108
1109 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001110 return( ok );
1111}
1112
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001113/** Do smoke tests on a key.
1114 *
1115 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1116 * sign/verify, or derivation) that is permitted according to \p usage.
1117 * \p usage and \p alg should correspond to the expected policy on the
1118 * key.
1119 *
1120 * Export the key if permitted by \p usage, and check that the output
1121 * looks sensible. If \p usage forbids export, check that
1122 * \p psa_export_key correctly rejects the attempt. If the key is
1123 * asymmetric, also check \p psa_export_public_key.
1124 *
1125 * If the key fails the tests, this function calls the test framework's
Chris Jones9634bb12021-01-20 15:56:42 +00001126 * `mbedtls_test_fail` function and returns false. Otherwise this function
1127 * returns true. Therefore it should be used as follows:
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001128 * ```
1129 * if( ! exercise_key( ... ) ) goto exit;
1130 * ```
1131 *
Ronald Cron5425a212020-08-04 14:58:35 +02001132 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001133 * \p alg.
1134 * \param usage The usage flags to assume.
1135 * \param alg The algorithm to exercise.
1136 *
1137 * \retval 0 The key failed the smoke tests.
1138 * \retval 1 The key passed the smoke tests.
1139 */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001140int mbedtls_test_psa_exercise_key( mbedtls_svc_key_id_t key,
1141 psa_key_usage_t usage,
1142 psa_algorithm_t alg )
Gilles Peskine02b75072018-07-01 22:31:34 +02001143{
1144 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001145
Ronald Cron5425a212020-08-04 14:58:35 +02001146 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001147 return( 0 );
1148
Gilles Peskine02b75072018-07-01 22:31:34 +02001149 if( alg == 0 )
1150 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1151 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001152 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001153 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001154 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001155 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001156 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001157 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001158 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001159 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001160 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001161 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001162 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001163 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001164 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001165 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001166 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001167 else
1168 {
1169 char message[40];
1170 mbedtls_snprintf( message, sizeof( message ),
1171 "No code to exercise alg=0x%08lx",
1172 (unsigned long) alg );
Chris Jones9634bb12021-01-20 15:56:42 +00001173 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskine02b75072018-07-01 22:31:34 +02001174 ok = 0;
1175 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001176
Ronald Cron5425a212020-08-04 14:58:35 +02001177 ok = ok && exercise_export_key( key, usage );
1178 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001179
Gilles Peskine02b75072018-07-01 22:31:34 +02001180 return( ok );
1181}
1182
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001183psa_key_usage_t mbedtls_test_psa_usage_to_exercise( psa_key_type_t type,
1184 psa_algorithm_t alg )
Gilles Peskine10df3412018-10-25 22:35:43 +02001185{
1186 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1187 {
1188 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001189 PSA_KEY_USAGE_VERIFY_HASH :
1190 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001191 }
1192 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1193 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1194 {
1195 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1196 PSA_KEY_USAGE_ENCRYPT :
1197 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1198 }
1199 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1200 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1201 {
1202 return( PSA_KEY_USAGE_DERIVE );
1203 }
1204 else
1205 {
1206 return( 0 );
1207 }
1208
1209}
Darryl Green0c6575a2018-11-07 16:05:30 +00001210
Ronald Cron5425a212020-08-04 14:58:35 +02001211static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001212{
1213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001214 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001215 uint8_t buffer[1];
1216 size_t length;
1217 int ok = 0;
1218
Ronald Cronecfb2372020-07-23 17:13:42 +02001219 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1221 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1222 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001223 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001224 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001225 TEST_EQUAL(
1226 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1227 TEST_EQUAL(
1228 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001229 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001230 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1231 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1232 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1233 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1234
Ronald Cron5425a212020-08-04 14:58:35 +02001235 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001236 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001237 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001238 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001239 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001240
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001241 ok = 1;
1242
1243exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001244 /*
1245 * Key attributes may have been returned by psa_get_key_attributes()
1246 * thus reset them as required.
1247 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001248 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001249
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001250 return( ok );
1251}
1252
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001253/* Assert that a key isn't reported as having a slot number. */
1254#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1255#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1256 do \
1257 { \
1258 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1259 TEST_EQUAL( psa_get_key_slot_number( \
1260 attributes, \
1261 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1262 PSA_ERROR_INVALID_ARGUMENT ); \
1263 } \
1264 while( 0 )
1265#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1266#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1267 ( (void) 0 )
1268#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1269
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001270/* An overapproximation of the amount of storage needed for a key of the
1271 * given type and with the given content. The API doesn't make it easy
1272 * to find a good value for the size. The current implementation doesn't
1273 * care about the value anyway. */
1274#define KEY_BITS_FROM_DATA( type, data ) \
1275 ( data )->len
1276
Darryl Green0c6575a2018-11-07 16:05:30 +00001277typedef enum {
1278 IMPORT_KEY = 0,
1279 GENERATE_KEY = 1,
1280 DERIVE_KEY = 2
1281} generate_method;
1282
Gilles Peskinee59236f2018-01-27 23:32:46 +01001283/* END_HEADER */
1284
1285/* BEGIN_DEPENDENCIES
1286 * depends_on:MBEDTLS_PSA_CRYPTO_C
1287 * END_DEPENDENCIES
1288 */
1289
1290/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001291void static_checks( )
1292{
1293 size_t max_truncated_mac_size =
1294 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1295
1296 /* Check that the length for a truncated MAC always fits in the algorithm
1297 * encoding. The shifted mask is the maximum truncated value. The
1298 * untruncated algorithm may be one byte larger. */
1299 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001300
1301#if defined(MBEDTLS_TEST_DEPRECATED)
1302 /* Check deprecated constants. */
1303 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1304 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1305 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1306 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1307 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1308 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1309 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1310 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001311
Paul Elliott8ff510a2020-06-02 17:19:28 +01001312 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1314 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1326 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1328 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1329 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1330 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1331 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1332 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1333 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1334 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1335 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1336 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1337 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1338 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1339 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1340 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1341 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1342
1343 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1344 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1345 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1346 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1347 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1348 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1349 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1350 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001351
Paul Elliott75e27032020-06-03 15:17:39 +01001352 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1353 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1354 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1355 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1356 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1357
1358 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1359 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001360#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001361}
1362/* END_CASE */
1363
1364/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001365void import_with_policy( int type_arg,
1366 int usage_arg, int alg_arg,
1367 int expected_status_arg )
1368{
1369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1370 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001372 psa_key_type_t type = type_arg;
1373 psa_key_usage_t usage = usage_arg;
1374 psa_algorithm_t alg = alg_arg;
1375 psa_status_t expected_status = expected_status_arg;
1376 const uint8_t key_material[16] = {0};
1377 psa_status_t status;
1378
1379 PSA_ASSERT( psa_crypto_init( ) );
1380
1381 psa_set_key_type( &attributes, type );
1382 psa_set_key_usage_flags( &attributes, usage );
1383 psa_set_key_algorithm( &attributes, alg );
1384
1385 status = psa_import_key( &attributes,
1386 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001387 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001388 TEST_EQUAL( status, expected_status );
1389 if( status != PSA_SUCCESS )
1390 goto exit;
1391
Ronald Cron5425a212020-08-04 14:58:35 +02001392 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001393 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1394 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1395 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001396 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001397
Ronald Cron5425a212020-08-04 14:58:35 +02001398 PSA_ASSERT( psa_destroy_key( key ) );
1399 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001400
1401exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001402 /*
1403 * Key attributes may have been returned by psa_get_key_attributes()
1404 * thus reset them as required.
1405 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001406 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001407
1408 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001409 PSA_DONE( );
1410}
1411/* END_CASE */
1412
1413/* BEGIN_CASE */
1414void import_with_data( data_t *data, int type_arg,
1415 int attr_bits_arg,
1416 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001417{
1418 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1419 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001420 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001421 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001422 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001423 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001424 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001425
Gilles Peskine8817f612018-12-18 00:18:46 +01001426 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001427
Gilles Peskine4747d192019-04-17 15:05:45 +02001428 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001429 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001430
Ronald Cron5425a212020-08-04 14:58:35 +02001431 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001432 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001433 if( status != PSA_SUCCESS )
1434 goto exit;
1435
Ronald Cron5425a212020-08-04 14:58:35 +02001436 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001437 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001438 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001439 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001440 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001441
Ronald Cron5425a212020-08-04 14:58:35 +02001442 PSA_ASSERT( psa_destroy_key( key ) );
1443 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001444
1445exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001446 /*
1447 * Key attributes may have been returned by psa_get_key_attributes()
1448 * thus reset them as required.
1449 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001450 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001451
1452 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001453 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001454}
1455/* END_CASE */
1456
1457/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001458void import_large_key( int type_arg, int byte_size_arg,
1459 int expected_status_arg )
1460{
1461 psa_key_type_t type = type_arg;
1462 size_t byte_size = byte_size_arg;
1463 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1464 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001465 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001466 psa_status_t status;
1467 uint8_t *buffer = NULL;
1468 size_t buffer_size = byte_size + 1;
1469 size_t n;
1470
Steven Cooreman69967ce2021-01-18 18:01:08 +01001471 /* Skip the test case if the target running the test cannot
1472 * accomodate large keys due to heap size constraints */
1473 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001474 memset( buffer, 'K', byte_size );
1475
1476 PSA_ASSERT( psa_crypto_init( ) );
1477
1478 /* Try importing the key */
1479 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1480 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001481 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001482 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001483 TEST_EQUAL( status, expected_status );
1484
1485 if( status == PSA_SUCCESS )
1486 {
Ronald Cron5425a212020-08-04 14:58:35 +02001487 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001488 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1489 TEST_EQUAL( psa_get_key_bits( &attributes ),
1490 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001491 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001492 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001493 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001494 for( n = 0; n < byte_size; n++ )
1495 TEST_EQUAL( buffer[n], 'K' );
1496 for( n = byte_size; n < buffer_size; n++ )
1497 TEST_EQUAL( buffer[n], 0 );
1498 }
1499
1500exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001501 /*
1502 * Key attributes may have been returned by psa_get_key_attributes()
1503 * thus reset them as required.
1504 */
1505 psa_reset_key_attributes( &attributes );
1506
Ronald Cron5425a212020-08-04 14:58:35 +02001507 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001508 PSA_DONE( );
1509 mbedtls_free( buffer );
1510}
1511/* END_CASE */
1512
1513/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001514void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1515{
Ronald Cron5425a212020-08-04 14:58:35 +02001516 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001517 size_t bits = bits_arg;
1518 psa_status_t expected_status = expected_status_arg;
1519 psa_status_t status;
1520 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001521 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001522 size_t buffer_size = /* Slight overapproximations */
1523 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001524 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001525 unsigned char *p;
1526 int ret;
1527 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001529
Gilles Peskine8817f612018-12-18 00:18:46 +01001530 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001531 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001532
1533 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1534 bits, keypair ) ) >= 0 );
1535 length = ret;
1536
1537 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001538 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001539 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001540 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001541
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001542 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001543 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001544
1545exit:
1546 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001547 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001548}
1549/* END_CASE */
1550
1551/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001552void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001553 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001554 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001555 int expected_bits,
1556 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001557 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001558 int canonical_input )
1559{
Ronald Cron5425a212020-08-04 14:58:35 +02001560 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001561 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001562 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001563 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001565 unsigned char *exported = NULL;
1566 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001568 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001569 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001571 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001572
Moran Pekercb088e72018-07-17 17:36:59 +03001573 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001574 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001575 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001576 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001577 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001578
Gilles Peskine4747d192019-04-17 15:05:45 +02001579 psa_set_key_usage_flags( &attributes, usage_arg );
1580 psa_set_key_algorithm( &attributes, alg );
1581 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001582
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001583 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001584 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001585
1586 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001587 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001588 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1589 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001590 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591
1592 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001593 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001594 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001595
1596 /* The exported length must be set by psa_export_key() to a value between 0
1597 * and export_size. On errors, the exported length must be 0. */
1598 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1599 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1600 TEST_ASSERT( exported_length <= export_size );
1601
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001602 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001603 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001604 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001605 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001606 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001607 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001608 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001609
Gilles Peskineea38a922021-02-13 00:05:16 +01001610 /* Run sanity checks on the exported key. For non-canonical inputs,
1611 * this validates the canonical representations. For canonical inputs,
1612 * this doesn't directly validate the implementation, but it still helps
1613 * by cross-validating the test data with the sanity check code. */
1614 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001615 goto exit;
1616
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001617 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001618 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001619 else
1620 {
Ronald Cron5425a212020-08-04 14:58:35 +02001621 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001622 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001623 &key2 ) );
1624 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001625 reexported,
1626 export_size,
1627 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001628 ASSERT_COMPARE( exported, exported_length,
1629 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001630 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001631 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001632 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001633
1634destroy:
1635 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001636 PSA_ASSERT( psa_destroy_key( key ) );
1637 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001638
1639exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001640 /*
1641 * Key attributes may have been returned by psa_get_key_attributes()
1642 * thus reset them as required.
1643 */
1644 psa_reset_key_attributes( &got_attributes );
1645
itayzafrir3e02b3b2018-06-12 17:06:52 +03001646 mbedtls_free( exported );
1647 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001648 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001649}
1650/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001651
Moran Pekerf709f4a2018-06-06 17:26:04 +03001652/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001653void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001654 int type_arg,
1655 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001656 int export_size_delta,
1657 int expected_export_status_arg,
1658 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001659{
Ronald Cron5425a212020-08-04 14:58:35 +02001660 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001661 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001662 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001663 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001664 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001665 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001666 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001667 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001669
Gilles Peskine8817f612018-12-18 00:18:46 +01001670 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001671
Gilles Peskine4747d192019-04-17 15:05:45 +02001672 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1673 psa_set_key_algorithm( &attributes, alg );
1674 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001675
1676 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001677 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001678
Gilles Peskine49c25912018-10-29 15:15:31 +01001679 /* Export the public key */
1680 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001681 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001682 exported, export_size,
1683 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001684 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001685 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001686 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001687 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001688 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001689 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001690 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001691 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001692 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001693 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1694 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001695 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001696
1697exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001698 /*
1699 * Key attributes may have been returned by psa_get_key_attributes()
1700 * thus reset them as required.
1701 */
1702 psa_reset_key_attributes( &attributes );
1703
itayzafrir3e02b3b2018-06-12 17:06:52 +03001704 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001705 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001706 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001707}
1708/* END_CASE */
1709
Gilles Peskine20035e32018-02-03 22:44:14 +01001710/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001711void import_and_exercise_key( data_t *data,
1712 int type_arg,
1713 int bits_arg,
1714 int alg_arg )
1715{
Ronald Cron5425a212020-08-04 14:58:35 +02001716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001717 psa_key_type_t type = type_arg;
1718 size_t bits = bits_arg;
1719 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001720 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001722 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001723
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001725
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 psa_set_key_usage_flags( &attributes, usage );
1727 psa_set_key_algorithm( &attributes, alg );
1728 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001729
1730 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001731 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001732
1733 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001734 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001735 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1736 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001737
1738 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001739 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001740 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001741
Ronald Cron5425a212020-08-04 14:58:35 +02001742 PSA_ASSERT( psa_destroy_key( key ) );
1743 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001744
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001745exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001746 /*
1747 * Key attributes may have been returned by psa_get_key_attributes()
1748 * thus reset them as required.
1749 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001750 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001751
1752 psa_reset_key_attributes( &attributes );
1753 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001754 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001755}
1756/* END_CASE */
1757
1758/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001759void effective_key_attributes( int type_arg, int expected_type_arg,
1760 int bits_arg, int expected_bits_arg,
1761 int usage_arg, int expected_usage_arg,
1762 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001763{
Ronald Cron5425a212020-08-04 14:58:35 +02001764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001765 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001766 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001767 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001768 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001769 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001770 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001771 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001772 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001774
Gilles Peskine8817f612018-12-18 00:18:46 +01001775 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001776
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001777 psa_set_key_usage_flags( &attributes, usage );
1778 psa_set_key_algorithm( &attributes, alg );
1779 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001780 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001781
Ronald Cron5425a212020-08-04 14:58:35 +02001782 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001783 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001784
Ronald Cron5425a212020-08-04 14:58:35 +02001785 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001786 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1787 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1788 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1789 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001790
1791exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001792 /*
1793 * Key attributes may have been returned by psa_get_key_attributes()
1794 * thus reset them as required.
1795 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001796 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001797
1798 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001799 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001800}
1801/* END_CASE */
1802
1803/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001804void check_key_policy( int type_arg, int bits_arg,
1805 int usage_arg, int alg_arg )
1806{
1807 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1808 usage_arg, usage_arg, alg_arg, alg_arg );
1809 goto exit;
1810}
1811/* END_CASE */
1812
1813/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001814void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001815{
1816 /* Test each valid way of initializing the object, except for `= {0}`, as
1817 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1818 * though it's OK by the C standard. We could test for this, but we'd need
1819 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001820 psa_key_attributes_t func = psa_key_attributes_init( );
1821 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1822 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001823
1824 memset( &zero, 0, sizeof( zero ) );
1825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001826 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1827 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1828 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001829
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001830 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1831 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1832 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1833
1834 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1835 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1836 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1837
1838 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1839 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1840 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1841
1842 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1843 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1844 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001845}
1846/* END_CASE */
1847
1848/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001849void mac_key_policy( int policy_usage,
1850 int policy_alg,
1851 int key_type,
1852 data_t *key_data,
1853 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001854{
Ronald Cron5425a212020-08-04 14:58:35 +02001855 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001857 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001858 psa_status_t status;
1859 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001860
Gilles Peskine8817f612018-12-18 00:18:46 +01001861 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001862
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001863 psa_set_key_usage_flags( &attributes, policy_usage );
1864 psa_set_key_algorithm( &attributes, policy_alg );
1865 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001866
Gilles Peskine049c7532019-05-15 20:22:09 +02001867 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001868 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001869
Ronald Cron5425a212020-08-04 14:58:35 +02001870 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001871 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001872 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001873 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001874 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001875 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001876 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001877
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001878 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001879 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001880 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001881 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001882 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001883 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001884 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001885
1886exit:
1887 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001888 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001889 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001890}
1891/* END_CASE */
1892
1893/* BEGIN_CASE */
1894void cipher_key_policy( int policy_usage,
1895 int policy_alg,
1896 int key_type,
1897 data_t *key_data,
1898 int exercise_alg )
1899{
Ronald Cron5425a212020-08-04 14:58:35 +02001900 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001902 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001903 psa_status_t status;
1904
Gilles Peskine8817f612018-12-18 00:18:46 +01001905 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001906
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001907 psa_set_key_usage_flags( &attributes, policy_usage );
1908 psa_set_key_algorithm( &attributes, policy_alg );
1909 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001910
Gilles Peskine049c7532019-05-15 20:22:09 +02001911 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001912 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001913
Ronald Cron5425a212020-08-04 14:58:35 +02001914 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001915 if( policy_alg == exercise_alg &&
1916 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001917 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001919 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920 psa_cipher_abort( &operation );
1921
Ronald Cron5425a212020-08-04 14:58:35 +02001922 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001923 if( policy_alg == exercise_alg &&
1924 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001925 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001926 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001927 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001928
1929exit:
1930 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001931 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001932 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001933}
1934/* END_CASE */
1935
1936/* BEGIN_CASE */
1937void aead_key_policy( int policy_usage,
1938 int policy_alg,
1939 int key_type,
1940 data_t *key_data,
1941 int nonce_length_arg,
1942 int tag_length_arg,
1943 int exercise_alg )
1944{
Ronald Cron5425a212020-08-04 14:58:35 +02001945 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001946 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001947 psa_status_t status;
1948 unsigned char nonce[16] = {0};
1949 size_t nonce_length = nonce_length_arg;
1950 unsigned char tag[16];
1951 size_t tag_length = tag_length_arg;
1952 size_t output_length;
1953
1954 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1955 TEST_ASSERT( tag_length <= sizeof( tag ) );
1956
Gilles Peskine8817f612018-12-18 00:18:46 +01001957 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001959 psa_set_key_usage_flags( &attributes, policy_usage );
1960 psa_set_key_algorithm( &attributes, policy_alg );
1961 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001962
Gilles Peskine049c7532019-05-15 20:22:09 +02001963 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001964 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001965
Ronald Cron5425a212020-08-04 14:58:35 +02001966 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001967 nonce, nonce_length,
1968 NULL, 0,
1969 NULL, 0,
1970 tag, tag_length,
1971 &output_length );
1972 if( policy_alg == exercise_alg &&
1973 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001974 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001975 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001976 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977
1978 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001979 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001980 nonce, nonce_length,
1981 NULL, 0,
1982 tag, tag_length,
1983 NULL, 0,
1984 &output_length );
1985 if( policy_alg == exercise_alg &&
1986 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001987 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001988 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001989 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990
1991exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001992 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001993 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994}
1995/* END_CASE */
1996
1997/* BEGIN_CASE */
1998void asymmetric_encryption_key_policy( int policy_usage,
1999 int policy_alg,
2000 int key_type,
2001 data_t *key_data,
2002 int exercise_alg )
2003{
Ronald Cron5425a212020-08-04 14:58:35 +02002004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002005 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006 psa_status_t status;
2007 size_t key_bits;
2008 size_t buffer_length;
2009 unsigned char *buffer = NULL;
2010 size_t output_length;
2011
Gilles Peskine8817f612018-12-18 00:18:46 +01002012 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002014 psa_set_key_usage_flags( &attributes, policy_usage );
2015 psa_set_key_algorithm( &attributes, policy_alg );
2016 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017
Gilles Peskine049c7532019-05-15 20:22:09 +02002018 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002019 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020
Ronald Cron5425a212020-08-04 14:58:35 +02002021 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002022 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2024 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002025 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002026
Ronald Cron5425a212020-08-04 14:58:35 +02002027 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028 NULL, 0,
2029 NULL, 0,
2030 buffer, buffer_length,
2031 &output_length );
2032 if( policy_alg == exercise_alg &&
2033 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002034 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002036 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002038 if( buffer_length != 0 )
2039 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002040 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041 buffer, buffer_length,
2042 NULL, 0,
2043 buffer, buffer_length,
2044 &output_length );
2045 if( policy_alg == exercise_alg &&
2046 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002047 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002048 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002049 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050
2051exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002052 /*
2053 * Key attributes may have been returned by psa_get_key_attributes()
2054 * thus reset them as required.
2055 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002056 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002057
2058 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002059 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002060 mbedtls_free( buffer );
2061}
2062/* END_CASE */
2063
2064/* BEGIN_CASE */
2065void asymmetric_signature_key_policy( int policy_usage,
2066 int policy_alg,
2067 int key_type,
2068 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002069 int exercise_alg,
2070 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002071{
Ronald Cron5425a212020-08-04 14:58:35 +02002072 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002073 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002074 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002075 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2076 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2077 * compatible with the policy and `payload_length_arg` is supposed to be
2078 * a valid input length to sign. If `payload_length_arg <= 0`,
2079 * `exercise_alg` is supposed to be forbidden by the policy. */
2080 int compatible_alg = payload_length_arg > 0;
2081 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002082 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002083 size_t signature_length;
2084
Gilles Peskine8817f612018-12-18 00:18:46 +01002085 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002086
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002087 psa_set_key_usage_flags( &attributes, policy_usage );
2088 psa_set_key_algorithm( &attributes, policy_alg );
2089 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090
Gilles Peskine049c7532019-05-15 20:22:09 +02002091 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002092 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002093
Ronald Cron5425a212020-08-04 14:58:35 +02002094 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002095 payload, payload_length,
2096 signature, sizeof( signature ),
2097 &signature_length );
2098 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002099 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002101 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002102
2103 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002104 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002105 payload, payload_length,
2106 signature, sizeof( signature ) );
2107 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002108 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002109 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002110 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002111
2112exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002113 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002114 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002115}
2116/* END_CASE */
2117
Janos Follathba3fab92019-06-11 14:50:16 +01002118/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002119void derive_key_policy( int policy_usage,
2120 int policy_alg,
2121 int key_type,
2122 data_t *key_data,
2123 int exercise_alg )
2124{
Ronald Cron5425a212020-08-04 14:58:35 +02002125 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002127 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002128 psa_status_t status;
2129
Gilles Peskine8817f612018-12-18 00:18:46 +01002130 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002131
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002132 psa_set_key_usage_flags( &attributes, policy_usage );
2133 psa_set_key_algorithm( &attributes, policy_alg );
2134 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002135
Gilles Peskine049c7532019-05-15 20:22:09 +02002136 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002137 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002138
Janos Follathba3fab92019-06-11 14:50:16 +01002139 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2140
2141 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2142 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002143 {
Janos Follathba3fab92019-06-11 14:50:16 +01002144 PSA_ASSERT( psa_key_derivation_input_bytes(
2145 &operation,
2146 PSA_KEY_DERIVATION_INPUT_SEED,
2147 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002148 }
Janos Follathba3fab92019-06-11 14:50:16 +01002149
2150 status = psa_key_derivation_input_key( &operation,
2151 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002152 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002153
Gilles Peskineea0fb492018-07-12 17:17:20 +02002154 if( policy_alg == exercise_alg &&
2155 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002156 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002157 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002158 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002159
2160exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002161 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002162 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002163 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002164}
2165/* END_CASE */
2166
2167/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002168void agreement_key_policy( int policy_usage,
2169 int policy_alg,
2170 int key_type_arg,
2171 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002172 int exercise_alg,
2173 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002174{
Ronald Cron5425a212020-08-04 14:58:35 +02002175 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002177 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002178 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002179 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002180 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002181
Gilles Peskine8817f612018-12-18 00:18:46 +01002182 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002183
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002184 psa_set_key_usage_flags( &attributes, policy_usage );
2185 psa_set_key_algorithm( &attributes, policy_alg );
2186 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002187
Gilles Peskine049c7532019-05-15 20:22:09 +02002188 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002189 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002190
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002191 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002192 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002193
Steven Cooremance48e852020-10-05 16:02:45 +02002194 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002195
2196exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002197 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002198 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002199 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002200}
2201/* END_CASE */
2202
2203/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002204void key_policy_alg2( int key_type_arg, data_t *key_data,
2205 int usage_arg, int alg_arg, int alg2_arg )
2206{
Ronald Cron5425a212020-08-04 14:58:35 +02002207 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002208 psa_key_type_t key_type = key_type_arg;
2209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2210 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2211 psa_key_usage_t usage = usage_arg;
2212 psa_algorithm_t alg = alg_arg;
2213 psa_algorithm_t alg2 = alg2_arg;
2214
2215 PSA_ASSERT( psa_crypto_init( ) );
2216
2217 psa_set_key_usage_flags( &attributes, usage );
2218 psa_set_key_algorithm( &attributes, alg );
2219 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2220 psa_set_key_type( &attributes, key_type );
2221 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002222 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002223
Ronald Cron5425a212020-08-04 14:58:35 +02002224 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002225 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2226 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2227 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2228
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002229 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002230 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002231 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002232 goto exit;
2233
2234exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002235 /*
2236 * Key attributes may have been returned by psa_get_key_attributes()
2237 * thus reset them as required.
2238 */
2239 psa_reset_key_attributes( &got_attributes );
2240
Ronald Cron5425a212020-08-04 14:58:35 +02002241 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002242 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002243}
2244/* END_CASE */
2245
2246/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002247void raw_agreement_key_policy( int policy_usage,
2248 int policy_alg,
2249 int key_type_arg,
2250 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002251 int exercise_alg,
2252 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002253{
Ronald Cron5425a212020-08-04 14:58:35 +02002254 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002256 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002258 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002259 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002260
2261 PSA_ASSERT( psa_crypto_init( ) );
2262
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002263 psa_set_key_usage_flags( &attributes, policy_usage );
2264 psa_set_key_algorithm( &attributes, policy_alg );
2265 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002266
Gilles Peskine049c7532019-05-15 20:22:09 +02002267 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002268 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002269
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002270 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002271
Steven Cooremance48e852020-10-05 16:02:45 +02002272 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002273
2274exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002275 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002276 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002277 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002278}
2279/* END_CASE */
2280
2281/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002282void copy_success( int source_usage_arg,
2283 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002284 int type_arg, data_t *material,
2285 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002286 int target_usage_arg,
2287 int target_alg_arg, int target_alg2_arg,
2288 int expected_usage_arg,
2289 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002290{
Gilles Peskineca25db92019-04-19 11:43:08 +02002291 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2292 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002293 psa_key_usage_t expected_usage = expected_usage_arg;
2294 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002295 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002296 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2297 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002298 uint8_t *export_buffer = NULL;
2299
Gilles Peskine57ab7212019-01-28 13:03:09 +01002300 PSA_ASSERT( psa_crypto_init( ) );
2301
Gilles Peskineca25db92019-04-19 11:43:08 +02002302 /* Prepare the source key. */
2303 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2304 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002305 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002306 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002307 PSA_ASSERT( psa_import_key( &source_attributes,
2308 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002309 &source_key ) );
2310 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002311
Gilles Peskineca25db92019-04-19 11:43:08 +02002312 /* Prepare the target attributes. */
2313 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002314 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002315 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002316 /* Set volatile lifetime to reset the key identifier to 0. */
2317 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2318 }
2319
Gilles Peskineca25db92019-04-19 11:43:08 +02002320 if( target_usage_arg != -1 )
2321 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2322 if( target_alg_arg != -1 )
2323 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002324 if( target_alg2_arg != -1 )
2325 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002326
2327 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002328 PSA_ASSERT( psa_copy_key( source_key,
2329 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002330
2331 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002332 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002333
2334 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002335 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002336 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2337 psa_get_key_type( &target_attributes ) );
2338 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2339 psa_get_key_bits( &target_attributes ) );
2340 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2341 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002342 TEST_EQUAL( expected_alg2,
2343 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002344 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2345 {
2346 size_t length;
2347 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002348 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002349 material->len, &length ) );
2350 ASSERT_COMPARE( material->x, material->len,
2351 export_buffer, length );
2352 }
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002353 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002354 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002355 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002356 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002357
Ronald Cron5425a212020-08-04 14:58:35 +02002358 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002359
2360exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002361 /*
2362 * Source and target key attributes may have been returned by
2363 * psa_get_key_attributes() thus reset them as required.
2364 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002365 psa_reset_key_attributes( &source_attributes );
2366 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002367
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002368 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002369 mbedtls_free( export_buffer );
2370}
2371/* END_CASE */
2372
2373/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002374void copy_fail( int source_usage_arg,
2375 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002376 int type_arg, data_t *material,
2377 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002378 int target_usage_arg,
2379 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002380 int expected_status_arg )
2381{
2382 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2383 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002384 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2385 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002386
2387 PSA_ASSERT( psa_crypto_init( ) );
2388
2389 /* Prepare the source key. */
2390 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2391 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002392 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002393 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002394 PSA_ASSERT( psa_import_key( &source_attributes,
2395 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002396 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002397
2398 /* Prepare the target attributes. */
2399 psa_set_key_type( &target_attributes, target_type_arg );
2400 psa_set_key_bits( &target_attributes, target_bits_arg );
2401 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2402 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002403 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002404
2405 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002406 TEST_EQUAL( psa_copy_key( source_key,
2407 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002408 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002409
Ronald Cron5425a212020-08-04 14:58:35 +02002410 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002411
Gilles Peskine4a644642019-05-03 17:14:08 +02002412exit:
2413 psa_reset_key_attributes( &source_attributes );
2414 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002415 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002416}
2417/* END_CASE */
2418
2419/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002420void hash_operation_init( )
2421{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002422 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002423 /* Test each valid way of initializing the object, except for `= {0}`, as
2424 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2425 * though it's OK by the C standard. We could test for this, but we'd need
2426 * to supress the Clang warning for the test. */
2427 psa_hash_operation_t func = psa_hash_operation_init( );
2428 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2429 psa_hash_operation_t zero;
2430
2431 memset( &zero, 0, sizeof( zero ) );
2432
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002433 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002434 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2435 PSA_ERROR_BAD_STATE );
2436 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2437 PSA_ERROR_BAD_STATE );
2438 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2439 PSA_ERROR_BAD_STATE );
2440
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002441 /* A default hash operation should be abortable without error. */
2442 PSA_ASSERT( psa_hash_abort( &func ) );
2443 PSA_ASSERT( psa_hash_abort( &init ) );
2444 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002445}
2446/* END_CASE */
2447
2448/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449void hash_setup( int alg_arg,
2450 int expected_status_arg )
2451{
2452 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002453 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002454 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002455 psa_status_t status;
2456
Gilles Peskine8817f612018-12-18 00:18:46 +01002457 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002458
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002459 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002460 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002461
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002462 /* Whether setup succeeded or failed, abort must succeed. */
2463 PSA_ASSERT( psa_hash_abort( &operation ) );
2464
2465 /* If setup failed, reproduce the failure, so as to
2466 * test the resulting state of the operation object. */
2467 if( status != PSA_SUCCESS )
2468 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2469
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002470 /* Now the operation object should be reusable. */
2471#if defined(KNOWN_SUPPORTED_HASH_ALG)
2472 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2473 PSA_ASSERT( psa_hash_abort( &operation ) );
2474#endif
2475
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002477 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002478}
2479/* END_CASE */
2480
2481/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002482void hash_compute_fail( int alg_arg, data_t *input,
2483 int output_size_arg, int expected_status_arg )
2484{
2485 psa_algorithm_t alg = alg_arg;
2486 uint8_t *output = NULL;
2487 size_t output_size = output_size_arg;
2488 size_t output_length = INVALID_EXPORT_LENGTH;
2489 psa_status_t expected_status = expected_status_arg;
2490 psa_status_t status;
2491
2492 ASSERT_ALLOC( output, output_size );
2493
2494 PSA_ASSERT( psa_crypto_init( ) );
2495
2496 status = psa_hash_compute( alg, input->x, input->len,
2497 output, output_size, &output_length );
2498 TEST_EQUAL( status, expected_status );
2499 TEST_ASSERT( output_length <= output_size );
2500
2501exit:
2502 mbedtls_free( output );
2503 PSA_DONE( );
2504}
2505/* END_CASE */
2506
2507/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002508void hash_compare_fail( int alg_arg, data_t *input,
2509 data_t *reference_hash,
2510 int expected_status_arg )
2511{
2512 psa_algorithm_t alg = alg_arg;
2513 psa_status_t expected_status = expected_status_arg;
2514 psa_status_t status;
2515
2516 PSA_ASSERT( psa_crypto_init( ) );
2517
2518 status = psa_hash_compare( alg, input->x, input->len,
2519 reference_hash->x, reference_hash->len );
2520 TEST_EQUAL( status, expected_status );
2521
2522exit:
2523 PSA_DONE( );
2524}
2525/* END_CASE */
2526
2527/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002528void hash_compute_compare( int alg_arg, data_t *input,
2529 data_t *expected_output )
2530{
2531 psa_algorithm_t alg = alg_arg;
2532 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2533 size_t output_length = INVALID_EXPORT_LENGTH;
2534 size_t i;
2535
2536 PSA_ASSERT( psa_crypto_init( ) );
2537
2538 /* Compute with tight buffer */
2539 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002540 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002541 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002542 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002543 ASSERT_COMPARE( output, output_length,
2544 expected_output->x, expected_output->len );
2545
2546 /* Compute with larger buffer */
2547 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2548 output, sizeof( output ),
2549 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002550 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002551 ASSERT_COMPARE( output, output_length,
2552 expected_output->x, expected_output->len );
2553
2554 /* Compare with correct hash */
2555 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2556 output, output_length ) );
2557
2558 /* Compare with trailing garbage */
2559 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2560 output, output_length + 1 ),
2561 PSA_ERROR_INVALID_SIGNATURE );
2562
2563 /* Compare with truncated hash */
2564 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2565 output, output_length - 1 ),
2566 PSA_ERROR_INVALID_SIGNATURE );
2567
2568 /* Compare with corrupted value */
2569 for( i = 0; i < output_length; i++ )
2570 {
Chris Jones9634bb12021-01-20 15:56:42 +00002571 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002572 output[i] ^= 1;
2573 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2574 output, output_length ),
2575 PSA_ERROR_INVALID_SIGNATURE );
2576 output[i] ^= 1;
2577 }
2578
2579exit:
2580 PSA_DONE( );
2581}
2582/* END_CASE */
2583
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002584/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002585void hash_bad_order( )
2586{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002587 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002588 unsigned char input[] = "";
2589 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002590 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002591 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2592 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2593 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002594 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002595 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002596 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002597
Gilles Peskine8817f612018-12-18 00:18:46 +01002598 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002599
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002600 /* Call setup twice in a row. */
2601 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2602 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2603 PSA_ERROR_BAD_STATE );
2604 PSA_ASSERT( psa_hash_abort( &operation ) );
2605
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002606 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002607 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002608 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002609 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002610
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002611 /* Call update after finish. */
2612 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2613 PSA_ASSERT( psa_hash_finish( &operation,
2614 hash, sizeof( hash ), &hash_len ) );
2615 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002616 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002617 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002618
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002619 /* Call verify without calling setup beforehand. */
2620 TEST_EQUAL( psa_hash_verify( &operation,
2621 valid_hash, sizeof( valid_hash ) ),
2622 PSA_ERROR_BAD_STATE );
2623 PSA_ASSERT( psa_hash_abort( &operation ) );
2624
2625 /* Call verify after finish. */
2626 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2627 PSA_ASSERT( psa_hash_finish( &operation,
2628 hash, sizeof( hash ), &hash_len ) );
2629 TEST_EQUAL( psa_hash_verify( &operation,
2630 valid_hash, sizeof( valid_hash ) ),
2631 PSA_ERROR_BAD_STATE );
2632 PSA_ASSERT( psa_hash_abort( &operation ) );
2633
2634 /* Call verify twice in a row. */
2635 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2636 PSA_ASSERT( psa_hash_verify( &operation,
2637 valid_hash, sizeof( valid_hash ) ) );
2638 TEST_EQUAL( psa_hash_verify( &operation,
2639 valid_hash, sizeof( valid_hash ) ),
2640 PSA_ERROR_BAD_STATE );
2641 PSA_ASSERT( psa_hash_abort( &operation ) );
2642
2643 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002644 TEST_EQUAL( psa_hash_finish( &operation,
2645 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002646 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002647 PSA_ASSERT( psa_hash_abort( &operation ) );
2648
2649 /* Call finish twice in a row. */
2650 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2651 PSA_ASSERT( psa_hash_finish( &operation,
2652 hash, sizeof( hash ), &hash_len ) );
2653 TEST_EQUAL( psa_hash_finish( &operation,
2654 hash, sizeof( hash ), &hash_len ),
2655 PSA_ERROR_BAD_STATE );
2656 PSA_ASSERT( psa_hash_abort( &operation ) );
2657
2658 /* Call finish after calling verify. */
2659 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2660 PSA_ASSERT( psa_hash_verify( &operation,
2661 valid_hash, sizeof( valid_hash ) ) );
2662 TEST_EQUAL( psa_hash_finish( &operation,
2663 hash, sizeof( hash ), &hash_len ),
2664 PSA_ERROR_BAD_STATE );
2665 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002666
2667exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002668 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002669}
2670/* END_CASE */
2671
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002672/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002673void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002674{
2675 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002676 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2677 * appended to it */
2678 unsigned char hash[] = {
2679 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2680 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2681 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002682 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002683 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002684
Gilles Peskine8817f612018-12-18 00:18:46 +01002685 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002686
itayzafrir27e69452018-11-01 14:26:34 +02002687 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002688 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002689 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002690 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002691
itayzafrir27e69452018-11-01 14:26:34 +02002692 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002693 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002694 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002695 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002696
itayzafrir27e69452018-11-01 14:26:34 +02002697 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002698 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002699 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002700 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002701
itayzafrirec93d302018-10-18 18:01:10 +03002702exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002703 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002704}
2705/* END_CASE */
2706
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002707/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2708void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002709{
2710 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002711 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002712 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002713 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002714 size_t hash_len;
2715
Gilles Peskine8817f612018-12-18 00:18:46 +01002716 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002717
itayzafrir58028322018-10-25 10:22:01 +03002718 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002719 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002720 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002721 hash, expected_size - 1, &hash_len ),
2722 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002723
2724exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002725 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002726}
2727/* END_CASE */
2728
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002729/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2730void hash_clone_source_state( )
2731{
2732 psa_algorithm_t alg = PSA_ALG_SHA_256;
2733 unsigned char hash[PSA_HASH_MAX_SIZE];
2734 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2735 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2736 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2737 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2738 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2739 size_t hash_len;
2740
2741 PSA_ASSERT( psa_crypto_init( ) );
2742 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2743
2744 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2745 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2746 PSA_ASSERT( psa_hash_finish( &op_finished,
2747 hash, sizeof( hash ), &hash_len ) );
2748 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2749 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2750
2751 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2752 PSA_ERROR_BAD_STATE );
2753
2754 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2755 PSA_ASSERT( psa_hash_finish( &op_init,
2756 hash, sizeof( hash ), &hash_len ) );
2757 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2758 PSA_ASSERT( psa_hash_finish( &op_finished,
2759 hash, sizeof( hash ), &hash_len ) );
2760 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2761 PSA_ASSERT( psa_hash_finish( &op_aborted,
2762 hash, sizeof( hash ), &hash_len ) );
2763
2764exit:
2765 psa_hash_abort( &op_source );
2766 psa_hash_abort( &op_init );
2767 psa_hash_abort( &op_setup );
2768 psa_hash_abort( &op_finished );
2769 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002770 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002771}
2772/* END_CASE */
2773
2774/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2775void hash_clone_target_state( )
2776{
2777 psa_algorithm_t alg = PSA_ALG_SHA_256;
2778 unsigned char hash[PSA_HASH_MAX_SIZE];
2779 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2780 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2781 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2782 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2783 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2784 size_t hash_len;
2785
2786 PSA_ASSERT( psa_crypto_init( ) );
2787
2788 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2789 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2790 PSA_ASSERT( psa_hash_finish( &op_finished,
2791 hash, sizeof( hash ), &hash_len ) );
2792 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2793 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2794
2795 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2796 PSA_ASSERT( psa_hash_finish( &op_target,
2797 hash, sizeof( hash ), &hash_len ) );
2798
2799 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2800 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2801 PSA_ERROR_BAD_STATE );
2802 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2803 PSA_ERROR_BAD_STATE );
2804
2805exit:
2806 psa_hash_abort( &op_target );
2807 psa_hash_abort( &op_init );
2808 psa_hash_abort( &op_setup );
2809 psa_hash_abort( &op_finished );
2810 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002811 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002812}
2813/* END_CASE */
2814
itayzafrir58028322018-10-25 10:22:01 +03002815/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002816void mac_operation_init( )
2817{
Jaeden Amero252ef282019-02-15 14:05:35 +00002818 const uint8_t input[1] = { 0 };
2819
Jaeden Amero769ce272019-01-04 11:48:03 +00002820 /* Test each valid way of initializing the object, except for `= {0}`, as
2821 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2822 * though it's OK by the C standard. We could test for this, but we'd need
2823 * to supress the Clang warning for the test. */
2824 psa_mac_operation_t func = psa_mac_operation_init( );
2825 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2826 psa_mac_operation_t zero;
2827
2828 memset( &zero, 0, sizeof( zero ) );
2829
Jaeden Amero252ef282019-02-15 14:05:35 +00002830 /* A freshly-initialized MAC operation should not be usable. */
2831 TEST_EQUAL( psa_mac_update( &func,
2832 input, sizeof( input ) ),
2833 PSA_ERROR_BAD_STATE );
2834 TEST_EQUAL( psa_mac_update( &init,
2835 input, sizeof( input ) ),
2836 PSA_ERROR_BAD_STATE );
2837 TEST_EQUAL( psa_mac_update( &zero,
2838 input, sizeof( input ) ),
2839 PSA_ERROR_BAD_STATE );
2840
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002841 /* A default MAC operation should be abortable without error. */
2842 PSA_ASSERT( psa_mac_abort( &func ) );
2843 PSA_ASSERT( psa_mac_abort( &init ) );
2844 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002845}
2846/* END_CASE */
2847
2848/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002849void mac_setup( int key_type_arg,
2850 data_t *key,
2851 int alg_arg,
2852 int expected_status_arg )
2853{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002854 psa_key_type_t key_type = key_type_arg;
2855 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002856 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002857 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002858 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2859#if defined(KNOWN_SUPPORTED_MAC_ALG)
2860 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2861#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002862
Gilles Peskine8817f612018-12-18 00:18:46 +01002863 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002864
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002865 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2866 &operation, &status ) )
2867 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002868 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002869
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002870 /* The operation object should be reusable. */
2871#if defined(KNOWN_SUPPORTED_MAC_ALG)
2872 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2873 smoke_test_key_data,
2874 sizeof( smoke_test_key_data ),
2875 KNOWN_SUPPORTED_MAC_ALG,
2876 &operation, &status ) )
2877 goto exit;
2878 TEST_EQUAL( status, PSA_SUCCESS );
2879#endif
2880
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002881exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002882 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002883}
2884/* END_CASE */
2885
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002886/* 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 +00002887void mac_bad_order( )
2888{
Ronald Cron5425a212020-08-04 14:58:35 +02002889 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002890 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2891 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002892 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002893 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2894 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2895 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002897 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2898 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2899 size_t sign_mac_length = 0;
2900 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2901 const uint8_t verify_mac[] = {
2902 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2903 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2904 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2905
2906 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002907 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002908 psa_set_key_algorithm( &attributes, alg );
2909 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002910
Ronald Cron5425a212020-08-04 14:58:35 +02002911 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2912 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002913
Jaeden Amero252ef282019-02-15 14:05:35 +00002914 /* Call update without calling setup beforehand. */
2915 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2916 PSA_ERROR_BAD_STATE );
2917 PSA_ASSERT( psa_mac_abort( &operation ) );
2918
2919 /* Call sign finish without calling setup beforehand. */
2920 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2921 &sign_mac_length),
2922 PSA_ERROR_BAD_STATE );
2923 PSA_ASSERT( psa_mac_abort( &operation ) );
2924
2925 /* Call verify finish without calling setup beforehand. */
2926 TEST_EQUAL( psa_mac_verify_finish( &operation,
2927 verify_mac, sizeof( verify_mac ) ),
2928 PSA_ERROR_BAD_STATE );
2929 PSA_ASSERT( psa_mac_abort( &operation ) );
2930
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002931 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002932 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2933 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002934 PSA_ERROR_BAD_STATE );
2935 PSA_ASSERT( psa_mac_abort( &operation ) );
2936
Jaeden Amero252ef282019-02-15 14:05:35 +00002937 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002938 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002939 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2940 PSA_ASSERT( psa_mac_sign_finish( &operation,
2941 sign_mac, sizeof( sign_mac ),
2942 &sign_mac_length ) );
2943 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2944 PSA_ERROR_BAD_STATE );
2945 PSA_ASSERT( psa_mac_abort( &operation ) );
2946
2947 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002948 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002949 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2950 PSA_ASSERT( psa_mac_verify_finish( &operation,
2951 verify_mac, sizeof( verify_mac ) ) );
2952 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2953 PSA_ERROR_BAD_STATE );
2954 PSA_ASSERT( psa_mac_abort( &operation ) );
2955
2956 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002957 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002958 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2959 PSA_ASSERT( psa_mac_sign_finish( &operation,
2960 sign_mac, sizeof( sign_mac ),
2961 &sign_mac_length ) );
2962 TEST_EQUAL( psa_mac_sign_finish( &operation,
2963 sign_mac, sizeof( sign_mac ),
2964 &sign_mac_length ),
2965 PSA_ERROR_BAD_STATE );
2966 PSA_ASSERT( psa_mac_abort( &operation ) );
2967
2968 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002969 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002970 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2971 PSA_ASSERT( psa_mac_verify_finish( &operation,
2972 verify_mac, sizeof( verify_mac ) ) );
2973 TEST_EQUAL( psa_mac_verify_finish( &operation,
2974 verify_mac, sizeof( verify_mac ) ),
2975 PSA_ERROR_BAD_STATE );
2976 PSA_ASSERT( psa_mac_abort( &operation ) );
2977
2978 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002979 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002980 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2981 TEST_EQUAL( psa_mac_verify_finish( &operation,
2982 verify_mac, sizeof( verify_mac ) ),
2983 PSA_ERROR_BAD_STATE );
2984 PSA_ASSERT( psa_mac_abort( &operation ) );
2985
2986 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002987 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002988 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2989 TEST_EQUAL( psa_mac_sign_finish( &operation,
2990 sign_mac, sizeof( sign_mac ),
2991 &sign_mac_length ),
2992 PSA_ERROR_BAD_STATE );
2993 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002994
Ronald Cron5425a212020-08-04 14:58:35 +02002995 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002996
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002997exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002998 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002999}
3000/* END_CASE */
3001
3002/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003003void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003004 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003005 int alg_arg,
3006 data_t *input,
3007 data_t *expected_mac )
3008{
Ronald Cron5425a212020-08-04 14:58:35 +02003009 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003010 psa_key_type_t key_type = key_type_arg;
3011 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003012 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003014 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003015 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003016 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003017 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003018 const size_t output_sizes_to_test[] = {
3019 0,
3020 1,
3021 expected_mac->len - 1,
3022 expected_mac->len,
3023 expected_mac->len + 1,
3024 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003025
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003026 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003027 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003028 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003029
Gilles Peskine8817f612018-12-18 00:18:46 +01003030 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003031
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003032 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003033 psa_set_key_algorithm( &attributes, alg );
3034 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003035
Ronald Cron5425a212020-08-04 14:58:35 +02003036 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3037 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003038
Gilles Peskine8b356b52020-08-25 23:44:59 +02003039 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3040 {
3041 const size_t output_size = output_sizes_to_test[i];
3042 psa_status_t expected_status =
3043 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3044 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003045
Chris Jones9634bb12021-01-20 15:56:42 +00003046 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003047 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003048
Gilles Peskine8b356b52020-08-25 23:44:59 +02003049 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003050 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003051 PSA_ASSERT( psa_mac_update( &operation,
3052 input->x, input->len ) );
3053 TEST_EQUAL( psa_mac_sign_finish( &operation,
3054 actual_mac, output_size,
3055 &mac_length ),
3056 expected_status );
3057 PSA_ASSERT( psa_mac_abort( &operation ) );
3058
3059 if( expected_status == PSA_SUCCESS )
3060 {
3061 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3062 actual_mac, mac_length );
3063 }
3064 mbedtls_free( actual_mac );
3065 actual_mac = NULL;
3066 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003067
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003068exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003069 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003070 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003071 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003072 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003073}
3074/* END_CASE */
3075
3076/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003077void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003078 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003079 int alg_arg,
3080 data_t *input,
3081 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003082{
Ronald Cron5425a212020-08-04 14:58:35 +02003083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003084 psa_key_type_t key_type = key_type_arg;
3085 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003086 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003088 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089
Gilles Peskine69c12672018-06-28 00:07:19 +02003090 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3091
Gilles Peskine8817f612018-12-18 00:18:46 +01003092 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003093
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003094 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003095 psa_set_key_algorithm( &attributes, alg );
3096 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003097
Ronald Cron5425a212020-08-04 14:58:35 +02003098 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3099 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003100
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003101 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003102 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003103 PSA_ASSERT( psa_mac_update( &operation,
3104 input->x, input->len ) );
3105 PSA_ASSERT( psa_mac_verify_finish( &operation,
3106 expected_mac->x,
3107 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003108
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003109 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003110 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003111 PSA_ASSERT( psa_mac_update( &operation,
3112 input->x, input->len ) );
3113 TEST_EQUAL( psa_mac_verify_finish( &operation,
3114 expected_mac->x,
3115 expected_mac->len - 1 ),
3116 PSA_ERROR_INVALID_SIGNATURE );
3117
3118 /* Test a MAC that's too long. */
3119 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3120 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003121 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003122 PSA_ASSERT( psa_mac_update( &operation,
3123 input->x, input->len ) );
3124 TEST_EQUAL( psa_mac_verify_finish( &operation,
3125 perturbed_mac,
3126 expected_mac->len + 1 ),
3127 PSA_ERROR_INVALID_SIGNATURE );
3128
3129 /* Test changing one byte. */
3130 for( size_t i = 0; i < expected_mac->len; i++ )
3131 {
Chris Jones9634bb12021-01-20 15:56:42 +00003132 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003133 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003134 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003135 PSA_ASSERT( psa_mac_update( &operation,
3136 input->x, input->len ) );
3137 TEST_EQUAL( psa_mac_verify_finish( &operation,
3138 perturbed_mac,
3139 expected_mac->len ),
3140 PSA_ERROR_INVALID_SIGNATURE );
3141 perturbed_mac[i] ^= 1;
3142 }
3143
Gilles Peskine8c9def32018-02-08 10:02:12 +01003144exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003145 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003146 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003147 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003148 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003149}
3150/* END_CASE */
3151
3152/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003153void cipher_operation_init( )
3154{
Jaeden Ameroab439972019-02-15 14:12:05 +00003155 const uint8_t input[1] = { 0 };
3156 unsigned char output[1] = { 0 };
3157 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003158 /* Test each valid way of initializing the object, except for `= {0}`, as
3159 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3160 * though it's OK by the C standard. We could test for this, but we'd need
3161 * to supress the Clang warning for the test. */
3162 psa_cipher_operation_t func = psa_cipher_operation_init( );
3163 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3164 psa_cipher_operation_t zero;
3165
3166 memset( &zero, 0, sizeof( zero ) );
3167
Jaeden Ameroab439972019-02-15 14:12:05 +00003168 /* A freshly-initialized cipher operation should not be usable. */
3169 TEST_EQUAL( psa_cipher_update( &func,
3170 input, sizeof( input ),
3171 output, sizeof( output ),
3172 &output_length ),
3173 PSA_ERROR_BAD_STATE );
3174 TEST_EQUAL( psa_cipher_update( &init,
3175 input, sizeof( input ),
3176 output, sizeof( output ),
3177 &output_length ),
3178 PSA_ERROR_BAD_STATE );
3179 TEST_EQUAL( psa_cipher_update( &zero,
3180 input, sizeof( input ),
3181 output, sizeof( output ),
3182 &output_length ),
3183 PSA_ERROR_BAD_STATE );
3184
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003185 /* A default cipher operation should be abortable without error. */
3186 PSA_ASSERT( psa_cipher_abort( &func ) );
3187 PSA_ASSERT( psa_cipher_abort( &init ) );
3188 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003189}
3190/* END_CASE */
3191
3192/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003193void cipher_setup( int key_type_arg,
3194 data_t *key,
3195 int alg_arg,
3196 int expected_status_arg )
3197{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003198 psa_key_type_t key_type = key_type_arg;
3199 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003200 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003201 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003202 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003203#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003204 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3205#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003206
Gilles Peskine8817f612018-12-18 00:18:46 +01003207 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003208
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003209 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3210 &operation, &status ) )
3211 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003212 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003213
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003214 /* The operation object should be reusable. */
3215#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3216 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3217 smoke_test_key_data,
3218 sizeof( smoke_test_key_data ),
3219 KNOWN_SUPPORTED_CIPHER_ALG,
3220 &operation, &status ) )
3221 goto exit;
3222 TEST_EQUAL( status, PSA_SUCCESS );
3223#endif
3224
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003225exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003226 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003227 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003228}
3229/* END_CASE */
3230
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003231/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003232void cipher_bad_order( )
3233{
Ronald Cron5425a212020-08-04 14:58:35 +02003234 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003235 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3236 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003237 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003238 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003239 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003240 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003241 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3242 0xaa, 0xaa, 0xaa, 0xaa };
3243 const uint8_t text[] = {
3244 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3245 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003246 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003247 size_t length = 0;
3248
3249 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3251 psa_set_key_algorithm( &attributes, alg );
3252 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003253 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3254 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003255
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003256 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003257 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3258 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003259 PSA_ERROR_BAD_STATE );
3260 PSA_ASSERT( psa_cipher_abort( &operation ) );
3261
3262 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003263 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3264 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003265 PSA_ERROR_BAD_STATE );
3266 PSA_ASSERT( psa_cipher_abort( &operation ) );
3267
Jaeden Ameroab439972019-02-15 14:12:05 +00003268 /* Generate an IV without calling setup beforehand. */
3269 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3270 buffer, sizeof( buffer ),
3271 &length ),
3272 PSA_ERROR_BAD_STATE );
3273 PSA_ASSERT( psa_cipher_abort( &operation ) );
3274
3275 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003276 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003277 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3278 buffer, sizeof( buffer ),
3279 &length ) );
3280 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3281 buffer, sizeof( buffer ),
3282 &length ),
3283 PSA_ERROR_BAD_STATE );
3284 PSA_ASSERT( psa_cipher_abort( &operation ) );
3285
3286 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003287 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003288 PSA_ASSERT( psa_cipher_set_iv( &operation,
3289 iv, sizeof( iv ) ) );
3290 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3291 buffer, sizeof( buffer ),
3292 &length ),
3293 PSA_ERROR_BAD_STATE );
3294 PSA_ASSERT( psa_cipher_abort( &operation ) );
3295
3296 /* Set an IV without calling setup beforehand. */
3297 TEST_EQUAL( psa_cipher_set_iv( &operation,
3298 iv, sizeof( iv ) ),
3299 PSA_ERROR_BAD_STATE );
3300 PSA_ASSERT( psa_cipher_abort( &operation ) );
3301
3302 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003303 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003304 PSA_ASSERT( psa_cipher_set_iv( &operation,
3305 iv, sizeof( iv ) ) );
3306 TEST_EQUAL( psa_cipher_set_iv( &operation,
3307 iv, sizeof( iv ) ),
3308 PSA_ERROR_BAD_STATE );
3309 PSA_ASSERT( psa_cipher_abort( &operation ) );
3310
3311 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003312 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003313 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3314 buffer, sizeof( buffer ),
3315 &length ) );
3316 TEST_EQUAL( psa_cipher_set_iv( &operation,
3317 iv, sizeof( iv ) ),
3318 PSA_ERROR_BAD_STATE );
3319 PSA_ASSERT( psa_cipher_abort( &operation ) );
3320
3321 /* Call update without calling setup beforehand. */
3322 TEST_EQUAL( psa_cipher_update( &operation,
3323 text, sizeof( text ),
3324 buffer, sizeof( buffer ),
3325 &length ),
3326 PSA_ERROR_BAD_STATE );
3327 PSA_ASSERT( psa_cipher_abort( &operation ) );
3328
3329 /* Call update without an IV where an IV is required. */
3330 TEST_EQUAL( psa_cipher_update( &operation,
3331 text, sizeof( text ),
3332 buffer, sizeof( buffer ),
3333 &length ),
3334 PSA_ERROR_BAD_STATE );
3335 PSA_ASSERT( psa_cipher_abort( &operation ) );
3336
3337 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003338 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003339 PSA_ASSERT( psa_cipher_set_iv( &operation,
3340 iv, sizeof( iv ) ) );
3341 PSA_ASSERT( psa_cipher_finish( &operation,
3342 buffer, sizeof( buffer ), &length ) );
3343 TEST_EQUAL( psa_cipher_update( &operation,
3344 text, sizeof( text ),
3345 buffer, sizeof( buffer ),
3346 &length ),
3347 PSA_ERROR_BAD_STATE );
3348 PSA_ASSERT( psa_cipher_abort( &operation ) );
3349
3350 /* Call finish without calling setup beforehand. */
3351 TEST_EQUAL( psa_cipher_finish( &operation,
3352 buffer, sizeof( buffer ), &length ),
3353 PSA_ERROR_BAD_STATE );
3354 PSA_ASSERT( psa_cipher_abort( &operation ) );
3355
3356 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003357 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003358 /* Not calling update means we are encrypting an empty buffer, which is OK
3359 * for cipher modes with padding. */
3360 TEST_EQUAL( psa_cipher_finish( &operation,
3361 buffer, sizeof( buffer ), &length ),
3362 PSA_ERROR_BAD_STATE );
3363 PSA_ASSERT( psa_cipher_abort( &operation ) );
3364
3365 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003366 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003367 PSA_ASSERT( psa_cipher_set_iv( &operation,
3368 iv, sizeof( iv ) ) );
3369 PSA_ASSERT( psa_cipher_finish( &operation,
3370 buffer, sizeof( buffer ), &length ) );
3371 TEST_EQUAL( psa_cipher_finish( &operation,
3372 buffer, sizeof( buffer ), &length ),
3373 PSA_ERROR_BAD_STATE );
3374 PSA_ASSERT( psa_cipher_abort( &operation ) );
3375
Ronald Cron5425a212020-08-04 14:58:35 +02003376 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003377
Jaeden Ameroab439972019-02-15 14:12:05 +00003378exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003379 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003380 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003381}
3382/* END_CASE */
3383
3384/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003385void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003386 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003387 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003388 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003389{
Ronald Cron5425a212020-08-04 14:58:35 +02003390 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003391 psa_status_t status;
3392 psa_key_type_t key_type = key_type_arg;
3393 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003394 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003395 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003396 size_t output_buffer_size = 0;
3397 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003398 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003399 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003400 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003401
Gilles Peskine8817f612018-12-18 00:18:46 +01003402 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003403
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003404 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3405 psa_set_key_algorithm( &attributes, alg );
3406 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003407
Ronald Cron5425a212020-08-04 14:58:35 +02003408 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3409 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003410
Ronald Cron5425a212020-08-04 14:58:35 +02003411 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003412
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003413 if( iv->len > 0 )
3414 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003415 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003416 }
3417
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003418 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003419 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003420 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003421
Gilles Peskine8817f612018-12-18 00:18:46 +01003422 PSA_ASSERT( psa_cipher_update( &operation,
3423 input->x, input->len,
3424 output, output_buffer_size,
3425 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003426 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003427 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003428 output + total_output_length,
3429 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003430 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003431 total_output_length += function_output_length;
3432
Gilles Peskinefe11b722018-12-18 00:24:04 +01003433 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003434 if( expected_status == PSA_SUCCESS )
3435 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003436 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003437 ASSERT_COMPARE( expected_output->x, expected_output->len,
3438 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003439 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003440
Gilles Peskine50e586b2018-06-08 14:28:46 +02003441exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003442 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003443 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003444 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003445 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003446}
3447/* END_CASE */
3448
3449/* BEGIN_CASE */
3450void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003451 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003452 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003453 int first_part_size_arg,
3454 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003455 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003456{
Ronald Cron5425a212020-08-04 14:58:35 +02003457 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003458 psa_key_type_t key_type = key_type_arg;
3459 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003460 size_t first_part_size = first_part_size_arg;
3461 size_t output1_length = output1_length_arg;
3462 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003463 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003464 size_t output_buffer_size = 0;
3465 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003466 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003467 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003469
Gilles Peskine8817f612018-12-18 00:18:46 +01003470 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003471
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3473 psa_set_key_algorithm( &attributes, alg );
3474 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003475
Ronald Cron5425a212020-08-04 14:58:35 +02003476 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3477 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003478
Ronald Cron5425a212020-08-04 14:58:35 +02003479 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003480
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003481 if( iv->len > 0 )
3482 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003483 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003484 }
3485
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003486 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003487 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003488 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003489
Gilles Peskinee0866522019-02-19 19:44:00 +01003490 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003491 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3492 output, output_buffer_size,
3493 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003494 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003495 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003496 PSA_ASSERT( psa_cipher_update( &operation,
3497 input->x + first_part_size,
3498 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003499 output + total_output_length,
3500 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003501 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003502 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003503 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003504 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003505 output + total_output_length,
3506 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003507 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003508 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003509 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003510
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003511 ASSERT_COMPARE( expected_output->x, expected_output->len,
3512 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003513
3514exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003515 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003516 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003517 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003518 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003519}
3520/* END_CASE */
3521
3522/* BEGIN_CASE */
3523void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003524 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003525 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003526 int first_part_size_arg,
3527 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003528 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003529{
Ronald Cron5425a212020-08-04 14:58:35 +02003530 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003531 psa_key_type_t key_type = key_type_arg;
3532 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003533 size_t first_part_size = first_part_size_arg;
3534 size_t output1_length = output1_length_arg;
3535 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003536 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003537 size_t output_buffer_size = 0;
3538 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003539 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003540 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003541 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003542
Gilles Peskine8817f612018-12-18 00:18:46 +01003543 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003544
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003545 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3546 psa_set_key_algorithm( &attributes, alg );
3547 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003548
Ronald Cron5425a212020-08-04 14:58:35 +02003549 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3550 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003551
Ronald Cron5425a212020-08-04 14:58:35 +02003552 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003553
Steven Cooreman177deba2020-09-07 17:14:14 +02003554 if( iv->len > 0 )
3555 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003556 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003557 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003558
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003559 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003560 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003561 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003562
Gilles Peskinee0866522019-02-19 19:44:00 +01003563 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003564 PSA_ASSERT( psa_cipher_update( &operation,
3565 input->x, first_part_size,
3566 output, output_buffer_size,
3567 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003568 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003569 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003570 PSA_ASSERT( psa_cipher_update( &operation,
3571 input->x + first_part_size,
3572 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003573 output + total_output_length,
3574 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003575 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003576 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003577 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003578 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003579 output + total_output_length,
3580 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003582 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003583 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003584
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003585 ASSERT_COMPARE( expected_output->x, expected_output->len,
3586 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003587
3588exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003589 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003590 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003591 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003592 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003593}
3594/* END_CASE */
3595
Gilles Peskine50e586b2018-06-08 14:28:46 +02003596/* BEGIN_CASE */
3597void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003598 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003599 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003600 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003601{
Ronald Cron5425a212020-08-04 14:58:35 +02003602 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003603 psa_status_t status;
3604 psa_key_type_t key_type = key_type_arg;
3605 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003606 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003607 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003608 size_t output_buffer_size = 0;
3609 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003610 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003611 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003613
Gilles Peskine8817f612018-12-18 00:18:46 +01003614 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003615
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003616 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3617 psa_set_key_algorithm( &attributes, alg );
3618 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003619
Ronald Cron5425a212020-08-04 14:58:35 +02003620 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3621 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003622
Ronald Cron5425a212020-08-04 14:58:35 +02003623 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003624
Steven Cooreman177deba2020-09-07 17:14:14 +02003625 if( iv->len > 0 )
3626 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003627 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003628 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003629
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003630 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003631 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003632 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003633
Gilles Peskine8817f612018-12-18 00:18:46 +01003634 PSA_ASSERT( psa_cipher_update( &operation,
3635 input->x, input->len,
3636 output, output_buffer_size,
3637 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003638 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003639 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003640 output + total_output_length,
3641 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003642 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003643 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003644 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003645
3646 if( expected_status == PSA_SUCCESS )
3647 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003648 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003649 ASSERT_COMPARE( expected_output->x, expected_output->len,
3650 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003651 }
3652
Gilles Peskine50e586b2018-06-08 14:28:46 +02003653exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003654 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003655 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003656 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003657 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003658}
3659/* END_CASE */
3660
Gilles Peskine50e586b2018-06-08 14:28:46 +02003661/* BEGIN_CASE */
3662void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003663 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003664 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003665{
Ronald Cron5425a212020-08-04 14:58:35 +02003666 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003667 psa_key_type_t key_type = key_type_arg;
3668 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003669 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003670 size_t iv_size = 16;
3671 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003672 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003673 size_t output1_size = 0;
3674 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003675 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003676 size_t output2_size = 0;
3677 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003678 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003679 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3680 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003682
Gilles Peskine8817f612018-12-18 00:18:46 +01003683 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003684
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003685 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3686 psa_set_key_algorithm( &attributes, alg );
3687 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003688
Ronald Cron5425a212020-08-04 14:58:35 +02003689 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3690 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003691
Ronald Cron5425a212020-08-04 14:58:35 +02003692 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3693 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003694
Steven Cooreman177deba2020-09-07 17:14:14 +02003695 if( alg != PSA_ALG_ECB_NO_PADDING )
3696 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003697 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3698 iv, iv_size,
3699 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003700 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003701 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003702 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003703 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003704
Gilles Peskine8817f612018-12-18 00:18:46 +01003705 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3706 output1, output1_size,
3707 &output1_length ) );
3708 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003709 output1 + output1_length,
3710 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003711 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003712
Gilles Peskine048b7f02018-06-08 14:20:49 +02003713 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003714
Gilles Peskine8817f612018-12-18 00:18:46 +01003715 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003716
3717 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003718 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003719
Steven Cooreman177deba2020-09-07 17:14:14 +02003720 if( iv_length > 0 )
3721 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003722 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3723 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003724 }
3725
Gilles Peskine8817f612018-12-18 00:18:46 +01003726 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3727 output2, output2_size,
3728 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003729 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003730 PSA_ASSERT( psa_cipher_finish( &operation2,
3731 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003732 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003733 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003734
Gilles Peskine048b7f02018-06-08 14:20:49 +02003735 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003736
Gilles Peskine8817f612018-12-18 00:18:46 +01003737 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003738
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003739 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003740
3741exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003742 psa_cipher_abort( &operation1 );
3743 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003744 mbedtls_free( output1 );
3745 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003746 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003747 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003748}
3749/* END_CASE */
3750
3751/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003752void cipher_verify_output_multipart( int alg_arg,
3753 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003754 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003755 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003756 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003757{
Ronald Cron5425a212020-08-04 14:58:35 +02003758 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003759 psa_key_type_t key_type = key_type_arg;
3760 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003761 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003762 unsigned char iv[16] = {0};
3763 size_t iv_size = 16;
3764 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003765 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003766 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003767 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003768 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003769 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003770 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003771 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003772 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3773 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003775
Gilles Peskine8817f612018-12-18 00:18:46 +01003776 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003777
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3779 psa_set_key_algorithm( &attributes, alg );
3780 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003781
Ronald Cron5425a212020-08-04 14:58:35 +02003782 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3783 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003784
Ronald Cron5425a212020-08-04 14:58:35 +02003785 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3786 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003787
Steven Cooreman177deba2020-09-07 17:14:14 +02003788 if( alg != PSA_ALG_ECB_NO_PADDING )
3789 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003790 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3791 iv, iv_size,
3792 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003793 }
3794
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003795 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003796 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003797 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003798
Gilles Peskinee0866522019-02-19 19:44:00 +01003799 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003800
Gilles Peskine8817f612018-12-18 00:18:46 +01003801 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3802 output1, output1_buffer_size,
3803 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003804 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003805
Gilles Peskine8817f612018-12-18 00:18:46 +01003806 PSA_ASSERT( psa_cipher_update( &operation1,
3807 input->x + first_part_size,
3808 input->len - first_part_size,
3809 output1, output1_buffer_size,
3810 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003811 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003812
Gilles Peskine8817f612018-12-18 00:18:46 +01003813 PSA_ASSERT( psa_cipher_finish( &operation1,
3814 output1 + output1_length,
3815 output1_buffer_size - output1_length,
3816 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003817 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003818
Gilles Peskine8817f612018-12-18 00:18:46 +01003819 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003820
Gilles Peskine048b7f02018-06-08 14:20:49 +02003821 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003822 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003823
Steven Cooreman177deba2020-09-07 17:14:14 +02003824 if( iv_length > 0 )
3825 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003826 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3827 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003828 }
Moran Pekerded84402018-06-06 16:36:50 +03003829
Gilles Peskine8817f612018-12-18 00:18:46 +01003830 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3831 output2, output2_buffer_size,
3832 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003833 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003834
Gilles Peskine8817f612018-12-18 00:18:46 +01003835 PSA_ASSERT( psa_cipher_update( &operation2,
3836 output1 + first_part_size,
3837 output1_length - first_part_size,
3838 output2, output2_buffer_size,
3839 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003840 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003841
Gilles Peskine8817f612018-12-18 00:18:46 +01003842 PSA_ASSERT( psa_cipher_finish( &operation2,
3843 output2 + output2_length,
3844 output2_buffer_size - output2_length,
3845 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003846 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003847
Gilles Peskine8817f612018-12-18 00:18:46 +01003848 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003849
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003850 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003851
3852exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003853 psa_cipher_abort( &operation1 );
3854 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003855 mbedtls_free( output1 );
3856 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003857 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003858 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003859}
3860/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003861
Gilles Peskine20035e32018-02-03 22:44:14 +01003862/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003863void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003864 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003865 data_t *nonce,
3866 data_t *additional_data,
3867 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003868 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003869{
Ronald Cron5425a212020-08-04 14:58:35 +02003870 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003871 psa_key_type_t key_type = key_type_arg;
3872 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003873 unsigned char *output_data = NULL;
3874 size_t output_size = 0;
3875 size_t output_length = 0;
3876 unsigned char *output_data2 = NULL;
3877 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003878 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003879 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003881
Gilles Peskine4abf7412018-06-18 16:35:34 +02003882 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003883 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3884 * should be exact. */
3885 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3886 TEST_EQUAL( output_size,
3887 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003888 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003889
Gilles Peskine8817f612018-12-18 00:18:46 +01003890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003891
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003892 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3893 psa_set_key_algorithm( &attributes, alg );
3894 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003895
Gilles Peskine049c7532019-05-15 20:22:09 +02003896 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003897 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003898
Ronald Cron5425a212020-08-04 14:58:35 +02003899 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003900 nonce->x, nonce->len,
3901 additional_data->x,
3902 additional_data->len,
3903 input_data->x, input_data->len,
3904 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003905 &output_length ),
3906 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003907
3908 if( PSA_SUCCESS == expected_result )
3909 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003910 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003911
Gilles Peskine003a4a92019-05-14 16:09:40 +02003912 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3913 * should be exact. */
3914 TEST_EQUAL( input_data->len,
3915 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3916
Ronald Cron5425a212020-08-04 14:58:35 +02003917 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003918 nonce->x, nonce->len,
3919 additional_data->x,
3920 additional_data->len,
3921 output_data, output_length,
3922 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003923 &output_length2 ),
3924 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003925
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003926 ASSERT_COMPARE( input_data->x, input_data->len,
3927 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003928 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003929
Gilles Peskinea1cac842018-06-11 19:33:02 +02003930exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003931 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003932 mbedtls_free( output_data );
3933 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003934 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003935}
3936/* END_CASE */
3937
3938/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003939void aead_encrypt( int key_type_arg, data_t *key_data,
3940 int alg_arg,
3941 data_t *nonce,
3942 data_t *additional_data,
3943 data_t *input_data,
3944 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003945{
Ronald Cron5425a212020-08-04 14:58:35 +02003946 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003947 psa_key_type_t key_type = key_type_arg;
3948 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003949 unsigned char *output_data = NULL;
3950 size_t output_size = 0;
3951 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003952 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003954 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003955
Gilles Peskine4abf7412018-06-18 16:35:34 +02003956 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003957 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3958 * should be exact. */
3959 TEST_EQUAL( output_size,
3960 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003961 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003962
Gilles Peskine8817f612018-12-18 00:18:46 +01003963 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003964
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003965 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3966 psa_set_key_algorithm( &attributes, alg );
3967 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003968
Gilles Peskine049c7532019-05-15 20:22:09 +02003969 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003970 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003971
Steven Cooremand588ea12021-01-11 19:36:04 +01003972 status = psa_aead_encrypt( key, alg,
3973 nonce->x, nonce->len,
3974 additional_data->x, additional_data->len,
3975 input_data->x, input_data->len,
3976 output_data, output_size,
3977 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003978
Ronald Cron28a45ed2021-02-09 20:35:42 +01003979 /* If the operation is not supported, just skip and not fail in case the
3980 * encryption involves a common limitation of cryptography hardwares and
3981 * an alternative implementation. */
3982 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003983 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003984 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3985 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003986 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003987
3988 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003989 ASSERT_COMPARE( expected_result->x, expected_result->len,
3990 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003991
Gilles Peskinea1cac842018-06-11 19:33:02 +02003992exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003993 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003994 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003995 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003996}
3997/* END_CASE */
3998
3999/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004000void aead_decrypt( int key_type_arg, data_t *key_data,
4001 int alg_arg,
4002 data_t *nonce,
4003 data_t *additional_data,
4004 data_t *input_data,
4005 data_t *expected_data,
4006 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004007{
Ronald Cron5425a212020-08-04 14:58:35 +02004008 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004009 psa_key_type_t key_type = key_type_arg;
4010 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004011 unsigned char *output_data = NULL;
4012 size_t output_size = 0;
4013 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004014 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004016 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004017 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004018
Gilles Peskine003a4a92019-05-14 16:09:40 +02004019 output_size = input_data->len - tag_length;
4020 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4021 * should be exact. */
4022 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4023 TEST_EQUAL( output_size,
4024 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004025 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004026
Gilles Peskine8817f612018-12-18 00:18:46 +01004027 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004028
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004029 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4030 psa_set_key_algorithm( &attributes, alg );
4031 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004032
Gilles Peskine049c7532019-05-15 20:22:09 +02004033 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004034 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004035
Steven Cooremand588ea12021-01-11 19:36:04 +01004036 status = psa_aead_decrypt( key, alg,
4037 nonce->x, nonce->len,
4038 additional_data->x,
4039 additional_data->len,
4040 input_data->x, input_data->len,
4041 output_data, output_size,
4042 &output_length );
4043
Ronald Cron28a45ed2021-02-09 20:35:42 +01004044 /* If the operation is not supported, just skip and not fail in case the
4045 * decryption involves a common limitation of cryptography hardwares and
4046 * an alternative implementation. */
4047 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004048 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004049 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4050 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004051 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004052
4053 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004054
Gilles Peskine2d277862018-06-18 15:41:12 +02004055 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004056 ASSERT_COMPARE( expected_data->x, expected_data->len,
4057 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004058
Gilles Peskinea1cac842018-06-11 19:33:02 +02004059exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004060 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004061 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004062 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004063}
4064/* END_CASE */
4065
4066/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004067void signature_size( int type_arg,
4068 int bits,
4069 int alg_arg,
4070 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004071{
4072 psa_key_type_t type = type_arg;
4073 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004074 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004075
Gilles Peskinefe11b722018-12-18 00:24:04 +01004076 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004077#if defined(MBEDTLS_TEST_DEPRECATED)
4078 TEST_EQUAL( actual_size,
4079 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4080#endif /* MBEDTLS_TEST_DEPRECATED */
4081
Gilles Peskinee59236f2018-01-27 23:32:46 +01004082exit:
4083 ;
4084}
4085/* END_CASE */
4086
4087/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004088void sign_deterministic( int key_type_arg, data_t *key_data,
4089 int alg_arg, data_t *input_data,
4090 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004091{
Ronald Cron5425a212020-08-04 14:58:35 +02004092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004093 psa_key_type_t key_type = key_type_arg;
4094 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004095 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004096 unsigned char *signature = NULL;
4097 size_t signature_size;
4098 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004099 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004100
Gilles Peskine8817f612018-12-18 00:18:46 +01004101 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004102
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004103 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004104 psa_set_key_algorithm( &attributes, alg );
4105 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004106
Gilles Peskine049c7532019-05-15 20:22:09 +02004107 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004108 &key ) );
4109 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004110 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004111
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004112 /* Allocate a buffer which has the size advertized by the
4113 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004114 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004115 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004116 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004117 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004118 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004119
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004120 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004121 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004122 input_data->x, input_data->len,
4123 signature, signature_size,
4124 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004125 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004126 ASSERT_COMPARE( output_data->x, output_data->len,
4127 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004128
Gilles Peskine0627f982019-11-26 19:12:16 +01004129#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004130 memset( signature, 0, signature_size );
4131 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004132 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004133 input_data->x, input_data->len,
4134 signature, signature_size,
4135 &signature_length ) );
4136 ASSERT_COMPARE( output_data->x, output_data->len,
4137 signature, signature_length );
4138#endif /* MBEDTLS_TEST_DEPRECATED */
4139
Gilles Peskine20035e32018-02-03 22:44:14 +01004140exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004141 /*
4142 * Key attributes may have been returned by psa_get_key_attributes()
4143 * thus reset them as required.
4144 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004145 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004146
Ronald Cron5425a212020-08-04 14:58:35 +02004147 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004148 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004149 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004150}
4151/* END_CASE */
4152
4153/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004154void sign_fail( int key_type_arg, data_t *key_data,
4155 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004156 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004157{
Ronald Cron5425a212020-08-04 14:58:35 +02004158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004159 psa_key_type_t key_type = key_type_arg;
4160 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004161 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004162 psa_status_t actual_status;
4163 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004164 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004165 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004167
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004168 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004169
Gilles Peskine8817f612018-12-18 00:18:46 +01004170 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004171
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004172 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004173 psa_set_key_algorithm( &attributes, alg );
4174 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004175
Gilles Peskine049c7532019-05-15 20:22:09 +02004176 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004177 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004178
Ronald Cron5425a212020-08-04 14:58:35 +02004179 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004180 input_data->x, input_data->len,
4181 signature, signature_size,
4182 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004183 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004184 /* The value of *signature_length is unspecified on error, but
4185 * whatever it is, it should be less than signature_size, so that
4186 * if the caller tries to read *signature_length bytes without
4187 * checking the error code then they don't overflow a buffer. */
4188 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004189
Gilles Peskine895242b2019-11-29 12:15:40 +01004190#if defined(MBEDTLS_TEST_DEPRECATED)
4191 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004192 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004193 input_data->x, input_data->len,
4194 signature, signature_size,
4195 &signature_length ),
4196 expected_status );
4197 TEST_ASSERT( signature_length <= signature_size );
4198#endif /* MBEDTLS_TEST_DEPRECATED */
4199
Gilles Peskine20035e32018-02-03 22:44:14 +01004200exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004201 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004202 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004203 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004204 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004205}
4206/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004207
4208/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004209void sign_verify( int key_type_arg, data_t *key_data,
4210 int alg_arg, data_t *input_data )
4211{
Ronald Cron5425a212020-08-04 14:58:35 +02004212 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004213 psa_key_type_t key_type = key_type_arg;
4214 psa_algorithm_t alg = alg_arg;
4215 size_t key_bits;
4216 unsigned char *signature = NULL;
4217 size_t signature_size;
4218 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004220
Gilles Peskine8817f612018-12-18 00:18:46 +01004221 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004222
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004223 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004224 psa_set_key_algorithm( &attributes, alg );
4225 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004226
Gilles Peskine049c7532019-05-15 20:22:09 +02004227 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004228 &key ) );
4229 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004230 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004231
4232 /* Allocate a buffer which has the size advertized by the
4233 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004234 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004235 key_bits, alg );
4236 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004237 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004238 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004239
4240 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004241 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004242 input_data->x, input_data->len,
4243 signature, signature_size,
4244 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004245 /* Check that the signature length looks sensible. */
4246 TEST_ASSERT( signature_length <= signature_size );
4247 TEST_ASSERT( signature_length > 0 );
4248
4249 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004250 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004251 input_data->x, input_data->len,
4252 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004253
4254 if( input_data->len != 0 )
4255 {
4256 /* Flip a bit in the input and verify that the signature is now
4257 * detected as invalid. Flip a bit at the beginning, not at the end,
4258 * because ECDSA may ignore the last few bits of the input. */
4259 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004260 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004261 input_data->x, input_data->len,
4262 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004263 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004264 }
4265
4266exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004267 /*
4268 * Key attributes may have been returned by psa_get_key_attributes()
4269 * thus reset them as required.
4270 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004271 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004272
Ronald Cron5425a212020-08-04 14:58:35 +02004273 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004274 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004275 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004276}
4277/* END_CASE */
4278
4279/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004280void asymmetric_verify( int key_type_arg, data_t *key_data,
4281 int alg_arg, data_t *hash_data,
4282 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004283{
Ronald Cron5425a212020-08-04 14:58:35 +02004284 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004285 psa_key_type_t key_type = key_type_arg;
4286 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004287 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004288
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004289 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004290
Gilles Peskine8817f612018-12-18 00:18:46 +01004291 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004292
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004293 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004294 psa_set_key_algorithm( &attributes, alg );
4295 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004296
Gilles Peskine049c7532019-05-15 20:22:09 +02004297 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004298 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004299
Ronald Cron5425a212020-08-04 14:58:35 +02004300 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004301 hash_data->x, hash_data->len,
4302 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004303
4304#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004305 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004306 hash_data->x, hash_data->len,
4307 signature_data->x,
4308 signature_data->len ) );
4309
4310#endif /* MBEDTLS_TEST_DEPRECATED */
4311
itayzafrir5c753392018-05-08 11:18:38 +03004312exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004313 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004314 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004315 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004316}
4317/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004318
4319/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004320void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4321 int alg_arg, data_t *hash_data,
4322 data_t *signature_data,
4323 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004324{
Ronald Cron5425a212020-08-04 14:58:35 +02004325 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004326 psa_key_type_t key_type = key_type_arg;
4327 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004328 psa_status_t actual_status;
4329 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004330 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004331
Gilles Peskine8817f612018-12-18 00:18:46 +01004332 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004333
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004334 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004335 psa_set_key_algorithm( &attributes, alg );
4336 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004337
Gilles Peskine049c7532019-05-15 20:22:09 +02004338 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004339 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340
Ronald Cron5425a212020-08-04 14:58:35 +02004341 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004342 hash_data->x, hash_data->len,
4343 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004344 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004345
Gilles Peskine895242b2019-11-29 12:15:40 +01004346#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004347 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004348 hash_data->x, hash_data->len,
4349 signature_data->x, signature_data->len ),
4350 expected_status );
4351#endif /* MBEDTLS_TEST_DEPRECATED */
4352
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004353exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004354 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004355 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004356 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004357}
4358/* END_CASE */
4359
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004360/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004361void asymmetric_encrypt( int key_type_arg,
4362 data_t *key_data,
4363 int alg_arg,
4364 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004365 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004366 int expected_output_length_arg,
4367 int expected_status_arg )
4368{
Ronald Cron5425a212020-08-04 14:58:35 +02004369 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004370 psa_key_type_t key_type = key_type_arg;
4371 psa_algorithm_t alg = alg_arg;
4372 size_t expected_output_length = expected_output_length_arg;
4373 size_t key_bits;
4374 unsigned char *output = NULL;
4375 size_t output_size;
4376 size_t output_length = ~0;
4377 psa_status_t actual_status;
4378 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004379 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004380
Gilles Peskine8817f612018-12-18 00:18:46 +01004381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004382
Gilles Peskine656896e2018-06-29 19:12:28 +02004383 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004384 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4385 psa_set_key_algorithm( &attributes, alg );
4386 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004387 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004388 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004389
4390 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004391 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004392 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004393 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004394 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004395
4396 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004397 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004398 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004399 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004400 output, output_size,
4401 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004402 TEST_EQUAL( actual_status, expected_status );
4403 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004404
Gilles Peskine68428122018-06-30 18:42:41 +02004405 /* If the label is empty, the test framework puts a non-null pointer
4406 * in label->x. Test that a null pointer works as well. */
4407 if( label->len == 0 )
4408 {
4409 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004410 if( output_size != 0 )
4411 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004412 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004413 input_data->x, input_data->len,
4414 NULL, label->len,
4415 output, output_size,
4416 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004417 TEST_EQUAL( actual_status, expected_status );
4418 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004419 }
4420
Gilles Peskine656896e2018-06-29 19:12:28 +02004421exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004422 /*
4423 * Key attributes may have been returned by psa_get_key_attributes()
4424 * thus reset them as required.
4425 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004426 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004427
Ronald Cron5425a212020-08-04 14:58:35 +02004428 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004429 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004430 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004431}
4432/* END_CASE */
4433
4434/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004435void asymmetric_encrypt_decrypt( int key_type_arg,
4436 data_t *key_data,
4437 int alg_arg,
4438 data_t *input_data,
4439 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004440{
Ronald Cron5425a212020-08-04 14:58:35 +02004441 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004442 psa_key_type_t key_type = key_type_arg;
4443 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004444 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004445 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004446 size_t output_size;
4447 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004448 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004449 size_t output2_size;
4450 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004452
Gilles Peskine8817f612018-12-18 00:18:46 +01004453 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004454
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004455 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4456 psa_set_key_algorithm( &attributes, alg );
4457 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004458
Gilles Peskine049c7532019-05-15 20:22:09 +02004459 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004460 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004461
4462 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004463 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004464 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004465 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004466 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004467 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004468 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004469
Gilles Peskineeebd7382018-06-08 18:11:54 +02004470 /* We test encryption by checking that encrypt-then-decrypt gives back
4471 * the original plaintext because of the non-optional random
4472 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004473 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004474 input_data->x, input_data->len,
4475 label->x, label->len,
4476 output, output_size,
4477 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004478 /* We don't know what ciphertext length to expect, but check that
4479 * it looks sensible. */
4480 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004481
Ronald Cron5425a212020-08-04 14:58:35 +02004482 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004483 output, output_length,
4484 label->x, label->len,
4485 output2, output2_size,
4486 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004487 ASSERT_COMPARE( input_data->x, input_data->len,
4488 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004489
4490exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004491 /*
4492 * Key attributes may have been returned by psa_get_key_attributes()
4493 * thus reset them as required.
4494 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004495 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004496
Ronald Cron5425a212020-08-04 14:58:35 +02004497 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004498 mbedtls_free( output );
4499 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004500 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004501}
4502/* END_CASE */
4503
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004504/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004505void asymmetric_decrypt( int key_type_arg,
4506 data_t *key_data,
4507 int alg_arg,
4508 data_t *input_data,
4509 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004510 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004511{
Ronald Cron5425a212020-08-04 14:58:35 +02004512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004513 psa_key_type_t key_type = key_type_arg;
4514 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004515 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004516 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004517 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004518 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004519
Jaeden Amero412654a2019-02-06 12:57:46 +00004520 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004521 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004522
Gilles Peskine8817f612018-12-18 00:18:46 +01004523 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004524
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004525 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4526 psa_set_key_algorithm( &attributes, alg );
4527 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004528
Gilles Peskine049c7532019-05-15 20:22:09 +02004529 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004530 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004531
Ronald Cron5425a212020-08-04 14:58:35 +02004532 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004533 input_data->x, input_data->len,
4534 label->x, label->len,
4535 output,
4536 output_size,
4537 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004538 ASSERT_COMPARE( expected_data->x, expected_data->len,
4539 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004540
Gilles Peskine68428122018-06-30 18:42:41 +02004541 /* If the label is empty, the test framework puts a non-null pointer
4542 * in label->x. Test that a null pointer works as well. */
4543 if( label->len == 0 )
4544 {
4545 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004546 if( output_size != 0 )
4547 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004548 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004549 input_data->x, input_data->len,
4550 NULL, label->len,
4551 output,
4552 output_size,
4553 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004554 ASSERT_COMPARE( expected_data->x, expected_data->len,
4555 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004556 }
4557
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004558exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004559 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004560 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004561 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004562 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004563}
4564/* END_CASE */
4565
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004566/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004567void asymmetric_decrypt_fail( int key_type_arg,
4568 data_t *key_data,
4569 int alg_arg,
4570 data_t *input_data,
4571 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004572 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004573 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004574{
Ronald Cron5425a212020-08-04 14:58:35 +02004575 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004576 psa_key_type_t key_type = key_type_arg;
4577 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004578 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004579 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004580 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004581 psa_status_t actual_status;
4582 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004584
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004585 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004586
Gilles Peskine8817f612018-12-18 00:18:46 +01004587 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004588
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004589 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4590 psa_set_key_algorithm( &attributes, alg );
4591 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004592
Gilles Peskine049c7532019-05-15 20:22:09 +02004593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004594 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004595
Ronald Cron5425a212020-08-04 14:58:35 +02004596 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004597 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004598 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004599 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004600 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004601 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004602 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004603
Gilles Peskine68428122018-06-30 18:42:41 +02004604 /* If the label is empty, the test framework puts a non-null pointer
4605 * in label->x. Test that a null pointer works as well. */
4606 if( label->len == 0 )
4607 {
4608 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004609 if( output_size != 0 )
4610 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004611 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004612 input_data->x, input_data->len,
4613 NULL, label->len,
4614 output, output_size,
4615 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004616 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004617 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004618 }
4619
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004620exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004621 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004622 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004623 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004624 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004625}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004626/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004627
4628/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004629void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004630{
4631 /* Test each valid way of initializing the object, except for `= {0}`, as
4632 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4633 * though it's OK by the C standard. We could test for this, but we'd need
4634 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004635 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004636 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4637 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4638 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004639
4640 memset( &zero, 0, sizeof( zero ) );
4641
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004642 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004643 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &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( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004646 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004647 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004648 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004649
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004650 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004651 PSA_ASSERT( psa_key_derivation_abort(&func) );
4652 PSA_ASSERT( psa_key_derivation_abort(&init) );
4653 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004654}
4655/* END_CASE */
4656
Janos Follath16de4a42019-06-13 16:32:24 +01004657/* BEGIN_CASE */
4658void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004659{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004660 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004661 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004663
Gilles Peskine8817f612018-12-18 00:18:46 +01004664 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004665
Janos Follath16de4a42019-06-13 16:32:24 +01004666 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004667 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004668
4669exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004671 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004672}
4673/* END_CASE */
4674
Janos Follathaf3c2a02019-06-12 12:34:34 +01004675/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004676void derive_set_capacity( int alg_arg, int capacity_arg,
4677 int expected_status_arg )
4678{
4679 psa_algorithm_t alg = alg_arg;
4680 size_t capacity = capacity_arg;
4681 psa_status_t expected_status = expected_status_arg;
4682 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4683
4684 PSA_ASSERT( psa_crypto_init( ) );
4685
4686 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4687
4688 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4689 expected_status );
4690
4691exit:
4692 psa_key_derivation_abort( &operation );
4693 PSA_DONE( );
4694}
4695/* END_CASE */
4696
4697/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004698void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004699 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004700 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004701 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004702 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004703 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004704 int expected_status_arg3,
4705 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004706{
4707 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004708 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4709 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004710 psa_status_t expected_statuses[] = {expected_status_arg1,
4711 expected_status_arg2,
4712 expected_status_arg3};
4713 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004714 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4715 MBEDTLS_SVC_KEY_ID_INIT,
4716 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004717 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4719 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004720 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004721 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004722 psa_status_t expected_output_status = expected_output_status_arg;
4723 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004724
4725 PSA_ASSERT( psa_crypto_init( ) );
4726
4727 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4728 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004729
4730 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4731
4732 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4733 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004734 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004735 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004736 psa_set_key_type( &attributes, key_types[i] );
4737 PSA_ASSERT( psa_import_key( &attributes,
4738 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004739 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004740 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4741 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4742 {
4743 // When taking a private key as secret input, use key agreement
4744 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004745 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4746 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004747 expected_statuses[i] );
4748 }
4749 else
4750 {
4751 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004752 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004753 expected_statuses[i] );
4754 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004755 }
4756 else
4757 {
4758 TEST_EQUAL( psa_key_derivation_input_bytes(
4759 &operation, steps[i],
4760 inputs[i]->x, inputs[i]->len ),
4761 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004762 }
4763 }
4764
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004765 if( output_key_type != PSA_KEY_TYPE_NONE )
4766 {
4767 psa_reset_key_attributes( &attributes );
4768 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4769 psa_set_key_bits( &attributes, 8 );
4770 actual_output_status =
4771 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004772 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004773 }
4774 else
4775 {
4776 uint8_t buffer[1];
4777 actual_output_status =
4778 psa_key_derivation_output_bytes( &operation,
4779 buffer, sizeof( buffer ) );
4780 }
4781 TEST_EQUAL( actual_output_status, expected_output_status );
4782
Janos Follathaf3c2a02019-06-12 12:34:34 +01004783exit:
4784 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004785 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4786 psa_destroy_key( keys[i] );
4787 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004788 PSA_DONE( );
4789}
4790/* END_CASE */
4791
Janos Follathd958bb72019-07-03 15:02:16 +01004792/* BEGIN_CASE */
4793void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004794{
Janos Follathd958bb72019-07-03 15:02:16 +01004795 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004796 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004797 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004798 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004799 unsigned char input1[] = "Input 1";
4800 size_t input1_length = sizeof( input1 );
4801 unsigned char input2[] = "Input 2";
4802 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004803 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004804 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004805 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4806 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4807 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004808 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004809
Gilles Peskine8817f612018-12-18 00:18:46 +01004810 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004811
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004812 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4813 psa_set_key_algorithm( &attributes, alg );
4814 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004815
Gilles Peskine73676cb2019-05-15 20:15:10 +02004816 PSA_ASSERT( psa_import_key( &attributes,
4817 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004818 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004819
4820 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004821 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4822 input1, input1_length,
4823 input2, input2_length,
4824 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004825 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004826
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004828 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004829 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004830
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004832
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004834 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004835
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004836exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004838 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004839 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004840}
4841/* END_CASE */
4842
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004843/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004844void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004845{
4846 uint8_t output_buffer[16];
4847 size_t buffer_size = 16;
4848 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004850
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004851 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4852 output_buffer, buffer_size )
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 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004856 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004857
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004859
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004860 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4861 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004862 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004863
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004865 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004866
4867exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004868 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004869}
4870/* END_CASE */
4871
4872/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004873void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004874 int step1_arg, data_t *input1,
4875 int step2_arg, data_t *input2,
4876 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004877 int requested_capacity_arg,
4878 data_t *expected_output1,
4879 data_t *expected_output2 )
4880{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004881 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004882 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4883 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004884 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4885 MBEDTLS_SVC_KEY_ID_INIT,
4886 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004887 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004889 uint8_t *expected_outputs[2] =
4890 {expected_output1->x, expected_output2->x};
4891 size_t output_sizes[2] =
4892 {expected_output1->len, expected_output2->len};
4893 size_t output_buffer_size = 0;
4894 uint8_t *output_buffer = NULL;
4895 size_t expected_capacity;
4896 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004898 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004899 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004900
4901 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4902 {
4903 if( output_sizes[i] > output_buffer_size )
4904 output_buffer_size = output_sizes[i];
4905 if( output_sizes[i] == 0 )
4906 expected_outputs[i] = NULL;
4907 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004908 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004909 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004910
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004911 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4912 psa_set_key_algorithm( &attributes, alg );
4913 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004914
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004915 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004916 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4917 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4918 requested_capacity ) );
4919 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004920 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004921 switch( steps[i] )
4922 {
4923 case 0:
4924 break;
4925 case PSA_KEY_DERIVATION_INPUT_SECRET:
4926 PSA_ASSERT( psa_import_key( &attributes,
4927 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004928 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004929 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004930 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004931 break;
4932 default:
4933 PSA_ASSERT( psa_key_derivation_input_bytes(
4934 &operation, steps[i],
4935 inputs[i]->x, inputs[i]->len ) );
4936 break;
4937 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004938 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004939
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004940 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004941 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004942 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004943 expected_capacity = requested_capacity;
4944
4945 /* Expansion phase. */
4946 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4947 {
4948 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004950 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004951 if( expected_capacity == 0 && output_sizes[i] == 0 )
4952 {
4953 /* Reading 0 bytes when 0 bytes are available can go either way. */
4954 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004955 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004956 continue;
4957 }
4958 else if( expected_capacity == 0 ||
4959 output_sizes[i] > expected_capacity )
4960 {
4961 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004962 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004963 expected_capacity = 0;
4964 continue;
4965 }
4966 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004967 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004968 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004969 ASSERT_COMPARE( output_buffer, output_sizes[i],
4970 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004972 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004974 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004975 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004976 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004978
4979exit:
4980 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004982 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4983 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004984 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004985}
4986/* END_CASE */
4987
4988/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004989void derive_full( int alg_arg,
4990 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004991 data_t *input1,
4992 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004993 int requested_capacity_arg )
4994{
Ronald Cron5425a212020-08-04 14:58:35 +02004995 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004996 psa_algorithm_t alg = alg_arg;
4997 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004998 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004999 unsigned char output_buffer[16];
5000 size_t expected_capacity = requested_capacity;
5001 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005003
Gilles Peskine8817f612018-12-18 00:18:46 +01005004 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005005
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005006 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5007 psa_set_key_algorithm( &attributes, alg );
5008 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005009
Gilles Peskine049c7532019-05-15 20:22:09 +02005010 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005011 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005012
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005013 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5014 input1->x, input1->len,
5015 input2->x, input2->len,
5016 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005017 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005018
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005019 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005020 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005021 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005022
5023 /* Expansion phase. */
5024 while( current_capacity > 0 )
5025 {
5026 size_t read_size = sizeof( output_buffer );
5027 if( read_size > current_capacity )
5028 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005029 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005030 output_buffer,
5031 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005032 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005033 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005034 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005035 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005036 }
5037
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005038 /* Check that the operation refuses to go over capacity. */
5039 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005040 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005041
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005042 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005043
5044exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005046 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005047 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005048}
5049/* END_CASE */
5050
Janos Follathe60c9052019-07-03 13:51:30 +01005051/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005052void derive_key_exercise( int alg_arg,
5053 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005054 data_t *input1,
5055 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005056 int derived_type_arg,
5057 int derived_bits_arg,
5058 int derived_usage_arg,
5059 int derived_alg_arg )
5060{
Ronald Cron5425a212020-08-04 14:58:35 +02005061 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5062 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005063 psa_algorithm_t alg = alg_arg;
5064 psa_key_type_t derived_type = derived_type_arg;
5065 size_t derived_bits = derived_bits_arg;
5066 psa_key_usage_t derived_usage = derived_usage_arg;
5067 psa_algorithm_t derived_alg = derived_alg_arg;
5068 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005069 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005071 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005072
Gilles Peskine8817f612018-12-18 00:18:46 +01005073 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005074
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005075 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5076 psa_set_key_algorithm( &attributes, alg );
5077 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005078 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005079 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005080
5081 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005082 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5083 input1->x, input1->len,
5084 input2->x, input2->len,
5085 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005086 goto exit;
5087
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005088 psa_set_key_usage_flags( &attributes, derived_usage );
5089 psa_set_key_algorithm( &attributes, derived_alg );
5090 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005091 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005092 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005093 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005094
5095 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005096 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005097 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5098 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005099
5100 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005101 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005102 goto exit;
5103
5104exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005105 /*
5106 * Key attributes may have been returned by psa_get_key_attributes()
5107 * thus reset them as required.
5108 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005109 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005110
5111 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005112 psa_destroy_key( base_key );
5113 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005114 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005115}
5116/* END_CASE */
5117
Janos Follath42fd8882019-07-03 14:17:09 +01005118/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005119void derive_key_export( int alg_arg,
5120 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005121 data_t *input1,
5122 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005123 int bytes1_arg,
5124 int bytes2_arg )
5125{
Ronald Cron5425a212020-08-04 14:58:35 +02005126 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5127 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005128 psa_algorithm_t alg = alg_arg;
5129 size_t bytes1 = bytes1_arg;
5130 size_t bytes2 = bytes2_arg;
5131 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005133 uint8_t *output_buffer = NULL;
5134 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005135 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5136 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005137 size_t length;
5138
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005139 ASSERT_ALLOC( output_buffer, capacity );
5140 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005141 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005142
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005143 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5144 psa_set_key_algorithm( &base_attributes, alg );
5145 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005146 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005147 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005148
5149 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005150 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5151 input1->x, input1->len,
5152 input2->x, input2->len,
5153 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005154 goto exit;
5155
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005156 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005157 output_buffer,
5158 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005159 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005160
5161 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005162 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5163 input1->x, input1->len,
5164 input2->x, input2->len,
5165 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005166 goto exit;
5167
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005168 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5169 psa_set_key_algorithm( &derived_attributes, 0 );
5170 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005171 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005172 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005173 &derived_key ) );
5174 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005175 export_buffer, bytes1,
5176 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005177 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005178 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005179 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005180 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005181 &derived_key ) );
5182 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005183 export_buffer + bytes1, bytes2,
5184 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005185 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005186
5187 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005188 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5189 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005190
5191exit:
5192 mbedtls_free( output_buffer );
5193 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005194 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005195 psa_destroy_key( base_key );
5196 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005197 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005198}
5199/* END_CASE */
5200
5201/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005202void derive_key( int alg_arg,
5203 data_t *key_data, data_t *input1, data_t *input2,
5204 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005205 int expected_status_arg,
5206 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005207{
Ronald Cron5425a212020-08-04 14:58:35 +02005208 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5209 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005210 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005211 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005212 size_t bits = bits_arg;
5213 psa_status_t expected_status = expected_status_arg;
5214 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5215 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5216 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5217
5218 PSA_ASSERT( psa_crypto_init( ) );
5219
5220 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5221 psa_set_key_algorithm( &base_attributes, alg );
5222 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5223 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005224 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005225
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005226 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5227 input1->x, input1->len,
5228 input2->x, input2->len,
5229 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005230 goto exit;
5231
5232 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5233 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005234 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005235 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005236
5237 psa_status_t status =
5238 psa_key_derivation_output_key( &derived_attributes,
5239 &operation,
5240 &derived_key );
5241 if( is_large_output > 0 )
5242 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5243 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005244
5245exit:
5246 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005247 psa_destroy_key( base_key );
5248 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005249 PSA_DONE( );
5250}
5251/* END_CASE */
5252
5253/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005254void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005255 int our_key_type_arg, int our_key_alg_arg,
5256 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005257 int expected_status_arg )
5258{
Ronald Cron5425a212020-08-04 14:58:35 +02005259 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005260 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005261 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005262 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005265 psa_status_t expected_status = expected_status_arg;
5266 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005267
Gilles Peskine8817f612018-12-18 00:18:46 +01005268 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005269
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005270 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005271 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005272 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005273 PSA_ASSERT( psa_import_key( &attributes,
5274 our_key_data->x, our_key_data->len,
5275 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005276
Gilles Peskine77f40d82019-04-11 21:27:06 +02005277 /* The tests currently include inputs that should fail at either step.
5278 * Test cases that fail at the setup step should be changed to call
5279 * key_derivation_setup instead, and this function should be renamed
5280 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005281 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005282 if( status == PSA_SUCCESS )
5283 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005284 TEST_EQUAL( psa_key_derivation_key_agreement(
5285 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5286 our_key,
5287 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005288 expected_status );
5289 }
5290 else
5291 {
5292 TEST_ASSERT( status == expected_status );
5293 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005294
5295exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005296 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005297 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005298 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005299}
5300/* END_CASE */
5301
5302/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005303void raw_key_agreement( int alg_arg,
5304 int our_key_type_arg, data_t *our_key_data,
5305 data_t *peer_key_data,
5306 data_t *expected_output )
5307{
Ronald Cron5425a212020-08-04 14:58:35 +02005308 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005309 psa_algorithm_t alg = alg_arg;
5310 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005311 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005312 unsigned char *output = NULL;
5313 size_t output_length = ~0;
5314
5315 ASSERT_ALLOC( output, expected_output->len );
5316 PSA_ASSERT( psa_crypto_init( ) );
5317
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005318 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5319 psa_set_key_algorithm( &attributes, alg );
5320 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005321 PSA_ASSERT( psa_import_key( &attributes,
5322 our_key_data->x, our_key_data->len,
5323 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005324
Gilles Peskinebe697d82019-05-16 18:00:41 +02005325 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5326 peer_key_data->x, peer_key_data->len,
5327 output, expected_output->len,
5328 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005329 ASSERT_COMPARE( output, output_length,
5330 expected_output->x, expected_output->len );
5331
5332exit:
5333 mbedtls_free( output );
5334 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005335 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005336}
5337/* END_CASE */
5338
5339/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005340void key_agreement_capacity( int alg_arg,
5341 int our_key_type_arg, data_t *our_key_data,
5342 data_t *peer_key_data,
5343 int expected_capacity_arg )
5344{
Ronald Cron5425a212020-08-04 14:58:35 +02005345 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005346 psa_algorithm_t alg = alg_arg;
5347 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005350 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005351 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005352
Gilles Peskine8817f612018-12-18 00:18:46 +01005353 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005354
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005355 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5356 psa_set_key_algorithm( &attributes, alg );
5357 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005358 PSA_ASSERT( psa_import_key( &attributes,
5359 our_key_data->x, our_key_data->len,
5360 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005361
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005362 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005363 PSA_ASSERT( psa_key_derivation_key_agreement(
5364 &operation,
5365 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5366 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005367 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5368 {
5369 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005370 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005371 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005372 NULL, 0 ) );
5373 }
Gilles Peskine59685592018-09-18 12:11:34 +02005374
Gilles Peskinebf491972018-10-25 22:36:12 +02005375 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005376 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005377 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005378 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005379
Gilles Peskinebf491972018-10-25 22:36:12 +02005380 /* Test the actual capacity by reading the output. */
5381 while( actual_capacity > sizeof( output ) )
5382 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005383 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005384 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005385 actual_capacity -= sizeof( output );
5386 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005387 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005388 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005389 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005390 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005391
Gilles Peskine59685592018-09-18 12:11:34 +02005392exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005394 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005395 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005396}
5397/* END_CASE */
5398
5399/* BEGIN_CASE */
5400void key_agreement_output( int alg_arg,
5401 int our_key_type_arg, data_t *our_key_data,
5402 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005403 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005404{
Ronald Cron5425a212020-08-04 14:58:35 +02005405 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005406 psa_algorithm_t alg = alg_arg;
5407 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005408 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005410 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005411
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005412 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5413 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005414
Gilles Peskine8817f612018-12-18 00:18:46 +01005415 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005416
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005417 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5418 psa_set_key_algorithm( &attributes, alg );
5419 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005420 PSA_ASSERT( psa_import_key( &attributes,
5421 our_key_data->x, our_key_data->len,
5422 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005423
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005424 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005425 PSA_ASSERT( psa_key_derivation_key_agreement(
5426 &operation,
5427 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5428 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005429 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5430 {
5431 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005432 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005433 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005434 NULL, 0 ) );
5435 }
Gilles Peskine59685592018-09-18 12:11:34 +02005436
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_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005440 ASSERT_COMPARE( actual_output, expected_output1->len,
5441 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005442 if( expected_output2->len != 0 )
5443 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005444 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005445 actual_output,
5446 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005447 ASSERT_COMPARE( actual_output, expected_output2->len,
5448 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005449 }
Gilles Peskine59685592018-09-18 12:11:34 +02005450
5451exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005453 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005454 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005455 mbedtls_free( actual_output );
5456}
5457/* END_CASE */
5458
5459/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005460void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005461{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005462 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005463 unsigned char *output = NULL;
5464 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005465 size_t i;
5466 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005467
Simon Butcher49f8e312020-03-03 15:51:50 +00005468 TEST_ASSERT( bytes_arg >= 0 );
5469
Gilles Peskine91892022021-02-08 19:50:26 +01005470 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005471 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005472
Gilles Peskine8817f612018-12-18 00:18:46 +01005473 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005474
Gilles Peskinea50d7392018-06-21 10:22:13 +02005475 /* Run several times, to ensure that every output byte will be
5476 * nonzero at least once with overwhelming probability
5477 * (2^(-8*number_of_runs)). */
5478 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005479 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005480 if( bytes != 0 )
5481 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005482 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005483
Gilles Peskinea50d7392018-06-21 10:22:13 +02005484 for( i = 0; i < bytes; i++ )
5485 {
5486 if( output[i] != 0 )
5487 ++changed[i];
5488 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005489 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005490
5491 /* Check that every byte was changed to nonzero at least once. This
5492 * validates that psa_generate_random is overwriting every byte of
5493 * the output buffer. */
5494 for( i = 0; i < bytes; i++ )
5495 {
5496 TEST_ASSERT( changed[i] != 0 );
5497 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005498
5499exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005500 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005501 mbedtls_free( output );
5502 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005503}
5504/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005505
5506/* BEGIN_CASE */
5507void generate_key( int type_arg,
5508 int bits_arg,
5509 int usage_arg,
5510 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005511 int expected_status_arg,
5512 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005513{
Ronald Cron5425a212020-08-04 14:58:35 +02005514 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005515 psa_key_type_t type = type_arg;
5516 psa_key_usage_t usage = usage_arg;
5517 size_t bits = bits_arg;
5518 psa_algorithm_t alg = alg_arg;
5519 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005521 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005522
Gilles Peskine8817f612018-12-18 00:18:46 +01005523 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005524
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005525 psa_set_key_usage_flags( &attributes, usage );
5526 psa_set_key_algorithm( &attributes, alg );
5527 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005528 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005529
5530 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005531 psa_status_t status = psa_generate_key( &attributes, &key );
5532
5533 if( is_large_key > 0 )
5534 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5535 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005536 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005537 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005538
5539 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005540 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005541 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5542 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005543
Gilles Peskine818ca122018-06-20 18:16:48 +02005544 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005545 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005546 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005547
5548exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005549 /*
5550 * Key attributes may have been returned by psa_get_key_attributes()
5551 * thus reset them as required.
5552 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005553 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005554
Ronald Cron5425a212020-08-04 14:58:35 +02005555 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005556 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005557}
5558/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005559
Gilles Peskinee56e8782019-04-26 17:34:02 +02005560/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5561void generate_key_rsa( int bits_arg,
5562 data_t *e_arg,
5563 int expected_status_arg )
5564{
Ronald Cron5425a212020-08-04 14:58:35 +02005565 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005566 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005567 size_t bits = bits_arg;
5568 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5569 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5570 psa_status_t expected_status = expected_status_arg;
5571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5572 uint8_t *exported = NULL;
5573 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005574 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005575 size_t exported_length = SIZE_MAX;
5576 uint8_t *e_read_buffer = NULL;
5577 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005578 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005579 size_t e_read_length = SIZE_MAX;
5580
5581 if( e_arg->len == 0 ||
5582 ( e_arg->len == 3 &&
5583 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5584 {
5585 is_default_public_exponent = 1;
5586 e_read_size = 0;
5587 }
5588 ASSERT_ALLOC( e_read_buffer, e_read_size );
5589 ASSERT_ALLOC( exported, exported_size );
5590
5591 PSA_ASSERT( psa_crypto_init( ) );
5592
5593 psa_set_key_usage_flags( &attributes, usage );
5594 psa_set_key_algorithm( &attributes, alg );
5595 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5596 e_arg->x, e_arg->len ) );
5597 psa_set_key_bits( &attributes, bits );
5598
5599 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005600 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005601 if( expected_status != PSA_SUCCESS )
5602 goto exit;
5603
5604 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005605 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005606 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5607 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5608 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5609 e_read_buffer, e_read_size,
5610 &e_read_length ) );
5611 if( is_default_public_exponent )
5612 TEST_EQUAL( e_read_length, 0 );
5613 else
5614 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5615
5616 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005617 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005618 goto exit;
5619
5620 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005621 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005622 exported, exported_size,
5623 &exported_length ) );
5624 {
5625 uint8_t *p = exported;
5626 uint8_t *end = exported + exported_length;
5627 size_t len;
5628 /* RSAPublicKey ::= SEQUENCE {
5629 * modulus INTEGER, -- n
5630 * publicExponent INTEGER } -- e
5631 */
5632 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005633 MBEDTLS_ASN1_SEQUENCE |
5634 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005635 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5636 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5637 MBEDTLS_ASN1_INTEGER ) );
5638 if( len >= 1 && p[0] == 0 )
5639 {
5640 ++p;
5641 --len;
5642 }
5643 if( e_arg->len == 0 )
5644 {
5645 TEST_EQUAL( len, 3 );
5646 TEST_EQUAL( p[0], 1 );
5647 TEST_EQUAL( p[1], 0 );
5648 TEST_EQUAL( p[2], 1 );
5649 }
5650 else
5651 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5652 }
5653
5654exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005655 /*
5656 * Key attributes may have been returned by psa_get_key_attributes() or
5657 * set by psa_set_key_domain_parameters() thus reset them as required.
5658 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005659 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005660
Ronald Cron5425a212020-08-04 14:58:35 +02005661 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005662 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005663 mbedtls_free( e_read_buffer );
5664 mbedtls_free( exported );
5665}
5666/* END_CASE */
5667
Darryl Greend49a4992018-06-18 17:27:26 +01005668/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005669void persistent_key_load_key_from_storage( data_t *data,
5670 int type_arg, int bits_arg,
5671 int usage_flags_arg, int alg_arg,
5672 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005673{
Ronald Cron71016a92020-08-28 19:01:50 +02005674 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005676 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5677 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005678 psa_key_type_t type = type_arg;
5679 size_t bits = bits_arg;
5680 psa_key_usage_t usage_flags = usage_flags_arg;
5681 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005683 unsigned char *first_export = NULL;
5684 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005685 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005686 size_t first_exported_length;
5687 size_t second_exported_length;
5688
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005689 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5690 {
5691 ASSERT_ALLOC( first_export, export_size );
5692 ASSERT_ALLOC( second_export, export_size );
5693 }
Darryl Greend49a4992018-06-18 17:27:26 +01005694
Gilles Peskine8817f612018-12-18 00:18:46 +01005695 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005696
Gilles Peskinec87af662019-05-15 16:12:22 +02005697 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005698 psa_set_key_usage_flags( &attributes, usage_flags );
5699 psa_set_key_algorithm( &attributes, alg );
5700 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005701 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005702
Darryl Green0c6575a2018-11-07 16:05:30 +00005703 switch( generation_method )
5704 {
5705 case IMPORT_KEY:
5706 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005707 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005708 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005709 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005710
Darryl Green0c6575a2018-11-07 16:05:30 +00005711 case GENERATE_KEY:
5712 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005713 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005714 break;
5715
5716 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005717#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005718 {
5719 /* Create base key */
5720 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5721 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5722 psa_set_key_usage_flags( &base_attributes,
5723 PSA_KEY_USAGE_DERIVE );
5724 psa_set_key_algorithm( &base_attributes, derive_alg );
5725 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005726 PSA_ASSERT( psa_import_key( &base_attributes,
5727 data->x, data->len,
5728 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005729 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005730 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005731 PSA_ASSERT( psa_key_derivation_input_key(
5732 &operation,
5733 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005734 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005735 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005736 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005737 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5738 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005739 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005741 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005742 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005743 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005744#else
5745 TEST_ASSUME( ! "KDF not supported in this configuration" );
5746#endif
5747 break;
5748
5749 default:
5750 TEST_ASSERT( ! "generation_method not implemented in test" );
5751 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005752 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005753 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005754
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005755 /* Export the key if permitted by the key policy. */
5756 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5757 {
Ronald Cron5425a212020-08-04 14:58:35 +02005758 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005759 first_export, export_size,
5760 &first_exported_length ) );
5761 if( generation_method == IMPORT_KEY )
5762 ASSERT_COMPARE( data->x, data->len,
5763 first_export, first_exported_length );
5764 }
Darryl Greend49a4992018-06-18 17:27:26 +01005765
5766 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005767 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005768 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005769 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005770
Darryl Greend49a4992018-06-18 17:27:26 +01005771 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005772 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005773 TEST_ASSERT( mbedtls_svc_key_id_equal(
5774 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005775 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5776 PSA_KEY_LIFETIME_PERSISTENT );
5777 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5778 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5779 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5780 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005781
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005782 /* Export the key again if permitted by the key policy. */
5783 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005784 {
Ronald Cron5425a212020-08-04 14:58:35 +02005785 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005786 second_export, export_size,
5787 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005788 ASSERT_COMPARE( first_export, first_exported_length,
5789 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005790 }
5791
5792 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005793 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005794 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005795
5796exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005797 /*
5798 * Key attributes may have been returned by psa_get_key_attributes()
5799 * thus reset them as required.
5800 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005801 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005802
Darryl Greend49a4992018-06-18 17:27:26 +01005803 mbedtls_free( first_export );
5804 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005805 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005806 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005807 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005808 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005809}
5810/* END_CASE */