blob: b172da6a9694221feac3339fc355efd3f76d84e0 [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
Ronald Cron5425a212020-08-04 14:58:35 +02001610 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001611 goto exit;
1612
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001613 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001614 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001615 else
1616 {
Ronald Cron5425a212020-08-04 14:58:35 +02001617 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001618 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001619 &key2 ) );
1620 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001621 reexported,
1622 export_size,
1623 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001624 ASSERT_COMPARE( exported, exported_length,
1625 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001626 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001627 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001628 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001629
1630destroy:
1631 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001632 PSA_ASSERT( psa_destroy_key( key ) );
1633 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001634
1635exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001636 /*
1637 * Key attributes may have been returned by psa_get_key_attributes()
1638 * thus reset them as required.
1639 */
1640 psa_reset_key_attributes( &got_attributes );
1641
itayzafrir3e02b3b2018-06-12 17:06:52 +03001642 mbedtls_free( exported );
1643 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001644 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001645}
1646/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001647
Moran Pekerf709f4a2018-06-06 17:26:04 +03001648/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001649void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001650 int type_arg,
1651 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001652 int export_size_delta,
1653 int expected_export_status_arg,
1654 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001655{
Ronald Cron5425a212020-08-04 14:58:35 +02001656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001657 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001658 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001659 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001660 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001661 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001662 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001663 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001665
Gilles Peskine8817f612018-12-18 00:18:46 +01001666 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001667
Gilles Peskine4747d192019-04-17 15:05:45 +02001668 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1669 psa_set_key_algorithm( &attributes, alg );
1670 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001671
1672 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001673 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001674
Gilles Peskine49c25912018-10-29 15:15:31 +01001675 /* Export the public key */
1676 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001677 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001678 exported, export_size,
1679 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001680 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001681 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001682 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001683 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001684 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001685 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001686 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001687 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001688 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001689 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1690 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001691 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001692
1693exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001694 /*
1695 * Key attributes may have been returned by psa_get_key_attributes()
1696 * thus reset them as required.
1697 */
1698 psa_reset_key_attributes( &attributes );
1699
itayzafrir3e02b3b2018-06-12 17:06:52 +03001700 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001702 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001703}
1704/* END_CASE */
1705
Gilles Peskine20035e32018-02-03 22:44:14 +01001706/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001707void import_and_exercise_key( data_t *data,
1708 int type_arg,
1709 int bits_arg,
1710 int alg_arg )
1711{
Ronald Cron5425a212020-08-04 14:58:35 +02001712 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001713 psa_key_type_t type = type_arg;
1714 size_t bits = bits_arg;
1715 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001716 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001718 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001719
Gilles Peskine8817f612018-12-18 00:18:46 +01001720 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001721
Gilles Peskine4747d192019-04-17 15:05:45 +02001722 psa_set_key_usage_flags( &attributes, usage );
1723 psa_set_key_algorithm( &attributes, alg );
1724 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001725
1726 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001727 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001728
1729 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001730 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001731 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1732 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001733
1734 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001735 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001736 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001737
Ronald Cron5425a212020-08-04 14:58:35 +02001738 PSA_ASSERT( psa_destroy_key( key ) );
1739 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001740
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001741exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001742 /*
1743 * Key attributes may have been returned by psa_get_key_attributes()
1744 * thus reset them as required.
1745 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001746 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001747
1748 psa_reset_key_attributes( &attributes );
1749 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001750 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001751}
1752/* END_CASE */
1753
1754/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001755void effective_key_attributes( int type_arg, int expected_type_arg,
1756 int bits_arg, int expected_bits_arg,
1757 int usage_arg, int expected_usage_arg,
1758 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001759{
Ronald Cron5425a212020-08-04 14:58:35 +02001760 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001761 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001762 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001763 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001764 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001765 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001766 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001767 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001768 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001769 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001770
Gilles Peskine8817f612018-12-18 00:18:46 +01001771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001772
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001773 psa_set_key_usage_flags( &attributes, usage );
1774 psa_set_key_algorithm( &attributes, alg );
1775 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001776 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001777
Ronald Cron5425a212020-08-04 14:58:35 +02001778 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001779 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001780
Ronald Cron5425a212020-08-04 14:58:35 +02001781 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001782 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1783 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1784 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1785 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001786
1787exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001788 /*
1789 * Key attributes may have been returned by psa_get_key_attributes()
1790 * thus reset them as required.
1791 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001792 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001793
1794 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001795 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001796}
1797/* END_CASE */
1798
1799/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001800void check_key_policy( int type_arg, int bits_arg,
1801 int usage_arg, int alg_arg )
1802{
1803 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1804 usage_arg, usage_arg, alg_arg, alg_arg );
1805 goto exit;
1806}
1807/* END_CASE */
1808
1809/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001810void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001811{
1812 /* Test each valid way of initializing the object, except for `= {0}`, as
1813 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1814 * though it's OK by the C standard. We could test for this, but we'd need
1815 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001816 psa_key_attributes_t func = psa_key_attributes_init( );
1817 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1818 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001819
1820 memset( &zero, 0, sizeof( zero ) );
1821
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001822 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1823 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1824 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001826 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1827 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1828 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1829
1830 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1831 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1832 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1833
1834 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1835 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1836 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1837
1838 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1839 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1840 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001841}
1842/* END_CASE */
1843
1844/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001845void mac_key_policy( int policy_usage,
1846 int policy_alg,
1847 int key_type,
1848 data_t *key_data,
1849 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001850{
Ronald Cron5425a212020-08-04 14:58:35 +02001851 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001853 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001854 psa_status_t status;
1855 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001856
Gilles Peskine8817f612018-12-18 00:18:46 +01001857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001858
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001859 psa_set_key_usage_flags( &attributes, policy_usage );
1860 psa_set_key_algorithm( &attributes, policy_alg );
1861 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001862
Gilles Peskine049c7532019-05-15 20:22:09 +02001863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001864 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001865
Ronald Cron5425a212020-08-04 14:58:35 +02001866 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001867 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001868 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001869 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001870 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001871 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001872 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001873
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001874 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001875 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001876 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001877 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001878 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001879 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001880 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001881
1882exit:
1883 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001884 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001885 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001886}
1887/* END_CASE */
1888
1889/* BEGIN_CASE */
1890void cipher_key_policy( int policy_usage,
1891 int policy_alg,
1892 int key_type,
1893 data_t *key_data,
1894 int exercise_alg )
1895{
Ronald Cron5425a212020-08-04 14:58:35 +02001896 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001898 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001899 psa_status_t status;
1900
Gilles Peskine8817f612018-12-18 00:18:46 +01001901 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001902
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001903 psa_set_key_usage_flags( &attributes, policy_usage );
1904 psa_set_key_algorithm( &attributes, policy_alg );
1905 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001906
Gilles Peskine049c7532019-05-15 20:22:09 +02001907 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001908 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001909
Ronald Cron5425a212020-08-04 14:58:35 +02001910 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911 if( policy_alg == exercise_alg &&
1912 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001913 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001915 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001916 psa_cipher_abort( &operation );
1917
Ronald Cron5425a212020-08-04 14:58:35 +02001918 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001919 if( policy_alg == exercise_alg &&
1920 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001921 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001923 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924
1925exit:
1926 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001927 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001928 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001929}
1930/* END_CASE */
1931
1932/* BEGIN_CASE */
1933void aead_key_policy( int policy_usage,
1934 int policy_alg,
1935 int key_type,
1936 data_t *key_data,
1937 int nonce_length_arg,
1938 int tag_length_arg,
1939 int exercise_alg )
1940{
Ronald Cron5425a212020-08-04 14:58:35 +02001941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001943 psa_status_t status;
1944 unsigned char nonce[16] = {0};
1945 size_t nonce_length = nonce_length_arg;
1946 unsigned char tag[16];
1947 size_t tag_length = tag_length_arg;
1948 size_t output_length;
1949
1950 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1951 TEST_ASSERT( tag_length <= sizeof( tag ) );
1952
Gilles Peskine8817f612018-12-18 00:18:46 +01001953 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001954
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001955 psa_set_key_usage_flags( &attributes, policy_usage );
1956 psa_set_key_algorithm( &attributes, policy_alg );
1957 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958
Gilles Peskine049c7532019-05-15 20:22:09 +02001959 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001960 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001961
Ronald Cron5425a212020-08-04 14:58:35 +02001962 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001963 nonce, nonce_length,
1964 NULL, 0,
1965 NULL, 0,
1966 tag, tag_length,
1967 &output_length );
1968 if( policy_alg == exercise_alg &&
1969 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001970 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001971 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001972 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001973
1974 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001975 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001976 nonce, nonce_length,
1977 NULL, 0,
1978 tag, tag_length,
1979 NULL, 0,
1980 &output_length );
1981 if( policy_alg == exercise_alg &&
1982 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001983 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001985 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986
1987exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001988 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001989 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990}
1991/* END_CASE */
1992
1993/* BEGIN_CASE */
1994void asymmetric_encryption_key_policy( int policy_usage,
1995 int policy_alg,
1996 int key_type,
1997 data_t *key_data,
1998 int exercise_alg )
1999{
Ronald Cron5425a212020-08-04 14:58:35 +02002000 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002 psa_status_t status;
2003 size_t key_bits;
2004 size_t buffer_length;
2005 unsigned char *buffer = NULL;
2006 size_t output_length;
2007
Gilles Peskine8817f612018-12-18 00:18:46 +01002008 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002009
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002010 psa_set_key_usage_flags( &attributes, policy_usage );
2011 psa_set_key_algorithm( &attributes, policy_alg );
2012 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013
Gilles Peskine049c7532019-05-15 20:22:09 +02002014 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002015 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002016
Ronald Cron5425a212020-08-04 14:58:35 +02002017 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002018 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2020 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002021 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022
Ronald Cron5425a212020-08-04 14:58:35 +02002023 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 NULL, 0,
2025 NULL, 0,
2026 buffer, buffer_length,
2027 &output_length );
2028 if( policy_alg == exercise_alg &&
2029 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002030 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002031 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002032 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002034 if( buffer_length != 0 )
2035 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002036 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037 buffer, buffer_length,
2038 NULL, 0,
2039 buffer, buffer_length,
2040 &output_length );
2041 if( policy_alg == exercise_alg &&
2042 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002043 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002044 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002045 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046
2047exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002048 /*
2049 * Key attributes may have been returned by psa_get_key_attributes()
2050 * thus reset them as required.
2051 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002052 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002053
2054 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002055 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056 mbedtls_free( buffer );
2057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
2061void asymmetric_signature_key_policy( int policy_usage,
2062 int policy_alg,
2063 int key_type,
2064 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002065 int exercise_alg,
2066 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002067{
Ronald Cron5425a212020-08-04 14:58:35 +02002068 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002069 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002071 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2072 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2073 * compatible with the policy and `payload_length_arg` is supposed to be
2074 * a valid input length to sign. If `payload_length_arg <= 0`,
2075 * `exercise_alg` is supposed to be forbidden by the policy. */
2076 int compatible_alg = payload_length_arg > 0;
2077 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002078 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079 size_t signature_length;
2080
Gilles Peskine8817f612018-12-18 00:18:46 +01002081 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002082
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002083 psa_set_key_usage_flags( &attributes, policy_usage );
2084 psa_set_key_algorithm( &attributes, policy_alg );
2085 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002086
Gilles Peskine049c7532019-05-15 20:22:09 +02002087 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002088 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002089
Ronald Cron5425a212020-08-04 14:58:35 +02002090 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002091 payload, payload_length,
2092 signature, sizeof( signature ),
2093 &signature_length );
2094 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002095 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002097 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002098
2099 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002100 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002101 payload, payload_length,
2102 signature, sizeof( signature ) );
2103 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002104 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002105 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002106 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002107
2108exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002109 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002110 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002111}
2112/* END_CASE */
2113
Janos Follathba3fab92019-06-11 14:50:16 +01002114/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002115void derive_key_policy( int policy_usage,
2116 int policy_alg,
2117 int key_type,
2118 data_t *key_data,
2119 int exercise_alg )
2120{
Ronald Cron5425a212020-08-04 14:58:35 +02002121 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002123 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002124 psa_status_t status;
2125
Gilles Peskine8817f612018-12-18 00:18:46 +01002126 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002127
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002128 psa_set_key_usage_flags( &attributes, policy_usage );
2129 psa_set_key_algorithm( &attributes, policy_alg );
2130 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002131
Gilles Peskine049c7532019-05-15 20:22:09 +02002132 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002133 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002134
Janos Follathba3fab92019-06-11 14:50:16 +01002135 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2136
2137 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2138 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002139 {
Janos Follathba3fab92019-06-11 14:50:16 +01002140 PSA_ASSERT( psa_key_derivation_input_bytes(
2141 &operation,
2142 PSA_KEY_DERIVATION_INPUT_SEED,
2143 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002144 }
Janos Follathba3fab92019-06-11 14:50:16 +01002145
2146 status = psa_key_derivation_input_key( &operation,
2147 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002148 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002149
Gilles Peskineea0fb492018-07-12 17:17:20 +02002150 if( policy_alg == exercise_alg &&
2151 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002152 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002153 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002154 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002155
2156exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002157 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002158 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002159 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002160}
2161/* END_CASE */
2162
2163/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002164void agreement_key_policy( int policy_usage,
2165 int policy_alg,
2166 int key_type_arg,
2167 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002168 int exercise_alg,
2169 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002170{
Ronald Cron5425a212020-08-04 14:58:35 +02002171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002173 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002174 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002175 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002176 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002177
Gilles Peskine8817f612018-12-18 00:18:46 +01002178 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002179
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002180 psa_set_key_usage_flags( &attributes, policy_usage );
2181 psa_set_key_algorithm( &attributes, policy_alg );
2182 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002183
Gilles Peskine049c7532019-05-15 20:22:09 +02002184 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002185 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002186
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002187 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002188 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002189
Steven Cooremance48e852020-10-05 16:02:45 +02002190 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002191
2192exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002193 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002194 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002195 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002196}
2197/* END_CASE */
2198
2199/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002200void key_policy_alg2( int key_type_arg, data_t *key_data,
2201 int usage_arg, int alg_arg, int alg2_arg )
2202{
Ronald Cron5425a212020-08-04 14:58:35 +02002203 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002204 psa_key_type_t key_type = key_type_arg;
2205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2206 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2207 psa_key_usage_t usage = usage_arg;
2208 psa_algorithm_t alg = alg_arg;
2209 psa_algorithm_t alg2 = alg2_arg;
2210
2211 PSA_ASSERT( psa_crypto_init( ) );
2212
2213 psa_set_key_usage_flags( &attributes, usage );
2214 psa_set_key_algorithm( &attributes, alg );
2215 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2216 psa_set_key_type( &attributes, key_type );
2217 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002218 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002219
Ronald Cron5425a212020-08-04 14:58:35 +02002220 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002221 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2222 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2223 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2224
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002225 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002226 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002227 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002228 goto exit;
2229
2230exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002231 /*
2232 * Key attributes may have been returned by psa_get_key_attributes()
2233 * thus reset them as required.
2234 */
2235 psa_reset_key_attributes( &got_attributes );
2236
Ronald Cron5425a212020-08-04 14:58:35 +02002237 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002238 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002239}
2240/* END_CASE */
2241
2242/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002243void raw_agreement_key_policy( int policy_usage,
2244 int policy_alg,
2245 int key_type_arg,
2246 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002247 int exercise_alg,
2248 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002249{
Ronald Cron5425a212020-08-04 14:58:35 +02002250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002252 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002253 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002254 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002255 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002256
2257 PSA_ASSERT( psa_crypto_init( ) );
2258
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002259 psa_set_key_usage_flags( &attributes, policy_usage );
2260 psa_set_key_algorithm( &attributes, policy_alg );
2261 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002262
Gilles Peskine049c7532019-05-15 20:22:09 +02002263 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002264 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002265
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002266 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002267
Steven Cooremance48e852020-10-05 16:02:45 +02002268 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002269
2270exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002271 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002272 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002273 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002274}
2275/* END_CASE */
2276
2277/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002278void copy_success( int source_usage_arg,
2279 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002280 int type_arg, data_t *material,
2281 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002282 int target_usage_arg,
2283 int target_alg_arg, int target_alg2_arg,
2284 int expected_usage_arg,
2285 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002286{
Gilles Peskineca25db92019-04-19 11:43:08 +02002287 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2288 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002289 psa_key_usage_t expected_usage = expected_usage_arg;
2290 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002291 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002292 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2293 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002294 uint8_t *export_buffer = NULL;
2295
Gilles Peskine57ab7212019-01-28 13:03:09 +01002296 PSA_ASSERT( psa_crypto_init( ) );
2297
Gilles Peskineca25db92019-04-19 11:43:08 +02002298 /* Prepare the source key. */
2299 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2300 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002301 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002302 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002303 PSA_ASSERT( psa_import_key( &source_attributes,
2304 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002305 &source_key ) );
2306 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002307
Gilles Peskineca25db92019-04-19 11:43:08 +02002308 /* Prepare the target attributes. */
2309 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002310 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002311 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002312 /* Set volatile lifetime to reset the key identifier to 0. */
2313 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2314 }
2315
Gilles Peskineca25db92019-04-19 11:43:08 +02002316 if( target_usage_arg != -1 )
2317 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2318 if( target_alg_arg != -1 )
2319 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002320 if( target_alg2_arg != -1 )
2321 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002322
2323 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002324 PSA_ASSERT( psa_copy_key( source_key,
2325 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002326
2327 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002328 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002329
2330 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002331 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002332 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2333 psa_get_key_type( &target_attributes ) );
2334 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2335 psa_get_key_bits( &target_attributes ) );
2336 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2337 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002338 TEST_EQUAL( expected_alg2,
2339 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002340 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2341 {
2342 size_t length;
2343 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002344 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002345 material->len, &length ) );
2346 ASSERT_COMPARE( material->x, material->len,
2347 export_buffer, length );
2348 }
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002349 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002350 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002351 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002352 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002353
Ronald Cron5425a212020-08-04 14:58:35 +02002354 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002355
2356exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002357 /*
2358 * Source and target key attributes may have been returned by
2359 * psa_get_key_attributes() thus reset them as required.
2360 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002361 psa_reset_key_attributes( &source_attributes );
2362 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002363
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002364 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002365 mbedtls_free( export_buffer );
2366}
2367/* END_CASE */
2368
2369/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002370void copy_fail( int source_usage_arg,
2371 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002372 int type_arg, data_t *material,
2373 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002374 int target_usage_arg,
2375 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002376 int expected_status_arg )
2377{
2378 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2379 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002380 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2381 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002382
2383 PSA_ASSERT( psa_crypto_init( ) );
2384
2385 /* Prepare the source key. */
2386 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2387 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002388 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002389 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002390 PSA_ASSERT( psa_import_key( &source_attributes,
2391 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002392 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002393
2394 /* Prepare the target attributes. */
2395 psa_set_key_type( &target_attributes, target_type_arg );
2396 psa_set_key_bits( &target_attributes, target_bits_arg );
2397 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2398 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002399 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002400
2401 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002402 TEST_EQUAL( psa_copy_key( source_key,
2403 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002404 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002405
Ronald Cron5425a212020-08-04 14:58:35 +02002406 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002407
Gilles Peskine4a644642019-05-03 17:14:08 +02002408exit:
2409 psa_reset_key_attributes( &source_attributes );
2410 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002411 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002412}
2413/* END_CASE */
2414
2415/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002416void hash_operation_init( )
2417{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002418 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002419 /* Test each valid way of initializing the object, except for `= {0}`, as
2420 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2421 * though it's OK by the C standard. We could test for this, but we'd need
2422 * to supress the Clang warning for the test. */
2423 psa_hash_operation_t func = psa_hash_operation_init( );
2424 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2425 psa_hash_operation_t zero;
2426
2427 memset( &zero, 0, sizeof( zero ) );
2428
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002429 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002430 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2431 PSA_ERROR_BAD_STATE );
2432 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2433 PSA_ERROR_BAD_STATE );
2434 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2435 PSA_ERROR_BAD_STATE );
2436
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002437 /* A default hash operation should be abortable without error. */
2438 PSA_ASSERT( psa_hash_abort( &func ) );
2439 PSA_ASSERT( psa_hash_abort( &init ) );
2440 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002441}
2442/* END_CASE */
2443
2444/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002445void hash_setup( int alg_arg,
2446 int expected_status_arg )
2447{
2448 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002449 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002450 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002451 psa_status_t status;
2452
Gilles Peskine8817f612018-12-18 00:18:46 +01002453 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002454
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002455 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002456 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002457
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002458 /* Whether setup succeeded or failed, abort must succeed. */
2459 PSA_ASSERT( psa_hash_abort( &operation ) );
2460
2461 /* If setup failed, reproduce the failure, so as to
2462 * test the resulting state of the operation object. */
2463 if( status != PSA_SUCCESS )
2464 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2465
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002466 /* Now the operation object should be reusable. */
2467#if defined(KNOWN_SUPPORTED_HASH_ALG)
2468 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2469 PSA_ASSERT( psa_hash_abort( &operation ) );
2470#endif
2471
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002472exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002473 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002474}
2475/* END_CASE */
2476
2477/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002478void hash_compute_fail( int alg_arg, data_t *input,
2479 int output_size_arg, int expected_status_arg )
2480{
2481 psa_algorithm_t alg = alg_arg;
2482 uint8_t *output = NULL;
2483 size_t output_size = output_size_arg;
2484 size_t output_length = INVALID_EXPORT_LENGTH;
2485 psa_status_t expected_status = expected_status_arg;
2486 psa_status_t status;
2487
2488 ASSERT_ALLOC( output, output_size );
2489
2490 PSA_ASSERT( psa_crypto_init( ) );
2491
2492 status = psa_hash_compute( alg, input->x, input->len,
2493 output, output_size, &output_length );
2494 TEST_EQUAL( status, expected_status );
2495 TEST_ASSERT( output_length <= output_size );
2496
2497exit:
2498 mbedtls_free( output );
2499 PSA_DONE( );
2500}
2501/* END_CASE */
2502
2503/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002504void hash_compare_fail( int alg_arg, data_t *input,
2505 data_t *reference_hash,
2506 int expected_status_arg )
2507{
2508 psa_algorithm_t alg = alg_arg;
2509 psa_status_t expected_status = expected_status_arg;
2510 psa_status_t status;
2511
2512 PSA_ASSERT( psa_crypto_init( ) );
2513
2514 status = psa_hash_compare( alg, input->x, input->len,
2515 reference_hash->x, reference_hash->len );
2516 TEST_EQUAL( status, expected_status );
2517
2518exit:
2519 PSA_DONE( );
2520}
2521/* END_CASE */
2522
2523/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002524void hash_compute_compare( int alg_arg, data_t *input,
2525 data_t *expected_output )
2526{
2527 psa_algorithm_t alg = alg_arg;
2528 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2529 size_t output_length = INVALID_EXPORT_LENGTH;
2530 size_t i;
2531
2532 PSA_ASSERT( psa_crypto_init( ) );
2533
2534 /* Compute with tight buffer */
2535 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002536 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002537 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002538 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002539 ASSERT_COMPARE( output, output_length,
2540 expected_output->x, expected_output->len );
2541
2542 /* Compute with larger buffer */
2543 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2544 output, sizeof( output ),
2545 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002546 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002547 ASSERT_COMPARE( output, output_length,
2548 expected_output->x, expected_output->len );
2549
2550 /* Compare with correct hash */
2551 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2552 output, output_length ) );
2553
2554 /* Compare with trailing garbage */
2555 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2556 output, output_length + 1 ),
2557 PSA_ERROR_INVALID_SIGNATURE );
2558
2559 /* Compare with truncated hash */
2560 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2561 output, output_length - 1 ),
2562 PSA_ERROR_INVALID_SIGNATURE );
2563
2564 /* Compare with corrupted value */
2565 for( i = 0; i < output_length; i++ )
2566 {
Chris Jones9634bb12021-01-20 15:56:42 +00002567 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002568 output[i] ^= 1;
2569 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2570 output, output_length ),
2571 PSA_ERROR_INVALID_SIGNATURE );
2572 output[i] ^= 1;
2573 }
2574
2575exit:
2576 PSA_DONE( );
2577}
2578/* END_CASE */
2579
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002580/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002581void hash_bad_order( )
2582{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002583 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002584 unsigned char input[] = "";
2585 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002586 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002587 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2588 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2589 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002590 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002591 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002592 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002593
Gilles Peskine8817f612018-12-18 00:18:46 +01002594 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002595
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002596 /* Call setup twice in a row. */
2597 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2598 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2599 PSA_ERROR_BAD_STATE );
2600 PSA_ASSERT( psa_hash_abort( &operation ) );
2601
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002602 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002603 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002604 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002605 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002606
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002607 /* Call update after finish. */
2608 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2609 PSA_ASSERT( psa_hash_finish( &operation,
2610 hash, sizeof( hash ), &hash_len ) );
2611 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002612 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002613 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002614
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002615 /* Call verify without calling setup beforehand. */
2616 TEST_EQUAL( psa_hash_verify( &operation,
2617 valid_hash, sizeof( valid_hash ) ),
2618 PSA_ERROR_BAD_STATE );
2619 PSA_ASSERT( psa_hash_abort( &operation ) );
2620
2621 /* Call verify after finish. */
2622 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2623 PSA_ASSERT( psa_hash_finish( &operation,
2624 hash, sizeof( hash ), &hash_len ) );
2625 TEST_EQUAL( psa_hash_verify( &operation,
2626 valid_hash, sizeof( valid_hash ) ),
2627 PSA_ERROR_BAD_STATE );
2628 PSA_ASSERT( psa_hash_abort( &operation ) );
2629
2630 /* Call verify twice in a row. */
2631 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2632 PSA_ASSERT( psa_hash_verify( &operation,
2633 valid_hash, sizeof( valid_hash ) ) );
2634 TEST_EQUAL( psa_hash_verify( &operation,
2635 valid_hash, sizeof( valid_hash ) ),
2636 PSA_ERROR_BAD_STATE );
2637 PSA_ASSERT( psa_hash_abort( &operation ) );
2638
2639 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002640 TEST_EQUAL( psa_hash_finish( &operation,
2641 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002642 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002643 PSA_ASSERT( psa_hash_abort( &operation ) );
2644
2645 /* Call finish twice in a row. */
2646 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2647 PSA_ASSERT( psa_hash_finish( &operation,
2648 hash, sizeof( hash ), &hash_len ) );
2649 TEST_EQUAL( psa_hash_finish( &operation,
2650 hash, sizeof( hash ), &hash_len ),
2651 PSA_ERROR_BAD_STATE );
2652 PSA_ASSERT( psa_hash_abort( &operation ) );
2653
2654 /* Call finish after calling verify. */
2655 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2656 PSA_ASSERT( psa_hash_verify( &operation,
2657 valid_hash, sizeof( valid_hash ) ) );
2658 TEST_EQUAL( psa_hash_finish( &operation,
2659 hash, sizeof( hash ), &hash_len ),
2660 PSA_ERROR_BAD_STATE );
2661 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002662
2663exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002664 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002665}
2666/* END_CASE */
2667
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002668/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002669void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002670{
2671 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002672 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2673 * appended to it */
2674 unsigned char hash[] = {
2675 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2676 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2677 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002678 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002679 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002680
Gilles Peskine8817f612018-12-18 00:18:46 +01002681 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002682
itayzafrir27e69452018-11-01 14:26:34 +02002683 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002684 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002685 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002686 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002687
itayzafrir27e69452018-11-01 14:26:34 +02002688 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002689 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002690 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002691 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002692
itayzafrir27e69452018-11-01 14:26:34 +02002693 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002694 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002695 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002696 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002697
itayzafrirec93d302018-10-18 18:01:10 +03002698exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002699 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002700}
2701/* END_CASE */
2702
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002703/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2704void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002705{
2706 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002707 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002708 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002709 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002710 size_t hash_len;
2711
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002713
itayzafrir58028322018-10-25 10:22:01 +03002714 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002715 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002716 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002717 hash, expected_size - 1, &hash_len ),
2718 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002719
2720exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002721 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002722}
2723/* END_CASE */
2724
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002725/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2726void hash_clone_source_state( )
2727{
2728 psa_algorithm_t alg = PSA_ALG_SHA_256;
2729 unsigned char hash[PSA_HASH_MAX_SIZE];
2730 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2731 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2732 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2733 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2734 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2735 size_t hash_len;
2736
2737 PSA_ASSERT( psa_crypto_init( ) );
2738 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2739
2740 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2741 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2742 PSA_ASSERT( psa_hash_finish( &op_finished,
2743 hash, sizeof( hash ), &hash_len ) );
2744 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2745 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2746
2747 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2748 PSA_ERROR_BAD_STATE );
2749
2750 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2751 PSA_ASSERT( psa_hash_finish( &op_init,
2752 hash, sizeof( hash ), &hash_len ) );
2753 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2754 PSA_ASSERT( psa_hash_finish( &op_finished,
2755 hash, sizeof( hash ), &hash_len ) );
2756 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2757 PSA_ASSERT( psa_hash_finish( &op_aborted,
2758 hash, sizeof( hash ), &hash_len ) );
2759
2760exit:
2761 psa_hash_abort( &op_source );
2762 psa_hash_abort( &op_init );
2763 psa_hash_abort( &op_setup );
2764 psa_hash_abort( &op_finished );
2765 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002766 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002767}
2768/* END_CASE */
2769
2770/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2771void hash_clone_target_state( )
2772{
2773 psa_algorithm_t alg = PSA_ALG_SHA_256;
2774 unsigned char hash[PSA_HASH_MAX_SIZE];
2775 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2776 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2777 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2778 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2779 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2780 size_t hash_len;
2781
2782 PSA_ASSERT( psa_crypto_init( ) );
2783
2784 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2785 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2786 PSA_ASSERT( psa_hash_finish( &op_finished,
2787 hash, sizeof( hash ), &hash_len ) );
2788 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2789 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2790
2791 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2792 PSA_ASSERT( psa_hash_finish( &op_target,
2793 hash, sizeof( hash ), &hash_len ) );
2794
2795 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2796 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2797 PSA_ERROR_BAD_STATE );
2798 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2799 PSA_ERROR_BAD_STATE );
2800
2801exit:
2802 psa_hash_abort( &op_target );
2803 psa_hash_abort( &op_init );
2804 psa_hash_abort( &op_setup );
2805 psa_hash_abort( &op_finished );
2806 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002807 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002808}
2809/* END_CASE */
2810
itayzafrir58028322018-10-25 10:22:01 +03002811/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002812void mac_operation_init( )
2813{
Jaeden Amero252ef282019-02-15 14:05:35 +00002814 const uint8_t input[1] = { 0 };
2815
Jaeden Amero769ce272019-01-04 11:48:03 +00002816 /* Test each valid way of initializing the object, except for `= {0}`, as
2817 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2818 * though it's OK by the C standard. We could test for this, but we'd need
2819 * to supress the Clang warning for the test. */
2820 psa_mac_operation_t func = psa_mac_operation_init( );
2821 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2822 psa_mac_operation_t zero;
2823
2824 memset( &zero, 0, sizeof( zero ) );
2825
Jaeden Amero252ef282019-02-15 14:05:35 +00002826 /* A freshly-initialized MAC operation should not be usable. */
2827 TEST_EQUAL( psa_mac_update( &func,
2828 input, sizeof( input ) ),
2829 PSA_ERROR_BAD_STATE );
2830 TEST_EQUAL( psa_mac_update( &init,
2831 input, sizeof( input ) ),
2832 PSA_ERROR_BAD_STATE );
2833 TEST_EQUAL( psa_mac_update( &zero,
2834 input, sizeof( input ) ),
2835 PSA_ERROR_BAD_STATE );
2836
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002837 /* A default MAC operation should be abortable without error. */
2838 PSA_ASSERT( psa_mac_abort( &func ) );
2839 PSA_ASSERT( psa_mac_abort( &init ) );
2840 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002841}
2842/* END_CASE */
2843
2844/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002845void mac_setup( int key_type_arg,
2846 data_t *key,
2847 int alg_arg,
2848 int expected_status_arg )
2849{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002850 psa_key_type_t key_type = key_type_arg;
2851 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002852 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002853 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002854 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2855#if defined(KNOWN_SUPPORTED_MAC_ALG)
2856 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2857#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002858
Gilles Peskine8817f612018-12-18 00:18:46 +01002859 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002860
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002861 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2862 &operation, &status ) )
2863 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002864 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002865
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002866 /* The operation object should be reusable. */
2867#if defined(KNOWN_SUPPORTED_MAC_ALG)
2868 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2869 smoke_test_key_data,
2870 sizeof( smoke_test_key_data ),
2871 KNOWN_SUPPORTED_MAC_ALG,
2872 &operation, &status ) )
2873 goto exit;
2874 TEST_EQUAL( status, PSA_SUCCESS );
2875#endif
2876
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002877exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002878 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002879}
2880/* END_CASE */
2881
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002882/* 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 +00002883void mac_bad_order( )
2884{
Ronald Cron5425a212020-08-04 14:58:35 +02002885 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002886 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2887 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002888 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002889 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2890 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2891 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002893 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2894 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2895 size_t sign_mac_length = 0;
2896 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2897 const uint8_t verify_mac[] = {
2898 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2899 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2900 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2901
2902 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002903 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002904 psa_set_key_algorithm( &attributes, alg );
2905 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002906
Ronald Cron5425a212020-08-04 14:58:35 +02002907 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2908 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002909
Jaeden Amero252ef282019-02-15 14:05:35 +00002910 /* Call update without calling setup beforehand. */
2911 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2912 PSA_ERROR_BAD_STATE );
2913 PSA_ASSERT( psa_mac_abort( &operation ) );
2914
2915 /* Call sign finish without calling setup beforehand. */
2916 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2917 &sign_mac_length),
2918 PSA_ERROR_BAD_STATE );
2919 PSA_ASSERT( psa_mac_abort( &operation ) );
2920
2921 /* Call verify finish without calling setup beforehand. */
2922 TEST_EQUAL( psa_mac_verify_finish( &operation,
2923 verify_mac, sizeof( verify_mac ) ),
2924 PSA_ERROR_BAD_STATE );
2925 PSA_ASSERT( psa_mac_abort( &operation ) );
2926
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002927 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002928 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2929 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002930 PSA_ERROR_BAD_STATE );
2931 PSA_ASSERT( psa_mac_abort( &operation ) );
2932
Jaeden Amero252ef282019-02-15 14:05:35 +00002933 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002934 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002935 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2936 PSA_ASSERT( psa_mac_sign_finish( &operation,
2937 sign_mac, sizeof( sign_mac ),
2938 &sign_mac_length ) );
2939 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2940 PSA_ERROR_BAD_STATE );
2941 PSA_ASSERT( psa_mac_abort( &operation ) );
2942
2943 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002944 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002945 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2946 PSA_ASSERT( psa_mac_verify_finish( &operation,
2947 verify_mac, sizeof( verify_mac ) ) );
2948 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2949 PSA_ERROR_BAD_STATE );
2950 PSA_ASSERT( psa_mac_abort( &operation ) );
2951
2952 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002953 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002954 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2955 PSA_ASSERT( psa_mac_sign_finish( &operation,
2956 sign_mac, sizeof( sign_mac ),
2957 &sign_mac_length ) );
2958 TEST_EQUAL( psa_mac_sign_finish( &operation,
2959 sign_mac, sizeof( sign_mac ),
2960 &sign_mac_length ),
2961 PSA_ERROR_BAD_STATE );
2962 PSA_ASSERT( psa_mac_abort( &operation ) );
2963
2964 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002965 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002966 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2967 PSA_ASSERT( psa_mac_verify_finish( &operation,
2968 verify_mac, sizeof( verify_mac ) ) );
2969 TEST_EQUAL( psa_mac_verify_finish( &operation,
2970 verify_mac, sizeof( verify_mac ) ),
2971 PSA_ERROR_BAD_STATE );
2972 PSA_ASSERT( psa_mac_abort( &operation ) );
2973
2974 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002975 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002976 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2977 TEST_EQUAL( psa_mac_verify_finish( &operation,
2978 verify_mac, sizeof( verify_mac ) ),
2979 PSA_ERROR_BAD_STATE );
2980 PSA_ASSERT( psa_mac_abort( &operation ) );
2981
2982 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002983 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002984 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2985 TEST_EQUAL( psa_mac_sign_finish( &operation,
2986 sign_mac, sizeof( sign_mac ),
2987 &sign_mac_length ),
2988 PSA_ERROR_BAD_STATE );
2989 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002990
Ronald Cron5425a212020-08-04 14:58:35 +02002991 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002992
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002993exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002994 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002995}
2996/* END_CASE */
2997
2998/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002999void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003000 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003001 int alg_arg,
3002 data_t *input,
3003 data_t *expected_mac )
3004{
Ronald Cron5425a212020-08-04 14:58:35 +02003005 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003006 psa_key_type_t key_type = key_type_arg;
3007 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003008 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003009 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003010 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003011 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003012 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003013 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003014 const size_t output_sizes_to_test[] = {
3015 0,
3016 1,
3017 expected_mac->len - 1,
3018 expected_mac->len,
3019 expected_mac->len + 1,
3020 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003021
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003022 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003023 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003024 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003025
Gilles Peskine8817f612018-12-18 00:18:46 +01003026 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003027
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003028 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003029 psa_set_key_algorithm( &attributes, alg );
3030 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003031
Ronald Cron5425a212020-08-04 14:58:35 +02003032 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3033 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003034
Gilles Peskine8b356b52020-08-25 23:44:59 +02003035 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3036 {
3037 const size_t output_size = output_sizes_to_test[i];
3038 psa_status_t expected_status =
3039 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3040 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003041
Chris Jones9634bb12021-01-20 15:56:42 +00003042 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003043 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003044
Gilles Peskine8b356b52020-08-25 23:44:59 +02003045 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003046 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003047 PSA_ASSERT( psa_mac_update( &operation,
3048 input->x, input->len ) );
3049 TEST_EQUAL( psa_mac_sign_finish( &operation,
3050 actual_mac, output_size,
3051 &mac_length ),
3052 expected_status );
3053 PSA_ASSERT( psa_mac_abort( &operation ) );
3054
3055 if( expected_status == PSA_SUCCESS )
3056 {
3057 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3058 actual_mac, mac_length );
3059 }
3060 mbedtls_free( actual_mac );
3061 actual_mac = NULL;
3062 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003063
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003064exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003065 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003066 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003067 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003068 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003069}
3070/* END_CASE */
3071
3072/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003073void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003074 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003075 int alg_arg,
3076 data_t *input,
3077 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003078{
Ronald Cron5425a212020-08-04 14:58:35 +02003079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003080 psa_key_type_t key_type = key_type_arg;
3081 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003082 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003084 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003085
Gilles Peskine69c12672018-06-28 00:07:19 +02003086 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3087
Gilles Peskine8817f612018-12-18 00:18:46 +01003088 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003090 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003091 psa_set_key_algorithm( &attributes, alg );
3092 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003093
Ronald Cron5425a212020-08-04 14:58:35 +02003094 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3095 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003096
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003097 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003098 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003099 PSA_ASSERT( psa_mac_update( &operation,
3100 input->x, input->len ) );
3101 PSA_ASSERT( psa_mac_verify_finish( &operation,
3102 expected_mac->x,
3103 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003104
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003105 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003106 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003107 PSA_ASSERT( psa_mac_update( &operation,
3108 input->x, input->len ) );
3109 TEST_EQUAL( psa_mac_verify_finish( &operation,
3110 expected_mac->x,
3111 expected_mac->len - 1 ),
3112 PSA_ERROR_INVALID_SIGNATURE );
3113
3114 /* Test a MAC that's too long. */
3115 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3116 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003117 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003118 PSA_ASSERT( psa_mac_update( &operation,
3119 input->x, input->len ) );
3120 TEST_EQUAL( psa_mac_verify_finish( &operation,
3121 perturbed_mac,
3122 expected_mac->len + 1 ),
3123 PSA_ERROR_INVALID_SIGNATURE );
3124
3125 /* Test changing one byte. */
3126 for( size_t i = 0; i < expected_mac->len; i++ )
3127 {
Chris Jones9634bb12021-01-20 15:56:42 +00003128 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003129 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003130 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003131 PSA_ASSERT( psa_mac_update( &operation,
3132 input->x, input->len ) );
3133 TEST_EQUAL( psa_mac_verify_finish( &operation,
3134 perturbed_mac,
3135 expected_mac->len ),
3136 PSA_ERROR_INVALID_SIGNATURE );
3137 perturbed_mac[i] ^= 1;
3138 }
3139
Gilles Peskine8c9def32018-02-08 10:02:12 +01003140exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003141 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003142 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003143 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003144 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003145}
3146/* END_CASE */
3147
3148/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003149void cipher_operation_init( )
3150{
Jaeden Ameroab439972019-02-15 14:12:05 +00003151 const uint8_t input[1] = { 0 };
3152 unsigned char output[1] = { 0 };
3153 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003154 /* Test each valid way of initializing the object, except for `= {0}`, as
3155 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3156 * though it's OK by the C standard. We could test for this, but we'd need
3157 * to supress the Clang warning for the test. */
3158 psa_cipher_operation_t func = psa_cipher_operation_init( );
3159 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3160 psa_cipher_operation_t zero;
3161
3162 memset( &zero, 0, sizeof( zero ) );
3163
Jaeden Ameroab439972019-02-15 14:12:05 +00003164 /* A freshly-initialized cipher operation should not be usable. */
3165 TEST_EQUAL( psa_cipher_update( &func,
3166 input, sizeof( input ),
3167 output, sizeof( output ),
3168 &output_length ),
3169 PSA_ERROR_BAD_STATE );
3170 TEST_EQUAL( psa_cipher_update( &init,
3171 input, sizeof( input ),
3172 output, sizeof( output ),
3173 &output_length ),
3174 PSA_ERROR_BAD_STATE );
3175 TEST_EQUAL( psa_cipher_update( &zero,
3176 input, sizeof( input ),
3177 output, sizeof( output ),
3178 &output_length ),
3179 PSA_ERROR_BAD_STATE );
3180
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003181 /* A default cipher operation should be abortable without error. */
3182 PSA_ASSERT( psa_cipher_abort( &func ) );
3183 PSA_ASSERT( psa_cipher_abort( &init ) );
3184 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003185}
3186/* END_CASE */
3187
3188/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003189void cipher_setup( int key_type_arg,
3190 data_t *key,
3191 int alg_arg,
3192 int expected_status_arg )
3193{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003194 psa_key_type_t key_type = key_type_arg;
3195 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003196 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003197 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003198 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003199#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003200 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3201#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003202
Gilles Peskine8817f612018-12-18 00:18:46 +01003203 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003204
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003205 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3206 &operation, &status ) )
3207 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003208 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003209
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003210 /* The operation object should be reusable. */
3211#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3212 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3213 smoke_test_key_data,
3214 sizeof( smoke_test_key_data ),
3215 KNOWN_SUPPORTED_CIPHER_ALG,
3216 &operation, &status ) )
3217 goto exit;
3218 TEST_EQUAL( status, PSA_SUCCESS );
3219#endif
3220
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003221exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003222 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003223 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003224}
3225/* END_CASE */
3226
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003227/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003228void cipher_bad_order( )
3229{
Ronald Cron5425a212020-08-04 14:58:35 +02003230 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003231 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3232 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003233 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003234 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003235 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003236 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003237 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3238 0xaa, 0xaa, 0xaa, 0xaa };
3239 const uint8_t text[] = {
3240 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3241 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003242 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003243 size_t length = 0;
3244
3245 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003246 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3247 psa_set_key_algorithm( &attributes, alg );
3248 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003249 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3250 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003251
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003252 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003253 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3254 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003255 PSA_ERROR_BAD_STATE );
3256 PSA_ASSERT( psa_cipher_abort( &operation ) );
3257
3258 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003259 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3260 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003261 PSA_ERROR_BAD_STATE );
3262 PSA_ASSERT( psa_cipher_abort( &operation ) );
3263
Jaeden Ameroab439972019-02-15 14:12:05 +00003264 /* Generate an IV without calling setup beforehand. */
3265 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3266 buffer, sizeof( buffer ),
3267 &length ),
3268 PSA_ERROR_BAD_STATE );
3269 PSA_ASSERT( psa_cipher_abort( &operation ) );
3270
3271 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003272 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003273 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3274 buffer, sizeof( buffer ),
3275 &length ) );
3276 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3277 buffer, sizeof( buffer ),
3278 &length ),
3279 PSA_ERROR_BAD_STATE );
3280 PSA_ASSERT( psa_cipher_abort( &operation ) );
3281
3282 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003283 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003284 PSA_ASSERT( psa_cipher_set_iv( &operation,
3285 iv, sizeof( iv ) ) );
3286 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3287 buffer, sizeof( buffer ),
3288 &length ),
3289 PSA_ERROR_BAD_STATE );
3290 PSA_ASSERT( psa_cipher_abort( &operation ) );
3291
3292 /* Set an IV without calling setup beforehand. */
3293 TEST_EQUAL( psa_cipher_set_iv( &operation,
3294 iv, sizeof( iv ) ),
3295 PSA_ERROR_BAD_STATE );
3296 PSA_ASSERT( psa_cipher_abort( &operation ) );
3297
3298 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003299 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003300 PSA_ASSERT( psa_cipher_set_iv( &operation,
3301 iv, sizeof( iv ) ) );
3302 TEST_EQUAL( psa_cipher_set_iv( &operation,
3303 iv, sizeof( iv ) ),
3304 PSA_ERROR_BAD_STATE );
3305 PSA_ASSERT( psa_cipher_abort( &operation ) );
3306
3307 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003308 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003309 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3310 buffer, sizeof( buffer ),
3311 &length ) );
3312 TEST_EQUAL( psa_cipher_set_iv( &operation,
3313 iv, sizeof( iv ) ),
3314 PSA_ERROR_BAD_STATE );
3315 PSA_ASSERT( psa_cipher_abort( &operation ) );
3316
3317 /* Call update without calling setup beforehand. */
3318 TEST_EQUAL( psa_cipher_update( &operation,
3319 text, sizeof( text ),
3320 buffer, sizeof( buffer ),
3321 &length ),
3322 PSA_ERROR_BAD_STATE );
3323 PSA_ASSERT( psa_cipher_abort( &operation ) );
3324
3325 /* Call update without an IV where an IV is required. */
3326 TEST_EQUAL( psa_cipher_update( &operation,
3327 text, sizeof( text ),
3328 buffer, sizeof( buffer ),
3329 &length ),
3330 PSA_ERROR_BAD_STATE );
3331 PSA_ASSERT( psa_cipher_abort( &operation ) );
3332
3333 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003334 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003335 PSA_ASSERT( psa_cipher_set_iv( &operation,
3336 iv, sizeof( iv ) ) );
3337 PSA_ASSERT( psa_cipher_finish( &operation,
3338 buffer, sizeof( buffer ), &length ) );
3339 TEST_EQUAL( psa_cipher_update( &operation,
3340 text, sizeof( text ),
3341 buffer, sizeof( buffer ),
3342 &length ),
3343 PSA_ERROR_BAD_STATE );
3344 PSA_ASSERT( psa_cipher_abort( &operation ) );
3345
3346 /* Call finish without calling setup beforehand. */
3347 TEST_EQUAL( psa_cipher_finish( &operation,
3348 buffer, sizeof( buffer ), &length ),
3349 PSA_ERROR_BAD_STATE );
3350 PSA_ASSERT( psa_cipher_abort( &operation ) );
3351
3352 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003353 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003354 /* Not calling update means we are encrypting an empty buffer, which is OK
3355 * for cipher modes with padding. */
3356 TEST_EQUAL( psa_cipher_finish( &operation,
3357 buffer, sizeof( buffer ), &length ),
3358 PSA_ERROR_BAD_STATE );
3359 PSA_ASSERT( psa_cipher_abort( &operation ) );
3360
3361 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003362 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003363 PSA_ASSERT( psa_cipher_set_iv( &operation,
3364 iv, sizeof( iv ) ) );
3365 PSA_ASSERT( psa_cipher_finish( &operation,
3366 buffer, sizeof( buffer ), &length ) );
3367 TEST_EQUAL( psa_cipher_finish( &operation,
3368 buffer, sizeof( buffer ), &length ),
3369 PSA_ERROR_BAD_STATE );
3370 PSA_ASSERT( psa_cipher_abort( &operation ) );
3371
Ronald Cron5425a212020-08-04 14:58:35 +02003372 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003373
Jaeden Ameroab439972019-02-15 14:12:05 +00003374exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003375 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003376 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003377}
3378/* END_CASE */
3379
3380/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003381void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003382 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003383 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003384 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003385{
Ronald Cron5425a212020-08-04 14:58:35 +02003386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003387 psa_status_t status;
3388 psa_key_type_t key_type = key_type_arg;
3389 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003390 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003391 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003392 size_t output_buffer_size = 0;
3393 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003394 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003395 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003397
Gilles Peskine8817f612018-12-18 00:18:46 +01003398 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003399
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003400 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3401 psa_set_key_algorithm( &attributes, alg );
3402 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003403
Ronald Cron5425a212020-08-04 14:58:35 +02003404 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3405 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003406
Ronald Cron5425a212020-08-04 14:58:35 +02003407 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003408
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003409 if( iv->len > 0 )
3410 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003411 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003412 }
3413
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003414 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003415 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003416 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003417
Gilles Peskine8817f612018-12-18 00:18:46 +01003418 PSA_ASSERT( psa_cipher_update( &operation,
3419 input->x, input->len,
3420 output, output_buffer_size,
3421 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003422 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003423 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003424 output + total_output_length,
3425 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003426 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003427 total_output_length += function_output_length;
3428
Gilles Peskinefe11b722018-12-18 00:24:04 +01003429 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003430 if( expected_status == PSA_SUCCESS )
3431 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003432 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003433 ASSERT_COMPARE( expected_output->x, expected_output->len,
3434 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003435 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003436
Gilles Peskine50e586b2018-06-08 14:28:46 +02003437exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003438 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003439 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003440 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003441 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003442}
3443/* END_CASE */
3444
3445/* BEGIN_CASE */
3446void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003447 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003448 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003449 int first_part_size_arg,
3450 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003451 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003452{
Ronald Cron5425a212020-08-04 14:58:35 +02003453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003454 psa_key_type_t key_type = key_type_arg;
3455 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003456 size_t first_part_size = first_part_size_arg;
3457 size_t output1_length = output1_length_arg;
3458 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003459 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003460 size_t output_buffer_size = 0;
3461 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003462 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003463 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003464 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003465
Gilles Peskine8817f612018-12-18 00:18:46 +01003466 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003467
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003468 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3469 psa_set_key_algorithm( &attributes, alg );
3470 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003471
Ronald Cron5425a212020-08-04 14:58:35 +02003472 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3473 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003474
Ronald Cron5425a212020-08-04 14:58:35 +02003475 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003476
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003477 if( iv->len > 0 )
3478 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003479 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003480 }
3481
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003482 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003483 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003484 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003485
Gilles Peskinee0866522019-02-19 19:44:00 +01003486 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003487 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3488 output, output_buffer_size,
3489 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003490 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003491 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003492 PSA_ASSERT( psa_cipher_update( &operation,
3493 input->x + first_part_size,
3494 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003495 output + total_output_length,
3496 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003497 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003498 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003499 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003500 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003501 output + total_output_length,
3502 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003503 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003504 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003505 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003506
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003507 ASSERT_COMPARE( expected_output->x, expected_output->len,
3508 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003509
3510exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003511 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003512 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003513 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003514 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003515}
3516/* END_CASE */
3517
3518/* BEGIN_CASE */
3519void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003520 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003521 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003522 int first_part_size_arg,
3523 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003524 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003525{
Ronald Cron5425a212020-08-04 14:58:35 +02003526 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003527 psa_key_type_t key_type = key_type_arg;
3528 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003529 size_t first_part_size = first_part_size_arg;
3530 size_t output1_length = output1_length_arg;
3531 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003532 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003533 size_t output_buffer_size = 0;
3534 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003535 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003536 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003538
Gilles Peskine8817f612018-12-18 00:18:46 +01003539 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003540
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003541 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3542 psa_set_key_algorithm( &attributes, alg );
3543 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003544
Ronald Cron5425a212020-08-04 14:58:35 +02003545 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3546 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003547
Ronald Cron5425a212020-08-04 14:58:35 +02003548 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003549
Steven Cooreman177deba2020-09-07 17:14:14 +02003550 if( iv->len > 0 )
3551 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003552 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003553 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003554
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003555 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003556 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003557 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003558
Gilles Peskinee0866522019-02-19 19:44:00 +01003559 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003560 PSA_ASSERT( psa_cipher_update( &operation,
3561 input->x, first_part_size,
3562 output, output_buffer_size,
3563 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003564 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003565 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003566 PSA_ASSERT( psa_cipher_update( &operation,
3567 input->x + first_part_size,
3568 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003569 output + total_output_length,
3570 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003571 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003572 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003573 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003574 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003575 output + total_output_length,
3576 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003577 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003578 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003579 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003580
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003581 ASSERT_COMPARE( expected_output->x, expected_output->len,
3582 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003583
3584exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003585 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003586 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003587 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003588 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003589}
3590/* END_CASE */
3591
Gilles Peskine50e586b2018-06-08 14:28:46 +02003592/* BEGIN_CASE */
3593void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003594 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003595 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003596 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003597{
Ronald Cron5425a212020-08-04 14:58:35 +02003598 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003599 psa_status_t status;
3600 psa_key_type_t key_type = key_type_arg;
3601 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003602 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003603 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003604 size_t output_buffer_size = 0;
3605 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003606 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003607 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003609
Gilles Peskine8817f612018-12-18 00:18:46 +01003610 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003611
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003612 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3613 psa_set_key_algorithm( &attributes, alg );
3614 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003615
Ronald Cron5425a212020-08-04 14:58:35 +02003616 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3617 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003618
Ronald Cron5425a212020-08-04 14:58:35 +02003619 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003620
Steven Cooreman177deba2020-09-07 17:14:14 +02003621 if( iv->len > 0 )
3622 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003623 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003624 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003625
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003626 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003627 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003628 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003629
Gilles Peskine8817f612018-12-18 00:18:46 +01003630 PSA_ASSERT( psa_cipher_update( &operation,
3631 input->x, input->len,
3632 output, output_buffer_size,
3633 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003634 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003635 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003636 output + total_output_length,
3637 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003638 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003639 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003640 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003641
3642 if( expected_status == PSA_SUCCESS )
3643 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003644 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003645 ASSERT_COMPARE( expected_output->x, expected_output->len,
3646 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003647 }
3648
Gilles Peskine50e586b2018-06-08 14:28:46 +02003649exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003650 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003651 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003652 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003653 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003654}
3655/* END_CASE */
3656
Gilles Peskine50e586b2018-06-08 14:28:46 +02003657/* BEGIN_CASE */
3658void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003659 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003660 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003661{
Ronald Cron5425a212020-08-04 14:58:35 +02003662 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003663 psa_key_type_t key_type = key_type_arg;
3664 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003665 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003666 size_t iv_size = 16;
3667 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003668 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003669 size_t output1_size = 0;
3670 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003671 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003672 size_t output2_size = 0;
3673 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003674 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003675 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3676 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003678
Gilles Peskine8817f612018-12-18 00:18:46 +01003679 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003680
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003681 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3682 psa_set_key_algorithm( &attributes, alg );
3683 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003684
Ronald Cron5425a212020-08-04 14:58:35 +02003685 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3686 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003687
Ronald Cron5425a212020-08-04 14:58:35 +02003688 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3689 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003690
Steven Cooreman177deba2020-09-07 17:14:14 +02003691 if( alg != PSA_ALG_ECB_NO_PADDING )
3692 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003693 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3694 iv, iv_size,
3695 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003696 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003697 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003698 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003699 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003700
Gilles Peskine8817f612018-12-18 00:18:46 +01003701 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3702 output1, output1_size,
3703 &output1_length ) );
3704 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003705 output1 + output1_length,
3706 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003707 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003708
Gilles Peskine048b7f02018-06-08 14:20:49 +02003709 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003710
Gilles Peskine8817f612018-12-18 00:18:46 +01003711 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003712
3713 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003714 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003715
Steven Cooreman177deba2020-09-07 17:14:14 +02003716 if( iv_length > 0 )
3717 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003718 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3719 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003720 }
3721
Gilles Peskine8817f612018-12-18 00:18:46 +01003722 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3723 output2, output2_size,
3724 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003725 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003726 PSA_ASSERT( psa_cipher_finish( &operation2,
3727 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003728 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003729 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003730
Gilles Peskine048b7f02018-06-08 14:20:49 +02003731 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003732
Gilles Peskine8817f612018-12-18 00:18:46 +01003733 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003734
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003735 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003736
3737exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003738 psa_cipher_abort( &operation1 );
3739 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003740 mbedtls_free( output1 );
3741 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003742 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003743 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003744}
3745/* END_CASE */
3746
3747/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003748void cipher_verify_output_multipart( int alg_arg,
3749 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003750 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003751 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003752 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003753{
Ronald Cron5425a212020-08-04 14:58:35 +02003754 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003755 psa_key_type_t key_type = key_type_arg;
3756 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003757 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003758 unsigned char iv[16] = {0};
3759 size_t iv_size = 16;
3760 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003761 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003762 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003763 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003764 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003765 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003766 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003767 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003768 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3769 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003770 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003771
Gilles Peskine8817f612018-12-18 00:18:46 +01003772 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003773
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003774 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3775 psa_set_key_algorithm( &attributes, alg );
3776 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003777
Ronald Cron5425a212020-08-04 14:58:35 +02003778 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3779 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003780
Ronald Cron5425a212020-08-04 14:58:35 +02003781 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3782 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003783
Steven Cooreman177deba2020-09-07 17:14:14 +02003784 if( alg != PSA_ALG_ECB_NO_PADDING )
3785 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003786 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3787 iv, iv_size,
3788 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003789 }
3790
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003791 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003792 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003793 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003794
Gilles Peskinee0866522019-02-19 19:44:00 +01003795 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003796
Gilles Peskine8817f612018-12-18 00:18:46 +01003797 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3798 output1, output1_buffer_size,
3799 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003800 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003801
Gilles Peskine8817f612018-12-18 00:18:46 +01003802 PSA_ASSERT( psa_cipher_update( &operation1,
3803 input->x + first_part_size,
3804 input->len - first_part_size,
3805 output1, output1_buffer_size,
3806 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003807 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003808
Gilles Peskine8817f612018-12-18 00:18:46 +01003809 PSA_ASSERT( psa_cipher_finish( &operation1,
3810 output1 + output1_length,
3811 output1_buffer_size - output1_length,
3812 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003813 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003814
Gilles Peskine8817f612018-12-18 00:18:46 +01003815 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003816
Gilles Peskine048b7f02018-06-08 14:20:49 +02003817 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003818 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003819
Steven Cooreman177deba2020-09-07 17:14:14 +02003820 if( iv_length > 0 )
3821 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003822 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3823 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003824 }
Moran Pekerded84402018-06-06 16:36:50 +03003825
Gilles Peskine8817f612018-12-18 00:18:46 +01003826 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3827 output2, output2_buffer_size,
3828 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003829 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003830
Gilles Peskine8817f612018-12-18 00:18:46 +01003831 PSA_ASSERT( psa_cipher_update( &operation2,
3832 output1 + first_part_size,
3833 output1_length - first_part_size,
3834 output2, output2_buffer_size,
3835 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003836 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003837
Gilles Peskine8817f612018-12-18 00:18:46 +01003838 PSA_ASSERT( psa_cipher_finish( &operation2,
3839 output2 + output2_length,
3840 output2_buffer_size - output2_length,
3841 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003842 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003843
Gilles Peskine8817f612018-12-18 00:18:46 +01003844 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003845
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003846 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003847
3848exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003849 psa_cipher_abort( &operation1 );
3850 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003851 mbedtls_free( output1 );
3852 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003853 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003854 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003855}
3856/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003857
Gilles Peskine20035e32018-02-03 22:44:14 +01003858/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003859void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003860 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003861 data_t *nonce,
3862 data_t *additional_data,
3863 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003864 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003865{
Ronald Cron5425a212020-08-04 14:58:35 +02003866 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003867 psa_key_type_t key_type = key_type_arg;
3868 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003869 unsigned char *output_data = NULL;
3870 size_t output_size = 0;
3871 size_t output_length = 0;
3872 unsigned char *output_data2 = NULL;
3873 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003874 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003875 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003877
Gilles Peskine4abf7412018-06-18 16:35:34 +02003878 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003879 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3880 * should be exact. */
3881 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3882 TEST_EQUAL( output_size,
3883 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003884 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003885
Gilles Peskine8817f612018-12-18 00:18:46 +01003886 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003887
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003888 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3889 psa_set_key_algorithm( &attributes, alg );
3890 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003891
Gilles Peskine049c7532019-05-15 20:22:09 +02003892 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003893 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003894
Ronald Cron5425a212020-08-04 14:58:35 +02003895 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003896 nonce->x, nonce->len,
3897 additional_data->x,
3898 additional_data->len,
3899 input_data->x, input_data->len,
3900 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003901 &output_length ),
3902 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003903
3904 if( PSA_SUCCESS == expected_result )
3905 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003906 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003907
Gilles Peskine003a4a92019-05-14 16:09:40 +02003908 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3909 * should be exact. */
3910 TEST_EQUAL( input_data->len,
3911 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3912
Ronald Cron5425a212020-08-04 14:58:35 +02003913 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003914 nonce->x, nonce->len,
3915 additional_data->x,
3916 additional_data->len,
3917 output_data, output_length,
3918 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003919 &output_length2 ),
3920 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003921
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003922 ASSERT_COMPARE( input_data->x, input_data->len,
3923 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003924 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003925
Gilles Peskinea1cac842018-06-11 19:33:02 +02003926exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003927 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003928 mbedtls_free( output_data );
3929 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003930 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003931}
3932/* END_CASE */
3933
3934/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003935void aead_encrypt( int key_type_arg, data_t *key_data,
3936 int alg_arg,
3937 data_t *nonce,
3938 data_t *additional_data,
3939 data_t *input_data,
3940 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003941{
Ronald Cron5425a212020-08-04 14:58:35 +02003942 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003943 psa_key_type_t key_type = key_type_arg;
3944 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003945 unsigned char *output_data = NULL;
3946 size_t output_size = 0;
3947 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003948 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003950 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003951
Gilles Peskine4abf7412018-06-18 16:35:34 +02003952 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003953 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3954 * should be exact. */
3955 TEST_EQUAL( output_size,
3956 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003957 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003958
Gilles Peskine8817f612018-12-18 00:18:46 +01003959 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003960
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3962 psa_set_key_algorithm( &attributes, alg );
3963 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003964
Gilles Peskine049c7532019-05-15 20:22:09 +02003965 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003966 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003967
Steven Cooremand588ea12021-01-11 19:36:04 +01003968 status = psa_aead_encrypt( key, alg,
3969 nonce->x, nonce->len,
3970 additional_data->x, additional_data->len,
3971 input_data->x, input_data->len,
3972 output_data, output_size,
3973 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003974
Ronald Cron28a45ed2021-02-09 20:35:42 +01003975 /* If the operation is not supported, just skip and not fail in case the
3976 * encryption involves a common limitation of cryptography hardwares and
3977 * an alternative implementation. */
3978 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003979 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003980 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3981 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003982 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003983
3984 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003985 ASSERT_COMPARE( expected_result->x, expected_result->len,
3986 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003987
Gilles Peskinea1cac842018-06-11 19:33:02 +02003988exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003989 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003990 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003991 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003992}
3993/* END_CASE */
3994
3995/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003996void aead_decrypt( int key_type_arg, data_t *key_data,
3997 int alg_arg,
3998 data_t *nonce,
3999 data_t *additional_data,
4000 data_t *input_data,
4001 data_t *expected_data,
4002 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004003{
Ronald Cron5425a212020-08-04 14:58:35 +02004004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004005 psa_key_type_t key_type = key_type_arg;
4006 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004007 unsigned char *output_data = NULL;
4008 size_t output_size = 0;
4009 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004010 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004012 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004013 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004014
Gilles Peskine003a4a92019-05-14 16:09:40 +02004015 output_size = input_data->len - tag_length;
4016 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4017 * should be exact. */
4018 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4019 TEST_EQUAL( output_size,
4020 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004021 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004022
Gilles Peskine8817f612018-12-18 00:18:46 +01004023 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004024
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004025 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4026 psa_set_key_algorithm( &attributes, alg );
4027 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004028
Gilles Peskine049c7532019-05-15 20:22:09 +02004029 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004030 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004031
Steven Cooremand588ea12021-01-11 19:36:04 +01004032 status = psa_aead_decrypt( key, alg,
4033 nonce->x, nonce->len,
4034 additional_data->x,
4035 additional_data->len,
4036 input_data->x, input_data->len,
4037 output_data, output_size,
4038 &output_length );
4039
Ronald Cron28a45ed2021-02-09 20:35:42 +01004040 /* If the operation is not supported, just skip and not fail in case the
4041 * decryption involves a common limitation of cryptography hardwares and
4042 * an alternative implementation. */
4043 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004044 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004045 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4046 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004047 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004048
4049 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004050
Gilles Peskine2d277862018-06-18 15:41:12 +02004051 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004052 ASSERT_COMPARE( expected_data->x, expected_data->len,
4053 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004054
Gilles Peskinea1cac842018-06-11 19:33:02 +02004055exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004056 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004057 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004058 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004059}
4060/* END_CASE */
4061
4062/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004063void signature_size( int type_arg,
4064 int bits,
4065 int alg_arg,
4066 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004067{
4068 psa_key_type_t type = type_arg;
4069 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004070 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004071
Gilles Peskinefe11b722018-12-18 00:24:04 +01004072 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004073#if defined(MBEDTLS_TEST_DEPRECATED)
4074 TEST_EQUAL( actual_size,
4075 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4076#endif /* MBEDTLS_TEST_DEPRECATED */
4077
Gilles Peskinee59236f2018-01-27 23:32:46 +01004078exit:
4079 ;
4080}
4081/* END_CASE */
4082
4083/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004084void sign_deterministic( int key_type_arg, data_t *key_data,
4085 int alg_arg, data_t *input_data,
4086 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004087{
Ronald Cron5425a212020-08-04 14:58:35 +02004088 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004089 psa_key_type_t key_type = key_type_arg;
4090 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004091 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004092 unsigned char *signature = NULL;
4093 size_t signature_size;
4094 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004095 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004096
Gilles Peskine8817f612018-12-18 00:18:46 +01004097 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004098
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004099 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004100 psa_set_key_algorithm( &attributes, alg );
4101 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004102
Gilles Peskine049c7532019-05-15 20:22:09 +02004103 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004104 &key ) );
4105 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004106 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004107
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004108 /* Allocate a buffer which has the size advertized by the
4109 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004110 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004111 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004112 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004113 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004114 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004115
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004116 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004117 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004118 input_data->x, input_data->len,
4119 signature, signature_size,
4120 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004121 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004122 ASSERT_COMPARE( output_data->x, output_data->len,
4123 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004124
Gilles Peskine0627f982019-11-26 19:12:16 +01004125#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004126 memset( signature, 0, signature_size );
4127 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004128 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004129 input_data->x, input_data->len,
4130 signature, signature_size,
4131 &signature_length ) );
4132 ASSERT_COMPARE( output_data->x, output_data->len,
4133 signature, signature_length );
4134#endif /* MBEDTLS_TEST_DEPRECATED */
4135
Gilles Peskine20035e32018-02-03 22:44:14 +01004136exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004137 /*
4138 * Key attributes may have been returned by psa_get_key_attributes()
4139 * thus reset them as required.
4140 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004141 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004142
Ronald Cron5425a212020-08-04 14:58:35 +02004143 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004144 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004145 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004146}
4147/* END_CASE */
4148
4149/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004150void sign_fail( int key_type_arg, data_t *key_data,
4151 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004152 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004153{
Ronald Cron5425a212020-08-04 14:58:35 +02004154 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004155 psa_key_type_t key_type = key_type_arg;
4156 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004157 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004158 psa_status_t actual_status;
4159 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004160 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004161 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004163
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004164 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004165
Gilles Peskine8817f612018-12-18 00:18:46 +01004166 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004167
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004168 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004169 psa_set_key_algorithm( &attributes, alg );
4170 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004171
Gilles Peskine049c7532019-05-15 20:22:09 +02004172 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004173 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004174
Ronald Cron5425a212020-08-04 14:58:35 +02004175 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004176 input_data->x, input_data->len,
4177 signature, signature_size,
4178 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004179 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004180 /* The value of *signature_length is unspecified on error, but
4181 * whatever it is, it should be less than signature_size, so that
4182 * if the caller tries to read *signature_length bytes without
4183 * checking the error code then they don't overflow a buffer. */
4184 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004185
Gilles Peskine895242b2019-11-29 12:15:40 +01004186#if defined(MBEDTLS_TEST_DEPRECATED)
4187 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004188 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004189 input_data->x, input_data->len,
4190 signature, signature_size,
4191 &signature_length ),
4192 expected_status );
4193 TEST_ASSERT( signature_length <= signature_size );
4194#endif /* MBEDTLS_TEST_DEPRECATED */
4195
Gilles Peskine20035e32018-02-03 22:44:14 +01004196exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004197 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004198 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004199 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004200 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004201}
4202/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004203
4204/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004205void sign_verify( int key_type_arg, data_t *key_data,
4206 int alg_arg, data_t *input_data )
4207{
Ronald Cron5425a212020-08-04 14:58:35 +02004208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004209 psa_key_type_t key_type = key_type_arg;
4210 psa_algorithm_t alg = alg_arg;
4211 size_t key_bits;
4212 unsigned char *signature = NULL;
4213 size_t signature_size;
4214 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004216
Gilles Peskine8817f612018-12-18 00:18:46 +01004217 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004218
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004220 psa_set_key_algorithm( &attributes, alg );
4221 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004222
Gilles Peskine049c7532019-05-15 20:22:09 +02004223 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004224 &key ) );
4225 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004226 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004227
4228 /* Allocate a buffer which has the size advertized by the
4229 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004230 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004231 key_bits, alg );
4232 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004233 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004234 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004235
4236 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004237 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004238 input_data->x, input_data->len,
4239 signature, signature_size,
4240 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004241 /* Check that the signature length looks sensible. */
4242 TEST_ASSERT( signature_length <= signature_size );
4243 TEST_ASSERT( signature_length > 0 );
4244
4245 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004246 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004247 input_data->x, input_data->len,
4248 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004249
4250 if( input_data->len != 0 )
4251 {
4252 /* Flip a bit in the input and verify that the signature is now
4253 * detected as invalid. Flip a bit at the beginning, not at the end,
4254 * because ECDSA may ignore the last few bits of the input. */
4255 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004256 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004257 input_data->x, input_data->len,
4258 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004259 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004260 }
4261
4262exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004263 /*
4264 * Key attributes may have been returned by psa_get_key_attributes()
4265 * thus reset them as required.
4266 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004267 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004268
Ronald Cron5425a212020-08-04 14:58:35 +02004269 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004270 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004271 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004272}
4273/* END_CASE */
4274
4275/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004276void asymmetric_verify( int key_type_arg, data_t *key_data,
4277 int alg_arg, data_t *hash_data,
4278 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004279{
Ronald Cron5425a212020-08-04 14:58:35 +02004280 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004281 psa_key_type_t key_type = key_type_arg;
4282 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004283 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004284
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004285 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004286
Gilles Peskine8817f612018-12-18 00:18:46 +01004287 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004288
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004289 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004290 psa_set_key_algorithm( &attributes, alg );
4291 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004292
Gilles Peskine049c7532019-05-15 20:22:09 +02004293 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004294 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004295
Ronald Cron5425a212020-08-04 14:58:35 +02004296 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004297 hash_data->x, hash_data->len,
4298 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004299
4300#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004301 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004302 hash_data->x, hash_data->len,
4303 signature_data->x,
4304 signature_data->len ) );
4305
4306#endif /* MBEDTLS_TEST_DEPRECATED */
4307
itayzafrir5c753392018-05-08 11:18:38 +03004308exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004309 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004310 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004311 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004312}
4313/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004314
4315/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004316void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4317 int alg_arg, data_t *hash_data,
4318 data_t *signature_data,
4319 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004320{
Ronald Cron5425a212020-08-04 14:58:35 +02004321 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004322 psa_key_type_t key_type = key_type_arg;
4323 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004324 psa_status_t actual_status;
4325 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004326 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004327
Gilles Peskine8817f612018-12-18 00:18:46 +01004328 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004329
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004330 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004331 psa_set_key_algorithm( &attributes, alg );
4332 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004333
Gilles Peskine049c7532019-05-15 20:22:09 +02004334 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004335 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004336
Ronald Cron5425a212020-08-04 14:58:35 +02004337 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004338 hash_data->x, hash_data->len,
4339 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004340 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004341
Gilles Peskine895242b2019-11-29 12:15:40 +01004342#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004343 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004344 hash_data->x, hash_data->len,
4345 signature_data->x, signature_data->len ),
4346 expected_status );
4347#endif /* MBEDTLS_TEST_DEPRECATED */
4348
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004349exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004350 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004351 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004352 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004353}
4354/* END_CASE */
4355
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004356/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004357void asymmetric_encrypt( int key_type_arg,
4358 data_t *key_data,
4359 int alg_arg,
4360 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004361 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004362 int expected_output_length_arg,
4363 int expected_status_arg )
4364{
Ronald Cron5425a212020-08-04 14:58:35 +02004365 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004366 psa_key_type_t key_type = key_type_arg;
4367 psa_algorithm_t alg = alg_arg;
4368 size_t expected_output_length = expected_output_length_arg;
4369 size_t key_bits;
4370 unsigned char *output = NULL;
4371 size_t output_size;
4372 size_t output_length = ~0;
4373 psa_status_t actual_status;
4374 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004376
Gilles Peskine8817f612018-12-18 00:18:46 +01004377 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004378
Gilles Peskine656896e2018-06-29 19:12:28 +02004379 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004380 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4381 psa_set_key_algorithm( &attributes, alg );
4382 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004383 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004384 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004385
4386 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004387 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004388 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004389 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004390 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004391
4392 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004393 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004394 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004395 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004396 output, output_size,
4397 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004398 TEST_EQUAL( actual_status, expected_status );
4399 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004400
Gilles Peskine68428122018-06-30 18:42:41 +02004401 /* If the label is empty, the test framework puts a non-null pointer
4402 * in label->x. Test that a null pointer works as well. */
4403 if( label->len == 0 )
4404 {
4405 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004406 if( output_size != 0 )
4407 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004408 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004409 input_data->x, input_data->len,
4410 NULL, label->len,
4411 output, output_size,
4412 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004413 TEST_EQUAL( actual_status, expected_status );
4414 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004415 }
4416
Gilles Peskine656896e2018-06-29 19:12:28 +02004417exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004418 /*
4419 * Key attributes may have been returned by psa_get_key_attributes()
4420 * thus reset them as required.
4421 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004422 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004423
Ronald Cron5425a212020-08-04 14:58:35 +02004424 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004425 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004426 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004427}
4428/* END_CASE */
4429
4430/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004431void asymmetric_encrypt_decrypt( int key_type_arg,
4432 data_t *key_data,
4433 int alg_arg,
4434 data_t *input_data,
4435 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004436{
Ronald Cron5425a212020-08-04 14:58:35 +02004437 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004438 psa_key_type_t key_type = key_type_arg;
4439 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004440 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004441 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004442 size_t output_size;
4443 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004444 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004445 size_t output2_size;
4446 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004448
Gilles Peskine8817f612018-12-18 00:18:46 +01004449 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004450
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4452 psa_set_key_algorithm( &attributes, alg );
4453 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004454
Gilles Peskine049c7532019-05-15 20:22:09 +02004455 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004456 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004457
4458 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004459 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004460 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004461 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004462 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004463 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004464 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004465
Gilles Peskineeebd7382018-06-08 18:11:54 +02004466 /* We test encryption by checking that encrypt-then-decrypt gives back
4467 * the original plaintext because of the non-optional random
4468 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004469 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004470 input_data->x, input_data->len,
4471 label->x, label->len,
4472 output, output_size,
4473 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004474 /* We don't know what ciphertext length to expect, but check that
4475 * it looks sensible. */
4476 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004477
Ronald Cron5425a212020-08-04 14:58:35 +02004478 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004479 output, output_length,
4480 label->x, label->len,
4481 output2, output2_size,
4482 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004483 ASSERT_COMPARE( input_data->x, input_data->len,
4484 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004485
4486exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004487 /*
4488 * Key attributes may have been returned by psa_get_key_attributes()
4489 * thus reset them as required.
4490 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004491 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004492
Ronald Cron5425a212020-08-04 14:58:35 +02004493 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004494 mbedtls_free( output );
4495 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004496 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004497}
4498/* END_CASE */
4499
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004500/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004501void asymmetric_decrypt( int key_type_arg,
4502 data_t *key_data,
4503 int alg_arg,
4504 data_t *input_data,
4505 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004506 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004507{
Ronald Cron5425a212020-08-04 14:58:35 +02004508 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004509 psa_key_type_t key_type = key_type_arg;
4510 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004511 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004512 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004513 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004514 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004515
Jaeden Amero412654a2019-02-06 12:57:46 +00004516 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004517 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004518
Gilles Peskine8817f612018-12-18 00:18:46 +01004519 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004520
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004521 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4522 psa_set_key_algorithm( &attributes, alg );
4523 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004524
Gilles Peskine049c7532019-05-15 20:22:09 +02004525 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004526 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004527
Ronald Cron5425a212020-08-04 14:58:35 +02004528 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004529 input_data->x, input_data->len,
4530 label->x, label->len,
4531 output,
4532 output_size,
4533 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004534 ASSERT_COMPARE( expected_data->x, expected_data->len,
4535 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004536
Gilles Peskine68428122018-06-30 18:42:41 +02004537 /* If the label is empty, the test framework puts a non-null pointer
4538 * in label->x. Test that a null pointer works as well. */
4539 if( label->len == 0 )
4540 {
4541 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004542 if( output_size != 0 )
4543 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004544 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004545 input_data->x, input_data->len,
4546 NULL, label->len,
4547 output,
4548 output_size,
4549 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004550 ASSERT_COMPARE( expected_data->x, expected_data->len,
4551 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004552 }
4553
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004554exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004555 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004556 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004557 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004558 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004559}
4560/* END_CASE */
4561
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004562/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004563void asymmetric_decrypt_fail( int key_type_arg,
4564 data_t *key_data,
4565 int alg_arg,
4566 data_t *input_data,
4567 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004568 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004569 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004570{
Ronald Cron5425a212020-08-04 14:58:35 +02004571 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004572 psa_key_type_t key_type = key_type_arg;
4573 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004574 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004575 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004576 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004577 psa_status_t actual_status;
4578 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004580
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004581 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004582
Gilles Peskine8817f612018-12-18 00:18:46 +01004583 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004584
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004585 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4586 psa_set_key_algorithm( &attributes, alg );
4587 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004588
Gilles Peskine049c7532019-05-15 20:22:09 +02004589 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004590 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004591
Ronald Cron5425a212020-08-04 14:58:35 +02004592 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004593 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004594 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004595 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004596 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004597 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004598 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004599
Gilles Peskine68428122018-06-30 18:42:41 +02004600 /* If the label is empty, the test framework puts a non-null pointer
4601 * in label->x. Test that a null pointer works as well. */
4602 if( label->len == 0 )
4603 {
4604 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004605 if( output_size != 0 )
4606 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004607 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004608 input_data->x, input_data->len,
4609 NULL, label->len,
4610 output, output_size,
4611 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004612 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004613 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004614 }
4615
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004616exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004617 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004618 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004619 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004620 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004621}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004622/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004623
4624/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004625void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004626{
4627 /* Test each valid way of initializing the object, except for `= {0}`, as
4628 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4629 * though it's OK by the C standard. We could test for this, but we'd need
4630 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004631 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004632 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4633 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4634 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004635
4636 memset( &zero, 0, sizeof( zero ) );
4637
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004638 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004639 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004640 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004641 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004642 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004643 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004644 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004645
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004646 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004647 PSA_ASSERT( psa_key_derivation_abort(&func) );
4648 PSA_ASSERT( psa_key_derivation_abort(&init) );
4649 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004650}
4651/* END_CASE */
4652
Janos Follath16de4a42019-06-13 16:32:24 +01004653/* BEGIN_CASE */
4654void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004655{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004656 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004657 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004658 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004659
Gilles Peskine8817f612018-12-18 00:18:46 +01004660 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004661
Janos Follath16de4a42019-06-13 16:32:24 +01004662 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004663 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004664
4665exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004667 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004668}
4669/* END_CASE */
4670
Janos Follathaf3c2a02019-06-12 12:34:34 +01004671/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004672void derive_set_capacity( int alg_arg, int capacity_arg,
4673 int expected_status_arg )
4674{
4675 psa_algorithm_t alg = alg_arg;
4676 size_t capacity = capacity_arg;
4677 psa_status_t expected_status = expected_status_arg;
4678 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4679
4680 PSA_ASSERT( psa_crypto_init( ) );
4681
4682 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4683
4684 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4685 expected_status );
4686
4687exit:
4688 psa_key_derivation_abort( &operation );
4689 PSA_DONE( );
4690}
4691/* END_CASE */
4692
4693/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004694void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004695 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004696 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004697 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004698 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004699 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004700 int expected_status_arg3,
4701 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004702{
4703 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004704 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4705 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004706 psa_status_t expected_statuses[] = {expected_status_arg1,
4707 expected_status_arg2,
4708 expected_status_arg3};
4709 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004710 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4711 MBEDTLS_SVC_KEY_ID_INIT,
4712 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004713 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4714 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4715 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004716 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004717 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004718 psa_status_t expected_output_status = expected_output_status_arg;
4719 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004720
4721 PSA_ASSERT( psa_crypto_init( ) );
4722
4723 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4724 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004725
4726 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4727
4728 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4729 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004730 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004731 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004732 psa_set_key_type( &attributes, key_types[i] );
4733 PSA_ASSERT( psa_import_key( &attributes,
4734 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004735 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004736 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4737 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4738 {
4739 // When taking a private key as secret input, use key agreement
4740 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004741 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4742 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004743 expected_statuses[i] );
4744 }
4745 else
4746 {
4747 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004748 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004749 expected_statuses[i] );
4750 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004751 }
4752 else
4753 {
4754 TEST_EQUAL( psa_key_derivation_input_bytes(
4755 &operation, steps[i],
4756 inputs[i]->x, inputs[i]->len ),
4757 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004758 }
4759 }
4760
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004761 if( output_key_type != PSA_KEY_TYPE_NONE )
4762 {
4763 psa_reset_key_attributes( &attributes );
4764 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4765 psa_set_key_bits( &attributes, 8 );
4766 actual_output_status =
4767 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004768 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004769 }
4770 else
4771 {
4772 uint8_t buffer[1];
4773 actual_output_status =
4774 psa_key_derivation_output_bytes( &operation,
4775 buffer, sizeof( buffer ) );
4776 }
4777 TEST_EQUAL( actual_output_status, expected_output_status );
4778
Janos Follathaf3c2a02019-06-12 12:34:34 +01004779exit:
4780 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004781 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4782 psa_destroy_key( keys[i] );
4783 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004784 PSA_DONE( );
4785}
4786/* END_CASE */
4787
Janos Follathd958bb72019-07-03 15:02:16 +01004788/* BEGIN_CASE */
4789void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004790{
Janos Follathd958bb72019-07-03 15:02:16 +01004791 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004792 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004793 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004794 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004795 unsigned char input1[] = "Input 1";
4796 size_t input1_length = sizeof( input1 );
4797 unsigned char input2[] = "Input 2";
4798 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004799 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004800 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004801 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4802 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4803 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004805
Gilles Peskine8817f612018-12-18 00:18:46 +01004806 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004807
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004808 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4809 psa_set_key_algorithm( &attributes, alg );
4810 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004811
Gilles Peskine73676cb2019-05-15 20:15:10 +02004812 PSA_ASSERT( psa_import_key( &attributes,
4813 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004814 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004815
4816 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004817 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4818 input1, input1_length,
4819 input2, input2_length,
4820 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004821 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004822
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004823 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004824 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004825 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004826
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004828
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004830 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004831
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004832exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004834 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004835 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004836}
4837/* END_CASE */
4838
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004839/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004840void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004841{
4842 uint8_t output_buffer[16];
4843 size_t buffer_size = 16;
4844 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004846
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004847 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4848 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004849 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004850
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004852 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004853
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004854 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004855
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004856 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4857 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004858 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004859
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004860 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004861 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004862
4863exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004865}
4866/* END_CASE */
4867
4868/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004869void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004870 int step1_arg, data_t *input1,
4871 int step2_arg, data_t *input2,
4872 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004873 int requested_capacity_arg,
4874 data_t *expected_output1,
4875 data_t *expected_output2 )
4876{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004877 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004878 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4879 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004880 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4881 MBEDTLS_SVC_KEY_ID_INIT,
4882 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004883 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004884 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004885 uint8_t *expected_outputs[2] =
4886 {expected_output1->x, expected_output2->x};
4887 size_t output_sizes[2] =
4888 {expected_output1->len, expected_output2->len};
4889 size_t output_buffer_size = 0;
4890 uint8_t *output_buffer = NULL;
4891 size_t expected_capacity;
4892 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004894 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004895 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004896
4897 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4898 {
4899 if( output_sizes[i] > output_buffer_size )
4900 output_buffer_size = output_sizes[i];
4901 if( output_sizes[i] == 0 )
4902 expected_outputs[i] = NULL;
4903 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004904 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004905 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004906
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004907 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4908 psa_set_key_algorithm( &attributes, alg );
4909 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004910
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004911 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004912 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4913 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4914 requested_capacity ) );
4915 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004916 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004917 switch( steps[i] )
4918 {
4919 case 0:
4920 break;
4921 case PSA_KEY_DERIVATION_INPUT_SECRET:
4922 PSA_ASSERT( psa_import_key( &attributes,
4923 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004924 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004925 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004926 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004927 break;
4928 default:
4929 PSA_ASSERT( psa_key_derivation_input_bytes(
4930 &operation, steps[i],
4931 inputs[i]->x, inputs[i]->len ) );
4932 break;
4933 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004934 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004935
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004937 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004938 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004939 expected_capacity = requested_capacity;
4940
4941 /* Expansion phase. */
4942 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4943 {
4944 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004946 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004947 if( expected_capacity == 0 && output_sizes[i] == 0 )
4948 {
4949 /* Reading 0 bytes when 0 bytes are available can go either way. */
4950 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004951 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004952 continue;
4953 }
4954 else if( expected_capacity == 0 ||
4955 output_sizes[i] > expected_capacity )
4956 {
4957 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004958 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004959 expected_capacity = 0;
4960 continue;
4961 }
4962 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004963 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004964 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004965 ASSERT_COMPARE( output_buffer, output_sizes[i],
4966 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004968 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004969 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004970 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004971 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004972 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004974
4975exit:
4976 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004978 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4979 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004980 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004981}
4982/* END_CASE */
4983
4984/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004985void derive_full( int alg_arg,
4986 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004987 data_t *input1,
4988 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004989 int requested_capacity_arg )
4990{
Ronald Cron5425a212020-08-04 14:58:35 +02004991 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004992 psa_algorithm_t alg = alg_arg;
4993 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004994 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004995 unsigned char output_buffer[16];
4996 size_t expected_capacity = requested_capacity;
4997 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004998 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004999
Gilles Peskine8817f612018-12-18 00:18:46 +01005000 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005001
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005002 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5003 psa_set_key_algorithm( &attributes, alg );
5004 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005005
Gilles Peskine049c7532019-05-15 20:22:09 +02005006 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005007 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005008
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005009 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5010 input1->x, input1->len,
5011 input2->x, input2->len,
5012 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005013 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005014
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005015 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005016 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005017 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005018
5019 /* Expansion phase. */
5020 while( current_capacity > 0 )
5021 {
5022 size_t read_size = sizeof( output_buffer );
5023 if( read_size > current_capacity )
5024 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005025 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005026 output_buffer,
5027 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005028 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005029 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005030 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005031 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005032 }
5033
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005034 /* Check that the operation refuses to go over capacity. */
5035 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005036 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005037
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005038 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005039
5040exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005041 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005042 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005043 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005044}
5045/* END_CASE */
5046
Janos Follathe60c9052019-07-03 13:51:30 +01005047/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005048void derive_key_exercise( int alg_arg,
5049 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005050 data_t *input1,
5051 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005052 int derived_type_arg,
5053 int derived_bits_arg,
5054 int derived_usage_arg,
5055 int derived_alg_arg )
5056{
Ronald Cron5425a212020-08-04 14:58:35 +02005057 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5058 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005059 psa_algorithm_t alg = alg_arg;
5060 psa_key_type_t derived_type = derived_type_arg;
5061 size_t derived_bits = derived_bits_arg;
5062 psa_key_usage_t derived_usage = derived_usage_arg;
5063 psa_algorithm_t derived_alg = derived_alg_arg;
5064 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005065 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005066 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005067 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005068
Gilles Peskine8817f612018-12-18 00:18:46 +01005069 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005070
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005071 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5072 psa_set_key_algorithm( &attributes, alg );
5073 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005074 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005075 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005076
5077 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005078 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5079 input1->x, input1->len,
5080 input2->x, input2->len,
5081 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005082 goto exit;
5083
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005084 psa_set_key_usage_flags( &attributes, derived_usage );
5085 psa_set_key_algorithm( &attributes, derived_alg );
5086 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005087 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005088 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005089 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005090
5091 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005092 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005093 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5094 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005095
5096 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005097 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005098 goto exit;
5099
5100exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005101 /*
5102 * Key attributes may have been returned by psa_get_key_attributes()
5103 * thus reset them as required.
5104 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005105 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005106
5107 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005108 psa_destroy_key( base_key );
5109 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005110 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005111}
5112/* END_CASE */
5113
Janos Follath42fd8882019-07-03 14:17:09 +01005114/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005115void derive_key_export( int alg_arg,
5116 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005117 data_t *input1,
5118 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005119 int bytes1_arg,
5120 int bytes2_arg )
5121{
Ronald Cron5425a212020-08-04 14:58:35 +02005122 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5123 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005124 psa_algorithm_t alg = alg_arg;
5125 size_t bytes1 = bytes1_arg;
5126 size_t bytes2 = bytes2_arg;
5127 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005129 uint8_t *output_buffer = NULL;
5130 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005131 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5132 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005133 size_t length;
5134
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005135 ASSERT_ALLOC( output_buffer, capacity );
5136 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005137 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005138
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005139 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5140 psa_set_key_algorithm( &base_attributes, alg );
5141 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005142 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005143 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005144
5145 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005146 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5147 input1->x, input1->len,
5148 input2->x, input2->len,
5149 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005150 goto exit;
5151
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005152 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005153 output_buffer,
5154 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005156
5157 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005158 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5159 input1->x, input1->len,
5160 input2->x, input2->len,
5161 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005162 goto exit;
5163
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005164 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5165 psa_set_key_algorithm( &derived_attributes, 0 );
5166 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005167 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005169 &derived_key ) );
5170 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005171 export_buffer, bytes1,
5172 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005173 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005174 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005175 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005176 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005177 &derived_key ) );
5178 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005179 export_buffer + bytes1, bytes2,
5180 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005181 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005182
5183 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005184 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5185 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005186
5187exit:
5188 mbedtls_free( output_buffer );
5189 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005191 psa_destroy_key( base_key );
5192 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005193 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005194}
5195/* END_CASE */
5196
5197/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005198void derive_key( int alg_arg,
5199 data_t *key_data, data_t *input1, data_t *input2,
5200 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005201 int expected_status_arg,
5202 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005203{
Ronald Cron5425a212020-08-04 14:58:35 +02005204 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5205 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005206 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005207 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005208 size_t bits = bits_arg;
5209 psa_status_t expected_status = expected_status_arg;
5210 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5211 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5212 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5213
5214 PSA_ASSERT( psa_crypto_init( ) );
5215
5216 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5217 psa_set_key_algorithm( &base_attributes, alg );
5218 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5219 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005220 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005221
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005222 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5223 input1->x, input1->len,
5224 input2->x, input2->len,
5225 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005226 goto exit;
5227
5228 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5229 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005230 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005231 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005232
5233 psa_status_t status =
5234 psa_key_derivation_output_key( &derived_attributes,
5235 &operation,
5236 &derived_key );
5237 if( is_large_output > 0 )
5238 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5239 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005240
5241exit:
5242 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005243 psa_destroy_key( base_key );
5244 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005245 PSA_DONE( );
5246}
5247/* END_CASE */
5248
5249/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005250void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005251 int our_key_type_arg, int our_key_alg_arg,
5252 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005253 int expected_status_arg )
5254{
Ronald Cron5425a212020-08-04 14:58:35 +02005255 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005256 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005257 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005258 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005259 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005261 psa_status_t expected_status = expected_status_arg;
5262 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005263
Gilles Peskine8817f612018-12-18 00:18:46 +01005264 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005265
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005266 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005267 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005268 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005269 PSA_ASSERT( psa_import_key( &attributes,
5270 our_key_data->x, our_key_data->len,
5271 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005272
Gilles Peskine77f40d82019-04-11 21:27:06 +02005273 /* The tests currently include inputs that should fail at either step.
5274 * Test cases that fail at the setup step should be changed to call
5275 * key_derivation_setup instead, and this function should be renamed
5276 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005278 if( status == PSA_SUCCESS )
5279 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005280 TEST_EQUAL( psa_key_derivation_key_agreement(
5281 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5282 our_key,
5283 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005284 expected_status );
5285 }
5286 else
5287 {
5288 TEST_ASSERT( status == expected_status );
5289 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005290
5291exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005292 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005293 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005294 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005295}
5296/* END_CASE */
5297
5298/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005299void raw_key_agreement( int alg_arg,
5300 int our_key_type_arg, data_t *our_key_data,
5301 data_t *peer_key_data,
5302 data_t *expected_output )
5303{
Ronald Cron5425a212020-08-04 14:58:35 +02005304 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005305 psa_algorithm_t alg = alg_arg;
5306 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005307 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005308 unsigned char *output = NULL;
5309 size_t output_length = ~0;
5310
5311 ASSERT_ALLOC( output, expected_output->len );
5312 PSA_ASSERT( psa_crypto_init( ) );
5313
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005314 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5315 psa_set_key_algorithm( &attributes, alg );
5316 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005317 PSA_ASSERT( psa_import_key( &attributes,
5318 our_key_data->x, our_key_data->len,
5319 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005320
Gilles Peskinebe697d82019-05-16 18:00:41 +02005321 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5322 peer_key_data->x, peer_key_data->len,
5323 output, expected_output->len,
5324 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005325 ASSERT_COMPARE( output, output_length,
5326 expected_output->x, expected_output->len );
5327
5328exit:
5329 mbedtls_free( output );
5330 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005331 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005332}
5333/* END_CASE */
5334
5335/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005336void key_agreement_capacity( int alg_arg,
5337 int our_key_type_arg, data_t *our_key_data,
5338 data_t *peer_key_data,
5339 int expected_capacity_arg )
5340{
Ronald Cron5425a212020-08-04 14:58:35 +02005341 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005342 psa_algorithm_t alg = alg_arg;
5343 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005346 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005347 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005348
Gilles Peskine8817f612018-12-18 00:18:46 +01005349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005350
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005351 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5352 psa_set_key_algorithm( &attributes, alg );
5353 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005354 PSA_ASSERT( psa_import_key( &attributes,
5355 our_key_data->x, our_key_data->len,
5356 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005357
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005358 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005359 PSA_ASSERT( psa_key_derivation_key_agreement(
5360 &operation,
5361 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5362 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005363 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5364 {
5365 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005366 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005367 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005368 NULL, 0 ) );
5369 }
Gilles Peskine59685592018-09-18 12:11:34 +02005370
Gilles Peskinebf491972018-10-25 22:36:12 +02005371 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005372 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005373 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005374 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005375
Gilles Peskinebf491972018-10-25 22:36:12 +02005376 /* Test the actual capacity by reading the output. */
5377 while( actual_capacity > sizeof( output ) )
5378 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005379 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005380 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005381 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, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005385 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005386 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005387
Gilles Peskine59685592018-09-18 12:11:34 +02005388exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005389 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005390 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005391 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005392}
5393/* END_CASE */
5394
5395/* BEGIN_CASE */
5396void key_agreement_output( int alg_arg,
5397 int our_key_type_arg, data_t *our_key_data,
5398 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005399 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005400{
Ronald Cron5425a212020-08-04 14:58:35 +02005401 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005402 psa_algorithm_t alg = alg_arg;
5403 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005404 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005406 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005407
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005408 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5409 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005410
Gilles Peskine8817f612018-12-18 00:18:46 +01005411 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005412
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005413 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5414 psa_set_key_algorithm( &attributes, alg );
5415 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005416 PSA_ASSERT( psa_import_key( &attributes,
5417 our_key_data->x, our_key_data->len,
5418 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005419
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005420 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005421 PSA_ASSERT( psa_key_derivation_key_agreement(
5422 &operation,
5423 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5424 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005425 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5426 {
5427 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005428 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005429 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005430 NULL, 0 ) );
5431 }
Gilles Peskine59685592018-09-18 12:11:34 +02005432
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005434 actual_output,
5435 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005436 ASSERT_COMPARE( actual_output, expected_output1->len,
5437 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005438 if( expected_output2->len != 0 )
5439 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005440 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005441 actual_output,
5442 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005443 ASSERT_COMPARE( actual_output, expected_output2->len,
5444 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005445 }
Gilles Peskine59685592018-09-18 12:11:34 +02005446
5447exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005448 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005449 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005450 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005451 mbedtls_free( actual_output );
5452}
5453/* END_CASE */
5454
5455/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005456void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005457{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005458 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005459 unsigned char *output = NULL;
5460 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005461 size_t i;
5462 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005463
Simon Butcher49f8e312020-03-03 15:51:50 +00005464 TEST_ASSERT( bytes_arg >= 0 );
5465
Gilles Peskine91892022021-02-08 19:50:26 +01005466 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005467 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005468
Gilles Peskine8817f612018-12-18 00:18:46 +01005469 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005470
Gilles Peskinea50d7392018-06-21 10:22:13 +02005471 /* Run several times, to ensure that every output byte will be
5472 * nonzero at least once with overwhelming probability
5473 * (2^(-8*number_of_runs)). */
5474 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005475 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005476 if( bytes != 0 )
5477 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005478 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005479
Gilles Peskinea50d7392018-06-21 10:22:13 +02005480 for( i = 0; i < bytes; i++ )
5481 {
5482 if( output[i] != 0 )
5483 ++changed[i];
5484 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005485 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005486
5487 /* Check that every byte was changed to nonzero at least once. This
5488 * validates that psa_generate_random is overwriting every byte of
5489 * the output buffer. */
5490 for( i = 0; i < bytes; i++ )
5491 {
5492 TEST_ASSERT( changed[i] != 0 );
5493 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005494
5495exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005496 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005497 mbedtls_free( output );
5498 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005499}
5500/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005501
5502/* BEGIN_CASE */
5503void generate_key( int type_arg,
5504 int bits_arg,
5505 int usage_arg,
5506 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005507 int expected_status_arg,
5508 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005509{
Ronald Cron5425a212020-08-04 14:58:35 +02005510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005511 psa_key_type_t type = type_arg;
5512 psa_key_usage_t usage = usage_arg;
5513 size_t bits = bits_arg;
5514 psa_algorithm_t alg = alg_arg;
5515 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005517 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005518
Gilles Peskine8817f612018-12-18 00:18:46 +01005519 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005520
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005521 psa_set_key_usage_flags( &attributes, usage );
5522 psa_set_key_algorithm( &attributes, alg );
5523 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005524 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005525
5526 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005527 psa_status_t status = psa_generate_key( &attributes, &key );
5528
5529 if( is_large_key > 0 )
5530 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5531 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005532 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005533 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005534
5535 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005536 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005537 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5538 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005539
Gilles Peskine818ca122018-06-20 18:16:48 +02005540 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005541 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005542 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005543
5544exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005545 /*
5546 * Key attributes may have been returned by psa_get_key_attributes()
5547 * thus reset them as required.
5548 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005549 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005550
Ronald Cron5425a212020-08-04 14:58:35 +02005551 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005552 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005553}
5554/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005555
Gilles Peskinee56e8782019-04-26 17:34:02 +02005556/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5557void generate_key_rsa( int bits_arg,
5558 data_t *e_arg,
5559 int expected_status_arg )
5560{
Ronald Cron5425a212020-08-04 14:58:35 +02005561 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005562 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005563 size_t bits = bits_arg;
5564 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5565 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5566 psa_status_t expected_status = expected_status_arg;
5567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5568 uint8_t *exported = NULL;
5569 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005570 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005571 size_t exported_length = SIZE_MAX;
5572 uint8_t *e_read_buffer = NULL;
5573 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005574 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005575 size_t e_read_length = SIZE_MAX;
5576
5577 if( e_arg->len == 0 ||
5578 ( e_arg->len == 3 &&
5579 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5580 {
5581 is_default_public_exponent = 1;
5582 e_read_size = 0;
5583 }
5584 ASSERT_ALLOC( e_read_buffer, e_read_size );
5585 ASSERT_ALLOC( exported, exported_size );
5586
5587 PSA_ASSERT( psa_crypto_init( ) );
5588
5589 psa_set_key_usage_flags( &attributes, usage );
5590 psa_set_key_algorithm( &attributes, alg );
5591 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5592 e_arg->x, e_arg->len ) );
5593 psa_set_key_bits( &attributes, bits );
5594
5595 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005596 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005597 if( expected_status != PSA_SUCCESS )
5598 goto exit;
5599
5600 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005601 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005602 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5603 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5604 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5605 e_read_buffer, e_read_size,
5606 &e_read_length ) );
5607 if( is_default_public_exponent )
5608 TEST_EQUAL( e_read_length, 0 );
5609 else
5610 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5611
5612 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005613 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005614 goto exit;
5615
5616 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005617 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005618 exported, exported_size,
5619 &exported_length ) );
5620 {
5621 uint8_t *p = exported;
5622 uint8_t *end = exported + exported_length;
5623 size_t len;
5624 /* RSAPublicKey ::= SEQUENCE {
5625 * modulus INTEGER, -- n
5626 * publicExponent INTEGER } -- e
5627 */
5628 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005629 MBEDTLS_ASN1_SEQUENCE |
5630 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005631 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5632 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5633 MBEDTLS_ASN1_INTEGER ) );
5634 if( len >= 1 && p[0] == 0 )
5635 {
5636 ++p;
5637 --len;
5638 }
5639 if( e_arg->len == 0 )
5640 {
5641 TEST_EQUAL( len, 3 );
5642 TEST_EQUAL( p[0], 1 );
5643 TEST_EQUAL( p[1], 0 );
5644 TEST_EQUAL( p[2], 1 );
5645 }
5646 else
5647 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5648 }
5649
5650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005651 /*
5652 * Key attributes may have been returned by psa_get_key_attributes() or
5653 * set by psa_set_key_domain_parameters() thus reset them as required.
5654 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005655 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005656
Ronald Cron5425a212020-08-04 14:58:35 +02005657 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005658 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005659 mbedtls_free( e_read_buffer );
5660 mbedtls_free( exported );
5661}
5662/* END_CASE */
5663
Darryl Greend49a4992018-06-18 17:27:26 +01005664/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005665void persistent_key_load_key_from_storage( data_t *data,
5666 int type_arg, int bits_arg,
5667 int usage_flags_arg, int alg_arg,
5668 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005669{
Ronald Cron71016a92020-08-28 19:01:50 +02005670 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005672 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5673 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005674 psa_key_type_t type = type_arg;
5675 size_t bits = bits_arg;
5676 psa_key_usage_t usage_flags = usage_flags_arg;
5677 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005678 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005679 unsigned char *first_export = NULL;
5680 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005681 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005682 size_t first_exported_length;
5683 size_t second_exported_length;
5684
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005685 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5686 {
5687 ASSERT_ALLOC( first_export, export_size );
5688 ASSERT_ALLOC( second_export, export_size );
5689 }
Darryl Greend49a4992018-06-18 17:27:26 +01005690
Gilles Peskine8817f612018-12-18 00:18:46 +01005691 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005692
Gilles Peskinec87af662019-05-15 16:12:22 +02005693 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005694 psa_set_key_usage_flags( &attributes, usage_flags );
5695 psa_set_key_algorithm( &attributes, alg );
5696 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005697 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005698
Darryl Green0c6575a2018-11-07 16:05:30 +00005699 switch( generation_method )
5700 {
5701 case IMPORT_KEY:
5702 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005703 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005704 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005705 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005706
Darryl Green0c6575a2018-11-07 16:05:30 +00005707 case GENERATE_KEY:
5708 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005709 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005710 break;
5711
5712 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005713#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005714 {
5715 /* Create base key */
5716 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5717 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5718 psa_set_key_usage_flags( &base_attributes,
5719 PSA_KEY_USAGE_DERIVE );
5720 psa_set_key_algorithm( &base_attributes, derive_alg );
5721 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005722 PSA_ASSERT( psa_import_key( &base_attributes,
5723 data->x, data->len,
5724 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005725 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005726 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005727 PSA_ASSERT( psa_key_derivation_input_key(
5728 &operation,
5729 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005730 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005731 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005732 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005733 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5734 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005735 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005736 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005737 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005738 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005739 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005740#else
5741 TEST_ASSUME( ! "KDF not supported in this configuration" );
5742#endif
5743 break;
5744
5745 default:
5746 TEST_ASSERT( ! "generation_method not implemented in test" );
5747 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005748 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005749 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005750
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005751 /* Export the key if permitted by the key policy. */
5752 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5753 {
Ronald Cron5425a212020-08-04 14:58:35 +02005754 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005755 first_export, export_size,
5756 &first_exported_length ) );
5757 if( generation_method == IMPORT_KEY )
5758 ASSERT_COMPARE( data->x, data->len,
5759 first_export, first_exported_length );
5760 }
Darryl Greend49a4992018-06-18 17:27:26 +01005761
5762 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005763 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005764 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005765 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005766
Darryl Greend49a4992018-06-18 17:27:26 +01005767 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005768 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005769 TEST_ASSERT( mbedtls_svc_key_id_equal(
5770 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005771 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5772 PSA_KEY_LIFETIME_PERSISTENT );
5773 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5774 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5775 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5776 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005777
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005778 /* Export the key again if permitted by the key policy. */
5779 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005780 {
Ronald Cron5425a212020-08-04 14:58:35 +02005781 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005782 second_export, export_size,
5783 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005784 ASSERT_COMPARE( first_export, first_exported_length,
5785 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005786 }
5787
5788 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005789 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005790 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005791
5792exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005793 /*
5794 * Key attributes may have been returned by psa_get_key_attributes()
5795 * thus reset them as required.
5796 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005797 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005798
Darryl Greend49a4992018-06-18 17:27:26 +01005799 mbedtls_free( first_export );
5800 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005801 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005802 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005803 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005804 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005805}
5806/* END_CASE */